Make settings a singleton.
[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     _langIndex = settings.value ("langIndex", _langIndex).toInt ();
41     if (_langIndex < 0 || _langIndex >= _langs.count ())
42         _langIndex = 0;
43 }
44
45
46 void Settings::save ()
47 {
48     QSettings settings;
49
50     settings.setValue ("region", _regionID);
51
52     settings.setValue ("checks/light", _checks[C_ShowLight]);
53     settings.setValue ("checks/rank", _checks[C_ShowRank]);
54     settings.setValue ("checks/time", _checks[C_ShowTime]);
55     settings.setValue ("checks/hint", _checks[C_ShowHint]);
56     settings.setValue ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]);
57     settings.setValue ("checks/updateOnGSM", _checks[C_UpdateOnGSM]);
58     settings.setValue ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]);
59
60     settings.setValue ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex));
61
62     settings.setValue ("langIndex", _langIndex);
63 }
64
65
66 void Settings::makeDefault ()
67 {
68     _regionID = "1";            // Default city
69
70     _cities["1"] = tr ("Moscow");
71     _cities["10174"] = tr ("St.Petersburg");
72     _cities["20544"] = tr ("Kiev");
73     _cities["11162"] = tr ("Ekaterinburg");
74
75     setCheck (C_ShowLight, true);
76     setCheck (C_ShowRank, true);
77     setCheck (C_ShowHint, true);
78
79     setCheck (C_UpdateOnWiFi, true);
80
81     setCheck (C_UpdateWhenLocked, true);
82
83     _updateIntervalIndex = 3;
84
85     // languages
86     _langIndex = 0;
87     _langs.append (Language (QString (""),   tr ("System")));
88     _langs.append (Language (QString ("en"), tr ("English")));
89     _langs.append (Language (QString ("ru"), tr ("Russian")));
90 }
91
92
93 QStringList Settings::updateIntervals () const
94 {
95     QStringList res;
96
97     res.append (tr ("Never"));
98     res.append (tr ("1 min"));
99     res.append (tr ("2 min"));
100     res.append (tr ("5 min"));
101     res.append (tr ("15 min"));
102     res.append (tr ("30 min"));
103
104     return res;
105 }
106
107
108 int Settings::intervalIndex2Minutes (int index) const
109 {
110     int int2min[] = { -1, 1, 2, 5, 15, 30 };
111
112     if (index < 0 || sizeof (int2min) / sizeof (int2min[0]) <= (unsigned int)index)
113         return -1;
114
115     return int2min[index];
116 }
117
118
119 int Settings::minutes2IntervalIndex (int minutes) const
120 {
121     switch (minutes) {
122         case -1:
123             return 0;
124         case 1:
125             return 1;
126         case 2:
127             return 2;
128         case 5:
129             return 3;
130         case 15:
131             return 4;
132         case 30:
133             return 5;
134         default:
135             return 0;
136     }
137 }