Check for device state on update.
[yandex-traffic] / settings.cpp
1 #include <QtCore>
2 #include <settings.hpp>
3
4
5 Settings::Settings ()
6 {
7     load ();
8 }
9
10
11 void Settings::load ()
12 {
13     QSettings settings;
14
15     makeDefault ();
16
17     _regionID = settings.value ("region", _regionID).toString ();
18
19     _checks[C_ShowLight] = settings.value ("checks/light", _checks[C_ShowLight]).toBool ();
20     _checks[C_ShowRank] = settings.value ("checks/rank", _checks[C_ShowRank]).toBool ();
21     _checks[C_ShowTime] = settings.value ("checks/time", _checks[C_ShowTime]).toBool ();
22     _checks[C_ShowHint] = settings.value ("checks/hint", _checks[C_ShowHint]).toBool ();
23     _checks[C_UpdateOnWiFi] = settings.value ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]).toBool ();
24     _checks[C_UpdateOnGSM] = settings.value ("checks/updateOnGSM", _checks[C_UpdateOnGSM]).toBool ();
25     _checks[C_UpdateWhenLocked] = settings.value ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]).toBool ();
26
27     loadCities (&settings);
28
29     _updateIntervalIndex = minutes2IntervalIndex (settings.value ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex)).toInt ());
30 }
31
32
33 void Settings::save ()
34 {
35     QSettings settings;
36
37     settings.setValue ("region", _regionID);
38
39     settings.setValue ("checks/light", _checks[C_ShowLight]);
40     settings.setValue ("checks/rank", _checks[C_ShowRank]);
41     settings.setValue ("checks/time", _checks[C_ShowTime]);
42     settings.setValue ("checks/hint", _checks[C_ShowHint]);
43     settings.setValue ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]);
44     settings.setValue ("checks/updateOnGSM", _checks[C_UpdateOnGSM]);
45     settings.setValue ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]);
46
47     settings.setValue ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex));
48
49     saveCities (&settings);
50 }
51
52
53 void Settings::loadCities (QSettings *settings)
54 {
55     QMap<QString, QVariant> v;
56     QMap<QString, QVariant>::const_iterator it;
57
58     v = settings->value ("cities", v).toMap ();
59
60     if (v.size () == 0)
61         return;
62
63     it = v.begin ();
64     _cities.clear ();
65
66     while (it != v.end ()) {
67         _cities[it.key ()] = it.value ().toString ();
68         it++;
69     }
70 }
71
72
73 void Settings::saveCities (QSettings *settings)
74 {
75     QMap<QString, QVariant> v;
76     QMap<QString, QString>::const_iterator it;
77
78     it = _cities.begin ();
79
80     while (it != _cities.end ()) {
81         v[it.key ()] = it.value ();
82         it++;
83     }
84
85     settings->setValue ("cities", v);
86 }
87
88
89 void Settings::makeDefault ()
90 {
91     _regionID = "1";            // Default city
92
93     _cities["1"] = tr ("Moscow");
94     _cities["10174"] = tr ("St.Petersburg");
95     _cities["20544"] = tr ("Kiev");
96     _cities["11162"] = tr ("Ekaterinburg");
97
98     setCheck (C_ShowLight, true);
99     setCheck (C_ShowRank, true);
100     setCheck (C_ShowHint, true);
101
102     setCheck (C_UpdateOnWiFi, true);
103
104     setCheck (C_UpdateWhenLocked, true);
105
106     _updateIntervalIndex = 3;
107 }
108
109
110 QStringList Settings::updateIntervals () const
111 {
112     QStringList res;
113
114     res.append (tr ("Never"));
115     res.append (tr ("1 min"));
116     res.append (tr ("2 min"));
117     res.append (tr ("5 min"));
118     res.append (tr ("15 min"));
119     res.append (tr ("30 min"));
120
121     return res;
122 }
123
124
125 int Settings::intervalIndex2Minutes (int index) const
126 {
127     int int2min[] = { -1, 1, 2, 5, 15, 30 };
128
129     if (index < 0 || sizeof (int2min) / sizeof (int2min[0]) <= (unsigned int)index)
130         return -1;
131
132     return int2min[index];
133 }
134
135
136 int Settings::minutes2IntervalIndex (int minutes) const
137 {
138     switch (minutes) {
139         case -1:
140             return 0;
141         case 1:
142             return 1;
143         case 2:
144             return 2;
145         case 5:
146             return 3;
147         case 15:
148             return 4;
149         case 30:
150             return 5;
151         default:
152             return 0;
153     }
154 }