e543217199c27dd315cb77d7da631e63db0dffd8
[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     // languages
110     _langIndex = 0;
111     _langs.append (Language (QString (""), tr ("System")));
112     _langs.append (Language (QString ("en"), tr ("English")));
113     _langs.append (Language (QString ("ru"), tr ("Russian")));
114 }
115
116
117 QStringList Settings::updateIntervals () const
118 {
119     QStringList res;
120
121     res.append (tr ("Never"));
122     res.append (tr ("1 min"));
123     res.append (tr ("2 min"));
124     res.append (tr ("5 min"));
125     res.append (tr ("15 min"));
126     res.append (tr ("30 min"));
127
128     return res;
129 }
130
131
132 int Settings::intervalIndex2Minutes (int index) const
133 {
134     int int2min[] = { -1, 1, 2, 5, 15, 30 };
135
136     if (index < 0 || sizeof (int2min) / sizeof (int2min[0]) <= (unsigned int)index)
137         return -1;
138
139     return int2min[index];
140 }
141
142
143 int Settings::minutes2IntervalIndex (int minutes) const
144 {
145     switch (minutes) {
146         case -1:
147             return 0;
148         case 1:
149             return 1;
150         case 2:
151             return 2;
152         case 5:
153             return 3;
154         case 15:
155             return 4;
156         case 30:
157             return 5;
158         default:
159             return 0;
160     }
161 }