Added saving and loading of timers
authorHeli Hyvättinen <heli@pantteri.(none)>
Wed, 6 Apr 2011 08:43:14 +0000 (11:43 +0300)
committerHeli Hyvättinen <heli@pantteri.(none)>
Wed, 6 Apr 2011 08:44:00 +0000 (11:44 +0300)
src/currentalertstablemodel.cpp
src/currentalertstablemodel.h
src/kitchenalertmainwindow.cpp
src/kitchenalertmainwindow.h
src/kitchenalertmainwindow.ui
src/main.cpp
src/savedtimers.cpp [deleted file]
src/savedtimers.h [deleted file]
src/timer.cpp
src/timer.h

index 9913288..62e8327 100644 (file)
@@ -325,3 +325,8 @@ void CurrentAlertsTableModel::removeTimer(QModelIndex index)
 
 }
 
+bool CurrentAlertsTableModel::saveTimer(QModelIndex index, QString filename)
+{
+    return currentTimers_.at(index.row())->save(filename);
+}
+
index 8f4622a..b20ead1 100755 (executable)
@@ -35,7 +35,7 @@
 /*! Class that contains the model that holds the timers'
 
   @author Heli Hyvättinen
-  @date 2011-03-29
+  @date 2011-04-05
   @version 0.2.1
 
 Class that contains the model that holds the timers
@@ -86,6 +86,13 @@ public:
     bool isThisTimerAlerting(QModelIndex index);
 
 
+    /*! Saves a timer to a file
+      @param index The index of the timer to be saved. Any cell from the row of the timer is good here.
+      @param filename The name of the file to which the timer is to be saved
+    */
+    bool saveTimer(QModelIndex index,QString filename);
+
+
 signals:
 
 public slots:
index 4fc1c18..b1bd95c 100644 (file)
@@ -43,6 +43,7 @@
 #include <QMenuBar>
 #include <QMessageBox>
 #include <QSettings>
+#include <QFileDialog>
 
 
 
@@ -89,6 +90,8 @@ KitchenAlertMainWindow::KitchenAlertMainWindow(QWidget *parent) :
   connect(ui->RestartButton,SIGNAL(clicked()),this,SLOT(restart()));
   connect(ui->SnoozeButton,SIGNAL(clicked()),this, SLOT(snooze()));
   connect(ui->RemoveButton,SIGNAL(clicked()),this,SLOT(remove()));
+  connect(ui->SaveButton,SIGNAL(clicked()),this,SLOT(saveTimer()));
+  connect(ui->OpenButton,SIGNAL(clicked()),this,SLOT(loadTimer()));
   // menu setup
 
   QAction * p_SelectSoundAction = new QAction(tr("Select alert sound"),this);
@@ -389,3 +392,120 @@ void KitchenAlertMainWindow::remove()
     }
     ui->SnoozeButton->setDisabled(true);
 }
+
+void KitchenAlertMainWindow::saveTimer()
+{
+
+    QModelIndex row = selectedRow();
+
+    if (row.isValid() == false) //If there was no row selected invalid row was returned
+        return;
+
+
+    //file name is asked. As the filename will be appended, there's no point in confirming owerwrite here
+    QString filename = QFileDialog::getSaveFileName(this, "", "", "*.kitchenalert",NULL,QFileDialog::DontConfirmOverwrite);
+
+    disableSelectionDependentButtons();
+
+    qDebug() << filename;
+
+    if (filename.isEmpty()) //user cancelled the dialog (or gave an empty name)
+    {
+        return;
+    }
+
+    if (!filename.endsWith(".kitchenalert"))
+    {
+        filename.append(".kitchenalert");
+
+    }
+
+    qDebug() << "filename appended to " << filename;
+
+
+    //MANUAL CONFIRMATION OF OWERWRITE
+
+    if ( QFile::exists(filename))
+    {
+         //ASK FOR CONFIRMATION
+
+        QString overwriteQuestion ("File ");
+        overwriteQuestion.append(filename);
+        overwriteQuestion.append(" already exists. Do you want to overwrite it?");
+        if (QMessageBox::question(this,"Confirm overwrite?", overwriteQuestion,QMessageBox::Yes | QMessageBox::No,QMessageBox::No) != QMessageBox::Yes)
+        {
+            return;
+        }
+    }
+
+
+
+
+    QString errorMessage(tr("Cannot write to file "));
+    errorMessage.append(filename);
+
+    if (!model_.saveTimer(row,filename)) //Save the file, if not successful give an error message
+    {
+        QMessageBox::critical(this,tr("Save timer failed!"), errorMessage);
+    }
+
+
+}
+
+void KitchenAlertMainWindow::loadTimer()
+{
+    QString filename = QFileDialog::getOpenFileName(this,"","",tr("KitchenAlert timer files (*.kitchenalert)"));
+    if (!filename.isEmpty())
+    {
+
+//        if (!filename.endsWith(".kitchenalert"))      //not needed, the dialog won't let the user to select files not ending with ".kitchenalert"
+//        {
+//            filename.append(".kitchenalert");
+//        }
+
+        QString errorTitle(tr("Failed to load file "));
+        errorTitle.append(filename);
+
+        Timer * p_timer = new Timer();
+        if (!p_timer->load(filename))
+        {
+            QMessageBox::critical(this,errorTitle,tr("Unable to open file or not a valid KitchenAlert timer file."));
+            delete p_timer;
+            return;
+        }
+
+        initializeTimer(p_timer);
+    }
+}
+
+void KitchenAlertMainWindow::initializeTimer(Timer *p_timer)
+{
+
+//connect alert
+
+
+connect(p_timer,SIGNAL(alert(QModelIndex)),this,SLOT(alert(QModelIndex)));
+
+
+//connect change sound functions
+
+connect(this,SIGNAL(defaultSoundEnabled()),p_timer,SLOT(enableDefaultSound()));
+connect(this,SIGNAL(soundChanged(QString)),p_timer,SLOT(changeAlertSound(QString)));
+
+
+//Disable buttons, as selection is cleared when view is refreshed to show the new timer
+
+disableSelectionDependentButtons();
+
+
+// give timers to the model (model wants list of timers now..)
+
+QList<Timer *> timerList;
+
+timerList.append(p_timer);
+model_.addTimers(timerList,true); //timer gets started in the model
+
+}
+
+
+
index a9e86a6..c8b4e44 100644 (file)
@@ -40,7 +40,7 @@ namespace Ui {
 /*! The main window class of KitchenAlert'
 
   @author Heli Hyvättinen
-  @date 2011-03-29
+  @date 2011-04-05
   @version 0.2.1
 
 Operates the UI.
@@ -117,6 +117,15 @@ public slots:
        */
     void remove();
 
+    /*! Opens a dialog for saving the selected timer and, if confirmed, saves the timer to a file.
+    */
+    void saveTimer();
+
+    /*! Opens a dialog for reading a timer from a timer file, and if confirmed opens and starts the timer.
+    */
+    void loadTimer();
+
+
 signals:
 
     void defaultSoundEnabled();
@@ -154,6 +163,8 @@ private:
     */
     void initializeAlertSound();
 
+    void initializeTimer(Timer * p_timer);
+
 
 };
 
index 5f13826..b8ddeac 100644 (file)
           </widget>
          </item>
          <item>
+          <widget class="QPushButton" name="SaveButton">
+           <property name="text">
+            <string>Save timer</string>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="QPushButton" name="OpenButton">
+           <property name="text">
+            <string>Open timer</string>
+           </property>
+          </widget>
+         </item>
+         <item>
           <spacer name="horizontalSpacer_2">
            <property name="orientation">
             <enum>Qt::Horizontal</enum>
index 5090c6d..e162590 100644 (file)
@@ -29,7 +29,7 @@ int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
     a.setApplicationName("KitchenAlert"); //a name is required to connect to DBus
-    a.setApplicationVersion("0.2.0");
+    a.setApplicationVersion("0.2.1");
     a.setOrganizationName("KitchenAlert");
     KitchenAlertMainWindow w;
 #if defined(Q_WS_S60)
diff --git a/src/savedtimers.cpp b/src/savedtimers.cpp
deleted file mode 100644 (file)
index 0de87cf..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/**************************************************************************
-        KitchenAlert
-
-        Copyright (C) 2010  Heli Hyvättinen
-        
-        This file is part of KitchenAlert.
-
-        Kitchen Alert is free software: you can redistribute it and/or modify
-        it under the terms of the GNU General Public License as published by
-        the Free Software Foundation, either version 3 of the License, or
-        (at your option) any later version.
-
-        This program is distributed in the hope that it will be useful,
-        but WITHOUT ANY WARRANTY; without even the implied warranty of
-        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-        GNU General Public License for more details.
-
-        You should have received a copy of the GNU General Public License
-        along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-**************************************************************************/
-
-
-
-
-
-#include "savedtimers.h"
-
-SavedTimers::SavedTimers(QObject *parent) :
-    QObject(parent)
-{
-}
diff --git a/src/savedtimers.h b/src/savedtimers.h
deleted file mode 100644 (file)
index 79ae9e6..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/**************************************************************************
-        KitchenAlert
-
-        Copyright (C) 2010  Heli Hyvättinen
-        
-        This file is part of KitchenAlert.
-
-        Kitchen Alert is free software: you can redistribute it and/or modify
-        it under the terms of the GNU General Public License as published by
-        the Free Software Foundation, either version 3 of the License, or
-        (at your option) any later version.
-
-        This program is distributed in the hope that it will be useful,
-        but WITHOUT ANY WARRANTY; without even the implied warranty of
-        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-        GNU General Public License for more details.
-
-        You should have received a copy of the GNU General Public License
-        along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-**************************************************************************/
-
-
-
-
-
-#ifndef SAVEDTIMERS_H
-#define SAVEDTIMERS_H
-
-#include <QObject>
-
-class SavedTimers : public QObject
-{
-    Q_OBJECT
-public:
-    explicit SavedTimers(QObject *parent = 0);
-
-signals:
-
-public slots:
-
-};
-
-#endif // SAVEDTIMERS_H
index efe8af9..c65e862 100644 (file)
@@ -27,6 +27,8 @@
 #include "timer.h"
 #include "currentalertstablemodel.h"
 #include <qdebug.h>
+#include <QFile>
+#include <QXmlStreamWriter>
 
 Timer::Timer(QObject *parent) :
     QObject(parent)
@@ -157,3 +159,53 @@ alertSound_.setSound(filename);
 }
 
 
+
+
+bool Timer::save(QString filename)
+{
+    QFile file(filename);
+
+    if (!file.open(QFile::WriteOnly | QFile::Text))
+    {
+       return false;
+    }
+
+    QXmlStreamWriter xmlWriter(&file);
+    xmlWriter.setAutoFormatting(true);
+    xmlWriter.writeStartDocument();
+    xmlWriter.writeStartElement("kitchenalert");
+    xmlWriter.writeStartElement("timer");
+    xmlWriter.writeAttribute("alert_text",_alertText);
+    xmlWriter.writeAttribute("time_in_seconds", QString().setNum(_originalTime));
+    xmlWriter.writeEndDocument(); //this should close all open elements
+
+    return true;
+}
+
+bool Timer::load(QString filename)
+{
+    QFile file (filename);
+    if (!file.open(QFile::ReadOnly | QFile::Text))
+    {
+        return false;
+    }
+
+    QXmlStreamReader reader;
+    reader.setDevice(&file);
+
+    reader.readNextStartElement();
+
+    if (reader.name() != "kitchenalert")
+        return false;
+
+    reader.readNextStartElement();
+    if (reader.name() != "timer")
+        return false;
+
+
+    _alertText = reader.attributes().value("alert_text").toString();
+    _originalTime = reader.attributes().value("time_in_seconds").toString().toInt();
+    return true;
+}
+
+
index 061475d..ff2c22b 100644 (file)
@@ -39,7 +39,7 @@
 /*! The timer class of KitchenAlert'
 
   @author Heli Hyvättinen
-  @date 2011-02-10
+  @date 2011-04-05
   @version 0.2.1
 
 The timer class of KitchenAlert.
@@ -72,6 +72,16 @@ public:
     /*! Returns whether the timer is alerting */
     bool isAlerting();
 
+    /*! Saves the timer to a file
+      @param filename The file to which to save.
+    */
+    bool save(QString filename);
+
+    /*! Loads a timer from a file
+      @param filename The file from which to load.
+    */
+    bool load(QString filename);
+
 
 signals:
     /*! Emitted when the remaining time in the timer has changed */