# HG changeset patch # User Chris Cannam # Date 1158063344 0 # Node ID 0dbd08e365cec4d24ecb9509aa5c6bebba1adbbc # Parent 582f4d6e82c70adc16420b28df2b0bdc3440742f * Add session management support (commitData) diff -r 582f4d6e82c7 -r 0dbd08e365ce main/MainWindow.cpp --- 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 #include #include +#include +#include #include #include @@ -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, diff -r 582f4d6e82c7 -r 0dbd08e365ce main/MainWindow.h --- 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 diff -r 582f4d6e82c7 -r 0dbd08e365ce main/main.cpp --- 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 #include #include +#include #include #include @@ -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"); 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; }