From ef0252dea07c46470f7b1c94b903052aa68be3ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Heli=20Hyv=C3=A4ttinen?= Date: Wed, 6 Apr 2011 11:43:14 +0300 Subject: [PATCH] Added saving and loading of timers --- src/currentalertstablemodel.cpp | 5 ++ src/currentalertstablemodel.h | 9 ++- src/kitchenalertmainwindow.cpp | 120 +++++++++++++++++++++++++++++++++++++++ src/kitchenalertmainwindow.h | 13 ++++- src/kitchenalertmainwindow.ui | 14 +++++ src/main.cpp | 2 +- src/savedtimers.cpp | 32 ----------- src/savedtimers.h | 44 -------------- src/timer.cpp | 52 +++++++++++++++++ src/timer.h | 12 +++- 10 files changed, 223 insertions(+), 80 deletions(-) delete mode 100644 src/savedtimers.cpp delete mode 100644 src/savedtimers.h diff --git a/src/currentalertstablemodel.cpp b/src/currentalertstablemodel.cpp index 9913288..62e8327 100644 --- a/src/currentalertstablemodel.cpp +++ b/src/currentalertstablemodel.cpp @@ -325,3 +325,8 @@ void CurrentAlertsTableModel::removeTimer(QModelIndex index) } +bool CurrentAlertsTableModel::saveTimer(QModelIndex index, QString filename) +{ + return currentTimers_.at(index.row())->save(filename); +} + diff --git a/src/currentalertstablemodel.h b/src/currentalertstablemodel.h index 8f4622a..b20ead1 100755 --- a/src/currentalertstablemodel.h +++ b/src/currentalertstablemodel.h @@ -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: diff --git a/src/kitchenalertmainwindow.cpp b/src/kitchenalertmainwindow.cpp index 4fc1c18..b1bd95c 100644 --- a/src/kitchenalertmainwindow.cpp +++ b/src/kitchenalertmainwindow.cpp @@ -43,6 +43,7 @@ #include #include #include +#include @@ -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 timerList; + +timerList.append(p_timer); +model_.addTimers(timerList,true); //timer gets started in the model + +} + + + diff --git a/src/kitchenalertmainwindow.h b/src/kitchenalertmainwindow.h index a9e86a6..c8b4e44 100644 --- a/src/kitchenalertmainwindow.h +++ b/src/kitchenalertmainwindow.h @@ -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); + }; diff --git a/src/kitchenalertmainwindow.ui b/src/kitchenalertmainwindow.ui index 5f13826..b8ddeac 100644 --- a/src/kitchenalertmainwindow.ui +++ b/src/kitchenalertmainwindow.ui @@ -110,6 +110,20 @@ + + + Save timer + + + + + + + Open timer + + + + Qt::Horizontal diff --git a/src/main.cpp b/src/main.cpp index 5090c6d..e162590 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 index 0de87cf..0000000 --- a/src/savedtimers.cpp +++ /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 . - -**************************************************************************/ - - - - - -#include "savedtimers.h" - -SavedTimers::SavedTimers(QObject *parent) : - QObject(parent) -{ -} diff --git a/src/savedtimers.h b/src/savedtimers.h deleted file mode 100644 index 79ae9e6..0000000 --- a/src/savedtimers.h +++ /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 . - -**************************************************************************/ - - - - - -#ifndef SAVEDTIMERS_H -#define SAVEDTIMERS_H - -#include - -class SavedTimers : public QObject -{ - Q_OBJECT -public: - explicit SavedTimers(QObject *parent = 0); - -signals: - -public slots: - -}; - -#endif // SAVEDTIMERS_H diff --git a/src/timer.cpp b/src/timer.cpp index efe8af9..c65e862 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -27,6 +27,8 @@ #include "timer.h" #include "currentalertstablemodel.h" #include +#include +#include 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; +} + + diff --git a/src/timer.h b/src/timer.h index 061475d..ff2c22b 100644 --- a/src/timer.h +++ b/src/timer.h @@ -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 */ -- 1.7.9.5