Fix problem with instance list not using the instance folder path

This commit is contained in:
Petr Mrázek 2013-10-28 20:55:12 +01:00
parent 9233477295
commit 6ecb833dbf
6 changed files with 134 additions and 71 deletions

View File

@ -32,8 +32,8 @@ using namespace Util::Commandline;
MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv)
{ {
setOrganizationName("MultiMC"); setOrganizationName("MultiMC");
setApplicationName("MultiMC5"); setApplicationName("MultiMC5");
initTranslations(); initTranslations();
@ -139,9 +139,12 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv)
initGlobalSettings(); initGlobalSettings();
// and instances // 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..."; QLOG_INFO() << "Loading Instances...";
m_instances->loadList(); m_instances->loadList();
connect(InstDirSetting, SIGNAL(settingChanged(const Setting &, QVariant)),
m_instances.get(), SLOT(on_InstFolderChanged(const Setting &, QVariant)));
// init the http meta cache // init the http meta cache
initHttpMetaCache(); initHttpMetaCache();

View File

@ -25,6 +25,16 @@ LIBUTIL_EXPORT QString PathCombine(QString path1, QString path2, QString path3);
LIBUTIL_EXPORT QString AbsolutePath(QString path); 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 RemoveInvalidFilenameChars(QString string, QChar replaceWith = '-');
LIBUTIL_EXPORT QString DirNameFromString(QString string, QString inDir = "."); LIBUTIL_EXPORT QString DirNameFromString(QString string, QString inDir = ".");

View File

@ -39,6 +39,30 @@ QString AbsolutePath(QString path)
return QFileInfo(path).absolutePath(); 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 badFilenameChars = "\"\\/?<>:*|!";
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith) QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)

View File

@ -3,7 +3,7 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
@ -22,16 +22,16 @@
#include "logic/lists/JavaVersionList.h" #include "logic/lists/JavaVersionList.h"
#include <settingsobject.h> #include <settingsobject.h>
#include <pathutils.h>
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
#include <QDir>
SettingsDialog::SettingsDialog(QWidget *parent) : SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SettingsDialog)
QDialog(parent),
ui(new Ui::SettingsDialog)
{ {
MultiMCPlatform::fixWM_CLASS(this); MultiMCPlatform::fixWM_CLASS(this);
ui->setupUi(this); ui->setupUi(this);
loadSettings(MMC->settings().get()); loadSettings(MMC->settings().get());
updateCheckboxStuff(); updateCheckboxStuff();
} }
@ -40,7 +40,7 @@ SettingsDialog::~SettingsDialog()
{ {
delete ui; delete ui;
} }
void SettingsDialog::showEvent ( QShowEvent* ev ) void SettingsDialog::showEvent(QShowEvent *ev)
{ {
QDialog::showEvent(ev); QDialog::showEvent(ev);
adjustSize(); adjustSize();
@ -49,31 +49,40 @@ void SettingsDialog::showEvent ( QShowEvent* ev )
void SettingsDialog::updateCheckboxStuff() void SettingsDialog::updateCheckboxStuff()
{ {
ui->windowWidthSpinBox->setEnabled(!ui->maximizedCheckBox->isChecked()); ui->windowWidthSpinBox->setEnabled(!ui->maximizedCheckBox->isChecked());
ui->windowHeightSpinBox->setEnabled(! ui->maximizedCheckBox->isChecked()); ui->windowHeightSpinBox->setEnabled(!ui->maximizedCheckBox->isChecked());
} }
void SettingsDialog::on_instDirBrowseBtn_clicked() 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()); ui->instDirTextBox->text());
if (!dir.isEmpty()) QString cooked_dir = NormalizePath(raw_dir);
ui->instDirTextBox->setText(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() void SettingsDialog::on_modsDirBrowseBtn_clicked()
{ {
QString dir = QFileDialog::getExistingDirectory(this, tr("Mods Directory"), QString dir = QFileDialog::getExistingDirectory(this, tr("Mods Directory"),
ui->modsDirTextBox->text()); ui->modsDirTextBox->text());
if (!dir.isEmpty()) if (!dir.isEmpty())
{
ui->modsDirTextBox->setText(dir); ui->modsDirTextBox->setText(dir);
}
} }
void SettingsDialog::on_lwjglDirBrowseBtn_clicked() void SettingsDialog::on_lwjglDirBrowseBtn_clicked()
{ {
QString dir = QFileDialog::getExistingDirectory(this, tr("LWJGL Directory"), QString dir = QFileDialog::getExistingDirectory(this, tr("LWJGL Directory"),
ui->lwjglDirTextBox->text()); ui->lwjglDirTextBox->text());
if (!dir.isEmpty()) if (!dir.isEmpty())
{
ui->lwjglDirTextBox->setText(dir); ui->lwjglDirTextBox->setText(dir);
}
} }
void SettingsDialog::on_compatModeCheckBox_clicked(bool checked) void SettingsDialog::on_compatModeCheckBox_clicked(bool checked)
@ -96,7 +105,7 @@ void SettingsDialog::on_buttonBox_accepted()
void SettingsDialog::applySettings(SettingsObject *s) void SettingsDialog::applySettings(SettingsObject *s)
{ {
// Special cases // Special cases
// Warn about dev builds. // Warn about dev builds.
if (!ui->devBuildsCheckBox->isChecked()) if (!ui->devBuildsCheckBox->isChecked())
{ {
@ -104,46 +113,46 @@ void SettingsDialog::applySettings(SettingsObject *s)
} }
else if (!s->get("UseDevBuilds").toBool()) else if (!s->get("UseDevBuilds").toBool())
{ {
int response = QMessageBox::question(this, tr("Development builds"), int response = QMessageBox::question(
tr("Development builds contain experimental features " this, tr("Development builds"),
"and may be unstable. Are you sure you want to enable them?")); tr("Development builds contain experimental features "
"and may be unstable. Are you sure you want to enable them?"));
if (response == QMessageBox::Yes) if (response == QMessageBox::Yes)
{ {
s->set("UseDevBuilds", true); s->set("UseDevBuilds", true);
} }
} }
// Updates // Updates
s->set("AutoUpdate", ui->autoUpdateCheckBox->isChecked()); s->set("AutoUpdate", ui->autoUpdateCheckBox->isChecked());
// Folders // Folders
// TODO: Offer to move instances to new instance folder. // TODO: Offer to move instances to new instance folder.
s->set("InstanceDir", ui->instDirTextBox->text()); s->set("InstanceDir", ui->instDirTextBox->text());
s->set("CentralModsDir", ui->modsDirTextBox->text()); s->set("CentralModsDir", ui->modsDirTextBox->text());
s->set("LWJGLDir", ui->lwjglDirTextBox->text()); s->set("LWJGLDir", ui->lwjglDirTextBox->text());
// Console // Console
s->set("ShowConsole", ui->showConsoleCheck->isChecked()); s->set("ShowConsole", ui->showConsoleCheck->isChecked());
s->set("AutoCloseConsole", ui->autoCloseConsoleCheck->isChecked()); s->set("AutoCloseConsole", ui->autoCloseConsoleCheck->isChecked());
// Window Size // Window Size
s->set("LaunchMaximized", ui->maximizedCheckBox->isChecked()); s->set("LaunchMaximized", ui->maximizedCheckBox->isChecked());
s->set("MinecraftWinWidth", ui->windowWidthSpinBox->value()); s->set("MinecraftWinWidth", ui->windowWidthSpinBox->value());
s->set("MinecraftWinHeight", ui->windowHeightSpinBox->value()); s->set("MinecraftWinHeight", ui->windowHeightSpinBox->value());
// Auto Login // Auto Login
s->set("AutoLogin", ui->autoLoginCheckBox->isChecked()); s->set("AutoLogin", ui->autoLoginCheckBox->isChecked());
// Memory // Memory
s->set("MinMemAlloc", ui->minMemSpinBox->value()); s->set("MinMemAlloc", ui->minMemSpinBox->value());
s->set("MaxMemAlloc", ui->maxMemSpinBox->value()); s->set("MaxMemAlloc", ui->maxMemSpinBox->value());
s->set("PermGen", ui->permGenSpinBox->value()); s->set("PermGen", ui->permGenSpinBox->value());
// Java Settings // Java Settings
s->set("JavaPath", ui->javaPathTextBox->text()); s->set("JavaPath", ui->javaPathTextBox->text());
s->set("JvmArgs", ui->jvmArgsTextBox->text()); s->set("JvmArgs", ui->jvmArgsTextBox->text());
// Custom Commands // Custom Commands
s->set("PreLaunchCommand", ui->preLaunchCmdTextBox->text()); s->set("PreLaunchCommand", ui->preLaunchCmdTextBox->text());
s->set("PostExitCommand", ui->postExitCmdTextBox->text()); s->set("PostExitCommand", ui->postExitCmdTextBox->text());
@ -154,33 +163,33 @@ void SettingsDialog::loadSettings(SettingsObject *s)
// Updates // Updates
ui->autoUpdateCheckBox->setChecked(s->get("AutoUpdate").toBool()); ui->autoUpdateCheckBox->setChecked(s->get("AutoUpdate").toBool());
ui->devBuildsCheckBox->setChecked(s->get("UseDevBuilds").toBool()); ui->devBuildsCheckBox->setChecked(s->get("UseDevBuilds").toBool());
// Folders // Folders
ui->instDirTextBox->setText(s->get("InstanceDir").toString()); ui->instDirTextBox->setText(s->get("InstanceDir").toString());
ui->modsDirTextBox->setText(s->get("CentralModsDir").toString()); ui->modsDirTextBox->setText(s->get("CentralModsDir").toString());
ui->lwjglDirTextBox->setText(s->get("LWJGLDir").toString()); ui->lwjglDirTextBox->setText(s->get("LWJGLDir").toString());
// Console // Console
ui->showConsoleCheck->setChecked(s->get("ShowConsole").toBool()); ui->showConsoleCheck->setChecked(s->get("ShowConsole").toBool());
ui->autoCloseConsoleCheck->setChecked(s->get("AutoCloseConsole").toBool()); ui->autoCloseConsoleCheck->setChecked(s->get("AutoCloseConsole").toBool());
// Window Size // Window Size
ui->maximizedCheckBox->setChecked(s->get("LaunchMaximized").toBool()); ui->maximizedCheckBox->setChecked(s->get("LaunchMaximized").toBool());
ui->windowWidthSpinBox->setValue(s->get("MinecraftWinWidth").toInt()); ui->windowWidthSpinBox->setValue(s->get("MinecraftWinWidth").toInt());
ui->windowHeightSpinBox->setValue(s->get("MinecraftWinHeight").toInt()); ui->windowHeightSpinBox->setValue(s->get("MinecraftWinHeight").toInt());
// Auto Login // Auto Login
ui->autoLoginCheckBox->setChecked(s->get("AutoLogin").toBool()); ui->autoLoginCheckBox->setChecked(s->get("AutoLogin").toBool());
// Memory // Memory
ui->minMemSpinBox->setValue(s->get("MinMemAlloc").toInt()); ui->minMemSpinBox->setValue(s->get("MinMemAlloc").toInt());
ui->maxMemSpinBox->setValue(s->get("MaxMemAlloc").toInt()); ui->maxMemSpinBox->setValue(s->get("MaxMemAlloc").toInt());
ui->permGenSpinBox->setValue(s->get("PermGen").toInt()); ui->permGenSpinBox->setValue(s->get("PermGen").toInt());
// Java Settings // Java Settings
ui->javaPathTextBox->setText(s->get("JavaPath").toString()); ui->javaPathTextBox->setText(s->get("JavaPath").toString());
ui->jvmArgsTextBox->setText(s->get("JvmArgs").toString()); ui->jvmArgsTextBox->setText(s->get("JvmArgs").toString());
// Custom Commands // Custom Commands
ui->preLaunchCmdTextBox->setText(s->get("PreLaunchCommand").toString()); ui->preLaunchCmdTextBox->setText(s->get("PreLaunchCommand").toString());
ui->postExitCmdTextBox->setText(s->get("PostExitCommand").toString()); ui->postExitCmdTextBox->setText(s->get("PostExitCommand").toString());
@ -204,7 +213,7 @@ void SettingsDialog::on_pushButton_clicked()
void SettingsDialog::on_btnBrowse_clicked() void SettingsDialog::on_btnBrowse_clicked()
{ {
QString dir = QFileDialog::getOpenFileName(this, tr("Find Java executable")); QString dir = QFileDialog::getOpenFileName(this, tr("Find Java executable"));
if(!dir.isNull()) if (!dir.isNull())
{ {
ui->javaPathTextBox->setText(dir); ui->javaPathTextBox->setText(dir);
} }

View File

@ -34,7 +34,7 @@
const static int GROUP_FILE_FORMAT_VERSION = 1; const static int GROUP_FILE_FORMAT_VERSION = 1;
InstanceList::InstanceList(const QString &instDir, QObject *parent) 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<QString, QString> &groupMap)
if (error.error != QJsonParseError::NoError) if (error.error != QJsonParseError::NoError)
{ {
QLOG_ERROR() << QString("Failed to parse instance group file: %1 at offset %2") QLOG_ERROR() << QString("Failed to parse instance group file: %1 at offset %2")
.arg(error.errorString(), QString::number(error.offset)) .arg(error.errorString(), QString::number(error.offset))
.toUtf8(); .toUtf8();
return; return;
} }
@ -269,7 +269,8 @@ InstanceList::InstListError InstanceList::loadList()
m_instances.clear(); m_instances.clear();
QDir dir(m_instDir); QDir dir(m_instDir);
QDirIterator iter(dir); QDirIterator iter(m_instDir, QDir::Dirs | QDir::NoDot | QDir::NoDotDot | QDir::Readable,
QDirIterator::FollowSymlinks);
while (iter.hasNext()) while (iter.hasNext())
{ {
QString subDir = iter.next(); QString subDir = iter.next();
@ -340,7 +341,12 @@ void InstanceList::clear()
endResetModel(); endResetModel();
emit dataIsInvalid(); 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 /// Add an instance. Triggers notifications, returns the new index
int InstanceList::add(InstancePtr t) int InstanceList::add(InstancePtr t)

View File

@ -3,7 +3,7 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
@ -29,19 +29,19 @@ class InstanceList : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
private: private:
void loadGroupList(QMap<QString, QString> & groupList); void loadGroupList(QMap<QString, QString> &groupList);
void saveGroupList(); void saveGroupList();
public: public:
explicit InstanceList(const QString &instDir, QObject *parent = 0); explicit InstanceList(const QString &instDir, QObject *parent = 0);
virtual ~InstanceList(); virtual ~InstanceList();
public: public:
QModelIndex index ( int row, int column = 0, const QModelIndex& parent = QModelIndex() ) const; QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
int rowCount ( const QModelIndex& parent = QModelIndex() ) const; int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data ( const QModelIndex& index, int role ) const; QVariant data(const QModelIndex &index, int role) const;
Qt::ItemFlags flags ( const QModelIndex& index ) const; Qt::ItemFlags flags(const QModelIndex &index) const;
enum AdditionalRoles enum AdditionalRoles
{ {
InstancePointerRole = 0x34B1CB48 ///< Return pointer to real instance InstancePointerRole = 0x34B1CB48 ///< Return pointer to real instance
@ -56,58 +56,69 @@ public:
NoError = 0, NoError = 0,
UnknownError UnknownError
}; };
QString instDir() const { return m_instDir; } QString instDir() const
{
return m_instDir;
}
/*! /*!
* \brief Loads the instance list. Triggers notifications. * \brief Loads the instance list. Triggers notifications.
*/ */
InstListError loadList(); InstListError loadList();
/*! /*!
* \brief Get the instance at index * \brief Get the instance at index
*/ */
InstancePtr at(int i) const InstancePtr at(int i) const
{ {
return m_instances.at(i); return m_instances.at(i);
}; }
;
/*! /*!
* \brief Get the count of loaded instances * \brief Get the count of loaded instances
*/ */
int count() const int count() const
{ {
return m_instances.count(); return m_instances.count();
}; }
;
/// Clear all instances. Triggers notifications. /// Clear all instances. Triggers notifications.
void clear(); void clear();
/// Add an instance. Triggers notifications, returns the new index /// Add an instance. Triggers notifications, returns the new index
int add(InstancePtr t); int add(InstancePtr t);
/// Get an instance by ID /// Get an instance by ID
InstancePtr getInstanceById (QString id); InstancePtr getInstanceById(QString id);
signals: signals:
void dataIsInvalid(); void dataIsInvalid();
private slots: public
void propertiesChanged(BaseInstance * inst); slots:
void instanceNuked(BaseInstance * inst); void on_InstFolderChanged(const Setting & setting, QVariant value);
private
slots:
void propertiesChanged(BaseInstance *inst);
void instanceNuked(BaseInstance *inst);
void groupChanged(); void groupChanged();
private: private:
int getInstIndex(BaseInstance * inst); int getInstIndex(BaseInstance *inst);
protected: protected:
QString m_instDir; QString m_instDir;
QList< InstancePtr > m_instances; QList<InstancePtr> m_instances;
}; };
class InstanceProxyModel : public KCategorizedSortFilterProxyModel class InstanceProxyModel : public KCategorizedSortFilterProxyModel
{ {
public: public:
explicit InstanceProxyModel ( QObject *parent = 0 ); explicit InstanceProxyModel(QObject *parent = 0);
protected: protected:
virtual bool subSortLessThan ( const QModelIndex& left, const QModelIndex& right ) const; virtual bool subSortLessThan(const QModelIndex &left, const QModelIndex &right) const;
}; };