1 /**************************************************************************
4 Copyright (C) 2010-2011 Heli Hyvättinen
6 This file is part of KitchenAlert.
8 Kitchen Alert is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 **************************************************************************/
28 #include "currentalertstablemodel.h"
31 #include <QXmlStreamWriter>
33 Timer::Timer(QObject *parent) :
38 _actualTimer.setInterval(1000); // 1 s
39 connect(&_actualTimer, SIGNAL(timeout()), this, SLOT(secondPassed()));
45 int Timer::getOriginalTimeInSeconds()
50 void Timer::setOriginalTimeInSeconds(int seconds)
52 _originalTime = seconds;
55 int Timer::getRemainingTimeInSeconds()
57 return _remainingTime;
60 QString Timer::getAlertText()
65 void Timer::setAlertText(QString text)
70 void Timer::secondPassed()
75 if (_remainingTime == 0)
79 emit alert(whereAmI());
80 // qDebug() << "alerting";
83 emit remainingTimeChanged(); //after alerting in case of alert so that status gets updated immediately
88 _remainingTime = _originalTime;
93 if (_originalTime == 0) //a 00:00:00 alert has to be checked here, since it's already negative when checked for the next time
97 emit alert(whereAmI());
98 // qDebug () << "Alerting 00:00:00 from row: " << whereAmI().row();
113 _remainingTime = 0; //Stopped timer shows 00:00:00
121 _remainingTime = 120;
128 bool Timer::isAlerting()
133 QModelIndex Timer::whereAmI()
137 QObject * p_parent = parent();
140 CurrentAlertsTableModel* p_parentModel = qobject_cast<CurrentAlertsTableModel *> (p_parent);
142 if (p_parentModel == NULL) //If no parent or parent is not of right class, return an invalid index
143 return QModelIndex();
146 return p_parentModel->giveIndexForTimer(this);
151 void Timer::enableDefaultSound()
153 alertSound_.setDefaultSound();
156 void Timer::changeAlertSound(QString filename)
158 alertSound_.setSound(filename);
164 bool Timer::save(QString filename)
166 QFile file(filename);
168 if (!file.open(QFile::WriteOnly | QFile::Text))
173 QXmlStreamWriter xmlWriter(&file);
174 xmlWriter.setAutoFormatting(true);
175 xmlWriter.writeStartDocument();
176 xmlWriter.writeStartElement("kitchenalert");
177 xmlWriter.writeStartElement("timer");
178 xmlWriter.writeAttribute("alert_text",_alertText);
179 xmlWriter.writeAttribute("time_in_seconds", QString().setNum(_originalTime));
180 xmlWriter.writeEndDocument(); //this should close all open elements
185 bool Timer::load(QString filename)
187 QFile file (filename);
188 if (!file.open(QFile::ReadOnly | QFile::Text))
193 QXmlStreamReader reader;
194 reader.setDevice(&file);
196 reader.readNextStartElement();
198 if (reader.name() != "kitchenalert")
201 reader.readNextStartElement();
202 if (reader.name() != "timer")
206 _alertText = reader.attributes().value("alert_text").toString();
207 _originalTime = reader.attributes().value("time_in_seconds").toString().toInt();
209 filenameWithPath_ = filename;
214 QString Timer::getFilename()
216 return filenameWithPath_;