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

    From KDE TechBase
    No edit summary
    No edit summary
    Line 10: Line 10:


    == system ==
    == 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.
     
    除了不該使用 fork + exec 的原因外,system 還有同步的問題,因此它會阻礙應用程式執行。


    == KProcess ==
    == KProcess ==
    Line 21: Line 22:


    === 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.
    如果 desktop 檔指定了[http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html#_details X-DBUS-StartupType]鍵,那 KLauncher 將一直等到應用程式註冊到 DBUS 上。一旦 startServiceByDesktopPath 返回,DBUS才會呼叫用程式,而避免了競爭的風險。注意 KLauncher 需要知道應用程式註冊 DBUS 的名稱;預設是 org.kde.<i>二進位檔名</i>,但你也可以透過 .desktop 檔裡的 X-DBUS-ServiceName 配置他。如果你不這麼做,KLauncher 會拋出一個錯誤,說這個應用程式無法啟動,因為期望的 dbus 名稱沒有出現。


    === 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 利用 KDEinit 來提高啟動性能和降低記憶體使用量。這只適合應用程式可用於 KDEinit loadable module(KLM)時。


    == KRun ==
    == KRun ==


    打開文件、應用程式、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.
    打開文件、應用程式、Shell指令的一般方法。適當的使用 startServiceByDesktopPath,可以提供額外的啟動通知好處。<br/> [http://api.kde.org/4.x-api/kdelibs-apidocs/kio/html/classKRun.html KRun] 可以從二進位檔或 desktop 檔啟動任何應用程式。他會判斷檔案的 MIME 類型,再執行最佳的處理方式,這同樣可以啟動 Shell 指令。這就是我們推薦使用 KRun 執行其他程式的原因。


    == KToolInvocation::invokeBrowser ==
    == KToolInvocation::invokeBrowser ==
    [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html 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.
    [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html KToolInvocation::invokeBrowser] 將啟動網頁瀏覽器。相較於通用性的 KRun,網頁 URL KRun 會先測定 URL 的 MIME類型(如果是HTTP,那麼他會開始下載,並讀取標頭)。如果你已經知道 URL 是一個 HTML 網頁,直接用 invokeBrowser 會快很多。


    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.
    更多細節: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.


    [[Category:KDE4]]
    [[Category:KDE4]]
    [[Category:Architecture]]
    [[Category:Architecture]]

    Revision as of 03:31, 3 December 2009


    Development/Architecture/KDE4/Starting Other Programs


    KDE 架構 - 啟動程式

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

    fork + exec

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

    system

    除了不該使用 fork + exec 的原因外,system 還有同步的問題,因此它會阻礙應用程式執行。

    KProcess

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

    KToolInvocation::startServiceByDesktopPath

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

    klauncher

    如果 desktop 檔指定了X-DBUS-StartupType鍵,那 KLauncher 將一直等到應用程式註冊到 DBUS 上。一旦 startServiceByDesktopPath 返回,DBUS才會呼叫用程式,而避免了競爭的風險。注意 KLauncher 需要知道應用程式註冊 DBUS 的名稱;預設是 org.kde.二進位檔名,但你也可以透過 .desktop 檔裡的 X-DBUS-ServiceName 配置他。如果你不這麼做,KLauncher 會拋出一個錯誤,說這個應用程式無法啟動,因為期望的 dbus 名稱沒有出現。

    kdeinit

    klauncher 利用 KDEinit 來提高啟動性能和降低記憶體使用量。這只適合應用程式可用於 KDEinit loadable module(KLM)時。

    KRun

    打開文件、應用程式、Shell指令的一般方法。適當的使用 startServiceByDesktopPath,可以提供額外的啟動通知好處。
    KRun 可以從二進位檔或 desktop 檔啟動任何應用程式。他會判斷檔案的 MIME 類型,再執行最佳的處理方式,這同樣可以啟動 Shell 指令。這就是我們推薦使用 KRun 執行其他程式的原因。

    KToolInvocation::invokeBrowser

    KToolInvocation::invokeBrowser 將啟動網頁瀏覽器。相較於通用性的 KRun,網頁 URL 在 KRun 會先測定 URL 的 MIME類型(如果是HTTP,那麼他會開始下載,並讀取標頭)。如果你已經知道 URL 是一個 HTML 網頁,直接用 invokeBrowser 會快很多。

    更多細節: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.