Browse Source

Добавлен класс для работы с файлом конфигураций

master
Тим 10 months ago
parent
commit
4670d56b5d
3 changed files with 171 additions and 2 deletions
  1. 112
    0
      WBConfig.cpp
  2. 55
    0
      WBConfig.h
  3. 4
    2
      WineBottle.pro

+ 112
- 0
WBConfig.cpp View File

@@ -0,0 +1,112 @@
#include "WBConfig.h"
#include <QDir>

stWBBottle::stWBBottle(QString bottle, QString wine, QString launcher)
: bottle(bottle), wine(wine), launcher(launcher){
auto modPath = [](QString &path){
if (path.isEmpty())
path = "/";
else if (path.back() != '/')
path.push_back('/');
};
modPath(this->bottle);
modPath(this->wine);
}

stWBProgram::stWBProgram(QString bottleName, QStringList args, QStringList envs)
: bottleName(bottleName), args(args)
{
if (!envs.isEmpty())
this->envs = new Environments(envs);
else this->envs = nullptr;
}

stWBProgram::~stWBProgram()
{
if (envs)
delete envs;
}

WBConfig::WBConfig(QObject *parent)
: QObject (parent)
{
_settings = new QSettings("Prime-Hack", "WineBottle", this);

_bottleCount = _settings->value("General/BottleCount", 0).toUInt();
_lastBottleId = _settings->value("General/LastBottle", 0).toUInt();

if (!_bottleCount){
QDir defaulthBottle(QDir::homePath() + ".wine");
if (defaulthBottle.exists())
_bottles["Default"] = stWBBottle(defaulthBottle.path());
} else {
for (uint id = 1; id <= _bottleCount; ++id){
QString section = "Bottle_" + QString::number(id) + "/";
stWBBottle btl = stWBBottle(
_settings->value(section + "bottle").toString(),
_settings->value(section + "wine").toString(),
_settings->value(section + "launcher").toString()
);
_bottles[_settings->value(section + "name").toString()] = btl;
if (_lastBottleId == id)
_lastBottle = _settings->value(section + "name").toString();
}
}
if (_lastBottle.isEmpty() && !_bottles.isEmpty())
_lastBottle = _bottles.firstKey();
}

WBConfig::~WBConfig()
{
delete _settings;
}

stWBBottle &WBConfig::bottle(const QString &name)
{
return _bottles[name];
}

QStringList WBConfig::names()
{
return _bottles.keys();
}

stWBProgram WBConfig::loadProgram(const QString &name)
{
auto bottleName = _settings->value(name + "/Bottle", "").toString();
if (bottleName.isEmpty())
return stWBProgram(_lastBottle);
auto program = stWBProgram(bottleName,
_settings->value(name + "/Args", "").toString().split(" "),
_settings->value(name + "/AddEnv", "").toString().split("\n")
);
auto window = _settings->value(name + "/Window", "0").toString();
// `window == "true"` is for compatible with WB2
if (window == "true" || window == "1")
program.deskMode = eWineDesktop::WD_wine;
else if (window == "2")
program.deskMode = eWineDesktop::WD_xephyr;
program.wndSize.rx() = _settings->value(name + "/WinX", 1024).toInt();
program.wndSize.ry() = _settings->value(name + "/WinY", 768).toInt();
program.dxMode = _settings->value(name + "/DirectX", -1).toInt();
auto log = _settings->value(name + "/Logging", "").toString();
// `log == "true"` is for compatible with WB2
if (log == "true" || log == "2")
program.logMode = eWineLog::WL_file;
else if (log == "1")
program.logMode = eWineLog::WL_default;
return program;
}

void WBConfig::saveProgram(const QString &name, const stWBProgram &program)
{
_settings->setValue(name + "/Bottle", program.bottleName);
_settings->setValue(name + "/Args", program.args);
if (program.envs)
_settings->setValue(name + "/AddEnv", program.envs->environments());
_settings->setValue(name + "/Window", program.deskMode);
_settings->setValue(name + "/WinX", program.wndSize.x());
_settings->setValue(name + "/WinY", program.wndSize.y());
_settings->setValue(name + "/DirectX", program.dxMode);
_settings->setValue(name + "/Logging", program.logMode);
}

+ 55
- 0
WBConfig.h View File

@@ -0,0 +1,55 @@
#ifndef WBCONFIG_H
#define WBCONFIG_H

#include <QSettings>
#include <QMap>
#include "Environments.h"
#include "wine/Execute.h"

struct stWBBottle{
QString bottle;
QString wine;
QString launcher;

stWBBottle(QString bottle, QString wine = "/usr/", QString launcher = "");
stWBBottle(){}
};

struct stWBProgram{
QString bottleName;
QStringList args;
Environments *envs;
int dxMode = 0;
eWineLog logMode = eWineLog::WL_disable;
eWineDesktop deskMode = eWineDesktop::WD_disable;
QPoint wndSize;

stWBProgram(QString bottleName, QStringList args = QStringList(), QStringList envs = QStringList());
stWBProgram(){}
~stWBProgram();
};

// TODO: синглтон, ибо не только в WBMain нужен, а и в настройках
class WBConfig : QObject
{
Q_OBJECT
public:
explicit WBConfig(QObject *parent = nullptr);
virtual ~WBConfig();

virtual stWBBottle &bottle(const QString &name);
virtual QStringList names();

virtual stWBProgram loadProgram(const QString &name);
virtual void saveProgram(const QString &name, const stWBProgram &program);

protected:
QSettings *_settings;

uint _bottleCount;
uint _lastBottleId;
QString _lastBottle;
QMap<QString, stWBBottle> _bottles;
};

#endif // WBCONFIG_H

+ 4
- 2
WineBottle.pro View File

@@ -31,7 +31,8 @@ SOURCES += \
WBTmp.cpp \
ui/WBFace.cpp \
wine/Execute.cpp \
Environments.cpp
Environments.cpp \
WBConfig.cpp

HEADERS += \
WBItem.h \
@@ -43,7 +44,8 @@ HEADERS += \
PEHeaders.h \
wine/Execute.h \
Environments.h \
GetterSetter.hpp
GetterSetter.hpp \
WBConfig.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin

Loading…
Cancel
Save