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