-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesktopConfigListModel.cpp
163 lines (149 loc) · 4.34 KB
/
DesktopConfigListModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Unit for desktop config
// { name: string,
// hasAlias: bool,
// alias: string,
// envs: [string]
// }
#include "DesktopConfigListModel.h"
#include <QFile>
#include <QGuiApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonValueRef>
#include <QStandardPaths>
#include <algorithm>
#include <format>
static QString
get_config_path()
{
return QString::fromStdString(
std::format("{}/{}/desktopconfig.json",
QStandardPaths::writableLocation(QStandardPaths::ConfigLocation).toStdString(),
qApp->organizationName().toStdString()));
}
DesktopConfigModel::DesktopConfigModel(QObject *parent)
: QAbstractListModel(parent)
{
get_file_config();
}
void
DesktopConfigModel::get_file_config()
{
QFile file(get_config_path());
if (!file.exists() && !file.isReadable()) {
return;
}
file.open(QIODevice::ReadOnly | QIODevice::Text);
auto bytes = file.readAll();
QJsonDocument document = QJsonDocument::fromJson(bytes);
auto arrays = document.array();
for (auto array : arrays) {
auto object = array.toObject();
m_configs.push_back(DesktopConfig{.name = object["name"].toString(),
.hasAlias = object["hasAlias"].toBool(),
.execAlias = object["execAlias"].toString(),
.envs = object["envs"].toVariant().toStringList()});
}
}
void
DesktopConfigModel::save_file_config()
{
QJsonDocument document;
QJsonArray array;
for (auto config : m_configs) {
array.push_back(QJsonObject({{"name", config.name},
{"hasAlias", config.hasAlias},
{"execAlias", config.execAlias},
{"envs", QJsonArray::fromStringList(config.envs)}}));
}
document.setArray(array);
QByteArray bytes = document.toJson(QJsonDocument::Indented);
QFile file(get_config_path());
if (file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
QTextStream iStream(&file);
iStream.setEncoding(QStringConverter::Encoding::Utf8);
iStream << bytes;
file.close();
} else {
qWarning() << "Cannot save config";
}
}
int
DesktopConfigModel::rowCount(const QModelIndex &index) const
{
return m_configs.length();
}
QHash<int, QByteArray>
DesktopConfigModel::roleNames() const
{
static const QHash<int, QByteArray> roles{
{Name, "name"}, {HasAlias, "hasAlias"}, {ExecAlias, "execAlias"}, {Envs, "envs"}};
return roles;
}
bool
DesktopConfigModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
switch (role) {
case Name:
m_configs[index.row()].name = value.toString();
break;
case HasAlias:
m_configs[index.row()].hasAlias = value.toBool();
break;
case ExecAlias:
m_configs[index.row()].execAlias = value.toString();
break;
case Envs:
m_configs[index.row()].envs = value.toStringList();
break;
default:
return false;
}
save_file_config();
return true;
}
QVariant
DesktopConfigModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case Name:
return m_configs[index.row()].name;
case HasAlias:
return m_configs[index.row()].hasAlias;
case ExecAlias:
return m_configs[index.row()].execAlias;
case Envs:
return m_configs[index.row()].envs;
default:
return QVariant();
}
}
bool
DesktopConfigModel::dataIsExist(const QString &desktop) const
{
return std::any_of(m_configs.begin(), m_configs.end(), [desktop](const DesktopConfig &config) {
return config.name == desktop;
});
}
void
DesktopConfigModel::insertData(const QString &desktop)
{
if (dataIsExist(desktop)) {
return;
}
beginInsertRows(QModelIndex(), m_configs.length(), m_configs.length());
m_configs.push_back(
DesktopConfig{.name = desktop, .hasAlias = false, .execAlias = "", .envs = {}});
endInsertRows();
}
void
DesktopConfigModel::remove(int row)
{
if (row < 0 || row >= m_configs.count())
return;
beginRemoveRows(QModelIndex(), row, row);
m_configs.removeAt(row);
endRemoveRows();
}