diff data/fileio/CSVFileWriter.cpp @ 675:341e4e1a6ed3

Use TempWriteFile for CSV export as well
author Chris Cannam
date Tue, 29 Mar 2011 17:55:31 +0100
parents 4b2ea82fd0ed
children 38bb7c8e415d
line wrap: on
line diff
--- a/data/fileio/CSVFileWriter.cpp	Tue Mar 29 17:30:23 2011 +0100
+++ b/data/fileio/CSVFileWriter.cpp	Tue Mar 29 17:55:31 2011 +0100
@@ -21,6 +21,9 @@
 #include "model/NoteModel.h"
 #include "model/TextModel.h"
 
+#include "base/TempWriteFile.h"
+#include "base/Exceptions.h"
+
 #include <QFile>
 #include <QTextStream>
 
@@ -51,16 +54,25 @@
 void
 CSVFileWriter::write()
 {
-    QFile file(m_path);
-    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
-        m_error = tr("Failed to open file %1 for writing").arg(m_path);
-        return;
+    try {
+        TempWriteFile temp(m_path);
+
+        QFile file(temp.getTemporaryFilename());
+        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+            m_error = tr("Failed to open file %1 for writing")
+                .arg(temp.getTemporaryFilename());
+            return;
+        }
+    
+        QTextStream out(&file);
+        out << m_model->toDelimitedDataString(m_delimiter);
+
+        file.close();
+        temp.moveToTarget();
+
+    } catch (FileOperationFailed &f) {
+        m_error = f.what();
     }
-    
-    QTextStream out(&file);
-    out << m_model->toDelimitedDataString(m_delimiter);
-
-    file.close();
 }