Archive:Development/Architecture/KDE4/Starting Other Programs (zh TW)

    From KDE TechBase
    Revision as of 15:49, 1 December 2009 by Alisha (talk | contribs)


    Development/Architecture/KDE4/Starting Other Programs


    KDE 架構 - 啟動程式

    在 KDE 中,有許多方法可以讓您的應用程式啟動其他程式。這裡總結了一些你應該和不應該使用的方法和理由。

    fork + exec

    不要使用他們。除非你有很好的理由,是不能使用 KProcess 的。

    system

    Other than the reasons about why you should not use fork + exec, system is also synchronous, thus it will block the application that run it.

    KProcess

    只有在你要啟動子行程(process)的時候,才使用KProcess。例如,你要取得 stdout/stderr或需要透過 stdin 發送資料。您不應該用它來啟動其他 KDE 應用程式(除非您的應用程式是除錯器 :-)

    KToolInvocation::startServiceByDesktopPath

    KToolInvocation::startServiceByDesktopPath 是啟動桌面(KDE/Gnome/X)應用程式或 KDE 服務的最佳方式。應用程式或服務必須有 .desktop 檔,而且你必須知道檔名。KToolInvocation 會經由 DBUS 告訴 KLauncher 行程要啟動應用程式。

    klauncher

    klauncher will wait until the application registers to DBUS if the desktop file specifies the X-DBUS-StartupType key. This allows to make DBUS calls to the application once startServiceByDesktopPath returns, without the risk of race conditions. Note that klauncher needs to know under which name the application will register to DBUS: by default that is assumed to be org.kde.binaryname, but you can configure this by setting X-DBUS-ServiceName in the .desktop file. If you don't, you will get an error from klauncher which will think the application failed to start, since the expected dbus name never showed up.

    kdeinit

    klauncher makes use of KDEinit for increased startup performance and lower memory usage. These benefits only apply to applications available as KDEinit loadable module (KLM).

    KRun

    打開文件、應用程式、Shell指令的一般方法。適當的使用 startServiceByDesktopPath,可以提供額外的啟動通知好處。
    KRun 可以從二進位檔或 desktop檔啟動任何應用程式。他會判斷檔案的 MIME 類型,再執行最佳的處理方式,這同樣可以啟動 Shell 指令。This makes KRun the recommended way to run another program in KDE.

    KToolInvocation::invokeBrowser

    KToolInvocation::invokeBrowser launches a web browser. The difference with using the more generic KRun on the webpage URL is that KRun has to determine the mimetype of the URL first (which, for HTTP, involves starting a download to read the headers), so if you know that the URL is an HTML webpage, use invokeBrowser, it will be faster.

    More details: the problem with KRun for webpages is that it delays the appearance of the browser window, and if the user's preferred browser is a non-kde application like firefox then it has to start a second download while konqueror which can reuse the kioslave started by KRun. On the other hand if the URL might be an image or anything else than html, then KRun is the right solution, so that the right application is started.