File indexing completed on 2024-05-12 04:57:54

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@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 "settings.h"
0019 #include "qzsettings.h"
0020 
0021 #include <QSettings>
0022 
0023 QSettings* Settings::s_settings = nullptr;
0024 QzSettings* Settings::s_qzSettings = nullptr;
0025 
0026 Settings::Settings()
0027 {
0028     // Save currently opened group
0029     if (!s_settings->group().isEmpty()) {
0030         m_openedGroup = s_settings->group();
0031         s_settings->endGroup();
0032     }
0033 }
0034 
0035 void Settings::createSettings(const QString &fileName)
0036 {
0037     s_settings = new QSettings(fileName, QSettings::IniFormat);
0038     s_qzSettings = new QzSettings();
0039 }
0040 
0041 void Settings::syncSettings()
0042 {
0043     if (!s_settings)
0044         return;
0045 
0046     s_settings->sync();
0047 }
0048 
0049 QStringList Settings::childKeys() const
0050 {
0051     return s_settings->childKeys();
0052 }
0053 
0054 QStringList Settings::childGroups() const
0055 {
0056     return s_settings->childGroups();
0057 }
0058 
0059 bool Settings::contains(const QString &key) const
0060 {
0061     return s_settings->contains(key);
0062 }
0063 
0064 void Settings::remove(const QString &key)
0065 {
0066     s_settings->remove(key);
0067 }
0068 
0069 void Settings::setValue(const QString &key, const QVariant &defaultValue)
0070 {
0071     s_settings->setValue(key, defaultValue);
0072 }
0073 
0074 QVariant Settings::value(const QString &key, const QVariant &defaultValue)
0075 {
0076     return s_settings->value(key, defaultValue);
0077 }
0078 
0079 void Settings::beginGroup(const QString &prefix)
0080 {
0081     s_settings->beginGroup(prefix);
0082 }
0083 
0084 void Settings::endGroup()
0085 {
0086     s_settings->endGroup();
0087 }
0088 
0089 void Settings::sync()
0090 {
0091     s_settings->sync();
0092 }
0093 
0094 QSettings* Settings::globalSettings()
0095 {
0096     return s_settings;
0097 }
0098 
0099 QzSettings* Settings::staticSettings()
0100 {
0101     return s_qzSettings;
0102 }
0103 
0104 Settings::~Settings()
0105 {
0106     if (!s_settings->group().isEmpty()) {
0107         qDebug() << "Settings: Deleting object with opened group!";
0108         s_settings->endGroup();
0109     }
0110 
0111     // Restore opened group
0112     if (!m_openedGroup.isEmpty())
0113         s_settings->beginGroup(m_openedGroup);
0114 }