From fce0f5df045f1d956cabeda41406001a037c9ab7 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 14 Jan 2013 17:42:38 -0600 Subject: Added stuff. --- MultiMC.pro | 8 ++++- MultiMC.pro.user | 2 +- data/appsettings.cpp | 22 ++++++++++++++ data/appsettings.h | 27 +++++++++++++++++ data/inifile.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++ data/inifile.h | 36 ++++++++++++++++++++++ data/instancebase.cpp | 22 ++++++++------ data/instancebase.h | 13 ++++---- data/instancelist.cpp | 52 ++++++++++++++++++++++++++++++++ data/instancelist.h | 38 +++++++++++++++++++++++ data/settingsbase.cpp | 22 ++++++++++++++ data/settingsbase.h | 33 ++++++++++++++++++++ data/settingsmacros.h | 35 ++++++++++++++++++++++ data/settingsmacrosundef.h | 26 ++++++++++++++++ data/stdinstance.cpp | 22 ++++++++++++++ data/stdinstance.h | 28 +++++++++++++++++ gui/mainwindow.cpp | 6 +++- gui/mainwindow.h | 6 ++++ stdinstance.cpp | 0 stdinstance.h | 0 util/pathutils.cpp | 5 ++++ util/pathutils.h | 1 + 22 files changed, 461 insertions(+), 18 deletions(-) create mode 100644 data/appsettings.cpp create mode 100644 data/appsettings.h create mode 100644 data/inifile.cpp create mode 100644 data/inifile.h create mode 100644 data/instancelist.cpp create mode 100644 data/instancelist.h create mode 100644 data/settingsbase.cpp create mode 100644 data/settingsbase.h create mode 100644 data/settingsmacros.h create mode 100644 data/settingsmacrosundef.h create mode 100644 data/stdinstance.cpp create mode 100644 data/stdinstance.h create mode 100644 stdinstance.cpp create mode 100644 stdinstance.h diff --git a/MultiMC.pro b/MultiMC.pro index 6115627c..81d72aee 100644 --- a/MultiMC.pro +++ b/MultiMC.pro @@ -15,11 +15,17 @@ TEMPLATE = app SOURCES += main.cpp\ gui/mainwindow.cpp \ data/instancebase.cpp \ - util/pathutils.cpp + util/pathutils.cpp \ + data/instancelist.cpp \ + data/stdinstance.cpp \ + data/inifile.cpp HEADERS += gui/mainwindow.h \ data/instancebase.h \ util/pathutils.h \ + data/instancelist.h \ + data/stdinstance.h \ + data/inifile.h FORMS += gui/mainwindow.ui diff --git a/MultiMC.pro.user b/MultiMC.pro.user index c72c06aa..f23af787 100644 --- a/MultiMC.pro.user +++ b/MultiMC.pro.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget diff --git a/data/appsettings.cpp b/data/appsettings.cpp new file mode 100644 index 00000000..525def6e --- /dev/null +++ b/data/appsettings.cpp @@ -0,0 +1,22 @@ +/* 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" + +AppSettings::AppSettings(QString fileName) : + SettingsBase(fileName) +{ + +} diff --git a/data/appsettings.h b/data/appsettings.h new file mode 100644 index 00000000..f8c7ff73 --- /dev/null +++ b/data/appsettings.h @@ -0,0 +1,27 @@ +/* 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 "settingsbase.h" + +class AppSettings : public SettingsBase +{ +public: + AppSettings(QString fileName); +}; + +#endif // APPSETTINGS_H diff --git a/data/inifile.cpp b/data/inifile.cpp new file mode 100644 index 00000000..df94e43e --- /dev/null +++ b/data/inifile.cpp @@ -0,0 +1,75 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "inifile.h" + +#include +#include +#include + +INIFile::INIFile() +{ + +} + +bool INIFile::saveFile(QString fileName) +{ + // TODO Handle errors. + QFile file(fileName); + file.open(QIODevice::WriteOnly); + QTextStream out(&file); + + for (Iterator iter = begin(); iter != end(); iter++) + { + out << iter.key() << "=" << iter.value().toString() << "\n"; + } + + return true; +} + +bool INIFile::loadFile(QString fileName) +{ + // TODO Handle errors. + QFile file(fileName); + file.open(QIODevice::ReadOnly); + QTextStream in(&file); + + QStringList lines = in.readAll().split('\n'); + for (int i = 0; i < lines.count(); i++) + { + // Ignore comments. + QString line = lines[i].left('#').trimmed(); + + QString key = line.section('=', 0).trimmed(); + QVariant value(line.section('=', 1).trimmed()); + + this->operator [](key) = value; + } + + return true; +} + +QVariant INIFile::get(QString key, QVariant def) const +{ + if (!this->contains(key)) + return def; + else + return this->operator [](key); +} + +void INIFile::set(QString key, QVariant val) +{ + this->operator [](key) = val; +} diff --git a/data/inifile.h b/data/inifile.h new file mode 100644 index 00000000..75783859 --- /dev/null +++ b/data/inifile.h @@ -0,0 +1,36 @@ +/* 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 INIFILE_H +#define INIFILE_H + +#include +#include +#include + +// Sectionless INI parser (for instance config files) +class INIFile : public QMap +{ +public: + explicit INIFile(); + + bool loadFile(QString fileName); + bool saveFile(QString fileName); + + QVariant get(QString key, QVariant def) const; + void set(QString key, QVariant val); +}; + +#endif // INIFILE_H diff --git a/data/instancebase.cpp b/data/instancebase.cpp index 2e0a65df..15dc54f4 100644 --- a/data/instancebase.cpp +++ b/data/instancebase.cpp @@ -15,31 +15,35 @@ #include "instancebase.h" +#include + #include "../util/pathutils.h" -InstanceBase::InstanceBase(QString rootDir, QObject *parent) : +InstanceBase::InstanceBase(QString dir, QObject *parent) : QObject(parent), - m_rootDir(rootDir), - m_config(PathCombine(rootDir, "instance.cfg"), QSettings::IniFormat) + rootDir(dir) { + QFileInfo cfgFile; + if (cfgFile.exists()) + config.loadFile(PathCombine(rootDir, "instance.cfg")); } -QString InstanceBase::GetRootDir() const +QString InstanceBase::getRootDir() const { - return m_rootDir; + return rootDir; } ///////////// Config Values ///////////// // Name -QString InstanceBase::GetInstName() const +QString InstanceBase::getInstName() const { - return m_config.value("name", "Unnamed").toString(); + return config.get("name", "Unnamed").toString(); } -void InstanceBase::SetInstName(QString name) +void InstanceBase::setInstName(QString name) { - m_config.setValue("name", name); + config.set("name", name); } diff --git a/data/instancebase.h b/data/instancebase.h index df23b1b0..8c61aee0 100644 --- a/data/instancebase.h +++ b/data/instancebase.h @@ -18,7 +18,8 @@ #include #include -#include + +#include "../data/inifile.h" class InstanceBase : public QObject { @@ -26,18 +27,18 @@ class InstanceBase : public QObject public: explicit InstanceBase(QString rootDir, QObject *parent = 0); - QString GetRootDir() const; + QString getRootDir() const; - QString GetInstName() const; - void SetInstName(QString name); + QString getInstName() const; + void setInstName(QString name); protected: private: - QString m_rootDir; + QString rootDir; - QSettings m_config; + INIFile config; }; #endif // INSTANCEBASE_H diff --git a/data/instancelist.cpp b/data/instancelist.cpp new file mode 100644 index 00000000..7385ba86 --- /dev/null +++ b/data/instancelist.cpp @@ -0,0 +1,52 @@ +/* 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 "instancelist.h" + +#include +#include + +#include "stdinstance.h" + +#include "../util/pathutils.h" + +InstanceList::InstanceList() : + QList() +{ + +} + +void InstanceList::addInstance(InstanceBase *inst) +{ + append(inst); +} + +void InstanceList::loadInstances(QString dir) +{ + qDebug("Loading instances"); + QDir instDir(dir); + QDirIterator iter(instDir); + + while (iter.hasNext()) + { + QString subDir = iter.next(); + if (QFileInfo(PathCombine(subDir, "instance.cfg")).exists()) + { + // TODO Differentiate between different instance types. + InstanceBase* inst = new StdInstance(subDir); + addInstance(inst); + } + } +} diff --git a/data/instancelist.h b/data/instancelist.h new file mode 100644 index 00000000..83ea48e2 --- /dev/null +++ b/data/instancelist.h @@ -0,0 +1,38 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INSTANCELIST_H +#define INSTANCELIST_H + +#include + +#include "instancebase.h" + +class InstanceList : public QList +{ +public: + explicit InstanceList(); + + void addInstance(InstanceBase *inst); + + void loadInstances(QString dir); + +signals: + +public slots: + +}; + +#endif // INSTANCELIST_H diff --git a/data/settingsbase.cpp b/data/settingsbase.cpp new file mode 100644 index 00000000..66195603 --- /dev/null +++ b/data/settingsbase.cpp @@ -0,0 +1,22 @@ +/* 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 "settingsbase.h" + +SettingsBase::SettingsBase(QString fileName) : + QSettings(fileName, QSettings::IniFormat) +{ + +} diff --git a/data/settingsbase.h b/data/settingsbase.h new file mode 100644 index 00000000..8a7e7e77 --- /dev/null +++ b/data/settingsbase.h @@ -0,0 +1,33 @@ +/* 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 SETTINGSBASE_H +#define SETTINGSBASE_H + +#include + +#include "../util/settingsmacros.h" + +class SettingsBase : public QSettings +{ +public: + SettingsBase(QString fileName); + + +}; + +#include "../util/settingsmacrosundef.h" + +#endif // SETTINGSBASE_H diff --git a/data/settingsmacros.h b/data/settingsmacros.h new file mode 100644 index 00000000..94e52155 --- /dev/null +++ b/data/settingsmacros.h @@ -0,0 +1,35 @@ +/* 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 SETTINGSMACROS_H +#define SETTINGSMACROS_H + +#define STR_VAL(val) # val + +#define DEFINE_SETTING(funcName, name, defVal, typeName, toFunc) \ + virtual typeName Get ## funcName() const { return value(name). ## toFunc(); } \ + virtual void Set ## funcName(typeName value) { setValue(name, value); } \ + virtual void Reset ## funcName() { + +#define DEFINE_SETTING_STR(name, defVal) \ + DEFINE_SETTING(name, STR_VAL(name), defVal, QString, toString) + +#define DEFINE_SETTING_BOOL(name, defVal) \ + DEFINE_SETTING(name, STR_VAL(name), defVal, bool, toBool) + +#define DEFINE_SETTING_INT(name, defVal) \ + DEFINE_SETTING(name, STR_VAL(name), defVal, int, toInt) + +#endif // SETTINGSMACROS_H diff --git a/data/settingsmacrosundef.h b/data/settingsmacrosundef.h new file mode 100644 index 00000000..85b13bac --- /dev/null +++ b/data/settingsmacrosundef.h @@ -0,0 +1,26 @@ +/* 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 SETTINGSMACROSUNDEF_H +#define SETTINGSMACROSUNDEF_H + +#undef DEFINE_SETTING +#undef DEFINE_SETTING_STR +#undef DEFINE_SETTING_BOOL +#undef DEFINE_SETTING_INT + +#undef STR_VAL + +#endif // SETTINGSMACROSUNDEF_H diff --git a/data/stdinstance.cpp b/data/stdinstance.cpp new file mode 100644 index 00000000..8bb74097 --- /dev/null +++ b/data/stdinstance.cpp @@ -0,0 +1,22 @@ +/* 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 "stdinstance.h" + +StdInstance::StdInstance(QString dir) : + InstanceBase(dir) +{ + +} diff --git a/data/stdinstance.h b/data/stdinstance.h new file mode 100644 index 00000000..0bb8a0c3 --- /dev/null +++ b/data/stdinstance.h @@ -0,0 +1,28 @@ +/* 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 STDINSTANCE_H +#define STDINSTANCE_H + +#include "instancebase.h" + +// Standard client instance. +class StdInstance : public InstanceBase +{ +public: + StdInstance(QString dir); +}; + +#endif // STDINSTANCE_H diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 875cc256..ff9d0461 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -24,6 +24,10 @@ MainWindow::MainWindow(QWidget *parent) : ui(new Ui::MainWindow) { ui->setupUi(this); + instList.loadInstances("instances"); + + model.setInstanceList(&instList); + ui->instListView->setModel(&model); } MainWindow::~MainWindow() @@ -43,7 +47,7 @@ void MainWindow::on_actionViewInstanceFolder_triggered() void MainWindow::on_actionRefresh_triggered() { - + instList.loadInstances("instances"); } void MainWindow::on_actionViewCentralModsFolder_triggered() diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 6d9a0216..a735d78e 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -18,6 +18,9 @@ #include +#include "../data/instancelist.h" +#include "../data/instancelistmodel.h" + namespace Ui { class MainWindow; } @@ -51,6 +54,9 @@ private slots: private: Ui::MainWindow *ui; + + InstanceList instList; + InstanceListModel model; }; #endif // MAINWINDOW_H diff --git a/stdinstance.cpp b/stdinstance.cpp new file mode 100644 index 00000000..e69de29b diff --git a/stdinstance.h b/stdinstance.h new file mode 100644 index 00000000..e69de29b diff --git a/util/pathutils.cpp b/util/pathutils.cpp index 809dd6f2..8610b80d 100644 --- a/util/pathutils.cpp +++ b/util/pathutils.cpp @@ -26,6 +26,11 @@ QString PathCombine(QString path1, QString path2) return path1.append(path2); } +QString PathCombine(QString path1, QString path2, QString path3) +{ + return PathCombine(PathCombine(path1, path2), path3); +} + QString AbsolutePath(QString path) { return QFileInfo(path).absolutePath(); diff --git a/util/pathutils.h b/util/pathutils.h index bfd11bea..1922e3a0 100644 --- a/util/pathutils.h +++ b/util/pathutils.h @@ -19,6 +19,7 @@ #include QString PathCombine(QString path1, QString path2); +QString PathCombine(QString path1, QString path2, QString path3); QString AbsolutePath(QString path); -- cgit v1.2.3