[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kronometer] /: Make saveFile() and saveFileAs() return false upon failure
From:       Arjun AK <arjunak234 () gmail ! com>
Date:       2014-06-30 20:32:37
Message-ID: E1X1iFh-0005Cq-Ae () scm ! kde ! org
[Download RAW message or body]

Git commit 3df31e4baac093d9b2564d632ba46bab4ef80b01 by Arjun AK.
Committed on 30/06/2014 at 16:32.
Pushed by arjunak into branch 'master'.

Make saveFile() and saveFileAs() return false upon failure

M  +25   -15   mainwindow.cpp
M  +6    -3    mainwindow.h

http://commits.kde.org/kronometer/3df31e4baac093d9b2564d632ba46bab4ef80b01

diff --git a/mainwindow.cpp b/mainwindow.cpp
index d3f269b..c136d11 100644
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -119,8 +119,7 @@ bool MainWindow::queryClose()
 
         switch (buttonCode) {
         case KMessageBox::Yes:
-          saveFileAs();
-          return true;  // TODO: return false if saving fails
+          return saveFileAs();
         case KMessageBox::No:
           return true;
         default: // cancel
@@ -134,8 +133,7 @@ bool MainWindow::queryClose()
         switch (buttonCode) {
         case KMessageBox::Yes:
           // save document here. If saving fails, return false;
-          saveFile();
-          return true;
+          return saveFile();
         case KMessageBox::No:
           return true;
         default: // cancel
@@ -258,12 +256,12 @@ void MainWindow::openFile()
     delete dialog;
 }
 
-void MainWindow::saveFile()
+bool MainWindow::saveFile()
 {
-    saveFileAs(fileName);
+    return saveFileAs(fileName);
 }
 
-void MainWindow::saveFileAs()
+bool MainWindow::saveFileAs()
 {
     QPointer<KFileDialog> dialog = new KFileDialog(KUrl(), QString(), this);
     dialog->setOperationMode(KFileDialog::Saving);
@@ -274,11 +272,13 @@ void MainWindow::saveFileAs()
     mimeTypes << XML_MIMETYPE;
     dialog->setMimeFilter(mimeTypes);
 
+    bool rc = false;
     if (dialog->exec() == QDialog::Accepted) {
-        saveFileAs(dialog->selectedFile());
+        rc = saveFileAs(dialog->selectedFile());
     }
 
     delete dialog;
+    return rc;
 }
 
 void MainWindow::exportLapsAs()
@@ -446,10 +446,10 @@ void MainWindow::setupGranularity(bool tenths, bool hundredths, bool msec)
     }
 }
 
-void MainWindow::saveFileAs(const QString& name)
+bool MainWindow::saveFileAs(const QString& name)
 {
     if (name.isEmpty()) {
-        return;
+        return false;
     }
 
     QString saveName = name;
@@ -459,7 +459,10 @@ void MainWindow::saveFileAs(const QString& name)
     }
 
     KSaveFile saveFile(saveName);
-    saveFile.open();
+    if (!saveFile.open()) {
+         KMessageBox::error(this, i18n("Failed to open file"));
+        return false;
+    }
 
     // OLD: persistence using binary files
     //QDataStream stream(&saveFile);
@@ -470,13 +473,20 @@ void MainWindow::saveFileAs(const QString& name)
     QTextStream stream(&saveFile);
     createXmlSaveFile(stream);
 
-    saveFile.finalize();
+
+    bool isSaveSuccessfull = saveFile.finalize();
     saveFile.close();
 
-    fileName = saveName;
+    if (isSaveSuccessfull) {
+        fileName = saveName;
 
-    unsavedTimes = false;
-    setWindowModified(unsavedTimes);
+        unsavedTimes = false;
+        setWindowModified(unsavedTimes);
+        return true;
+    }
+    else {
+        return false;
+    }
 }
 
 void MainWindow::openFile(const QString& name)
diff --git a/mainwindow.h b/mainwindow.h
index bb61a7a..1f6a78e 100644
--- a/mainwindow.h
+++ b/mainwindow.h
@@ -94,13 +94,15 @@ private slots:
 
     /**
      * Save current times on the current file.
+     * @returns true if operation was successfull
      */
-    void saveFile();
+    bool saveFile();
 
     /**
      * Save current times on a new file.
+     * @returns true if operation was successfull
      */
-    void saveFileAs();
+    bool saveFileAs();
 
     /**
      * Export current lap times on a file.
@@ -165,8 +167,9 @@ private:
     /**
      * Create a file with the current stopwatch time and lap times.
      * @param name The name of the file to be saved.
+     * @return true if operation was successfull
      */
-    void saveFileAs(const QString& name);
+    bool saveFileAs(const QString& name);
 
     /**
      * Load the XML save file. If an error occurs, the window is closed.
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic