Allow the use of synonyms in settings. Refactor settings.
Remove a bunch of obsolete/unused code.
This commit is contained in:
		| @@ -5,44 +5,27 @@ find_package(Qt5Core REQUIRED) | ||||
|  | ||||
| # Include Qt headers. | ||||
| include_directories(${Qt5Base_INCLUDE_DIRS}) | ||||
| include_directories(${Qt5Network_INCLUDE_DIRS}) | ||||
|  | ||||
| SET(LIBSETTINGS_HEADERS | ||||
| include/libsettings_config.h | ||||
|  | ||||
| include/inifile.h | ||||
|  | ||||
| include/settingsobject.h | ||||
| include/setting.h | ||||
| include/overridesetting.h | ||||
|  | ||||
| include/basicsettingsobject.h | ||||
| include/inisettingsobject.h | ||||
|  | ||||
| include/keyring.h | ||||
| ) | ||||
|  | ||||
| SET(LIBSETTINGS_HEADERS_PRIVATE | ||||
| src/stubkeyring.h | ||||
| ) | ||||
|  | ||||
| SET(LIBSETTINGS_SOURCES | ||||
| src/inifile.cpp | ||||
| libsettings_config.h | ||||
|  | ||||
| src/settingsobject.cpp | ||||
| src/setting.cpp | ||||
| src/overridesetting.cpp | ||||
| inifile.h | ||||
| inifile.cpp | ||||
|  | ||||
| src/basicsettingsobject.cpp | ||||
| src/inisettingsobject.cpp | ||||
| settingsobject.h | ||||
| settingsobject.cpp | ||||
| inisettingsobject.h | ||||
| inisettingsobject.cpp | ||||
|  | ||||
| src/keyring.cpp | ||||
| src/stubkeyring.cpp | ||||
| setting.h | ||||
| setting.cpp | ||||
| overridesetting.h | ||||
| overridesetting.cpp | ||||
| ) | ||||
|  | ||||
| # Set the include dir path. | ||||
| SET(LIBSETTINGS_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE) | ||||
| include_directories(${LIBSETTINGS_INCLUDE_DIR}) | ||||
| SET(LIBSETTINGS_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" PARENT_SCOPE) | ||||
|  | ||||
| # Static link! | ||||
| ADD_DEFINITIONS(-DLIBSETTINGS_STATIC) | ||||
| @@ -59,6 +42,6 @@ IF(MultiMC_CODE_COVERAGE) | ||||
| 	SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O0 --coverage") | ||||
| ENDIF(MultiMC_CODE_COVERAGE) | ||||
|  | ||||
| add_library(libSettings STATIC ${LIBSETTINGS_SOURCES} ${LIBSETTINGS_HEADERS} ${LIBSETTINGS_HEADERS_PRIVATE}) | ||||
| add_library(libSettings STATIC ${LIBSETTINGS_SOURCES}) | ||||
| qt5_use_modules(libSettings Core) | ||||
| target_link_libraries(libSettings) | ||||
|   | ||||
| @@ -1,42 +0,0 @@ | ||||
| /* 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. | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include <QObject> | ||||
| #include <QSettings> | ||||
|  | ||||
| #include "settingsobject.h" | ||||
|  | ||||
| #include "libsettings_config.h" | ||||
|  | ||||
| /*! | ||||
|  * \brief A settings object that stores its settings in a QSettings object. | ||||
|  */ | ||||
| class LIBSETTINGS_EXPORT BasicSettingsObject : public SettingsObject | ||||
| { | ||||
| 	Q_OBJECT | ||||
| public: | ||||
| 	explicit BasicSettingsObject(QObject *parent = 0); | ||||
|  | ||||
| protected | ||||
| slots: | ||||
| 	virtual void changeSetting(const Setting &setting, QVariant value); | ||||
|  | ||||
| protected: | ||||
| 	virtual QVariant retrieveValue(const Setting &setting); | ||||
|  | ||||
| 	QSettings config; | ||||
| }; | ||||
| @@ -1,97 +0,0 @@ | ||||
| /* 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. | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include <QString> | ||||
|  | ||||
| #include "libsettings_config.h" | ||||
|  | ||||
| /** | ||||
|  * @file libsettings/include/keyring.h | ||||
|  * Access to System Keyrings | ||||
|  */ | ||||
|  | ||||
| /** | ||||
|  * @brief The Keyring class | ||||
|  * the System Keyring/Keychain/Wallet/Vault/etc | ||||
|  */ | ||||
| class LIBSETTINGS_EXPORT Keyring | ||||
| { | ||||
| public: | ||||
| 	/** | ||||
| 	 * @brief virtual dtor | ||||
| 	 */ | ||||
| 	virtual ~Keyring() {}; | ||||
|  | ||||
| 	/** | ||||
| 	 * @brief the System Keyring instance | ||||
| 	 * @return the Keyring instance | ||||
| 	 */ | ||||
| 	static Keyring *instance(); | ||||
|  | ||||
| 	/** | ||||
| 	 * @brief store a password in the Keyring | ||||
| 	 * @param service the service name | ||||
| 	 * @param username the account name | ||||
| 	 * @param password the password to store | ||||
| 	 * @return success | ||||
| 	 */ | ||||
| 	virtual bool storePassword(QString service, QString username, QString password) = 0; | ||||
|  | ||||
| 	/** | ||||
| 	 * @brief get a password from the Keyring | ||||
| 	 * @param service the service name | ||||
| 	 * @param username the account name | ||||
| 	 * @return the password (success=!isNull()) | ||||
| 	 */ | ||||
| 	virtual QString getPassword(QString service, QString username) = 0; | ||||
|  | ||||
| 	/** | ||||
| 	 * @brief lookup a password | ||||
| 	 * @param service the service name | ||||
| 	 * @param username the account name | ||||
| 	 * @return wether the password is available | ||||
| 	 */ | ||||
| 	virtual bool hasPassword(QString service, QString username) = 0; | ||||
|  | ||||
| 	/** | ||||
| 	 * @brief get a list of all stored accounts. | ||||
| 	 * @param service the service name | ||||
| 	 * @return | ||||
| 	 */ | ||||
| 	virtual QStringList getStoredAccounts(QString service) = 0; | ||||
|  | ||||
| 	/** | ||||
| 	 * @brief Remove the specified account from storage | ||||
| 	 * @param service the service name | ||||
| 	 * @param username the account name | ||||
| 	 * @return | ||||
| 	 */ | ||||
| 	virtual void removeStoredAccount(QString service, QString username) = 0; | ||||
|  | ||||
| protected: | ||||
| 	/// fall back to StubKeyring if false | ||||
| 	virtual bool isValid() | ||||
| 	{ | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| private: | ||||
| 	static Keyring *m_instance; | ||||
| 	static void destroy(); | ||||
| }; | ||||
| @@ -13,7 +13,7 @@ | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| #include "include/inifile.h" | ||||
| #include "inifile.h" | ||||
| 
 | ||||
| #include <QFile> | ||||
| #include <QTextStream> | ||||
| @@ -13,8 +13,8 @@ | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| #include "include/inisettingsobject.h" | ||||
| #include "include/setting.h" | ||||
| #include "inisettingsobject.h" | ||||
| #include "setting.h" | ||||
| 
 | ||||
| INISettingsObject::INISettingsObject(const QString &path, QObject *parent) | ||||
| 	: SettingsObject(parent) | ||||
| @@ -32,31 +32,45 @@ void INISettingsObject::changeSetting(const Setting &setting, QVariant value) | ||||
| { | ||||
| 	if (contains(setting.id())) | ||||
| 	{ | ||||
| 		// valid value -> set the main config, remove all the sysnonyms
 | ||||
| 		if (value.isValid()) | ||||
| 			m_ini.set(setting.configKey(), value); | ||||
| 		{ | ||||
| 			auto list = setting.configKeys(); | ||||
| 			m_ini.set(list.takeFirst(), value); | ||||
| 			for(auto iter: list) | ||||
| 				m_ini.remove(iter); | ||||
| 		} | ||||
| 		// invalid -> remove all (just like resetSetting)
 | ||||
| 		else | ||||
| 			m_ini.remove(setting.configKey()); | ||||
| 		{ | ||||
| 			for(auto iter: setting.configKeys()) | ||||
| 				m_ini.remove(iter); | ||||
| 		} | ||||
| 		m_ini.saveFile(m_filePath); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| void INISettingsObject::resetSetting(const Setting &setting) | ||||
| { | ||||
| 	// if we have the setting, remove all the synonyms. ALL OF THEM
 | ||||
| 	if (contains(setting.id())) | ||||
| 	{ | ||||
| 		m_ini.remove(setting.configKey()); | ||||
| 		for(auto iter: setting.configKeys()) | ||||
| 			m_ini.remove(iter); | ||||
| 		m_ini.saveFile(m_filePath); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| QVariant INISettingsObject::retrieveValue(const Setting &setting) | ||||
| { | ||||
| 	// if we have the setting, return value of the first matching synonym
 | ||||
| 	if (contains(setting.id())) | ||||
| 	{ | ||||
| 		return m_ini.get(setting.configKey(), QVariant()); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		return QVariant(); | ||||
| 		for(auto iter: setting.configKeys()) | ||||
| 		{ | ||||
| 			if(m_ini.contains(iter)) | ||||
| 				return m_ini[iter]; | ||||
| 		} | ||||
| 	} | ||||
| 	return QVariant(); | ||||
| } | ||||
| @@ -26,3 +26,4 @@ | ||||
| #define LIBSETTINGS_EXPORT Q_DECL_IMPORT | ||||
| #endif | ||||
| #endif | ||||
| 
 | ||||
| @@ -13,10 +13,10 @@ | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| #include "include/overridesetting.h" | ||||
| #include "overridesetting.h" | ||||
| 
 | ||||
| OverrideSetting::OverrideSetting(const QString &name, Setting *other, QObject *parent) | ||||
| 	: Setting(name, QVariant(), parent) | ||||
| OverrideSetting::OverrideSetting(std::shared_ptr<Setting> other) | ||||
| 	: Setting(other->configKeys(), QVariant()) | ||||
| { | ||||
| 	m_other = other; | ||||
| } | ||||
| @@ -16,6 +16,7 @@ | ||||
| #pragma once | ||||
| 
 | ||||
| #include <QObject> | ||||
| #include <memory> | ||||
| 
 | ||||
| #include "setting.h" | ||||
| 
 | ||||
| @@ -31,10 +32,10 @@ class LIBSETTINGS_EXPORT OverrideSetting : public Setting | ||||
| { | ||||
| 	Q_OBJECT | ||||
| public: | ||||
| 	explicit OverrideSetting(const QString &name, Setting *other, QObject *parent = 0); | ||||
| 	explicit OverrideSetting(std::shared_ptr<Setting> other); | ||||
| 
 | ||||
| 	virtual QVariant defValue() const; | ||||
| 
 | ||||
| protected: | ||||
| 	Setting *m_other; | ||||
| 	std::shared_ptr<Setting> m_other; | ||||
| }; | ||||
| @@ -13,17 +13,23 @@ | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| #include "include/setting.h" | ||||
| #include "include/settingsobject.h" | ||||
| #include "setting.h" | ||||
| #include "settingsobject.h" | ||||
| 
 | ||||
| Setting::Setting(QString id, QVariant defVal, QObject *parent) | ||||
| 	: QObject(parent), m_id(id), m_defVal(defVal) | ||||
| Setting::Setting(QStringList synonyms, QVariant defVal) | ||||
| 	: QObject(), m_synonyms(synonyms), m_defVal(defVal) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| Setting::Setting(QString id, QVariant defVal) | ||||
| 	: QObject(), m_synonyms({id}), m_defVal(defVal) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| QVariant Setting::get() const | ||||
| { | ||||
| 	SettingsObject *sbase = qobject_cast<SettingsObject *>(parent()); | ||||
| 	SettingsObject *sbase = m_storage; | ||||
| 	if (!sbase) | ||||
| 	{ | ||||
| 		return defValue(); | ||||
| @@ -17,6 +17,8 @@ | ||||
| 
 | ||||
| #include <QObject> | ||||
| #include <QVariant> | ||||
| #include <QStringList> | ||||
| #include <memory> | ||||
| 
 | ||||
| #include "libsettings_config.h" | ||||
| 
 | ||||
| @@ -33,7 +35,13 @@ public: | ||||
| 	 * \brief Constructs a new Setting object with the given parent. | ||||
| 	 * \param parent The Setting's parent object. | ||||
| 	 */ | ||||
| 	explicit Setting(QString id, QVariant defVal = QVariant(), QObject *parent = 0); | ||||
| 	explicit Setting(QStringList synonyms, QVariant defVal = QVariant()); | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Constructs a new Setting object with the given parent. | ||||
| 	 * \param parent The Setting's parent object. | ||||
| 	 */ | ||||
| 	explicit Setting(QString id, QVariant defVal = QVariant()); | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Gets this setting's ID. | ||||
| @@ -44,7 +52,7 @@ public: | ||||
| 	 */ | ||||
| 	virtual QString id() const | ||||
| 	{ | ||||
| 		return m_id; | ||||
| 		return m_synonyms.first(); | ||||
| 	} | ||||
| 
 | ||||
| 	/*!
 | ||||
| @@ -53,9 +61,9 @@ public: | ||||
| 	 * the same as the setting's ID, but it can be different. | ||||
| 	 * \return The setting's config file key. | ||||
| 	 */ | ||||
| 	virtual QString configKey() const | ||||
| 	virtual QStringList configKeys() const | ||||
| 	{ | ||||
| 		return id(); | ||||
| 		return m_synonyms; | ||||
| 	} | ||||
| 
 | ||||
| 	/*!
 | ||||
| @@ -111,11 +119,12 @@ slots: | ||||
| 	 * \brief Reset the setting to default | ||||
| 	 * This is done by emitting the settingReset() signal which will then be | ||||
| 	 * handled by the SettingsObject object and cause the setting to change. | ||||
| 	 * \param value The new value. | ||||
| 	 */ | ||||
| 	virtual void reset(); | ||||
| 
 | ||||
| protected: | ||||
| 	QString m_id; | ||||
| 	friend class SettingsObject; | ||||
| 	SettingsObject * m_storage; | ||||
| 	QStringList m_synonyms; | ||||
| 	QVariant m_defVal; | ||||
| }; | ||||
| @@ -13,8 +13,9 @@ | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| #include "include/settingsobject.h" | ||||
| #include "include/setting.h" | ||||
| #include "settingsobject.h" | ||||
| #include "setting.h" | ||||
| #include "overridesetting.h" | ||||
| 
 | ||||
| #include <QVariant> | ||||
| 
 | ||||
| @@ -22,17 +23,49 @@ SettingsObject::SettingsObject(QObject *parent) : QObject(parent) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| SettingsObject::~SettingsObject() | ||||
| { | ||||
| 	m_settings.clear(); | ||||
| } | ||||
| 
 | ||||
| std::shared_ptr<Setting> SettingsObject::registerOverride(std::shared_ptr<Setting> original) | ||||
| { | ||||
| 	if (contains(original->id())) | ||||
| 	{ | ||||
| 		qDebug(QString("Failed to register setting %1. ID already exists.") | ||||
| 				   .arg(original->id()) | ||||
| 				   .toUtf8()); | ||||
| 		return nullptr; // Fail
 | ||||
| 	} | ||||
| 	auto override = std::make_shared<OverrideSetting>(original); | ||||
| 	override->m_storage = this; | ||||
| 	connectSignals(*override); | ||||
| 	m_settings.insert(override->id(), override); | ||||
| 	return override; | ||||
| } | ||||
| 
 | ||||
| std::shared_ptr<Setting> SettingsObject::registerSetting(QStringList synonyms, QVariant defVal) | ||||
| { | ||||
| 	if (synonyms.empty()) | ||||
| 		return nullptr; | ||||
| 	if (contains(synonyms.first())) | ||||
| 	{ | ||||
| 		qDebug(QString("Failed to register setting %1. ID already exists.") | ||||
| 				   .arg(synonyms.first()) | ||||
| 				   .toUtf8()); | ||||
| 		return nullptr; // Fail
 | ||||
| 	} | ||||
| 	auto setting = std::make_shared<Setting>(synonyms, defVal); | ||||
| 	setting->m_storage = this; | ||||
| 	connectSignals(*setting); | ||||
| 	m_settings.insert(setting->id(), setting); | ||||
| 	return setting; | ||||
| } | ||||
| 
 | ||||
| /*
 | ||||
| 
 | ||||
| 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.") | ||||
| @@ -50,21 +83,8 @@ bool SettingsObject::registerSetting(Setting *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 | ||||
| */ | ||||
| std::shared_ptr<Setting> SettingsObject::getSetting(const QString &id) const | ||||
| { | ||||
| 	// Make sure there is a setting with the given ID.
 | ||||
| 	if (!m_settings.contains(id)) | ||||
| @@ -75,13 +95,13 @@ Setting *SettingsObject::getSetting(const QString &id) const | ||||
| 
 | ||||
| QVariant SettingsObject::get(const QString &id) const | ||||
| { | ||||
| 	Setting *setting = getSetting(id); | ||||
| 	auto setting = getSetting(id); | ||||
| 	return (setting ? setting->get() : QVariant()); | ||||
| } | ||||
| 
 | ||||
| bool SettingsObject::set(const QString &id, QVariant value) | ||||
| { | ||||
| 	Setting *setting = getSetting(id); | ||||
| 	auto setting = getSetting(id); | ||||
| 	if (!setting) | ||||
| 	{ | ||||
| 		qDebug(QString("Error changing setting %1. Setting doesn't exist.").arg(id).toUtf8()); | ||||
| @@ -96,16 +116,11 @@ bool SettingsObject::set(const QString &id, QVariant value) | ||||
| 
 | ||||
| void SettingsObject::reset(const QString &id) const | ||||
| { | ||||
| 	Setting *setting = getSetting(id); | ||||
| 	auto 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); | ||||
| @@ -121,16 +136,3 @@ void SettingsObject::connectSignals(const Setting &setting) | ||||
| 	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))); | ||||
| } | ||||
| @@ -17,6 +17,9 @@ | ||||
| 
 | ||||
| #include <QObject> | ||||
| #include <QMap> | ||||
| #include <QStringList> | ||||
| #include <QVariant> | ||||
| #include <memory> | ||||
| 
 | ||||
| #include "libsettings_config.h" | ||||
| 
 | ||||
| @@ -39,32 +42,37 @@ class LIBSETTINGS_EXPORT SettingsObject : public QObject | ||||
| 	Q_OBJECT | ||||
| public: | ||||
| 	explicit SettingsObject(QObject *parent = 0); | ||||
| 
 | ||||
| 	virtual ~SettingsObject(); | ||||
| 	/*!
 | ||||
| 	 * \brief Registers the given setting with this SettingsObject and connects the necessary | ||||
| 	 * signals. | ||||
| 	 * Registers an override setting for the given original setting in this settings object | ||||
| 	 * | ||||
| 	 * This will fail if there is already a setting with the same ID as | ||||
| 	 * the one that is being registered. | ||||
| 	 * \note Registering a setting object causes the SettingsObject to take ownership | ||||
| 	 * of the object. This means that setting's parent will be set to the object | ||||
| 	 * it was registered with. Because the object it was registered with has taken | ||||
| 	 * ownership, it becomes responsible for managing that setting object's memory. | ||||
| 	 * \warning Do \b not delete the setting after registering it. | ||||
| 	 * \param setting A pointer to the setting that will be registered. | ||||
| 	 * \return True if successful. False if registry failed. | ||||
| 	 * \return A valid Setting shared pointer if successful. | ||||
| 	 */ | ||||
| 	virtual bool registerSetting(Setting *setting); | ||||
| 	std::shared_ptr<Setting> registerOverride(std::shared_ptr<Setting> original); | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Unregisters the given setting from this SettingsObject and disconnects its | ||||
| 	 * signals. | ||||
| 	 * \note This does not delete the setting. Furthermore, when the setting is | ||||
| 	 * unregistered, the SettingsObject drops ownership of the setting. This means | ||||
| 	 * that if you unregister a setting, its parent is set to null and you become | ||||
| 	 * responsible for freeing its memory. | ||||
| 	 * \param setting The setting to unregister. | ||||
| 	 * Registers the given setting with this SettingsObject and connects the necessary  signals. | ||||
| 	 * | ||||
| 	 * This will fail if there is already a setting with the same ID as | ||||
| 	 * the one that is being registered. | ||||
| 	 * \return A valid Setting shared pointer if successful. | ||||
| 	 */ | ||||
| 	virtual void unregisterSetting(Setting *setting); | ||||
| 	std::shared_ptr<Setting> registerSetting(QStringList synonyms, | ||||
| 											 QVariant defVal = QVariant()); | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * Registers the given setting with this SettingsObject and connects the necessary signals. | ||||
| 	 * | ||||
| 	 * This will fail if there is already a setting with the same ID as | ||||
| 	 * the one that is being registered. | ||||
| 	 * \return A valid Setting shared pointer if successful. | ||||
| 	 */ | ||||
| 	std::shared_ptr<Setting> registerSetting(QString id, QVariant defVal = QVariant()) | ||||
| 	{ | ||||
| 		return registerSetting(QStringList(id), defVal); | ||||
| 	} | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Gets the setting with the given ID. | ||||
| @@ -73,18 +81,7 @@ public: | ||||
| 	 * Returns null if there is no setting with the given ID. | ||||
| 	 * \sa operator []() | ||||
| 	 */ | ||||
| 	virtual Setting *getSetting(const QString &id) const; | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Same as getSetting() | ||||
| 	 * \param id The ID of the setting to get. | ||||
| 	 * \return A pointer to the setting with the given ID. | ||||
| 	 * \sa getSetting() | ||||
| 	 */ | ||||
| 	inline Setting *operator[](const QString &id) | ||||
| 	{ | ||||
| 		return getSetting(id); | ||||
| 	} | ||||
| 	std::shared_ptr<Setting> getSetting(const QString &id) const; | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Gets the value of the setting with the given ID. | ||||
| @@ -92,7 +89,7 @@ public: | ||||
| 	 * \return The setting's value as a QVariant. | ||||
| 	 * If no setting with the given ID exists, returns an invalid QVariant. | ||||
| 	 */ | ||||
| 	virtual QVariant get(const QString &id) const; | ||||
| 	QVariant get(const QString &id) const; | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Sets the value of the setting with the given ID. | ||||
| @@ -101,27 +98,20 @@ public: | ||||
| 	 * \param value The new value of the setting. | ||||
| 	 * \return True if successful, false if it failed. | ||||
| 	 */ | ||||
| 	virtual bool set(const QString &id, QVariant value); | ||||
| 	bool set(const QString &id, QVariant value); | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Reverts the setting with the given ID to default. | ||||
| 	 * \param id The ID of the setting to reset. | ||||
| 	 */ | ||||
| 	virtual void reset(const QString &id) const; | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Gets a QList with pointers to all of the registered settings. | ||||
| 	 * The order of the entries in the list is undefined. | ||||
| 	 * \return A QList with pointers to all registered settings. | ||||
| 	 */ | ||||
| 	virtual QList<Setting *> getSettings(); | ||||
| 	void reset(const QString &id) const; | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Checks if this SettingsObject contains a setting with the given ID. | ||||
| 	 * \param id The ID to check for. | ||||
| 	 * \return True if the SettingsObject has a setting with the given ID. | ||||
| 	 */ | ||||
| 	virtual bool contains(const QString &id); | ||||
| 	bool contains(const QString &id); | ||||
| 
 | ||||
| signals: | ||||
| 	/*!
 | ||||
| @@ -167,13 +157,7 @@ protected: | ||||
| 	 * \brief Connects the necessary signals to the given Setting. | ||||
| 	 * \param setting The setting to connect. | ||||
| 	 */ | ||||
| 	virtual void connectSignals(const Setting &setting); | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Disconnects signals from the given Setting. | ||||
| 	 * \param setting The setting to disconnect. | ||||
| 	 */ | ||||
| 	virtual void disconnectSignals(const Setting &setting); | ||||
| 	void connectSignals(const Setting &setting); | ||||
| 
 | ||||
| 	/*!
 | ||||
| 	 * \brief Function used by Setting objects to get their values from the SettingsObject. | ||||
| @@ -185,5 +169,5 @@ protected: | ||||
| 	friend class Setting; | ||||
| 
 | ||||
| private: | ||||
| 	QMap<QString, Setting *> m_settings; | ||||
| 	QMap<QString, std::shared_ptr<Setting>> m_settings; | ||||
| }; | ||||
| @@ -1,44 +0,0 @@ | ||||
| /* 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(); | ||||
| 	} | ||||
| } | ||||
| @@ -1,63 +0,0 @@ | ||||
| /* 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; | ||||
| @@ -1,105 +0,0 @@ | ||||
| /* 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) | ||||
| { | ||||
| } | ||||
| @@ -1,47 +0,0 @@ | ||||
| /* 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. | ||||
|  */ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include "include/keyring.h" | ||||
|  | ||||
| #include <QSettings> | ||||
|  | ||||
| class StubKeyring : public Keyring | ||||
| { | ||||
| public: | ||||
| 	/** | ||||
| 	 * @brief virtual dtor | ||||
| 	 */ | ||||
| 	virtual ~StubKeyring() {}; | ||||
|  | ||||
| 	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; | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user