LCOV - code coverage report
Current view: top level - src/models - FangSettings.cpp (source / functions) Coverage Total Hit
Test: coverage.info.cleaned Lines: 71.6 % 141 101
Test Date: 2026-03-23 10:19:47 Functions: 84.0 % 25 21

            Line data    Source code
       1              : #include "FangSettings.h"
       2              : #include "../utilities/ErrorHandling.h"
       3              : #include <QAutoStart.h>
       4              : #include <QCoreApplication>
       5              : #include <QDebug>
       6              : #include <QGuiApplication>
       7              : #include <QPalette>
       8              : 
       9              : #ifdef Q_OS_WIN
      10              : #include <QSettings>
      11              : #endif
      12              : 
      13              : #ifdef Q_OS_LINUX
      14              : #include <QFile>
      15              : #include <QStandardPaths>
      16              : #include <QTextStream>
      17              : #endif
      18              : 
      19           37 : FangSettings::FangSettings(FangObject *parent) :
      20              :     FangObject(parent),
      21           37 :     dbSettings(nullptr),
      22           37 :     styleHints(nullptr)
      23              : {
      24           37 : }
      25              : 
      26           33 : void FangSettings::init(DBSettingsInterface *dbSettings)
      27              : {
      28              :     // Database
      29           33 :     this->dbSettings = dbSettings;
      30           33 :     connect(this->dbSettings, &DBSettingsInterface::settingChanged, this, &FangSettings::onDBSettingChanged);
      31              : 
      32              :     // System default color scheme.
      33           33 :     styleHints = QGuiApplication::styleHints();
      34           33 :     connect(styleHints, &QStyleHints::colorSchemeChanged, this, &FangSettings::onSystemColorSchemeChanged);
      35              : 
      36              :     // Manually apply the color scheme if not set to the system default.
      37           33 :     QString style = getStyle();
      38           33 :     if (style == "LIGHT") {
      39            0 :         styleHints->setColorScheme(Qt::ColorScheme::Light);
      40           33 :     } else if (style == "DARK") {
      41            0 :         styleHints->setColorScheme(Qt::ColorScheme::Dark);
      42              :     }
      43           33 : }
      44              : 
      45           70 : QString FangSettings::getStringSetting(const QString& name, const QString& defaultValue)
      46              : {
      47           70 :     QString ret = defaultValue;
      48          140 :     settings.beginGroup("FangSettings");
      49           70 :     if (settings.contains(name)) {
      50           16 :         ret = settings.value(name).toString();
      51              :     }
      52              :     
      53           70 :     settings.endGroup();
      54           70 :     return ret;
      55            0 : }
      56              : 
      57           15 : void FangSettings::setStringSetting(const QString& name, const QString& newValue)
      58              : {
      59           30 :     settings.beginGroup("FangSettings");
      60           15 :     settings.setValue(name, newValue);
      61           15 :     settings.endGroup();
      62           15 : }
      63              : 
      64            8 : bool FangSettings::getBoolSetting(const QString& name, bool defaultValue)
      65              : {
      66            8 :     bool ret = defaultValue;
      67           16 :     settings.beginGroup("FangSettings");
      68            8 :     if (settings.contains(name)) {
      69            1 :         ret = settings.value(name).toBool();
      70              :     }
      71              : 
      72            8 :     settings.endGroup();
      73            8 :     return ret;
      74              : }
      75              : 
      76            2 : void FangSettings::setBoolSetting(const QString& name, bool newValue)
      77              : {
      78            4 :     settings.beginGroup("FangSettings");
      79            2 :     settings.setValue(name, newValue);
      80            2 :     settings.endGroup();
      81            2 : }
      82              : 
      83            4 : void FangSettings::onDBSettingChanged(DBSettingsKey key, QString value)
      84              : {
      85            4 :     switch (key) {
      86            4 :     case CACHE_LENGTH:
      87            4 :         emit cacheLengthChanged(value);
      88            4 :         break;
      89              : 
      90            0 :     default:
      91            0 :         FANG_UNREACHABLE("Unknown settings key in FangSettings");
      92              :         break;
      93              :     }
      94            4 : }
      95              : 
      96            0 : bool FangSettings::event(QEvent *event)
      97              : {
      98            0 :     if (event->type() == QEvent::ApplicationPaletteChange) {
      99            0 :         if (getStyle() == "DEFAULT" &&
     100            0 :             QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Unknown) {
     101            0 :             emit currentStyleChanged(getCurrentStyle());
     102              :         }
     103              :     }
     104            0 :     return QObject::event(event);
     105              : }
     106              : 
     107            0 : void FangSettings::onSystemColorSchemeChanged(Qt::ColorScheme colorScheme)
     108              : {
     109            0 :     if (getStyle() == "DEFAULT") {
     110            0 :         emit currentStyleChanged(colorSchemeToString(colorScheme));
     111              :     }
     112            0 : }
     113              : 
     114            3 : QString FangSettings::colorSchemeToString(Qt::ColorScheme colorScheme)
     115              : {
     116            3 :     if (colorScheme == Qt::ColorScheme::Dark) {
     117            1 :         return "DARK";
     118              :     } else {
     119            2 :         return "LIGHT";
     120              :     }
     121              : }
     122              : 
     123           52 : QString FangSettings::getStyle()
     124              : {
     125           52 :     return getStringSetting("style", "DEFAULT");
     126              : }
     127              : 
     128            8 : void FangSettings::setStyle(QString s)
     129              : {
     130            8 :     if (s == getStyle()) {
     131            2 :         return; // Nothing to do!
     132              :     }
     133              : 
     134            6 :     setStringSetting("style", s);
     135              : 
     136              :     // Set the color scheme.
     137            6 :     if (s == "LIGHT") {
     138            2 :         QGuiApplication::styleHints()->setColorScheme(Qt::ColorScheme::Light);
     139            4 :     } else if (s == "DARK") {
     140            4 :         QGuiApplication::styleHints()->setColorScheme(Qt::ColorScheme::Dark);
     141              :     } else {
     142              :         // This clears the override and goes with the OS default.
     143            0 :         QGuiApplication::styleHints()->setColorScheme(Qt::ColorScheme::Unknown);
     144              :     }
     145              : 
     146            6 :     emit styleChanged(s);
     147            6 :     emit currentStyleChanged(getCurrentStyle());
     148              : }
     149              : 
     150            9 : QString FangSettings::getCurrentStyle()
     151              : {
     152            9 :     QString ret = getStyle();
     153            9 :     if (ret == "DEFAULT") {
     154            1 :         Qt::ColorScheme scheme = QGuiApplication::styleHints()->colorScheme();
     155            1 :         if (scheme != Qt::ColorScheme::Unknown) {
     156              :             // Convert Qt's color scheme to our string-based color scheme.
     157            0 :             ret = colorSchemeToString(scheme);
     158              :         } else {
     159              :             // Essentially the same as above, but for older versions of Linux desktops.
     160            1 :             QColor windowColor = QGuiApplication::palette().color(QPalette::Window);
     161            1 :             ret = (windowColor.lightnessF() < 0.5) ? "DARK" : "LIGHT";
     162              :         }
     163              :     }
     164              : 
     165            9 :     return ret;
     166            0 : }
     167              : 
     168            6 : QString FangSettings::getFontSize()
     169              : {
     170            6 :     return getStringSetting("fontSize", "MEDIUM");
     171              : }
     172              : 
     173            4 : void FangSettings::setFontSize(QString s)
     174              : {
     175            4 :     if (s == getFontSize()) {
     176            1 :         return; // Nothing to do!
     177              :     }
     178              :     
     179            3 :     setStringSetting("fontSize", s);
     180            3 :     emit fontSizeChanged(s);
     181              : }
     182              : 
     183            1 : QString FangSettings::getCacheLength()
     184              : {
     185            1 :     return dbSettings->get(CACHE_LENGTH);
     186              : }
     187              : 
     188            1 : void FangSettings::setCacheLength(QString s)
     189              : {
     190            1 :     dbSettings->set(CACHE_LENGTH, s);
     191            1 : }
     192              : 
     193            6 : QString FangSettings::getRefresh()
     194              : {
     195            6 :     return getStringSetting("refresh", "10MIN");
     196              : }
     197              : 
     198            4 : void FangSettings::setRefresh(QString s)
     199              : {
     200            4 :     if (s == getRefresh()) {
     201            1 :         return; // Nothing to do!
     202              :     }
     203              : 
     204            3 :     setStringSetting("refresh", s);
     205            3 :     emit refreshChanged(s);
     206              : }
     207              : 
     208            6 : QString FangSettings::getLastSeenVersion()
     209              : {
     210            6 :     return getStringSetting("lastSeenVersion", "");
     211              : }
     212              : 
     213            4 : void FangSettings::setLastSeenVersion(QString s)
     214              : {
     215            4 :     if (s == getLastSeenVersion()) {
     216            1 :         return; // Nothing to do!
     217              :     }
     218              : 
     219            3 :     setStringSetting("lastSeenVersion", s);
     220            3 :     emit lastSeenVersionChanged(s);
     221              : }
     222              : 
     223            8 : bool FangSettings::getShowTrayIcon()
     224              : {
     225            8 :     bool defaultValue = false;
     226            8 :     return getBoolSetting("showTrayIcon", defaultValue);
     227              : }
     228              : 
     229            3 : void FangSettings::setShowTrayIcon(bool b)
     230              : {
     231            3 :     if (b == getShowTrayIcon()) {
     232            1 :         return; // Nothing to do!
     233              :     }
     234              : 
     235            2 :     setBoolSetting("showTrayIcon", b);
     236            2 :     emit showTrayIconChanged(b);
     237              : }
     238              : 
     239            0 : bool FangSettings::getStartAtLogin()
     240              : {
     241            0 :     return QAutoStart::Get().isEnabled();
     242              : }
     243              : 
     244            0 : void FangSettings::setStartAtLogin(bool b)
     245              : {
     246            0 :     if (b == getStartAtLogin()) {
     247            0 :         return; // Nothing to do!
     248              :     }
     249              : 
     250            0 :     QAutoStart::Get().setEnabled(b);
     251              : 
     252              :     // Append --minimized so the app starts hidden in the system tray.
     253            0 :     if (b) {
     254              : #ifdef Q_OS_WIN
     255              :         QSettings reg("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",
     256              :                        QSettings::NativeFormat);
     257              :         QString appPath = QCoreApplication::applicationFilePath();
     258              :         appPath.replace('/', '\\');
     259              :         reg.setValue(QCoreApplication::applicationName(),
     260              :                      "\"" + appPath + "\" --minimized");
     261              :         reg.sync();
     262              : #endif
     263              : 
     264              : #ifdef Q_OS_LINUX
     265              :         QString configDir = QStandardPaths::writableLocation(
     266            0 :             QStandardPaths::GenericConfigLocation);
     267            0 :         QString desktopPath = configDir + "/autostart/"
     268            0 :             + QCoreApplication::organizationDomain() + "."
     269            0 :             + QCoreApplication::applicationName() + ".desktop";
     270              : 
     271            0 :         QFile file(desktopPath);
     272            0 :         if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
     273            0 :             QString content = QTextStream(&file).readAll();
     274            0 :             content.replace(
     275            0 :                 "Exec=" + QCoreApplication::applicationFilePath(),
     276            0 :                 "Exec=" + QCoreApplication::applicationFilePath() + " --minimized");
     277            0 :             file.resize(0);
     278            0 :             QTextStream(&file) << content;
     279            0 :         }
     280              : #endif
     281            0 :     }
     282              : 
     283            0 :     emit startAtLoginChanged(b);
     284              : }
        

Generated by: LCOV version 2.0-1