1a182b972070922725cc870f749f3800650425a3
[kitchenalert] / src / timer.cpp
1 /**************************************************************************
2         KitchenAlert
3
4         Copyright (C) 2010-2011  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
33 Timer::Timer(QObject *parent) :
34     QObject(parent)
35 {
36     _originalTime = 0;
37
38     _actualTimer.setInterval(1000); // 1 s
39     connect(&_actualTimer, SIGNAL(timeout()), this, SLOT(secondPassed()));
40
41     alerting_ = false;
42
43     _remainingTime = 0; //Same as when stopped
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         alertSound_.play();
81         emit alert(whereAmI());
82 //        qDebug() << "alerting";
83     }
84
85     emit remainingTimeChanged(); //after alerting in case of alert so that status gets updated immediately
86 }
87
88 void Timer::start()
89 {
90     _remainingTime = _originalTime;
91     _actualTimer.start();
92
93
94
95     if (_originalTime == 0) //a 00:00:00 alert has to  be checked here, since it's already negative when checked for the next time
96     {
97         alerting_  = true;
98         alertSound_.play();
99         emit alert(whereAmI());
100 //        qDebug () << "Alerting 00:00:00 from row: " << whereAmI().row();
101     }
102
103     else
104     {
105         alerting_ = false;
106         alertSound_.stop();
107     }
108
109 }
110
111
112 void Timer::stop()
113 {
114     _actualTimer.stop();
115     _remainingTime = 0; //Stopped timer shows 00:00:00
116
117     alerting_ = false;
118     alertSound_.stop();
119 }
120
121 void Timer::snooze()
122 {
123     _remainingTime = 120;
124
125     alerting_ = false;
126     alertSound_.stop();
127 }
128
129
130 bool Timer::isAlerting()
131 {
132     return alerting_;
133 }
134
135 QModelIndex Timer::whereAmI()
136 {
137
138
139    QObject * p_parent = parent();
140
141
142    CurrentAlertsTableModel* p_parentModel = qobject_cast<CurrentAlertsTableModel *> (p_parent);
143
144    if (p_parentModel == NULL) //If no parent or parent is not of right class, return an invalid index
145         return QModelIndex();
146
147
148   return p_parentModel->giveIndexForTimer(this);
149
150
151 }
152
153 void Timer::enableDefaultSound()
154 {
155 alertSound_.setDefaultSound();
156 }
157
158 void Timer::changeAlertSound(QString filename)
159 {
160 alertSound_.setSound(filename);
161 }
162
163
164
165
166 bool Timer::save(QString filename)
167 {
168     QFile file(filename);
169
170     if (!file.open(QFile::WriteOnly | QFile::Text))
171     {
172        return false;
173     }
174
175     QXmlStreamWriter xmlWriter(&file);
176     xmlWriter.setAutoFormatting(true);
177     xmlWriter.writeStartDocument();
178     xmlWriter.writeStartElement("kitchenalert");
179     xmlWriter.writeStartElement("timer");
180     xmlWriter.writeAttribute("alert_text",_alertText);
181     xmlWriter.writeAttribute("time_in_seconds", QString().setNum(_originalTime));
182     xmlWriter.writeEndDocument(); //this should close all open elements
183
184     return true;
185 }
186
187 bool Timer::load(QString filename)
188 {
189     QFile file (filename);
190     if (!file.open(QFile::ReadOnly | QFile::Text))
191     {
192         return false;
193     }
194
195     QXmlStreamReader reader;
196     reader.setDevice(&file);
197
198     reader.readNextStartElement();
199
200     if (reader.name() != "kitchenalert")
201         return false;
202
203     reader.readNextStartElement();
204     if (reader.name() != "timer")
205         return false;
206
207
208     _alertText = reader.attributes().value("alert_text").toString();
209     _originalTime = reader.attributes().value("time_in_seconds").toString().toInt();
210
211     filenameWithPath_ = filename;
212
213     return true;
214 }
215
216 //QString Timer::getFilename()
217 //{
218 //    return filenameWithPath_;
219 //}
220
221