Difference between revisions of "Archive:Development/Architecture/KDE4/Starting Other Programs (zh CN)"

Jump to: navigation, search
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Architecture/KDE4/Starting Other Programs}}
+
 
  
 
'''KDE 架构 - 程序启动'''
 
'''KDE 架构 - 程序启动'''
Line 9: Line 9:
  
 
== 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 :-)
+
只有在你要启动一个子进程的时候,才使用[http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKProcess.html KProcess]。这时候你要捕捉系统输出或者错误,或者由系输入的数据。这不是启动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.
+
如果.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>binaryname</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可用模块(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方法,会有更多的启动优势。[http://api.kde.org/4.x-api/kdelibs-apidocs/kio/html/classKRun.html KRun]可以启动任何程序,从二进制文件到桌面文件。在运行之前,他会自动判断文件的MIME类型从而调用合适的处理方式,同样这适用于Shell命令。这些特性就是我们建议使用KRun在KDE调用程序的原因。
  
 
== 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,他会首先根据MIME类型决定如何加载(如果是HTTP,那么他会现下载,在调用相应程序加载。)。如果你事先就知道他是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.
+
更多内容: 对KRun来说,网页因该在浏览器里显示,如果用户所选的浏览器是非KDE应用程序, 象火狐(Firefox)等,系统不得不启动另一轮下载来得到该网页,而Konqueror就可以利用KRun所设定的kioslave。另一方面,如果URL是一幅图像或其他的,反正不是网页,那KRun就是个合理地选择了,合适的应用程序会启动的。
  
 
[[Category:KDE4]]
 
[[Category:KDE4]]
 
[[Category:Architecture]]
 
[[Category:Architecture]]

Revision as of 17:51, 17 July 2012


KDE 架构 - 程序启动

KDE中有许多启动程序的方法。这里列举了一些你应该和不应该用的方法。

Contents

fork + exec

不要用他们,除非你有很好的理由,KProcess不能完成你想的工作。

KProcess

只有在你要启动一个子进程的时候,才使用KProcess。这时候你要捕捉系统输出或者错误,或者由系输入的数据。这不是启动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.binaryname, 但你也可以在.desktop文件里设置X-DBUS-ServiceName值。如果你不这么做,KLauncher会抛出一个错误,说这个应用程序启动不了,因为所期望的DBUS名字就没出现过。

kdeinit

KLauncher利用KDEinit来提高启动性能和降低内存使用。这只适用于KDEinit可用模块(KLM)。

KRun

打开文件、应用程序、Shell命令的通用方法。尽可能使用startServiceByDesktopPath方法,会有更多的启动优势。KRun可以启动任何程序,从二进制文件到桌面文件。在运行之前,他会自动判断文件的MIME类型从而调用合适的处理方式,同样这适用于Shell命令。这些特性就是我们建议使用KRun在KDE调用程序的原因。

KToolInvocation::invokeBrowser

KToolInvocation::invokeBrowser 将加载浏览器。 相对于KRun,他会首先根据MIME类型决定如何加载(如果是HTTP,那么他会现下载,在调用相应程序加载。)。如果你事先就知道他是HTML网页,直接用invokeBrowser会快很多。

更多内容: 对KRun来说,网页因该在浏览器里显示,如果用户所选的浏览器是非KDE应用程序, 象火狐(Firefox)等,系统不得不启动另一轮下载来得到该网页,而Konqueror就可以利用KRun所设定的kioslave。另一方面,如果URL是一幅图像或其他的,反正不是网页,那KRun就是个合理地选择了,合适的应用程序会启动的。


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal