00001
00002 #include "langsettings.h"
00003
00004
00005 #include <QApplication>
00006 #include <QDir>
00007 #include <QFileInfo>
00008 #include <QApplication>
00009
00010
00011 #include "mythcorecontext.h"
00012 #include "mythstorage.h"
00013 #include "mythdirs.h"
00014 #include "mythlogging.h"
00015 #include "mythlocale.h"
00016 #include "mythtranslation.h"
00017
00018
00019 #include "mythuibuttonlist.h"
00020 #include "mythuibutton.h"
00021 #include "mythmainwindow.h"
00022
00023 LanguageSelection::LanguageSelection(MythScreenStack *parent, bool exitOnFinish)
00024 :MythScreenType(parent, "LanguageSelection"),
00025 m_exitOnFinish(exitOnFinish), m_loaded(false)
00026 {
00027 m_language = gCoreContext->GetSetting("Language");
00028 m_country = gCoreContext->GetSetting("Country");
00029 }
00030
00031 LanguageSelection::~LanguageSelection()
00032 {
00033 }
00034
00035 bool LanguageSelection::Create(void)
00036 {
00037 if (!LoadWindowFromXML("config-ui.xml", "languageselection", this))
00038 return false;
00039
00040 bool err = false;
00041 UIUtilE::Assign(this, m_languageList, "languages", &err);
00042 UIUtilE::Assign(this, m_countryList, "countries", &err);
00043 UIUtilE::Assign(this, m_saveButton, "save", &err);
00044 UIUtilE::Assign(this, m_cancelButton, "cancel", &err);
00045
00046 if (err)
00047 {
00048 LOG(VB_GENERAL, LOG_ALERT,
00049 "Cannot load screen 'languageselection'");
00050 return false;
00051 }
00052
00053 #if 0
00054 connect(m_countryList, SIGNAL(itemClicked(MythUIButtonListItem*)),
00055 SLOT(LocaleClicked(MythUIButtonListItem*)));
00056 connect(m_languageList, SIGNAL(itemClicked(MythUIButtonListItem*)),
00057 SLOT(LanguageClicked(MythUIButtonListItem*)));
00058 #endif
00059
00060 connect(m_saveButton, SIGNAL(Clicked()), SLOT(Save()));
00061 connect(m_cancelButton, SIGNAL(Clicked()), SLOT(Close()));
00062
00063 m_languageList->SetLCDTitles(tr("Preferred language"), "");
00064 m_countryList->SetLCDTitles(tr("Your location"), "");
00065
00066 BuildFocusList();
00067
00068 return true;
00069 }
00070
00071 void LanguageSelection::Load(void)
00072 {
00073 MythLocale *locale = new MythLocale();
00074
00075 QString langCode;
00076
00077 if (gCoreContext->GetLocale())
00078 {
00079
00080
00081
00082 *locale = *gCoreContext->GetLocale();
00083 }
00084 else
00085 {
00086
00087
00088
00089
00090
00091
00092
00093
00094 langCode = locale->GetLocaleSetting("Language");
00095 }
00096
00097 if (langCode.isEmpty())
00098 langCode = locale->GetLanguageCode();
00099 QString localeCode = locale->GetLocaleCode();
00100 QString countryCode = locale->GetCountryCode();
00101
00102 LOG(VB_GENERAL, LOG_INFO,
00103 QString("System Locale (%1), Country (%2), Language (%3)")
00104 .arg(localeCode).arg(countryCode).arg(langCode));
00105
00106 QMap<QString,QString> langMap = MythTranslation::getLanguages();
00107 QStringList langs = langMap.values();
00108 langs.sort();
00109 MythUIButtonListItem *item;
00110 bool foundLanguage = false;
00111 for (QStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
00112 {
00113 QString nativeLang = *it;
00114 QString code = langMap.key(nativeLang);
00115 QString language = GetISO639EnglishLanguageName(code);
00116 item = new MythUIButtonListItem(m_languageList, nativeLang);
00117 item->SetText(language, "language");
00118 item->SetText(nativeLang, "nativelanguage");
00119 item->SetData(code);
00120
00121
00122 if (code.toLower() == m_language.toLower() ||
00123 code == langCode || code == localeCode)
00124 {
00125 m_languageList->SetItemCurrent(item);
00126 foundLanguage = true;
00127 }
00128 }
00129
00130 if (!foundLanguage)
00131 m_languageList->SetValueByData("en_US");
00132
00133 ISO3166ToNameMap localesMap = GetISO3166EnglishCountryMap();
00134 QStringList locales = localesMap.values();
00135 locales.sort();
00136 for (QStringList::Iterator it = locales.begin(); it != locales.end();
00137 ++it)
00138 {
00139 QString country = *it;
00140 QString code = localesMap.key(country);
00141 QString nativeCountry = GetISO3166CountryName(code);
00142 item = new MythUIButtonListItem(m_countryList, country);
00143 item->SetData(code);
00144 item->SetText(country, "country");
00145 item->SetText(nativeCountry, "nativecountry");
00146 item->SetImage(QString("locale/%1.png").arg(code.toLower()));
00147
00148 if (code == m_country || code == countryCode)
00149 m_countryList->SetItemCurrent(item);
00150 }
00151
00152 delete locale;
00153 }
00154
00155 bool LanguageSelection::m_languageChanged = false;
00156
00157 bool LanguageSelection::prompt(bool force)
00158 {
00159 m_languageChanged = false;
00160 QString language = gCoreContext->GetSetting("Language", "");
00161 QString country = gCoreContext->GetSetting("Country", "");
00162
00163 if (force || language.isEmpty() || country.isEmpty())
00164 {
00165 MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
00166 if (!mainStack)
00167 return false;
00168
00169 LanguageSelection *langSettings = new LanguageSelection(mainStack,
00170 true);
00171
00172 if (langSettings->Create())
00173 {
00174 mainStack->AddScreen(langSettings, false);
00175 qApp->exec();
00176 mainStack->PopScreen(langSettings, false);
00177 }
00178 else
00179 delete langSettings;
00180 }
00181
00182 return m_languageChanged;
00183 }
00184
00185 void LanguageSelection::Save(void)
00186 {
00187 MythUIButtonListItem *item = m_languageList->GetItemCurrent();
00188
00189 QString langCode = item->GetData().toString();
00190 gCoreContext->SaveSetting("Language", langCode);
00191
00192 item = m_countryList->GetItemCurrent();
00193
00194 QString countryCode = item->GetData().toString();
00195 gCoreContext->SaveSetting("Country", countryCode);
00196
00197 if (m_language != langCode)
00198 m_languageChanged = true;
00199
00200 Close();
00201 }
00202
00203 void LanguageSelection::Close(void)
00204 {
00205 if (m_exitOnFinish)
00206 qApp->quit();
00207 else
00208 MythScreenType::Close();
00209 }