Fix keymap.
[vuplus_xbmc] / xbmc / LangInfo.cpp
index b098019..3f2e04f 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "LangInfo.h"
 #include "Application.h"
+#include "ApplicationMessenger.h"
 #include "FileItem.h"
 #include "Util.h"
 #include "filesystem/Directory.h"
@@ -27,7 +28,7 @@
 #include "guilib/LocalizeStrings.h"
 #include "pvr/PVRManager.h"
 #include "settings/AdvancedSettings.h"
-#include "settings/Setting.h"
+#include "settings/lib/Setting.h"
 #include "settings/Settings.h"
 #include "utils/CharsetConverter.h"
 #include "utils/log.h"
@@ -53,6 +54,7 @@ CLangInfo::CRegion::CRegion(const CRegion& region)
   m_strDVDAudioLanguage=region.m_strDVDAudioLanguage;
   m_strDVDSubtitleLanguage=region.m_strDVDSubtitleLanguage;
   m_strLangLocaleName = region.m_strLangLocaleName;
+  m_strLangLocaleCodeTwoChar = region.m_strLangLocaleCodeTwoChar;
   m_strRegionLocaleName = region.m_strRegionLocaleName;
 
   m_strDateFormatShort=region.m_strDateFormatShort;
@@ -86,6 +88,7 @@ void CLangInfo::CRegion::SetDefaults()
   m_strDVDAudioLanguage="en";
   m_strDVDSubtitleLanguage="en";
   m_strLangLocaleName = "English";
+  m_strLangLocaleCodeTwoChar = "en";
 
   m_strDateFormatShort="DD/MM/YYYY";
   m_strDateFormatLong="DDDD, D MMMM YYYY";
@@ -194,6 +197,7 @@ void CLangInfo::CRegion::SetGlobalLocale()
 
   locale::global(current_locale);
 #endif
+  g_charsetConverter.resetSystemCharset();
   CLog::Log(LOGINFO, "global locale set to %s", strLocale.c_str());
 }
 
@@ -228,22 +232,21 @@ void CLangInfo::OnSettingChanged(const CSetting *setting)
   }
 }
 
-bool CLangInfo::Load(const CStdString& strFileName)
+bool CLangInfo::Load(const std::string& strFileName, bool onlyCheckLanguage /*= false*/)
 {
   SetDefaults();
 
   CXBMCTinyXML xmlDoc;
   if (!xmlDoc.LoadFile(strFileName))
   {
-    CLog::Log(LOGERROR, "unable to load %s: %s at line %d", strFileName.c_str(), xmlDoc.ErrorDesc(), xmlDoc.ErrorRow());
+    CLog::Log(onlyCheckLanguage ? LOGDEBUG : LOGERROR, "unable to load %s: %s at line %d", strFileName.c_str(), xmlDoc.ErrorDesc(), xmlDoc.ErrorRow());
     return false;
   }
 
   TiXmlElement* pRootElement = xmlDoc.RootElement();
-  CStdString strValue = pRootElement->Value();
-  if (strValue != CStdString("language"))
+  if (pRootElement->ValueStr() != "language")
   {
-    CLog::Log(LOGERROR, "%s Doesn't contain <language>", strFileName.c_str());
+    CLog::Log(onlyCheckLanguage ? LOGDEBUG : LOGERROR, "%s Doesn't contain <language>", strFileName.c_str());
     return false;
   }
 
@@ -263,13 +266,17 @@ bool CLangInfo::Load(const CStdString& strFileName)
 #else
   if (m_defaultRegion.m_strLangLocaleName.length() != 3)
   {
-    if (!g_LangCodeExpander.ConvertToThreeCharCode(m_languageCodeGeneral, m_defaultRegion.m_strLangLocaleName))
+    if (!g_LangCodeExpander.ConvertToThreeCharCode(m_languageCodeGeneral, m_defaultRegion.m_strLangLocaleName, !onlyCheckLanguage))
       m_languageCodeGeneral = "";
   }
   else
     m_languageCodeGeneral = m_defaultRegion.m_strLangLocaleName;
 #endif
 
+  CStdString tmp;
+  if (g_LangCodeExpander.ConvertToTwoCharCode(tmp, m_defaultRegion.m_strLangLocaleName))
+    m_defaultRegion.m_strLangLocaleCodeTwoChar = tmp;
+
   const TiXmlNode *pCharSets = pRootElement->FirstChild("charsets");
   if (pCharSets && !pCharSets->NoChildren())
   {
@@ -313,7 +320,7 @@ bool CLangInfo::Load(const CStdString& strFileName)
     {
       CRegion region(m_defaultRegion);
       region.m_strName=pRegion->Attribute("name");
-      if (region.m_strName.IsEmpty())
+      if (region.m_strName.empty())
         region.m_strName="N/A";
 
       if (pRegion->Attribute("locale"))
@@ -361,15 +368,26 @@ bool CLangInfo::Load(const CStdString& strFileName)
       pRegion=pRegion->NextSiblingElement("region");
     }
 
-    const CStdString& strName=CSettings::Get().GetString("locale.country");
-    SetCurrentRegion(strName);
+    if (!onlyCheckLanguage)
+    {
+      const CStdString& strName = CSettings::Get().GetString("locale.country");
+      SetCurrentRegion(strName);
+    }
   }
+  g_charsetConverter.reinitCharsetsFromSettings();
 
-  LoadTokens(pRootElement->FirstChild("sorttokens"),g_advancedSettings.m_vecTokens);
+  if (!onlyCheckLanguage)
+    LoadTokens(pRootElement->FirstChild("sorttokens"), g_advancedSettings.m_vecTokens);
 
   return true;
 }
 
+bool CLangInfo::CheckLanguage(const std::string& language)
+{
+  CLangInfo li;
+  return li.Load("special://xbmc/language/" + language + "/langinfo.xml", true);
+}
+
 void CLangInfo::LoadTokens(const TiXmlNode* pTokens, vector<CStdString>& vecTokens)
 {
   if (pTokens && !pTokens->NoChildren())
@@ -382,7 +400,7 @@ void CLangInfo::LoadTokens(const TiXmlNode* pTokens, vector<CStdString>& vecToke
         strSep = pToken->Attribute("separators");
       if (pToken->FirstChild() && pToken->FirstChild()->Value())
       {
-        if (strSep.IsEmpty())
+        if (strSep.empty())
           vecTokens.push_back(pToken->FirstChild()->Value());
         else
           for (unsigned int i=0;i<strSep.size();++i)
@@ -428,7 +446,7 @@ CStdString CLangInfo::GetSubtitleCharSet() const
 bool CLangInfo::SetLanguage(const std::string &strLanguage)
 {
   string strLangInfoPath = StringUtils::Format("special://xbmc/language/%s/langinfo.xml", strLanguage.c_str());
-  if (!g_langInfo.Load(strLangInfoPath))
+  if (!Load(strLangInfoPath))
     return false;
 
   if (ForceUnicodeFont() && !g_fontManager.IsFontSetUnicode())
@@ -439,19 +457,22 @@ bool CLangInfo::SetLanguage(const std::string &strLanguage)
       CLog::Log(LOGERROR, "No ttf font found but needed: %s", strFontSet.c_str());
   }
 
-  g_charsetConverter.reset();
-
   if (!g_localizeStrings.Load("special://xbmc/language/", strLanguage))
     return false;
 
   // also tell our weather and skin to reload as these are localized
   g_weatherManager.Refresh();
   g_PVRManager.LocalizationChanged();
-  g_application.ReloadSkin();
+  CApplicationMessenger::Get().ExecBuiltIn("ReloadSkin", false);
 
   return true;
 }
 
+bool CLangInfo::CheckLoadLanguage(const std::string &language)
+{
+  return Load("special://xbmc/language/" + language + "/langinfo.xml", true);
+}
+
 // three char language code (not win32 specific)
 const CStdString& CLangInfo::GetAudioLanguage() const
 {
@@ -461,9 +482,12 @@ const CStdString& CLangInfo::GetAudioLanguage() const
   return m_languageCodeGeneral;
 }
 
-void CLangInfo::SetAudioLanguage(const CStdString &language)
+void CLangInfo::SetAudioLanguage(const std::string& language)
 {
-  if (language.empty() || !g_LangCodeExpander.ConvertToThreeCharCode(m_audioLanguage, language))
+  if (language.empty()
+    || StringUtils::EqualsNoCase(language, "default")
+    || StringUtils::EqualsNoCase(language, "original")
+    || !g_LangCodeExpander.ConvertToThreeCharCode(m_audioLanguage, language))
     m_audioLanguage.clear();
 }
 
@@ -476,32 +500,50 @@ const CStdString& CLangInfo::GetSubtitleLanguage() const
   return m_languageCodeGeneral;
 }
 
-void CLangInfo::SetSubtitleLanguage(const CStdString &language)
+void CLangInfo::SetSubtitleLanguage(const std::string& language)
 {
-  if (language.empty() || !g_LangCodeExpander.ConvertToThreeCharCode(m_subtitleLanguage, language))
+  if (language.empty()
+    || StringUtils::EqualsNoCase(language, "default")
+    || StringUtils::EqualsNoCase(language, "original")
+    || !g_LangCodeExpander.ConvertToThreeCharCode(m_subtitleLanguage, language))
     m_subtitleLanguage.clear();
 }
 
 // two character codes as defined in ISO639
-const CStdString& CLangInfo::GetDVDMenuLanguage() const
+const std::string CLangInfo::GetDVDMenuLanguage() const
 {
-  return m_currentRegion->m_strDVDMenuLanguage;
+  CStdString code;
+  if (!g_LangCodeExpander.ConvertToTwoCharCode(code, m_currentRegion->m_strLangLocaleName))
+    code = m_currentRegion->m_strDVDMenuLanguage;
+  
+  return code;
 }
 
 // two character codes as defined in ISO639
-const CStdString& CLangInfo::GetDVDAudioLanguage() const
+const std::string CLangInfo::GetDVDAudioLanguage() const
 {
-  return m_currentRegion->m_strDVDAudioLanguage;
+  CStdString code;
+  if (!g_LangCodeExpander.ConvertToTwoCharCode(code, m_audioLanguage))
+    code = m_currentRegion->m_strDVDAudioLanguage;
+  
+  return code;
 }
 
 // two character codes as defined in ISO639
-const CStdString& CLangInfo::GetDVDSubtitleLanguage() const
+const std::string CLangInfo::GetDVDSubtitleLanguage() const
 {
-  return m_currentRegion->m_strDVDSubtitleLanguage;
+  CStdString code;
+  if (!g_LangCodeExpander.ConvertToTwoCharCode(code, m_subtitleLanguage))
+    code = m_currentRegion->m_strDVDSubtitleLanguage;
+  
+  return code;
 }
 
-const CStdString& CLangInfo::GetLanguageLocale() const
+const std::string CLangInfo::GetLanguageLocale(bool twochar /* = false */) const
 {
+  if (twochar)
+    return m_currentRegion->m_strLangLocaleCodeTwoChar;
+
   return m_currentRegion->m_strLangLocaleName;
 }