81741775cb2c82a814cbfea14d32ab963692708e
[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
44
45 int Timer::getOriginalTimeInSeconds()
46 {
47     return _originalTime;
48 }
49
50 void Timer::setOriginalTimeInSeconds(int seconds)
51 {
52 _originalTime = seconds;
53 }
54
55 int Timer::getRemainingTimeInSeconds()
56 {
57     return _remainingTime;
58 }
59
60 QString Timer::getAlertText()
61 {
62     return _alertText;
63 }
64
65 void Timer::setAlertText(QString text)
66 {
67     _alertText = text;
68 }
69
70 void Timer::secondPassed()
71 {
72     _remainingTime--;
73
74
75     if (_remainingTime == 0)
76     {
77         alerting_ = true;
78         alertSound_.play();
79         emit alert(whereAmI());
80 //        qDebug() << "alerting";
81     }
82
83     emit remainingTimeChanged(); //after alerting in case of alert so that status gets updated immediately
84 }
85
86 void Timer::start()
87 {
88     _remainingTime = _originalTime;
89     _actualTimer.start();
90
91
92
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
94     {
95         alerting_  = true;
96         alertSound_.play();
97         emit alert(whereAmI());
98 //        qDebug () << "Alerting 00:00:00 from row: " << whereAmI().row();
99     }
100
101     else
102     {
103         alerting_ = false;
104         alertSound_.stop();
105     }
106
107 }
108
109
110 void Timer::stop()
111 {
112     _actualTimer.stop();
113     _remainingTime = 0; //Stopped timer shows 00:00:00
114
115     alerting_ = false;
116     alertSound_.stop();
117 }
118
119 void Timer::snooze()
120 {
121     _remainingTime = 120;
122
123     alerting_ = false;
124     alertSound_.stop();
125 }
126
127
128 bool Timer::isAlerting()
129 {
130     return alerting_;
131 }
132
133 QModelIndex Timer::whereAmI()
134 {
135
136
137    QObject * p_parent = parent();
138
139
140    CurrentAlertsTableModel* p_parentModel = qobject_cast<CurrentAlertsTableModel *> (p_parent);
141
142    if (p_parentModel == NULL) //If no parent or parent is not of right class, return an invalid index
143         return QModelIndex();
144
145
146   return p_parentModel->giveIndexForTimer(this);
147
148
149 }
150
151 void Timer::enableDefaultSound()
152 {
153 alertSound_.setDefaultSound();
154 }
155
156 void Timer::changeAlertSound(QString filename)
157 {
158 alertSound_.setSound(filename);
159 }
160
161
162
163
164 bool Timer::save(QString filename)
165 {
166     QFile file(filename);
167
168     if (!file.open(QFile::WriteOnly | QFile::Text))
169     {
170        return false;
171     }
172
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
181
182     return true;
183 }
184
185 bool Timer::load(QString filename)
186 {
187     QFile file (filename);
188     if (!file.open(QFile::ReadOnly | QFile::Text))
189     {
190         return false;
191     }
192
193     QXmlStreamReader reader;
194     reader.setDevice(&file);
195
196     reader.readNextStartElement();
197
198     if (reader.name() != "kitchenalert")
199         return false;
200
201     reader.readNextStartElement();
202     if (reader.name() != "timer")
203         return false;
204
205
206     _alertText = reader.attributes().value("alert_text").toString();
207     _originalTime = reader.attributes().value("time_in_seconds").toString().toInt();
208
209     filenameWithPath_ = filename;
210
211     return true;
212 }
213
214 QString Timer::getFilename()
215 {
216     return filenameWithPath_;
217 }
218
219