/
manager.cpp
216 lines (189 loc) · 7.74 KB
/
manager.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
205
206
207
208
209
210
211
212
213
214
215
216
#include "manager.h"
#include <QDebug>
#include <QTranslator>
Manager::Manager(QTextEdit *logs)
{
this->logs = logs;
}
Manager::~Manager()
{
}
QStringList Manager::getLocalMods()
{
try
{
QStringList localMods;
QDirIterator localIterator(QDir().currentPath(), QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
while (localIterator.hasNext()) { localMods.append(localIterator.next()); }
return localMods;
} catch(QException* error)
{
logs->append(QApplication::tr("<span style=\"color: #ea7878\"><b>•</b> Error in function getLocalMods: %1</span>").arg(error->what()));
return QStringList();
}
}
QStringList Manager::getAllFiles(QString dir, bool fulllocation)
{
try
{
QStringList files;
QDirIterator localIterator(dir, QDir::Files);
while (localIterator.hasNext()) {
QString filestr(localIterator.next());
if(!fulllocation)
{
if(filestr.contains("/"))
{
int lenght = filestr.split("/").count() - 1;
QString filename = QString(filestr.split("/")[lenght]);
files.append(filename);
}
} else files.append(filestr);
}
return files;
} catch(QException* error)
{
logs->append(QApplication::tr("<span style=\"color: #ea7878\"><b>•</b> Error in function getAllfiles: %1</span>").arg(error->what()));
return QStringList();
}
}
QStringList Manager::getAllFolders(QString dir)
{
try
{
QStringList folders;
QDirIterator localIterator(dir, QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (localIterator.hasNext()) { folders.append(localIterator.next()); qDebug() << localIterator.next(); }
return folders;
} catch(QException* error)
{
logs->append(QApplication::tr("<span style=\"color: #ea7878\"><b>•</b> Error in function getAllFolders: %1</span>").arg(error->what()));
return QStringList();
}
}
bool Manager::modInstalled(QString localDir, QString GTADir)
{
try
{
QDirIterator it(localDir, QStringList() << "*", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString fileName = it.next();
QString smallFileName = fileName.split(localDir)[1].mid(1);
QString pathgtafile = QString("%1/%2").arg(GTADir).arg(smallFileName);
QFile gtaFile(pathgtafile);
if(!gtaFile.exists()) return false;
//qDebug() << smallFileName;
}
return true;
} catch(QException* error)
{
logs->append(QApplication::tr("<span style=\"color: #ea7878\"><b>•</b> Error in function modInstalled: %1</span>").arg(error->what()));
return false;
}
}
bool Manager::move(QString folder, QString dir, bool remove)
{
try
{
QStringList files = this->getAllFiles(folder);
QStringList folders = this->getAllFolders(folder);
foreach(QString file, files)
{
QFile rfile(QString("%1/%2").arg(folder).arg(file));
if(!rfile.exists()) return false;
QString nfile = QString("%1/%2").arg(dir).arg(file);
if(QFile(nfile).exists()) { logs->append(QApplication::tr("<span style=\"color: #ea7878\"><b>•</b> File %1 already exists, replacing...</span>").arg(nfile)); QFile(nfile).remove(); }
rfile.copy(nfile);
logs->append(QApplication::tr("<span style=\"color: #ced0f3\"><b>•</b> File %1 moved to %2.</span>").arg(file).arg(dir));
if(remove)
{
rfile.remove();
logs->append(QApplication::tr("<span style=\"color: #ced0f3\"><b>•</b> File %1 has successfully removed.</span>").arg(file));
}
}
foreach(QString path, folders)
{
QDir directory(path);
//qDebug() << path;
//qDebug() << directory.dirName();
}
} catch(QException* error)
{
logs->append(QApplication::tr("<span style=\"color: #ea7878\"><b>•</b> Error in function move: %1</span>").arg(error->what()));
return false;
}
return true;
}
bool Manager::remove(QString folder, QString gta)
{
try
{
QDirIterator it(folder, QStringList() << "*", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString fileName = it.next();
QString smallFileName = fileName.split(folder)[1].mid(1);
QString pathgtafile = QString("%1/%2").arg(gta).arg(smallFileName);
QFile gtaFile(pathgtafile);
if(gtaFile.exists())
{
gtaFile.remove();
logs->append(QApplication::tr("<span style=\"color: #ced0f3\"><b>•</b> File %1 has successfully removed.</span>").arg(gtaFile.fileName()));
int lenght = gtaFile.fileName().split("/").count()-1;
QString gtaFileNameCompact = gtaFile.fileName().split("/")[lenght];
QString gtaFileLocation = gtaFile.fileName().split(gtaFileNameCompact)[0];
gtaFileLocation = gtaFileLocation.mid(0, gtaFileLocation.length()-1);
if(gtaFileLocation != gta)
{
if(QDir(gtaFileLocation).entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).count() == 0)
{
QDir().rmpath(gtaFileLocation);
logs->append(QApplication::tr("<span style=\"color: #ced0f3\"><b>•</b> <u>Empty folder</u> %1 has successfully removed.</span>").arg(gtaFileLocation));
}
}
}
}
return true;
} catch(QException* error)
{
logs->append(QApplication::tr("<span style=\"color: #ea7878\"><b>•</b> Error in function remove: %1</span>").arg(error->what()));
return false;
}
return true;
}
bool Manager::copyRecursively(const QString& sourceFolder,const QString& destFolder)
{
QQueue< QPair<QString, QString> > queue;
queue.enqueue(qMakePair(sourceFolder, destFolder));
while (!queue.isEmpty())
{
QPair<QString, QString> pair = queue.dequeue();
QDir sourceDir(pair.first);
QDir destDir(pair.second);
if(!sourceDir.exists())
continue;
if(!destDir.exists())
destDir.mkpath(pair.second);
QStringList files = sourceDir.entryList(QDir::Files);
for(int i = 0; i < files.count(); i++)
{
QString srcName = pair.first + "/" + files.at(i);
QString destName = pair.second + "/" + files.at(i);
if(QFile(destName).exists())
{
QFile(destName).remove();
logs->append(QApplication::tr("<span style=\"color: #f0f0f0\"><b>•</b> File <u>%1</u> already exists, replacing...</span>").arg(destName));
}
QFile::copy(srcName, destName);
logs->append(QApplication::tr("<span style=\"color: #f0f0f0\"><b>•</b> Move file: %1</span>").arg(destName));
}
QStringList dirs = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
for(int i = 0; i < dirs.count(); i++)
{
QString srcName = pair.first + "/" + dirs.at(i);
QString destName = pair.second + "/" + dirs.at(i);
queue.enqueue(qMakePair(srcName, destName));
logs->append(QApplication::tr("<span style=\"color: #f0f0f0\"><b>•</b> Move folder: %1</span>").arg(destName));
}
}
return true;
}