/** \file LanguagesManager.cpp \brief Define the class to manage and load the languages \author alpha_one_x86 \version 0.3 \date 2010 \licence GPL3, see the file COPYING */ #include #include #include "LanguagesManager.h" /// \brief Create the manager and load the defaults variables LanguagesManager::LanguagesManager() { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start"); //load the overall instance resources=ResourcesManager::getInstance(); options=OptionEngine::getInstance(); plugins=PluginsManager::getInstance(); //load the rest QStringList resourcesPaths=resources->getReadPath(); int index=0; while(indexlockPluginListEdition(); qRegisterMetaType("PluginsAvailable"); connect(this,SIGNAL(previouslyPluginAdded(PluginsAvailable)), this,SLOT(onePluginAdded(PluginsAvailable)),Qt::QueuedConnection); connect(plugins,SIGNAL(onePluginAdded(PluginsAvailable)), this, SLOT(onePluginAdded(PluginsAvailable)),Qt::QueuedConnection); connect(plugins,SIGNAL(onePluginWillBeRemoved(PluginsAvailable)), this, SLOT(onePluginWillBeRemoved(PluginsAvailable)),Qt::DirectConnection); connect(plugins,SIGNAL(pluginListingIsfinish()), this, SLOT(allPluginIsLoaded()),Qt::QueuedConnection); QList list=plugins->getPluginsByCategory(PluginType_Languages); foreach(PluginsAvailable currentPlugin,list) emit previouslyPluginAdded(currentPlugin); plugins->unlockPluginListEdition(); //load the GUI option QList > KeysList; KeysList.append(qMakePair(QString("Language"),QVariant("en"))); KeysList.append(qMakePair(QString("Language_autodetect"),QVariant(true))); options->addOptionGroup("Language",KeysList); // connect(this, SIGNAL(newLanguageLoaded(QString)), plugins,SLOT(refreshPluginList(QString))); // connect(this, SIGNAL(newLanguageLoaded(QString)), this,SLOT(retranslateTheUI())); connect(options,SIGNAL(newOptionValue(QString,QString,QVariant)), this, SLOT(newOptionValue(QString)),Qt::QueuedConnection); connect(this, SIGNAL(newLanguageLoaded(QString)), plugins,SIGNAL(newLanguageLoaded()),Qt::QueuedConnection); } /// \brief Destroy the language manager LanguagesManager::~LanguagesManager() { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start"); PluginsManager::destroyInstanceAtTheLastCall(); OptionEngine::destroyInstanceAtTheLastCall(); ResourcesManager::destroyInstanceAtTheLastCall(); } /// \brief load the language selected, return the main short code like en, fr, .. QString LanguagesManager::getTheRightLanguage() { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start"); if(LanguagesAvailableList.size()==0) { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"empty combobox list, failing back to english"); return "en"; } else { if(options->getOptionValue("Language","Language_autodetect").toBool()) { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"language auto-detection, QLocale::system().name(): "+QLocale::system().name()+", QLocale::languageToString(QLocale::system().language()): "+QLocale::languageToString(QLocale::system().language())); QString tempLanguage=getMainShortName(QLocale::languageToString(QLocale::system().language())); if(tempLanguage!="") return tempLanguage; else { tempLanguage=getMainShortName(QLocale::system().name()); if(tempLanguage!="") return tempLanguage; else { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"Autodetection of the language failed, QLocale::languageToString(QLocale::system().language()): "+QLocale::languageToString(QLocale::system().language())+", QLocale::system().name(): "+QLocale::system().name()+", failing back to english"); return options->getOptionValue("Language","Language").toString(); } } } else return options->getOptionValue("Language","Language").toString(); } } /* \brief To set the current language \param newLanguage Should be short name code found into informations.xml of language file */ void LanguagesManager::setCurrentLanguage(const QString &newLanguage) { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start: "+newLanguage); //protection for re-set the same language if(currentLanguage==newLanguage) return; //store the language plugins->setLanguage(newLanguage); //unload the old language if(currentLanguage!="en") { int indexTranslator=0; while(indexTranslator listLoadedPlugins=plugins->getPlugins(); int indexPluginIndex=0; while(indexPluginIndexload(fileToLoad.at(indexTranslationFile)) || temp->isEmpty()) { delete temp; ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"Unable to load the translation file: "+fileToLoad.at(indexTranslationFile)); } else { QCoreApplication::installTranslator(temp); installedTranslator<load(QString("qt_")+newLanguage, QLibraryInfo::location(QLibraryInfo::TranslationsPath)) && !temp->isEmpty()) { QCoreApplication::installTranslator(temp); installedTranslator<load(LanguagesAvailableList.at(index).path+"qt.qm") || temp->isEmpty()) { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"Unable to load the translation file: qt.qm, into: "+LanguagesAvailableList.at(index).path); delete temp; } else { QCoreApplication::installTranslator(temp); installedTranslator<allPluginHaveBeenLoaded()) setCurrentLanguage(getTheRightLanguage()); } void LanguagesManager::onePluginWillBeRemoved(const PluginsAvailable &plugin) { ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start"); int index=0; while(index