Development/Architecture/KDE4/Starting Other Programs: Difference between revisions

    From KDE TechBase
    (Upgraded to KDE4, added invokeBrowser.)
     
    m (fix formatting, using more standard == for headings; couldn't find any docu on the semicolon/colon syntax previously used.)
    Line 5: Line 5:
    you should or should not use them.
    you should or should not use them.


    ;fork + exec
    == fork + exec ==
    :You never want to use this unless you have a very good reason why it is impossible to use KProcess.
    You never want to use this unless you have a very good reason why it is impossible to use KProcess.


    ;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 :-)
    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 :-)


    ;KToolInvocation::startServiceByDesktopPath
    == KToolInvocation::startServiceByDesktopPath ==
    [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html]
    [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. It will make use of KDEinit for increased startup performance and lower memory usage. These benefits only apply to applications available as KDEinit loadable module (KLM).
    :Preferred way to launch desktop (KDE/Gnome/X) applications or KDE services. The application/service must have a .desktop file. It will make 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.
    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.


    ;KToolInvocation::invokeBrowser
    == KToolInvocation::invokeBrowser ==
    [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html]
    [http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKToolInvocation.html KToolInvocation::invokeBrowser] launches a web browser. The difference with KRun 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 in order to skip that step and let the browser window show faster (the mimetype check is especially annoying if the user's preferred browser is a non-kde application like firefox, rather than konqueror, which can reuse the kioslave started by KRun).
    Launches a web browser. The difference with KRun 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 in order to skip that step and let the  
    browser window show faster (the mimetype check is especially annoying if the user's preferred browser is a non-kde application like firefox, rather than konqueror, which can reuse the kioslave started by KRun).


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

    Revision as of 11:45, 16 December 2008

    KDE Architecture - Starting programs

    In KDE there are several ways to start other programs from within your application. Here is a short summary of your options with reasons why you should or should not use them.

    fork + exec

    You never want to use this unless you have a very good reason why it is impossible to use KProcess.

    KProcess

    You want to use 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 :-)

    KToolInvocation::startServiceByDesktopPath

    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. It will make use of KDEinit for increased startup performance and lower memory usage. These benefits only apply to applications available as KDEinit loadable module (KLM).

    KRun

    Generic way to open documents/applications/shell commands. Uses startServiceByDesktopPath where applicable. Offers the additional benefit of startup-notification.
    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.

    KToolInvocation::invokeBrowser

    KToolInvocation::invokeBrowser launches a web browser. The difference with KRun 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 in order to skip that step and let the browser window show faster (the mimetype check is especially annoying if the user's preferred browser is a non-kde application like firefox, rather than konqueror, which can reuse the kioslave started by KRun).