7.3. Session Management
We touched on session management in Chapter 2 "A Simple KDE Application." You created KSimpleApp and endowed it with the capability to be restarted and maintain its position and size across sessions. (Session refers to the time between logging in and logging out.) Actually, this functionality was provided by KTMainWindow from which the class KSimpleApp was derived.
In general, you'll want to save more information across sessions than just the window position and size. KTMainWindow offers the virtual methods saveProperties() and readProperties() for this purpose. Also, you will want to save the user's data (or at least offer this option) before a session exits. You reimplement the virtual method queryClose() to do this. Listings 7.11–7.13 show the source code for KSaveAcross, an application that demonstrates these features.
Example 7.11. ksaveacross.h: Class Declaration for KSaveAcross, a Widget That Demonstrates Session Management Features of KTMainWindow
The content area of the KSaveAcross widget is a QLineEdit widget. The menubar offers two choices: change the font used by the QLineEdit widget or quit the application. The font chosen by the user is saved across sessions.
Example 7.12. ksaveacross.cpp: Class Definition for KSaveAcross
The method saveProperties() (lines 61–66) is called just before the application is terminated by the session manager. If the user quits the application normally, the method is not called. When the session manager restarts the application at the beginning of the next session, the method readProperties() is called. In these methods, you should save and read in properties that describe the current state of the application but that may not be saved in or specified in the configuration file. A Web browser, for example, might save the URL of the current page in the method saveProperties(), although it wouldn't want to store this in the configuration file.
saveProperties() and readProperties() work with KConfig objects. These KConfig objects do not operate on the default application configuration file. Instead, they operate on instance-specific configuration files created solely for the purpose of saving this session. The reading and writing methods were described in the previous section. Don't forget to call kconfig->sync() (line 65) at the end of saveProperties() to write the information to disk.
Before the application exits, it should ask the user whether he or she wishes to save the current document. If the application does this in the method queryClose() (a virtual method of the class KTMainWindow), the question is asked whether the application is closed by the user or by the session manager.
If queryClose() returns 3false, the application does not exit. The reimplementation of queryClose() in KSaveAcross (line 34–58) presents the user with the choices Yes, No, and Cancel as answers to the question, "Save changes to document?" If the user answers Yes or No, queryClose() returns true, letting the application exit. If the user chooses Cancel, queryClose() returns false and the user can continue working.
Listing 7.13 provides the main() function needed to create and start this application. Notice that I have included the session management code (lines 9–15), which was discussed in Chapter 2. Figure 7.4 shows running KSaveAcross.
To create an executable, you'll need the code in Listing 7.13, and you'll need to place the GUI file, ksaveacrossui.rc (found on this book's web site), in the directory $KDEDIR/share/ kstatusbardemo.
Example 7.13. main.cpp: A main() Function Suitable for Testing KSaveAcross