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