-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSettings.cpp
More file actions
110 lines (96 loc) · 2.96 KB
/
AppSettings.cpp
File metadata and controls
110 lines (96 loc) · 2.96 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
#include "AppSettings.h"
bool AppSettings::safeDelete;
bool AppSettings::displayByTopics;
std::string* AppSettings::selectedTopics;
int AppSettings::countOfSelectedTopics;
void AppSettings::defaultSettings() {
safeDelete = true;
displayByTopics = false;
selectedTopics = nullptr;
countOfSelectedTopics = 0;
saveSettings();
}
void AppSettings::loadSettings() {
ifstream inputFile;
inputFile.open("appSettings.json");
if (inputFile.is_open()) {
json jsonHolder;
inputFile >> jsonHolder;
safeDelete = jsonHolder["safeDelete"];
displayByTopics = jsonHolder["displayByTopics"];
countOfSelectedTopics = jsonHolder["countOfSelectedTopics"];
if (countOfSelectedTopics == 0)
selectedTopics = nullptr;
else {
selectedTopics = new string[countOfSelectedTopics];
for (int i = 0; i < countOfSelectedTopics; i++)
selectedTopics[i] = unconvertUTF8(jsonHolder["selectedTopic" + to_string(i)]);
}
}
else
defaultSettings();
inputFile.close();
}
void AppSettings::saveSettings() {
json jsonHolder;
jsonHolder["safeDelete"] = safeDelete;
jsonHolder["displayByTopics"] = displayByTopics;
for (int i = 0; i < countOfSelectedTopics; i++) {
jsonHolder["selectedTopic" + to_string(i)] = convertUTF8(selectedTopics[i]);
}
jsonHolder["countOfSelectedTopics"] = countOfSelectedTopics;
ofstream outputFile;
outputFile.open("appSettings.json");
outputFile << jsonHolder.dump();
outputFile.close();
}
void AppSettings::changeStateSafeDelete() { safeDelete = !safeDelete; saveSettings(); }
bool AppSettings::isSafeDelete() { return safeDelete; }
void AppSettings::setDisplayTopicsMode(bool state) { displayByTopics = state; saveSettings(); }
bool AppSettings::isDisplayTopicsMode() { return displayByTopics; }
string* AppSettings::getSelectedTopics() { return selectedTopics; }
void AppSettings::addTopicInSelected(string topic) {
countOfSelectedTopics++;
string* temp = selectedTopics;
selectedTopics = new string[countOfSelectedTopics];
int i = 0;
for (; i < countOfSelectedTopics - 1; i++) {
selectedTopics[i] = temp[i];
}
selectedTopics[i] = topic;
saveSettings();
}
bool AppSettings::isTopicSelected(string topic) {
for (int i = 0; i < countOfSelectedTopics; i++) {
if (selectedTopics[i] == topic)
return true;
}
return false;
}
void AppSettings::unselectTopic(string topic) {
if (!isTopicSelected(topic))
return;
string* temp = selectedTopics;
countOfSelectedTopics--;
selectedTopics = new string[countOfSelectedTopics];
// This flag is used to check that the chosen topic has not been unselected yet.
bool notUnselected = true;
for (int i = 0; i <= countOfSelectedTopics; i++) {
if (notUnselected) {
if (temp[i] == topic)
notUnselected = false;
else
selectedTopics[i] = temp[i];
}
else {
selectedTopics[i - 1] = temp[i];
}
}
saveSettings();
}
void AppSettings::unselectAllTopics() {
selectedTopics = nullptr;
countOfSelectedTopics = 0;
saveSettings();
}
int AppSettings::getCountOfSelectedTopics() { return countOfSelectedTopics; }