6.2. Session Management

When a user logs out from a KDE session, all running KDE applications are alerted of this event and are told to save and quit. When the user logs in again, those programs are restored and should go to the same state as they were in before.

Your main() function checks whether it is being restored; if so, it then triggers the restart.

In Listings 6.1–6.3, session management is shown.


Example 6.1. main.cpp: Example of Session Management

   1 
   2  1: In main.cpp:
   3  2:
   4  3: #include "mykapp.h"
   5  4: #include <kapp.h>
   6  5: #include <dcopclient.h>
   7  6:
   8  7: int main(int argc, char **argv)
   9  8: {
  10  9:   // … KAboutData code here …
  11 10:   KApplication app;
  12 11:
  13 12:   // register ourselves as a dcop client
  14 13:   app.dcopClient()->registerAs(app.name());
  15 14:
  16 15:   // see if we are starting with session management
  17 16:   if (app.isRestored())
  18 17:      RESTORE(MyKApp)
  19 18:   else
  20 19:   {
  21 20:      // no session.. just start up normally
  22 21:      MyKApp *widget = new MyKApp;
  23 22:      widget->show();
  24 23:   }
  25 24:
  26 25:   return app.exec();
  27 26: }
  28 


Example 6.2. mykapp.h: Header File for Main Window

   1 
   2  1: #include <ktmainwindow.h>
   3  2:
   4  3: class MyKApp : public KTMainWindow
   5  4: {
   6  5:   // … Declaration of class …
   7  6:
   8  7: protected:
   9  8:   void saveProperties(KConfig *);
  10  9:   void readProperties(KConfig *);
  11 10:
  12 11:   // … Rest of Class declaration …
  13 12: };
  14 


Example 6.3. mykapp.cpp: Source File for Main Window

   1 
   2  1: void MyKApp::saveProperties(KConfig *config)
   3  2: {
   4  3:   // config is where you write all the options to save.
   5  4:   // It's already opened and ready for your use.
   6  5: }
   7  6:
   8  7: void MyKApp::readProperties(KConfig *config)
   9  8: {
  10  9:   // config will have been opened for you, just
  11 10:   // read what you saved in saveProperties(..)
  12 11:   // and recover your program.
  13 12: }
  14 

KEdit is a fine example of a simple but effective session management. In Listing 6.4, its session management code is shown:


Example 6.4. kedit.cpp: A Part of KEdit's Main Window Source File

   1 
   2  1: void TopLevel::saveProperties(KConfig* config)
   3  2: {
   4  3:   // Test if document needs to be saved
   5  4:   // If empty AND isn't modified, no need to save.
   6  5:   if(location.isEmpty() &!eframe->isModified())
   7  6:     return;
   8  7:
   9  8:   // Store the config filename
  10  9:   config->writeEntry("filename",name());
  11 10:   // Store the state of modification, if it's modified,
  12 11:   // we'll also store a temporary file elsewhere
  13 12:   config->writeEntry("modified",eframe->isModified());
  14 13:
  15 14:   if(eframe->isModified())
  16 15:   {
  17 16:        QString tmplocation = kapp->tempSaveName(name());
  18 17:        saveFile(tmplocation);
  19 18:   }
  20 19: }
  21 20:
  22 21: void TopLevel::readProperties(KConfig* config)
  23 22: {
  24 23:   QString filename = config->readEntry("filename","");
  25 24:   int modified = config->readNumEntry("modified",0);
  26 25:
  27 26:   if(!filename.isEmpty()&modified)
  28 27:   {
  29 28:      bool ok;
  30 29:      QString fn = kapp->checkRecoverFile(filename,ok);
  31 30:
  32 31:      if(ok)
  33 32:      { // Yes, there's a temporary file, and it's 'fn'
  34 33:        openFile(fn,KEdit::OPEN_READWRITE);
  35 34:        location = filename;
  36 35:        eframe->setModified();
  37 36:        setFileCaption();
  38 37:      }
  39 38:   }
  40 39:   else if(!filename.isEmpty())
  41 40:   { // No temp file, so we just open up the previously
  42 41:     // opened file.
  43 42:     openFile(filename,KEdit::OPEN_READWRITE);
  44 43:     location = filename;
  45 44:     eframe->setModified(false);
  46 45:     setFileCaption();
  47 46:   }
  48 47: }
  49 

Note that the following methods have been declared and defined by KEdit itself:

  • eframe->setModified(..);(line 12)

  • saveFile(..);(line 17)

  • openFile(..);(line 33)

  • setFileCaption();(line 36)

If your program does not need to open any files, you should at least store the state—the value in a calculator output, for example:


   1 
   2 void IntCalc::saveProperties(KConfig* config)
   3 {
   4   // Store the value
   5   config->writeEntry("amount", theNumber);
   6 }
   7 
   8 void IntCalc::readProperties(KConfig* config)
   9 {
  10   // Read the value, where "theNumber" is a
  11   // member variable
  12   theNumber = config->readNumEntry("theNumber",0);
  13 }
  14