File indexing completed on 2024-04-21 14:43:48

0001 /* GCompris - ApplicationSettings.cpp
0002  *
0003  * SPDX-FileCopyrightText: 2014-2016 Johnny Jazeix <jazeix@gmail.com>
0004  *
0005  * Authors:
0006  *   Johnny Jazeix <jazeix@gmail.com>
0007  *
0008  *   SPDX-License-Identifier: GPL-3.0-or-later
0009  */
0010 
0011 #include "ApplicationSettings.h"
0012 #include "ApplicationInfo.h"
0013 
0014 #include "DownloadManager.h"
0015 
0016 #include <qmath.h>
0017 #include <QGuiApplication>
0018 #include <QScreen>
0019 #include <QRect>
0020 
0021 #include <QtQml>
0022 
0023 namespace {
0024     const char *GC_DEFAULT_FONT = "Andika-R.ttf";
0025     constexpr int GC_DEFAULT_FONT_CAPITALIZATION = 0; // Font.MixedCase
0026     constexpr int GC_DEFAULT_FONT_LETTER_SPACING = 0;
0027 
0028     const char *GENERAL_GROUP_KEY = "General";
0029     const char *ADMIN_GROUP_KEY = "Admin";
0030     const char *INTERNAL_GROUP_KEY = "Internal";
0031     const char *FAVORITE_GROUP_KEY = "Favorite";
0032     const char *LEVELS_GROUP_KEY = "Levels";
0033 
0034     const char *FULLSCREEN_KEY = "fullscreen";
0035     const char *PREVIOUS_HEIGHT_KEY = "previousHeight";
0036     const char *PREVIOUS_WIDTH_KEY = "previousWidth";
0037     const char *ENABLE_AUDIO_VOICES_KEY = "enableAudioVoices";
0038     const char *ENABLE_AUDIO_EFFECTS_KEY = "enableAudioEffects";
0039     const char *ENABLE_BACKGROUND_MUSIC_KEY = "enableBackgroundMusic";
0040     const char *VIRTUALKEYBOARD_KEY = "virtualKeyboard";
0041     const char *LOCALE_KEY = "locale";
0042     const char *FONT_KEY = "font";
0043     const char *IS_CURRENT_FONT_EMBEDDED = "isCurrentFontEmbedded";
0044     const char *ENABLE_AUTOMATIC_DOWNLOADS = "enableAutomaticDownloads";
0045     const char *FILTERED_BACKGROUND_MUSIC_KEY = "filteredBackgroundMusic";
0046     const char *BACKGROUND_MUSIC_VOLUME_KEY = "backgroundMusicVolume";
0047     const char *AUDIO_EFFECTS_VOLUME_KEY = "audioEffectsVolume";
0048 
0049     const char *DOWNLOAD_SERVER_URL_KEY = "downloadServerUrl";
0050     const char *CACHE_PATH_KEY = "cachePath";
0051     const char *USERDATA_PATH_KEY = "userDataPath";
0052     const char *RENDERER_KEY = "renderer";
0053 
0054     const char *EXE_COUNT_KEY = "exeCount";
0055     const char *LAST_GC_VERSION_RAN = "lastGCVersionRan";
0056 
0057     const char *FILTER_LEVEL_MIN = "filterLevelMin";
0058     const char *FILTER_LEVEL_MAX = "filterLevelMax";
0059 
0060     const char *BASE_FONT_SIZE_KEY = "baseFontSize";
0061     const char *FONT_CAPITALIZATION = "fontCapitalization";
0062     const char *FONT_LETTER_SPACING = "fontLetterSpacing";
0063 
0064     const char *DEFAULT_CURSOR = "defaultCursor";
0065     const char *NO_CURSOR = "noCursor";
0066     const char *KIOSK_KEY = "kiosk";
0067     const char *SECTION_VISIBLE = "sectionVisible";
0068     const char *EXIT_CONFIRMATION = "exitConfirmation";
0069 
0070     const char *PROGRESS_KEY = "progress";
0071 
0072     const char *DEFAULT_DOWNLOAD_SERVER = "https://cdn.kde.org/gcompris";
0073 }
0074 
0075 ApplicationSettings *ApplicationSettings::m_instance = nullptr;
0076 
0077 ApplicationSettings::ApplicationSettings(const QString &configPath, QObject *parent) :
0078     QObject(parent),
0079     m_baseFontSizeMin(-7), m_baseFontSizeMax(7),
0080     m_fontLetterSpacingMin(0.0), m_fontLetterSpacingMax(8.0),
0081     m_config(configPath, QSettings::IniFormat)
0082 {
0083     const QRect &screenSize = QGuiApplication::screens().at(0)->availableGeometry();
0084     // initialize from settings file or default
0085 
0086     // general group
0087     m_config.beginGroup(GENERAL_GROUP_KEY);
0088     m_isAudioEffectsEnabled = m_config.value(ENABLE_AUDIO_EFFECTS_KEY, true).toBool();
0089     m_isBackgroundMusicEnabled = m_config.value(ENABLE_BACKGROUND_MUSIC_KEY, true).toBool();
0090     m_isFullscreen = m_config.value(FULLSCREEN_KEY, true).toBool();
0091     m_previousHeight = m_config.value(PREVIOUS_HEIGHT_KEY, screenSize.height()).toUInt();
0092     m_previousWidth = m_config.value(PREVIOUS_WIDTH_KEY, screenSize.width()).toUInt();
0093     m_isAudioVoicesEnabled = m_config.value(ENABLE_AUDIO_VOICES_KEY, true).toBool();
0094     m_isVirtualKeyboard = m_config.value(VIRTUALKEYBOARD_KEY,
0095                                          ApplicationInfo::getInstance()->isMobile())
0096                               .toBool();
0097     m_locale = m_config.value(LOCALE_KEY, GC_DEFAULT_LOCALE).toString();
0098     m_font = m_config.value(FONT_KEY, QLatin1String(GC_DEFAULT_FONT)).toString();
0099     if (m_font == QLatin1String("Andika-R.otf"))
0100         m_font = "Andika-R.ttf";
0101     m_fontCapitalization = m_config.value(FONT_CAPITALIZATION, GC_DEFAULT_FONT_CAPITALIZATION).toUInt();
0102     m_fontLetterSpacing = m_config.value(FONT_LETTER_SPACING, GC_DEFAULT_FONT_LETTER_SPACING).toReal();
0103     m_isEmbeddedFont = m_config.value(IS_CURRENT_FONT_EMBEDDED, true).toBool();
0104     m_filteredBackgroundMusic = m_config.value(FILTERED_BACKGROUND_MUSIC_KEY, ApplicationInfo::getInstance()->getBackgroundMusicFromRcc()).toStringList();
0105     m_backgroundMusicVolume = m_config.value(BACKGROUND_MUSIC_VOLUME_KEY, 0.2).toReal();
0106     m_audioEffectsVolume = m_config.value(AUDIO_EFFECTS_VOLUME_KEY, 0.7).toReal();
0107 
0108 #if defined(WITH_KIOSK_MODE)
0109     m_isKioskMode = m_config.value(KIOSK_KEY, true).toBool();
0110 #else
0111     m_isKioskMode = m_config.value(KIOSK_KEY, false).toBool();
0112 #endif
0113 
0114     m_sectionVisible = m_config.value(SECTION_VISIBLE, true).toBool();
0115     m_exitConfirmation = m_config.value(EXIT_CONFIRMATION, ApplicationInfo::getInstance()->isMobile() ? true : false).toBool();
0116 
0117     m_isAutomaticDownloadsEnabled = m_config.value(ENABLE_AUTOMATIC_DOWNLOADS,
0118                                                    !ApplicationInfo::getInstance()->isMobile() && ApplicationInfo::isDownloadAllowed())
0119                                         .toBool();
0120     m_filterLevelMin = m_config.value(FILTER_LEVEL_MIN, 1).toUInt();
0121     m_filterLevelMax = m_config.value(FILTER_LEVEL_MAX, 6).toUInt();
0122     m_defaultCursor = m_config.value(DEFAULT_CURSOR, false).toBool();
0123     m_noCursor = m_config.value(NO_CURSOR, false).toBool();
0124     m_baseFontSize = m_config.value(BASE_FONT_SIZE_KEY, 0).toInt();
0125 
0126     m_config.sync(); // make sure all defaults are written back
0127     m_config.endGroup();
0128 
0129     // admin group
0130     m_config.beginGroup(ADMIN_GROUP_KEY);
0131     m_downloadServerUrl = m_config.value(DOWNLOAD_SERVER_URL_KEY, QLatin1String(DEFAULT_DOWNLOAD_SERVER)).toString();
0132     if (m_downloadServerUrl == "http://gcompris.net") {
0133         setDownloadServerUrl(DEFAULT_DOWNLOAD_SERVER);
0134     }
0135     m_cachePath = m_config.value(CACHE_PATH_KEY, QStandardPaths::writableLocation(QStandardPaths::CacheLocation)).toString();
0136 #if defined(UBUNTUTOUCH)
0137     m_userDataPath = m_config.value(USERDATA_PATH_KEY, QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).toString();
0138 #else
0139     m_userDataPath = m_config.value(USERDATA_PATH_KEY, QString(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/GCompris"))).toString();
0140 #endif
0141     m_renderer = m_config.value(RENDERER_KEY, GRAPHICAL_RENDERER).toString();
0142     m_config.endGroup();
0143 
0144     // internal group
0145     m_config.beginGroup(INTERNAL_GROUP_KEY);
0146     m_exeCount = m_config.value(EXE_COUNT_KEY, 0).toUInt();
0147     m_lastGCVersionRan = m_config.value(LAST_GC_VERSION_RAN, 0).toUInt();
0148     m_config.endGroup();
0149 
0150     // no group
0151     m_isBarHidden = false;
0152 
0153     connect(this, &ApplicationSettings::audioVoicesEnabledChanged, this, &ApplicationSettings::notifyAudioVoicesEnabledChanged);
0154     connect(this, &ApplicationSettings::audioEffectsEnabledChanged, this, &ApplicationSettings::notifyAudioEffectsEnabledChanged);
0155     connect(this, &ApplicationSettings::backgroundMusicEnabledChanged, this, &ApplicationSettings::notifyBackgroundMusicEnabledChanged);
0156     connect(this, &ApplicationSettings::filteredBackgroundMusicChanged, this, &ApplicationSettings::notifyFilteredBackgroundMusicChanged);
0157     connect(this, &ApplicationSettings::fullscreenChanged, this, &ApplicationSettings::notifyFullscreenChanged);
0158     connect(this, &ApplicationSettings::previousHeightChanged, this, &ApplicationSettings::notifyPreviousHeightChanged);
0159     connect(this, &ApplicationSettings::previousWidthChanged, this, &ApplicationSettings::notifyPreviousWidthChanged);
0160     connect(this, &ApplicationSettings::localeChanged, this, &ApplicationSettings::notifyLocaleChanged);
0161     connect(this, &ApplicationSettings::fontChanged, this, &ApplicationSettings::notifyFontChanged);
0162     connect(this, &ApplicationSettings::virtualKeyboardChanged, this, &ApplicationSettings::notifyVirtualKeyboardChanged);
0163     connect(this, &ApplicationSettings::automaticDownloadsEnabledChanged, this, &ApplicationSettings::notifyAutomaticDownloadsEnabledChanged);
0164     connect(this, &ApplicationSettings::filterLevelMinChanged, this, &ApplicationSettings::notifyFilterLevelMinChanged);
0165     connect(this, &ApplicationSettings::filterLevelMaxChanged, this, &ApplicationSettings::notifyFilterLevelMaxChanged);
0166     connect(this, &ApplicationSettings::sectionVisibleChanged, this, &ApplicationSettings::notifySectionVisibleChanged);
0167     connect(this, &ApplicationSettings::exitConfirmationChanged, this, &ApplicationSettings::notifyExitConfirmationChanged);
0168     connect(this, &ApplicationSettings::kioskModeChanged, this, &ApplicationSettings::notifyKioskModeChanged);
0169     connect(this, &ApplicationSettings::downloadServerUrlChanged, this, &ApplicationSettings::notifyDownloadServerUrlChanged);
0170     connect(this, &ApplicationSettings::cachePathChanged, this, &ApplicationSettings::notifyCachePathChanged);
0171     connect(this, &ApplicationSettings::userDataPathChanged, this, &ApplicationSettings::notifyUserDataPathChanged);
0172     connect(this, &ApplicationSettings::rendererChanged, this, &ApplicationSettings::notifyRendererChanged);
0173     connect(this, &ApplicationSettings::exeCountChanged, this, &ApplicationSettings::notifyExeCountChanged);
0174     connect(this, &ApplicationSettings::barHiddenChanged, this, &ApplicationSettings::notifyBarHiddenChanged);
0175     connect(this, &ApplicationSettings::lastGCVersionRanChanged, this, &ApplicationSettings::notifyLastGCVersionRanChanged);
0176     connect(this, &ApplicationSettings::backgroundMusicVolumeChanged, this, &ApplicationSettings::notifyBackgroundMusicVolumeChanged);
0177     connect(this, &ApplicationSettings::audioEffectsVolumeChanged, this, &ApplicationSettings::notifyAudioEffectsVolumeChanged);
0178 }
0179 
0180 ApplicationSettings::~ApplicationSettings()
0181 {
0182     // make sure settings file is up2date:
0183     // general group
0184     m_config.beginGroup(GENERAL_GROUP_KEY);
0185     m_config.setValue(ENABLE_AUDIO_VOICES_KEY, m_isAudioVoicesEnabled);
0186     m_config.setValue(ENABLE_BACKGROUND_MUSIC_KEY, m_isBackgroundMusicEnabled);
0187     m_config.setValue(FILTERED_BACKGROUND_MUSIC_KEY, m_filteredBackgroundMusic);
0188     m_config.setValue(BACKGROUND_MUSIC_VOLUME_KEY, m_backgroundMusicVolume);
0189     m_config.setValue(AUDIO_EFFECTS_VOLUME_KEY, m_audioEffectsVolume);
0190     m_config.setValue(LOCALE_KEY, m_locale);
0191     m_config.setValue(FONT_KEY, m_font);
0192     m_config.setValue(IS_CURRENT_FONT_EMBEDDED, m_isEmbeddedFont);
0193     m_config.setValue(FULLSCREEN_KEY, m_isFullscreen);
0194     m_config.setValue(PREVIOUS_HEIGHT_KEY, m_previousHeight);
0195     m_config.setValue(PREVIOUS_WIDTH_KEY, m_previousWidth);
0196     m_config.setValue(VIRTUALKEYBOARD_KEY, m_isVirtualKeyboard);
0197     m_config.setValue(ENABLE_AUTOMATIC_DOWNLOADS, m_isAutomaticDownloadsEnabled);
0198     if (!m_filterLevelOverridedByCommandLineOption) {
0199         m_config.setValue(FILTER_LEVEL_MIN, m_filterLevelMin);
0200         m_config.setValue(FILTER_LEVEL_MAX, m_filterLevelMax);
0201     }
0202     m_config.setValue(KIOSK_KEY, m_isKioskMode);
0203     m_config.setValue(SECTION_VISIBLE, m_sectionVisible);
0204     m_config.setValue(EXIT_CONFIRMATION, m_exitConfirmation);
0205     m_config.setValue(DEFAULT_CURSOR, m_defaultCursor);
0206     m_config.setValue(NO_CURSOR, m_noCursor);
0207     m_config.setValue(BASE_FONT_SIZE_KEY, m_baseFontSize);
0208     m_config.setValue(FONT_CAPITALIZATION, m_fontCapitalization);
0209     m_config.setValue(FONT_LETTER_SPACING, m_fontLetterSpacing);
0210     m_config.endGroup();
0211 
0212     // admin group
0213     m_config.beginGroup(ADMIN_GROUP_KEY);
0214     m_config.setValue(DOWNLOAD_SERVER_URL_KEY, m_downloadServerUrl);
0215     m_config.setValue(CACHE_PATH_KEY, m_cachePath);
0216     m_config.setValue(USERDATA_PATH_KEY, m_userDataPath);
0217     m_config.setValue(RENDERER_KEY, m_renderer);
0218     m_config.endGroup();
0219 
0220     // internal group
0221     m_config.beginGroup(INTERNAL_GROUP_KEY);
0222     m_config.setValue(EXE_COUNT_KEY, m_exeCount);
0223     m_config.setValue(LAST_GC_VERSION_RAN, m_lastGCVersionRan);
0224     m_config.endGroup();
0225 
0226     m_config.sync();
0227 
0228     m_instance = nullptr;
0229 }
0230 
0231 void ApplicationSettings::notifyAudioVoicesEnabledChanged()
0232 {
0233     updateValueInConfig(GENERAL_GROUP_KEY, ENABLE_AUDIO_VOICES_KEY, m_isAudioVoicesEnabled);
0234     qDebug() << "notifyAudioVoices: " << m_isAudioVoicesEnabled;
0235 }
0236 
0237 void ApplicationSettings::notifyAudioEffectsEnabledChanged()
0238 {
0239     updateValueInConfig(GENERAL_GROUP_KEY, ENABLE_AUDIO_EFFECTS_KEY, m_isAudioEffectsEnabled);
0240     qDebug() << "notifyAudioEffects: " << m_isAudioEffectsEnabled;
0241 }
0242 
0243 void ApplicationSettings::notifyBackgroundMusicEnabledChanged()
0244 {
0245     updateValueInConfig(GENERAL_GROUP_KEY, ENABLE_BACKGROUND_MUSIC_KEY, m_isBackgroundMusicEnabled);
0246     qDebug() << "notifyBackgroundMusic: " << m_isBackgroundMusicEnabled;
0247 }
0248 
0249 void ApplicationSettings::notifyFilteredBackgroundMusicChanged()
0250 {
0251     updateValueInConfig(GENERAL_GROUP_KEY, FILTERED_BACKGROUND_MUSIC_KEY, m_filteredBackgroundMusic);
0252     qDebug() << "filteredBackgroundMusic: " << m_filteredBackgroundMusic;
0253 }
0254 
0255 void ApplicationSettings::notifyBackgroundMusicVolumeChanged()
0256 {
0257     updateValueInConfig(GENERAL_GROUP_KEY, BACKGROUND_MUSIC_VOLUME_KEY, m_backgroundMusicVolume);
0258     qDebug() << "backgroundMusicVolume: " << m_backgroundMusicVolume;
0259 }
0260 
0261 void ApplicationSettings::notifyAudioEffectsVolumeChanged()
0262 {
0263     updateValueInConfig(GENERAL_GROUP_KEY, AUDIO_EFFECTS_VOLUME_KEY, m_audioEffectsVolume);
0264     qDebug() << "audioEffectsVolume: " << m_audioEffectsVolume;
0265 }
0266 
0267 void ApplicationSettings::notifyLocaleChanged()
0268 {
0269     updateValueInConfig(GENERAL_GROUP_KEY, LOCALE_KEY, m_locale);
0270     qDebug() << "new locale: " << m_locale;
0271 }
0272 
0273 void ApplicationSettings::notifyFontChanged()
0274 {
0275     updateValueInConfig(GENERAL_GROUP_KEY, FONT_KEY, m_font);
0276     qDebug() << "new font: " << m_font;
0277 }
0278 
0279 void ApplicationSettings::notifyEmbeddedFontChanged()
0280 {
0281     updateValueInConfig(GENERAL_GROUP_KEY, IS_CURRENT_FONT_EMBEDDED, m_isEmbeddedFont);
0282     qDebug() << "new font is embedded: " << m_isEmbeddedFont;
0283 }
0284 
0285 void ApplicationSettings::notifyFontCapitalizationChanged()
0286 {
0287     updateValueInConfig(GENERAL_GROUP_KEY, FONT_CAPITALIZATION, m_fontCapitalization);
0288     qDebug() << "new fontCapitalization: " << m_fontCapitalization;
0289 }
0290 
0291 void ApplicationSettings::notifyFontLetterSpacingChanged()
0292 {
0293     updateValueInConfig(GENERAL_GROUP_KEY, FONT_LETTER_SPACING, m_fontLetterSpacing);
0294     qDebug() << "new fontLetterSpacing: " << m_fontLetterSpacing;
0295 }
0296 
0297 void ApplicationSettings::notifyFullscreenChanged()
0298 {
0299     updateValueInConfig(GENERAL_GROUP_KEY, FULLSCREEN_KEY, m_isFullscreen);
0300     qDebug() << "fullscreen set to: " << m_isFullscreen;
0301 }
0302 
0303 void ApplicationSettings::notifyPreviousHeightChanged()
0304 {
0305     updateValueInConfig(GENERAL_GROUP_KEY, PREVIOUS_HEIGHT_KEY, m_previousHeight);
0306     qDebug() << "previous height set to: " << m_previousHeight;
0307 }
0308 
0309 void ApplicationSettings::notifyPreviousWidthChanged()
0310 {
0311     updateValueInConfig(GENERAL_GROUP_KEY, PREVIOUS_WIDTH_KEY, m_previousWidth);
0312     qDebug() << "previous width set to: " << m_previousWidth;
0313 }
0314 
0315 void ApplicationSettings::notifyVirtualKeyboardChanged()
0316 {
0317     updateValueInConfig(GENERAL_GROUP_KEY, VIRTUALKEYBOARD_KEY, m_isVirtualKeyboard);
0318     qDebug() << "virtualkeyboard set to: " << m_isVirtualKeyboard;
0319 }
0320 
0321 bool ApplicationSettings::isAutomaticDownloadsEnabled() const
0322 {
0323     return m_isAutomaticDownloadsEnabled && ApplicationInfo::isDownloadAllowed();
0324 }
0325 void ApplicationSettings::setIsAutomaticDownloadsEnabled(const bool newIsAutomaticDownloadsEnabled)
0326 {
0327     if (ApplicationInfo::isDownloadAllowed()) {
0328         m_isAutomaticDownloadsEnabled = newIsAutomaticDownloadsEnabled;
0329         Q_EMIT automaticDownloadsEnabledChanged();
0330     }
0331 }
0332 void ApplicationSettings::notifyAutomaticDownloadsEnabledChanged()
0333 {
0334     updateValueInConfig(GENERAL_GROUP_KEY, ENABLE_AUTOMATIC_DOWNLOADS, m_isAutomaticDownloadsEnabled);
0335     qDebug() << "enableAutomaticDownloads set to: " << m_isAutomaticDownloadsEnabled;
0336 }
0337 
0338 void ApplicationSettings::notifyFilterLevelMinChanged()
0339 {
0340     qDebug() << "filterLevelMin set to: " << m_filterLevelMin;
0341     if (!m_filterLevelOverridedByCommandLineOption) {
0342         updateValueInConfig(GENERAL_GROUP_KEY, FILTER_LEVEL_MIN, m_filterLevelMin);
0343     }
0344 }
0345 
0346 void ApplicationSettings::notifyFilterLevelMaxChanged()
0347 {
0348     qDebug() << "filterLevelMax set to: " << m_filterLevelMax;
0349     if (!m_filterLevelOverridedByCommandLineOption) {
0350         updateValueInConfig(GENERAL_GROUP_KEY, FILTER_LEVEL_MAX, m_filterLevelMax);
0351     }
0352 }
0353 
0354 void ApplicationSettings::notifyKioskModeChanged()
0355 {
0356     updateValueInConfig(GENERAL_GROUP_KEY, KIOSK_KEY, m_isKioskMode);
0357     qDebug() << "notifyKioskMode: " << m_isKioskMode;
0358 }
0359 
0360 void ApplicationSettings::notifySectionVisibleChanged()
0361 {
0362     updateValueInConfig(GENERAL_GROUP_KEY, SECTION_VISIBLE, m_sectionVisible);
0363     qDebug() << "notifySectionVisible: " << m_sectionVisible;
0364 }
0365 
0366 void ApplicationSettings::notifyExitConfirmationChanged()
0367 {
0368     updateValueInConfig(GENERAL_GROUP_KEY, EXIT_CONFIRMATION, m_exitConfirmation);
0369 }
0370 
0371 void ApplicationSettings::notifyDownloadServerUrlChanged()
0372 {
0373     updateValueInConfig(ADMIN_GROUP_KEY, DOWNLOAD_SERVER_URL_KEY, m_downloadServerUrl);
0374     qDebug() << "downloadServerUrl set to: " << m_downloadServerUrl;
0375 }
0376 
0377 void ApplicationSettings::notifyCachePathChanged()
0378 {
0379     updateValueInConfig(ADMIN_GROUP_KEY, CACHE_PATH_KEY, m_cachePath);
0380     qDebug() << "cachePath set to: " << m_cachePath;
0381 }
0382 
0383 void ApplicationSettings::notifyUserDataPathChanged()
0384 {
0385     updateValueInConfig(ADMIN_GROUP_KEY, USERDATA_PATH_KEY, m_userDataPath);
0386     qDebug() << "userDataPath set to: " << m_userDataPath;
0387 }
0388 
0389 void ApplicationSettings::notifyRendererChanged()
0390 {
0391     updateValueInConfig(ADMIN_GROUP_KEY, RENDERER_KEY, m_renderer);
0392     qDebug() << "renderer set to: " << m_renderer;
0393 }
0394 
0395 void ApplicationSettings::notifyExeCountChanged()
0396 {
0397     updateValueInConfig(INTERNAL_GROUP_KEY, EXE_COUNT_KEY, m_exeCount);
0398     qDebug() << "exeCount set to: " << m_exeCount;
0399 }
0400 
0401 void ApplicationSettings::notifyLastGCVersionRanChanged()
0402 {
0403     updateValueInConfig(INTERNAL_GROUP_KEY, LAST_GC_VERSION_RAN, m_lastGCVersionRan);
0404     qDebug() << "lastVersionRan set to: " << m_lastGCVersionRan;
0405 }
0406 
0407 void ApplicationSettings::notifyBarHiddenChanged()
0408 {
0409     qDebug() << "is bar hidden: " << m_isBarHidden;
0410 }
0411 
0412 void ApplicationSettings::saveBaseFontSize()
0413 {
0414     updateValueInConfig(GENERAL_GROUP_KEY, BASE_FONT_SIZE_KEY, m_baseFontSize);
0415 }
0416 
0417 void ApplicationSettings::saveActivityConfiguration(const QString &activity, const QVariantMap &data)
0418 {
0419     qDebug() << "save configuration for:" << activity;
0420     QMapIterator<QString, QVariant> i(data);
0421     while (i.hasNext()) {
0422         i.next();
0423         updateValueInConfig(activity, i.key(), i.value(), false);
0424     }
0425     m_config.sync();
0426 }
0427 
0428 QVariantMap ApplicationSettings::loadActivityConfiguration(const QString &activity)
0429 {
0430     qDebug() << "load configuration for:" << activity;
0431     m_config.beginGroup(activity);
0432     const QStringList keys = m_config.childKeys();
0433     QVariantMap data;
0434     for (const QString &key: keys) {
0435         data[key] = m_config.value(key);
0436     }
0437     m_config.endGroup();
0438     return data;
0439 }
0440 
0441 void ApplicationSettings::setFavorite(const QString &activity, bool favorite)
0442 {
0443     updateValueInConfig(FAVORITE_GROUP_KEY, activity, favorite);
0444 }
0445 
0446 bool ApplicationSettings::isFavorite(const QString &activity)
0447 {
0448     m_config.beginGroup(FAVORITE_GROUP_KEY);
0449     bool favorite = m_config.value(activity, false).toBool();
0450     m_config.endGroup();
0451     return favorite;
0452 }
0453 
0454 void ApplicationSettings::setCurrentLevels(const QString &activity, const QStringList &level, bool sync)
0455 {
0456     if (!m_filterLevelOverridedByCommandLineOption) {
0457         updateValueInConfig(LEVELS_GROUP_KEY, activity, level, sync);
0458     }
0459 }
0460 
0461 QStringList ApplicationSettings::currentLevels(const QString &activity)
0462 {
0463     m_config.beginGroup(LEVELS_GROUP_KEY);
0464     QStringList level = m_config.value(activity, QStringList()).toStringList();
0465     m_config.endGroup();
0466     return level;
0467 }
0468 
0469 template <class T>
0470 void ApplicationSettings::updateValueInConfig(const QString &group,
0471                                               const QString &key, const T &value, bool sync)
0472 {
0473     m_config.beginGroup(group);
0474     m_config.setValue(key, value);
0475     m_config.endGroup();
0476     if (sync) {
0477         m_config.sync();
0478     }
0479 }
0480 
0481 void ApplicationSettings::sync()
0482 {
0483     m_config.sync();
0484 }
0485 
0486 int ApplicationSettings::loadActivityProgress(const QString &activity)
0487 {
0488     int progress = 0;
0489     m_config.beginGroup(activity);
0490     progress = m_config.value(PROGRESS_KEY, 0).toInt();
0491     m_config.endGroup();
0492     qDebug() << "loaded progress for activity" << activity << ":" << progress;
0493     return progress;
0494 }
0495 
0496 void ApplicationSettings::saveActivityProgress(const QString &activity, int progress)
0497 {
0498     updateValueInConfig(activity, PROGRESS_KEY, progress);
0499 }
0500 
0501 bool ApplicationSettings::useExternalWordset()
0502 {
0503     return DownloadManager::getInstance()->isDataRegistered("words-webp");
0504 }
0505 
0506 QObject *ApplicationSettings::applicationSettingsProvider(QQmlEngine *engine,
0507                                                           QJSEngine *scriptEngine)
0508 {
0509     Q_UNUSED(engine)
0510     Q_UNUSED(scriptEngine)
0511 
0512     ApplicationSettings *appSettings = getInstance();
0513     return appSettings;
0514 }
0515 
0516 #include "moc_ApplicationSettings.cpp"