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 #pragma once
0019 
0020 #include <QObject>
0021 #include <QSettings>
0022 
0023 /**
0024  * @brief The class exposing Settings API to QML
0025  */
0026 class QmlSettings : public QObject
0027 {
0028     Q_OBJECT
0029 
0030     /**
0031      * @brief name of the folder in which settings.ini file is located
0032      *        on the standard extension path.
0033      */
0034     Q_PROPERTY(QString name READ name WRITE setName)
0035 
0036 public:
0037     explicit QmlSettings(QObject *parent = nullptr);
0038     /**
0039      * @brief Sets the value for a given key.
0040      * @param A JavaScript object containing
0041      *        - key: QString representing the key
0042      *        - value: QVariant representing the value for the key
0043      * @return true if value is set, else false
0044      */
0045     Q_INVOKABLE bool setValue(const QVariantMap &map);
0046     /**
0047      * @brief Gets the value for a given key.
0048      * @param A JavaScript object containing
0049      *        - key: QString representing the key
0050      *        - defaultValue: QVariant representing the default value for the key
0051      * @return QVariant representing value
0052      */
0053     Q_INVOKABLE QVariant value(const QVariantMap &map);
0054     /**
0055      * @brief Checks if a given key exists.
0056      * @param QString representing the key
0057      * @return true if key exists, else false
0058      */
0059     Q_INVOKABLE bool contains(const QString &key);
0060     /**
0061      * @brief Removes the given key-value from the settings.
0062      * @param QString representing the key
0063      * @return true if key-value pair is removed, else false
0064      */
0065     Q_INVOKABLE bool remove(const QString &key);
0066     /**
0067      * @brief syncs the settings
0068      * @return true if success, else false
0069      */
0070     Q_INVOKABLE bool sync();
0071 
0072 private:
0073     QSettings *m_settings = nullptr;
0074     QString m_settingsPath;
0075     QString m_name;
0076 
0077     QString name() const;
0078     void setName(const QString &name);
0079     void createSettings();
0080 };