Repository navigation
Expand file tree
/
Copy pathusermanager.cpp
More file actions
146 lines (127 loc) · 3.48 KB
/
Copy pathusermanager.cpp
File metadata and controls
146 lines (127 loc) · 3.48 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
#include "usermanager.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDebug>
#include <QMessageBox>
UserManager& UserManager::getInstance() {
static UserManager instance;
return instance;
}
UserManager::UserManager() {
loadProfiles();
}
void UserManager::addProfile(const UserProfile& profile) {
if (profiles.size() < MAX_PROFILES) {
// Ensure only one current user at a time
for (auto& existingProfile : profiles) {
existingProfile.cuser = 0;
}
UserProfile newProfile = profile;
newProfile.cuser = 1;
profiles.append(newProfile);
saveProfiles();
}
}
bool UserManager::removeProfile(int index) {
if (index >= 0 && index < profiles.size()) {
if (profiles[index].cuser == 1 && profiles.size() == 1) {
// Cannot remove last profile
return false;
}
if (profiles[index].cuser != 1) {
profiles.remove(index);
saveProfiles();
return true;
}
}
return false;
}
bool UserManager::editProfile(int index, const UserProfile& updatedProfile) {
if (index >= 0 && index < profiles.size()) {
int currentUserStatus = profiles[index].cuser;
profiles[index] = updatedProfile;
profiles[index].cuser = currentUserStatus;
saveProfiles();
return true;
}
return false;
}
UserProfile* UserManager::getCurrentUser() {
for (auto& profile : profiles) {
if (profile.cuser == 1) {
return &profile;
}
}
return nullptr;
}
void UserManager::setCurrentUser(int index) {
if (index >= 0 && index < profiles.size()) {
// Clear current user
for (auto& profile : profiles) {
profile.cuser = 0;
}
// Set new current user
profiles[index].cuser = 1;
saveProfiles();
}
}
void UserManager::clearCurrentUser() {
for (auto& profile : profiles) {
profile.cuser = 0;
}
saveProfiles();
}
QVector<UserProfile>& UserManager::getAllProfiles() {
return profiles;
}
int UserManager::getProfileCount() const {
return profiles.size();
}
UserProfile* UserManager::getProfileByIndex(int index) {
if (index >= 0 && index < profiles.size()) {
return &profiles[index];
}
return nullptr;
}
void UserManager::loadProfiles() {
QFile file("profiles.json");
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Could not open profiles file";
return;
}
QByteArray saveData = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(saveData));
QJsonArray array = doc.array();
profiles.clear();
for (const QJsonValue &value : array) {
QJsonObject obj = value.toObject();
UserProfile profile;
profile.fromJson(obj);
profiles.append(profile);
}
}
void UserManager::saveProfiles() {
QJsonArray array;
for (const UserProfile &profile : profiles) {
array.append(profile.toJson());
}
QJsonDocument doc(array);
QFile file("profiles.json");
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Could not open profiles file for writing";
return;
}
file.write(doc.toJson());
}
bool UserManager::hasCurrentUser() const {
for (const auto& profile : profiles) {
if (profile.cuser == 1) {
return true;
}
}
return false;
}
bool UserManager::isMaxProfileReached() const {
return profiles.size() >= MAX_PROFILES;
}