Update data when device unlocked.
[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     _updateIntervalIndex = minutes2IntervalIndex (settings.value ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex)).toInt ());
28
29     _langIndex = settings.value ("langIndex", _langIndex).toInt ();
30     if (_langIndex < 0 || _langIndex >= _langs.count ())
31         _langIndex = 0;
32 }
33
34
35 void Settings::save ()
36 {
37     QSettings settings;
38
39     settings.setValue ("region", _regionID);
40
41     settings.setValue ("checks/light", _checks[C_ShowLight]);
42     settings.setValue ("checks/rank", _checks[C_ShowRank]);
43     settings.setValue ("checks/time", _checks[C_ShowTime]);
44     settings.setValue ("checks/hint", _checks[C_ShowHint]);
45     settings.setValue ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]);
46     settings.setValue ("checks/updateOnGSM", _checks[C_UpdateOnGSM]);
47     settings.setValue ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]);
48
49     settings.setValue ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex));
50
51     settings.setValue ("langIndex", _langIndex);
52 }
53
54
55 void Settings::makeDefault ()
56 {
57     _regionID = "1";            // Default city
58
59     _cities["1"] = tr ("Moscow");
60     _cities["10174"] = tr ("St.Petersburg");
61     _cities["20544"] = tr ("Kiev");
62     _cities["11162"] = tr ("Ekaterinburg");
63
64     setCheck (C_ShowLight, true);
65     setCheck (C_ShowRank, true);
66     setCheck (C_ShowHint, true);
67
68     setCheck (C_UpdateOnWiFi, true);
69
70     setCheck (C_UpdateWhenLocked, true);
71
72     _updateIntervalIndex = 3;
73
74     // languages
75     _langIndex = 0;
76     _langs.append (Language (QString (""),   tr ("System")));
77     _langs.append (Language (QString ("en"), tr ("English")));
78     _langs.append (Language (QString ("ru"), tr ("Russian")));
79 }
80
81
82 QStringList Settings::updateIntervals () const
83 {
84     QStringList res;
85
86     res.append (tr ("Never"));
87     res.append (tr ("1 min"));
88     res.append (tr ("2 min"));
89     res.append (tr ("5 min"));
90     res.append (tr ("15 min"));
91     res.append (tr ("30 min"));
92
93     return res;
94 }
95
96
97 int Settings::intervalIndex2Minutes (int index) const
98 {
99     int int2min[] = { -1, 1, 2, 5, 15, 30 };
100
101     if (index < 0 || sizeof (int2min) / sizeof (int2min[0]) <= (unsigned int)index)
102         return -1;
103
104     return int2min[index];
105 }
106
107
108 int Settings::minutes2IntervalIndex (int minutes) const
109 {
110     switch (minutes) {
111         case -1:
112             return 0;
113         case 1:
114             return 1;
115         case 2:
116             return 2;
117         case 5:
118             return 3;
119         case 15:
120             return 4;
121         case 30:
122             return 5;
123         default:
124             return 0;
125     }
126 }