diff --git a/GlosSIConfig/GlosSIConfig.vcxproj b/GlosSIConfig/GlosSIConfig.vcxproj index 28bde64..bb55052 100644 --- a/GlosSIConfig/GlosSIConfig.vcxproj +++ b/GlosSIConfig/GlosSIConfig.vcxproj @@ -106,7 +106,6 @@ - @@ -121,7 +120,6 @@ - diff --git a/GlosSIConfig/GlosSIConfig.vcxproj.filters b/GlosSIConfig/GlosSIConfig.vcxproj.filters index 7abfc2c..7983baf 100644 --- a/GlosSIConfig/GlosSIConfig.vcxproj.filters +++ b/GlosSIConfig/GlosSIConfig.vcxproj.filters @@ -34,9 +34,6 @@ Source Files - - Source Files - @@ -62,9 +59,6 @@ Header Files - - Header Files - diff --git a/GlosSIConfig/TargetConfig.cpp b/GlosSIConfig/TargetConfig.cpp deleted file mode 100644 index 0b2e3e1..0000000 --- a/GlosSIConfig/TargetConfig.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* -Copyright 2021 Peter Repukat - FlatspotSoftware - -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 "TargetConfig.h" - -TargetConfig::TargetConfig() : QObject(nullptr) -{ - -} - -QString TargetConfig::getName() const -{ - return name_; -} - -void TargetConfig::setName(const QString& name) -{ - name_ = name; - emit nameChanged(); -} - -bool TargetConfig::getLaunch() const -{ - return launch_; -} - -void TargetConfig::setLaunch(const bool launch) -{ - launch_ = launch; -} - -QString TargetConfig::getLaunchPath() const -{ - return launch_path_; -} - -void TargetConfig::setLaunchPath(const QString& launch_path) -{ - launch_path_ = launch_path; -} - -QString TargetConfig::getLaunchAppArgs() const -{ - return launch_app_args_; -} - -void TargetConfig::setLaunchAppArgs(const QString& launch_app_args) -{ - launch_app_args_ = launch_app_args; -} - -bool TargetConfig::getCloseOnExit() const -{ - return close_on_exit_; -} - -void TargetConfig::setCloseOnExit(const bool close_on_exit) -{ - close_on_exit_ = close_on_exit; -} - -bool TargetConfig::getHideDevices() const -{ - return hide_devices_; -} - -void TargetConfig::setHideDevices(const bool hide_devices) -{ - hide_devices_ = hide_devices; -} - -bool TargetConfig::getWindowMode() const -{ - return window_mode_; -} - -void TargetConfig::setWindowMode(const bool window_mode) -{ - window_mode_ = window_mode; -} - -int TargetConfig::getMaxFps() const -{ - return max_fps_; -} - -void TargetConfig::setMaxFps(const int max_fps) -{ - max_fps_ = max_fps; -} - -float TargetConfig::getScale() const -{ - return scale_; -} - -void TargetConfig::setScale(const float scale) -{ - scale_ = scale; -} diff --git a/GlosSIConfig/TargetConfig.h b/GlosSIConfig/TargetConfig.h deleted file mode 100644 index f199391..0000000 --- a/GlosSIConfig/TargetConfig.h +++ /dev/null @@ -1,80 +0,0 @@ -/* -Copyright 2021 Peter Repukat - FlatspotSoftware - -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. -*/ -#pragma once -#include -#include -class TargetConfig : public QObject -{ - Q_OBJECT - - Q_PROPERTY(QString name READ getName WRITE setName NOTIFY nameChanged) - Q_PROPERTY(bool launch READ getLaunch WRITE setLaunch NOTIFY launchChanged) - Q_PROPERTY(QString launchPath READ getLaunchPath WRITE setLaunchPath NOTIFY launchPathChanged) - Q_PROPERTY(QString launchAppArgs READ getLaunchAppArgs WRITE setLaunchAppArgs NOTIFY launchAppArgsChanged) - Q_PROPERTY(bool closeOnExit READ getCloseOnExit WRITE setCloseOnExit NOTIFY closeOnExitChanged) - Q_PROPERTY(bool hideDevices READ getHideDevices WRITE setHideDevices NOTIFY hideDevicesChanged) - Q_PROPERTY(bool windowMode READ getWindowMode WRITE setWindowMode NOTIFY windowModeChanged) - Q_PROPERTY(int maxFps READ getMaxFps WRITE setMaxFps NOTIFY maxFpsChanged) - Q_PROPERTY(float scale READ getScale WRITE setScale NOTIFY scaleChanged) -public: - TargetConfig(); - - [[nodiscard]] QString getName() const; - void setName(const QString& name); - [[nodiscard]] bool getLaunch() const; - void setLaunch(const bool launch); - [[nodiscard]] QString getLaunchPath() const; - void setLaunchPath(const QString& launch_path); - [[nodiscard]] QString getLaunchAppArgs() const; - void setLaunchAppArgs(const QString& launch_app_args); - [[nodiscard]] bool getCloseOnExit() const; - void setCloseOnExit(const bool close_on_exit); - [[nodiscard]] bool getHideDevices() const; - void setHideDevices(const bool hide_devices); - [[nodiscard]] bool getWindowMode() const; - void setWindowMode(const bool window_mode); - [[nodiscard]] int getMaxFps() const; - void setMaxFps(const int max_fps); - [[nodiscard]] float getScale() const; - void setScale(const float scale); - - -signals: - void nameChanged(); - void launchChanged(); - void launchPathChanged(); - void launchAppArgsChanged(); - void closeOnExitChanged(); - void hideDevicesChanged(); - void windowModeChanged(); - void maxFpsChanged(); - void scaleChanged(); - - -private: - static inline constexpr int version_ = 1; - QString name_; - bool launch_ = true; - QString launch_path_; - QString launch_app_args_; - bool close_on_exit_ = true; - bool hide_devices_ = true; - bool window_mode_ = false; - int max_fps_ = 0; - float scale_ = 0; - -}; - diff --git a/GlosSIConfig/UIModel.cpp b/GlosSIConfig/UIModel.cpp index 235293d..e370812 100644 --- a/GlosSIConfig/UIModel.cpp +++ b/GlosSIConfig/UIModel.cpp @@ -16,9 +16,13 @@ limitations under the License. #include "UIModel.h" #include +#include +#include + #include "TargetConfig.h" + UIModel::UIModel() : QObject(nullptr) { auto path = std::filesystem::temp_directory_path() @@ -33,6 +37,11 @@ UIModel::UIModel() : QObject(nullptr) config_path_ = path; config_dir_name_ = (path /= "Targets").string().data(); + + if (!std::filesystem::exists(path)) + std::filesystem::create_directories(path); + + readConfigs(); } void UIModel::readConfigs() @@ -40,17 +49,25 @@ void UIModel::readConfigs() QDir dir(config_dir_name_); auto entries = dir.entryList(QDir::Files, QDir::SortFlag::Name); entries.removeIf([](const auto& entry) { - return entry.endsWith(".json"); - }); - QStringList fileNames; - std::ranges::transform(fileNames, std::back_inserter(fileNames), [](const auto& entry) - { - return entry.mid(0, entry.lastIndexOf(".json")); + return !entry.endsWith(".json"); }); - std::ranges::for_each(fileNames, [this](const auto& name) + std::ranges::for_each(entries, [this](const auto& name) { - targets_.append(QMap{ { "name", name }}); + auto path = config_path_; + path /= config_dir_name_.toStdString(); + path /= name.toStdString(); + QFile file(path); + if (!file.open(QIODevice::Text | QIODevice::ReadOnly)) + { + // meh + return; + } + const auto data = file.readAll(); + file.close(); + const auto jsondoc = QJsonDocument::fromJson(data); + const auto json = jsondoc.object(); + targets_.append(json.toVariantMap()); }); @@ -65,40 +82,25 @@ QVariantList UIModel::getTargetList() const void UIModel::addTarget(QVariant shortcut) { // TODO: write config - auto map = shortcut.toMap(); - - QVariantMap copy; - copy.insert("name", map["name"].toString()); - copy.insert("launch", map["launch"].toBool()); - copy.insert("launchPath", map["launchPath"].toString()); - copy.insert("launchAppArgs", map["launchAppArgs"].toString()); - copy.insert("closeOnExit", map["closeOnExit"].toBool()); - copy.insert("hideDevices", map["hideDevices"].toBool()); - copy.insert("windowMode", map["windowMode"].toBool()); - copy.insert("maxFps", map["maxFps"].toInt()); - copy.insert("scale", map["scale"].toInt()); - - targets_.append(copy); + const auto map = shortcut.toMap(); + const auto json = QJsonDocument(QJsonObject::fromVariantMap(map)); + auto wtf = json.toJson(QJsonDocument::Indented).toStdString(); + + writeTarget(wtf, map["name"].toString()); + + targets_.append(json.toVariant()); emit targetListChanged(); } void UIModel::updateTarget(int index, QVariant shortcut) { + const auto map = shortcut.toMap(); + const auto json = QJsonDocument(QJsonObject::fromVariantMap(map)); + auto wtf = json.toJson(QJsonDocument::Indented).toStdString(); - auto map = shortcut.toMap(); + writeTarget(wtf, map["name"].toString()); - QVariantMap copy; - copy.insert("name", map["name"].toString()); - copy.insert("launch", map["launch"].toBool()); - copy.insert("launchPath", map["launchPath"].toString()); - copy.insert("launchAppArgs", map["launchAppArgs"].toString()); - copy.insert("closeOnExit", map["closeOnExit"].toBool()); - copy.insert("hideDevices", map["hideDevices"].toBool()); - copy.insert("windowMode", map["windowMode"].toBool()); - copy.insert("maxFps", map["maxFps"].toInt()); - copy.insert("scale", map["scale"].toInt()); - - targets_.replace(index, copy); + targets_.replace(index, json.toVariant()); emit targetListChanged(); } @@ -117,3 +119,19 @@ void UIModel::setAcrylicEffect(bool has_acrylic_affect) has_acrylic_affect_ = has_acrylic_affect; emit acrylicChanged(); } + +void UIModel::writeTarget(const std::string& json, const QString& name) +{ + auto path = config_path_; + path /= config_dir_name_.toStdString(); + path /= (name + ".json").toStdString(); + QFile file(path); + if (!file.open(QIODevice::Text | QIODevice::ReadWrite)) + { + // meh + return; + } + file.write(json.data()); + file.close(); +} + diff --git a/GlosSIConfig/UIModel.h b/GlosSIConfig/UIModel.h index e763123..758ab5c 100644 --- a/GlosSIConfig/UIModel.h +++ b/GlosSIConfig/UIModel.h @@ -46,6 +46,8 @@ private: std::filesystem::path config_path_; QString config_dir_name_; + void writeTarget(const std::string& json, const QString& name); + QVariantList targets_; #ifdef _WIN32 diff --git a/GlosSIConfig/qml/main.qml b/GlosSIConfig/qml/main.qml index bf4611a..0c51736 100644 --- a/GlosSIConfig/qml/main.qml +++ b/GlosSIConfig/qml/main.qml @@ -144,8 +144,6 @@ Window { onEditClicked: function(index, shortcutInfo){ shortcutProps.opacity = 1; homeContent.opacity = 0; - console.log(index) - Object.keys(shortcutInfo).forEach((key) => console.log(key, shortcutInfo[key])) props.shortcutInfo = shortcutInfo windowContent.editedIndex = index; }