Difference between revisions of "Development/Architecture/KDE4/Starting Other Programs"

Jump to: navigation, search
(explain klauncher and dbus startuptype/servicename)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
 +
 
'''KDE Architecture - Starting programs'''
 
'''KDE Architecture - Starting programs'''
  
Line 7: Line 9:
 
== 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.
 +
 +
== 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 ==
 
== KProcess ==
Line 14: Line 19:
 
[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] 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.  
 
=== 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/classKService.html#6419c30c7fab9341c1df192621f5953a 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).
Line 22: Line 27:
  
 
== 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 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).
+
[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.
 +
 
 +
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.
  
 
[[Category:KDE4]]
 
[[Category:KDE4]]
 
[[Category:Architecture]]
 
[[Category:Architecture]]

Latest revision as of 19:24, 15 July 2012


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.

Contents

[edit] fork + exec

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

[edit] 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.

[edit] 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 :-)

[edit] 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. KToolInvocation asks the klauncher process, via DBUS, to start the program.

[edit] 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.

[edit] 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).

[edit] 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.

[edit] 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.


This page was last modified on 15 July 2012, at 19:24. This page has been accessed 9,098 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal