From 396e63500ec189b003d36504bae91a2b6a78a69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Wed, 1 Jan 2014 15:08:40 +0100 Subject: Allow the use of synonyms in settings. Refactor settings. Remove a bunch of obsolete/unused code. --- depends/settings/CMakeLists.txt | 43 ++---- depends/settings/include/basicsettingsobject.h | 42 ------ depends/settings/include/inifile.h | 38 ----- depends/settings/include/inisettingsobject.h | 61 -------- depends/settings/include/keyring.h | 97 ------------- depends/settings/include/libsettings_config.h | 28 ---- depends/settings/include/overridesetting.h | 40 ------ depends/settings/include/setting.h | 121 ---------------- depends/settings/include/settingsobject.h | 189 ------------------------- depends/settings/inifile.cpp | 106 ++++++++++++++ depends/settings/inifile.h | 38 +++++ depends/settings/inisettingsobject.cpp | 76 ++++++++++ depends/settings/inisettingsobject.h | 61 ++++++++ depends/settings/libsettings_config.h | 29 ++++ depends/settings/overridesetting.cpp | 30 ++++ depends/settings/overridesetting.h | 41 ++++++ depends/settings/setting.cpp | 59 ++++++++ depends/settings/setting.h | 130 +++++++++++++++++ depends/settings/settingsobject.cpp | 138 ++++++++++++++++++ depends/settings/settingsobject.h | 173 ++++++++++++++++++++++ depends/settings/src/basicsettingsobject.cpp | 44 ------ depends/settings/src/inifile.cpp | 106 -------------- depends/settings/src/inisettingsobject.cpp | 62 -------- depends/settings/src/keyring.cpp | 63 --------- depends/settings/src/overridesetting.cpp | 30 ---- depends/settings/src/setting.cpp | 53 ------- depends/settings/src/settingsobject.cpp | 136 ------------------ depends/settings/src/stubkeyring.cpp | 105 -------------- depends/settings/src/stubkeyring.h | 47 ------ 29 files changed, 894 insertions(+), 1292 deletions(-) delete mode 100644 depends/settings/include/basicsettingsobject.h delete mode 100644 depends/settings/include/inifile.h delete mode 100644 depends/settings/include/inisettingsobject.h delete mode 100644 depends/settings/include/keyring.h delete mode 100644 depends/settings/include/libsettings_config.h delete mode 100644 depends/settings/include/overridesetting.h delete mode 100644 depends/settings/include/setting.h delete mode 100644 depends/settings/include/settingsobject.h create mode 100644 depends/settings/inifile.cpp create mode 100644 depends/settings/inifile.h create mode 100644 depends/settings/inisettingsobject.cpp create mode 100644 depends/settings/inisettingsobject.h create mode 100644 depends/settings/libsettings_config.h create mode 100644 depends/settings/overridesetting.cpp create mode 100644 depends/settings/overridesetting.h create mode 100644 depends/settings/setting.cpp create mode 100644 depends/settings/setting.h create mode 100644 depends/settings/settingsobject.cpp create mode 100644 depends/settings/settingsobject.h delete mode 100644 depends/settings/src/basicsettingsobject.cpp delete mode 100644 depends/settings/src/inifile.cpp delete mode 100644 depends/settings/src/inisettingsobject.cpp delete mode 100644 depends/settings/src/keyring.cpp delete mode 100644 depends/settings/src/overridesetting.cpp delete mode 100644 depends/settings/src/setting.cpp delete mode 100644 depends/settings/src/settingsobject.cpp delete mode 100644 depends/settings/src/stubkeyring.cpp delete mode 100644 depends/settings/src/stubkeyring.h (limited to 'depends') diff --git a/depends/settings/CMakeLists.txt b/depends/settings/CMakeLists.txt index 154697f6..da853a73 100644 --- a/depends/settings/CMakeLists.txt +++ b/depends/settings/CMakeLists.txt @@ -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) diff --git a/depends/settings/include/basicsettingsobject.h b/depends/settings/include/basicsettingsobject.h deleted file mode 100644 index 387a3646..00000000 --- a/depends/settings/include/basicsettingsobject.h +++ /dev/null @@ -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 -#include - -#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; -}; diff --git a/depends/settings/include/inifile.h b/depends/settings/include/inifile.h deleted file mode 100644 index 27da7bf0..00000000 --- a/depends/settings/include/inifile.h +++ /dev/null @@ -1,38 +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 -#include -#include - -#include "libsettings_config.h" - -// Sectionless INI parser (for instance config files) -class LIBSETTINGS_EXPORT INIFile : public QMap -{ -public: - explicit INIFile(); - - bool loadFile(QByteArray file); - bool loadFile(QString fileName); - bool saveFile(QString fileName); - - QVariant get(QString key, QVariant def) const; - void set(QString key, QVariant val); - QString unescape(QString orig); - QString escape(QString orig); -}; diff --git a/depends/settings/include/inisettingsobject.h b/depends/settings/include/inisettingsobject.h deleted file mode 100644 index 8badc0c6..00000000 --- a/depends/settings/include/inisettingsobject.h +++ /dev/null @@ -1,61 +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 - -#include "inifile.h" - -#include "settingsobject.h" - -#include "libsettings_config.h" - -/*! - * \brief A settings object that stores its settings in an INIFile. - */ -class LIBSETTINGS_EXPORT INISettingsObject : public SettingsObject -{ - Q_OBJECT -public: - explicit INISettingsObject(const QString &path, QObject *parent = 0); - - /*! - * \brief Gets the path to the INI file. - * \return The path to the INI file. - */ - virtual QString filePath() const - { - return m_filePath; - } - - /*! - * \brief Sets the path to the INI file and reloads it. - * \param filePath The INI file's new path. - */ - virtual void setFilePath(const QString &filePath); - -protected -slots: - virtual void changeSetting(const Setting &setting, QVariant value); - virtual void resetSetting(const Setting &setting); - -protected: - virtual QVariant retrieveValue(const Setting &setting); - - INIFile m_ini; - - QString m_filePath; -}; diff --git a/depends/settings/include/keyring.h b/depends/settings/include/keyring.h deleted file mode 100644 index a4da23b1..00000000 --- a/depends/settings/include/keyring.h +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright 2013 MultiMC Contributors - * - * Authors: Orochimarufan - * - * 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 "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(); -}; diff --git a/depends/settings/include/libsettings_config.h b/depends/settings/include/libsettings_config.h deleted file mode 100644 index ba77f640..00000000 --- a/depends/settings/include/libsettings_config.h +++ /dev/null @@ -1,28 +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 - -#ifdef LIBSETTINGS_STATIC -#define LIBSETTINGS_EXPORT -#else -#ifdef LIBSETTINGS_LIBRARY -#define LIBSETTINGS_EXPORT Q_DECL_EXPORT -#else -#define LIBSETTINGS_EXPORT Q_DECL_IMPORT -#endif -#endif diff --git a/depends/settings/include/overridesetting.h b/depends/settings/include/overridesetting.h deleted file mode 100644 index 3e60dc7c..00000000 --- a/depends/settings/include/overridesetting.h +++ /dev/null @@ -1,40 +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 - -#include "setting.h" - -#include "libsettings_config.h" - -/*! - * \brief A setting that 'overrides another.' - * This means that the setting's default value will be the value of another setting. - * The other setting can be (and usually is) a part of a different SettingsObject - * than this one. - */ -class LIBSETTINGS_EXPORT OverrideSetting : public Setting -{ - Q_OBJECT -public: - explicit OverrideSetting(const QString &name, Setting *other, QObject *parent = 0); - - virtual QVariant defValue() const; - -protected: - Setting *m_other; -}; diff --git a/depends/settings/include/setting.h b/depends/settings/include/setting.h deleted file mode 100644 index 39490207..00000000 --- a/depends/settings/include/setting.h +++ /dev/null @@ -1,121 +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 -#include - -#include "libsettings_config.h" - -class SettingsObject; - -/*! - * - */ -class LIBSETTINGS_EXPORT Setting : public QObject -{ - Q_OBJECT -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); - - /*! - * \brief Gets this setting's ID. - * This is used to refer to the setting within the application. - * \warning Changing the ID while the setting is registered with a SettingsObject results in - * undefined behavior. - * \return The ID of the setting. - */ - virtual QString id() const - { - return m_id; - } - - /*! - * \brief Gets this setting's config file key. - * This is used to store the setting's value in the config file. It is usually - * the same as the setting's ID, but it can be different. - * \return The setting's config file key. - */ - virtual QString configKey() const - { - return id(); - } - - /*! - * \brief Gets this setting's value as a QVariant. - * This is done by calling the SettingsObject's retrieveValue() function. - * If this Setting doesn't have a SettingsObject, this returns an invalid QVariant. - * \return QVariant containing this setting's value. - * \sa value() - */ - virtual QVariant get() const; - - /*! - * \brief Gets this setting's actual value (I.E. not as a QVariant). - * This function is just shorthand for get().value() - * \return The setting's actual value. - */ - template inline T value() const - { - return get().value(); - } - - /*! - * \brief Gets this setting's default value. - * \return The default value of this setting. - */ - virtual QVariant defValue() const; - -signals: - /*! - * \brief Signal emitted when this Setting object's value changes. - * \param setting A reference to the Setting that changed. - * \param value This Setting object's new value. - */ - void settingChanged(const Setting &setting, QVariant value); - - /*! - * \brief Signal emitted when this Setting object's value resets to default. - * \param setting A reference to the Setting that changed. - */ - void settingReset(const Setting &setting); - -public -slots: - /*! - * \brief Changes the setting's value. - * This is done by emitting the settingChanged() signal which will then be - * handled by the SettingsObject object and cause the setting to change. - * \param value The new value. - */ - virtual void set(QVariant value); - - /*! - * \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; - QVariant m_defVal; -}; diff --git a/depends/settings/include/settingsobject.h b/depends/settings/include/settingsobject.h deleted file mode 100644 index 7a6b3cb6..00000000 --- a/depends/settings/include/settingsobject.h +++ /dev/null @@ -1,189 +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 -#include - -#include "libsettings_config.h" - -class Setting; - -/*! - * \brief The SettingsObject handles communicating settings between the application and a - *settings file. - * The class keeps a list of Setting objects. Each Setting object represents one - * of the application's settings. These Setting objects are registered with - * a SettingsObject and can be managed similarly to the way a list works. - * - * \author Andrew Okin - * \date 2/22/2013 - * - * \sa Setting - */ -class LIBSETTINGS_EXPORT SettingsObject : public QObject -{ - Q_OBJECT -public: - explicit SettingsObject(QObject *parent = 0); - - /*! - * \brief 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. - * \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. - */ - virtual bool registerSetting(Setting *setting); - - /*! - * \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. - */ - virtual void unregisterSetting(Setting *setting); - - /*! - * \brief Gets the setting with the given ID. - * \param id The ID of the setting to get. - * \return A pointer to the setting with the given ID. - * 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); - } - - /*! - * \brief Gets the value of the setting with the given ID. - * \param id The ID of the setting to get. - * \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; - - /*! - * \brief Sets the value of the setting with the given ID. - * If no setting with the given ID exists, returns false and logs to qDebug - * \param id The ID of the setting to change. - * \param value The new value of the setting. - * \return True if successful, false if it failed. - */ - virtual 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 getSettings(); - - /*! - * \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); - -signals: - /*! - * \brief Signal emitted when one of this SettingsObject object's settings changes. - * This is usually just connected directly to each Setting object's - * settingChanged() signals. - * \param setting A reference to the Setting object that changed. - * \param value The Setting object's new value. - */ - void settingChanged(const Setting &setting, QVariant value); - - /*! - * \brief Signal emitted when one of this SettingsObject object's settings resets. - * This is usually just connected directly to each Setting object's - * settingReset() signals. - * \param setting A reference to the Setting object that changed. - */ - void settingReset(const Setting &setting); - -protected -slots: - /*! - * \brief Changes a setting. - * This slot is usually connected to each Setting object's - * settingChanged() signal. The signal is emitted, causing this slot - * to update the setting's value in the config file. - * \param setting A reference to the Setting object that changed. - * \param value The setting's new value. - */ - virtual void changeSetting(const Setting &setting, QVariant value) = 0; - - /*! - * \brief Resets a setting. - * This slot is usually connected to each Setting object's - * settingReset() signal. The signal is emitted, causing this slot - * to update the setting's value in the config file. - * \param setting A reference to the Setting object that changed. - */ - virtual void resetSetting(const Setting &setting) = 0; - -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); - - /*! - * \brief Function used by Setting objects to get their values from the SettingsObject. - * \param setting The - * \return - */ - virtual QVariant retrieveValue(const Setting &setting) = 0; - - friend class Setting; - -private: - QMap m_settings; -}; diff --git a/depends/settings/inifile.cpp b/depends/settings/inifile.cpp new file mode 100644 index 00000000..1170f0b1 --- /dev/null +++ b/depends/settings/inifile.cpp @@ -0,0 +1,106 @@ +/* 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 "inifile.h" + +#include +#include +#include + +INIFile::INIFile() +{ +} + +QString INIFile::unescape(QString orig) +{ + orig.replace("\\n", "\n"); + orig.replace("\\t", "\t"); + orig.replace("\\\\", "\\"); + return orig; +} +QString INIFile::escape(QString orig) +{ + orig.replace("\\", "\\\\"); + orig.replace("\n", "\\n"); + orig.replace("\t", "\\t"); + return orig; +} + +bool INIFile::saveFile(QString fileName) +{ + // TODO Handle errors. + QFile file(fileName); + file.open(QIODevice::WriteOnly); + QTextStream out(&file); + out.setCodec("UTF-8"); + + for (Iterator iter = begin(); iter != end(); iter++) + { + QString value = iter.value().toString(); + value = escape(value); + out << iter.key() << "=" << value << "\n"; + } + + return true; +} + +bool INIFile::loadFile(QString fileName) +{ + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly)) + return false; + bool success = loadFile(file.readAll()); + file.close(); + return success; +} +bool INIFile::loadFile(QByteArray file) +{ + QTextStream in(file); + in.setCodec("UTF-8"); + + 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(); + + valueStr = unescape(valueStr); + + QVariant value(valueStr); + 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/inifile.h b/depends/settings/inifile.h new file mode 100644 index 00000000..27da7bf0 --- /dev/null +++ b/depends/settings/inifile.h @@ -0,0 +1,38 @@ +/* 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 +#include +#include + +#include "libsettings_config.h" + +// Sectionless INI parser (for instance config files) +class LIBSETTINGS_EXPORT INIFile : public QMap +{ +public: + explicit INIFile(); + + bool loadFile(QByteArray file); + bool loadFile(QString fileName); + bool saveFile(QString fileName); + + QVariant get(QString key, QVariant def) const; + void set(QString key, QVariant val); + QString unescape(QString orig); + QString escape(QString orig); +}; diff --git a/depends/settings/inisettingsobject.cpp b/depends/settings/inisettingsobject.cpp new file mode 100644 index 00000000..5e52a56f --- /dev/null +++ b/depends/settings/inisettingsobject.cpp @@ -0,0 +1,76 @@ +/* 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 "inisettingsobject.h" +#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())) + { + // valid value -> set the main config, remove all the sysnonyms + if (value.isValid()) + { + 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 + { + 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())) + { + 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())) + { + for(auto iter: setting.configKeys()) + { + if(m_ini.contains(iter)) + return m_ini[iter]; + } + } + return QVariant(); +} diff --git a/depends/settings/inisettingsobject.h b/depends/settings/inisettingsobject.h new file mode 100644 index 00000000..8badc0c6 --- /dev/null +++ b/depends/settings/inisettingsobject.h @@ -0,0 +1,61 @@ +/* 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 + +#include "inifile.h" + +#include "settingsobject.h" + +#include "libsettings_config.h" + +/*! + * \brief A settings object that stores its settings in an INIFile. + */ +class LIBSETTINGS_EXPORT INISettingsObject : public SettingsObject +{ + Q_OBJECT +public: + explicit INISettingsObject(const QString &path, QObject *parent = 0); + + /*! + * \brief Gets the path to the INI file. + * \return The path to the INI file. + */ + virtual QString filePath() const + { + return m_filePath; + } + + /*! + * \brief Sets the path to the INI file and reloads it. + * \param filePath The INI file's new path. + */ + virtual void setFilePath(const QString &filePath); + +protected +slots: + virtual void changeSetting(const Setting &setting, QVariant value); + virtual void resetSetting(const Setting &setting); + +protected: + virtual QVariant retrieveValue(const Setting &setting); + + INIFile m_ini; + + QString m_filePath; +}; diff --git a/depends/settings/libsettings_config.h b/depends/settings/libsettings_config.h new file mode 100644 index 00000000..e5beed28 --- /dev/null +++ b/depends/settings/libsettings_config.h @@ -0,0 +1,29 @@ +/* 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 + +#ifdef LIBSETTINGS_STATIC +#define LIBSETTINGS_EXPORT +#else +#ifdef LIBSETTINGS_LIBRARY +#define LIBSETTINGS_EXPORT Q_DECL_EXPORT +#else +#define LIBSETTINGS_EXPORT Q_DECL_IMPORT +#endif +#endif + diff --git a/depends/settings/overridesetting.cpp b/depends/settings/overridesetting.cpp new file mode 100644 index 00000000..7b5f5418 --- /dev/null +++ b/depends/settings/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 "overridesetting.h" + +OverrideSetting::OverrideSetting(std::shared_ptr other) + : Setting(other->configKeys(), QVariant()) +{ + m_other = other; +} + +QVariant OverrideSetting::defValue() const +{ + if (m_other) + return m_other->get(); + else + return QVariant(); +} diff --git a/depends/settings/overridesetting.h b/depends/settings/overridesetting.h new file mode 100644 index 00000000..5ef901d0 --- /dev/null +++ b/depends/settings/overridesetting.h @@ -0,0 +1,41 @@ +/* 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 +#include + +#include "setting.h" + +#include "libsettings_config.h" + +/*! + * \brief A setting that 'overrides another.' + * This means that the setting's default value will be the value of another setting. + * The other setting can be (and usually is) a part of a different SettingsObject + * than this one. + */ +class LIBSETTINGS_EXPORT OverrideSetting : public Setting +{ + Q_OBJECT +public: + explicit OverrideSetting(std::shared_ptr other); + + virtual QVariant defValue() const; + +protected: + std::shared_ptr m_other; +}; diff --git a/depends/settings/setting.cpp b/depends/settings/setting.cpp new file mode 100644 index 00000000..1a0c6b6a --- /dev/null +++ b/depends/settings/setting.cpp @@ -0,0 +1,59 @@ +/* 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 "setting.h" +#include "settingsobject.h" + +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 = m_storage; + 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/setting.h b/depends/settings/setting.h new file mode 100644 index 00000000..179fa3bb --- /dev/null +++ b/depends/settings/setting.h @@ -0,0 +1,130 @@ +/* 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 +#include +#include +#include + +#include "libsettings_config.h" + +class SettingsObject; + +/*! + * + */ +class LIBSETTINGS_EXPORT Setting : public QObject +{ + Q_OBJECT +public: + /*! + * \brief Constructs a new Setting object with the given parent. + * \param parent The Setting's parent object. + */ + 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. + * This is used to refer to the setting within the application. + * \warning Changing the ID while the setting is registered with a SettingsObject results in + * undefined behavior. + * \return The ID of the setting. + */ + virtual QString id() const + { + return m_synonyms.first(); + } + + /*! + * \brief Gets this setting's config file key. + * This is used to store the setting's value in the config file. It is usually + * the same as the setting's ID, but it can be different. + * \return The setting's config file key. + */ + virtual QStringList configKeys() const + { + return m_synonyms; + } + + /*! + * \brief Gets this setting's value as a QVariant. + * This is done by calling the SettingsObject's retrieveValue() function. + * If this Setting doesn't have a SettingsObject, this returns an invalid QVariant. + * \return QVariant containing this setting's value. + * \sa value() + */ + virtual QVariant get() const; + + /*! + * \brief Gets this setting's actual value (I.E. not as a QVariant). + * This function is just shorthand for get().value() + * \return The setting's actual value. + */ + template inline T value() const + { + return get().value(); + } + + /*! + * \brief Gets this setting's default value. + * \return The default value of this setting. + */ + virtual QVariant defValue() const; + +signals: + /*! + * \brief Signal emitted when this Setting object's value changes. + * \param setting A reference to the Setting that changed. + * \param value This Setting object's new value. + */ + void settingChanged(const Setting &setting, QVariant value); + + /*! + * \brief Signal emitted when this Setting object's value resets to default. + * \param setting A reference to the Setting that changed. + */ + void settingReset(const Setting &setting); + +public +slots: + /*! + * \brief Changes the setting's value. + * This is done by emitting the settingChanged() signal which will then be + * handled by the SettingsObject object and cause the setting to change. + * \param value The new value. + */ + virtual void set(QVariant value); + + /*! + * \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. + */ + virtual void reset(); + +protected: + friend class SettingsObject; + SettingsObject * m_storage; + QStringList m_synonyms; + QVariant m_defVal; +}; diff --git a/depends/settings/settingsobject.cpp b/depends/settings/settingsobject.cpp new file mode 100644 index 00000000..43fc989a --- /dev/null +++ b/depends/settings/settingsobject.cpp @@ -0,0 +1,138 @@ +/* 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 "settingsobject.h" +#include "setting.h" +#include "overridesetting.h" + +#include + +SettingsObject::SettingsObject(QObject *parent) : QObject(parent) +{ +} + +SettingsObject::~SettingsObject() +{ + m_settings.clear(); +} + +std::shared_ptr SettingsObject::registerOverride(std::shared_ptr 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(original); + override->m_storage = this; + connectSignals(*override); + m_settings.insert(override->id(), override); + return override; +} + +std::shared_ptr 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(synonyms, defVal); + setting->m_storage = this; + connectSignals(*setting); + m_settings.insert(setting->id(), setting); + return setting; +} + +/* + +bool SettingsObject::registerSetting(Setting *setting) +{ + 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; +} +*/ +std::shared_ptr 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 +{ + auto setting = getSetting(id); + return (setting ? setting->get() : QVariant()); +} + +bool SettingsObject::set(const QString &id, QVariant value) +{ + auto 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 +{ + auto setting = getSetting(id); + if (setting) + setting->reset(); +} + +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 &))); +} diff --git a/depends/settings/settingsobject.h b/depends/settings/settingsobject.h new file mode 100644 index 00000000..27746f2d --- /dev/null +++ b/depends/settings/settingsobject.h @@ -0,0 +1,173 @@ +/* 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 +#include +#include +#include +#include + +#include "libsettings_config.h" + +class Setting; + +/*! + * \brief The SettingsObject handles communicating settings between the application and a + *settings file. + * The class keeps a list of Setting objects. Each Setting object represents one + * of the application's settings. These Setting objects are registered with + * a SettingsObject and can be managed similarly to the way a list works. + * + * \author Andrew Okin + * \date 2/22/2013 + * + * \sa Setting + */ +class LIBSETTINGS_EXPORT SettingsObject : public QObject +{ + Q_OBJECT +public: + explicit SettingsObject(QObject *parent = 0); + virtual ~SettingsObject(); + /*! + * 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. + * \return A valid Setting shared pointer if successful. + */ + std::shared_ptr registerOverride(std::shared_ptr original); + + /*! + * 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 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 registerSetting(QString id, QVariant defVal = QVariant()) + { + return registerSetting(QStringList(id), defVal); + } + + /*! + * \brief Gets the setting with the given ID. + * \param id The ID of the setting to get. + * \return A pointer to the setting with the given ID. + * Returns null if there is no setting with the given ID. + * \sa operator []() + */ + std::shared_ptr getSetting(const QString &id) const; + + /*! + * \brief Gets the value of the setting with the given ID. + * \param id The ID of the setting to get. + * \return The setting's value as a QVariant. + * If no setting with the given ID exists, returns an invalid QVariant. + */ + QVariant get(const QString &id) const; + + /*! + * \brief Sets the value of the setting with the given ID. + * If no setting with the given ID exists, returns false and logs to qDebug + * \param id The ID of the setting to change. + * \param value The new value of the setting. + * \return True if successful, false if it failed. + */ + 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. + */ + 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. + */ + bool contains(const QString &id); + +signals: + /*! + * \brief Signal emitted when one of this SettingsObject object's settings changes. + * This is usually just connected directly to each Setting object's + * settingChanged() signals. + * \param setting A reference to the Setting object that changed. + * \param value The Setting object's new value. + */ + void settingChanged(const Setting &setting, QVariant value); + + /*! + * \brief Signal emitted when one of this SettingsObject object's settings resets. + * This is usually just connected directly to each Setting object's + * settingReset() signals. + * \param setting A reference to the Setting object that changed. + */ + void settingReset(const Setting &setting); + +protected +slots: + /*! + * \brief Changes a setting. + * This slot is usually connected to each Setting object's + * settingChanged() signal. The signal is emitted, causing this slot + * to update the setting's value in the config file. + * \param setting A reference to the Setting object that changed. + * \param value The setting's new value. + */ + virtual void changeSetting(const Setting &setting, QVariant value) = 0; + + /*! + * \brief Resets a setting. + * This slot is usually connected to each Setting object's + * settingReset() signal. The signal is emitted, causing this slot + * to update the setting's value in the config file. + * \param setting A reference to the Setting object that changed. + */ + virtual void resetSetting(const Setting &setting) = 0; + +protected: + /*! + * \brief Connects the necessary signals to the given Setting. + * \param setting The setting to connect. + */ + void connectSignals(const Setting &setting); + + /*! + * \brief Function used by Setting objects to get their values from the SettingsObject. + * \param setting The + * \return + */ + virtual QVariant retrieveValue(const Setting &setting) = 0; + + friend class Setting; + +private: + QMap> m_settings; +}; diff --git a/depends/settings/src/basicsettingsobject.cpp b/depends/settings/src/basicsettingsobject.cpp deleted file mode 100644 index 96b38851..00000000 --- a/depends/settings/src/basicsettingsobject.cpp +++ /dev/null @@ -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(); - } -} diff --git a/depends/settings/src/inifile.cpp b/depends/settings/src/inifile.cpp deleted file mode 100644 index 83aec15e..00000000 --- a/depends/settings/src/inifile.cpp +++ /dev/null @@ -1,106 +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/inifile.h" - -#include -#include -#include - -INIFile::INIFile() -{ -} - -QString INIFile::unescape(QString orig) -{ - orig.replace("\\n", "\n"); - orig.replace("\\t", "\t"); - orig.replace("\\\\", "\\"); - return orig; -} -QString INIFile::escape(QString orig) -{ - orig.replace("\\", "\\\\"); - orig.replace("\n", "\\n"); - orig.replace("\t", "\\t"); - return orig; -} - -bool INIFile::saveFile(QString fileName) -{ - // TODO Handle errors. - QFile file(fileName); - file.open(QIODevice::WriteOnly); - QTextStream out(&file); - out.setCodec("UTF-8"); - - for (Iterator iter = begin(); iter != end(); iter++) - { - QString value = iter.value().toString(); - value = escape(value); - out << iter.key() << "=" << value << "\n"; - } - - return true; -} - -bool INIFile::loadFile(QString fileName) -{ - QFile file(fileName); - if (!file.open(QIODevice::ReadOnly)) - return false; - bool success = loadFile(file.readAll()); - file.close(); - return success; -} -bool INIFile::loadFile(QByteArray file) -{ - QTextStream in(file); - in.setCodec("UTF-8"); - - 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(); - - valueStr = unescape(valueStr); - - QVariant value(valueStr); - 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 deleted file mode 100644 index 4a7a7428..00000000 --- a/depends/settings/src/inisettingsobject.cpp +++ /dev/null @@ -1,62 +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/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 deleted file mode 100644 index 9eaba684..00000000 --- a/depends/settings/src/keyring.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright 2013 MultiMC Contributors - * - * Authors: Orochimarufan - * - * 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 deleted file mode 100644 index 5b10920d..00000000 --- a/depends/settings/src/overridesetting.cpp +++ /dev/null @@ -1,30 +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/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 deleted file mode 100644 index 899463dc..00000000 --- a/depends/settings/src/setting.cpp +++ /dev/null @@ -1,53 +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/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(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 deleted file mode 100644 index 32a63b8d..00000000 --- a/depends/settings/src/settingsobject.cpp +++ /dev/null @@ -1,136 +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/settingsobject.h" -#include "include/setting.h" - -#include - -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 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 deleted file mode 100644 index 53fca025..00000000 --- a/depends/settings/src/stubkeyring.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* Copyright 2013 MultiMC Contributors - * - * Authors: Orochimarufan - * - * 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 - -// 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 deleted file mode 100644 index 1f4b1be0..00000000 --- a/depends/settings/src/stubkeyring.h +++ /dev/null @@ -1,47 +0,0 @@ -/* Copyright 2013 MultiMC Contributors - * - * Authors: Orochimarufan - * - * 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 - -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; -}; -- cgit v1.2.3