MythTV  master
mythlocale.cpp
Go to the documentation of this file.
1 
2 #include "mythlocale.h"
3 
4 // QT
5 #include <QDomDocument>
6 #include <QFile>
7 #include <QIODevice>
8 
9 // libmythbase
10 #include "mythlogging.h"
11 #include "mythdb.h"
12 #include "mythdirs.h"
13 #include "iso3166.h"
14 #include "iso639.h"
15 
16 MythLocale::MythLocale(const QString &localeName)
17 {
18  Init(localeName);
19 }
20 
21 void MythLocale::Init(const QString &localeName)
22 {
23  QString dbLanguage = GetMythDB()->GetSetting("Language", "");
24  QString dbCountry = GetMythDB()->GetSetting("Country", "");
25 
26  if (!localeName.isEmpty())
27  {
28  m_localeCode = localeName;
29  }
30  else if (!dbLanguage.isEmpty() &&
31  !dbCountry.isEmpty())
32  {
33  QString langcode = dbLanguage.section('_',0,0);
34  m_localeCode = QString("%1_%2").arg(langcode)
35  .arg(dbCountry.toUpper());
36  }
37  else
38  {
39  QLocale locale = QLocale::system();
40 
41  if (locale.name().isEmpty() || locale.name() == "C")
42  {
43  // If all else has failed use the US locale
44  m_localeCode = "en_US";
45  }
46  else
47  m_localeCode = locale.name();
48  }
49 
50  m_qtLocale = QLocale(m_localeCode);
51 }
52 
54 {
55  Init();
56 }
57 
58 QString MythLocale::GetCountryCode(void) const
59 {
60  QString isoCountry = m_localeCode.section('_', 1, 1);
61 
62  return isoCountry;
63 }
64 
65 QString MythLocale::GetCountry() const
66 {
68 }
69 
70 QString MythLocale::GetNativeCountry(void) const
71 {
73 }
74 
75 QString MythLocale::GetLanguageCode(void) const
76 {
77  QString isoLanguage = m_localeCode.section('_', 0, 0);
78 
79  return isoLanguage;
80 }
81 
82 QString MythLocale::GetLanguage() const
83 {
85 }
86 
87 QString MythLocale::GetNativeLanguage(void) const
88 {
90 }
91 
93 {
94  m_defaultsLoaded = true;
95  m_globalSettings.clear();
96  QDomDocument doc;
97 
98  QString path = QString("/locales/%1.xml").arg(m_localeCode.toLower());
99 
100  QFile file(path.prepend(GetShareDir()));
101  if (!file.exists())
102  {
103  file.setFileName(path.prepend(GetConfDir()));
104 
105  if (!file.exists())
106  {
107  LOG(VB_GENERAL, LOG_ERR,
108  QString("No locale defaults file for %1, skipping")
109  .arg(m_localeCode));
110  return false;
111  }
112  }
113 
114  if (!file.open(QIODevice::ReadOnly))
115  {
116  LOG(VB_GENERAL, LOG_ERR, QString("Unable to open %1")
117  .arg(file.fileName()));
118  return false;
119  }
120 
121  LOG(VB_GENERAL, LOG_NOTICE, QString("Reading locale defaults from %1")
122  .arg(file.fileName()));
123 
124  if (!doc.setContent(&file))
125  {
126  LOG(VB_GENERAL, LOG_ERR, QString("Unable to parse %1")
127  .arg(file.fileName()));
128 
129  file.close();
130  return false;
131  }
132  file.close();
133 
134  QDomElement docElem = doc.documentElement();
135 
136  for (QDomNode n = docElem.firstChild(); !n.isNull();
137  n = n.nextSibling())
138  {
139  QDomElement e = n.toElement();
140  if (!e.isNull())
141  {
142  if (e.tagName() == "setting")
143  {
144  QString name = e.attribute("name", "");
145  bool global = (e.attribute("global", "false") == "true");
146  QString value = e.firstChild().toText().data();
147 
148  // TODO Assumes no setting accepts an empty value, which may not
149  // be the case
150  if (!name.isEmpty() && !value.isEmpty())
151  {
152  if (global)
153  m_globalSettings[name] = value;
154  else
155  m_hostSettings[name] = value;
156  }
157  }
158  }
159  }
160 
161  if (m_globalSettings.isEmpty() && m_hostSettings.isEmpty())
162  {
163  LOG(VB_GENERAL, LOG_ERR,
164  QString("No locale defaults specified in %1, skipping")
165  .arg(file.fileName()));
166  return false;
167  }
168 
169  return true;
170 }
171 
172 void MythLocale::SaveLocaleDefaults(bool overwrite)
173 {
174  if (!m_defaultsLoaded &&
176  return;
177 
178  SettingsMap::iterator it;
179  for (it = m_globalSettings.begin(); it != m_globalSettings.end(); ++it)
180  {
181  MythDB *mythDB = MythDB::getMythDB();
182  if (overwrite || mythDB->GetSetting(it.key()).isEmpty())
183  mythDB->SaveSettingOnHost(it.key(), it.value(), "");
184  }
185 
186  for (it = m_hostSettings.begin(); it != m_hostSettings.end(); ++it)
187  {
188  MythDB *mythDB = MythDB::getMythDB();
189  if (overwrite || mythDB->GetSetting(it.key()).isEmpty())
190  mythDB->SaveSetting(it.key(), it.value());
191  }
192 }
193 
195 {
196  SaveLocaleDefaults(true);
197 }
198 
200 {
201  // TODO Not implemented yet, delete everything in m_globalSettings
202  // from the database then let the standard defaults populate them
203  // again. Used if the user wants to revert the changes
204 }
205 
206 QString MythLocale::GetLocaleSetting(const QString &key)
207 {
208  if (!m_defaultsLoaded &&
210  return QString();
211 
212  QString value = m_globalSettings.value(key);
213  if (m_hostSettings.contains(key))
214  value = m_hostSettings.value(key);
215 
216  return value;
217 }
void ResetToLocaleDefaults(void)
Definition: mythlocale.cpp:194
ISO 639-1 and ISO 639-2 support functions.
Definition: mythdb.h:14
QString GetLanguage() const
ISO639 2-letter.
Definition: mythlocale.cpp:82
ISO 3166-1 support functions.
void ReInit()
Definition: mythlocale.cpp:53
QLocale m_qtLocale
Definition: mythlocale.h:44
MythLocale(const QString &localeName=QString())
Definition: mythlocale.cpp:16
static MythDB * getMythDB()
Definition: mythdb.cpp:25
QString GetCountryCode() const
Definition: mythlocale.cpp:58
void Init(const QString &localeName=QString())
Definition: mythlocale.cpp:21
QString GetISO3166CountryName(const QString &iso3166Code)
Definition: iso3166.cpp:368
QString GetConfDir(void)
Definition: mythdirs.cpp:224
QString GetLocaleSetting(const QString &key)
Definition: mythlocale.cpp:206
SettingsMap m_globalSettings
Definition: mythlocale.h:47
QString GetLanguageCode() const
Name of country in the native language.
Definition: mythlocale.cpp:75
QString GetNativeLanguage() const
Name of language in English.
Definition: mythlocale.cpp:87
QString GetNativeCountry() const
Name of country in English.
Definition: mythlocale.cpp:70
bool m_defaultsLoaded
Definition: mythlocale.h:43
QString GetShareDir(void)
Definition: mythdirs.cpp:222
bool LoadDefaultsFromXML(void)
Definition: mythlocale.cpp:92
const char * name
Definition: ParseText.cpp:328
QString GetISO3166EnglishCountryName(const QString &iso3166Code)
Definition: iso3166.cpp:352
QString GetSetting(const QString &_key, const QString &defaultval)
Definition: mythdb.cpp:362
void ResetToStandardDefaults(void)
Definition: mythlocale.cpp:199
SettingsMap m_hostSettings
Definition: mythlocale.h:48
QString m_localeCode
Definition: mythlocale.h:42
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QString GetCountry() const
ISO3166 2-letter.
Definition: mythlocale.cpp:65
void SaveLocaleDefaults(bool overwrite=false)
Definition: mythlocale.cpp:172
void SaveSetting(const QString &key, int newValue)
Definition: mythdb.cpp:243
QString GetISO639EnglishLanguageName(const QString &iso639_1)
Definition: iso639.cpp:967
MythDB * GetMythDB(void)
Definition: mythdb.cpp:46
bool SaveSettingOnHost(const QString &key, const QString &newValue, const QString &host)
Definition: mythdb.cpp:254
QString GetISO639LanguageName(const QString &iso639_1)
Definition: iso639.cpp:959