MythTV  0.26-pre
mythlocale.cpp
Go to the documentation of this file.
00001 
00002 #include "mythlocale.h"
00003 
00004 // QT
00005 #include <QDomDocument>
00006 #include <QFile>
00007 #include <QIODevice>
00008 
00009 // libmythbase
00010 #include "mythlogging.h"
00011 #include "mythdb.h"
00012 #include "mythdirs.h"
00013 
00014 MythLocale::MythLocale(QString localeName) :
00015     m_defaultsLoaded(false)
00016 {
00017     Init(localeName);
00018 }
00019 
00020 void MythLocale::Init(const QString &localeName)
00021 {
00022     QString dbLanguage = GetMythDB()->GetSetting("Language", "");
00023     QString dbCountry = GetMythDB()->GetSetting("Country", "");
00024 
00025     if (!localeName.isEmpty())
00026     {
00027         m_localeCode = localeName;
00028     }
00029     else if (!dbLanguage.isEmpty() &&
00030              !dbCountry.isEmpty())
00031     {
00032         QString langcode = dbLanguage.section('_',0,0);
00033         m_localeCode = QString("%1_%2").arg(langcode)
00034                                        .arg(dbCountry.toUpper());
00035     }
00036     else
00037     {
00038         QLocale locale = QLocale::system();
00039 
00040         if (locale.name().isEmpty() || locale.name() == "C")
00041         {
00042             // If all else has failed use the US locale
00043             m_localeCode = "en_US";
00044         }
00045         else
00046             m_localeCode = locale.name();
00047     }
00048 
00049     m_qtLocale = QLocale(m_localeCode);
00050 }
00051 
00052 void MythLocale::ReInit()
00053 {
00054     Init();
00055 }
00056 
00057 QString MythLocale::GetCountryCode(void) const
00058 {
00059     QString isoCountry = m_localeCode.section('_', 1, 1);
00060 
00061     return isoCountry;
00062 }
00063 
00064 QString MythLocale::GetCountry() const
00065 {
00066     return GetISO3166EnglishCountryName(GetCountryCode());
00067 }
00068 
00069 QString MythLocale::GetNativeCountry(void) const
00070 {
00071     return GetISO3166CountryName(GetCountryCode());
00072 }
00073 
00074 QString MythLocale::GetLanguageCode(void) const
00075 {
00076     QString isoLanguage = m_localeCode.section('_', 0, 0);
00077 
00078     return isoLanguage;
00079 }
00080 
00081 QString MythLocale::GetLanguage() const
00082 {
00083     return GetISO639EnglishLanguageName(GetLanguageCode());
00084 }
00085 
00086 QString MythLocale::GetNativeLanguage(void) const
00087 {
00088     return GetISO639LanguageName(GetLanguageCode());
00089 }
00090 
00091 bool MythLocale::LoadDefaultsFromXML(void)
00092 {
00093     m_defaultsLoaded = true;
00094     m_globalSettings.clear();
00095     QDomDocument doc;
00096 
00097     QString path = QString("/locales/%1.xml").arg(m_localeCode.toLower());
00098 
00099     QFile file(path.prepend(GetShareDir()));
00100     if (!file.exists())
00101     {
00102         file.setFileName(path.prepend(GetConfDir()));
00103 
00104         if (!file.exists())
00105         {
00106             LOG(VB_GENERAL, LOG_ERR,
00107                 QString("No locale defaults file for %1, skipping")
00108                     .arg(m_localeCode));
00109             return false;
00110         }
00111     }
00112 
00113     if (!file.open(QIODevice::ReadOnly))
00114     {
00115         LOG(VB_GENERAL, LOG_ERR, QString("Unable to open %1")
00116                                                         .arg(file.fileName()));
00117         return false;
00118     }
00119 
00120     LOG(VB_GENERAL, LOG_NOTICE, QString("Reading locale defaults from %1")
00121                                                         .arg(file.fileName()));
00122 
00123     if (!doc.setContent(&file))
00124     {
00125         LOG(VB_GENERAL, LOG_ERR, QString("Unable to parse %1")
00126                                                         .arg(file.fileName()));
00127 
00128         file.close();
00129         return false;
00130     }
00131     file.close();
00132 
00133     QDomElement docElem = doc.documentElement();
00134 
00135     for (QDomNode n = docElem.firstChild(); !n.isNull();
00136             n = n.nextSibling())
00137     {
00138         QDomElement e = n.toElement();
00139         if (!e.isNull())
00140         {
00141             if (e.tagName() == "setting")
00142             {
00143                 QString name = e.attribute("name", "");
00144                 bool global = (e.attribute("global", "false") == "true");
00145                 QString value = e.firstChild().toText().data();
00146 
00147                 // TODO Assumes no setting accepts an empty value, which may not
00148                 // be the case
00149                 if (!name.isEmpty() && !value.isEmpty())
00150                 {
00151                     if (global)
00152                         m_globalSettings[name] = value;
00153                     else
00154                         m_hostSettings[name] = value;
00155                 }
00156             }
00157         }
00158     }
00159 
00160     if (m_globalSettings.isEmpty() && m_hostSettings.isEmpty())
00161     {
00162         LOG(VB_GENERAL, LOG_ERR,
00163             QString("No locale defaults specified in %1, skipping")
00164                 .arg(file.fileName()));
00165         return false;
00166     }
00167 
00168     return true;
00169 }
00170 
00171 void MythLocale::SaveLocaleDefaults(bool overwrite)
00172 {
00173     if (!m_defaultsLoaded &&
00174         !LoadDefaultsFromXML())
00175         return;
00176 
00177     SettingsMap::iterator it;
00178     for (it = m_globalSettings.begin(); it != m_globalSettings.end(); ++it)
00179     {
00180         MythDB *mythDB = MythDB::getMythDB();
00181         if (overwrite || mythDB->GetSetting(it.key()).isEmpty())
00182             mythDB->SaveSettingOnHost(it.key(), it.value(), "");
00183     }
00184 
00185     for (it = m_hostSettings.begin(); it != m_hostSettings.end(); ++it)
00186     {
00187         MythDB *mythDB = MythDB::getMythDB();
00188         if (overwrite || mythDB->GetSetting(it.key()).isEmpty())
00189             mythDB->SaveSetting(it.key(), it.value());
00190     }
00191 }
00192 
00193 void MythLocale::ResetToLocaleDefaults(void)
00194 {
00195     SaveLocaleDefaults(true);
00196 }
00197 
00198 void MythLocale::ResetToStandardDefaults(void)
00199 {
00200     // TODO Not implemented yet, delete everything in m_globalSettings
00201     //      from the database then let the standard defaults populate them
00202     //      again. Used if the user wants to revert the changes
00203     return;
00204 }
00205 
00206 QString MythLocale::GetLocaleSetting(const QString &key)
00207 {
00208     if (!m_defaultsLoaded &&
00209         !LoadDefaultsFromXML())
00210         return QString();
00211 
00212     QString value = m_globalSettings.value(key);
00213     if (m_hostSettings.contains(key))
00214         value = m_hostSettings.value(key);
00215 
00216     return value;
00217 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends