-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressmanager.cpp
More file actions
121 lines (94 loc) · 2.63 KB
/
Copy pathprogressmanager.cpp
File metadata and controls
121 lines (94 loc) · 2.63 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
// process manager , for user Dynamic data
#include "progressmanager.h"
#include <qdir.h>
#include <qstandardpaths.h>
#include <QJsonDocument>
ProgressManager::ProgressManager(QObject *parent)
: QObject(parent)
{
// Get app data location
QString appDataPath =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(appDataPath);
if (!dir.exists()) {
dir.mkpath(".");
}
filePath = dir.filePath("user_progress.json");
}
void ProgressManager::load()
{
QFile file(filePath);
if (!file.exists()) {
qDebug() << "Progress file does not exist. Creating new one.";
progressData = QJsonObject();
save();
return;
}
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open progress file.";
return;
}
QByteArray data = file.readAll();
file.close();
QJsonDocument doc = QJsonDocument::fromJson(data);
if (!doc.isObject()) {
qWarning() << "Invalid progress file format.";
progressData = QJsonObject();
return;
}
progressData = doc.object();
}
void ProgressManager::save()
{
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "Failed to save progress file.";
return;
}
QJsonDocument doc(progressData);
file.write(doc.toJson(QJsonDocument::Indented));
file.close();
}
void ProgressManager::ensureProblemExists(const QString &id)
{
if (!progressData.contains(id)) {
QJsonObject obj;
obj["solved"] = false;
obj["starred"] = false;
obj["attempts"] = 0;
progressData[id] = obj;
}
}
bool ProgressManager::isSolved(const QString &id) const
{
if (!progressData.contains(id))
return false;
QJsonObject obj = progressData.value(id).toObject();
return obj.value("solved").toBool(false);
}
void ProgressManager::markSolved(const QString &id, bool solved)
{
ensureProblemExists(id);
QJsonObject obj = progressData.value(id).toObject();
obj["solved"] = solved;
progressData[id] = obj;
save();
emit progressChanged(id);
}
bool ProgressManager::isStarred(const QString &id) const
{
if (!progressData.contains(id))
return false;
QJsonObject obj = progressData.value(id).toObject();
return obj.value("starred").toBool(false);
}
void ProgressManager::toggleStar(const QString &id)
{
ensureProblemExists(id);
QJsonObject obj = progressData.value(id).toObject();
bool current = obj.value("starred").toBool(false);
obj["starred"] = !current;
progressData[id] = obj;
save();
emit progressChanged(id);
}