-
Notifications
You must be signed in to change notification settings - Fork 64
/
mainwindow.cpp
372 lines (335 loc) · 12.2 KB
/
mainwindow.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <QMessageBox>
#include <QStandardPaths>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "helpers.h"
#include "logger.h"
#include "repodialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//initialize the logger
Logger::initialize(ui->txt_log);
//initialize the downloader
m_dl = new Downloader(this, ui->progressBar);
connect(m_dl, SIGNAL(downloadComplete(bool,QString)), this, SLOT(downloadComplete(bool,QString)));
//initialize the download tracker
m_dling = NONE;
//make sure the temp dir was created
if (!m_tmpDir.isValid())
{
QMessageBox b;
b.setText("ERROR: Could not create temporary files directory");
b.exec();
exit(1);
}
//get the path to the data directory, create it if not exists
m_dataDir = DATA_DIR;
if (!QDir(m_dataDir).exists())
QDir().mkdir(m_dataDir);
//read the saved repo URLs
QFile f(REPO_URL_FILE_PATH);
if (f.exists() && f.open(QIODevice::ReadOnly | QIODevice::Text))
{
ui->cmb_repo_urls->insertItem(0, "Select a Repository");
while (!f.atEnd())
{
QString curUrl = f.readLine().trimmed();
ui->cmb_repo_urls->insertItem(1, curUrl);
}
f.close();
ui->cmb_repo_urls->setCurrentIndex(0);
}
}
MainWindow::~MainWindow()
{
delete ui;
delete m_dl;
}
void MainWindow::downloadComplete(bool success, QString errMsg)
{
switch (m_dling)
{
case RELEASE:
if (!success)
{
m_dl->startDownload(helpers::urlCombine(m_url, "dists/stable/main/binary-iphoneos-arm/Release"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Release"));
m_dling = RELEASE_DEBIAN;
break;
}
else
Logger::log("Done");
//start downloading the package list
Logger::log("Getting package list...");
m_dling = PACKAGES;
m_dl->startDownload(helpers::urlCombine(m_url, "Packages.bz2"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.bz2"));
break;
case RELEASE_DEBIAN:
if (!success)
Logger::log("No Release file could be found, repo information will be blank");
else
Logger::log("Done");
//start downloading the package list
Logger::log("Getting package list...");
m_dling = PACKAGES;
m_dl->startDownload(helpers::urlCombine(m_url, "Packages.bz2"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.bz2"));
break;
case PACKAGES:
if (!success)
{
m_dling = PACKAGES_DEBIAN;
m_dl->startDownload(helpers::urlCombine(m_url, "dists/stable/main/binary-iphoneos-arm/Packages.bz2"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.bz2"));
break;
}
//extract the Packages list
if (!helpers::decompress(QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.bz2"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages")))
{
Logger::log("Error extracting Packages.bz2");
break;
}
Logger::log("Done");
repoInfoDownloaded();
m_dling = PACKAGE;
break;
case PACKAGES_DEBIAN:
if (!success)
{
m_dling = PACKAGESGZ;
m_dl->startDownload(helpers::urlCombine(m_url, "Packages.gz"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.gz"));
break;
}
//extract the Packages list
if (!helpers::decompress(QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.bz2"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages")))
{
Logger::log("Error extracting Packages.bz2");
break;
}
Logger::log("Done");
repoInfoDownloaded();
m_dling = PACKAGE;
break;
case PACKAGESGZ:
if (!success)
{
m_dling = PACKAGESGZ_DEBIAN;
m_dl->startDownload(helpers::urlCombine(m_url, "dists/stable/main/binary-iphoneos-arm/Packages.gz"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.gz"));
break;
}
//extract the Packages list
if (!helpers::decompress(QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.gz"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages")))
{
Logger::log("Error extracting Packages.bz2");
break;
}
Logger::log("Done");
repoInfoDownloaded();
m_dling = PACKAGE;
break;
case PACKAGESGZ_DEBIAN:
if (!success)
{
Logger::log("Not a valid Cydia repository!");
m_dling = NONE;
break;
}
//extract the Packages list
if (!helpers::decompress(QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages.gz"), QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages")))
{
Logger::log("Error extracting Packages.gz");
break;
}
Logger::log("Done");
repoInfoDownloaded();
m_dling = PACKAGE;
break;
case PACKAGE:
break;
case NONE:
break;
}
}
void MainWindow::repoInfoDownloaded()
{
//we have everything we need to display the repo information and packages list
QString releaseFile = QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Release");
QString packagesFile = QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Packages");
//wipe any old release data
m_repo.origin = "";
m_repo.label = "";
m_repo.suite = "";
m_repo.version = "";
m_repo.codename = "";
m_repo.architectures = "";
m_repo.components = "";
m_repo.description = "";
//parse the Release file, if it exists
if (QFile::exists(releaseFile))
{
//open it
QFile *rFile = new QFile(releaseFile);
if (!rFile->open(QIODevice::ReadOnly | QIODevice::Text))
{
Logger::log("Failed opening Release file");
return;
}
//read each line of the file
while (!rFile->atEnd())
{
QString line = rFile->readLine();
QStringList list = line.split(":");
if (list.at(0).trimmed().toLower() == "origin")
m_repo.origin = list.at(1).trimmed();
else if (list.at(0).trimmed().toLower() == "label")
m_repo.label = list.at(1).trimmed();
else if (list.at(0).trimmed().toLower() == "suite")
m_repo.suite = list.at(1).trimmed();
else if (list.at(0).trimmed().toLower() == "version")
m_repo.version = list.at(1).trimmed();
else if (list.at(0).trimmed().toLower() == "codename")
m_repo.codename = list.at(1).trimmed();
else if (list.at(0).trimmed().toLower() == "architectures")
m_repo.architectures = list.at(1).trimmed();
else if (list.at(0).trimmed().toLower() == "components")
m_repo.components = list.at(1).trimmed();
else if (list.at(0).trimmed().toLower() == "description")
m_repo.description = list.at(1).trimmed();
}
//close it
rFile->close();
delete rFile;
}
else
{
m_repo.origin = m_url;
m_repo.description = "";
}
m_repo.url = m_url;
//open the Packages file
QFile *pFile = new QFile(packagesFile);
if (!pFile->open(QIODevice::ReadOnly | QIODevice::Text))
{
Logger::log("Failed opening Packages file");
return;
}
Package pkg = Package::null();
//parse the Packages file
while (!pFile->atEnd())
{
QString line = pFile->readLine();
//check for new package
if (line.startsWith("\n"))
{
m_packages.push_back(pkg);
pkg = Package::null();
continue;
}
QRegExp rx("^(.*): (.*)");
rx.indexIn(line);
if (rx.cap(1).trimmed().toLower() == "package")
pkg.packageid = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "version")
pkg.version = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "section")
pkg.section = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "maintainer")
pkg.maintainer = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "depends")
pkg.depends = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "architecture")
pkg.architecture = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "filename")
pkg.filename = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "size")
pkg.size = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "installed-size")
pkg.installedsize = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "md5sum")
pkg.md5sum = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "description")
pkg.description = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "name")
pkg.name = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "author")
pkg.author = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "website")
pkg.website = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "depiction")
pkg.depiction = rx.cap(2).trimmed();
else if (rx.cap(1).trimmed().toLower() == "tag")
pkg.tags = rx.cap(2).trimmed();
}
//and close it
pFile->close();
delete pFile;
//now split the packages into sections
m_sections.empty();
for (unsigned long i=0;i<m_packages.size();i++)
{
QString secName = m_packages[i].section;
if (m_sections[secName].find(m_packages[i].name) != m_sections[secName].end())
{ //package was already added
m_sections[secName][m_packages[i].name][m_packages[i].version] = m_packages[i];
}
else
{ //package was not already added, need to create a PackageVersions
PackageVersions pkgVers;
pkgVers[m_packages[i].version] = m_packages[i];
m_sections[secName][m_packages[i].name] = pkgVers;
}
}
//and empty the old package list
m_packages.empty();
//now we can open a new dialog with the repo contents
RepoDialog *rd = new RepoDialog();
rd->initialize(m_repo, m_sections);
rd->exec();
delete rd;
}
void MainWindow::on_btn_open_repo_clicked()
{
QString networkUrl, fileUrl;
//get the URL of the repo to connect to
m_url = ui->cmb_repo_urls->currentText();
/**Download Release**/
networkUrl = helpers::urlCombine(m_url, "Release");
fileUrl = QDir::cleanPath(m_tmpDir.path() + QDir::separator() + "Release");
//start the download
Logger::log("Getting repo information...");
m_dling = RELEASE;
m_dl->startDownload(networkUrl, fileUrl);
}
void MainWindow::on_btn_new_repo_clicked()
{
m_dr = new dialogrepo();
m_dr->exec();
//add to the combo box
ui->cmb_repo_urls->insertItem(ui->cmb_repo_urls->count(), m_dr->getText());
ui->cmb_repo_urls->setCurrentIndex(ui->cmb_repo_urls->count()-1);
ui->btn_open_repo->setEnabled(true);
ui->btn_del_repo->setEnabled(true);
//write it to the file
helpers::addRepoToFile(m_dr->getText(), REPO_URL_FILE_PATH);
delete m_dr;
}
void MainWindow::on_cmb_repo_urls_currentIndexChanged(int index)
{
if (index == 0 || index == -1)
{
ui->btn_open_repo->setEnabled(false);
ui->btn_del_repo->setEnabled(false);
return;
}
ui->btn_open_repo->setEnabled(true);
ui->btn_del_repo->setEnabled(true);
}
void MainWindow::on_btn_del_repo_clicked()
{
QString url = ui->cmb_repo_urls->currentText();
//remove the entry from the combo box
ui->cmb_repo_urls->removeItem(ui->cmb_repo_urls->currentIndex());
ui->cmb_repo_urls->setCurrentIndex(0);
//remove it from the file too
helpers::delRepoFromFile(url, REPO_URL_FILE_PATH);
}