changeset 217:64dfa7d6171d sv_v1.8

Use TempWriteFile when saving session files, to avoid clobbering until file save complete
author Chris Cannam
date Tue, 29 Mar 2011 17:56:32 +0100
parents ee31e2f3a0f1
children f702756154b0
files framework/MainWindowBase.cpp
diffstat 1 files changed, 40 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/framework/MainWindowBase.cpp	Tue Mar 08 11:31:24 2011 +0000
+++ b/framework/MainWindowBase.cpp	Tue Mar 29 17:56:32 2011 +0100
@@ -66,6 +66,8 @@
 #include "base/XmlExportable.h"
 #include "base/Profiler.h"
 #include "base/Preferences.h"
+#include "base/TempWriteFile.h"
+#include "base/Exceptions.h"
 
 #include "data/osc/OSCQueue.h"
 #include "data/midi/MIDIInput.h"
@@ -1890,32 +1892,46 @@
 bool
 MainWindowBase::saveSessionFile(QString path)
 {
-    BZipFileDevice bzFile(path);
-    if (!bzFile.open(QIODevice::WriteOnly)) {
-        std::cerr << "Failed to open session file \"" << path.toStdString()
-                  << "\" for writing: "
-                  << bzFile.errorString().toStdString() << std::endl;
+    try {
+
+        TempWriteFile temp(path);
+
+        BZipFileDevice bzFile(temp.getTemporaryFilename());
+        if (!bzFile.open(QIODevice::WriteOnly)) {
+            std::cerr << "Failed to open session file \""
+                      << temp.getTemporaryFilename().toStdString()
+                      << "\" for writing: "
+                      << bzFile.errorString().toStdString() << std::endl;
+            return false;
+        }
+
+        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
+
+        QTextStream out(&bzFile);
+        toXml(out);
+        out.flush();
+
+        QApplication::restoreOverrideCursor();
+
+        if (!bzFile.isOK()) {
+            QMessageBox::critical(this, tr("Failed to write file"),
+                                  tr("<b>Save failed</b><p>Failed to write to file \"%1\": %2")
+                                  .arg(path).arg(bzFile.errorString()));
+            bzFile.close();
+            return false;
+        }
+
+        bzFile.close();
+        temp.moveToTarget();
+        return true;
+
+    } catch (FileOperationFailed &f) {
+        
+        QMessageBox::critical(this, tr("Failed to write file"),
+                              tr("<b>Save failed</b><p>Failed to write to file \"%1\": %2")
+                              .arg(path).arg(f.what()));
         return false;
     }
-
-    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
-
-    QTextStream out(&bzFile);
-    toXml(out);
-    out.flush();
-
-    QApplication::restoreOverrideCursor();
-
-    if (!bzFile.isOK()) {
-	QMessageBox::critical(this, tr("Failed to write file"),
-			      tr("<b>Save failed</b><p>Failed to write to file \"%1\": %2")
-			      .arg(path).arg(bzFile.errorString()));
-        bzFile.close();
-	return false;
-    }
-
-    bzFile.close();
-    return true;
 }
 
 void