You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GlosSI/GlosSIConfig/UIModel.cpp

138 lines
3.4 KiB
C++

/*
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 "UIModel.h"
#include <QDir>
#include <QJsonObject>
#include <QJsonDocument>
#include "TargetConfig.h"
UIModel::UIModel() : QObject(nullptr)
{
auto path = std::filesystem::temp_directory_path()
.parent_path()
.parent_path()
.parent_path();
path /= "Roaming";
path /= "GlosSI";
if (!std::filesystem::exists(path))
std::filesystem::create_directories(path);
config_path_ = path;
config_dir_name_ = (path /= "Targets").string().data();
if (!std::filesystem::exists(path))
std::filesystem::create_directories(path);
readConfigs();
}
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");
});
std::ranges::for_each(entries, [this](const auto& 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());
});
emit targetListChanged();
}
QVariantList UIModel::getTargetList() const
{
return targets_;
}
void UIModel::addTarget(QVariant shortcut)
{
// TODO: write config
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();
writeTarget(wtf, map["name"].toString());
targets_.replace(index, json.toVariant());
emit targetListChanged();
}
bool UIModel::getIsWindows() const
{
return is_windows_;
}
bool UIModel::hasAcrylicEffect() const
{
return has_acrylic_affect_;
}
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();
}