MythTV
master
libs
libmythbase
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
53
void
MythLocale::ReInit
()
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
{
67
return
GetISO3166EnglishCountryName
(
GetCountryCode
());
68
}
69
70
QString
MythLocale::GetNativeCountry
(
void
)
const
71
{
72
return
GetISO3166CountryName
(
GetCountryCode
());
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
{
84
return
GetISO639EnglishLanguageName
(
GetLanguageCode
());
85
}
86
87
QString
MythLocale::GetNativeLanguage
(
void
)
const
88
{
89
return
GetISO639LanguageName
(
GetLanguageCode
());
90
}
91
92
bool
MythLocale::LoadDefaultsFromXML
(
void
)
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
&&
175
!
LoadDefaultsFromXML
())
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
194
void
MythLocale::ResetToLocaleDefaults
(
void
)
195
{
196
SaveLocaleDefaults
(
true
);
197
}
198
199
void
MythLocale::ResetToStandardDefaults
(
void
)
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
&&
209
!
LoadDefaultsFromXML
())
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
}
MythLocale::ReInit
void ReInit()
Definition:
mythlocale.cpp:53
e
QDomElement e
Definition:
mythplugins/mytharchive/mytharchivehelper/main.cpp:1420
mythdb.h
GetISO639EnglishLanguageName
QString GetISO639EnglishLanguageName(const QString &iso639_1)
Definition:
iso639.cpp:962
doc
QDomDocument doc("MYTHARCHIVEITEM")
MythLocale::GetLanguage
QString GetLanguage() const
ISO639 2-letter.
Definition:
mythlocale.cpp:82
MythLocale::MythLocale
MythLocale(const QString &localeName=QString())
Definition:
mythlocale.cpp:16
arg
arg(title).arg(filename).arg(doDelete))
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition:
mythlogging.h:23
MythLocale::GetNativeLanguage
QString GetNativeLanguage() const
Name of language in English.
Definition:
mythlocale.cpp:87
GetMythDB
MythDB * GetMythDB(void)
Definition:
mythdb.cpp:45
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:70
MythLocale::m_qtLocale
QLocale m_qtLocale
Definition:
mythlocale.h:44
mythlogging.h
MythLocale::GetCountryCode
QString GetCountryCode() const
Definition:
mythlocale.cpp:58
GetConfDir
QString GetConfDir(void)
Definition:
mythdirs.cpp:224
GetShareDir
QString GetShareDir(void)
Definition:
mythdirs.cpp:222
MythLocale::LoadDefaultsFromXML
bool LoadDefaultsFromXML(void)
Definition:
mythlocale.cpp:92
MythLocale::Init
void Init(const QString &localeName=QString())
Definition:
mythlocale.cpp:21
MythLocale::GetLocaleSetting
QString GetLocaleSetting(const QString &key)
Definition:
mythlocale.cpp:206
MythLocale::GetLanguageCode
QString GetLanguageCode() const
Name of country in the native language.
Definition:
mythlocale.cpp:75
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:24
MythLocale::SaveLocaleDefaults
void SaveLocaleDefaults(bool overwrite=false)
Definition:
mythlocale.cpp:172
iso3166.h
ISO 3166-1 support functions.
MythLocale::ResetToStandardDefaults
void ResetToStandardDefaults(void)
Definition:
mythlocale.cpp:199
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:65
MythLocale::m_localeCode
QString m_localeCode
Definition:
mythlocale.h:42
MythLocale::ResetToLocaleDefaults
void ResetToLocaleDefaults(void)
Definition:
mythlocale.cpp:194
Generated on Tue Mar 2 2021 03:18:14 for MythTV by
1.8.17