blob: 86766f4581255bd6afe87c744153b1e297a02934 [file] [log] [blame]
Andreas Traczyk84dec082020-09-01 14:31:31 -04001/*!
2 * Copyright (C) 2020 by Savoir-faire Linux
3 * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
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 * \file appsettingsmanager.h
19 */
20
21#pragma once
22
23#include "utils.h"
24
25#include <QMetaEnum>
26#include <QObject>
27#include <QString>
28#include <QStandardPaths>
29
30const QString defaultDownloadPath =
31 QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
32
33#define KEYS \
34 X(MinimizeOnClose, true) \
35 X(DownloadPath, defaultDownloadPath) \
36 X(EnableNotifications, true) \
37 X(AutoUpdate, true) \
38 X(NeverShowMeAgain, false)
39
40/*
41 * A class to expose settings keys in both c++ and QML.
42 * Note: this using a non-constructable class instead of a
43 * namespace allows for QML enum auto-completion in QtCreator.
44 * This works well when there is only one enum class. Otherwise,
45 * to prevent element name collision when defining multiple enums,
46 * use a namespace with:
47 *
48 * Q_NAMESPACE
49 * Q_CLASSINFO("RegisterEnumClassesUnscoped", "false")
50 */
51class Settings : public QObject
52{
53 Q_OBJECT
54public:
55 enum class Key {
56#define X(key, defaultValue) key,
57 KEYS
58#undef X
59 COUNT__
60 };
61 Q_ENUM(Key)
62 static QString toString(Key key)
63 {
64 return QMetaEnum::fromType<Key>().valueToKey(
65 Utils::toUnderlyingValue(key));
66 }
67 static QVariant defaultValue(const Key key)
68 {
69 switch (key) {
70#define X(key, defaultValue) \
71 case Key::key: return defaultValue;
72 KEYS
73#undef X
74 default: return {};
75 }
76 }
77private:
78 Settings() = delete;
79};
80Q_DECLARE_METATYPE(Settings::Key)
81
82/*
83 * A singleton object to manage settings access.
84 */
85class AppSettingsManager : public QObject
86{
87 Q_OBJECT
88public:
89 virtual ~AppSettingsManager() = default;
90
91 static AppSettingsManager&
92 instance()
93 {
94 static AppSettingsManager *instance_ =
95 new AppSettingsManager(nullptr);
96 return *instance_;
97 }
98
99 static QVariant
100 getValue(const Settings::Key key)
101 {
102 auto settings = instance().settings_;
103 auto value = settings->value(Settings::toString(key),
104 Settings::defaultValue(key));
105
106 if (QString(value.typeName()) == "QString" &&
107 (value.toString() == "false" || value.toString() == "true"))
108 return value.toBool();
109
110 return value;
111 }
112
113 static void
114 setValue(const Settings::Key key, const QVariant& value)
115 {
116 instance().settings_->setValue(Settings::toString(key), value);
117 }
118
119 static void
120 initValues()
121 {
122 for (int i = 0;
123 i < Utils::toUnderlyingValue(Settings::Key::COUNT__);
124 ++i) {
125 auto key = Utils::toEnum<Settings::Key>(i);
126 if (!instance().settings_->contains(Settings::toString(key)))
127 setValue(key, Settings::defaultValue(key));
128 }
129 }
130
131private:
132 explicit AppSettingsManager(QObject *)
133 : settings_(new QSettings("jami.net", "Jami", this)) {}
134
135 QSettings *settings_;
136};