File indexing completed on 2024-12-22 04:41:14

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qmlsettings.h"
0019 #include "datapaths.h"
0020 #include <QDebug>
0021 
0022 QmlSettings::QmlSettings(QObject *parent)
0023     : QObject(parent)
0024 {
0025     m_settingsPath = DataPaths::currentProfilePath() + QL1S("/extensions");
0026 }
0027 
0028 bool QmlSettings::setValue(const QVariantMap &map)
0029 {
0030     if (!m_settings) {
0031         return false;
0032     }
0033 
0034     if (!map.contains(QSL("key")) || !map.contains(QSL("value"))) {
0035         qWarning() << "Unable to set value:" << "cannot determine Key-Value from the argument";
0036         return false;
0037     }
0038     const QString key = map.value(QSL("key")).toString();
0039     const QVariant value = map.value(QSL("value"));
0040     m_settings->setValue(key, value);
0041     return true;
0042 }
0043 
0044 QVariant QmlSettings::value(const QVariantMap &map)
0045 {
0046     if (!m_settings) {
0047         return {};
0048     }
0049 
0050     if (!map.contains(QSL("key"))) {
0051         qWarning() << "Unable to get value:" << "key not defined";
0052         return {};
0053     }
0054 
0055     const QString key = map.value(QSL("key")).toString();
0056     const QVariant defaultValue = map.value(QSL("defaultValue"));
0057     return m_settings->value(key, defaultValue);
0058 }
0059 
0060 bool QmlSettings::contains(const QString &key)
0061 {
0062     if (!m_settings) {
0063         return false;
0064     }
0065 
0066     return m_settings->contains(key);
0067 }
0068 
0069 bool QmlSettings::remove(const QString &key)
0070 {
0071     if (!m_settings) {
0072         return false;
0073     }
0074 
0075     m_settings->remove(key);
0076     return true;
0077 }
0078 
0079 bool QmlSettings::sync()
0080 {
0081     if (!m_settings) {
0082         return false;
0083     }
0084 
0085     m_settings->sync();
0086     return true;
0087 }
0088 
0089 QString QmlSettings::name() const
0090 {
0091     return m_name;
0092 }
0093 
0094 void QmlSettings::setName(const QString &name)
0095 {
0096     m_name = name;
0097     createSettings();
0098 }
0099 
0100 void QmlSettings::createSettings()
0101 {
0102     m_settingsPath += QL1C('/') + m_name + QL1S("/settings.ini");
0103     m_settings = new QSettings(m_settingsPath, QSettings::IniFormat, this);
0104 }