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