data storage work
parent
a2a8b332d8
commit
ca05bbce4d
|
@ -1,24 +1,48 @@
|
|||
#include "datamanager.h"
|
||||
|
||||
DataManager::DataManager(QFile file)
|
||||
DataManager::DataManager(QString filename)
|
||||
{
|
||||
if (file.exists() && file.size() > 0) {
|
||||
file.open(QIODevice::ReadOnly);
|
||||
jsonDocument = QJsonDocument::fromJson(file.readAll());
|
||||
if (jsonDocument != nullptr) {
|
||||
jsonObject = jsonDocument.object();
|
||||
if (jsonObject["instances"].isArray()) {
|
||||
profileArray = jsonObject["instances"].toArray();
|
||||
for (int i = 0; i < profileArray.size(); i++) {
|
||||
profiles.push_back(Profile::fromJson(profileArray.at(i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
jsonDocument = QJsonDocument();
|
||||
jsonObject = defaultObject;
|
||||
profileArray = defaultObject["instances"];
|
||||
}
|
||||
file.close();
|
||||
file = new QFile(filename);
|
||||
qDebug(file->fileName().toStdString().c_str());
|
||||
if (file->exists() && file->size() > 0) {
|
||||
file->open(QIODevice::ReadOnly);
|
||||
jsonObject = QJsonDocument::fromJson(file->readAll()).object();
|
||||
file->close();
|
||||
}
|
||||
else {
|
||||
jsonObject = QJsonObject();
|
||||
}
|
||||
}
|
||||
|
||||
void DataManager::loadProfiles() {
|
||||
if (jsonObject["instances"].isArray()) {
|
||||
QJsonArray profileArray = jsonObject["instances"].toArray();
|
||||
for (int i = 0; i < profileArray.size(); i++) {
|
||||
manager->addProfile(Profile::fromJson(profileArray.at(i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DataManager::saveProfiles()
|
||||
{
|
||||
QJsonArray profileArray = QJsonArray();
|
||||
for (Profile profile : manager->profiles) {
|
||||
profileArray.append(profile.toJson());
|
||||
}
|
||||
jsonObject["instances"] = profileArray;
|
||||
|
||||
qDebug("Saved profiles");
|
||||
|
||||
save();
|
||||
}
|
||||
|
||||
void DataManager::save()
|
||||
{
|
||||
file->open(QIODevice::WriteOnly | QIODevice::Truncate);
|
||||
QJsonDocument document;
|
||||
document.setObject(jsonObject);
|
||||
file->write(document.toJson());
|
||||
file->close();
|
||||
|
||||
qDebug("Saved");
|
||||
}
|
||||
|
|
|
@ -1,25 +1,34 @@
|
|||
#ifndef DATAMANAGER_H
|
||||
#define DATAMANAGER_H
|
||||
|
||||
#include "profilemanager.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
const QJsonObject DEFAULT_OBJECT
|
||||
{
|
||||
{"instances", QJsonArray()}
|
||||
};
|
||||
//QString DEFAULT_PATH = QDir::homePath() + "/.minetest/minetestlauncher.conf";
|
||||
|
||||
class DataManager
|
||||
{
|
||||
public:
|
||||
DataManager(QFile);
|
||||
|
||||
QJsonObject defaultObject
|
||||
{
|
||||
{"instances", QJsonArray()}
|
||||
};
|
||||
DataManager(QString);
|
||||
inline void setManager(ProfileManager *mgr) { manager = mgr; }
|
||||
void loadProfiles();
|
||||
void saveProfiles();
|
||||
void save();
|
||||
|
||||
private:
|
||||
QJsonDocument jsonDocument;
|
||||
ProfileManager *manager;
|
||||
QJsonObject jsonObject;
|
||||
QJsonArray profileArray;
|
||||
QFile *file;
|
||||
};
|
||||
|
||||
#endif // DATAMANAGER_H
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
#include <QFile>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow)
|
||||
|
@ -9,6 +11,8 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||
connect(ui->createBtn, SIGNAL(clicked()), this, SLOT(newProfile()));
|
||||
connect(ui->deleteBtn, SIGNAL(clicked()), this, SLOT(deleteSelected()));
|
||||
connect(ui->playBtn, SIGNAL(clicked()), this, SLOT(playSelected()));
|
||||
|
||||
dataManager->setManager(profileManager);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
|
@ -18,17 +22,19 @@ MainWindow::~MainWindow()
|
|||
|
||||
void MainWindow::newProfile()
|
||||
{
|
||||
CreateDialog::newProfile(manager);
|
||||
manager->refreshList(ui->comboBox);
|
||||
CreateDialog::newProfile(profileManager);
|
||||
profileManager->refreshList(ui->comboBox);
|
||||
dataManager->saveProfiles();
|
||||
}
|
||||
|
||||
void MainWindow::deleteSelected()
|
||||
{
|
||||
manager->deleteByName(ui->comboBox->currentText());
|
||||
manager->refreshList(ui->comboBox);
|
||||
profileManager->deleteByName(ui->comboBox->currentText());
|
||||
profileManager->refreshList(ui->comboBox);
|
||||
dataManager->saveProfiles();
|
||||
}
|
||||
|
||||
void MainWindow::playSelected()
|
||||
{
|
||||
manager->getByName(ui->comboBox->currentText()).play(ui->tabWidget);
|
||||
profileManager->getByName(ui->comboBox->currentText()).play(ui->tabWidget);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,10 @@
|
|||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include "createdialog.h"
|
||||
#include "datamanager.h"
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
|
@ -23,7 +26,8 @@ private slots:
|
|||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
ProfileManager *manager = new ProfileManager();
|
||||
DataManager *dataManager = new DataManager(QDir::homePath() + "/.minetest/minetestlauncher.conf");
|
||||
ProfileManager *profileManager = new ProfileManager();
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
|
|
|
@ -6,16 +6,17 @@ Profile::Profile(QString n, QString v)
|
|||
version = v;
|
||||
}
|
||||
|
||||
Profile Profile::fromJson(QJsonObject obj)
|
||||
Profile Profile::fromJson(QJsonValue val)
|
||||
{
|
||||
QJsonObject obj = val.toObject();
|
||||
return Profile(obj.value("name").toString(), obj.value("version").toString());
|
||||
}
|
||||
|
||||
QJsonObject Profile::toJson()
|
||||
{
|
||||
QJsonObject object;
|
||||
object["name"] = name;
|
||||
object["version"] = version;
|
||||
object.insert("name", name);
|
||||
object.insert("version", version);
|
||||
return object;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ class Profile
|
|||
{
|
||||
public:
|
||||
Profile(QString, QString);
|
||||
static Profile fromJson(QJsonObject);
|
||||
static Profile fromJson(QJsonValue);
|
||||
QJsonObject toJson();
|
||||
QDir getPath();
|
||||
|
||||
|
|
|
@ -16,8 +16,6 @@ public:
|
|||
bool deleteByName(QString);
|
||||
void refreshList(QComboBox*);
|
||||
vector<Profile> profiles;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // PROFILEMANAGER_H
|
||||
|
|
Loading…
Reference in New Issue