This repository has been archived by the owner on May 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingsutils.cpp
204 lines (172 loc) · 6.08 KB
/
settingsutils.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
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
#include "settingsutils.h"
void SettingsUtils::checkSettings(){
QSettings settings;
if(!settings.contains("author")){
qDebug()<<"settings not exists";
initSettings();
}
#ifndef QT_NO_DEBUG
else{
qDebug()<<"settings exists";
}
#endif
}
void SettingsUtils::initSettings(){
qDebug()<<"creating new settings";
QString dataDirPath = appDataPath();
QDir dir;
if(!dir.exists(appDataPath())){
dir.mkpath(appDataPath());
}
QSettings settings;
settings.setValue("author","Croxx");
settings.setValue("spirit_count",0);
settings.setValue("spirits",QJsonArray());
}
QString SettingsUtils::appDataPath(){
return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
}
int SettingsUtils::addSpirit(){
QSettings settings;
QJsonArray spirits = settings.value("spirits").toJsonArray();
int count = settings.value("spirit_count").toInt();
QJsonObject spirit;
spirit.insert("id",count);
spirit.insert("x",0);
spirit.insert("y",0);
spirit.insert("shortcut_count",0);
spirit.insert("shortcuts",QJsonArray());
spirits.append(spirit);
settings.setValue("spirits",spirits);
settings.setValue("spirit_count",count+1);
return count;
}
bool SettingsUtils::removeSpirit(int id){
//TODO : REMOVE SHORTCUTS FILES
bool found = false;
QSettings settings;
QJsonArray spirits = settings.value("spirits").toJsonArray();
for(auto it = spirits.begin();it!=spirits.end();it++){
auto spirit = (*it).toObject();
if(spirit.value("id").toInt()==id){
found = true;
spirits.erase(it);
break;
}
}
settings.setValue("spirits",spirits);
return found;
}
QList<int> SettingsUtils::getSpiritIds(){
QSettings settings;
QJsonArray spirits = settings.value("spirits").toJsonArray();
QList<int> ids;
for(auto it = spirits.begin();it!=spirits.end();it++){
auto spirit = (*it).toObject();
ids.append(spirit.value("id").toInt());
}
return ids;
}
bool SettingsUtils::setPosition(int id,QPoint pos){
bool found = false;
QSettings settings;
QJsonArray spirits = settings.value("spirits").toJsonArray();
for(auto it = spirits.begin();it!=spirits.end();it++){
auto spirit = (*it).toObject();
if(spirit.value("id").toInt()==id){
found = true;
spirit.insert("x",pos.x());
spirit.insert("y",pos.y());
*it = spirit;
break;
}
}
settings.setValue("spirits",spirits);
return found;
}
QPoint SettingsUtils::getPosition(int id){
QPoint pos(400,400);
QSettings settings;
QJsonArray spirits = settings.value("spirits").toJsonArray();
for(auto it = spirits.begin();it!=spirits.end();it++){
auto spirit = (*it).toObject();
if(spirit.value("id").toInt()==id){
pos.setX(spirit.value("x").toInt());
pos.setY(spirit.value("y").toInt());
break;
}
}
return pos;
}
int SettingsUtils::addShortcut(int id,QString lnkPath,HWND hwnd,HINSTANCE hInstance){
int found = -1;
QSettings settings;
QJsonArray spirits = settings.value("spirits").toJsonArray();
for(auto it = spirits.begin();it!=spirits.end();it++){
auto spirit = (*it).toObject();
if(spirit.value("id").toInt()==id){
int count = spirit.value("shortcut_count").toInt();
found = count;
lnkPath = WinUtils::convertQTPath2WinPath(lnkPath);
QString lnkNewPath = WinUtils::convertQTPath2WinPath(appDataPath()+"/"+QString::number(id,10)+"_"+QString::number(count,10)+".lnk");
QString iconNewPath = WinUtils::convertQTPath2WinPath(appDataPath()+"/"+QString::number(id,10)+"_"+QString::number(count,10)+".png");
QFile::copy(lnkPath,lnkNewPath);
QFileInfo info(lnkPath);
QString targetPath = WinUtils::convertQTPath2WinPath(info.symLinkTarget());
QPixmap icon = WinUtils::getIconFromPEFile(hwnd,hInstance,targetPath.toStdWString().c_str());
icon.save(iconNewPath);
QJsonObject shortcut;
shortcut.insert("lnkPath",lnkNewPath);
shortcut.insert("iconPath",iconNewPath);
shortcut.insert("id",count);
QJsonArray shortcuts = spirit.value("shortcuts").toArray();
shortcuts.append(shortcut);
spirit.insert("shortcuts",shortcuts);
spirit.insert("shortcut_count",count+1);
*it = spirit;
break;
}
}
settings.setValue("spirits",spirits);
return found;
}
bool SettingsUtils::removeShortcut(int id,int sid){
//TODO : NEED TEST
bool found = false;
QSettings settings;
QJsonArray spirits = settings.value("spirits").toJsonArray();
for(auto it = spirits.begin();it!=spirits.end();it++){
auto spirit = (*it).toObject();
if(spirit.value("id").toInt()==id){
QJsonArray shortcuts = spirit.value("shortcuts").toArray();
for(auto sit = shortcuts.begin();sit!=shortcuts.end();sit++){
auto shortcut = (*sit).toObject();
if(shortcut.value("id").toInt()==sid){
found = true;
QFile iconFile(shortcut.value("iconPath").toString());
QFile lnkFile(shortcut.value("lnkPath").toString());
if(iconFile.exists())iconFile.remove();
if(lnkFile.exists())lnkFile.remove();
shortcuts.erase(sit);
}
}
*it = spirit;
break;
}
}
settings.setValue("spirits",spirits);
return found;
}
QJsonArray SettingsUtils::getShortcuts(int id){
QJsonArray shortcuts;
QSettings settings;
QJsonArray spirits = settings.value("spirits").toJsonArray();
for(auto it = spirits.begin();it!=spirits.end();it++){
auto spirit = (*it).toObject();
if(spirit.value("id").toInt()==id){
shortcuts = spirit.value("shortcuts").toArray();
break;
}
}
return shortcuts;
}