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

    From KDE TechBase
    (Created page with '{{Template:I18n/Language Navigation Bar|Development/Architecture/KDE4/Starting Other Programs}} '''KDE 架構 - 啟動程式''' 在 KDE 中,有許多方法可以讓您的應...')
     
    No edit summary
    Line 6: Line 6:


    == fork + exec ==
    == fork + exec ==
    You never want to use this unless you have a very good reason why it is impossible to use KProcess.
     
    不要使用他們。除非你有很好的理由,是不能使用 KProcess 的。


    == system ==
    == system ==
    Line 12: Line 13:


    == KProcess ==
    == KProcess ==
    You want to use [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKProcess.html KProcess] class if you need to start a new process which needs to be a child of your process, e.g. because you want to catch stdout/stderr or need to send it data via stdin. You should never use this to start other KDE applications (unless your application is a debugger :-)
     
    只有在你要啟動子行程(process)的時候,才使用[http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKProcess.html KProcess]。例如,你要取得 stdout/stderr或需要透過 stdin 發送資料。您不應該用它來啟動其他 KDE 應用程式(除非您的應用程式是除錯器 :-)


    == KToolInvocation::startServiceByDesktopPath ==
    == KToolInvocation::startServiceByDesktopPath ==
    [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html KToolInvocation::startServiceByDesktopPath] is the preferred way to launch desktop (KDE/Gnome/X) applications or KDE services. The application/service must have a .desktop file, and you must know the name of that file. KToolInvocation asks the klauncher process, via DBUS, to start the program.
     
    [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html KToolInvocation::startServiceByDesktopPath] 是啟動桌面(KDE/Gnome/X)應用程式或 KDE 服務的最佳方式。應用程式或服務必須有 .desktop 檔,而且你必須知道檔名。KToolInvocation 會經由 DBUS 告訴 KLauncher 行程要啟動應用程式。
     
    === klauncher ===
    === klauncher ===
    klauncher will wait until the application registers to DBUS if the desktop file specifies the [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html#_details 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.<i>binaryname</i>, 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.
    klauncher will wait until the application registers to DBUS if the desktop file specifies the [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html#_details 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.<i>binaryname</i>, 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 ===  
    === 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).
    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 ==
    == KRun ==
    Generic way to open documents/applications/shell commands. Uses startServiceByDesktopPath where applicable. Offers the additional benefit of startup-notification.<br/> [http://api.kde.org/4.x-api/kdelibs-apidocs/kio/html/classKRun.html KRun] can start any application, from the binary or the desktop file, it will determine the mimetype of a file before running the preferred handler for it, and it can also start shell commands. This makes KRun the recommended way to run another program in KDE.
     
    打開文件、應用程式、Shell指令的一般方法。適當的使用 startServiceByDesktopPath,提供額外的啟動通知好處。<br/> [http://api.kde.org/4.x-api/kdelibs-apidocs/kio/html/classKRun.html KRun] 可以從二進位檔或 desktop檔啟動任何應用程式。他會判斷檔案的 MIME 類型,再執行最佳的處理方式,這同樣可以啟動 Shell 指令。This makes KRun the recommended way to run another program in KDE.


    == KToolInvocation::invokeBrowser ==
    == KToolInvocation::invokeBrowser ==

    Revision as of 15:48, 1 December 2009


    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.