added session management
authorJonas Heese <mail-git@jonasheese.de>
Wed, 13 Aug 2008 14:49:58 +0000 (16:49 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Wed, 13 Aug 2008 16:01:08 +0000 (18:01 +0200)
src/common/main.cpp
src/qtui/CMakeLists.txt
src/qtui/mainwin.cpp
src/qtui/mainwin.h
src/qtui/qtuiapplication.cpp [new file with mode: 0644]
src/qtui/qtuiapplication.h [new file with mode: 0644]
src/qtui/sessionsettings.cpp [new file with mode: 0644]
src/qtui/sessionsettings.h [new file with mode: 0644]

index 50221a2..a46f099 100644 (file)
 #include "cliparser.h"
 
 #if defined BUILD_CORE
-#include <QCoreApplication>
 #include <QDir>
 #include "core.h"
 #include "message.h"
 
 #elif defined BUILD_QTUI
-#include <QApplication>
 #include "client.h"
+#include "qtuiapplication.h"
 #include "qtui.h"
 
 #elif defined BUILD_MONO
-#include <QApplication>
 #include "client.h"
 #include "core.h"
 #include "coresession.h"
+#include "qtuiapplication.h"
 #include "qtui.h"
 
 #else
 #error "Something is wrong - you need to #define a build mode!"
 #endif
 
+
 #include <signal.h>
 
 //! Signal handler for graceful shutdown.
@@ -68,6 +68,7 @@ int main(int argc, char **argv) {
   Global::registerMetaTypes();
   Global::setupVersion();
 
+/*
 #if defined BUILD_CORE
   Global::runMode = Global::CoreOnly;
   QCoreApplication app(argc, argv);
@@ -78,6 +79,19 @@ int main(int argc, char **argv) {
   Global::runMode = Global::Monolithic;
   QApplication app(argc, argv);
 #endif
+*/
+#if defined BUILD_CORE
+  Global::runMode = Global::CoreOnly;
+  QCoreApplication app(argc, argv);
+#elif defined BUILD_QTUI
+  Global::runMode = Global::ClientOnly;
+  QtUiApplication app(argc, argv);
+#else
+  Global::runMode = Global::Monolithic;
+  QtUiApplication app(argc, argv);
+#endif
+
+
 
   Global::parser = CliParser(QCoreApplication::arguments());
 
@@ -140,6 +154,7 @@ int main(int argc, char **argv) {
   QCoreApplication::setApplicationName("Quassel IRC");
   QCoreApplication::setOrganizationName("Quassel Project");
 
+  
 #ifndef BUILD_QTUI
   Core::instance();  // create and init the core
 #endif
@@ -147,6 +162,7 @@ int main(int argc, char **argv) {
   //Settings::init();
 
 #ifndef BUILD_CORE
+  // session resume
   QtUi *gui = new QtUi();
   Client::init(gui);
   // init gui only after the event loop has started
@@ -160,6 +176,10 @@ int main(int argc, char **argv) {
   }
 #endif
 
+#ifndef BUILD_CORE 
+  app.resumeSessionIfPossible();
+#endif
+  
   int exitCode = app.exec();
 
 #ifndef BUILD_QTUI
index f128985..74db1aa 100644 (file)
@@ -27,10 +27,12 @@ set(SOURCES
     mainwin.cpp
     msgprocessorstatuswidget.cpp
     nicklistwidget.cpp
+    qtuiapplication.cpp
     qtui.cpp
     qtuimessageprocessor.cpp
     qtuisettings.cpp
     qtuistyle.cpp
+    sessionsettings.cpp
     settingsdlg.cpp
     settingspagedlg.cpp
     titlesetter.cpp
@@ -58,6 +60,7 @@ set(MOC_HDRS
     msgprocessorstatuswidget.h
     nicklistwidget.h
     qtui.h
+    qtuiapplication.h
     qtuimessageprocessor.h
     settingsdlg.h
     settingspagedlg.h
index c888d22..beba80f 100644 (file)
@@ -35,6 +35,7 @@
 #include "coreconnectdlg.h"
 #include "msgprocessorstatuswidget.h"
 #include "qtuimessageprocessor.h"
+#include "qtuiapplication.h"
 #include "networkmodel.h"
 #include "buffermodel.h"
 #include "nicklistwidget.h"
@@ -48,6 +49,7 @@
 #include "uisettings.h"
 #include "qtuisettings.h"
 #include "jumpkeyhandler.h"
+#include "sessionsettings.h"
 
 #include "selectionmodelsynchronizer.h"
 #include "mappedselectionmodel.h"
@@ -108,6 +110,9 @@ MainWin::MainWin(QWidget *parent)
   connect(desktopNotifications, SIGNAL(NotificationClosed(uint, uint)), this, SLOT(desktopNotificationClosed(uint, uint)));
   connect(desktopNotifications, SIGNAL(ActionInvoked(uint, const QString&)), this, SLOT(desktopNotificationInvoked(uint, const QString&)));
 #endif
+  QtUiApplication* app = dynamic_cast<QtUiApplication*> qApp;
+  connect(app, SIGNAL(saveStateToSession(const QString&)), this, SLOT(saveStateToSession(const QString&)));
+  connect(app, SIGNAL(saveStateToSessionSettings(SessionSettings&)), this, SLOT(saveStateToSessionSettings(SessionSettings&)));
 }
 
 void MainWin::init() {
@@ -763,3 +768,19 @@ void MainWin::on_actionDebugNetworkModel_triggered(bool) {
   view->resize(610, 300);
   view->show();
 }
+
+void MainWin::saveStateToSession(const QString &sessionId) {
+  return;
+  SessionSettings s(sessionId);
+  
+  s.setValue("MainWinSize", size());
+  s.setValue("MainWinPos", pos());
+  s.setValue("MainWinState", saveState());
+}
+
+void MainWin::saveStateToSessionSettings(SessionSettings & s)
+{
+  s.setValue("MainWinSize", size());
+  s.setValue("MainWinPos", pos());
+  s.setValue("MainWinState", saveState());
+}
index 28c94d1..eb3f815 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "qtui.h"
 #include "titlesetter.h"
+#include "sessionsettings.h" 
 
 #include <QSystemTrayIcon>
 #include <QTimer>
@@ -43,115 +44,115 @@ class NickListWidget;
 class MainWin : public QMainWindow {
   Q_OBJECT
 
-public:
-  MainWin(QWidget *parent = 0);
-  virtual ~MainWin();
+  public:
+    MainWin(QWidget *parent = 0);
+    virtual ~MainWin();
 
-  void init();
-  void addBufferView(BufferViewConfig *config = 0);
+    void init();
+    void addBufferView(BufferViewConfig *config = 0);
 
-  void displayTrayIconMessage(const QString &title, const QString &message);
+    void displayTrayIconMessage(const QString &title, const QString &message);
 
 #ifdef HAVE_DBUS
-  void sendDesktopNotification(const QString &title, const QString &message);
+    void sendDesktopNotification(const QString &title, const QString &message);
 #endif
 
-  virtual bool event(QEvent *event);
-
-public slots:
-  void setTrayIconActivity(bool active = false);
-
-protected:
-  void closeEvent(QCloseEvent *event);
-  virtual void changeEvent(QEvent *event);
-                                        
-protected slots:
-  void connectedToCore();
-  void setConnectedState();
-  void updateLagIndicator(int lag);
-  void securedConnection();
-  void disconnectedFromCore();
-  void setDisconnectedState();
-  void systrayActivated( QSystemTrayIcon::ActivationReason );
-                                                           
-private slots:
-  void addBufferView(int bufferViewConfigId);
-  void removeBufferView(int bufferViewConfigId);
-  void messagesInserted(const QModelIndex &parent, int start, int end);
-  void showChannelList(NetworkId netId = NetworkId());
-  void showCoreInfoDlg();
-  void showSettingsDlg();
-  void on_actionEditNetworks_triggered();
-  void on_actionManageViews_triggered();
-  void on_actionLockDockPositions_toggled(bool lock);
-  void showAboutDlg();
-  void on_actionDebugNetworkModel_triggered(bool);
-  
-  void showCoreConnectionDlg(bool autoConnect = false);
-  
-  void clientNetworkCreated(NetworkId);
-  void clientNetworkRemoved(NetworkId);
-  void clientNetworkUpdated();
-  void connectOrDisconnectFromNet();
-  
-  void makeTrayIconBlink();
-  void saveStatusBarStatus(bool enabled);
-
-  void loadLayout();
-  void saveLayout();
-  
+    virtual bool event(QEvent *event);
+  public slots:
+    void setTrayIconActivity(bool active = false);
+    void saveStateToSession(const QString &sessionId);
+    void saveStateToSessionSettings(SessionSettings &s);
+
+  protected:
+    void closeEvent(QCloseEvent *event);
+    virtual void changeEvent(QEvent *event);
+
+  protected slots:
+    void connectedToCore();
+    void setConnectedState();
+    void updateLagIndicator(int lag);
+    void securedConnection();
+    void disconnectedFromCore();
+    void setDisconnectedState();
+    void systrayActivated( QSystemTrayIcon::ActivationReason );
+
+  private slots:
+    void addBufferView(int bufferViewConfigId);
+    void removeBufferView(int bufferViewConfigId);
+    void messagesInserted(const QModelIndex &parent, int start, int end);
+    void showChannelList(NetworkId netId = NetworkId());
+    void showCoreInfoDlg();
+    void showSettingsDlg();
+    void on_actionEditNetworks_triggered();
+    void on_actionManageViews_triggered();
+    void on_actionLockDockPositions_toggled(bool lock);
+    void showAboutDlg();
+    void on_actionDebugNetworkModel_triggered(bool);
+
+    void showCoreConnectionDlg(bool autoConnect = false);
+
+    void clientNetworkCreated(NetworkId);
+    void clientNetworkRemoved(NetworkId);
+    void clientNetworkUpdated();
+    void connectOrDisconnectFromNet();
+
+    void makeTrayIconBlink();
+    void saveStatusBarStatus(bool enabled);
+
+    void loadLayout();
+    void saveLayout();
+
 #ifdef HAVE_DBUS
-  void desktopNotificationClosed(uint id, uint reason);
-  void desktopNotificationInvoked(uint id, const QString & action);
+    void desktopNotificationClosed(uint id, uint reason);
+    void desktopNotificationInvoked(uint id, const QString & action);
 #endif
-  
-signals:
-  void connectToCore(const QVariantMap &connInfo);
-  void disconnectFromCore();
-  void requestBacklog(BufferInfo, QVariant, QVariant);
-  
-private:
-  Ui::MainWin ui;
-  
-  QMenu *systrayMenu;
-  QLabel *coreLagLabel;
-  QLabel *sslLabel;
-  MsgProcessorStatusWidget *msgProcessorStatusWidget;
-  
-  TitleSetter _titleSetter;
-  
-  void setupMenus();
-  void setupViews();
-  void setupNickWidget();
-  void setupChatMonitor();
-  void setupInputWidget();
-  void setupTopicWidget();
-  void setupStatusBar();
-  void setupSystray();
-  
-  void toggleVisibility();
-  
-  void enableMenus();
-  
-  QSystemTrayIcon *systray;
-  QIcon activeTrayIcon;
-  QIcon onlineTrayIcon;
-  QIcon offlineTrayIcon;
-  bool trayIconActive;
-  QTimer *timer;
-
-  BufferId currentBuffer;
-  QString currentProfile;
-  
-  QList<QDockWidget *> _netViews;
-  NickListWidget *nickListWidget;
-  
+
+  signals:
+    void connectToCore(const QVariantMap &connInfo);
+    void disconnectFromCore();
+    void requestBacklog(BufferInfo, QVariant, QVariant);
+
+  private:
+    Ui::MainWin ui;
+
+    QMenu *systrayMenu;
+    QLabel *coreLagLabel;
+    QLabel *sslLabel;
+    MsgProcessorStatusWidget *msgProcessorStatusWidget;
+
+    TitleSetter _titleSetter;
+
+    void setupMenus();
+    void setupViews();
+    void setupNickWidget();
+    void setupChatMonitor();
+    void setupInputWidget();
+    void setupTopicWidget();
+    void setupStatusBar();
+    void setupSystray();
+
+    void toggleVisibility();
+    void enableMenus();
+
+    QSystemTrayIcon *systray;
+    QIcon activeTrayIcon;
+    QIcon onlineTrayIcon;
+    QIcon offlineTrayIcon;
+    bool trayIconActive;
+    QTimer *timer;
+
+    BufferId currentBuffer;
+    QString currentProfile;
+
+    QList<QDockWidget *> _netViews;
+    NickListWidget *nickListWidget;
+
 #ifdef HAVE_DBUS
-  org::freedesktop::Notifications *desktopNotifications;
-  quint32 notificationId;
+    org::freedesktop::Notifications *desktopNotifications;
+    quint32 notificationId;
 #endif
-  
-  friend class QtUi;
+
+    friend class QtUi;
 };
 
 #endif
diff --git a/src/qtui/qtuiapplication.cpp b/src/qtui/qtuiapplication.cpp
new file mode 100644 (file)
index 0000000..5060c2a
--- /dev/null
@@ -0,0 +1,59 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+#include <QStringList>
+
+#include "qtuiapplication.h"
+#include "sessionsettings.h"
+#include "client.h"
+
+QtUiApplication::QtUiApplication(int &argc, char **argv) : QApplication(argc, argv) {
+
+}
+
+void QtUiApplication::saveState(QSessionManager & manager) {
+  //qDebug() << QString("saving session state to id %1").arg(manager.sessionId());
+  AccountId activeCore = Client::currentCoreAccount();
+  SessionSettings s(manager.sessionId());
+  s.setSessionAge(0);
+  emit saveStateToSession(manager.sessionId());
+  emit saveStateToSessionSettings(s);
+}
+
+QtUiApplication::~ QtUiApplication() {
+}
+
+void QtUiApplication::resumeSessionIfPossible() {
+  // load all sessions
+  if(isSessionRestored()) {
+    qDebug() << QString("restoring from session %1").arg(sessionId());
+    SessionSettings s(sessionId());
+    s.sessionAging();
+    s.setSessionAge(0);
+    emit resumeFromSession(sessionId());
+    emit resumeFromSessionSettings(s);
+    s.cleanup();
+  } else {
+    SessionSettings s(QString("1"));
+    s.sessionAging();
+    s.cleanup();
+  }
+}
+
diff --git a/src/qtui/qtuiapplication.h b/src/qtui/qtuiapplication.h
new file mode 100644 (file)
index 0000000..7b004d9
--- /dev/null
@@ -0,0 +1,45 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+#ifndef QTUIAPPLICATION_H_
+#define QTUIAPPLICATION_H_
+
+#include <QApplication>
+#include <QSessionManager>
+
+#include "sessionsettings.h"
+#include "qtui.h"
+
+class QtUiApplication : public QApplication {
+  Q_OBJECT
+  public:
+    QtUiApplication(int &, char**);
+    ~QtUiApplication();
+    void resumeSessionIfPossible();
+    virtual void saveState(QSessionManager & manager);
+  signals:
+    void saveStateToSession(const QString &sessionId);
+    void saveStateToSessionSettings(SessionSettings &s);
+    void resumeFromSession(const QString sessionId);
+    void resumeFromSessionSettings(SessionSettings &s);
+    
+};
+
+#endif
diff --git a/src/qtui/sessionsettings.cpp b/src/qtui/sessionsettings.cpp
new file mode 100644 (file)
index 0000000..d2a904f
--- /dev/null
@@ -0,0 +1,88 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#include "sessionsettings.h"
+
+#include <QStringList>
+
+void SessionSettings::setValue(const QString &key, const QVariant &data) {
+  setLocalValue(QString("%1/%2").arg(_sessionId, key), data);
+}
+
+QVariant SessionSettings::value(const QString &key, const QVariant &def) {
+  return localValue(QString("%1/%2").arg(_sessionId, key), def);
+}
+
+void SessionSettings::removeKey(const QString &key) {
+  removeLocalKey(QString("%1/%2").arg(_sessionId, key));
+}
+
+void SessionSettings::cleanup() {
+  QStringList sessions = localChildGroups();
+  QString str;
+  SessionSettings s(sessionId());
+  foreach(str, sessions) {
+    // load session and check age
+    s.setSessionId(str);
+    if(s.sessionAge() > 3) {
+      s.removeSession();
+    }
+  }
+}
+
+int SessionSettings::sessionAge() {
+  QVariant val = localValue(QString("%1/_sessionAge").arg(_sessionId), 0);
+  bool b = false;
+  int i = val.toInt(&b);
+  if(b) {
+    return i;
+  } else {
+    // no int saved, delete session
+    //qDebug() << QString("deleting invalid session %1 (invalid session age found)").arg(_sessionId);
+    removeSession();
+  }
+  return 10;
+}
+
+void SessionSettings::removeSession() {
+  QStringList keys = localChildKeys(sessionId());
+  foreach(QString k, keys) {
+    removeKey(k);
+  }
+}
+
+
+SessionSettings::SessionSettings(const QString & sessionId, const QString & group)  : ClientSettings(group), _sessionId(sessionId) {
+}
+
+void SessionSettings::setSessionAge(int age) {
+  setValue(QString("_sessionAge"),age);
+}
+
+void SessionSettings::sessionAging() {
+  QStringList sessions = localChildGroups();
+  QString str;
+  SessionSettings s(sessionId());
+  foreach(str, sessions) {
+    // load session and check age
+    s.setSessionId(str);
+    s.setSessionAge(s.sessionAge()+1);
+  }
+}
+
diff --git a/src/qtui/sessionsettings.h b/src/qtui/sessionsettings.h
new file mode 100644 (file)
index 0000000..15fb506
--- /dev/null
@@ -0,0 +1,51 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+
+#ifndef _SESSIONSETTINGS_H_
+#define _SESSIONSETTINGS_H_
+
+#include <QVariant>
+
+#include "clientsettings.h"
+
+class SessionSettings : public ClientSettings {
+
+  public: 
+    SessionSettings(const QString &sessionId, const QString &group = "Session");
+   
+    void setValue(const QString &key, const QVariant &data);
+    QVariant value(const QString &key, const QVariant &def = QVariant());
+    
+    void removeKey(const QString &key);
+    void removeSession();
+    
+    void cleanup();
+    void sessionAging();
+    
+    int sessionAge();
+    void setSessionAge(int age);
+    inline const QString sessionId() { return _sessionId; };
+    inline void setSessionId(const QString &sessionId) { _sessionId = sessionId; }
+  private:
+    QString _sessionId;
+}; 
+
+#endif