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