From 32b3ed0a1362a4b0798ad71fac3450fb77cb7e41 Mon Sep 17 00:00:00 2001 From: Thomas Groman Date: Thu, 19 Sep 2019 00:41:48 -0700 Subject: merged from 0.6.7 codebase --- api/logic/settings/INIFile.cpp | 204 ++++++++++--------- api/logic/settings/INIFile.h | 18 +- api/logic/settings/INIFile_test.cpp | 97 ++++----- api/logic/settings/INISettingsObject.cpp | 110 +++++------ api/logic/settings/INISettingsObject.h | 50 ++--- api/logic/settings/OverrideSetting.cpp | 30 +-- api/logic/settings/OverrideSetting.h | 20 +- api/logic/settings/PassthroughSetting.cpp | 60 +++--- api/logic/settings/PassthroughSetting.h | 20 +- api/logic/settings/Setting.cpp | 34 ++-- api/logic/settings/Setting.h | 148 +++++++------- api/logic/settings/SettingsObject.cpp | 142 +++++++------- api/logic/settings/SettingsObject.h | 314 +++++++++++++++--------------- 13 files changed, 630 insertions(+), 617 deletions(-) (limited to 'api/logic/settings') diff --git a/api/logic/settings/INIFile.cpp b/api/logic/settings/INIFile.cpp index 9e97f861..ff6d5cf3 100644 --- a/api/logic/settings/INIFile.cpp +++ b/api/logic/settings/INIFile.cpp @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,124 +28,136 @@ INIFile::INIFile() QString INIFile::unescape(QString orig) { - QString out; - QChar prev = 0; - for(auto c: orig) - { - if(prev == '\\') - { - if(c == 'n') - out += '\n'; - else if (c == 't') - out += '\t'; - else - out += c; - prev = 0; - } - else - { - if(c == '\\') - { - prev = c; - continue; - } - out += c; - prev = 0; - } - } - return out; + QString out; + QChar prev = 0; + for(auto c: orig) + { + if(prev == '\\') + { + if(c == 'n') + out += '\n'; + else if(c == 't') + out += '\t'; + else if(c == '#') + out += '#'; + else + out += c; + prev = 0; + } + else + { + if(c == '\\') + { + prev = c; + continue; + } + out += c; + prev = 0; + } + } + return out; } QString INIFile::escape(QString orig) { - QString out; - for(auto c: orig) - { - if(c == '\n') - out += "\\n"; - else if (c == '\t') - out += "\\t"; - else if(c == '\\') - out += "\\\\"; - else - out += c; - } - return out; + QString out; + for(auto c: orig) + { + if(c == '\n') + out += "\\n"; + else if (c == '\t') + out += "\\t"; + else if(c == '\\') + out += "\\\\"; + else if(c == '#') + out += "\\#"; + else + out += c; + } + return out; } bool INIFile::saveFile(QString fileName) { - QByteArray outArray; - for (Iterator iter = begin(); iter != end(); iter++) - { - QString value = iter.value().toString(); - value = escape(value); - outArray.append(iter.key().toUtf8()); - outArray.append('='); - outArray.append(value.toUtf8()); - outArray.append('\n'); - } - - try - { - FS::write(fileName, outArray); - } - catch (Exception & e) - { - qCritical() << e.what(); - return false; - } - - return true; + QByteArray outArray; + for (Iterator iter = begin(); iter != end(); iter++) + { + QString value = iter.value().toString(); + value = escape(value); + outArray.append(iter.key().toUtf8()); + outArray.append('='); + outArray.append(value.toUtf8()); + outArray.append('\n'); + } + + try + { + FS::write(fileName, outArray); + } + catch (const Exception &e) + { + qCritical() << e.what(); + return false; + } + + 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; + 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; + 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. + int commentIndex = 0; + QString line = lineRaw; + // Search for comments until no more escaped # are available + while((commentIndex = line.indexOf('#', commentIndex + 1)) != -1) { + if(commentIndex > 0 && line.at(commentIndex - 1) == '\\') { + continue; + } + line = line.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); + if (!this->contains(key)) + return def; + else + return this->operator[](key); } void INIFile::set(QString key, QVariant val) { - this->operator[](key) = val; + this->operator[](key) = val; } diff --git a/api/logic/settings/INIFile.h b/api/logic/settings/INIFile.h index f0c63d3c..9406f7bd 100644 --- a/api/logic/settings/INIFile.h +++ b/api/logic/settings/INIFile.h @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,14 +25,14 @@ class MULTIMC_LOGIC_EXPORT INIFile : public QMap { public: - explicit INIFile(); + explicit INIFile(); - bool loadFile(QByteArray file); - bool loadFile(QString fileName); - bool saveFile(QString fileName); + 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); - static QString unescape(QString orig); - static QString escape(QString orig); + QVariant get(QString key, QVariant def) const; + void set(QString key, QVariant val); + static QString unescape(QString orig); + static QString escape(QString orig); }; diff --git a/api/logic/settings/INIFile_test.cpp b/api/logic/settings/INIFile_test.cpp index b3ae7375..08c2155e 100644 --- a/api/logic/settings/INIFile_test.cpp +++ b/api/logic/settings/INIFile_test.cpp @@ -5,56 +5,57 @@ class IniFileTest : public QObject { - Q_OBJECT + Q_OBJECT private slots: - void initTestCase() - { - - } - void cleanupTestCase() - { - - } - - void test_Escape_data() - { - QTest::addColumn("through"); - - QTest::newRow("unix path") << "/abc/def/ghi/jkl"; - QTest::newRow("windows path") << "C:\\Program files\\terrible\\name\\of something\\"; - QTest::newRow("Plain text") << "Lorem ipsum dolor sit amet."; - QTest::newRow("Escape sequences") << "Lorem\n\t\n\\n\\tAAZ\nipsum dolor\n\nsit amet."; - QTest::newRow("Escape sequences 2") << "\"\n\n\""; - } - void test_Escape() - { - QFETCH(QString, through); - - QString there = INIFile::escape(through); - QString back = INIFile::unescape(there); - - QCOMPARE(back, through); - } - - void test_SaveLoad() - { - QString a = "a"; - QString b = "a\nb\t\n\\\\\\C:\\Program files\\terrible\\name\\of something\\"; - QString filename = "test_SaveLoad.ini"; - - // save - INIFile f; - f.set("a", a); - f.set("b", b); - f.saveFile(filename); - - // load - INIFile f2; - f2.loadFile(filename); - QCOMPARE(a, f2.get("a","NOT SET").toString()); - QCOMPARE(b, f2.get("b","NOT SET").toString()); - } + void initTestCase() + { + + } + void cleanupTestCase() + { + + } + + void test_Escape_data() + { + QTest::addColumn("through"); + + QTest::newRow("unix path") << "/abc/def/ghi/jkl"; + QTest::newRow("windows path") << "C:\\Program files\\terrible\\name\\of something\\"; + QTest::newRow("Plain text") << "Lorem ipsum dolor sit amet."; + QTest::newRow("Escape sequences") << "Lorem\n\t\n\\n\\tAAZ\nipsum dolor\n\nsit amet."; + QTest::newRow("Escape sequences 2") << "\"\n\n\""; + QTest::newRow("Hashtags") << "some data#something"; + } + void test_Escape() + { + QFETCH(QString, through); + + QString there = INIFile::escape(through); + QString back = INIFile::unescape(there); + + QCOMPARE(back, through); + } + + void test_SaveLoad() + { + QString a = "a"; + QString b = "a\nb\t\n\\\\\\C:\\Program files\\terrible\\name\\of something\\#thisIsNotAComment"; + QString filename = "test_SaveLoad.ini"; + + // save + INIFile f; + f.set("a", a); + f.set("b", b); + f.saveFile(filename); + + // load + INIFile f2; + f2.loadFile(filename); + QCOMPARE(a, f2.get("a","NOT SET").toString()); + QCOMPARE(b, f2.get("b","NOT SET").toString()); + } }; QTEST_GUILESS_MAIN(IniFileTest) diff --git a/api/logic/settings/INISettingsObject.cpp b/api/logic/settings/INISettingsObject.cpp index ff2cee31..f04a66b5 100644 --- a/api/logic/settings/INISettingsObject.cpp +++ b/api/logic/settings/INISettingsObject.cpp @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,91 +17,91 @@ #include "Setting.h" INISettingsObject::INISettingsObject(const QString &path, QObject *parent) - : SettingsObject(parent) + : SettingsObject(parent) { - m_filePath = path; - m_ini.loadFile(path); + m_filePath = path; + m_ini.loadFile(path); } void INISettingsObject::setFilePath(const QString &filePath) { - m_filePath = filePath; + m_filePath = filePath; } bool INISettingsObject::reload() { - return m_ini.loadFile(m_filePath) && SettingsObject::reload(); + return m_ini.loadFile(m_filePath) && SettingsObject::reload(); } void INISettingsObject::suspendSave() { - m_suspendSave = true; + m_suspendSave = true; } void INISettingsObject::resumeSave() { - m_suspendSave = false; - if(m_doSave) - { - m_ini.saveFile(m_filePath); - } + m_suspendSave = false; + if(m_doSave) + { + m_ini.saveFile(m_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); - } - doSave(); - } + 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); + } + doSave(); + } } void INISettingsObject::doSave() { - if(m_suspendSave) - { - m_doSave = true; - } - else - { - m_ini.saveFile(m_filePath); - } + if(m_suspendSave) + { + m_doSave = true; + } + else + { + 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); - doSave(); - } + // 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); + doSave(); + } } 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(); + // 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/api/logic/settings/INISettingsObject.h b/api/logic/settings/INISettingsObject.h index 111215e6..c8ea99ca 100644 --- a/api/logic/settings/INISettingsObject.h +++ b/api/logic/settings/INISettingsObject.h @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,39 +28,39 @@ */ class MULTIMC_LOGIC_EXPORT INISettingsObject : public SettingsObject { - Q_OBJECT + Q_OBJECT public: - explicit INISettingsObject(const QString &path, QObject *parent = 0); + 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 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); + /*! + * \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); - bool reload() override; + bool reload() override; - void suspendSave() override; - void resumeSave() override; + void suspendSave() override; + void resumeSave() override; protected slots: - virtual void changeSetting(const Setting &setting, QVariant value) override; - virtual void resetSetting(const Setting &setting) override; + virtual void changeSetting(const Setting &setting, QVariant value) override; + virtual void resetSetting(const Setting &setting) override; protected: - virtual QVariant retrieveValue(const Setting &setting) override; - void doSave(); + virtual QVariant retrieveValue(const Setting &setting) override; + void doSave(); protected: - INIFile m_ini; - QString m_filePath; + INIFile m_ini; + QString m_filePath; }; diff --git a/api/logic/settings/OverrideSetting.cpp b/api/logic/settings/OverrideSetting.cpp index a3d48e03..1efdebac 100644 --- a/api/logic/settings/OverrideSetting.cpp +++ b/api/logic/settings/OverrideSetting.cpp @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,39 +16,39 @@ #include "OverrideSetting.h" OverrideSetting::OverrideSetting(std::shared_ptr other, std::shared_ptr gate) - : Setting(other->configKeys(), QVariant()) + : Setting(other->configKeys(), QVariant()) { - Q_ASSERT(other); - Q_ASSERT(gate); - m_other = other; - m_gate = gate; + Q_ASSERT(other); + Q_ASSERT(gate); + m_other = other; + m_gate = gate; } bool OverrideSetting::isOverriding() const { - return m_gate->get().toBool(); + return m_gate->get().toBool(); } QVariant OverrideSetting::defValue() const { - return m_other->get(); + return m_other->get(); } QVariant OverrideSetting::get() const { - if(isOverriding()) - { - return Setting::get(); - } - return m_other->get(); + if(isOverriding()) + { + return Setting::get(); + } + return m_other->get(); } void OverrideSetting::reset() { - Setting::reset(); + Setting::reset(); } void OverrideSetting::set(QVariant value) { - Setting::set(value); + Setting::set(value); } diff --git a/api/logic/settings/OverrideSetting.h b/api/logic/settings/OverrideSetting.h index f2cbc5dc..f99caff1 100644 --- a/api/logic/settings/OverrideSetting.h +++ b/api/logic/settings/OverrideSetting.h @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,19 +28,19 @@ */ class OverrideSetting : public Setting { - Q_OBJECT + Q_OBJECT public: - explicit OverrideSetting(std::shared_ptr overriden, std::shared_ptr gate); + explicit OverrideSetting(std::shared_ptr overriden, std::shared_ptr gate); - virtual QVariant defValue() const; - virtual QVariant get() const; - virtual void set (QVariant value); - virtual void reset(); + virtual QVariant defValue() const; + virtual QVariant get() const; + virtual void set (QVariant value); + virtual void reset(); private: - bool isOverriding() const; + bool isOverriding() const; protected: - std::shared_ptr m_other; - std::shared_ptr m_gate; + std::shared_ptr m_other; + std::shared_ptr m_gate; }; diff --git a/api/logic/settings/PassthroughSetting.cpp b/api/logic/settings/PassthroughSetting.cpp index 5da5d11c..beee4152 100644 --- a/api/logic/settings/PassthroughSetting.cpp +++ b/api/logic/settings/PassthroughSetting.cpp @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,54 +16,54 @@ #include "PassthroughSetting.h" PassthroughSetting::PassthroughSetting(std::shared_ptr other, std::shared_ptr gate) - : Setting(other->configKeys(), QVariant()) + : Setting(other->configKeys(), QVariant()) { - Q_ASSERT(other); - m_other = other; - m_gate = gate; + Q_ASSERT(other); + m_other = other; + m_gate = gate; } bool PassthroughSetting::isOverriding() const { - if(!m_gate) - { - return false; - } - return m_gate->get().toBool(); + if(!m_gate) + { + return false; + } + return m_gate->get().toBool(); } QVariant PassthroughSetting::defValue() const { - if(isOverriding()) - { - return m_other->get(); - } - return m_other->defValue(); + if(isOverriding()) + { + return m_other->get(); + } + return m_other->defValue(); } QVariant PassthroughSetting::get() const { - if(isOverriding()) - { - return Setting::get(); - } - return m_other->get(); + if(isOverriding()) + { + return Setting::get(); + } + return m_other->get(); } void PassthroughSetting::reset() { - if(isOverriding()) - { - Setting::reset(); - } - m_other->reset(); + if(isOverriding()) + { + Setting::reset(); + } + m_other->reset(); } void PassthroughSetting::set(QVariant value) { - if(isOverriding()) - { - Setting::set(value); - } - m_other->set(value); + if(isOverriding()) + { + Setting::set(value); + } + m_other->set(value); } diff --git a/api/logic/settings/PassthroughSetting.h b/api/logic/settings/PassthroughSetting.h index ee844da4..00212072 100644 --- a/api/logic/settings/PassthroughSetting.h +++ b/api/logic/settings/PassthroughSetting.h @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,19 +27,19 @@ */ class PassthroughSetting : public Setting { - Q_OBJECT + Q_OBJECT public: - explicit PassthroughSetting(std::shared_ptr overriden, std::shared_ptr gate); + explicit PassthroughSetting(std::shared_ptr overriden, std::shared_ptr gate); - virtual QVariant defValue() const; - virtual QVariant get() const; - virtual void set (QVariant value); - virtual void reset(); + virtual QVariant defValue() const; + virtual QVariant get() const; + virtual void set (QVariant value); + virtual void reset(); private: - bool isOverriding() const; + bool isOverriding() const; protected: - std::shared_ptr m_other; - std::shared_ptr m_gate; + std::shared_ptr m_other; + std::shared_ptr m_gate; }; diff --git a/api/logic/settings/Setting.cpp b/api/logic/settings/Setting.cpp index fa0041e0..419dfa2f 100644 --- a/api/logic/settings/Setting.cpp +++ b/api/logic/settings/Setting.cpp @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,37 +17,37 @@ #include "settings/SettingsObject.h" Setting::Setting(QStringList synonyms, QVariant defVal) - : QObject(), m_synonyms(synonyms), m_defVal(defVal) + : QObject(), m_synonyms(synonyms), 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; - } + 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; + return m_defVal; } void Setting::set(QVariant value) { - emit SettingChanged(*this, value); + emit SettingChanged(*this, value); } void Setting::reset() { - emit settingReset(*this); + emit settingReset(*this); } diff --git a/api/logic/settings/Setting.h b/api/logic/settings/Setting.h index 3edea7be..9d827c72 100644 --- a/api/logic/settings/Setting.h +++ b/api/logic/settings/Setting.h @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,91 +29,91 @@ class SettingsObject; */ class MULTIMC_LOGIC_EXPORT Setting : public QObject { - Q_OBJECT + Q_OBJECT public: - /** - * Construct a Setting - * - * Synonyms are all the possible names used in the settings object, in order of preference. - * First synonym is the ID, which identifies the setting in MultiMC. - * - * defVal is the default value that will be returned when the settings object - * doesn't have any value for this setting. - */ - explicit Setting(QStringList synonyms, QVariant defVal = QVariant()); + /** + * Construct a Setting + * + * Synonyms are all the possible names used in the settings object, in order of preference. + * First synonym is the ID, which identifies the setting in MultiMC. + * + * defVal is the default value that will be returned when the settings object + * doesn't have any value for this setting. + */ + explicit Setting(QStringList synonyms, 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 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 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 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 default value. - * \return The default value of this setting. - */ - virtual QVariant defValue() const; + /*! + * \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 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); + /*! + * \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 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(); + /*! + * \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; + friend class SettingsObject; + SettingsObject * m_storage; + QStringList m_synonyms; + QVariant m_defVal; }; diff --git a/api/logic/settings/SettingsObject.cpp b/api/logic/settings/SettingsObject.cpp index 87a8c2a8..b4fc5a68 100644 --- a/api/logic/settings/SettingsObject.cpp +++ b/api/logic/settings/SettingsObject.cpp @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,116 +27,116 @@ SettingsObject::SettingsObject(QObject *parent) : QObject(parent) SettingsObject::~SettingsObject() { - m_settings.clear(); + m_settings.clear(); } std::shared_ptr SettingsObject::registerOverride(std::shared_ptr original, - std::shared_ptr gate) + std::shared_ptr gate) { - if (contains(original->id())) - { - qCritical() << QString("Failed to register setting %1. ID already exists.") - .arg(original->id()); - return nullptr; // Fail - } - auto override = std::make_shared(original, gate); - override->m_storage = this; - connectSignals(*override); - m_settings.insert(override->id(), override); - return override; + if (contains(original->id())) + { + qCritical() << QString("Failed to register setting %1. ID already exists.") + .arg(original->id()); + return nullptr; // Fail + } + auto override = std::make_shared(original, gate); + override->m_storage = this; + connectSignals(*override); + m_settings.insert(override->id(), override); + return override; } std::shared_ptr SettingsObject::registerPassthrough(std::shared_ptr original, - std::shared_ptr gate) + std::shared_ptr gate) { - if (contains(original->id())) - { - qCritical() << QString("Failed to register setting %1. ID already exists.") - .arg(original->id()); - return nullptr; // Fail - } - auto passthrough = std::make_shared(original, gate); - passthrough->m_storage = this; - connectSignals(*passthrough); - m_settings.insert(passthrough->id(), passthrough); - return passthrough; + if (contains(original->id())) + { + qCritical() << QString("Failed to register setting %1. ID already exists.") + .arg(original->id()); + return nullptr; // Fail + } + auto passthrough = std::make_shared(original, gate); + passthrough->m_storage = this; + connectSignals(*passthrough); + m_settings.insert(passthrough->id(), passthrough); + return passthrough; } std::shared_ptr SettingsObject::registerSetting(QStringList synonyms, QVariant defVal) { - if (synonyms.empty()) - return nullptr; - if (contains(synonyms.first())) - { - qCritical() << QString("Failed to register setting %1. ID already exists.") - .arg(synonyms.first()); - return nullptr; // Fail - } - auto setting = std::make_shared(synonyms, defVal); - setting->m_storage = this; - connectSignals(*setting); - m_settings.insert(setting->id(), setting); - return setting; + if (synonyms.empty()) + return nullptr; + if (contains(synonyms.first())) + { + qCritical() << QString("Failed to register setting %1. ID already exists.") + .arg(synonyms.first()); + return nullptr; // Fail + } + auto setting = std::make_shared(synonyms, defVal); + setting->m_storage = this; + connectSignals(*setting); + m_settings.insert(setting->id(), setting); + return setting; } 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; + // Make sure there is a setting with the given ID. + if (!m_settings.contains(id)) + return NULL; - return m_settings[id]; + return m_settings[id]; } QVariant SettingsObject::get(const QString &id) const { - auto setting = getSetting(id); - return (setting ? setting->get() : QVariant()); + auto setting = getSetting(id); + return (setting ? setting->get() : QVariant()); } bool SettingsObject::set(const QString &id, QVariant value) { - auto setting = getSetting(id); - if (!setting) - { - qCritical() << QString("Error changing setting %1. Setting doesn't exist.").arg(id); - return false; - } - else - { - setting->set(value); - return true; - } + auto setting = getSetting(id); + if (!setting) + { + qCritical() << QString("Error changing setting %1. Setting doesn't exist.").arg(id); + return false; + } + else + { + setting->set(value); + return true; + } } void SettingsObject::reset(const QString &id) const { - auto setting = getSetting(id); - if (setting) - setting->reset(); + auto setting = getSetting(id); + if (setting) + setting->reset(); } bool SettingsObject::contains(const QString &id) { - return m_settings.contains(id); + return m_settings.contains(id); } bool SettingsObject::reload() { - for (auto setting : m_settings.values()) - { - setting->set(setting->get()); - } - return true; + for (auto setting : m_settings.values()) + { + setting->set(setting->get()); + } + return true; } 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(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 &))); + connect(&setting, SIGNAL(settingReset(Setting)), SLOT(resetSetting(const Setting &))); + connect(&setting, SIGNAL(settingReset(Setting)), SIGNAL(settingReset(const Setting &))); } diff --git a/api/logic/settings/SettingsObject.h b/api/logic/settings/SettingsObject.h index 8582d8ad..11dc50ca 100644 --- a/api/logic/settings/SettingsObject.h +++ b/api/logic/settings/SettingsObject.h @@ -1,4 +1,4 @@ -/* Copyright 2013-2018 MultiMC Contributors +/* Copyright 2013-2019 MultiMC Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,173 +42,173 @@ typedef std::shared_ptr SettingsObjectPtr; */ class MULTIMC_LOGIC_EXPORT SettingsObject : public QObject { - Q_OBJECT + Q_OBJECT public: - class Lock - { - public: - Lock(SettingsObjectPtr locked) - :m_locked(locked) - { - m_locked->suspendSave(); - } - ~Lock() - { - m_locked->resumeSave(); - } - private: - SettingsObjectPtr m_locked; - }; + class Lock + { + public: + Lock(SettingsObjectPtr locked) + :m_locked(locked) + { + m_locked->suspendSave(); + } + ~Lock() + { + m_locked->resumeSave(); + } + private: + SettingsObjectPtr m_locked; + }; public: - explicit SettingsObject(QObject *parent = 0); - virtual ~SettingsObject(); - /*! - * Registers an override setting for the given original setting in this settings object - * gate decides if the passthrough (true) or the original (false) is used for value - * - * 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, std::shared_ptr gate); - - /*! - * Registers a passthorugh setting for the given original setting in this settings object - * gate decides if the passthrough (true) or the original (false) is used for value - * - * 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 registerPassthrough(std::shared_ptr original, std::shared_ptr gate); - - /*! - * 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 - * \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); - - /*! - * \brief Reloads the settings and emit signals for changed settings - * \return True if reloading was successful - */ - virtual bool reload(); - - virtual void suspendSave() = 0; - virtual void resumeSave() = 0; + explicit SettingsObject(QObject *parent = 0); + virtual ~SettingsObject(); + /*! + * Registers an override setting for the given original setting in this settings object + * gate decides if the passthrough (true) or the original (false) is used for value + * + * 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, std::shared_ptr gate); + + /*! + * Registers a passthorugh setting for the given original setting in this settings object + * gate decides if the passthrough (true) or the original (false) is used for value + * + * 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 registerPassthrough(std::shared_ptr original, std::shared_ptr gate); + + /*! + * 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 + * \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); + + /*! + * \brief Reloads the settings and emit signals for changed settings + * \return True if reloading was successful + */ + virtual bool reload(); + + virtual void suspendSave() = 0; + virtual void resumeSave() = 0; 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); + /*! + * \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; + /*! + * \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 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; + /*! + * \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; + friend class Setting; private: - QMap> m_settings; + QMap> m_settings; protected: - bool m_suspendSave = false; - bool m_doSave = false; + bool m_suspendSave = false; + bool m_doSave = false; }; -- cgit v1.2.3