}
+bool CurrentAlertsTableModel::saveTimer(QModelIndex index, QString filename)
+{
+ return currentTimers_.at(index.row())->save(filename);
+}
+
/*! 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
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:
#include <QMenuBar>
#include <QMessageBox>
#include <QSettings>
+#include <QFileDialog>
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);
}
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
+
+}
+
+
+
/*! 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.
*/
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();
*/
void initializeAlertSound();
+ void initializeTimer(Timer * p_timer);
+
};
</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>
{
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)
+++ /dev/null
-/**************************************************************************
- 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)
-{
-}
+++ /dev/null
-/**************************************************************************
- 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
#include "timer.h"
#include "currentalertstablemodel.h"
#include <qdebug.h>
+#include <QFile>
+#include <QXmlStreamWriter>
Timer::Timer(QObject *parent) :
QObject(parent)
}
+
+
+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;
+}
+
+
/*! 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.
/*! 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 */