sailfish-safe

Sailfish frontend for safe(1)
git clone git://git.z3bra.org/sailfish-safe.git
Log | Files | Refs | README | LICENSE

settings.cpp (2638B)


      1 /*
      2  *   Copyright (C) 2019  Daniel Vrátil <dvratil@kde.org>
      3  *                 2021  Willy Goiffon <contact@z3bra.org>
      4  *
      5  *   This program is free software: you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation, either version 3 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  *   GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17  */
     18 
     19 #include "settings.h"
     20 
     21 #include <QStandardPaths>
     22 #include <QDir>
     23 
     24 std::unique_ptr<Settings> Settings::kInstance = {};
     25 
     26 static const QString OldSettingsDir = QStringLiteral("Willy Goiffon");
     27 static const QString SettingsDir = QStringLiteral("harbour-safe");
     28 static const QString SettingsName = QStringLiteral("safe");
     29 
     30 Settings::Settings()
     31     : QObject()
     32 {
     33     // Migrate config directory to new location
     34     const auto oldPath = QStandardPaths::locate(QStandardPaths::ConfigLocation,
     35                                                 OldSettingsDir,
     36                                                 QStandardPaths::LocateDirectory);
     37     const QDir newDir(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
     38                                                 + QLatin1Char('/') + SettingsDir);
     39     if (!oldPath.isEmpty() && !newDir.exists()) {
     40         qDebug("Migrating Safe configuration from %s to %s",
     41                qUtf8Printable(oldPath),
     42                qUtf8Printable(newDir.absolutePath()));
     43         QDir().rename(oldPath, newDir.absolutePath());
     44     }
     45 
     46     mSettings.reset(new QSettings(SettingsDir, SettingsName));
     47 }
     48 
     49 Settings *Settings::self()
     50 {
     51     if (!kInstance) {
     52         kInstance.reset(new Settings);
     53     }
     54     return kInstance.get();
     55 }
     56 
     57 void Settings::destroy()
     58 {
     59     kInstance.reset();
     60 }
     61 
     62 void Settings::save()
     63 {
     64     mSettings->sync();
     65 }
     66 
     67 #define IMPLEMENT_OPTION(type, lc, uc, name, defValue) \
     68     void Settings::set##uc(type val) { \
     69         if (lc() != val) { \
     70             mSettings->setValue(QStringLiteral(name), val); \
     71             Q_EMIT lc##Changed(); \
     72         } \
     73     } \
     74     type Settings::lc() const { \
     75         return mSettings->value(QStringLiteral(name), defValue).value<type>(); \
     76     }
     77 
     78 IMPLEMENT_OPTION(int, expirationTimeout, ExpirationTimeout, "expirationTimeout", 45)
     79 
     80 
     81 #undef IMPLEMENT_OPTION