From 6ecb833dbf4c4930c8354dcce7967ad44c16c217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 28 Oct 2013 20:55:12 +0100 Subject: [PATCH] Fix problem with instance list not using the instance folder path --- MultiMC.cpp | 9 ++-- depends/util/include/pathutils.h | 10 +++++ depends/util/src/pathutils.cpp | 24 ++++++++++ gui/settingsdialog.cpp | 77 ++++++++++++++++++-------------- logic/lists/InstanceList.cpp | 16 ++++--- logic/lists/InstanceList.h | 69 ++++++++++++++++------------ 6 files changed, 134 insertions(+), 71 deletions(-) diff --git a/MultiMC.cpp b/MultiMC.cpp index 3d62f76e..56109ba6 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -32,8 +32,8 @@ using namespace Util::Commandline; MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) { - setOrganizationName("MultiMC"); - setApplicationName("MultiMC5"); + setOrganizationName("MultiMC"); + setApplicationName("MultiMC5"); initTranslations(); @@ -139,9 +139,12 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) initGlobalSettings(); // and instances - m_instances.reset(new InstanceList(m_settings->get("InstanceDir").toString(), this)); + auto InstDirSetting = m_settings->getSetting("InstanceDir"); + m_instances.reset(new InstanceList(InstDirSetting->get().toString(), this)); QLOG_INFO() << "Loading Instances..."; m_instances->loadList(); + connect(InstDirSetting, SIGNAL(settingChanged(const Setting &, QVariant)), + m_instances.get(), SLOT(on_InstFolderChanged(const Setting &, QVariant))); // init the http meta cache initHttpMetaCache(); diff --git a/depends/util/include/pathutils.h b/depends/util/include/pathutils.h index cea3a39a..c892c115 100644 --- a/depends/util/include/pathutils.h +++ b/depends/util/include/pathutils.h @@ -25,6 +25,16 @@ LIBUTIL_EXPORT QString PathCombine(QString path1, QString path2, QString path3); LIBUTIL_EXPORT QString AbsolutePath(QString path); +/** + * Normalize path + * + * Any paths inside the current directory will be normalized to relative paths (to current) + * Other paths will be made absolute + * + * Returns false if the path logic somehow filed (and normalizedPath in invalid) + */ +QString NormalizePath(QString path); + LIBUTIL_EXPORT QString RemoveInvalidFilenameChars(QString string, QChar replaceWith = '-'); LIBUTIL_EXPORT QString DirNameFromString(QString string, QString inDir = "."); diff --git a/depends/util/src/pathutils.cpp b/depends/util/src/pathutils.cpp index 4c24fa5d..590ac89d 100644 --- a/depends/util/src/pathutils.cpp +++ b/depends/util/src/pathutils.cpp @@ -39,6 +39,30 @@ QString AbsolutePath(QString path) return QFileInfo(path).absolutePath(); } +/** + * Normalize path + * + * Any paths inside the current directory will be normalized to relative paths (to current) + * Other paths will be made absolute + */ +QString NormalizePath(QString path) +{ + QDir a = QDir::currentPath(); + QString currentAbsolute = a.absolutePath(); + + QDir b(path); + QString newAbsolute = b.absolutePath(); + + if (newAbsolute.startsWith(currentAbsolute)) + { + return a.relativeFilePath(newAbsolute); + } + else + { + return newAbsolute; + } +} + QString badFilenameChars = "\"\\/?<>:*|!"; QString RemoveInvalidFilenameChars(QString string, QChar replaceWith) diff --git a/gui/settingsdialog.cpp b/gui/settingsdialog.cpp index 14eba492..b2fbd898 100644 --- a/gui/settingsdialog.cpp +++ b/gui/settingsdialog.cpp @@ -3,7 +3,7 @@ * 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 @@ -22,16 +22,16 @@ #include "logic/lists/JavaVersionList.h" #include +#include #include #include +#include -SettingsDialog::SettingsDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::SettingsDialog) +SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SettingsDialog) { - MultiMCPlatform::fixWM_CLASS(this); + MultiMCPlatform::fixWM_CLASS(this); ui->setupUi(this); - + loadSettings(MMC->settings().get()); updateCheckboxStuff(); } @@ -40,7 +40,7 @@ SettingsDialog::~SettingsDialog() { delete ui; } -void SettingsDialog::showEvent ( QShowEvent* ev ) +void SettingsDialog::showEvent(QShowEvent *ev) { QDialog::showEvent(ev); adjustSize(); @@ -49,31 +49,40 @@ void SettingsDialog::showEvent ( QShowEvent* ev ) void SettingsDialog::updateCheckboxStuff() { ui->windowWidthSpinBox->setEnabled(!ui->maximizedCheckBox->isChecked()); - ui->windowHeightSpinBox->setEnabled(! ui->maximizedCheckBox->isChecked()); + ui->windowHeightSpinBox->setEnabled(!ui->maximizedCheckBox->isChecked()); } void SettingsDialog::on_instDirBrowseBtn_clicked() { - QString dir = QFileDialog::getExistingDirectory(this, tr("Instance Directory"), + QString raw_dir = QFileDialog::getExistingDirectory(this, tr("Instance Directory"), ui->instDirTextBox->text()); - if (!dir.isEmpty()) - ui->instDirTextBox->setText(dir); + QString cooked_dir = NormalizePath(raw_dir); + + // do not allow current dir - it's dirty. Do not allow dirs that don't exist + if (!cooked_dir.isEmpty() && QDir(cooked_dir).exists()) + { + ui->instDirTextBox->setText(cooked_dir); + } } void SettingsDialog::on_modsDirBrowseBtn_clicked() { - QString dir = QFileDialog::getExistingDirectory(this, tr("Mods Directory"), + QString dir = QFileDialog::getExistingDirectory(this, tr("Mods Directory"), ui->modsDirTextBox->text()); if (!dir.isEmpty()) + { ui->modsDirTextBox->setText(dir); + } } void SettingsDialog::on_lwjglDirBrowseBtn_clicked() { - QString dir = QFileDialog::getExistingDirectory(this, tr("LWJGL Directory"), + QString dir = QFileDialog::getExistingDirectory(this, tr("LWJGL Directory"), ui->lwjglDirTextBox->text()); if (!dir.isEmpty()) + { ui->lwjglDirTextBox->setText(dir); + } } void SettingsDialog::on_compatModeCheckBox_clicked(bool checked) @@ -96,7 +105,7 @@ void SettingsDialog::on_buttonBox_accepted() void SettingsDialog::applySettings(SettingsObject *s) { // Special cases - + // Warn about dev builds. if (!ui->devBuildsCheckBox->isChecked()) { @@ -104,46 +113,46 @@ void SettingsDialog::applySettings(SettingsObject *s) } else if (!s->get("UseDevBuilds").toBool()) { - int response = QMessageBox::question(this, tr("Development builds"), - tr("Development builds contain experimental features " - "and may be unstable. Are you sure you want to enable them?")); + int response = QMessageBox::question( + this, tr("Development builds"), + tr("Development builds contain experimental features " + "and may be unstable. Are you sure you want to enable them?")); if (response == QMessageBox::Yes) { s->set("UseDevBuilds", true); } } - - + // Updates s->set("AutoUpdate", ui->autoUpdateCheckBox->isChecked()); - + // Folders // TODO: Offer to move instances to new instance folder. s->set("InstanceDir", ui->instDirTextBox->text()); s->set("CentralModsDir", ui->modsDirTextBox->text()); s->set("LWJGLDir", ui->lwjglDirTextBox->text()); - + // Console s->set("ShowConsole", ui->showConsoleCheck->isChecked()); s->set("AutoCloseConsole", ui->autoCloseConsoleCheck->isChecked()); - + // Window Size s->set("LaunchMaximized", ui->maximizedCheckBox->isChecked()); s->set("MinecraftWinWidth", ui->windowWidthSpinBox->value()); s->set("MinecraftWinHeight", ui->windowHeightSpinBox->value()); - + // Auto Login s->set("AutoLogin", ui->autoLoginCheckBox->isChecked()); - + // Memory s->set("MinMemAlloc", ui->minMemSpinBox->value()); s->set("MaxMemAlloc", ui->maxMemSpinBox->value()); s->set("PermGen", ui->permGenSpinBox->value()); - + // Java Settings s->set("JavaPath", ui->javaPathTextBox->text()); s->set("JvmArgs", ui->jvmArgsTextBox->text()); - + // Custom Commands s->set("PreLaunchCommand", ui->preLaunchCmdTextBox->text()); s->set("PostExitCommand", ui->postExitCmdTextBox->text()); @@ -154,33 +163,33 @@ void SettingsDialog::loadSettings(SettingsObject *s) // Updates ui->autoUpdateCheckBox->setChecked(s->get("AutoUpdate").toBool()); ui->devBuildsCheckBox->setChecked(s->get("UseDevBuilds").toBool()); - + // Folders 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->get("ShowConsole").toBool()); ui->autoCloseConsoleCheck->setChecked(s->get("AutoCloseConsole").toBool()); - + // Window Size 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->get("AutoLogin").toBool()); - + // Memory ui->minMemSpinBox->setValue(s->get("MinMemAlloc").toInt()); ui->maxMemSpinBox->setValue(s->get("MaxMemAlloc").toInt()); ui->permGenSpinBox->setValue(s->get("PermGen").toInt()); - + // Java Settings ui->javaPathTextBox->setText(s->get("JavaPath").toString()); ui->jvmArgsTextBox->setText(s->get("JvmArgs").toString()); - + // Custom Commands ui->preLaunchCmdTextBox->setText(s->get("PreLaunchCommand").toString()); ui->postExitCmdTextBox->setText(s->get("PostExitCommand").toString()); @@ -204,7 +213,7 @@ void SettingsDialog::on_pushButton_clicked() void SettingsDialog::on_btnBrowse_clicked() { QString dir = QFileDialog::getOpenFileName(this, tr("Find Java executable")); - if(!dir.isNull()) + if (!dir.isNull()) { ui->javaPathTextBox->setText(dir); } diff --git a/logic/lists/InstanceList.cpp b/logic/lists/InstanceList.cpp index 9740d5a5..08985a19 100644 --- a/logic/lists/InstanceList.cpp +++ b/logic/lists/InstanceList.cpp @@ -34,7 +34,7 @@ const static int GROUP_FILE_FORMAT_VERSION = 1; InstanceList::InstanceList(const QString &instDir, QObject *parent) - : QAbstractListModel(parent), m_instDir("instances") + : QAbstractListModel(parent), m_instDir(instDir) { } @@ -196,8 +196,8 @@ void InstanceList::loadGroupList(QMap &groupMap) if (error.error != QJsonParseError::NoError) { QLOG_ERROR() << QString("Failed to parse instance group file: %1 at offset %2") - .arg(error.errorString(), QString::number(error.offset)) - .toUtf8(); + .arg(error.errorString(), QString::number(error.offset)) + .toUtf8(); return; } @@ -269,7 +269,8 @@ InstanceList::InstListError InstanceList::loadList() m_instances.clear(); QDir dir(m_instDir); - QDirIterator iter(dir); + QDirIterator iter(m_instDir, QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::Readable, + QDirIterator::FollowSymlinks); while (iter.hasNext()) { QString subDir = iter.next(); @@ -340,7 +341,12 @@ void InstanceList::clear() endResetModel(); emit dataIsInvalid(); } -; + +void InstanceList::on_InstFolderChanged(const Setting &setting, QVariant value) +{ + m_instDir = value.toString(); + loadList(); +} /// Add an instance. Triggers notifications, returns the new index int InstanceList::add(InstancePtr t) diff --git a/logic/lists/InstanceList.h b/logic/lists/InstanceList.h index 501edeb1..3cde6bf5 100644 --- a/logic/lists/InstanceList.h +++ b/logic/lists/InstanceList.h @@ -3,7 +3,7 @@ * 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 @@ -29,19 +29,19 @@ class InstanceList : public QAbstractListModel { Q_OBJECT private: - void loadGroupList(QMap & groupList); + void loadGroupList(QMap &groupList); void saveGroupList(); - + public: explicit InstanceList(const QString &instDir, QObject *parent = 0); virtual ~InstanceList(); - + public: - QModelIndex index ( int row, int column = 0, const QModelIndex& parent = QModelIndex() ) const; - int rowCount ( const QModelIndex& parent = QModelIndex() ) const; - QVariant data ( const QModelIndex& index, int role ) const; - Qt::ItemFlags flags ( const QModelIndex& index ) const; - + QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + enum AdditionalRoles { InstancePointerRole = 0x34B1CB48 ///< Return pointer to real instance @@ -56,58 +56,69 @@ public: NoError = 0, UnknownError }; - - QString instDir() const { return m_instDir; } - + + QString instDir() const + { + return m_instDir; + } + /*! * \brief Loads the instance list. Triggers notifications. */ InstListError loadList(); - + /*! * \brief Get the instance at index */ InstancePtr at(int i) const { return m_instances.at(i); - }; - + } + ; + /*! * \brief Get the count of loaded instances */ int count() const { return m_instances.count(); - }; - + } + ; + /// Clear all instances. Triggers notifications. void clear(); - + /// Add an instance. Triggers notifications, returns the new index int add(InstancePtr t); - + /// Get an instance by ID - InstancePtr getInstanceById (QString id); + InstancePtr getInstanceById(QString id); signals: void dataIsInvalid(); - -private slots: - void propertiesChanged(BaseInstance * inst); - void instanceNuked(BaseInstance * inst); + +public +slots: + void on_InstFolderChanged(const Setting & setting, QVariant value); + +private +slots: + void propertiesChanged(BaseInstance *inst); + void instanceNuked(BaseInstance *inst); void groupChanged(); + private: - int getInstIndex(BaseInstance * inst); + int getInstIndex(BaseInstance *inst); + protected: QString m_instDir; - QList< InstancePtr > m_instances; + QList m_instances; }; class InstanceProxyModel : public KCategorizedSortFilterProxyModel { public: - explicit InstanceProxyModel ( QObject *parent = 0 ); + explicit InstanceProxyModel(QObject *parent = 0); protected: - virtual bool subSortLessThan ( const QModelIndex& left, const QModelIndex& right ) const; + virtual bool subSortLessThan(const QModelIndex &left, const QModelIndex &right) const; }; -