X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=settings.cpp;h=4a7cac3de00ce5261598f8543bf33e46254e2286;hb=HEAD;hp=36464aa5efab23ac40e1bb3e3553d5517ddf76be;hpb=27a079e27813fae5d9577c4af63abdf54176d6cd;p=yandex-traffic diff --git a/settings.cpp b/settings.cpp index 36464aa..4a7cac3 100644 --- a/settings.cpp +++ b/settings.cpp @@ -1,8 +1,34 @@ #include #include +#include "globals.hpp" + + +// -------------------------------------------------- +// Language +// -------------------------------------------------- +QString Language::title () const +{ + return Settings::tr (_title.toUtf8 ()); +} + + +// -------------------------------------------------- +// Settings +// -------------------------------------------------- +static Settings* _settings; + + +Settings* Settings::instance () +{ + if (!_settings) + _settings = new Settings; + return _settings; +} + Settings::Settings () + : _ts (NULL) { load (); } @@ -22,10 +48,11 @@ void Settings::load () _checks[C_ShowHint] = settings.value ("checks/hint", _checks[C_ShowHint]).toBool (); _checks[C_UpdateOnWiFi] = settings.value ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]).toBool (); _checks[C_UpdateOnGSM] = settings.value ("checks/updateOnGSM", _checks[C_UpdateOnGSM]).toBool (); - - loadCities (&settings); + _checks[C_UpdateWhenLocked] = settings.value ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]).toBool (); _updateIntervalIndex = minutes2IntervalIndex (settings.value ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex)).toInt ()); + + setLanguageIndex (settings.value ("langIndex", _langIndex).toInt ()); } @@ -41,57 +68,23 @@ void Settings::save () settings.setValue ("checks/hint", _checks[C_ShowHint]); settings.setValue ("checks/updateOnWifi", _checks[C_UpdateOnWiFi]); settings.setValue ("checks/updateOnGSM", _checks[C_UpdateOnGSM]); + settings.setValue ("checks/updateWhenLocked", _checks[C_UpdateWhenLocked]); settings.setValue ("updateInterval", intervalIndex2Minutes (_updateIntervalIndex)); - saveCities (&settings); -} - - -void Settings::loadCities (QSettings *settings) -{ - QMap v; - QMap::const_iterator it; - - v = settings->value ("cities", v).toMap (); - - if (v.size () == 0) - return; - - it = v.begin (); - _cities.clear (); - - while (it != v.end ()) { - _cities[it.key ()] = it.value ().toString (); - it++; - } -} - - -void Settings::saveCities (QSettings *settings) -{ - QMap v; - QMap::const_iterator it; - - it = _cities.begin (); - - while (it != _cities.end ()) { - v[it.key ()] = it.value (); - it++; - } - - settings->setValue ("cities", v); + settings.setValue ("langIndex", _langIndex); } +// This routine called before any translations loaded, so strings must be translated explicitly. void Settings::makeDefault () { _regionID = "1"; // Default city _cities["1"] = tr ("Moscow"); - _cities["10174"] = tr ("Kiev"); + _cities["10174"] = tr ("St.Petersburg"); + _cities["20544"] = tr ("Kiev"); _cities["11162"] = tr ("Ekaterinburg"); - _cities["11079"] = tr ("N.Novgorod"); setCheck (C_ShowLight, true); setCheck (C_ShowRank, true); @@ -99,7 +92,15 @@ void Settings::makeDefault () setCheck (C_UpdateOnWiFi, true); + setCheck (C_UpdateWhenLocked, true); + _updateIntervalIndex = 3; + + // languages + _langs.append (Language (QString (""), tr ("System"))); + _langs.append (Language (QString ("en"), tr ("English"))); + _langs.append (Language (QString ("ru"), tr ("Russian"))); + setLanguageIndex (0); } @@ -108,11 +109,13 @@ QStringList Settings::updateIntervals () const QStringList res; res.append (tr ("Never")); - res.append (tr ("1 min")); - res.append (tr ("2 min")); - res.append (tr ("5 min")); - res.append (tr ("15 min")); - res.append (tr ("30 min")); + res.append (tr ("1 minute")); + res.append (tr ("2 minutes")); + res.append (tr ("5 minutes")); + res.append (tr ("15 minutes")); + res.append (tr ("30 minutes")); + res.append (tr ("1 hour")); + res.append (tr ("2 hours")); return res; } @@ -120,9 +123,9 @@ QStringList Settings::updateIntervals () const int Settings::intervalIndex2Minutes (int index) const { - int int2min[] = { -1, 1, 2, 5, 15, 30 }; + int int2min[] = { -1, 1, 2, 5, 15, 30, 60, 120 }; - if (index < 0 || sizeof (int2min) / sizeof (int2min[0]) <= index) + if (index < 0 || sizeof (int2min) / sizeof (int2min[0]) <= (unsigned int)index) return -1; return int2min[index]; @@ -144,7 +147,51 @@ int Settings::minutes2IntervalIndex (int minutes) const return 4; case 30: return 5; + case 60: + return 6; + case 120: + return 7; default: return 0; } } + + +void Settings::setLanguageIndex (int index) +{ + if (index < 0 || index >= _langs.count ()) + _langIndex = 0; + else + _langIndex = index; + + // load settings + if (_ts) { + QCoreApplication::removeTranslator (_ts); + _ts = NULL; + } + + QString alias = _langs[_langIndex].alias (); + QString fileName = QString (APPLICATION_NAME) + "_"; + + _ts = new QTranslator; + + if (alias.isEmpty ()) + fileName += QLocale::system ().name (); + else + fileName += alias; + + if (_ts->load (fileName, TRANSLATION_PATH)) { + QCoreApplication::installTranslator (_ts); + translationsUpdated (); + } + else { + delete _ts; + _ts = NULL; + } +} + + +QString Settings::regionName (const QString &id) const +{ + return Settings::tr (_cities[id].toUtf8 ()); +}