Archive:Development/Tutorials/Session Management (zh CN): Difference between revisions

From KDE TechBase
No edit summary
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/Session Management}}
 


{{TutorialBrowser (zh_CN)|
{{TutorialBrowser (zh_CN)|
Line 18: Line 18:
从KDE 1.0起,遗留的X11R4和ICCCM会话管理协议就被支持着。Legacy applications that define the <tt>WM_COMMAND</tt>
从KDE 1.0起,遗留的X11R4和ICCCM会话管理协议就被支持着。Legacy applications that define the <tt>WM_COMMAND</tt>
property or support the <tt>WM_SAVE_YOURSELF</tt> protocol will be restarted
property or support the <tt>WM_SAVE_YOURSELF</tt> protocol will be restarted
with the specified command. The window geometries will be restored on a best
with the specified command. 窗口坐标会以最佳效果恢复。
effort basis.


Since KDE 2.0, also the standard X11R6 session management protocol XSMP
从KDE 2.0起,X11R6会话管理标准,XSMP也被广泛支持和使用。该标准的官方文档可以从X Consortium的FTP服务器上下载
is supported and used. The official documentation of the
[http://stuff.mit.edu/afs/sipb/contrib/doc/X11/hardcopy/SM/xsmp.PS.gz ftp.x.org]。与其他遗留协议不同,新的X11R6会话管理允许在登出后保存应用无关设置。例如对一个文本编辑器,可以保存已加载的文件名,并会您再次登录加载它们。另一项新协议的主要的应用是支持干净和安全的注销流程,即使用户决定下一次不恢复会话。该协议使应用与用户之间的交互变得可能,特别是在一些丢失数据的危险情况,如果有必要的话可以取消关机进程。
standard can be download from the X Consortium's FTP server
[http://stuff.mit.edu/afs/sipb/contrib/doc/X11/hardcopy/SM/xsmp.PS.gz ftp.x.org]. Unlike the legacy protocols, the new X11R6
session management gives a chance to save application
dependent settings when you log out.  A text
editor, for instance, would save the names of the loaded files and would
reload them when you log in again.  Another major advantage of the new
protocol is the support for a clean and safe logout procedure even if the
users decides not to restore the session next time. The protocol gives
applications the possibility to interact with the user in case they are in
danger to lose some data, and to cancel the shutdown process if necessary.


==更多阅读==
==更多阅读==


An introductive overview of session management functionality and
[http://doc.trolltech.com/4.4/session.html doc.trolltech.com],有更多关于会话管理功能和QT API的介绍.
the Qt API for it is available from [http://doc.trolltech.com/4.4/session.html doc.trolltech.com].


In KDE, the classes [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKApplication.html KApplication]
KDE里, [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKApplication.html KApplication]
and [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow]
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow]类对程序员隐藏了许多不必要的细节。基本上,一个
hide all the ugly details from the programmer. Basically, a
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKApplication.html KApplication]为你管理一个
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKApplication.html KApplication] manages a
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKConfig.html KConfig] 配置对象(通过
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdecore/html/classKConfig.html KConfig] configuration object (available trought
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKApplication.html#8f88369c240d6d90a04d29b2761989d9 KApplication::sessionConfig()])
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKApplication.html#8f88369c240d6d90a04d29b2761989d9 KApplication::sessionConfig()])
for you, that your application can utilize to store session specific data.
,这样你的应用就可以保存一些会话专用数据了。


Please read the respective class documentation, especially the one of
Please read the respective class documentation, especially the one of
Line 54: Line 41:
==在你的应用中实现会话管理==
==在你的应用中实现会话管理==


Here's just a brief overview how things are done. Again, see the
这里只是一个概要描述。详细内容见相关文档。
respective class documentation for details.


Implementing session management in KDE is easy. If your main window inherits from [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow], you have just 2 things to do:
实现会话管理并不难。如果你的窗口从[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow]继承,你只要做两件事:
* Reimplement some virtual functions of [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow].
* 实现几个[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow]的虚函数。
* Add session management support to your main() function.
* 在main()添加会话管理。
That's all.
就这样。


===重写KMainWindow的虚函数===
===重写KMainWindow的虚函数===
Line 68: Line 54:
For every other data that your application needs to restore a session, you have to write the code for saving and restoring yourself. However, [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow] makes this task easy. You can just re-implement the following functions:
For every other data that your application needs to restore a session, you have to write the code for saving and restoring yourself. However, [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow] makes this task easy. You can just re-implement the following functions:
* [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#4c7a6c395eec0bb245cd9ad6c884f897 saveProperties()]: This function is called only when the application is closed by the session manager (and not, when the application is closed by the user). To save the data that you need to restore the session, reimplement this function. (For a text editor for example, that would be the list of loaded files) Note that no user interaction is allowed in this function! (For example, you may not display any dialog!)
* [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#4c7a6c395eec0bb245cd9ad6c884f897 saveProperties()]: This function is called only when the application is closed by the session manager (and not, when the application is closed by the user). To save the data that you need to restore the session, reimplement this function. (For a text editor for example, that would be the list of loaded files) Note that no user interaction is allowed in this function! (For example, you may not display any dialog!)
* [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#c8d0d64ed5b309ba1da410423120d0a6 readProperties()]: To read the additional data again on next login, reimplement this function.
* [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#c8d0d64ed5b309ba1da410423120d0a6 readProperties()]: 下次登录是读入额外数据,重写这个函数。


Furthermore, the function [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#f8c5708414be62f259114b0453ef8432 queryClose()] could be interesting for you. This function is called ''always'' before the window is closed, either by the user or indirectly by the session manager. Typically, here you can warn the user that the application or some windows have unsaved data on close or logout (for example: show a dialog with the buttons "Save changes" and "Discard changes"). However, for session management it isn't nice to request a user interaction before closing, so you better avoid this. Note that it is not determined if [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#4c7a6c395eec0bb245cd9ad6c884f897 saveProperties()] is called before or after [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#f8c5708414be62f259114b0453ef8432 queryClose()]!
另外,[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#f8c5708414be62f259114b0453ef8432 queryClose()]也会让你有所兴趣。这个函数''一直''在窗口关闭之前被调用, 或是用户调用或是会话管理器调用。通常,你可以在这里提醒用户应用或窗口在退出关闭时有未保存的数据(例如: 跳出一个对话框提醒"保存变更" "忽略变更")。然而,对有些会话管理来说,退出前要用户确认并不是件好事,你也可以避免它的发生。注意:[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#4c7a6c395eec0bb245cd9ad6c884f897 saveProperties()] [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#f8c5708414be62f259114b0453ef8432 queryClose()]之前或之后调用并不起决定作用!


''To save your application-wide properties (data that is only needed once per application, and not for each main window instance) reimplement [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#cf811d77a3acdcf2b61f8826429615a7 saveGlobalProperties()] and it's counterpart [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#2d4da4f305f00e8a5c94f5b978334231 readGlobalProperties].  Normally, you don't need these functions.''
''To save your application-wide properties (data that is only needed once per application, and not for each main window instance) reimplement [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#cf811d77a3acdcf2b61f8826429615a7 saveGlobalProperties()] and it's counterpart [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#2d4da4f305f00e8a5c94f5b978334231 readGlobalProperties].  Normally, you don't need these functions.''
Line 83: Line 69:
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow])。在你的main()函数内,要加上下列代码来创建/恢复窗口:
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow])。在你的main()函数内,要加上下列代码来创建/恢复窗口:


<code cppqt>
<syntaxhighlight lang="cpp-qt">
KApplication app;
KApplication app;
if ( app.isSessionRestored() ) {
if ( app.isSessionRestored() ) {
   kRestoreMainWindows< MyWindow >();
   kRestoreMainWindows< MyWindow >();
} else {
} else {
   // create default application as usual
   // 像往常一样创建缺省应用
   // for example:
   // 例如:
   MyWindow * window = new MyWindow();
   MyWindow * window = new MyWindow();
   // # will be replaced with numbers that are guaranteed
   // #会被替换为一个数字以确保在应用系统内唯一:
  // to be unique in the application:
   window->setObjectName("MyWindow#");
   window->setObjectName("MyWindow#");
   window->show();
   window->show();
}
}
return app.exec();
return app.exec();
</code>
</syntaxhighlight>


[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/kmainwindow_8h.html#88ce427e39f425eefa5a94d746eb2bed kRestoreMainWindows<>()] will create (on the heap) as many instances of your main windows as have existed in the last session and call [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#46e01bd1aa6d488f1be2a5010030efb2 KMainWindow::restore()] with the correct arguments. Note that also QWidget::show() is called implicitly.
[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/kmainwindow_8h.html#88ce427e39f425eefa5a94d746eb2bed kRestoreMainWindows<>()] 会(在堆上)创建as many instances of your main windows as have existed in the last session and call [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#46e01bd1aa6d488f1be2a5010030efb2 KMainWindow::restore()] with the correct arguments. 注意QWidget::show()是隐式调用的。


关于setObjectName("MyWindow#"): For session management and window management to work properly, all main windows in the application should have a different name. If you don't do it, KMainWindow will create a unique name, but it's recommended to explicitly pass a window name that will also describe the type of the window. If there can be several windows of the same type, append '#' (hash) to the name, and KMainWindow will replace it with numbers to make the names unique. For example, for a mail client which has one main window showing the mails and folders, and which can also have one or more windows for composing mails, the name for the folders window should be e.g. "mainwindow" and for the composer windows "composer#".
关于setObjectName("MyWindow#"): 为让会话管理和窗口管理的正常工作,所有的应用程序主窗口应该有一个各不相同的名称。如果你不这样做,KMainWindow会创建一个唯一的名称,但它建议明确传递一个能描述窗口类型的名称。如果能够有同类型的几个窗口,请将''(散列值)加到名称上,同时KMainWindow将会用数字替代,使这个名称在应用中唯一。例如, for a mail client which has one main window showing the mails and folders, and which can also have one or more windows for composing mails, the name for the folders window should be e.g. "mainwindow" and for the composer windows "composer#".


With this you can easily restore all toplevel windows of your application.
这样你就可以简单的恢复所有应用的顶层窗口了。


It is also possible to restore different types of toplevel windows (each
他也同样适用于一个应用中的其他各类顶层窗口(只要从[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow]继承就行)。如果你的主窗口有三个类:childMW1, childMW2 and childMW3:
derived from [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html KMainWindow], of course) within one application. Imagine you have three classes of main windows: childMW1, childMW2 and childMW3:


<code cppqt>
<syntaxhighlight lang="cpp-qt">
KApplication app;
KApplication app;
if ( app.isSessionRestored() ) {
if ( app.isSessionRestored() ) {
   kRestoreMainWindows< childMW1, childMW2, childMW3 >();
   kRestoreMainWindows< childMW1, childMW2, childMW3 >();
} else {
} else {
   // create default application as usual
   // 像往常一样创建缺省应用
   // for example:
   // 例如:
   childMW1* window1 = new childMW1();
   childMW1* window1 = new childMW1();
   childMW2* window2 = new childMW2();
   childMW2* window2 = new childMW2();
   childMW3* window3 = new childMW3();
   childMW3* window3 = new childMW3();
   // # will be replaced with numbers that are guaranteed
   // #会被替换为一个数字以确保在应用系统内唯一:
  // to be unique in the application:
   window1->setObjectName("type1mainWindow#");
   window1->setObjectName("type1mainWindow#");
   window2->setObjectName("type2mainWindow#");
   window2->setObjectName("type2mainWindow#");
Line 128: Line 111:
}
}
return app.exec();
return app.exec();
</code>
</syntaxhighlight>


Currently, the [http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/kmainwindow_8h.html#88ce427e39f425eefa5a94d746eb2bed kRestoreMainWindows<>()] template functions are provided for up to three template arguments.
当前,[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/kmainwindow_8h.html#88ce427e39f425eefa5a94d746eb2bed kRestoreMainWindows<>()] 函数模板提供了至多三个参数。


==附录: KDE会话管理的架构==
==附录: KDE会话管理的架构==


The session management server in KDE is called '''ksmserver''' and it is
在KDE中,会话管理服务器称为'''KSMServer''',它是在'''KDEBbase'''包的一部分。
part of the '''kdebase''' package.  The server interacts with the KDE window
'''KSMServer'''服务器与与KDE的窗口管理器'''KWin'''相互作用,以保存和还原窗口的几何尺寸和执行旧会话管理。为了使会话管理有效,'''KSMSserver'''必须是X登录过程的最后一个进程。这会自动在'''startkde'''脚本的末尾执行。
manager '''kwin''' to save and restore the window geometries and to perform
legacy session management.  To make session management work, '''ksmserver'''
has to be started as last process of the X login procedure. This happens
automatically at the end of the '''startkde''' script.
 


''初始作者:'' [mailto:[email protected] Matthias Ettrich]
''初始作者:'' [mailto:[email protected] Matthias Ettrich]

Latest revision as of 13:05, 23 June 2013


Template:TutorialBrowser (zh CN)

关于KDE和X11的会话管理

从KDE 1.0起,遗留的X11R4和ICCCM会话管理协议就被支持着。Legacy applications that define the WM_COMMAND property or support the WM_SAVE_YOURSELF protocol will be restarted with the specified command. 窗口坐标会以最佳效果恢复。

从KDE 2.0起,X11R6会话管理标准,XSMP也被广泛支持和使用。该标准的官方文档可以从X Consortium的FTP服务器上下载 ftp.x.org。与其他遗留协议不同,新的X11R6会话管理允许在登出后保存应用无关设置。例如对一个文本编辑器,可以保存已加载的文件名,并会您再次登录加载它们。另一项新协议的主要的应用是支持干净和安全的注销流程,即使用户决定下一次不恢复会话。该协议使应用与用户之间的交互变得可能,特别是在一些丢失数据的危险情况,如果有必要的话可以取消关机进程。

更多阅读

doc.trolltech.com,有更多关于会话管理功能和QT API的介绍.

KDE里, KApplicationKMainWindow类对程序员隐藏了许多不必要的细节。基本上,一个 KApplication为你管理一个 KConfig 配置对象(通过 KApplication::sessionConfig()) ,这样你的应用就可以保存一些会话专用数据了。

Please read the respective class documentation, especially the one of KMainWindow, for a detailed interface description. With the advanced functionality in KMainWindow, it's really just a matter of a few lines to get even a multi-window application to retains its state between different user sessions.

在你的应用中实现会话管理

这里只是一个概要描述。详细内容见相关文档。

实现会话管理并不难。如果你的窗口从KMainWindow继承,你只要做两件事:

  • 实现几个KMainWindow的虚函数。
  • 在main()添加会话管理。

就这样。

重写KMainWindow的虚函数

KMainWindow will automatically save its position, geometry and positions of toolbars and menubar on logout - and restore it on the next startup.

For every other data that your application needs to restore a session, you have to write the code for saving and restoring yourself. However, KMainWindow makes this task easy. You can just re-implement the following functions:

  • saveProperties(): This function is called only when the application is closed by the session manager (and not, when the application is closed by the user). To save the data that you need to restore the session, reimplement this function. (For a text editor for example, that would be the list of loaded files) Note that no user interaction is allowed in this function! (For example, you may not display any dialog!)
  • readProperties(): 下次登录是读入额外数据,重写这个函数。

另外,queryClose()也会让你有所兴趣。这个函数一直在窗口关闭之前被调用, 或是用户调用或是会话管理器调用。通常,你可以在这里提醒用户应用或窗口在退出关闭时有未保存的数据(例如: 跳出一个对话框提醒"保存变更" 或"忽略变更")。然而,对有些会话管理来说,退出前要用户确认并不是件好事,你也可以避免它的发生。注意:saveProperties()queryClose()之前或之后调用并不起决定作用!

To save your application-wide properties (data that is only needed once per application, and not for each main window instance) reimplement saveGlobalProperties() and it's counterpart readGlobalProperties. Normally, you don't need these functions.

给你的main()函数添加会话管理

While KMainWindow::saveProperties() (and also KMainWindow::queryClose()) will be called automatically, KMainWindow::readProperties() will not. You have to add some code to your main() function to implement session restoring.

如果你的应用有一个主窗口,MyWindow,他继承与 KMainWindow (或从 KXmlGuiWindow, 他继承于 KMainWindow)。在你的main()函数内,要加上下列代码来创建/恢复窗口:

KApplication app;
if ( app.isSessionRestored() ) {
  kRestoreMainWindows< MyWindow >();
} else {
  // 像往常一样创建缺省应用
  // 例如:
  MyWindow * window = new MyWindow();
  // #会被替换为一个数字以确保在应用系统内唯一:
  window->setObjectName("MyWindow#");
  window->show();
}
return app.exec();

kRestoreMainWindows<>() 会(在堆上)创建as many instances of your main windows as have existed in the last session and call KMainWindow::restore() with the correct arguments. 注意QWidget::show()是隐式调用的。

关于setObjectName("MyWindow#"): 为让会话管理和窗口管理的正常工作,所有的应用程序主窗口应该有一个各不相同的名称。如果你不这样做,KMainWindow会创建一个唯一的名称,但它建议明确传递一个能描述窗口类型的名称。如果能够有同类型的几个窗口,请将'#'(散列值)加到名称上,同时KMainWindow将会用数字替代,使这个名称在应用中唯一。例如, for a mail client which has one main window showing the mails and folders, and which can also have one or more windows for composing mails, the name for the folders window should be e.g. "mainwindow" and for the composer windows "composer#".

这样你就可以简单的恢复所有应用的顶层窗口了。

他也同样适用于一个应用中的其他各类顶层窗口(只要从KMainWindow继承就行)。如果你的主窗口有三个类:childMW1, childMW2 and childMW3:

KApplication app;
if ( app.isSessionRestored() ) {
  kRestoreMainWindows< childMW1, childMW2, childMW3 >();
} else {
  // 像往常一样创建缺省应用
  // 例如:
  childMW1* window1 = new childMW1();
  childMW2* window2 = new childMW2();
  childMW3* window3 = new childMW3();
  // #会被替换为一个数字以确保在应用系统内唯一:
  window1->setObjectName("type1mainWindow#");
  window2->setObjectName("type2mainWindow#");
  window3->setObjectName("type3mainWindow#");
  window1->show();
  window2->show();
  window3->show();
}
return app.exec();

当前,kRestoreMainWindows<>() 函数模板提供了至多三个参数。

附录: KDE会话管理的架构

在KDE中,会话管理服务器称为KSMServer,它是在KDEBbase包的一部分。 KSMServer服务器与与KDE的窗口管理器KWin相互作用,以保存和还原窗口的几何尺寸和执行旧会话管理。为了使会话管理有效,KSMSserver必须是X登录过程的最后一个进程。这会自动在startkde脚本的末尾执行。

初始作者: Matthias Ettrich