changeset 11:0dbd08e365ce

* Add session management support (commitData)
author Chris Cannam
date Tue, 12 Sep 2006 12:15:44 +0000
parents 582f4d6e82c7
children ee967635c728
files main/MainWindow.cpp main/MainWindow.h main/main.cpp
diffstat 3 files changed, 66 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/main/MainWindow.cpp	Mon Sep 11 16:40:18 2006 +0000
+++ b/main/MainWindow.cpp	Tue Sep 12 12:15:44 2006 +0000
@@ -76,6 +76,8 @@
 #include <QProcess>
 #include <QShortcut>
 #include <QSettings>
+#include <QDateTime>
+#include <QProcess>
 
 #include <iostream>
 #include <cstdio>
@@ -2151,6 +2153,42 @@
 }
 
 bool
+MainWindow::commitData(bool mayAskUser)
+{
+    if (mayAskUser) {
+        return checkSaveModified();
+    } else {
+        if (!m_documentModified) return true;
+
+        // If we can't check with the user first, then we can't save
+        // to the original session file (even if we have it) -- have
+        // to use a temporary file
+
+        QString svDirBase = ".sv1";
+        QString svDir = QDir::home().filePath(svDirBase);
+
+        if (!QFileInfo(svDir).exists()) {
+            if (!QDir::home().mkdir(svDirBase)) return false;
+        } else {
+            if (!QFileInfo(svDir).isDir()) return false;
+        }
+        
+        // This name doesn't have to be unguessable
+
+        QString fname = QString("tmp-%1-%2.sv")
+            .arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"))
+            .arg(QProcess().pid());
+        QString fpath = QDir(svDir).filePath(fname);
+        if (saveSessionFile(fpath)) {
+            RecentFiles::getInstance()->addFile(fpath);
+            return true;
+        } else {
+            return false;
+        }
+    }
+}
+
+bool
 MainWindow::checkSaveModified()
 {
     // Called before some destructive operation (e.g. new session,
--- a/main/MainWindow.h	Mon Sep 11 16:40:18 2006 +0000
+++ b/main/MainWindow.h	Tue Sep 12 12:15:44 2006 +0000
@@ -66,6 +66,7 @@
     bool openLayerFile(QString path);
     bool openSessionFile(QString path);
     bool saveSessionFile(QString path);
+    bool commitData(bool mayAskUser); // on session shutdown
 
 signals:
     // Used to toggle the availability of menu actions
--- a/main/main.cpp	Mon Sep 11 16:40:18 2006 +0000
+++ b/main/main.cpp	Tue Sep 12 12:15:44 2006 +0000
@@ -29,6 +29,7 @@
 #include <QLocale>
 #include <QSettings>
 #include <QIcon>
+#include <QSessionManager>
 
 #include <iostream>
 #include <signal.h>
@@ -49,10 +50,33 @@
     exit(0); // without releasing mutex
 }
 
+class SVApplication : public QApplication
+{
+public:
+    SVApplication(int argc, char **argv) :
+        QApplication(argc, argv),
+        m_mainWindow(0) { }
+    virtual ~SVApplication() { }
+
+    void setMainWindow(MainWindow *mw) { m_mainWindow = mw; }
+    void releaseMainWindow() { m_mainWindow = 0; }
+
+    virtual void commitData(QSessionManager &manager) {
+        if (!m_mainWindow) return;
+        bool mayAskUser = manager.allowsInteraction();
+        bool success = m_mainWindow->commitData(mayAskUser);
+        manager.release();
+        if (!success) manager.cancel();
+    }
+
+protected:
+    MainWindow *m_mainWindow;
+};
+
 int
 main(int argc, char **argv)
 {
-    QApplication application(argc, argv);
+    SVApplication application(argc, argv);
 
     signal(SIGINT,  signalHandler);
     signal(SIGTERM, signalHandler);
@@ -89,6 +113,7 @@
     qRegisterMetaType<PropertyContainer::PropertyName>("PropertyContainer::PropertyName");
 
     MainWindow gui;
+    application.setMainWindow(&gui);
 
     QDesktopWidget *desktop = QApplication::desktop();
     QRect available = desktop->availableGeometry();
@@ -129,6 +154,7 @@
 
     cleanupMutex.lock();
     TempDirectory::getInstance()->cleanup();
+    application.releaseMainWindow();
 
     return rv;
 }