-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDbContext.cpp
More file actions
210 lines (182 loc) · 5.88 KB
/
Copy pathAppDbContext.cpp
File metadata and controls
210 lines (182 loc) · 5.88 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "AppDbContext.h"
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QUuid>
#include <QStandardPaths>
#include <QDir>
#include "SoundInfo.h"
AppDbContext::AppDbContext(QObject *parent) : QObject(parent)
{
initData();
initConfig();
}
AppDbContext::~AppDbContext()
{
delete dataFile;
dataFile = nullptr;
}
void AppDbContext::initConfig()
{
QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QString fileName = "/config.json";
QDir dir;
configFile = new QFile(filePath+fileName);
if(!dir.exists(filePath) && !dir.mkpath(filePath))
{
qDebug() << "Error occurred when creating path";
return;
}
if (!configFile->exists()) {
QJsonArray initArray;
QJsonObject config;
config["outputVolume"] = 0.5;
config["inputVolume"] = 0.5;
initArray << config;
QJsonDocument doc(initArray);
if (configFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
configFile->write(doc.toJson());
configFile->close();
qDebug() << "Created new config.json " << configFile->fileName();
} else {
qDebug() << "Failed to create config.json!";
}
}
}
void AppDbContext::initData()
{
QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QString fileName = "/data.json";
QDir dir;
dataFile = new QFile(filePath+fileName);
if(!dir.exists(filePath) && !dir.mkpath(filePath))
{
qDebug() << "Error occurred when creating path";
return;
}
if (!dataFile->exists()) {
QJsonArray emptyArray;
QJsonDocument doc(emptyArray);
if (dataFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
dataFile->write(doc.toJson());
dataFile->close();
qDebug() << "Created new data.json " << dataFile->fileName();
} else {
qDebug() << "Failed to create data.json!";
}
}
}
void AppDbContext::addSound(QString name, QUrl imagePath, QUrl soundPath, QString color)
{
if (!dataFile->open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open file for reading!";
return;
}
QJsonDocument doc = QJsonDocument::fromJson(dataFile->readAll());
QJsonArray soundsArray = doc.array();
dataFile->close();
QJsonObject newSound;
newSound["id"] = QUuid::createUuid().toString(QUuid::WithoutBraces);
newSound["name"] = name;
newSound["imagePath"] = imagePath.toString();
newSound["soundPath"] = soundPath.toString();
newSound["color"] = color;
soundsArray.append(newSound);
if (!dataFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "Failed to write to file!";
return;
}
dataFile->write(QJsonDocument(soundsArray).toJson());
dataFile->close();
emit soundsChanged();
qDebug() << "Sound added successfully.";
}
QList<SoundInfo*> AppDbContext::getAllSounds()
{
if (!dataFile->open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open file for reading!";
return {};
}
QJsonDocument doc = QJsonDocument::fromJson(dataFile->readAll());
QJsonArray soundsArray = doc.array();
dataFile->close();
QList<SoundInfo*> sounds;
for (const QJsonValue &value : soundsArray) {
QJsonObject obj = value.toObject();
SoundInfo* sound = new SoundInfo();
sound->setId(obj["id"].toString());
sound->setName(obj["name"].toString());
sound->setImagePath(QUrl(obj["imagePath"].toString()));
sound->setSoundPath(QUrl(obj["soundPath"].toString()));
sound->setColor(obj["color"].toString());
sounds.append(sound);
}
return sounds;
}
void AppDbContext::deleteSound(QString id)
{
if (!dataFile->open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open file for reading!";
return;
}
QJsonDocument doc = QJsonDocument::fromJson(dataFile->readAll());
QJsonArray soundsArray = doc.array();
dataFile->close();
bool found = false;
for (int i = 0; i < soundsArray.size(); ++i) {
if (soundsArray[i].toObject()["id"].toString() == id) {
soundsArray.removeAt(i);
found = true;
break;
}
}
if (!found) {
qDebug() << "Sound ID not found!";
return;
}
if (!dataFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "Failed to write to file!";
return;
}
dataFile->write(QJsonDocument(soundsArray).toJson());
dataFile->close();
emit soundsChanged();
qDebug() << "Sound deleted successfully.";
}
Config *AppDbContext::getConfig()
{
if (!configFile->open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open file for reading!";
return {};
}
QJsonDocument doc = QJsonDocument::fromJson(configFile->readAll());
QJsonArray configArray = doc.array();
configFile->close();
Config* config = new Config;
for (const QJsonValue &value : configArray) {
QJsonObject obj = value.toObject();
config->setOutputVolume(obj["outputVolume"].toDouble());
config->setInputVolume(obj["inputVolume"].toDouble());
}
return config;
}
void AppDbContext::updateConfig(float outputValue, float inputValue)
{
if (configFile->exists()) {
QJsonArray configArray;
QJsonObject config;
config["outputVolume"] = outputValue;
config["inputVolume"] = inputValue;
configArray << config;
QJsonDocument doc(configArray);
if (configFile->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
configFile->write(doc.toJson());
configFile->close();
qDebug() << "Updated new config.json " << configFile->fileName();
} else {
qDebug() << "Failed to update config.json!";
}
}
}