2008-08-14 20:23:27 -04:00
|
|
|
/****************************************************************
|
2010-01-27 17:31:25 -05:00
|
|
|
* This file is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008, crypton
|
2008-08-14 20:23:27 -04:00
|
|
|
* Copyright (c) 2008, Matt Edman, Justin Hipple
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#include <rshare.h>
|
2010-06-14 09:32:13 -04:00
|
|
|
#include <iostream>
|
2008-08-14 20:23:27 -04:00
|
|
|
|
|
|
|
#include "rsettings.h"
|
2010-08-06 05:40:23 -04:00
|
|
|
#include <retroshare/rsinit.h>
|
2008-08-14 20:23:27 -04:00
|
|
|
|
|
|
|
/** The file in which all settings will read and written. */
|
2014-01-17 21:35:06 -05:00
|
|
|
#define SETTINGS_FILE (QString::fromUtf8(RsAccounts::AccountDirectory().c_str()) + "/RetroShare.conf")
|
2008-08-14 20:23:27 -04:00
|
|
|
|
|
|
|
/** Constructor */
|
|
|
|
RSettings::RSettings(const QString settingsGroup)
|
2011-10-12 19:36:25 -04:00
|
|
|
: QSettings(SETTINGS_FILE, QSettings::IniFormat)
|
2010-01-27 17:31:25 -05:00
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
RsPeerId sPreferedId;
|
2014-01-17 21:35:06 -05:00
|
|
|
m_bValid = RsAccounts::GetPreferredAccountId(sPreferedId);
|
2010-06-14 09:32:13 -04:00
|
|
|
|
|
|
|
if (!settingsGroup.isEmpty())
|
|
|
|
beginGroup(settingsGroup);
|
2010-01-27 17:31:25 -05:00
|
|
|
}
|
|
|
|
|
2017-08-13 14:44:34 -04:00
|
|
|
RSettings::RSettings(const std::string &fileName, const QString settingsGroup)
|
2010-01-27 17:31:25 -05:00
|
|
|
: QSettings(QString::fromStdString(fileName), QSettings::IniFormat)
|
2008-08-14 20:23:27 -04:00
|
|
|
{
|
2010-06-14 09:32:13 -04:00
|
|
|
m_bValid = true;
|
2008-08-14 20:23:27 -04:00
|
|
|
if (!settingsGroup.isEmpty())
|
|
|
|
beginGroup(settingsGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the saved value associated with <b>key</b>. If no value has been
|
|
|
|
* set, the default value is returned.
|
|
|
|
* \sa setDefault
|
|
|
|
*/
|
|
|
|
QVariant
|
|
|
|
RSettings::value(const QString &key, const QVariant &defaultVal) const
|
|
|
|
{
|
2010-06-14 09:32:13 -04:00
|
|
|
if (m_bValid == false) {
|
|
|
|
// return only default value
|
|
|
|
return defaultVal.isNull() ? defaultValue(key) : defaultVal;
|
|
|
|
}
|
|
|
|
return QSettings::value(key, defaultVal.isNull() ? defaultValue(key) : defaultVal);
|
2008-08-14 20:23:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets the value associated with <b>key</b> to <b>val</b>. */
|
|
|
|
void
|
|
|
|
RSettings::setValue(const QString &key, const QVariant &val)
|
|
|
|
{
|
2010-06-14 09:32:13 -04:00
|
|
|
if (m_bValid == false) {
|
|
|
|
std::cerr << "RSettings::setValue() Calling on invalid object, key = " << key.toStdString() << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (val == defaultValue(key))
|
|
|
|
QSettings::remove(key);
|
|
|
|
else if (val != value(key))
|
|
|
|
QSettings::setValue(key, val);
|
2008-08-14 20:23:27 -04:00
|
|
|
}
|
|
|
|
|
2010-05-20 17:53:27 -04:00
|
|
|
QVariant RSettings::valueFromGroup(const QString &group, const QString &key, const QVariant &defaultVal)
|
|
|
|
{
|
|
|
|
beginGroup(group);
|
|
|
|
QVariant val = value(key, defaultVal);
|
|
|
|
endGroup();
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSettings::setValueToGroup(const QString &group, const QString &key, const QVariant &val)
|
|
|
|
{
|
|
|
|
beginGroup(group);
|
|
|
|
setValue(key, val);
|
|
|
|
endGroup();
|
|
|
|
}
|
|
|
|
|
2008-08-14 20:23:27 -04:00
|
|
|
/** Sets the default setting for <b>key</b> to <b>val</b>. */
|
|
|
|
void
|
|
|
|
RSettings::setDefault(const QString &key, const QVariant &val)
|
|
|
|
{
|
|
|
|
_defaults.insert(key, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the default setting value associated with <b>key</b>. If
|
|
|
|
* <b>key</b> has no default value, then an empty QVariant is returned. */
|
|
|
|
QVariant
|
|
|
|
RSettings::defaultValue(const QString &key) const
|
|
|
|
{
|
|
|
|
if (_defaults.contains(key))
|
|
|
|
return _defaults.value(key);
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2010-05-18 14:02:51 -04:00
|
|
|
/** Resets all settings. */
|
2008-08-14 20:23:27 -04:00
|
|
|
void
|
|
|
|
RSettings::reset()
|
|
|
|
{
|
|
|
|
/* Static method, so we have to create a QSettings object. */
|
2011-10-12 19:36:25 -04:00
|
|
|
QSettings settings(SETTINGS_FILE, QSettings::IniFormat);
|
2008-08-14 20:23:27 -04:00
|
|
|
settings.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns a map of all currently saved settings at the last appyl() point. */
|
|
|
|
/*QMap<QString, QVariant>
|
|
|
|
RSettings::allSettings() const
|
|
|
|
{
|
|
|
|
QMap<QString, QVariant> settings;
|
|
|
|
foreach (QString key, allKeys()) {
|
|
|
|
settings.insert(key, value(key));
|
|
|
|
}
|
|
|
|
return settings;
|
|
|
|
}*/
|
|
|
|
|