-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfavoritemanager.cpp
168 lines (138 loc) · 4.17 KB
/
favoritemanager.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
#include "favoritemanager.h"
#include <QFile>
#include <QByteArray>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDir>
#include "command.h"
#define FAVORITE_FILE "/favorites.json"
#define APP_FOLDER "/.service_manager_aah"
const char* FavoriteManager::OPTION_FAVORITE = "f";
const char* FavoriteManager::OPTION_FAVORITE_ADD = "fa";
const char* FavoriteManager::OPTION_FAVORITE_DEL = "fd";
const char* FavoriteManager::OPTION_FAVORITE_EDIT = "fr";
FavoriteManager::FavoriteManager()
{
}
bool FavoriteManager::loadFavorite(){
QDir d;
if( !QDir(d.homePath()+APP_FOLDER).exists() && !d.mkdir(d.homePath()+APP_FOLDER)){
qWarning("Couldn't create folder.");
return false;
}
QFile loadFile(d.homePath()+APP_FOLDER+FAVORITE_FILE);
if(!loadFile.exists()){
loadFile.open(QIODevice::ReadWrite | QIODevice::Text);
loadFile.close();
}
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open save file.");
return false;
}
QByteArray saveData = loadFile.readAll();
QJsonDocument loadDoc( QJsonDocument::fromJson(saveData));
read(loadDoc.object());
return true;
}
bool FavoriteManager::saveFavorite() const{
QDir d;
if( !QDir(d.homePath()+APP_FOLDER).exists() && !d.mkdir(d.homePath()+APP_FOLDER)){
qWarning("Couldn't create folder.");
return false;
}
QFile saveFile(d.homePath()+APP_FOLDER+FAVORITE_FILE);
if(!saveFile.exists()){
saveFile.open(QIODevice::ReadWrite | QIODevice::Text);
saveFile.close();
}
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
return false;
}
QJsonObject favManager;
this->write(favManager);
QJsonDocument saveDoc(favManager);
saveFile.write( saveDoc.toJson());
return true;
}
void FavoriteManager::read(const QJsonObject &json){
this->mFavorites.clear();
QJsonArray favoriteArray = json["favorites"].toArray();
for (int favoriteIndex = 0; favoriteIndex < favoriteArray.size(); ++favoriteIndex) {
QJsonObject favoriteObject = favoriteArray[favoriteIndex].toObject();
Favorite fav;
fav.read(favoriteObject);
this->mFavorites.append(fav);
}
}
void FavoriteManager::write(QJsonObject &json) const{
QJsonArray favoriteArray ;
foreach (const Favorite fav, this->mFavorites) {
QJsonObject favoriteObject ;
fav.write(favoriteObject);
favoriteArray.append(favoriteObject);
}
json["favorites"]=favoriteArray;
}
bool FavoriteManager::addFavorite(Favorite fav){
bool esta=false;
foreach (const Favorite favorite, this->mFavorites) {
if(fav.id==favorite.id){
esta=true;
break;
}
}
if(!esta){
this->mFavorites.append(fav);
}
return !esta;
}
bool FavoriteManager::editFavorite(QString id,QString new_id){
bool esta=false;
foreach ( Favorite favorite, this->mFavorites) {
if(id==favorite.id){
removeFavorite(favorite);
favorite.id=new_id;
addFavorite(favorite);
esta=true;
break;
}
}
return esta;
}
bool FavoriteManager::removeFavorite(Favorite fav){
return this->mFavorites.removeOne(fav);
}
bool FavoriteManager::removeFavorite(QString id){
bool esta=false;
foreach ( Favorite favorite, this->mFavorites) {
if(id==favorite.id){
removeFavorite(favorite);
esta=true;
break;
}
}
return esta;
}
Favorite FavoriteManager::getByFavorite(QString id){
foreach ( Favorite favorite, this->mFavorites) {
if(id==favorite.id){
return favorite;
break;
}
}
return Favorite(NULL,NULL);
}
QString FavoriteManager::pritFavorites(){
Command c;
QString output= c.list();
QStringList list = output.split('\n', QString::SkipEmptyParts);
QString print="[STATUS] [SERVICE] [ID]\n";
foreach (const QString &str, list) {
foreach ( Favorite favorite, this->mFavorites) {
if(str.indexOf(favorite.serviceName)>=0)
print+= str+" "+favorite.id +"\n";
}
}
return print;
}