Added stuff.

This commit is contained in:
Andrew 2013-01-14 17:42:38 -06:00
parent 586092cf99
commit fce0f5df04
22 changed files with 461 additions and 18 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by Qt Creator 2.6.1, 2013-01-09T12:15:16. -->
<!-- Written by Qt Creator 2.6.1, 2013-01-14T15:28:18. -->
<qtcreator>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>

22
data/appsettings.cpp Normal file
View File

@ -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)
{
}

27
data/appsettings.h Normal file
View File

@ -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

75
data/inifile.cpp Normal file
View File

@ -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 <QFile>
#include <QTextStream>
#include <QStringList>
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;
}

36
data/inifile.h Normal file
View File

@ -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 <QMap>
#include <QString>
#include <QVariant>
// Sectionless INI parser (for instance config files)
class INIFile : public QMap<QString, QVariant>
{
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

View File

@ -15,31 +15,35 @@
#include "instancebase.h"
#include <QFileInfo>
#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);
}

View File

@ -18,7 +18,8 @@
#include <QObject>
#include <QString>
#include <QSettings>
#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

52
data/instancelist.cpp Normal file
View File

@ -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 <QDir>
#include <QDirIterator>
#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);
}
}
}

38
data/instancelist.h Normal file
View File

@ -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 <QList>
#include "instancebase.h"
class InstanceList : public QList<InstanceBase*>
{
public:
explicit InstanceList();
void addInstance(InstanceBase *inst);
void loadInstances(QString dir);
signals:
public slots:
};
#endif // INSTANCELIST_H

22
data/settingsbase.cpp Normal file
View File

@ -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)
{
}

33
data/settingsbase.h Normal file
View File

@ -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 <QSettings>
#include "../util/settingsmacros.h"
class SettingsBase : public QSettings
{
public:
SettingsBase(QString fileName);
};
#include "../util/settingsmacrosundef.h"
#endif // SETTINGSBASE_H

35
data/settingsmacros.h Normal file
View File

@ -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

View File

@ -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

22
data/stdinstance.cpp Normal file
View File

@ -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)
{
}

28
data/stdinstance.h Normal file
View File

@ -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

View File

@ -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()

View File

@ -18,6 +18,9 @@
#include <QMainWindow>
#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

0
stdinstance.cpp Normal file
View File

0
stdinstance.h Normal file
View File

View File

@ -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();

View File

@ -19,6 +19,7 @@
#include <QString>
QString PathCombine(QString path1, QString path2);
QString PathCombine(QString path1, QString path2, QString path3);
QString AbsolutePath(QString path);