changeset 675:341e4e1a6ed3

Use TempWriteFile for CSV export as well
author Chris Cannam
date Tue, 29 Mar 2011 17:55:31 +0100
parents 920e3880f7b4
children 611a4fa14dde
files base/TempWriteFile.cpp data/fileio/CSVFileWriter.cpp
diffstat 2 files changed, 22 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/base/TempWriteFile.cpp	Tue Mar 29 17:30:23 2011 +0100
+++ b/base/TempWriteFile.cpp	Tue Mar 29 17:55:31 2011 +0100
@@ -32,7 +32,7 @@
     }
     
     m_temp = temp.fileName();
-    temp.close();
+    temp.close(); // does not remove the file
 }
 
 TempWriteFile::~TempWriteFile()
--- 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();
 }