diff options
author | Andrew <forkk@forkk.net> | 2013-02-25 13:24:46 -0600 |
---|---|---|
committer | Andrew <forkk@forkk.net> | 2013-02-25 13:24:46 -0600 |
commit | 498225debdb22d83e591635dbd172cca12476279 (patch) | |
tree | e26d8f8de3bed7d22cfc25ee010c999b9481ec87 | |
parent | a7111b132873e0f1e00221b2ad734454d69889e3 (diff) | |
download | MultiMC-498225debdb22d83e591635dbd172cca12476279.tar MultiMC-498225debdb22d83e591635dbd172cca12476279.tar.gz MultiMC-498225debdb22d83e591635dbd172cca12476279.tar.lz MultiMC-498225debdb22d83e591635dbd172cca12476279.tar.xz MultiMC-498225debdb22d83e591635dbd172cca12476279.zip |
Rewrote the settings system. It may still need some work.
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | data/appsettings.cpp | 70 | ||||
-rw-r--r-- | data/appsettings.h (renamed from libsettings/src/appsettings.cpp) | 30 | ||||
-rw-r--r-- | data/loginresponse.cpp | 36 | ||||
-rw-r--r-- | data/loginresponse.h | 59 | ||||
-rw-r--r-- | data/userinfo.cpp | 20 | ||||
-rw-r--r-- | data/userinfo.h | 8 | ||||
-rw-r--r-- | data/version.h | 27 | ||||
-rw-r--r-- | gui/mainwindow.cpp | 21 | ||||
-rw-r--r-- | gui/settingsdialog.cpp | 82 | ||||
-rw-r--r-- | gui/settingsdialog.h | 6 | ||||
-rw-r--r-- | libinstance/include/instance.h | 36 | ||||
-rw-r--r-- | libinstance/src/instance.cpp | 2 | ||||
-rw-r--r-- | libsettings/CMakeLists.txt | 10 | ||||
-rw-r--r-- | libsettings/include/appsettings.h | 119 | ||||
-rw-r--r-- | libsettings/include/basicsettingsobject.h | 44 | ||||
-rw-r--r-- | libsettings/include/setting.h | 101 | ||||
-rw-r--r-- | libsettings/include/settingsobject.h | 165 | ||||
-rw-r--r-- | libsettings/src/basicsettingsobject.cpp | 43 | ||||
-rw-r--r-- | libsettings/src/setting.cpp | 42 | ||||
-rw-r--r-- | libsettings/src/settingsobject.cpp | 124 | ||||
-rw-r--r-- | main.cpp | 2 | ||||
-rw-r--r-- | tasks/logintask.cpp | 4 |
23 files changed, 782 insertions, 271 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c73e0ae..754c073e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -166,6 +166,7 @@ gui/taskdialog.h data/version.h data/userinfo.h data/loginresponse.h +data/appsettings.h data/plugin/pluginmanager.h @@ -192,6 +193,7 @@ main.cpp data/version.cpp data/userinfo.cpp data/loginresponse.cpp +data/appsettings.cpp data/plugin/pluginmanager.cpp diff --git a/data/appsettings.cpp b/data/appsettings.cpp new file mode 100644 index 00000000..bdb66535 --- /dev/null +++ b/data/appsettings.cpp @@ -0,0 +1,70 @@ +/* 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 "appsettings.h" + +#include <setting.h> + +#include <QPoint> +#include <QColor> + +AppSettings *settings; + +AppSettings::AppSettings(QObject *parent) : + BasicSettingsObject(parent) +{ + // Updates + registerSetting(new Setting("UseDevBuilds", false)); + registerSetting(new Setting("AutoUpdate", true)); + + // Folders + registerSetting(new Setting("InstanceDir", "instances")); + registerSetting(new Setting("CentralModsDir", "mods")); + registerSetting(new Setting("LWJGLDir", "lwjgl")); + + // Console + registerSetting(new Setting("ShowConsole", true)); + registerSetting(new Setting("AutoCloseConsole", true)); + + // Toolbar settings + registerSetting(new Setting("InstanceToolbarVisible", true)); + registerSetting(new Setting("InstanceToolbarPosition", QPoint())); + + // Console Colors + registerSetting(new Setting("SysMessageColor", QColor(Qt::blue))); + registerSetting(new Setting("StdOutColor", QColor(Qt::black))); + registerSetting(new Setting("StdErrColor", QColor(Qt::red))); + + // Window Size + registerSetting(new Setting("LaunchCompatMode", false)); + registerSetting(new Setting("LaunchMaximized", false)); + registerSetting(new Setting("MinecraftWinWidth", 854)); + registerSetting(new Setting("MinecraftWinHeight", 480)); + + // Auto login + registerSetting(new Setting("AutoLogin", false)); + + // Memory + registerSetting(new Setting("MinMemAlloc", 512)); + registerSetting(new Setting("MaxMemAlloc", 1024)); + + // Java Settings + registerSetting(new Setting("JavaPath", "java")); + registerSetting(new Setting("JvmArgs", "")); + + // Custom Commands + registerSetting(new Setting("PreLaunchCommand", "")); + registerSetting(new Setting("PostExitCommand", "")); +} diff --git a/libsettings/src/appsettings.cpp b/data/appsettings.h index 8fe9b8ad..fc0098fc 100644 --- a/libsettings/src/appsettings.cpp +++ b/data/appsettings.h @@ -13,28 +13,20 @@ * limitations under the License. */ -#include "include/appsettings.h" +#ifndef APPSETTINGS_H +#define APPSETTINGS_H -AppSettings* settings; +#include <QObject> -SettingsBase::SettingsBase(QObject *parent) : - QObject(parent) -{ - -} +#include <basicsettingsobject.h> -AppSettings::AppSettings(QObject *parent) : - SettingsBase(parent) +class AppSettings : public BasicSettingsObject { - -} + Q_OBJECT +public: + explicit AppSettings(QObject *parent = 0); +}; -QVariant AppSettings::getValue(const QString& name, QVariant defVal) const -{ - return config.value(name, defVal); -} +extern AppSettings *settings; -void AppSettings::setValue(const QString& name, QVariant val) -{ - config.setValue(name, val); -} +#endif // APPSETTINGS_H diff --git a/data/loginresponse.cpp b/data/loginresponse.cpp index 44bc80eb..99a618ad 100644 --- a/data/loginresponse.cpp +++ b/data/loginresponse.cpp @@ -19,51 +19,51 @@ LoginResponse::LoginResponse(const QString& username, const QString& sessionID, qint64 latestVersion, QObject *parent) : QObject(parent) { - this->username = username; - this->sessionID = sessionID; - this->latestVersion = latestVersion; + this->m_username = username; + this->m_sessionID = sessionID; + this->m_latestVersion = latestVersion; } LoginResponse::LoginResponse() { - this->username = ""; - this->sessionID = ""; - this->latestVersion = 0; + this->m_username = ""; + this->m_sessionID = ""; + this->m_latestVersion = 0; } LoginResponse::LoginResponse(const LoginResponse &other) { - this->username = other.getUsername(); - this->sessionID = other.getSessionID(); - this->latestVersion = other.getLatestVersion(); + this->m_username = other.username(); + this->m_sessionID = other.sessionID(); + this->m_latestVersion = other.latestVersion(); } -QString LoginResponse::getUsername() const +QString LoginResponse::username() const { - return username; + return m_username; } void LoginResponse::setUsername(const QString& username) { - this->username = username; + this->m_username = username; } -QString LoginResponse::getSessionID() const +QString LoginResponse::sessionID() const { - return sessionID; + return m_sessionID; } void LoginResponse::setSessionID(const QString& sessionID) { - this->sessionID = sessionID; + this->m_sessionID = sessionID; } -qint64 LoginResponse::getLatestVersion() const +qint64 LoginResponse::latestVersion() const { - return latestVersion; + return m_latestVersion; } void LoginResponse::setLatestVersion(qint64 v) { - this->latestVersion = v; + this->m_latestVersion = v; } diff --git a/data/loginresponse.h b/data/loginresponse.h index bcb77d56..c30897c3 100644 --- a/data/loginresponse.h +++ b/data/loginresponse.h @@ -18,28 +18,75 @@ #include <QObject> +/*! + * \brief The LoginResponse class represents a response received from Minecraft's login servers. + */ class LoginResponse : public QObject { Q_OBJECT public: + /*! + * \brief Creates a new instance of the LoginResponse class. + * \param username The user's username. + * \param sessionID The user's session ID. + * \param latestVersion The latest version of Minecraft. + * \param parent The parent object. + */ explicit LoginResponse(const QString &username, const QString &sessionID, qint64 latestVersion, QObject *parent = 0); LoginResponse(); LoginResponse(const LoginResponse& other); - QString getUsername() const; + /*! + * \brief Gets the username. + * This one should go without saying. + * \return The username. + * \sa setUsername() + */ + QString username() const; + + /*! + * \brief setUsername Sets the username. + * \param username The new username. + * \sa username() + */ void setUsername(const QString& username); - QString getSessionID() const; + + /*! + * \brief Gets the session ID. + * \return The session ID. + * \sa setSessionID() + */ + QString sessionID() const; + + /*! + * \brief Sets the session ID. + * \param sessionID The new session ID. + * \sa sessionID() + */ void setSessionID(const QString& sessionID); - qint64 getLatestVersion() const; + + /*! + * \brief Gets the latest version. + * This is a value returned by the login servers when a user logs in. + * \return The latest version. + * \sa setLatestVersion() + */ + qint64 latestVersion() const; + + /*! + * \brief Sets the latest version. + * \param v The new latest version. + * \sa latestVersion() + */ void setLatestVersion(qint64 v); private: - QString username; - QString sessionID; - qint64 latestVersion; + QString m_username; + QString m_sessionID; + qint64 m_latestVersion; }; Q_DECLARE_METATYPE(LoginResponse) diff --git a/data/userinfo.cpp b/data/userinfo.cpp index 907f93a2..0bb5da11 100644 --- a/data/userinfo.cpp +++ b/data/userinfo.cpp @@ -18,32 +18,32 @@ UserInfo::UserInfo(const QString &username, const QString &password, QObject *parent) : QObject(parent) { - this->username = username; - this->password = password; + this->m_username = username; + this->m_password = password; } UserInfo::UserInfo(const UserInfo &other) { - this->username = other.username; - this->password = other.password; + this->m_username = other.m_username; + this->m_password = other.m_password; } -QString UserInfo::getUsername() const +QString UserInfo::username() const { - return username; + return m_username; } void UserInfo::setUsername(const QString &username) { - this->username = username; + this->m_username = username; } -QString UserInfo::getPassword() const +QString UserInfo::password() const { - return password; + return m_password; } void UserInfo::setPassword(const QString &password) { - this->password = password; + this->m_password = password; } diff --git a/data/userinfo.h b/data/userinfo.h index ccfc741e..486ce2a6 100644 --- a/data/userinfo.h +++ b/data/userinfo.h @@ -25,15 +25,15 @@ public: explicit UserInfo(const QString& username, const QString& password, QObject *parent = 0); explicit UserInfo(const UserInfo& other); - QString getUsername() const; + QString username() const; void setUsername(const QString& username); - QString getPassword() const; + QString password() const; void setPassword(const QString& password); protected: - QString username; - QString password; + QString m_username; + QString m_password; }; #endif // USERINFO_H diff --git a/data/version.h b/data/version.h index 321b1680..d5d276db 100644 --- a/data/version.h +++ b/data/version.h @@ -18,6 +18,9 @@ #include <QObject> +/*! + * \brief The Version class represents a MultiMC version number. + */ class Version : public QObject { Q_OBJECT @@ -27,11 +30,35 @@ public: Version(const Version& ver); + /*! + * \brief Converts the Version to a string. + * \return The version number in string format (major.minor.revision.build). + */ QString toString() const; + /*! + * \brief The major version number. + * For MultiMC 5, this will always be 5. + */ int major; + + /*! + * \brief The minor version number. + * This number is incremented when major features are added. + */ int minor; + + /*! + * \brief The revision number. + * This number is incremented for bugfixes and small features. + */ int revision; + + /*! + * \brief The build number. + * This number is automatically set by Jenkins. It is incremented every time + * a new build is run. + */ int build; static Version current; diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 19ff2108..ab46048a 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -31,7 +31,7 @@ #include "gui/taskdialog.h" #include "instancelist.h" -#include "appsettings.h" +#include "data/appsettings.h" #include "data/version.h" #include "tasks/logintask.h" @@ -43,14 +43,14 @@ void openInDefaultProgram(QString filename); MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), - instList(settings->getInstanceDir()) + instList(settings->get("InstanceDir").toString()) { ui->setupUi(this); setWindowTitle(QString("MultiMC %1").arg(Version::current.toString())); - - restoreGeometry(settings->getConfig().value("MainWindowGeometry", saveGeometry()).toByteArray()); - restoreState(settings->getConfig().value("MainWindowState", saveState()).toByteArray()); + // TODO: Make this work with the new settings system. +// restoreGeometry(settings->getConfig().value("MainWindowGeometry", saveGeometry()).toByteArray()); +// restoreState(settings->getConfig().value("MainWindowState", saveState()).toByteArray()); instList.loadList(); } @@ -68,7 +68,7 @@ void MainWindow::on_actionAddInstance_triggered() void MainWindow::on_actionViewInstanceFolder_triggered() { - openInDefaultProgram(settings->getInstanceDir()); + openInDefaultProgram(settings->get("InstanceDir").toString()); } void MainWindow::on_actionRefresh_triggered() @@ -78,7 +78,7 @@ void MainWindow::on_actionRefresh_triggered() void MainWindow::on_actionViewCentralModsFolder_triggered() { - openInDefaultProgram(settings->getCentralModsDir()); + openInDefaultProgram(settings->get("CentralModsDir").toString()); } void MainWindow::on_actionCheckUpdate_triggered() @@ -117,8 +117,9 @@ void MainWindow::on_mainToolBar_visibilityChanged(bool) void MainWindow::closeEvent(QCloseEvent *event) { // Save the window state and geometry. - settings->getConfig().setValue("MainWindowGeometry", saveGeometry()); - settings->getConfig().setValue("MainWindowState", saveState()); + // TODO: Make this work with the new settings system. +// settings->getConfig().setValue("MainWindowGeometry", saveGeometry()); +// settings->getConfig().setValue("MainWindowState", saveState()); QMainWindow::closeEvent(event); } @@ -159,7 +160,7 @@ void MainWindow::onLoginComplete(LoginResponse response) { QMessageBox::information(this, "Login Successful", QString("Logged in as %1 with session ID %2."). - arg(response.getUsername(), response.getSessionID())); + arg(response.username(), response.sessionID())); } void openInDefaultProgram(QString filename) diff --git a/gui/settingsdialog.cpp b/gui/settingsdialog.cpp index b3c42380..bc10560d 100644 --- a/gui/settingsdialog.cpp +++ b/gui/settingsdialog.cpp @@ -16,7 +16,7 @@ #include "settingsdialog.h" #include "ui_settingsdialog.h" -#include "appsettings.h" +#include "data/appsettings.h" #include <QFileDialog> #include <QMessageBox> @@ -87,95 +87,95 @@ void SettingsDialog::on_buttonBox_accepted() applySettings(settings); } -void SettingsDialog::applySettings(SettingsBase *s) +void SettingsDialog::applySettings(SettingsObject *s) { // Special cases // Warn about dev builds. if (!ui->devBuildsCheckBox->isChecked()) { - s->setUseDevBuilds(false); + s->set("UseDevBuilds", false); } - else if (!s->getUseDevBuilds()) + else if (!s->get("UseDevBuilds").toBool()) { int response = QMessageBox::question(this, "Development builds", "Development builds contain experimental features " "and may be unstable. Are you sure you want to enable them?"); if (response == QMessageBox::Yes) { - s->setUseDevBuilds(true); + s->set("UseDevBuilds", true); } } // Updates - s->setAutoUpdate(ui->autoUpdateCheckBox->isChecked()); + s->set("AutoUpdate", ui->autoUpdateCheckBox->isChecked()); // Folders // TODO: Offer to move instances to new instance folder. - s->setInstanceDir(ui->instDirTextBox->text()); - s->setCentralModsDir(ui->modsDirTextBox->text()); - s->setLWJGLDir(ui->lwjglDirTextBox->text()); + s->set("InstanceDir", ui->instDirTextBox->text()); + s->set("CentralModsDir", ui->modsDirTextBox->text()); + s->set("LWJGLDir", ui->lwjglDirTextBox->text()); // Console - s->setShowConsole(ui->showConsoleCheck->isChecked()); - s->setAutoCloseConsole(ui->autoCloseConsoleCheck->isChecked()); + s->set("ShowConsole", ui->showConsoleCheck->isChecked()); + s->set("AutoCloseConsole", ui->autoCloseConsoleCheck->isChecked()); // Window Size - s->setLaunchCompatMode(ui->compatModeCheckBox->isChecked()); - s->setLaunchMaximized(ui->maximizedCheckBox->isChecked()); - s->setMinecraftWinWidth(ui->windowWidthSpinBox->value()); - s->setMinecraftWinHeight(ui->windowHeightSpinBox->value()); + s->set("LaunchCompatMode", ui->compatModeCheckBox->isChecked()); + s->set("LaunchMaximized", ui->maximizedCheckBox->isChecked()); + s->set("MinecraftWinWidth", ui->windowWidthSpinBox->value()); + s->set("MinecraftWinHeight", ui->windowHeightSpinBox->value()); // Auto Login - s->setAutoLogin(ui->autoLoginCheckBox->isChecked()); + s->set("AutoLogin", ui->autoLoginCheckBox->isChecked()); // Memory - s->setMinMemAlloc(ui->minMemSpinBox->value()); - s->setMaxMemAlloc(ui->maxMemSpinBox->value()); + s->set("MinMemAlloc", ui->minMemSpinBox->value()); + s->set("MaxMemAlloc", ui->maxMemSpinBox->value()); // Java Settings - s->setJavaPath(ui->javaPathTextBox->text()); - s->setJvmArgs(ui->jvmArgsTextBox->text()); + s->set("JavaPath", ui->javaPathTextBox->text()); + s->set("JvmArgs", ui->jvmArgsTextBox->text()); // Custom Commands - s->setPreLaunchCommand(ui->preLaunchCmdTextBox->text()); - s->setPostExitCommand(ui->postExitCmdTextBox->text()); + s->set("PreLaunchCommand", ui->preLaunchCmdTextBox->text()); + s->set("PostExitCommand", ui->postExitCmdTextBox->text()); } -void SettingsDialog::loadSettings(SettingsBase *s) +void SettingsDialog::loadSettings(SettingsObject *s) { // Updates - ui->autoUpdateCheckBox->setChecked(s->getAutoUpdate()); - ui->devBuildsCheckBox->setChecked(s->getUseDevBuilds()); + ui->autoUpdateCheckBox->setChecked(s->get("AutoUpdate").toBool()); + ui->devBuildsCheckBox->setChecked(s->get("UseDevBuilds").toBool()); // Folders - ui->instDirTextBox->setText(s->getInstanceDir()); - ui->modsDirTextBox->setText(s->getCentralModsDir()); - ui->lwjglDirTextBox->setText(s->getLWJGLDir()); + ui->instDirTextBox->setText(s->get("InstanceDir").toString()); + ui->modsDirTextBox->setText(s->get("CentralModsDir").toString()); + ui->lwjglDirTextBox->setText(s->get("LWJGLDir").toString()); // Console - ui->showConsoleCheck->setChecked(s->getShowConsole()); - ui->autoCloseConsoleCheck->setChecked(s->getAutoCloseConsole()); + ui->showConsoleCheck->setChecked(s->get("ShowConsole").toBool()); + ui->autoCloseConsoleCheck->setChecked(s->get("AutoCloseConsole").toBool()); // Window Size - ui->compatModeCheckBox->setChecked(s->getLaunchCompatMode()); - ui->maximizedCheckBox->setChecked(s->getLaunchMaximized()); - ui->windowWidthSpinBox->setValue(s->getMinecraftWinWidth()); - ui->windowHeightSpinBox->setValue(s->getMinecraftWinHeight()); + ui->compatModeCheckBox->setChecked(s->get("LaunchCompatMode").toBool()); + ui->maximizedCheckBox->setChecked(s->get("LaunchMaximized").toBool()); + ui->windowWidthSpinBox->setValue(s->get("MinecraftWinWidth").toInt()); + ui->windowHeightSpinBox->setValue(s->get("MinecraftWinHeight").toInt()); // Auto Login - ui->autoLoginCheckBox->setChecked(s->getAutoLogin()); + ui->autoLoginCheckBox->setChecked(s->get("AutoLogin").toBool()); // Memory - ui->minMemSpinBox->setValue(s->getMinMemAlloc()); - ui->maxMemSpinBox->setValue(s->getMaxMemAlloc()); + ui->minMemSpinBox->setValue(s->get("MinMemAlloc").toInt()); + ui->maxMemSpinBox->setValue(s->get("MaxMemAlloc").toInt()); // Java Settings - ui->javaPathTextBox->setText(s->getJavaPath()); - ui->jvmArgsTextBox->setText(s->getJvmArgs()); + ui->javaPathTextBox->setText(s->get("JavaPath").toString()); + ui->jvmArgsTextBox->setText(s->get("JvmArgs").toString()); // Custom Commands - ui->preLaunchCmdTextBox->setText(s->getPreLaunchCommand()); - ui->postExitCmdTextBox->setText(s->getPostExitCommand()); + ui->preLaunchCmdTextBox->setText(s->get("PreLaunchCommand").toString()); + ui->postExitCmdTextBox->setText(s->get("PostExitCommand").toString()); } diff --git a/gui/settingsdialog.h b/gui/settingsdialog.h index e223237f..815aa11d 100644 --- a/gui/settingsdialog.h +++ b/gui/settingsdialog.h @@ -18,7 +18,7 @@ #include <QDialog> -class SettingsBase; +class SettingsObject; namespace Ui { class SettingsDialog; @@ -34,8 +34,8 @@ public: void updateCheckboxStuff(); - void applySettings(SettingsBase* s); - void loadSettings(SettingsBase* s); + void applySettings(SettingsObject *s); + void loadSettings(SettingsObject* s); private slots: void on_instDirBrowseBtn_clicked(); diff --git a/libinstance/include/instance.h b/libinstance/include/instance.h index 7b3c001d..b99d9953 100644 --- a/libinstance/include/instance.h +++ b/libinstance/include/instance.h @@ -19,17 +19,10 @@ #include <QObject> #include <QDateTime> -#include "appsettings.h" #include "inifile.h" #include "libinstance_config.h" -#define DEFINE_OVERRIDDEN_SETTING_ADVANCED(funcName, cfgEntryName, typeName) \ - typeName get ## funcName() const { return getField(cfgEntryName, settings->get ## funcName()).value<typeName>(); } - -#define DEFINE_OVERRIDDEN_SETTING(funcName, typeName) \ - DEFINE_OVERRIDDEN_SETTING_ADVANCED(funcName, STR_VAL(funcName), typeName) - class InstanceList; /*! @@ -40,7 +33,7 @@ class InstanceList; * To create a new instance type, create a new class inheriting from this class * and implement the pure virtual functions. */ -class LIBMMCINST_EXPORT Instance : public SettingsBase +class LIBMMCINST_EXPORT Instance : public QObject { Q_OBJECT public: @@ -261,27 +254,6 @@ public: //! Gets the path to the instance's modlist file. QString modListFile() const; - ////// Settings ////// - - //// Java Settings //// - DEFINE_OVERRIDDEN_SETTING_ADVANCED(JavaPath, JPATHKEY, QString) - DEFINE_OVERRIDDEN_SETTING(JvmArgs, QString) - - //// Custom Commands //// - DEFINE_OVERRIDDEN_SETTING(PreLaunchCommand, QString) - DEFINE_OVERRIDDEN_SETTING(PostExitCommand, QString) - - //// Memory //// - DEFINE_OVERRIDDEN_SETTING(MinMemAlloc, int) - DEFINE_OVERRIDDEN_SETTING(MaxMemAlloc, int) - - //// Auto login //// - DEFINE_OVERRIDDEN_SETTING(AutoLogin, bool) - - // This little guy here is to keep Qt Creator from being a dumbass and - // auto-indenting the lines below the macros. Seriously, it's really annoying. - ; - //////// OTHER FUNCTIONS //////// @@ -327,12 +299,6 @@ protected: */ virtual void setField(const QString &name, QVariant val); - // Overrides for SettingBase stuff. - virtual QVariant getValue(const QString &name, QVariant defVal = QVariant()) const - { return settings->getValue(name, defVal); } - virtual void setValue(const QString &name, QVariant val) - { setField(name, val); } - INIFile config; private: diff --git a/libinstance/src/instance.cpp b/libinstance/src/instance.cpp index c79c0213..b505ec86 100644 --- a/libinstance/src/instance.cpp +++ b/libinstance/src/instance.cpp @@ -20,7 +20,7 @@ #include "pathutils.h" Instance::Instance(const QString &rootDir, QObject *parent) : - SettingsBase(parent) + QObject(parent) { m_rootDir = rootDir; config.loadFile(PathCombine(rootDir, "instance.cfg")); diff --git a/libsettings/CMakeLists.txt b/libsettings/CMakeLists.txt index 2cf72365..4040032d 100644 --- a/libsettings/CMakeLists.txt +++ b/libsettings/CMakeLists.txt @@ -13,11 +13,17 @@ include_directories(${CMAKE_SOURCE_DIR}/libutil/include) SET(LIBSETTINGS_HEADERS include/libsettings_config.h -include/appsettings.h +include/settingsobject.h +include/setting.h + +include/basicsettingsobject.h ) SET(LIBSETTINGS_SOURCES -src/appsettings.cpp +src/settingsobject.cpp +src/setting.cpp + +src/basicsettingsobject.cpp ) # Set the include dir path. diff --git a/libsettings/include/appsettings.h b/libsettings/include/appsettings.h deleted file mode 100644 index 79515618..00000000 --- a/libsettings/include/appsettings.h +++ /dev/null @@ -1,119 +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. - */ - -#ifndef APPSETTINGS_H -#define APPSETTINGS_H - -#include <QObject> -#include <QSettings> -//#include <QColor> -#include <QPoint> - -#include <apputils.h> -#include <osutils.h> - -#include "libsettings_config.h" - -#if WINDOWS -#define JPATHKEY "JavaPathWindows" -#elif OSX -#define JPATHKEY "JavaPathOSX" -#else -#define JPATHKEY "JavaPathLinux" -#endif - -#define DEFINE_SETTING_ADVANCED(funcName, name, valType, defVal) \ - virtual valType get ## funcName() const { return getValue(name, defVal).value<valType>(); } \ - virtual void set ## funcName(valType value) { setValue(name, value); } - -#define DEFINE_SETTING(name, valType, defVal) \ - DEFINE_SETTING_ADVANCED(name, STR_VAL(name), valType, defVal) - - -class LIBMMCSETTINGS_EXPORT SettingsBase : public QObject -{ - Q_OBJECT -public: - explicit SettingsBase(QObject *parent = 0); - - // Updates - DEFINE_SETTING(UseDevBuilds, bool, false) - DEFINE_SETTING(AutoUpdate, bool, true) - - // Folders - DEFINE_SETTING(InstanceDir, QString, "instances") - DEFINE_SETTING(CentralModsDir, QString, "mods") - DEFINE_SETTING(LWJGLDir, QString, "lwjgl") - - // Console - DEFINE_SETTING(ShowConsole, bool, true) - DEFINE_SETTING(AutoCloseConsole, bool, true) - - // Toolbar settings - DEFINE_SETTING(InstanceToolbarVisible, bool, true) - DEFINE_SETTING(InstanceToolbarPosition, QPoint, QPoint()) - - // Console Colors - // Currently commented out because QColor is a part of QtGUI -// DEFINE_SETTING(SysMessageColor, QColor, QColor(Qt::blue)) -// DEFINE_SETTING(StdOutColor, QColor, QColor(Qt::black)) -// DEFINE_SETTING(StdErrColor, QColor, QColor(Qt::red)) - - // Window Size - DEFINE_SETTING(LaunchCompatMode, bool, false) - DEFINE_SETTING(LaunchMaximized, bool, false) - DEFINE_SETTING(MinecraftWinWidth, int, 854) - DEFINE_SETTING(MinecraftWinHeight, int, 480) - - // Auto login - DEFINE_SETTING(AutoLogin, bool, false) - - // Memory - DEFINE_SETTING(MinMemAlloc, int, 512) - DEFINE_SETTING(MaxMemAlloc, int, 1024) - - // Java Settings - DEFINE_SETTING_ADVANCED(JavaPath, JPATHKEY, QString, "java") - DEFINE_SETTING(JvmArgs, QString, "") - - // Custom Commands - DEFINE_SETTING(PreLaunchCommand, QString, "") - DEFINE_SETTING(PostExitCommand, QString, "") - - virtual QVariant getValue(const QString& name, QVariant defVal = QVariant()) const = 0; - virtual void setValue(const QString& name, QVariant val) = 0; -}; - -class LIBMMCSETTINGS_EXPORT AppSettings : public SettingsBase -{ - Q_OBJECT -public: - explicit AppSettings(QObject *parent = 0); - - QSettings& getConfig() { return config; } - - virtual QVariant getValue(const QString &name, QVariant defVal = QVariant()) const; - virtual void setValue(const QString& name, QVariant val); - -protected: - QSettings config; -}; - -#undef DEFINE_SETTING_ADVANCED -#undef DEFINE_SETTING - -LIBMMCSETTINGS_EXPORT extern AppSettings* settings; - -#endif // APPSETTINGS_H diff --git a/libsettings/include/basicsettingsobject.h b/libsettings/include/basicsettingsobject.h new file mode 100644 index 00000000..ec592b1e --- /dev/null +++ b/libsettings/include/basicsettingsobject.h @@ -0,0 +1,44 @@ +/* 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. + */ + +#ifndef BASICSETTINGSOBJECT_H +#define BASICSETTINGSOBJECT_H + +#include <QObject> +#include <QSettings> + +#include "settingsobject.h" + +#include "libsettings_config.h" + +/*! + * \brief A settings object that stores its settings in a QSettings object. + */ +class LIBMMCSETTINGS_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; +}; + +#endif // BASICSETTINGSOBJECT_H diff --git a/libsettings/include/setting.h b/libsettings/include/setting.h new file mode 100644 index 00000000..49185347 --- /dev/null +++ b/libsettings/include/setting.h @@ -0,0 +1,101 @@ +/* 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. + */ + +#ifndef SETTING_H +#define SETTING_H + +#include <QObject> +#include <QVariant> + +#include "libsettings_config.h" + +class SettingsObject; + +/*! + * + */ +class LIBMMCSETTINGS_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<T>() + * \return The setting's actual value. + */ + template<typename T> + inline T value() const { return get().value<T>(); } + + + /*! + * \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); + +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); + +protected: + QString m_id; + QVariant m_defVal; +}; + +#endif // SETTING_H diff --git a/libsettings/include/settingsobject.h b/libsettings/include/settingsobject.h new file mode 100644 index 00000000..3f09b72c --- /dev/null +++ b/libsettings/include/settingsobject.h @@ -0,0 +1,165 @@ +/* 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. + */ + +#ifndef SETTINGSOBJECT_H +#define SETTINGSOBJECT_H + +#include <QObject> +#include <QMap> + +#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 LIBMMCSETTINGS_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 Gets a QList with pointers to all of the registered settings. + * The order of the entries in the list is undefined. + * \return A QList with pointers to all registered settings. + */ + virtual QList<Setting *> getSettings(); + + /*! + * \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); + +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; + +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<QString, Setting *> m_settings; +}; + +#endif // SETTINGSOBJECT_H diff --git a/libsettings/src/basicsettingsobject.cpp b/libsettings/src/basicsettingsobject.cpp new file mode 100644 index 00000000..66a2c2c8 --- /dev/null +++ b/libsettings/src/basicsettingsobject.cpp @@ -0,0 +1,43 @@ +/* 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())) + { + config.setValue(setting.configKey(), value); + } +} + +QVariant BasicSettingsObject::retrieveValue(const Setting &setting) +{ + if (contains(setting.id())) + { + return config.value(setting.configKey()); + } + else + { + return QVariant(); + } +} diff --git a/libsettings/src/setting.cpp b/libsettings/src/setting.cpp new file mode 100644 index 00000000..a224ad39 --- /dev/null +++ b/libsettings/src/setting.cpp @@ -0,0 +1,42 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "include/setting.h" +#include "include/settingsobject.h" + +Setting::Setting(QString id, QVariant defVal, QObject *parent) : + QObject(parent), m_id(id), m_defVal(defVal) +{ + +} + +QVariant Setting::get() const +{ + SettingsObject *sbase = qobject_cast<SettingsObject *>(parent()); + if (!sbase) + return defValue(); + else + return sbase->retrieveValue(*this); +} + +QVariant Setting::defValue() const +{ + return m_defVal; +} + +void Setting::set(QVariant value) +{ + emit settingChanged(*this, value); +} diff --git a/libsettings/src/settingsobject.cpp b/libsettings/src/settingsobject.cpp new file mode 100644 index 00000000..27fbb40a --- /dev/null +++ b/libsettings/src/settingsobject.cpp @@ -0,0 +1,124 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "include/settingsobject.h" +#include "include/setting.h" + +#include <QVariant> + +SettingsObject::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; + } +} + +QList<Setting *> SettingsObject::getSettings() +{ + return m_settings.values(); +} + +bool SettingsObject::contains(const QString &id) +{ + return m_settings.contains(id); +} + + +void SettingsObject::connectSignals(const Setting &setting) +{ + connect(&setting, SIGNAL(settingChanged(const Setting &, QVariant)), + SLOT(changeSetting(const Setting &, QVariant))); + connect(&setting, SIGNAL(settingChanged(const Setting &, QVariant)), + SIGNAL(settingChanged(const Setting &, QVariant))); +} + +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))); +} @@ -17,7 +17,7 @@ #include "gui/mainwindow.h" #include <QApplication> -#include "appsettings.h" +#include "data/appsettings.h" #include "data/loginresponse.h" #include "data/plugin/pluginmanager.h" diff --git a/tasks/logintask.cpp b/tasks/logintask.cpp index f683b811..71075630 100644 --- a/tasks/logintask.cpp +++ b/tasks/logintask.cpp @@ -44,8 +44,8 @@ void LoginTask::executeTask() "application/x-www-form-urlencoded"); QUrlQuery params; - params.addQueryItem("user", uInfo.getUsername()); - params.addQueryItem("password", uInfo.getPassword()); + params.addQueryItem("user", uInfo.username()); + params.addQueryItem("password", uInfo.password()); params.addQueryItem("version", "13"); netReply = netMgr.post(netRequest, params.query(QUrl::EncodeSpaces).toUtf8()); |