Added saving of alerts - unstable!!!
[kitchenalert] / src / timer.cpp
1 /**************************************************************************
2         KitchenAlert
3
4         Copyright (C) 2010  Heli Hyvättinen
5
6         This file is part of KitchenAlert.
7
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.
12
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.
17
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/>.
20
21 **************************************************************************/
22
23
24
25
26
27 #include "timer.h"
28 #include "currentalertstablemodel.h"
29 #include <qdebug.h>
30 #include <QFile>
31 #include <QXmlStreamWriter>
32 #include <QXmlStreamReader>
33
34 Timer::Timer(QObject *parent) :
35     QObject(parent)
36 {
37     _originalTime = 0;
38
39     _actualTimer.setInterval(1000); // 1 s
40     connect(&_actualTimer, SIGNAL(timeout()), this, SLOT(secondPassed()));
41
42     alerting_ = false;
43 }
44
45
46
47 int Timer::getOriginalTimeInSeconds()
48 {
49     return _originalTime;
50 }
51
52 void Timer::setOriginalTimeInSeconds(int seconds)
53 {
54 _originalTime = seconds;
55 }
56
57 int Timer::getRemainingTimeInSeconds()
58 {
59     return _remainingTime;
60 }
61
62 QString Timer::getAlertText()
63 {
64     return _alertText;
65 }
66
67 void Timer::setAlertText(QString text)
68 {
69     _alertText = text;
70 }
71
72 void Timer::secondPassed()
73 {
74     _remainingTime--;
75
76
77     if (_remainingTime == 0)
78     {
79         alerting_ = true;
80         emit alert(whereAmI());
81         qDebug() << "alerted";
82     }
83
84     emit remainingTimeChanged(); //after alerting in case of alert so that status gets updated immediately
85 }
86
87 void Timer::start()
88 {
89     _remainingTime = _originalTime;
90     _actualTimer.start();
91
92     alerting_ = false;
93
94     if (_originalTime == 0) //has to be checked here, since 00:00:00 alert will already be negative when checked next time
95     //THIS ALERTS EVERY SECOND TIME THE TIMER IS STARTED! //This bug disappeared without explanation...
96     {
97         alerting_ = true;
98         emit alert(whereAmI());
99         qDebug () << "Alerted for 00:00:00 alert";
100     }
101 }
102
103
104 void Timer::stop()
105 {
106     _actualTimer.stop();
107     _remainingTime = 0; //Stopped timer shows 00:00:00
108
109     alerting_ = false;
110 }
111
112 void Timer::snooze()
113 {
114     _remainingTime = 120;
115
116     alerting_ = false;
117 }
118
119
120 bool Timer::isAlerting()
121 {
122     return alerting_;
123 }
124
125 QModelIndex Timer::whereAmI()
126 {
127
128
129    QObject * p_parent = parent();
130
131
132    CurrentAlertsTableModel* p_parentModel = qobject_cast<CurrentAlertsTableModel *> (p_parent);
133
134    if (p_parentModel == NULL) //If no parent or parent is not of right class, return an invalid index
135         return QModelIndex();
136
137
138   return p_parentModel->giveIndexForTimer(this);
139
140
141 }
142
143 bool Timer::save(QString filename)
144 {
145     QFile file(filename);
146
147     if (!file.open(QFile::WriteOnly | QFile::Text))
148     {
149        return false;
150     }
151
152     QXmlStreamWriter xmlWriter(&file);
153     xmlWriter.setAutoFormatting(true);
154     xmlWriter.writeStartDocument();
155     xmlWriter.writeStartElement("kitchenalert");
156     xmlWriter.writeStartElement("timer");
157     xmlWriter.writeAttribute("alert_text",_alertText);
158     xmlWriter.writeAttribute("time_in_seconds", QString().setNum(_originalTime));
159     xmlWriter.writeEndDocument(); //this should close all open elements
160
161     return true;
162 }
163
164 bool Timer::load(QString filename)
165 {
166     QFile file (filename);
167     if (!file.open(QFile::ReadOnly | QFile::Text))
168     {
169         return false;
170     }
171
172     QXmlStreamReader reader;
173     reader.setDevice(&file);
174
175     reader.readNextStartElement();
176
177     if (reader.name() != "kitchenalert")
178         return false;
179
180     reader.readNextStartElement();
181     if (reader.name() != "timer")
182         return false;
183
184
185     _alertText = reader.attributes().value("alert_text").toString();
186     _originalTime = reader.attributes().value("time_in_seconds").toString().toInt();
187     return true;
188
189
190
191
192 }
193