File indexing completed on 2024-05-12 05:39:41

0001 /***************************************************************************
0002  *  Copyright (C) 2020 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "rolisteamapplication.h"
0021 
0022 #include <QDebug>
0023 #include <QIcon>
0024 #include <QSettings>
0025 #include <QTranslator>
0026 #include <iostream>
0027 
0028 #include "controller/networkcontroller.h"
0029 
0030 RolisteamApplication::RolisteamApplication(const QString& appName, const QString& version, int& argn, char* argv[])
0031     : QApplication(argn, argv), m_game(GameController(appName, version, clipboard()))
0032 {
0033     setAttribute(Qt::AA_DontUseNativeMenuBar, true);
0034     connect(&m_game, &GameController::closingApp, this, [this]() {
0035         emit quitApp();
0036     });
0037     connect(m_game.networkController(), &NetworkController::connectedChanged, this, [this](bool connected) {
0038         qDebug() << "app connection status changed" << connected;
0039         emit connectStatusChanged(connected);
0040     });
0041     #ifdef Q_OS_WIN
0042         QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << ":/resources");
0043         QIcon::setThemeName("rolistheme");
0044     #endif
0045 
0046     qDebug() << QIcon::themeSearchPaths() << QIcon::themeName();
0047 
0048     QIcon::setFallbackSearchPaths(QIcon::fallbackSearchPaths() << ":/resources/rolistheme");
0049 
0050     setApplicationName(appName);
0051     setOrganizationName(appName);
0052     setWindowIcon(QIcon::fromTheme("500-symbole"));
0053 
0054     setApplicationVersion(version);
0055     readSettings();
0056 }
0057 
0058 bool RolisteamApplication::notify(QObject* receiver, QEvent* e)
0059 {
0060     try
0061     {
0062         return QApplication::notify(receiver, e);
0063     }
0064     catch(...)
0065     {
0066         std::exception_ptr p= std::current_exception();
0067         std::clog << (p ? typeid(p).name() : "null") << std::endl;
0068         qDebug() << e->type() << receiver << " other";
0069         return false;
0070     }
0071 }
0072 
0073 GameController* RolisteamApplication::gameCtrl()
0074 {
0075     return &m_game;
0076 }
0077 
0078 void RolisteamApplication::readSettings()
0079 {
0080     QSettings settings(applicationName(), QString("%1_%2/preferences").arg(applicationName(), applicationVersion()));
0081     settings.beginGroup("rolisteam/preferences");
0082     int size= settings.beginReadArray("preferenceMap");
0083     QHash<QString, QVariant> optionDictionary;
0084     for(int i= 0; i < size; ++i)
0085     {
0086         settings.setArrayIndex(i);
0087         QString key= settings.value("key").toString();
0088         QVariant value= settings.value("value");
0089         optionDictionary.insert(key, value);
0090     }
0091     settings.endArray();
0092     settings.endGroup();
0093 
0094     auto system= optionDictionary.value("i18n_system").toBool();
0095     auto paths= optionDictionary.value("i18n_path").toStringList();
0096     auto hasCustomfile= optionDictionary.value("i18n_hasCustomfile").toBool();
0097     auto customFile= optionDictionary.value("i18n_customfile").toString();
0098 
0099     if(hasCustomfile && !system)
0100     {
0101         setTranslator({customFile});
0102     }
0103     else if(!system)
0104     {
0105         setTranslator(paths);
0106     }
0107     else
0108     {
0109         QLocale locale;
0110         setTranslator({":/translations/rolisteam_" + locale.name(), ":/translations/qt_" + locale.name()});
0111     }
0112 }
0113 
0114 void RolisteamApplication::setTranslator(const QStringList& list)
0115 {
0116     for(auto trans : qAsConst(m_translators))
0117         removeTranslator(trans);
0118 
0119     qDeleteAll(m_translators);
0120     m_translators.clear();
0121 
0122     for(const auto& info : list)
0123     {
0124         QTranslator* trans= new QTranslator(this);
0125         trans->load(info);
0126         if(trans->isEmpty())
0127         {
0128             delete trans;
0129             continue;
0130         }
0131         installTranslator(trans);
0132         m_translators.append(trans);
0133     }
0134 }
0135 
0136 void RolisteamApplication::configureEnginePostLoad(QQmlApplicationEngine* engine)
0137 {
0138     Q_UNUSED(engine)
0139     // engine->addstuff
0140 }