Language settings continued.
[yandex-traffic] / settings.cpp
1 #include <QtCore>
2 #include <settings.hpp>
3
4
5 static Settings* _settings;
6
7
8 Settings* Settings::instance ()
9 {
10     if (!_settings)
11         _settings = new Settings;
12     return _settings;
13 }
14
15
16 Settings::Settings ()
17 {
18     load ();
19 }
20
21
22 void Settings::load ()
23 {
24     QSettings settings;
25
26     makeDefault ();
27
28     _regionID = settings.value ("region", _regionID).toString ();
29
30     _checks[C_ShowLight] = settings.value ("checks/light", _checks[C_ShowLight]).toBool ();
31     _checks[C_ShowRank] = settings.value ("checks/rank", _checks[C_ShowRank]).toBool ();
32     _checks[C_ShowTime] = settings.value ("checks/time", _checks[C_ShowTime]).toBool ();
33     _checks[C_ShowHint] = settings.value ("checks/hint", _checks[C_ShowHint]).toBool ();
34     _checks[C_UpdateOnWiFi] = settings.value ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]).toBool ();
35     _checks[C_UpdateOnGSM] = settings.value ("checks/updateOnGSM", _checks[C_UpdateOnGSM]).toBool ();
36     _checks[C_UpdateWhenLocked] = settings.value ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]).toBool ();
37
38     _updateIntervalIndex = minutes2IntervalIndex (settings.value ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex)).toInt ());
39
40     setLanguageIndex (settings.value ("langIndex", _langIndex).toInt ());
41 }
42
43
44 void Settings::save ()
45 {
46     QSettings settings;
47
48     settings.setValue ("region", _regionID);
49
50     settings.setValue ("checks/light", _checks[C_ShowLight]);
51     settings.setValue ("checks/rank", _checks[C_ShowRank]);
52     settings.setValue ("checks/time", _checks[C_ShowTime]);
53     settings.setValue ("checks/hint", _checks[C_ShowHint]);
54     settings.setValue ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]);
55     settings.setValue ("checks/updateOnGSM", _checks[C_UpdateOnGSM]);
56     settings.setValue ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]);
57
58     settings.setValue ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex));
59
60     settings.setValue ("langIndex", _langIndex);
61 }
62
63
64 void Settings::makeDefault ()
65 {
66     _regionID = "1";            // Default city
67
68     _cities["1"] = tr ("Moscow");
69     _cities["10174"] = tr ("St.Petersburg");
70     _cities["20544"] = tr ("Kiev");
71     _cities["11162"] = tr ("Ekaterinburg");
72
73     setCheck (C_ShowLight, true);
74     setCheck (C_ShowRank, true);
75     setCheck (C_ShowHint, true);
76
77     setCheck (C_UpdateOnWiFi, true);
78
79     setCheck (C_UpdateWhenLocked, true);
80
81     _updateIntervalIndex = 3;
82
83     // languages
84     _langs.append (Language (QString (""),   tr ("System")));
85     _langs.append (Language (QString ("en"), tr ("English")));
86     _langs.append (Language (QString ("ru"), tr ("Russian")));
87     setLanguageIndex (0);
88 }
89
90
91 QStringList Settings::updateIntervals () const
92 {
93     QStringList res;
94
95     res.append (tr ("Never"));
96     res.append (tr ("1 min"));
97     res.append (tr ("2 min"));
98     res.append (tr ("5 min"));
99     res.append (tr ("15 min"));
100     res.append (tr ("30 min"));
101
102     return res;
103 }
104
105
106 int Settings::intervalIndex2Minutes (int index) const
107 {
108     int int2min[] = { -1, 1, 2, 5, 15, 30 };
109
110     if (index < 0 || sizeof (int2min) / sizeof (int2min[0]) <= (unsigned int)index)
111         return -1;
112
113     return int2min[index];
114 }
115
116
117 int Settings::minutes2IntervalIndex (int minutes) const
118 {
119     switch (minutes) {
120         case -1:
121             return 0;
122         case 1:
123             return 1;
124         case 2:
125             return 2;
126         case 5:
127             return 3;
128         case 15:
129             return 4;
130         case 30:
131             return 5;
132         default:
133             return 0;
134     }
135 }
136
137
138 void Settings::setLanguageIndex (int index)
139 {
140     if (index < 0 || index >= _langs.count ())
141         _langIndex = 0;
142     else
143         _langIndex = index;
144 }