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