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

Jump to: navigation, search
Line 2: Line 2:
  
 
==关于KDE和X11会话管理==
 
==关于KDE和X11会话管理==
 
+
从KDE 1.0以来, 遗留的X11R4和ICCCM回话管理协议仍然被支持者。Legacy applications that define the <tt>WM_COMMAND</tt>
Since KDE 1.0, the legacy X11R4 and ICCCM session management protocols
+
are supported. 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.  The window geometries will be restored on a best
Line 42: Line 40:
 
==在应用程序中实现会话管理==
 
==在应用程序中实现会话管理==
  
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:
 
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:
Line 53: Line 50:
  
 
[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]会保存它的位置,几何状态以及工具栏,菜单的相对位置。
* To warn the user that the application or some windows have unsaved data on close or logout (例如: 显示一个忽略变更的对话框,上面有一个“保存”的按钮), 重新编写[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#4c7a6c395eec0bb245cd9ad6c884f897 saveProperties()]. (对一个文字编辑器来说,这就是已经加载的文件。)注意,用户不应该对这个函数有干扰。就是说,程序中更本就没有什么对话框。
 
* 下次登陆时读取一些额外信息,重新实现[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#c8d0d64ed5b309ba1da410423120d0a6 readProperties()].
 
* 下次登陆时读取一些额外信息,重新实现[http://api.kde.org/4.x-api/kdelibs-apidocs/kdeui/html/classKMainWindow.html#c8d0d64ed5b309ba1da410423120d0a6 readProperties()].
Line 91: Line 88:
 
About 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#".
 
About 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#".
  
With this you can easily restore all toplevel windows of your application.
+
这样,很容易就可以完全恢复所有应用的顶层窗口。
  
 
It is also possible to restore different types of toplevel windows (each
 
It is also possible to restore different types of toplevel windows (each

Revision as of 06:46, 17 March 2009


Contents

Development/Tutorials/Session Management


关于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. The window geometries will be restored on a best effort basis.

Since KDE 2.0, also the standard X11R6 session management protocol XSMP is supported and used. The official documentation of the standard can be download from the X Consortium's FTP server ftp.x.org. Unlike these 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 the Qt API for it is available from doc.trolltech.com.

In KDE, the classes KApplication and KMainWindow hide all the ugly details from the programmer. Basically, a KApplication manages a KConfig configuration object sessionConfig() for you, that your application can utilize to store session specific data.

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.

在应用程序中实现会话管理

这里只是做一个简单的介绍,详细内容请见具体类文档。

Implementing session management in KDE is easy. If your main window inherits from KMainWindow, you have just 2 things to do:

  • Reimplement some virtual functions of KMainWindow.
  • Add session management support to your main() function.

That's all.

实现KMainWindow的几个虚函数

KMainWindow会保存它的位置,几何状态以及工具栏,菜单的相对位置。

  • 在用户退出或关闭程序时,提醒用户保存数据(例如: 显示一个忽略变更的对话框,上面有一个“保存”的按钮), 重新编写queryClose()方法。
  • 保存额外信息, 重新实现saveProperties(). (对一个文字编辑器来说,这就是已经加载的文件。)注意,用户不应该对这个函数有干扰。就是说,程序中更本就没有什么对话框。
  • 下次登陆时读取一些额外信息,重新实现readProperties().

KMainWindow会自动调用这些方法函数,当然有会话管理器控制。Note that it is not determined if saveProperties() is called before or after queryClose()! 重写之前请仔细阅读相关文档。

保存应用程序级属性(这些数据只要以此就可以了,不必每次每个都加载),重写saveGlobalProperties() 以及相关的readGlobalProperties。通常,你不需要这些函数方法。

在main()函数添加会话管理

KMainWindow::saveProperties()KMainWindow::queryClose()是系统自有的,KMainWindow::readProperties()不是。你的在main()加几行代码来实现会话取回。

Imagine you have an application with a main window MyWindow inherited from KMainWindow (or from KXmlGuiWindow, which inherits from KMainWindow). In your main() function, you would then create/restore the application windows with something like:

KApplication app; if ( app.isSessionRestored() ) {

 kRestoreMainWindows< MyWindow >();

} else {

 // create default application as usual
 // for example:
 MyWindow * window = new MyWindow();
 // # will be replaced with numbers that are guaranteed
 // to be unique in the application:
 window->setObjectName("MyWindow#");
 window->show();

} return app.exec();

kRestoreMainWindows<>() will create (on the heap) as many instances of your main windows as have existed in the last session and call KMainWindow::restore() with the correct arguments. Note that also QWidget::show() is called implicitly.

About 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#".

这样,很容易就可以完全恢复所有应用的顶层窗口。

It is also possible to restore different types of toplevel windows (each derived from KMainWindow, of course) within one application. Imagine you have three classes of main windows: childMW1, childMW2 and childMW3:

KApplication app; if ( app.isSessionRestored() ) {

 kRestoreMainWindows< childMW1, childMW2, childMW3 >();

} else {

 // create default application as usual
 // for example:
 childMW1* window1 = new childMW1();
 childMW2* window2 = new childMW2();
 childMW3* window3 = new childMW3();
 // # will be replaced with numbers that are guaranteed
 // to be unique in the application:
 window1->setObjectName("type1mainWindow#");
 window2->setObjectName("type2mainWindow#");
 window3->setObjectName("type3mainWindow#");
 window1->show();
 window2->show();
 window3->show();

} return app.exec();

现在, kRestoreMainWindows<>()的方法模版提供最多三个模版参数。

附录: KDE会话管理架构

KDE的会话管理服务器叫做ksmserver,他是kdebase的一部分。服务进程与KDE的窗口管理服务进程kwin进行交互,保存和恢复窗口信息,进行遗留的会话管理。 要启用会话管理, ksmserver必须X登陆过程的最后一个进程。这个会在startkde脚本的末尾体现.


初始作者: Matthias Ettrich


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