-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabyphone.cpp
268 lines (222 loc) · 7.98 KB
/
babyphone.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
babyphone - A baby monitor application for Maemo / MeeGo (Nokia N900, N950, N9).
Copyright (C) 2011 Roman Morawek <[email protected]>
This program 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 "babyphone.h"
#include <QDebug>
/*!
the constructor instantiates the main subclasses for the functionality, i.e.
the audio monitor, the call monitor and the user notifier. Afterwards it
starts audio capturing.
*/
Babyphone::Babyphone(const Settings *settings, QObject *parent) :
QObject(parent), itsSettings(settings)
{
// setup profile switcher
itsProfileSwitcher = new ProfileSwitcher(itsSettings, this);
// setup state variables
itsState = STATE_OFF;
itsNotificationPending = false;
// setup audio monitor
itsAudioMonitor = new AudioMonitor(itsSettings, this);
connect(itsAudioMonitor, SIGNAL(update(int, int)), this, SLOT(refreshAudioData(int, int)));
// setup call monitor
itsCallMonitor = new CallMonitor(itsSettings, this);
connect(itsCallMonitor, SIGNAL(callReceived(QString)),
this, SLOT(callReceived(QString)));
connect(itsCallMonitor, SIGNAL(callFinished()),
this, SLOT(callFinished()));
// setup phone call handlers
itsUserNotifier = new UserNotifier(itsSettings, itsCallMonitor);
connect(itsUserNotifier, SIGNAL(notifyFinished()), this, SLOT(notifyFinished()));
connect(itsCallMonitor, SIGNAL(callStatusChanged(bool)),
itsUserNotifier, SLOT(callStatusChanged(bool)));
// start audio capturing
startAudio();
}
/*!
setState switches the application state of babyphone.
On enabling, it clears the statistics counters.
*/
void Babyphone::setState(State state)
{
if (state == itsState)
qWarning() << "Application state switch to old state:" << state;
// if we just switch on, clear old statics
if (itsState == STATE_OFF) {
// reset statistic counters
itsUserNotifier->itsCallCounterInvoke = 0;
itsUserNotifier->itsCallCounterTaken = 0;
itsUserNotifier->itsCallCounterTimeout = 0;
}
qDebug() << "New application state:" << (state == STATE_OFF ? "off" :
state == STATE_WAITING ? "inactive on" : "on");
itsState = state;
}
/*!
getStatistics provides a string summary of call statistcs.
*/
QString Babyphone::getStatistics() const
{
QString text;
if (itsUserNotifier->itsCallCounterInvoke > 0) {
text = tr("Notifications total: %1\n"
"Calls taken: %2\n"
"Unanswered or timed out: %3")
.arg(itsUserNotifier->itsCallCounterInvoke)
.arg(itsUserNotifier->itsCallCounterTaken)
.arg(itsUserNotifier->itsCallCounterTimeout);
}
else {
text = tr("No notifications took place.");
}
return text;
}
/*!
startAudio starts audio capturing. In case of failures it starts a retry
timer.
*/
void Babyphone::startAudio()
{
qDebug() << "Start audio capturing";
bool success = itsAudioMonitor->start();
if (success == false) {
qWarning() << "starting of audio failed, retrying later";
QTimer::singleShot(itsSettings->AUDIO_RETRY_TIMER, this, SLOT(startAudio()));
}
}
/*!
stopAudio deactivates audio capturing.
*/
void Babyphone::stopAudio()
{
// suspend audio monitoring
qDebug() << "Stop audio capturing";
itsAudioMonitor->stop();
}
/*!
refreshAudioData periodically receives the audio samples from the AudioMonitor,
and performs the threshold check to initiate a phone call if needed.
*/
void Babyphone::refreshAudioData(int counter, int value)
{
// update GUI
emit newAudioData(counter, value);
// check for noise
if ( (itsState == STATE_ON) &&
(counter > itsSettings->THRESHOLD_VALUE) &&
(!itsCallMonitor->itsCallPending) &&
(!itsNotificationPending) )
{
qDebug() << "Audio threshold reached. Notifying user.";
// reset audio monitor warning
itsAudioMonitor->itsCounter = 0;
// notify user
if (itsUserNotifier->Notify() == true) {
// store event
itsNotificationPending = true;
// stop monitoring
// we cannot do this directly because we got called from within the
// audio device sampling; start single shot timer instead
QTimer::singleShot(0, this, SLOT(stopAudio()));
// signal new state
emit newCallStatus(false, true);
}
else {
// the notify command yielded an error
emit notificationError();
}
}
}
/*!
callReceived handles incoming phone calls.
*/
void Babyphone::callReceived(QString phoneNumber)
{
// stop audio capturing in all cases, to allow audion connection also in
// manual taken calls
// we can do this directly here and do not need a single shot timer
stopAudio();
// we only monitor calls if the application is in active state
if (itsState != STATE_OFF) {
// we only handle incoming calls if enabled by settings
if (itsSettings->itsHandleIncomingCalls) {
// handle incoming calls
// take the call if it is from the parent's phone and no call is pending
if ( (itsSettings->itsContact.IsNumberMatching(phoneNumber)) &&
(!itsCallMonitor->itsCallPending) ) {
// this is handled as an outgoing call
itsNotificationPending = true;
// take call
itsCallMonitor->takeCall();
// signal new state
emit newCallStatus(false, false);
}
else {
// drop the call
if (itsCallMonitor->dropCall()) {
// notify parents on this event
itsUserNotifier->notifySMS(phoneNumber);
}
}
}
else {
// call handling is inactive
// nevertheless, we signal the call
itsUserNotifier->notifySMS(phoneNumber);
}
}
}
/*!
callFinished represents the slot that gets called as a phone call finishes.
It starts the timer to wait for phone app exit. Also it signals the change of
call status.
*/
void Babyphone::callFinished()
{
// bring the application back to focus and restart audio capturing
// we need a timeout for the phone application to quit
QTimer::singleShot(itsSettings->REFOCUS_TIMER, this, SLOT(phoneAppTimeout()));
// signal new state
emit newCallStatus(true, itsNotificationPending);
}
/*!
notifyFinished represents the slot that gets called as the UserNotifier
finished its phone call or the notify script. In case of script settings it
forwards the signal to callFinished.
*/
void Babyphone::notifyFinished()
{
// check whether we executed a script instead of the phone call
if (!itsSettings->itsUserNotifyScript.isEmpty()) {
// we notified by script
// therefore, we did not get a call finished event
// thus, this event is used to re-enable audio
callFinished();
}
}
/*!
phoneAppTimeout puts the application window to full screen foreground and
starts audio capturing.
*/
void Babyphone::phoneAppTimeout()
{
// notifcation finished
itsNotificationPending = false;
// reset audio monitor warning
itsAudioMonitor->itsCounter = 0;
// restart audio monitoring
startAudio();
// signal event
emit phoneApplicationFinished();
}