File indexing completed on 2024-05-19 04:59:16

0001 /* ============================================================
0002 * GreaseMonkey plugin for Falkon
0003 * Copyright (C) 2013-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 "gm_jsobject.h"
0019 
0020 #include "qzcommon.h"
0021 
0022 #include <QApplication>
0023 #include <QClipboard>
0024 
0025 GM_JSObject::GM_JSObject(QObject* parent)
0026     : QObject(parent)
0027     , m_settings(nullptr)
0028 {
0029 }
0030 
0031 void GM_JSObject::setSettingsFile(const QString &name)
0032 {
0033     if (m_settings) {
0034         m_settings->sync();
0035         delete m_settings;
0036     }
0037 
0038     m_settings = new QSettings(name, QSettings::IniFormat);
0039 }
0040 
0041 QString GM_JSObject::getValue(const QString &nspace, const QString &name, const QString &dValue)
0042 {
0043     QString valueName = QSL("GreaseMonkey-%1/%2").arg(nspace, name);
0044     QString savedValue = m_settings->value(valueName, dValue).toString();
0045 
0046     if (savedValue.isEmpty()) {
0047         return dValue;
0048     }
0049 
0050     return savedValue;
0051 }
0052 
0053 bool GM_JSObject::setValue(const QString &nspace, const QString &name, const QString &value)
0054 {
0055     QString valueName = QSL("GreaseMonkey-%1/%2").arg(nspace, name);
0056     m_settings->setValue(valueName, value);
0057     return true;
0058 }
0059 
0060 bool GM_JSObject::deleteValue(const QString &nspace, const QString &name)
0061 {
0062     QString valueName = QSL("GreaseMonkey-%1/%2").arg(nspace, name);
0063     m_settings->remove(valueName);
0064     return true;
0065 }
0066 
0067 QStringList GM_JSObject::listValues(const QString &nspace)
0068 {
0069     QString nspaceName = QSL("GreaseMonkey-%1").arg(nspace);
0070 
0071     m_settings->beginGroup(nspaceName);
0072     QStringList keys = m_settings->allKeys();
0073     m_settings->endGroup();
0074 
0075     return keys;
0076 }
0077 
0078 void GM_JSObject::setClipboard(const QString &text)
0079 {
0080     QApplication::clipboard()->setText(text);
0081 }
0082 
0083 GM_JSObject::~GM_JSObject()
0084 {
0085     if (m_settings) {
0086         m_settings->sync();
0087         delete m_settings;
0088     }
0089 }