b4908e73947b48a7b438022f5836d7a4bdb15811
[jspeed] / src / speedalarm.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QFile>
20 #include <QMediaPlayer>
21 #include "speedalarm.h"
22 #include "settings.h"
23 #include "odometer.h"
24 #include "poialerts.h"
25 #include "mediaplayer.h"
26
27 SpeedAlarm::SpeedAlarm(): QObject(0), loaded_(false), enabled_(false), isOver_(false)
28 {
29 }
30
31 SpeedAlarm& SpeedAlarm::instance()
32 {
33     static SpeedAlarm instance;
34     return instance;
35 }
36
37 void SpeedAlarm::start()
38 {
39     if(!loaded_)
40     {
41         loadConfig();
42     }
43
44     if(enabled_)
45     {
46         connect(&(Odometer::instance()), SIGNAL(dataUpdated()), this, SLOT(onDataUpdated()));
47     }
48
49 }
50
51 void SpeedAlarm::loadConfig()
52 {
53     loaded_ = true;
54
55     bool enabled = Settings::instance().value("alarm_enabled", false).toBool();
56
57     if(enabled)
58     {
59         enabled_ = true;
60
61         QString sound = Settings::instance().value("alarm_sound", "").toString();
62
63         if(sound.isEmpty())
64         {
65             enabled_ = false;
66             return;
67         }
68
69         QString soundDir = MediaPlayer::getSoundDir();
70
71         if(QFile::exists(soundDir + sound))
72         {
73             soundFile_ = soundDir + sound;
74         }
75         else if(QFile::exists(MediaPlayer::getLocalSoundDir() + sound))
76         {
77             soundFile_ = MediaPlayer::getLocalSoundDir() + sound;
78         }
79         else
80         {
81             enabled_ = false;
82             return;
83         }
84
85         threshold_ = Settings::instance().value("alarm_threshold", 0).toInt();
86
87         start();
88     }
89     else
90     {
91         end();
92         enabled_ = false;
93     }
94 }
95
96 void SpeedAlarm::end()
97 {
98     if(enabled_)
99     {
100         disconnect(&(Odometer::instance()), SIGNAL(dataUpdated()), this, SLOT(onDataUpdated()));
101     }
102 }
103
104 void SpeedAlarm::onDataUpdated()
105 {
106     if(Odometer::instance().getLatestFix().kmSpeed > threshold_)
107     {
108         if(!isOver_)
109         {
110             MediaPlayer::play(soundFile_);
111             isOver_ = true;
112         }
113     }
114     else
115     {
116         isOver_ = false;
117     }
118 }