Qt 4.8
Public Types | Public Functions | Private Functions | Friends | List of all members
QSessionManager Class Reference

The QSessionManager class provides access to the session manager. More...

#include <qsessionmanager.h>

Inheritance diagram for QSessionManager:
QObject

Public Types

enum  RestartHint { RestartIfRunning, RestartAnyway, RestartImmediately, RestartNever }
 This enum type defines the circumstances under which this application wants to be restarted by the session manager. More...
 

Public Functions

bool allowsErrorInteraction ()
 Returns true if error interaction is permitted; otherwise returns false. More...
 
bool allowsInteraction ()
 Asks the session manager for permission to interact with the user. More...
 
void cancel ()
 Tells the session manager to cancel the shutdown process. More...
 
QStringList discardCommand () const
 Returns the currently set discard command. More...
 
void * handle () const
 
bool isPhase2 () const
 Returns true if the session manager is currently performing a second session management phase; otherwise returns false. More...
 
void release ()
 Releases the session manager's interaction semaphore after an interaction phase. More...
 
void requestPhase2 ()
 Requests a second session management phase for the application. More...
 
QStringList restartCommand () const
 Returns the currently set restart command. More...
 
RestartHint restartHint () const
 Returns the application's current restart hint. More...
 
QString sessionId () const
 Returns the identifier of the current session. More...
 
QString sessionKey () const
 Returns the session key in the current session. More...
 
void setDiscardCommand (const QStringList &)
 Sets the discard command to the given list. More...
 
void setManagerProperty (const QString &name, const QString &value)
 Low-level write access to the application's identification and state records are kept in the session manager. More...
 
void setManagerProperty (const QString &name, const QStringList &value)
 Low-level write access to the application's identification and state record are kept in the session manager. More...
 
void setRestartCommand (const QStringList &)
 If the session manager is capable of restoring sessions it will execute command in order to restore the application. More...
 
void setRestartHint (RestartHint)
 Sets the application's restart hint to hint. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Private Functions

 QSessionManager (QApplication *app, QString &id, QString &key)
 
 ~QSessionManager ()
 

Friends

class QApplication
 
class QApplicationPrivate
 
class QBaseApplication
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QSessionManager class provides access to the session manager.

A session manager in a desktop environment (in which Qt GUI applications live) keeps track of a session, which is a group of running applications, each of which has a particular state. The state of an application contains (most notably) the documents the application has open and the position and size of its windows.

The session manager is used to save the session, e.g., when the machine is shut down, and to restore a session, e.g., when the machine is started up. We recommend that you use QSettings to save an application's settings, for example, window positions, recently used files, etc. When the application is restarted by the session manager, you can restore the settings.

QSessionManager provides an interface between the application and the session manager so that the program can work well with the session manager. In Qt, session management requests for action are handled by the two virtual functions QApplication::commitData() and QApplication::saveState(). Both provide a reference to a session manager object as argument, to allow the application to communicate with the session manager. The session manager can only be accessed through these functions.

No user interaction is possible unless the application gets explicit permission from the session manager. You ask for permission by calling allowsInteraction() or, if it is really urgent, allowsErrorInteraction(). Qt does not enforce this, but the session manager may.

You can try to abort the shutdown process by calling cancel(). The default commitData() function does this if some top-level window rejected its closeEvent().

For sophisticated session managers provided on Unix/X11, QSessionManager offers further possibilities to fine-tune an application's session management behavior: setRestartCommand(), setDiscardCommand(), setRestartHint(), setProperty(), requestPhase2(). See the respective function descriptions for further details.

See also
QApplication, {Session Management}

Definition at line 60 of file qsessionmanager.h.

Enumerations

◆ RestartHint

This enum type defines the circumstances under which this application wants to be restarted by the session manager.

The current values are:

  • RestartIfRunning If the application is still running when the session is shut down, it wants to be restarted at the start of the next session.
  • RestartAnyway The application wants to be started at the start of the next session, no matter what. (This is useful for utilities that run just after startup and then quit.)
  • RestartImmediately The application wants to be started immediately whenever it is not running.
  • RestartNever The application does not want to be restarted automatically.

The default hint is RestartIfRunning.

Enumerator
RestartIfRunning 
RestartAnyway 
RestartImmediately 
RestartNever 

Definition at line 79 of file qsessionmanager.h.

Constructors and Destructors

◆ QSessionManager()

QSessionManager::QSessionManager ( QApplication app,
QString id,
QString key 
)
private

Definition at line 5054 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and QSmSocketReceiver::socketActivated().

5055  : QObject(*new QSessionManagerPrivate, app)
5056 {
5058  setObjectName(QLatin1String("qt_sessionmanager"));
5059  qt_session_manager_self = this;
5060 #if defined(Q_WS_WIN)
5061  wchar_t guidstr[40];
5062  GUID guid;
5063  CoCreateGuid(&guid);
5064  StringFromGUID2(guid, guidstr, 40);
5065  id = QString::fromWCharArray(guidstr);
5066  CoCreateGuid(&guid);
5067  StringFromGUID2(guid, guidstr, 40);
5068  key = QString::fromWCharArray(guidstr);
5069 #endif
5070  d->sessionId = id;
5071  d->sessionKey = key;
5072  d->restartHint = RestartIfRunning;
5073 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
double d
Definition: qnumeric_p.h:62
Definition: quuid.h:52
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_D(Class)
Definition: qglobal.h:2482
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
The QSessionManager class provides access to the session manager.
#define CoCreateGuid
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
int key
QSessionManager * qt_session_manager_self

◆ ~QSessionManager()

QSessionManager::~QSessionManager ( )
private

Definition at line 5075 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and QSmSocketReceiver::socketActivated().

5076 {
5078 }
QSessionManager * qt_session_manager_self

Functions

◆ allowsErrorInteraction()

bool QSessionManager::allowsErrorInteraction ( )

Returns true if error interaction is permitted; otherwise returns false.

This is similar to allowsInteraction(), but also enables the application to tell the user about any errors that occur. Session managers may give error interaction requests higher priority, which means that it is more likely that an error interaction is permitted. However, you are still not guaranteed that the session manager will allow interaction.

See also
allowsInteraction(), release(), cancel()

Definition at line 4170 of file qapplication_win.cpp.

Referenced by handle(), QSessionManagerPrivate::QSessionManagerPrivate(), and sm_performSaveYourself().

4171 {
4172  sm_blockUserInput = false;
4173  return true;
4174 }
static bool sm_blockUserInput

◆ allowsInteraction()

bool QSessionManager::allowsInteraction ( )

Asks the session manager for permission to interact with the user.

Returns true if interaction is permitted; otherwise returns false.

The rationale behind this mechanism is to make it possible to synchronize user interaction during a shutdown. Advanced session managers may ask all applications simultaneously to commit their data, resulting in a much faster shutdown.

When the interaction is completed we strongly recommend releasing the user interaction semaphore with a call to release(). This way, other applications may get the chance to interact with the user while your application is still busy saving data. (The semaphore is implicitly released when the application exits.)

If the user decides to cancel the shutdown process during the interaction phase, you must tell the session manager that this has happened by calling cancel().

Here's an example of how an application's QApplication::commitData() might be implemented:

void MyApplication::commitData(QSessionManager& manager)
{
if (manager.allowsInteraction()) {
mainWindow,
tr("My Application"),
tr("Save changes to document?"),
switch (ret) {
manager.release();
if (!saveDocument())
manager.cancel();
break;
break;
default:
manager.cancel();
}
} else {
// we did not get permission to interact, then
// do something reasonable instead
}
}

If an error occurred within the application while saving its data, you may want to try allowsErrorInteraction() instead.

See also
QApplication::commitData(), release(), cancel()

Definition at line 4164 of file qapplication_win.cpp.

Referenced by QApplication::commitData(), handle(), QSessionManagerPrivate::QSessionManagerPrivate(), and QSmSocketReceiver::socketActivated().

4165 {
4166  sm_blockUserInput = false;
4167  return true;
4168 }
static bool sm_blockUserInput

◆ cancel()

void QSessionManager::cancel ( )

Tells the session manager to cancel the shutdown process.

Applications should not call this function without asking the user first.

See also
allowsInteraction(), allowsErrorInteraction()

Definition at line 4182 of file qapplication_win.cpp.

Referenced by QApplication::commitData(), handle(), and QSessionManagerPrivate::QSessionManagerPrivate().

4183 {
4184  sm_cancel = true;
4185 }
static bool sm_cancel

◆ discardCommand()

QStringList QSessionManager::discardCommand ( ) const

Returns the currently set discard command.

To iterate over the list, you can use the foreach pseudo-keyword:

foreach (const QString &command, mySession.discardCommand())
do_something(command);
See also
setDiscardCommand(), restartCommand(), setRestartCommand()

Definition at line 5150 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and sm_performSaveYourself().

5151 {
5152  Q_D(const QSessionManager);
5153  return d->discardCommand;
5154 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSessionManager class provides access to the session manager.

◆ handle()

void * QSessionManager::handle ( ) const
Warning
This function is not part of the public interface.

Definition at line 5094 of file qapplication.cpp.

Referenced by QSmSocketReceiver::socketActivated().

5095 {
5096  return 0;
5097 }

◆ isPhase2()

bool QSessionManager::isPhase2 ( ) const

Returns true if the session manager is currently performing a second session management phase; otherwise returns false.

See also
requestPhase2()

Definition at line 5164 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate().

5165 {
5166  return false;
5167 }

◆ release()

void QSessionManager::release ( )

Releases the session manager's interaction semaphore after an interaction phase.

See also
allowsInteraction(), allowsErrorInteraction()

Definition at line 4176 of file qapplication_win.cpp.

Referenced by handle(), and QSessionManagerPrivate::QSessionManagerPrivate().

4177 {
4178  if (sm_smActive)
4179  sm_blockUserInput = true;
4180 }
static bool sm_smActive
static bool sm_blockUserInput

◆ requestPhase2()

void QSessionManager::requestPhase2 ( )

Requests a second session management phase for the application.

The application may then return immediately from the QApplication::commitData() or QApplication::saveState() function, and they will be called again once most or all other applications have finished their session management.

The two phases are useful for applications such as the X11 window manager that need to store information about another application's windows and therefore have to wait until these applications have completed their respective session management tasks.

Note
If another application has requested a second phase it may get called before, simultaneously with, or after your application's second phase.
See also
isPhase2()

Definition at line 5169 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate().

5170 {
5171 }

◆ restartCommand()

QStringList QSessionManager::restartCommand ( ) const

Returns the currently set restart command.

To iterate over the list, you can use the foreach pseudo-keyword:

foreach (const QString &command, mySession.restartCommand())
do_something(command);
See also
setRestartCommand(), restartHint()

Definition at line 5138 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and sm_performSaveYourself().

5139 {
5140  Q_D(const QSessionManager);
5141  return d->restartCommand;
5142 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSessionManager class provides access to the session manager.

◆ restartHint()

QSessionManager::RestartHint QSessionManager::restartHint ( ) const

Returns the application's current restart hint.

The default is RestartIfRunning.

See also
setRestartHint()

Definition at line 5126 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and sm_performSaveYourself().

5127 {
5128  Q_D(const QSessionManager);
5129  return d->restartHint;
5130 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSessionManager class provides access to the session manager.

◆ sessionId()

QString QSessionManager::sessionId ( ) const

Returns the identifier of the current session.

If the application has been restored from an earlier session, this identifier is the same as it was in the earlier session.

See also
sessionKey(), QApplication::sessionId()

Definition at line 5080 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and QSmSocketReceiver::socketActivated().

5081 {
5082  Q_D(const QSessionManager);
5083  return d->sessionId;
5084 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSessionManager class provides access to the session manager.

◆ sessionKey()

QString QSessionManager::sessionKey ( ) const

Returns the session key in the current session.

If the application has been restored from an earlier session, this key is the same as it was when the previous session ended.

The session key changes with every call of commitData() or saveState().

See also
sessionId(), QApplication::sessionKey()

Definition at line 5086 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and QSmSocketReceiver::socketActivated().

5087 {
5088  Q_D(const QSessionManager);
5089  return d->sessionKey;
5090 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSessionManager class provides access to the session manager.

◆ setDiscardCommand()

void QSessionManager::setDiscardCommand ( const QStringList list)

Sets the discard command to the given list.

See also
discardCommand(), setRestartCommand()

Definition at line 5144 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and sm_performSaveYourself().

5145 {
5147  d->discardCommand = command;
5148 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSessionManager class provides access to the session manager.

◆ setManagerProperty() [1/2]

void QSessionManager::setManagerProperty ( const QString name,
const QString value 
)

Low-level write access to the application's identification and state records are kept in the session manager.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The property called name has its value set to the string value.

Definition at line 5156 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate().

5157 {
5158 }

◆ setManagerProperty() [2/2]

void QSessionManager::setManagerProperty ( const QString name,
const QStringList value 
)

Low-level write access to the application's identification and state record are kept in the session manager.

The property called name has its value set to the string list value.

Definition at line 5160 of file qapplication.cpp.

5161 {
5162 }

◆ setRestartCommand()

void QSessionManager::setRestartCommand ( const QStringList command)

If the session manager is capable of restoring sessions it will execute command in order to restore the application.

The command defaults to

appname -session id

The -session option is mandatory; otherwise QApplication cannot tell whether it has been restored or what the current session identifier is. See QApplication::isSessionRestored() and QApplication::sessionId() for details.

If your application is very simple, it may be possible to store the entire application state in additional command line options. This is usually a very bad idea because command lines are often limited to a few hundred bytes. Instead, use QSettings, temporary files, or a database for this purpose. By marking the data with the unique sessionId(), you will be able to restore the application in a future session.

See also
restartCommand(), setDiscardCommand(), setRestartHint()

Definition at line 5132 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate(), and sm_performSaveYourself().

5133 {
5135  d->restartCommand = command;
5136 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSessionManager class provides access to the session manager.

◆ setRestartHint()

void QSessionManager::setRestartHint ( QSessionManager::RestartHint  hint)

Sets the application's restart hint to hint.

On application startup, the hint is set to RestartIfRunning.

Note
These flags are only hints, a session manager may or may not respect them.

We recommend setting the restart hint in QApplication::saveState() because most session managers perform a checkpoint shortly after an application's startup.

See also
restartHint()

Definition at line 5120 of file qapplication.cpp.

Referenced by QSessionManagerPrivate::QSessionManagerPrivate().

5121 {
5123  d->restartHint = hint;
5124 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QSessionManager class provides access to the session manager.

Friends and Related Functions

◆ QApplication

friend class QApplication
friend

Definition at line 100 of file qsessionmanager.h.

◆ QApplicationPrivate

friend class QApplicationPrivate
friend

Definition at line 101 of file qsessionmanager.h.

◆ QBaseApplication

friend class QBaseApplication
friend

Definition at line 102 of file qsessionmanager.h.


The documentation for this class was generated from the following files: