Fix typo
[yandex-traffic] / settings.cpp
1 #include <QtCore>
2 #include <settings.hpp>
3
4 #include "globals.hpp"
5
6
7 // --------------------------------------------------
8 // Language
9 // --------------------------------------------------
10 QString Language::title () const
11 {
12     return Settings::tr (_title.toUtf8 ());
13 }
14
15
16 // --------------------------------------------------
17 // Settings
18 // --------------------------------------------------
19 static Settings* _settings;
20
21
22 Settings* Settings::instance ()
23 {
24     if (!_settings)
25         _settings = new Settings;
26     return _settings;
27 }
28
29
30 Settings::Settings ()
31     : _ts (NULL)
32 {
33     load ();
34 }
35
36
37 void Settings::load ()
38 {
39     QSettings settings;
40
41     makeDefault ();
42
43     _regionID = settings.value ("region", _regionID).toString ();
44
45     _checks[C_ShowLight] = settings.value ("checks/light", _checks[C_ShowLight]).toBool ();
46     _checks[C_ShowRank] = settings.value ("checks/rank", _checks[C_ShowRank]).toBool ();
47     _checks[C_ShowTime] = settings.value ("checks/time", _checks[C_ShowTime]).toBool ();
48     _checks[C_ShowHint] = settings.value ("checks/hint", _checks[C_ShowHint]).toBool ();
49     _checks[C_UpdateOnWiFi] = settings.value ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]).toBool ();
50     _checks[C_UpdateOnGSM] = settings.value ("checks/updateOnGSM", _checks[C_UpdateOnGSM]).toBool ();
51     _checks[C_UpdateWhenLocked] = settings.value ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]).toBool ();
52
53     _updateIntervalIndex = minutes2IntervalIndex (settings.value ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex)).toInt ());
54
55     setLanguageIndex (settings.value ("langIndex", _langIndex).toInt ());
56 }
57
58
59 void Settings::save ()
60 {
61     QSettings settings;
62
63     settings.setValue ("region", _regionID);
64
65     settings.setValue ("checks/light", _checks[C_ShowLight]);
66     settings.setValue ("checks/rank", _checks[C_ShowRank]);
67     settings.setValue ("checks/time", _checks[C_ShowTime]);
68     settings.setValue ("checks/hint", _checks[C_ShowHint]);
69     settings.setValue ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]);
70     settings.setValue ("checks/updateOnGSM", _checks[C_UpdateOnGSM]);
71     settings.setValue ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]);
72
73     settings.setValue ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex));
74
75     settings.setValue ("langIndex", _langIndex);
76 }
77
78
79 // This routine called before any translations loaded, so strings must be translated explicitly.
80 void Settings::makeDefault ()
81 {
82     _regionID = "1";            // Default city
83
84     _cities["1"] = tr ("Moscow");
85     _cities["10174"] = tr ("St.Petersburg");
86     _cities["20544"] = tr ("Kiev");
87     _cities["11162"] = tr ("Ekaterinburg");
88
89     setCheck (C_ShowLight, true);
90     setCheck (C_ShowRank, true);
91     setCheck (C_ShowHint, true);
92
93     setCheck (C_UpdateOnWiFi, true);
94
95     setCheck (C_UpdateWhenLocked, true);
96
97     _updateIntervalIndex = 3;
98
99     // languages
100     _langs.append (Language (QString (""),   tr ("System")));
101     _langs.append (Language (QString ("en"), tr ("English")));
102     _langs.append (Language (QString ("ru"), tr ("Russian")));
103     setLanguageIndex (0);
104 }
105
106
107 QStringList Settings::updateIntervals () const
108 {
109     QStringList res;
110
111     res.append (tr ("Never"));
112     res.append (tr ("1 minute"));
113     res.append (tr ("2 minutes"));
114     res.append (tr ("5 minutes"));
115     res.append (tr ("15 minutes"));
116     res.append (tr ("30 minutes"));
117     res.append (tr ("1 hour"));
118     res.append (tr ("2 hours"));
119
120     return res;
121 }
122
123
124 int Settings::intervalIndex2Minutes (int index) const
125 {
126     int int2min[] = { -1, 1, 2, 5, 15, 30, 60, 120 };
127
128     if (index < 0 || sizeof (int2min) / sizeof (int2min[0]) <= (unsigned int)index)
129         return -1;
130
131     return int2min[index];
132 }
133
134
135 int Settings::minutes2IntervalIndex (int minutes) const
136 {
137     switch (minutes) {
138         case -1:
139             return 0;
140         case 1:
141             return 1;
142         case 2:
143             return 2;
144         case 5:
145             return 3;
146         case 15:
147             return 4;
148         case 30:
149             return 5;
150         case 60:
151             return 6;
152         case 120:
153             return 7;
154         default:
155             return 0;
156     }
157 }
158
159
160 void Settings::setLanguageIndex (int index)
161 {
162     if (index < 0 || index >= _langs.count ())
163         _langIndex = 0;
164     else
165         _langIndex = index;
166
167     // load settings
168     if (_ts) {
169         QCoreApplication::removeTranslator (_ts);
170         _ts = NULL;
171     }
172
173     QString alias = _langs[_langIndex].alias ();
174     QString fileName = QString (APPLICATION_NAME) + "_";
175
176     _ts = new QTranslator;
177
178     if (alias.isEmpty ())
179         fileName += QLocale::system ().name ();
180     else
181         fileName += alias;
182
183     if (_ts->load (fileName, TRANSLATION_PATH)) {
184         QCoreApplication::installTranslator (_ts);
185         translationsUpdated ();
186     }
187     else {
188         delete _ts;
189         _ts = NULL;
190     }
191 }
192
193
194 QString Settings::regionName (const QString &id) const
195 {
196     return Settings::tr (_cities[id].toUtf8 ());
197 }