KDE 2 supports and uses the standard X11R6 session management protocol XSMP. The official documentation of the standard can be download from the X Consortium's FTP server ftp.x.org
In addition, the old X11R4 protocol is partically still supported. If an application defines the WM_COMMAND property, it will be restarted with this command. The window does not receive the WM_SAVE_YOURSELF message, though. The reasoning for dropping that functionality was that
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.
Here's just a brief overview how things are done. Again, see the respective class documentation for details. Imagine you have an application with a main window MyWindow inherited from KMainWindow. In your main() function, you would then create/restore the application windows with something like:
if (kapp->isRestored())
RESTORE(MyWindow)
else {
// create default application as usual
}
and reimplement the store/restore handlers in MyWindow to save and
restore all additional settings. For a text editor, that would be the
loaded files, for example:
void MyWindow::saveProperties( KConfig* ) {
// save properties here
}
void MyWindow::readProperties( KConfig* ) {
// read properties here
}
Note that standard settings like window sizes, toolbar settings etc. are automatically handled by the system.
With KMainWindow::classNameOfToplevel(), it is also possible to restore different types of toplevel windows within one application. In that case, the RESTORE maco is too primitive, use something like this in your main() function instead:
if (kapp->isRestored()){
int n = 1;
while (KMainWindow::canBeRestored(n)){
if ( KMainWindow::classNameOfToplevel(n) == "MyWindow1" )
(new MyWindow1)->restore(n);
else if ( KMainWindow::classNameOfToplevel(n) == "MyWindow2" )
(new MyWindow2)->restore(n);
// and so on....
n++;
}
} else {
// create default application as usual
}
The session management server in KDE 2 is called ksmserver and it is part of the kdebase package. To make session management work, the server has to be started as last process of the X login procedure. This happens automatically at the end of the startkde script.
Matthias Ettrich ettrich@kde.org