GlosSIConfig: Refactor reading/writing target configs

pull/179/head
Peter Repukat 2 years ago
parent 028b18deb6
commit 54e6bf610b

@ -39,16 +39,16 @@ UIModel::UIModel() : QObject(nullptr)
std::filesystem::create_directories(path); std::filesystem::create_directories(path);
config_path_ = path; config_path_ = path;
config_dir_name_ = QString::fromStdWString((path /= "Targets").wstring().data()); config_dir_name_ = QString::fromStdWString((path /= "Targets").wstring());
if (!std::filesystem::exists(path)) if (!std::filesystem::exists(path))
std::filesystem::create_directories(path); std::filesystem::create_directories(path);
parseShortcutVDF(); parseShortcutVDF();
readConfigs(); readTargetConfigs();
} }
void UIModel::readConfigs() void UIModel::readTargetConfigs()
{ {
QDir dir(config_dir_name_); QDir dir(config_dir_name_);
auto entries = dir.entryList(QDir::Files, QDir::SortFlag::Name); auto entries = dir.entryList(QDir::Files, QDir::SortFlag::Name);
@ -68,29 +68,13 @@ void UIModel::readConfigs()
const auto data = file.readAll(); const auto data = file.readAll();
file.close(); file.close();
const auto jsondoc = QJsonDocument::fromJson(data); const auto jsondoc = QJsonDocument::fromJson(data);
const auto filejson = jsondoc.object(); auto filejson = jsondoc.object();
QJsonObject json; filejson["name"] = filejson.contains("name")
json["version"] = filejson["version"]; ? filejson["name"].toString()
json["icon"] = filejson["icon"]; : QString(name).replace(QRegularExpression("\\.json"), "");
json["launch"] = filejson["launch"]["launch"];
json["launchPath"] = filejson["launch"]["launchPath"]; targets_.append(filejson.toVariantMap());
json["launchAppArgs"] = filejson["launch"]["launchAppArgs"];
json["closeOnExit"] = filejson["launch"]["closeOnExit"];
json["waitForChildProcs"] = filejson["launch"]["waitForChildProcs"];
json["hideDevices"] = filejson["devices"]["hideDevices"];
json["realDeviceIds"] = filejson["devices"]["realDeviceIds"];
json["windowMode"] = filejson["window"]["windowMode"];
json["maxFps"] = filejson["window"]["maxFps"];
json["scale"] = filejson["window"]["scale"];
json["disableOverlay"] = filejson["window"]["disableOverlay"];
json["maxControllers"] = filejson["controller"]["maxControllers"];
json["allowDesktopConfig"] = filejson["controller"]["allowDesktopConfig"];
json["emulateDS4"] = filejson["controller"]["emulateDS4"];
json["name"] = filejson.contains("name") ? filejson["name"] : QString(name).replace(QRegularExpression("\\.json"), "");
targets_.append(json.toVariantMap());
}); });
emit targetListChanged(); emit targetListChanged();
@ -359,49 +343,22 @@ void UIModel::setAcrylicEffect(bool has_acrylic_affect)
emit acrylicChanged(); emit acrylicChanged();
} }
void UIModel::writeTarget(const QJsonObject& json, const QString& name) void UIModel::writeTarget(const QJsonObject& json, const QString& name) const
{ {
auto path = config_path_; auto path = config_path_;
path /= config_dir_name_.toStdWString(); path /= config_dir_name_.toStdWString();
path /= (QString(name).replace(QRegularExpression("[\\\\/:*?\"<>|]"), "") + ".json").toStdWString(); path /= (QString(name).replace(QRegularExpression("[\\\\/:*?\"<>|]"), "") + ".json").toStdWString();
QFile file(path); QFile file(path);
if (!file.open(QIODevice::Text | QIODevice::ReadWrite)) { if (!file.open(QIODevice::Text | QIODevice::ReadWrite)) {
// meh qDebug() << "Couldn't open file for writing: " << path;
return; return;
} }
QJsonObject fileJson;
fileJson["version"] = json["version"]; file.write(
fileJson["icon"] = json["icon"]; QString(QJsonDocument(json).toJson(QJsonDocument::Indented))
fileJson["name"] = json["name"]; .toStdString()
.data()
QJsonObject launchObject; );
launchObject["launch"] = json["launch"];
launchObject["launchPath"] = json["launchPath"];
launchObject["launchAppArgs"] = json["launchAppArgs"];
launchObject["closeOnExit"] = json["closeOnExit"];
launchObject["waitForChildProcs"] = json["waitForChildProcs"];
fileJson["launch"] = launchObject;
QJsonObject devicesObject;
devicesObject["hideDevices"] = json["hideDevices"];
devicesObject["realDeviceIds"] = json["realDeviceIds"];
fileJson["devices"] = devicesObject;
QJsonObject windowObject;
windowObject["windowMode"] = json["windowMode"];
windowObject["maxFps"] = json["maxFps"];
windowObject["scale"] = json["scale"];
windowObject["disableOverlay"] = json["disableOverlay"];
fileJson["window"] = windowObject;
QJsonObject controllerObject;
controllerObject["maxControllers"] = json["maxControllers"];
controllerObject["allowDesktopConfig"] = json["allowDesktopConfig"];
controllerObject["emulateDS4"] = json["emulateDS4"];
fileJson["controller"] = controllerObject;
auto wtf = QString(QJsonDocument(fileJson).toJson(QJsonDocument::Indented)).toStdString();
file.write(wtf.data());
file.close(); file.close();
} }

@ -33,7 +33,7 @@ class UIModel : public QObject {
public: public:
UIModel(); UIModel();
Q_INVOKABLE void readConfigs(); Q_INVOKABLE void readTargetConfigs();
Q_INVOKABLE QVariantList getTargetList() const; Q_INVOKABLE QVariantList getTargetList() const;
Q_INVOKABLE void addTarget(QVariant shortcut); Q_INVOKABLE void addTarget(QVariant shortcut);
Q_INVOKABLE void updateTarget(int index, QVariant shortcut); Q_INVOKABLE void updateTarget(int index, QVariant shortcut);
@ -64,17 +64,15 @@ class UIModel : public QObject {
void targetListChanged(); void targetListChanged();
private: private:
#ifdef _WIN32
bool is_windows_ = true;
#else
bool is_windows_ = false;
#endif
bool has_acrylic_affect_ = false;
std::filesystem::path config_path_; std::filesystem::path config_path_;
QString config_dir_name_; QString config_dir_name_;
void writeTarget(const QJsonObject& json, const QString& name);
std::filesystem::path getSteamPath() const;
std::wstring getSteamUserId() const;
bool foundSteam() const;
void parseShortcutVDF();
bool isSteamInputXboxSupportEnabled() const;
QString shortcutsfile_ = "/config/shortcuts.vdf"; QString shortcutsfile_ = "/config/shortcuts.vdf";
QString user_config_file_ = "/config/localconfig.vdf"; QString user_config_file_ = "/config/localconfig.vdf";
@ -83,11 +81,13 @@ class UIModel : public QObject {
QVariantList targets_; QVariantList targets_;
std::vector<VDFParser::Shortcut> shortcuts_vdf_; std::vector<VDFParser::Shortcut> shortcuts_vdf_;
void writeTarget(const QJsonObject& json, const QString& name) const;
#ifdef _WIN32 std::filesystem::path getSteamPath() const;
bool is_windows_ = true; std::wstring getSteamUserId() const;
#else bool foundSteam() const;
bool is_windows_ = false; void parseShortcutVDF();
#endif
bool has_acrylic_affect_ = false; bool isSteamInputXboxSupportEnabled() const;
}; };

@ -29,62 +29,58 @@ Item {
signal cancel() signal cancel()
signal done(var shortcut) signal done(var shortcut)
property var shortcutInfo: ({ property var shortcutInfo: ({})
version: 1,
name: null,
launch: false,
launchPath: null,
launchAppArgs: null,
closeOnExit: true,
waitForChildProcs: true,
hideDevices: true,
windowMode: false,
maxFps: null,
scale: null,
icon: null,
maxControllers: 1,
disableOverlay: false,
realDeviceIds: false,
allowDesktopConfig: false,
emulateDS4: false,
})
function resetInfo() { function resetInfo() {
shortcutInfo = ({ shortcutInfo = ({
version: 1, "controller": {
name: null, "maxControllers": 1,
launch: false, "emulateDS4": false,
launchPath: null, "allowDesktopConfig": false
launchAppArgs: null, },
closeOnExit: true, "devices": {
waitForChildProcs: true, "hideDevices": true,
hideDevices: true, "realDeviceIds": false
windowMode: false, },
maxFps: null, "icon": null,
scale: null, "launch": {
icon: null, "closeOnExit": true,
maxControllers: 1, "launch": false,
disableOverlay: false, "launchAppArgs": null,
realDeviceIds: false, "launchPath": null,
allowDesktopConfig: false, "waitForChildProcs": true
emulateDS4: false, },
"name": null,
"version": 1,
"window": {
"disableOverlay": false,
"maxFps": null,
"scale": null,
"windowMode": false
},
"extendedLogging": false
}) })
} }
Component.onCompleted: function() {
resetInfo()
}
onShortcutInfoChanged: function() { onShortcutInfoChanged: function() {
nameInput.text = shortcutInfo.name || "" nameInput.text = shortcutInfo.name || ""
launchApp.checked = shortcutInfo.launch || false extendedLogging.checked = shortcutInfo.extendedLogging
pathInput.text = shortcutInfo.launchPath || "" launchApp.checked = shortcutInfo.launch.launch
argsInput.text = shortcutInfo.launchAppArgs || "" pathInput.text = shortcutInfo.launch.launchPath || ""
closeOnExit.checked = shortcutInfo.closeOnExit || false argsInput.text = shortcutInfo.launch.launchAppArgs || ""
waitForChildren.checked = shortcutInfo.waitForChildProcs closeOnExit.checked = shortcutInfo.launch.closeOnExit
hideDevices.checked = shortcutInfo.hideDevices || false waitForChildren.checked = shortcutInfo.launch.waitForChildProcs
windowMode.checked = shortcutInfo.windowMode || false hideDevices.checked = shortcutInfo.devices.hideDevices
maxControllersSpinBox.value = shortcutInfo.maxControllers realDeviceIds.checked = shortcutInfo.devices.realDeviceIds
disableOverlayCheckbox.checked = shortcutInfo.disableOverlay || false windowMode.checked = shortcutInfo.window.windowMode
realDeviceIds.checked = shortcutInfo.realDeviceIds || false disableOverlayCheckbox.checked = shortcutInfo.window.disableOverlay
allowDesktopConfig.checked = shortcutInfo.allowDesktopConfig || false maxControllersSpinBox.value = shortcutInfo.controller.maxControllers
emulateDS4.checked = shortcutInfo.emulateDS4 || false allowDesktopConfig.checked = shortcutInfo.controller.allowDesktopConfig
emulateDS4.checked = shortcutInfo.controller.emulateDS4
} }
Flickable { Flickable {
@ -159,9 +155,9 @@ Item {
CheckBox { CheckBox {
id: launchApp id: launchApp
text: qsTr("Launch app") text: qsTr("Launch app")
checked: shortcutInfo.launch checked: shortcutInfo.launch.launch
onCheckedChanged: function() { onCheckedChanged: function() {
shortcutInfo.launch = checked shortcutInfo.launch.launch = checked
if (checked) { if (checked) {
closeOnExit.enabled = true; closeOnExit.enabled = true;
if (closeOnExit.checked) { if (closeOnExit.checked) {
@ -181,9 +177,9 @@ Item {
CheckBox { CheckBox {
id: closeOnExit id: closeOnExit
text: qsTr("Close when launched app quits") text: qsTr("Close when launched app quits")
checked: shortcutInfo.closeOnExit checked: shortcutInfo.launch.closeOnExit
onCheckedChanged: function() { onCheckedChanged: function() {
shortcutInfo.closeOnExit = checked shortcutInfo.launch.closeOnExit = checked
if (checked) { if (checked) {
waitForChildren.enabled = true; waitForChildren.enabled = true;
} else { } else {
@ -201,9 +197,9 @@ Item {
CheckBox { CheckBox {
id: waitForChildren id: waitForChildren
text: qsTr("Wait for child processes") text: qsTr("Wait for child processes")
checked: shortcutInfo.waitForChildProcs checked: shortcutInfo.launch.waitForChildProcs
onCheckedChanged: function(){ onCheckedChanged: function(){
shortcutInfo.waitForChildProcs = checked shortcutInfo.launch.waitForChildProcs = checked
} }
} }
} }
@ -212,9 +208,9 @@ Item {
CheckBox { CheckBox {
id: allowDesktopConfig id: allowDesktopConfig
text: qsTr("Allow desktop-config") text: qsTr("Allow desktop-config")
checked: shortcutInfo.allowDesktopConfig checked: shortcutInfo.controller.allowDesktopConfig
onCheckedChanged: function(){ onCheckedChanged: function(){
shortcutInfo.allowDesktopConfig = checked shortcutInfo.controller.allowDesktopConfig = checked
} }
} }
Label { Label {
@ -238,7 +234,7 @@ Item {
? shortcutInfo.icon.endsWith(".exe") ? shortcutInfo.icon.endsWith(".exe")
? "image://exe/" + shortcutInfo.icon ? "image://exe/" + shortcutInfo.icon
: "file:///" + shortcutInfo.icon : "file:///" + shortcutInfo.icon
: null : ''
Layout.preferredWidth: 48 Layout.preferredWidth: 48
Layout.preferredHeight: 48 Layout.preferredHeight: 48
visible: shortcutInfo.icon visible: shortcutInfo.icon
@ -267,8 +263,8 @@ Item {
id: pathInput id: pathInput
placeholderText: qsTr("...") placeholderText: qsTr("...")
enabled: launchApp.checked enabled: launchApp.checked
text: shortcutInfo.launchPath || "" text: shortcutInfo.launch.launchPath || ""
onTextChanged: shortcutInfo.launchPath = text onTextChanged: shortcutInfo.launch.launchPath = text
} }
} }
Button { Button {
@ -304,8 +300,8 @@ Item {
anchors.topMargin: 4 anchors.topMargin: 4
id: argsInput id: argsInput
enabled: launchApp.checked enabled: launchApp.checked
text: shortcutInfo.launchAppArgs text: shortcutInfo.launch.launchAppArgs
onTextChanged: shortcutInfo.launchAppArgs = text onTextChanged: shortcutInfo.launch.launchAppArgs = text
} }
} }
} }
@ -333,8 +329,8 @@ Item {
CheckBox { CheckBox {
id: hideDevices id: hideDevices
text: qsTr("Hide (Real) Controllers") text: qsTr("Hide (Real) Controllers")
checked: shortcutInfo.hideDevices checked: shortcutInfo.devices.hideDevices
onCheckedChanged: shortcutInfo.hideDevices = checked onCheckedChanged: shortcutInfo.devices.hideDevices = checked
} }
RoundButton { RoundButton {
onClicked: () => { onClicked: () => {
@ -366,8 +362,8 @@ Item {
CheckBox { CheckBox {
id: realDeviceIds id: realDeviceIds
text: qsTr("Use real device (USB)-IDs") text: qsTr("Use real device (USB)-IDs")
checked: shortcutInfo.realDeviceIds checked: shortcutInfo.devices.realDeviceIds
onCheckedChanged: shortcutInfo.realDeviceIds = checked onCheckedChanged: shortcutInfo.devices.realDeviceIds = checked
} }
RoundButton { RoundButton {
onClicked: () => { onClicked: () => {
@ -399,8 +395,8 @@ Item {
CheckBox { CheckBox {
id: emulateDS4 id: emulateDS4
text: qsTr("Emulate DS4") text: qsTr("Emulate DS4")
checked: shortcutInfo.emulateDS4 checked: shortcutInfo.controller.emulateDS4 || false
onCheckedChanged: shortcutInfo.emulateDS4 = checked onCheckedChanged: shortcutInfo.controller.emulateDS4 = checked
} }
RoundButton { RoundButton {
onClicked: () => { onClicked: () => {
@ -438,10 +434,10 @@ Item {
SpinBox { SpinBox {
id: maxControllersSpinBox id: maxControllersSpinBox
width: 128 width: 128
value: 4 value: shortcutInfo.controller.maxControllers
from: 0 from: 0
to: 4 to: 4
onValueChanged: shortcutInfo.maxControllers = value onValueChanged: shortcutInfo.controller.maxControllers = value
} }
RoundButton { RoundButton {
onClicked: () => { onClicked: () => {
@ -479,8 +475,8 @@ Item {
CheckBox { CheckBox {
id: windowMode id: windowMode
text: qsTr("Steam/GlosSI overlay as separate window") text: qsTr("Steam/GlosSI overlay as separate window")
checked: shortcutInfo.windowMode checked: shortcutInfo.window.windowMode
onCheckedChanged: shortcutInfo.windowMode = checked onCheckedChanged: shortcutInfo.window.windowMode = checked
} }
RoundButton { RoundButton {
onClicked: () => { onClicked: () => {
@ -513,8 +509,8 @@ Item {
CheckBox { CheckBox {
id: disableOverlayCheckbox id: disableOverlayCheckbox
text: qsTr("Disable Steam/GlosSI overlay") text: qsTr("Disable Steam/GlosSI overlay")
checked: shortcutInfo.disableOverlay checked: shortcutInfo.window.disableOverlay
onCheckedChanged: shortcutInfo.disableOverlay = checked onCheckedChanged: shortcutInfo.window.disableOverlay = checked
} }
RoundButton { RoundButton {
onClicked: () => { onClicked: () => {

Loading…
Cancel
Save