summaryrefslogtreecommitdiffstats
path: root/depends/settings/src
diff options
context:
space:
mode:
Diffstat (limited to 'depends/settings/src')
-rw-r--r--depends/settings/src/basicsettingsobject.cpp46
-rw-r--r--depends/settings/src/inifile.cpp86
-rw-r--r--depends/settings/src/inisettingsobject.cpp62
-rw-r--r--depends/settings/src/keyring.cpp63
-rw-r--r--depends/settings/src/overridesetting.cpp30
-rw-r--r--depends/settings/src/setting.cpp54
-rw-r--r--depends/settings/src/settingsobject.cpp144
-rw-r--r--depends/settings/src/stubkeyring.cpp104
-rw-r--r--depends/settings/src/stubkeyring.h41
9 files changed, 630 insertions, 0 deletions
diff --git a/depends/settings/src/basicsettingsobject.cpp b/depends/settings/src/basicsettingsobject.cpp
new file mode 100644
index 00000000..484928c8
--- /dev/null
+++ b/depends/settings/src/basicsettingsobject.cpp
@@ -0,0 +1,46 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/basicsettingsobject.h"
+#include "include/setting.h"
+
+BasicSettingsObject::BasicSettingsObject(QObject *parent) :
+ SettingsObject(parent)
+{
+
+}
+
+void BasicSettingsObject::changeSetting(const Setting &setting, QVariant value)
+{
+ if (contains(setting.id()))
+ {
+ if(value.isValid())
+ config.setValue(setting.configKey(), value);
+ else
+ config.remove(setting.configKey());
+ }
+}
+
+QVariant BasicSettingsObject::retrieveValue(const Setting &setting)
+{
+ if (contains(setting.id()))
+ {
+ return config.value(setting.configKey());
+ }
+ else
+ {
+ return QVariant();
+ }
+}
diff --git a/depends/settings/src/inifile.cpp b/depends/settings/src/inifile.cpp
new file mode 100644
index 00000000..43545a4a
--- /dev/null
+++ b/depends/settings/src/inifile.cpp
@@ -0,0 +1,86 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/inifile.h"
+
+#include <QFile>
+#include <QTextStream>
+#include <QStringList>
+
+INIFile::INIFile()
+{
+
+}
+
+bool INIFile::saveFile(QString fileName)
+{
+ // TODO Handle errors.
+ QFile file(fileName);
+ file.open(QIODevice::WriteOnly);
+ QTextStream out(&file);
+
+ for (Iterator iter = begin(); iter != end(); iter++)
+ {
+ out << iter.key() << "=" << iter.value().toString() << "\n";
+ }
+
+ return true;
+}
+
+bool INIFile::loadFile(QString fileName)
+{
+ // TODO Handle errors.
+ QFile file(fileName);
+ file.open(QIODevice::ReadOnly);
+ QTextStream in(&file);
+
+ QStringList lines = in.readAll().split('\n');
+ for (int i = 0; i < lines.count(); i++)
+ {
+ QString & lineRaw = lines[i];
+ // Ignore comments.
+ QString line = lineRaw.left(lineRaw.indexOf('#')).trimmed();
+
+ int eqPos = line.indexOf('=');
+ if(eqPos == -1)
+ continue;
+ QString key = line.left(eqPos).trimmed();
+ QString valueStr = line.right(line.length() - eqPos - 1).trimmed();
+
+ QVariant value(valueStr);
+ /*
+ QString dbg = key;
+ dbg += " = ";
+ dbg += valueStr;
+ qDebug(dbg.toLocal8Bit());
+ */
+ this->operator [](key) = value;
+ }
+
+ return true;
+}
+
+QVariant INIFile::get(QString key, QVariant def) const
+{
+ if (!this->contains(key))
+ return def;
+ else
+ return this->operator [](key);
+}
+
+void INIFile::set(QString key, QVariant val)
+{
+ this->operator [](key) = val;
+}
diff --git a/depends/settings/src/inisettingsobject.cpp b/depends/settings/src/inisettingsobject.cpp
new file mode 100644
index 00000000..854421b6
--- /dev/null
+++ b/depends/settings/src/inisettingsobject.cpp
@@ -0,0 +1,62 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/inisettingsobject.h"
+#include "include/setting.h"
+
+INISettingsObject::INISettingsObject(const QString &path, QObject *parent) :
+ SettingsObject(parent)
+{
+ m_filePath = path;
+ m_ini.loadFile(path);
+}
+
+void INISettingsObject::setFilePath(const QString &filePath)
+{
+ m_filePath = filePath;
+}
+
+void INISettingsObject::changeSetting(const Setting &setting, QVariant value)
+{
+ if (contains(setting.id()))
+ {
+ if(value.isValid())
+ m_ini.set(setting.configKey(), value);
+ else
+ m_ini.remove(setting.configKey());
+ m_ini.saveFile(m_filePath);
+ }
+}
+
+void INISettingsObject::resetSetting ( const Setting& setting )
+{
+ if (contains(setting.id()))
+ {
+ m_ini.remove(setting.configKey());
+ m_ini.saveFile(m_filePath);
+ }
+}
+
+QVariant INISettingsObject::retrieveValue(const Setting &setting)
+{
+ if (contains(setting.id()))
+ {
+ return m_ini.get(setting.configKey(), QVariant());
+ }
+ else
+ {
+ return QVariant();
+ }
+}
diff --git a/depends/settings/src/keyring.cpp b/depends/settings/src/keyring.cpp
new file mode 100644
index 00000000..9eaba684
--- /dev/null
+++ b/depends/settings/src/keyring.cpp
@@ -0,0 +1,63 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Authors: Orochimarufan <orochimarufan.x3@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/keyring.h"
+
+#include "osutils.h"
+
+#include "stubkeyring.h"
+
+// system specific keyrings
+/*#if defined(OSX)
+class OSXKeychain;
+#define KEYRING OSXKeychain
+#elif defined(LINUX)
+class XDGKeyring;
+#define KEYRING XDGKeyring
+#elif defined(WINDOWS)
+class Win32Keystore;
+#define KEYRING Win32Keystore
+#else
+#pragma message Keyrings are not supported on your os. Falling back to the insecure StubKeyring
+#endif*/
+
+Keyring *Keyring::instance()
+{
+ if (m_instance == nullptr)
+ {
+#ifdef KEYRING
+ m_instance = new KEYRING();
+ if (!m_instance->isValid())
+ {
+ qWarning("Could not create SystemKeyring! falling back to StubKeyring.");
+ m_instance = new StubKeyring();
+ }
+#else
+ qWarning("Keyrings are not supported on your OS. Fallback StubKeyring is insecure!");
+ m_instance = new StubKeyring();
+#endif
+ atexit(Keyring::destroy);
+ }
+ return m_instance;
+}
+
+void Keyring::destroy()
+{
+ delete m_instance;
+}
+
+Keyring *Keyring::m_instance;
diff --git a/depends/settings/src/overridesetting.cpp b/depends/settings/src/overridesetting.cpp
new file mode 100644
index 00000000..eafb298f
--- /dev/null
+++ b/depends/settings/src/overridesetting.cpp
@@ -0,0 +1,30 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/overridesetting.h"
+
+OverrideSetting::OverrideSetting(const QString &name, Setting *other, QObject *parent) :
+ Setting(name, QVariant(), parent)
+{
+ m_other = other;
+}
+
+QVariant OverrideSetting::defValue() const
+{
+ if (m_other)
+ return m_other->get();
+ else
+ return QVariant();
+}
diff --git a/depends/settings/src/setting.cpp b/depends/settings/src/setting.cpp
new file mode 100644
index 00000000..8e60af06
--- /dev/null
+++ b/depends/settings/src/setting.cpp
@@ -0,0 +1,54 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/setting.h"
+#include "include/settingsobject.h"
+
+Setting::Setting(QString id, QVariant defVal, QObject *parent) :
+ QObject(parent), m_id(id), m_defVal(defVal)
+{
+
+}
+
+QVariant Setting::get() const
+{
+ SettingsObject *sbase = qobject_cast<SettingsObject *>(parent());
+ if (!sbase)
+ {
+ return defValue();
+ }
+ else
+ {
+ QVariant test = sbase->retrieveValue(*this);
+ if(!test.isValid())
+ return defValue();
+ return test;
+ }
+}
+
+QVariant Setting::defValue() const
+{
+ return m_defVal;
+}
+
+void Setting::set(QVariant value)
+{
+ emit settingChanged(*this, value);
+}
+
+void Setting::reset()
+{
+ emit settingReset(*this);
+}
diff --git a/depends/settings/src/settingsobject.cpp b/depends/settings/src/settingsobject.cpp
new file mode 100644
index 00000000..bf7b8825
--- /dev/null
+++ b/depends/settings/src/settingsobject.cpp
@@ -0,0 +1,144 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "include/settingsobject.h"
+#include "include/setting.h"
+
+#include <QVariant>
+
+SettingsObject *globalSettings;
+
+SettingsObject::SettingsObject(QObject *parent) :
+ QObject(parent)
+{
+
+}
+
+bool SettingsObject::registerSetting(Setting *setting)
+{
+ // Check if setting is null or we already have a setting with the same ID.
+ if (!setting)
+ {
+ qDebug(QString("Failed to register setting. Setting is null.").
+ arg(setting->id()).toUtf8());
+ return false; // Fail
+ }
+
+ if (contains(setting->id()))
+ {
+ qDebug(QString("Failed to register setting %1. ID already exists.").
+ arg(setting->id()).toUtf8());
+ return false; // Fail
+ }
+
+ m_settings.insert(setting->id(), setting);
+ setting->setParent(this); // Take ownership.
+
+ // Connect signals.
+ connectSignals(*setting);
+
+ // qDebug(QString("Registered setting %1.").arg(setting->id()).toUtf8());
+ return true;
+}
+
+void SettingsObject::unregisterSetting(Setting *setting)
+{
+ if (!setting || !m_settings.contains(setting->id()))
+ return; // We can't unregister something that's not registered.
+
+ m_settings.remove(setting->id());
+
+ // Disconnect signals.
+ disconnectSignals(*setting);
+
+ setting->setParent(NULL); // Drop ownership.
+}
+
+
+Setting *SettingsObject::getSetting(const QString &id) const
+{
+ // Make sure there is a setting with the given ID.
+ if (!m_settings.contains(id))
+ return NULL;
+
+ return m_settings[id];
+}
+
+QVariant SettingsObject::get(const QString &id) const
+{
+ Setting *setting = getSetting(id);
+ return (setting ? setting->get() : QVariant());
+}
+
+bool SettingsObject::set(const QString &id, QVariant value)
+{
+ Setting *setting = getSetting(id);
+ if (!setting)
+ {
+ qDebug(QString("Error changing setting %1. Setting doesn't exist.").
+ arg(id).toUtf8());
+ return false;
+ }
+ else
+ {
+ setting->set(value);
+ return true;
+ }
+}
+
+void SettingsObject::reset(const QString &id) const
+{
+ Setting *setting = getSetting(id);
+ if(setting)
+ setting->reset();
+}
+
+
+QList<Setting *> SettingsObject::getSettings()
+{
+ return m_settings.values();
+}
+
+bool SettingsObject::contains(const QString &id)
+{
+ return m_settings.contains(id);
+}
+
+
+void SettingsObject::connectSignals(const Setting &setting)
+{
+ connect(&setting, SIGNAL(settingChanged(const Setting &, QVariant)),
+ SLOT(changeSetting(const Setting &, QVariant)));
+ connect(&setting, SIGNAL(settingChanged(const Setting &, QVariant)),
+ SIGNAL(settingChanged(const Setting &, QVariant)));
+
+ connect(&setting, SIGNAL(settingReset(Setting)),
+ SLOT(resetSetting(const Setting &)));
+ connect(&setting, SIGNAL(settingReset(Setting)),
+ SIGNAL(settingReset(const Setting &)));
+}
+
+void SettingsObject::disconnectSignals(const Setting &setting)
+{
+ setting.disconnect(SIGNAL(settingChanged(const Setting &, QVariant)),
+ this, SLOT(changeSetting(const Setting &, QVariant)));
+ setting.disconnect(SIGNAL(settingChanged(const Setting &, QVariant)),
+ this, SIGNAL(settingChanged(const Setting &, QVariant)));
+
+ setting.disconnect(SIGNAL(settingReset(const Setting &, QVariant)),
+ this, SLOT(resetSetting(const Setting &, QVariant)));
+ setting.disconnect(SIGNAL(settingReset(const Setting &, QVariant)),
+ this, SIGNAL(settingReset(const Setting &, QVariant)));
+}
diff --git a/depends/settings/src/stubkeyring.cpp b/depends/settings/src/stubkeyring.cpp
new file mode 100644
index 00000000..cf814d2f
--- /dev/null
+++ b/depends/settings/src/stubkeyring.cpp
@@ -0,0 +1,104 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Authors: Orochimarufan <orochimarufan.x3@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "stubkeyring.h"
+
+#include <QStringList>
+
+// Scrambling
+// this is NOT SAFE, but it's not plain either.
+int scrambler = 0x9586309;
+
+QString scramble(QString in_)
+{
+ QByteArray in = in_.toUtf8();
+ QByteArray out;
+ for (int i = 0; i<in.length(); i++)
+ out.append(in.at(i) ^ scrambler);
+ return QString::fromUtf8(out);
+}
+
+inline QString base64(QString in)
+{
+ return QString(in.toUtf8().toBase64());
+}
+inline QString unbase64(QString in)
+{
+ return QString::fromUtf8(QByteArray::fromBase64(in.toLatin1()));
+}
+
+inline QString scramble64(QString in)
+{
+ return base64(scramble(in));
+}
+inline QString unscramble64(QString in)
+{
+ return scramble(unbase64(in));
+}
+
+// StubKeyring implementation
+inline QString generateKey(QString service, QString username)
+{
+ return QString("%1/%2").arg(base64(service)).arg(scramble64(username));
+}
+
+bool StubKeyring::storePassword(QString service, QString username, QString password)
+{
+ m_settings.setValue(generateKey(service, username), scramble64(password));
+ return true;
+}
+
+QString StubKeyring::getPassword(QString service, QString username)
+{
+ QString key = generateKey(service, username);
+ if (!m_settings.contains(key))
+ return QString();
+ return unscramble64(m_settings.value(key).toString());
+}
+
+bool StubKeyring::hasPassword(QString service, QString username)
+{
+ return m_settings.contains(generateKey(service, username));
+}
+
+QStringList StubKeyring::getStoredAccounts(QString service)
+{
+ service = base64(service).append('/');
+ QStringList out;
+ QStringList in(m_settings.allKeys());
+ QStringListIterator it(in);
+ while(it.hasNext())
+ {
+ QString c = it.next();
+ if (c.startsWith(service))
+ out << unscramble64(c.mid(service.length()));
+ }
+ return out;
+}
+
+void StubKeyring::removeStoredAccount ( QString service, QString username )
+{
+ QString key = generateKey(service, username);
+ m_settings.remove(key);
+}
+
+//FIXME: this needs tweaking/changes for user account level storage
+StubKeyring::StubKeyring() :
+// m_settings(QSettings::UserScope, "Orochimarufan", "Keyring")
+ m_settings("keyring.cfg", QSettings::IniFormat)
+{
+}
diff --git a/depends/settings/src/stubkeyring.h b/depends/settings/src/stubkeyring.h
new file mode 100644
index 00000000..45791c85
--- /dev/null
+++ b/depends/settings/src/stubkeyring.h
@@ -0,0 +1,41 @@
+/* Copyright 2013 MultiMC Contributors
+ *
+ * Authors: Orochimarufan <orochimarufan.x3@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef STUBKEYRING_H
+#define STUBKEYRING_H
+
+#include "include/keyring.h"
+
+#include <QSettings>
+
+class StubKeyring : public Keyring
+{
+public:
+ virtual bool storePassword(QString service, QString username, QString password);
+ virtual QString getPassword(QString service, QString username);
+ virtual bool hasPassword(QString service, QString username);
+ virtual QStringList getStoredAccounts(QString service);
+ virtual void removeStoredAccount(QString service, QString username);
+private:
+ friend class Keyring;
+ explicit StubKeyring();
+ virtual bool isValid() { return true; }
+
+ QSettings m_settings;
+};
+
+#endif // STUBKEYRING_H