summaryrefslogtreecommitdiff
path: root/LanguagesManager.cpp
blob: a8849c4aedc2021d5b7b86f247d9218cfc54d886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/** \file LanguagesManager.cpp
\brief Define the class to manage and load the languages
\author alpha_one_x86
\licence GPL3, see the file COPYING */

#include <QDir>
#include <QLibraryInfo>

#include "LanguagesManager.h"
#include "FacilityEngine.h"
#include "cpp11addition.h"

/// \brief Create the manager and load the defaults variables
LanguagesManager::LanguagesManager()
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    //load the rest
    std::vector<std::string> resourcesPaths=ResourcesManager::resourcesManager->getReadPath();
    unsigned int index=0;
    while(index<resourcesPaths.size())
    {
        std::string composedTempPath=resourcesPaths.at(index)+"Languages"+FacilityEngine::separator();
        QDir LanguagesConfiguration(QString::fromStdString(composedTempPath));
        if(LanguagesConfiguration.exists())
            languagePath.push_back(composedTempPath);
        index++;
    }
    //load the plugins
    PluginsManager::pluginsManager->lockPluginListEdition();
    connect(this,&LanguagesManager::previouslyPluginAdded,		this,	&LanguagesManager::onePluginAdded,Qt::QueuedConnection);
    connect(PluginsManager::pluginsManager,&PluginsManager::onePluginAdded,this,	&LanguagesManager::onePluginAdded,Qt::QueuedConnection);
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE
    connect(PluginsManager::pluginsManager,&PluginsManager::onePluginWillBeRemoved,	this,	&LanguagesManager::onePluginWillBeRemoved,Qt::DirectConnection);
    #endif
    connect(PluginsManager::pluginsManager,&PluginsManager::pluginListingIsfinish,		this,	&LanguagesManager::allPluginIsLoaded,Qt::QueuedConnection);
    std::vector<PluginsAvailable> list=PluginsManager::pluginsManager->getPluginsByCategory(PluginType_Languages);
    foreach(PluginsAvailable currentPlugin,list)
        emit previouslyPluginAdded(currentPlugin);
    PluginsManager::pluginsManager->unlockPluginListEdition();
    //load the GUI option
    std::vector<std::pair<std::string, std::string> > KeysList;
    KeysList.push_back(std::pair<std::string, std::string>("Language","en"));
    KeysList.push_back(std::pair<std::string, std::string>("Language_force","false"));
    OptionEngine::optionEngine->addOptionGroup("Language",KeysList);
//	connect(this,	&LanguagesManager::newLanguageLoaded,			plugins,&PluginsManager::refreshPluginList);
//	connect(this,	&LanguagesManager::newLanguageLoaded,			this,&LanguagesManager::retranslateTheUI);
    connect(OptionEngine::optionEngine,&OptionEngine::newOptionValue,				this,	&LanguagesManager::newOptionValue,Qt::QueuedConnection);
    connect(this,	&LanguagesManager::newLanguageLoaded,			PluginsManager::pluginsManager,&PluginsManager::newLanguageLoaded,Qt::QueuedConnection);
}

/// \brief Destroy the language manager
LanguagesManager::~LanguagesManager()
{
}

/// \brief load the language selected, return the main short code like en, fr, ..
std::string LanguagesManager::getTheRightLanguage() const
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    if(LanguagesAvailableList.size()==0)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"empty combobox list, failing back to english");
        return "en";
    }
    else
    {
        if(!stringtobool(OptionEngine::optionEngine->getOptionValue("Language","Language_force")))
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"language auto-detection, QLocale::system().name(): "+QLocale::system().name().toStdString()+", QLocale::languageToString(QLocale::system().language()): "+QLocale::languageToString(QLocale::system().language()).toStdString());
            std::string tempLanguage=getMainShortName(QLocale::languageToString(QLocale::system().language()).toStdString());
            if(!tempLanguage.empty())
                return tempLanguage;
            else
            {
                tempLanguage=getMainShortName(QLocale::system().name().toStdString());
                if(!tempLanguage.empty())
                    return tempLanguage;
                else
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Autodetection of the language failed, QLocale::languageToString(QLocale::system().language()): "+QLocale::languageToString(QLocale::system().language()).toStdString()+", QLocale::system().name(): "+QLocale::system().name().toStdString()+", failing back to english");
                    return OptionEngine::optionEngine->getOptionValue("Language","Language");
                }
            }
        }
        else
            return OptionEngine::optionEngine->getOptionValue("Language","Language");
    }
}

/* \brief To set the current language
\param newLanguage Should be short name code found into informations.xml of language file */
void LanguagesManager::setCurrentLanguage(const std::string &newLanguage)
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start: "+newLanguage);
    //protection for re-set the same language
    if(currentLanguage==newLanguage)
        return;
    //store the language
    PluginsManager::pluginsManager->setLanguage(newLanguage);
    //unload the old language
    if(currentLanguage!="en")
    {
        unsigned int indexTranslator=0;
        while(indexTranslator<installedTranslator.size())
        {
            QCoreApplication::removeTranslator(installedTranslator.at(indexTranslator));
            delete installedTranslator.at(indexTranslator);
            indexTranslator++;
        }
        installedTranslator.clear();
    }
    unsigned int index=0;
    while(index<LanguagesAvailableList.size())
    {
        if(LanguagesAvailableList.at(index).mainShortName==newLanguage)
        {
            //load the new language
            if(newLanguage!="en")
            {
                QTranslator *temp;
                std::vector<std::string> fileToLoad;
                //load the language main
                if(newLanguage=="en")
                    fileToLoad.push_back(":/Languages/en/translation.qm");
                else
                    fileToLoad.push_back(LanguagesAvailableList.at(index).path+"translation.qm");
                //load the language plugin
                std::vector<PluginsAvailable> listLoadedPlugins=PluginsManager::pluginsManager->getPlugins();
                unsigned int indexPluginIndex=0;
                while(indexPluginIndex<listLoadedPlugins.size())
                {
                    if(listLoadedPlugins.at(indexPluginIndex).category!=PluginType_Languages)
                    {
                        std::string tempPath=listLoadedPlugins.at(indexPluginIndex).path+"Languages"+FacilityEngine::separator()+LanguagesAvailableList.at(index).mainShortName+FacilityEngine::separator()+"translation.qm";
                        if(QFile::exists(QString::fromStdString(tempPath)))
                            fileToLoad.push_back(tempPath);
                    }
                    indexPluginIndex++;
                }
                unsigned int indexTranslationFile=0;
                while(indexTranslationFile<fileToLoad.size())
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"Translation to load: "+fileToLoad.at(indexTranslationFile));
                    temp=new QTranslator();
                    if(!temp->load(QString::fromStdString(fileToLoad.at(indexTranslationFile))) || temp->isEmpty())
                    {
                        delete temp;
                        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Unable to load the translation file: "+fileToLoad.at(indexTranslationFile));
                    }
                    else
                    {
                        QCoreApplication::installTranslator(temp);
                        installedTranslator.push_back(temp);
                    }
                    indexTranslationFile++;
                }
                temp=new QTranslator();
                if(temp->load(QString("qt_")+QString::fromStdString(newLanguage), QLibraryInfo::location(QLibraryInfo::TranslationsPath)) && !temp->isEmpty())
                {
                    QCoreApplication::installTranslator(temp);
                    installedTranslator.push_back(temp);
                }
                else
                {
                    if(!temp->load(QString::fromStdString(LanguagesAvailableList.at(index).path)+"qt.qm") || temp->isEmpty())
                    {
                        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Unable to load the translation file: qt.qm, into: "+LanguagesAvailableList.at(index).path);
                        delete temp;
                    }
                    else
                    {
                        QCoreApplication::installTranslator(temp);
                        installedTranslator.push_back(temp);
                    }
                }
            }
            currentLanguage=newLanguage;
            FacilityEngine::facilityEngine.retranslate();
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"emit newLanguageLoaded()");
            emit newLanguageLoaded(currentLanguage);
            return;
        }
        index++;
    }
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to found language: "+newLanguage+", LanguagesAvailableList.size(): "+std::to_string(LanguagesAvailableList.size()));
}

/// \brief check if short name is found into language
std::string LanguagesManager::getMainShortName(const std::string &shortName) const
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    unsigned int index=0;
    while(index<LanguagesAvailableList.size())
    {
        const LanguagesAvailable &languagesAvailable=LanguagesAvailableList.at(index);
        if(languagesAvailable.shortName.find(shortName)!=languagesAvailable.shortName.cend() || languagesAvailable.fullName==shortName)
            return languagesAvailable.mainShortName;
        index++;
    }
    return "";
}

/// \brief load the language in languagePath
void LanguagesManager::allPluginIsLoaded()
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    setCurrentLanguage(getTheRightLanguage());
}

const std::string LanguagesManager::autodetectedLanguage() const
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"language auto-detection, QLocale::system().name(): "+QLocale::system().name().toStdString()+", QLocale::languageToString(QLocale::system().language()): "+QLocale::languageToString(QLocale::system().language()).toStdString());
    std::string tempLanguage=getMainShortName(QLocale::languageToString(QLocale::system().language()).toStdString());
    if(!tempLanguage.empty())
        return tempLanguage;
    else
    {
        tempLanguage=getMainShortName(QLocale::system().name().toStdString());
        if(!tempLanguage.empty())
            return tempLanguage;
    }
    return std::string();
}

void LanguagesManager::onePluginAdded(const PluginsAvailable &plugin)
{
    if(plugin.category!=PluginType_Languages)
        return;
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    QDomElement child = plugin.categorySpecific.firstChildElement(QStringLiteral("fullName"));
    LanguagesAvailable temp;
    if(!child.isNull() && child.isElement())
        temp.fullName=child.text().toStdString();
    child = plugin.categorySpecific.firstChildElement(QStringLiteral("shortName"));
    while(!child.isNull())
    {
        if(child.isElement())
        {
            if(child.hasAttribute("mainCode") && child.attribute(QStringLiteral("mainCode"))==QStringLiteral("true"))
                temp.mainShortName=child.text().toStdString();
            temp.shortName.insert(child.text().toStdString());
        }
        child = child.nextSiblingElement(QStringLiteral("shortName"));
    }
    temp.path=plugin.path;
    if(temp.fullName.empty())
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"fullName empty for: "+plugin.path);
    else if(temp.path.empty())
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"path empty for: "+plugin.path);
    else if(temp.mainShortName.empty())
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"mainShortName empty for: "+plugin.path);
    else if(temp.shortName.size()<=0)
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"temp.shortName.size()<=0 for: "+plugin.path);
    else if(!QFile::exists(QString::fromStdString(temp.path)+QStringLiteral("flag.png")))
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"flag file not found for: "+plugin.path);
    else if(!QFile::exists(QString::fromStdString(temp.path)+QStringLiteral("translation.qm")) && temp.mainShortName!="en")
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"translation not found for: "+plugin.path);
    else
        LanguagesAvailableList.push_back(temp);
    if(PluginsManager::pluginsManager->allPluginHaveBeenLoaded())
        setCurrentLanguage(getTheRightLanguage());
}

#ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE
void LanguagesManager::onePluginWillBeRemoved(const PluginsAvailable &plugin)
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    unsigned int index=0;
    while(index<LanguagesAvailableList.size())
    {
        if(plugin.path==LanguagesAvailableList.at(index).path)
        {
            return;
        }
        index++;
    }
}
#endif

void LanguagesManager::newOptionValue(const std::string &group)
{
    if(group=="Language")
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"group: "+group);
        setCurrentLanguage(getTheRightLanguage());
    }
}