Falkon Develop
Cross-platform Qt-based web browser
qmlsettings.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.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 <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmlsettings.h"
19#include "datapaths.h"
20#include <QDebug>
21
23 : QObject(parent)
24{
25 m_settingsPath = DataPaths::currentProfilePath() + QL1S("/extensions");
26}
27
28bool QmlSettings::setValue(const QVariantMap &map)
29{
30 if (!m_settings) {
31 return false;
32 }
33
34 if (!map.contains(QSL("key")) || !map.contains(QSL("value"))) {
35 qWarning() << "Unable to set value:" << "cannot determine Key-Value from the argument";
36 return false;
37 }
38 const QString key = map.value(QSL("key")).toString();
39 const QVariant value = map.value(QSL("value"));
40 m_settings->setValue(key, value);
41 return true;
42}
43
44QVariant QmlSettings::value(const QVariantMap &map)
45{
46 if (!m_settings) {
47 return {};
48 }
49
50 if (!map.contains(QSL("key"))) {
51 qWarning() << "Unable to get value:" << "key not defined";
52 return {};
53 }
54
55 const QString key = map.value(QSL("key")).toString();
56 const QVariant defaultValue = map.value(QSL("defaultValue"));
57 return m_settings->value(key, defaultValue);
58}
59
60bool QmlSettings::contains(const QString &key)
61{
62 if (!m_settings) {
63 return false;
64 }
65
66 return m_settings->contains(key);
67}
68
69bool QmlSettings::remove(const QString &key)
70{
71 if (!m_settings) {
72 return false;
73 }
74
75 m_settings->remove(key);
76 return true;
77}
78
80{
81 if (!m_settings) {
82 return false;
83 }
84
85 m_settings->sync();
86 return true;
87}
88
89QString QmlSettings::name() const
90{
91 return m_name;
92}
93
94void QmlSettings::setName(const QString &name)
95{
96 m_name = name;
97 createSettings();
98}
99
100void QmlSettings::createSettings()
101{
102 m_settingsPath += QL1C('/') + m_name + QL1S("/settings.ini");
103 m_settings = new QSettings(m_settingsPath, QSettings::IniFormat, this);
104}
static QString currentProfilePath()
Definition: datapaths.cpp:95
Q_INVOKABLE bool contains(const QString &key)
Checks if a given key exists.
Definition: qmlsettings.cpp:60
QString name
name of the folder in which settings.ini file is located on the standard extension path.
Definition: qmlsettings.h:34
Q_INVOKABLE bool setValue(const QVariantMap &map)
Sets the value for a given key.
Definition: qmlsettings.cpp:28
Q_INVOKABLE bool sync()
syncs the settings
Definition: qmlsettings.cpp:79
QmlSettings(QObject *parent=nullptr)
Definition: qmlsettings.cpp:22
Q_INVOKABLE QVariant value(const QVariantMap &map)
Gets the value for a given key.
Definition: qmlsettings.cpp:44
Q_INVOKABLE bool remove(const QString &key)
Removes the given key-value from the settings.
Definition: qmlsettings.cpp:69
#define QL1S(x)
Definition: qzcommon.h:44
#define QL1C(x)
Definition: qzcommon.h:48
#define QSL(x)
Definition: qzcommon.h:40