-
Notifications
You must be signed in to change notification settings - Fork 5
/
mainwindow.cpp
497 lines (406 loc) · 15.9 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardPaths>
#include <QFile>
#include <QDir>
#include <QProcess>
#include <QMessageBox>
#include <QClipboard>
#include <QTextStream>
const QString PHP_TMP_DIR = "tmp";
const QString BIN_DIR = "bin";
const QString GIT_CORE_DIR = "git-core";
const QString GIT_TEMPLATES_DIR = "templates";
const QString TERMINFO_DIR = "terminfo";
const QString MC_DIR = "mc";
const QString MC_THEMES_SUBDIR = "skins";
const QString MC_SYNTAX_SUBDIR = "syntax";
const QString APP_DATA_HOME = "/data/data/com.github.ziracms.devpack/files";
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList stddirs = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
if (stddirs.size()>0) dataDir = stddirs.at(0);
else dataDir = "";
binFiles << "php" << "sqlite3" << "git" << "git-receive-pack" << "git-upload-archive" << "git-upload-pack" << "sassc" << "nano" << "mc" << "vim" << "termux-elf-cleaner";
gitFiles << "git" << "git-remote" << "git-remote-http" << "git-remote-https" << "git-receive-pack" << "git-upload-archive" << "git-upload-pack";
termFiles << "linux" << "screen" << "screen-256color" << "vt100" << "xterm" << "cygwin";
mcFiles << "filehighlight.ini" << "mc.charsets";
mcThemeFiles << "default.ini" << "standard.ini";
mcSyntaxFiles << "Syntax" << "unknown.syntax";
otherFiles << "php.ini" << "gitconfig" << "cacert.pem" << "nanorc";
if (!isPackInstalled()) {
ui->checkButton->hide();
ui->uninstallButton->hide();
ui->infoButton->hide();
ui->envLabel->hide();
ui->envTextEdit->hide();
ui->copyButton->hide();
} else {
ui->installButton->hide();
}
ui->versionsLabel->hide();
ui->envTextEdit->setText(
"export ZIRA_HOME=\""+APP_DATA_HOME+"\"\n" +
"export PATH=\"$ZIRA_HOME/"+BIN_DIR+":$PATH\"\n" +
"export TERMINFO=\"$ZIRA_HOME/"+TERMINFO_DIR+"\"\n" +
"export TERM=\"linux\"\n" +
"export LC_CTYPE=\"en_US.UTF-8\"\n" +
"cd ~"
);
connect(ui->installButton, SIGNAL(clicked(bool)), this, SLOT(installButtonClicked(bool)));
connect(ui->uninstallButton, SIGNAL(clicked(bool)), this, SLOT(uninstallButtonClicked(bool)));
connect(ui->checkButton, SIGNAL(clicked(bool)), this, SLOT(checkButtonClicked(bool)));
connect(ui->infoButton, SIGNAL(clicked(bool)), this, SLOT(infoButtonClicked(bool)));
connect(ui->copyButton, SIGNAL(clicked(bool)), this, SLOT(copyPathClicked(bool)));
applyStyle();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::installButtonClicked(bool)
{
QString installButtonText = ui->installButton->text();
ui->installButton->setText(tr("Please wait")+"...");
ui->installButton->repaint();
QApplication::processEvents();
if (!installPack(true)) {
QMessageBox msgBox(this);
msgBox.setWindowTitle(QObject::tr("Error"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setText("Installation failed :(");
msgBox.exec();
} else {
QSize size = geometry().size();
ui->installButton->hide();
ui->checkButton->show();
ui->infoButton->show();
ui->uninstallButton->show();
ui->envLabel->show();
ui->envTextEdit->show();
ui->copyButton->show();
setFixedSize(size);
/*
QMessageBox msgBox(this);
msgBox.setWindowTitle(QObject::tr("Success"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setText("Installation completed :)");
msgBox.exec();
*/
}
ui->installButton->setText(installButtonText);
}
void MainWindow::uninstallButtonClicked(bool)
{
if (QMessageBox::question(this, tr("Confirmation"), tr("Uninstall pack ?"), QMessageBox::Ok | QMessageBox::Cancel) != QMessageBox::Ok) return;
QString uninstallButtonText = ui->uninstallButton->text();
ui->uninstallButton->setText(tr("Please wait")+"...");
ui->uninstallButton->repaint();
QApplication::processEvents();
if (!uninstallPack()) {
QMessageBox msgBox(this);
msgBox.setWindowTitle(QObject::tr("Error"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setText("An error occurred :(");
msgBox.exec();
} else {
QSize size = geometry().size();
ui->checkButton->hide();
ui->infoButton->hide();
ui->uninstallButton->hide();
ui->envLabel->hide();
ui->envTextEdit->hide();
ui->copyButton->hide();
ui->installButton->show();
if (ui->versionsLabel->isVisible()) {
ui->versionsLabel->hide();
}
if (!ui->ziraLabel->isVisible()){
ui->ziraLabel->show();
}
setFixedSize(size);
/*
QMessageBox msgBox(this);
msgBox.setWindowTitle(QObject::tr("Success"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setText("Uninstalled successfully :)");
msgBox.exec();
*/
}
ui->uninstallButton->setText(uninstallButtonText);
}
void MainWindow::showVersionsLabel()
{
if (ui->versionsLabel->isVisible()) return;
ui->versionsLabel->show();
ui->ziraLabel->hide();
}
void MainWindow::checkButtonClicked(bool)
{
QSize size = geometry().size();
showVersionsLabel();
ui->envLabel->show();
ui->envTextEdit->show();
ui->copyButton->show();
showVersions();
setFixedSize(size);
}
void MainWindow::infoButtonClicked(bool)
{
QSize size = geometry().size();
showVersionsLabel();
ui->envLabel->hide();
ui->envTextEdit->hide();
ui->copyButton->hide();
showPHPInfo();
setFixedSize(size);
}
bool MainWindow::installPack(bool cleanELF)
{
if (dataDir.size() == 0) return false;
QFile f(dataDir);
setPermissions(f);
QFileInfo di(dataDir);
if (!di.isWritable() || !di.isExecutable()) return false;
QDir d(dataDir);
if (!createDirectory(d, dataDir+"/"+BIN_DIR)) return false;
if (!createDirectory(d, dataDir+"/"+BIN_DIR+"/"+GIT_CORE_DIR)) return false;
if (!createDirectory(d, dataDir+"/"+GIT_TEMPLATES_DIR)) return false;
if (!createDirectory(d, dataDir+"/"+PHP_TMP_DIR)) return false;
if (!createDirectory(d, dataDir+"/"+TERMINFO_DIR)) return false;
if (!createDirectory(d, dataDir+"/"+MC_DIR)) return false;
if (!createDirectory(d, dataDir+"/"+MC_DIR+"/"+MC_THEMES_SUBDIR)) return false;
if (!createDirectory(d, dataDir+"/"+MC_DIR+"/"+MC_SYNTAX_SUBDIR)) return false;
for (QString fileName : binFiles) {
if (!installBinaryFile(fileName, dataDir+"/"+BIN_DIR)) return false;
}
for (QString fileName : gitFiles) {
if (!installBinaryFile(fileName, dataDir+"/"+BIN_DIR+"/"+GIT_CORE_DIR)) return false;
}
for (QString fileName : termFiles) {
if (fileName.size() == 0) continue;
QChar c = fileName.at(0);
if (!createDirectory(d, dataDir+"/"+TERMINFO_DIR+"/"+c)) return false;
if (!installBinaryFile(fileName, dataDir+"/"+TERMINFO_DIR+"/"+c)) return false;
}
for (QString fileName : mcFiles) {
if (!installBinaryFile(fileName, dataDir+"/"+MC_DIR)) return false;
}
for (QString fileName : mcThemeFiles) {
if (!installBinaryFile(fileName, dataDir+"/"+MC_DIR+"/"+MC_THEMES_SUBDIR)) return false;
}
for (QString fileName : mcSyntaxFiles) {
if (!installBinaryFile(fileName, dataDir+"/"+MC_DIR+"/"+MC_SYNTAX_SUBDIR)) return false;
}
for (QString fileName : otherFiles) {
if (!installBinaryFile(fileName, dataDir)) return false;
}
QFile gitConfigHidden(dataDir+"/.gitconfig");
if (gitConfigHidden.exists()) gitConfigHidden.remove();
QFile gitConfig(dataDir+"/gitconfig");
if (gitConfig.exists()) gitConfig.rename(".gitconfig");
if (cleanELF) {
QStringList cleanFiles;
for (QString fileName : binFiles) {
if (fileName == "termux-elf-cleaner") continue;
cleanFiles << dataDir+"/"+BIN_DIR+"/"+fileName;
}
for (QString fileName : gitFiles) {
cleanFiles << dataDir+"/"+BIN_DIR+"/"+GIT_CORE_DIR+"/"+fileName;
}
QProcess cleanProcess(this);
cleanProcess.start(dataDir+"/"+BIN_DIR+"/termux-elf-cleaner", cleanFiles);
if (!cleanProcess.waitForFinished()) return false;
}
return true;
}
bool MainWindow::uninstallPack()
{
if (dataDir.size() == 0) return false;
for (QString fileName : binFiles) {
if (!removeFile(dataDir+"/"+BIN_DIR+"/"+fileName)) return false;
}
for (QString fileName : gitFiles) {
if (!removeFile(dataDir+"/"+BIN_DIR+"/"+GIT_CORE_DIR+"/"+fileName)) return false;
}
for (QString fileName : termFiles) {
if (fileName.size() == 0) continue;
QChar c = fileName.at(0);
if (!removeFile(dataDir+"/"+TERMINFO_DIR+"/"+c+"/"+fileName)) return false;
}
for (QString fileName : mcFiles) {
if (!removeFile(dataDir+"/"+MC_DIR+"/"+fileName)) return false;
}
for (QString fileName : mcThemeFiles) {
if (!removeFile(dataDir+"/"+MC_DIR+"/"+MC_THEMES_SUBDIR+"/"+fileName)) return false;
}
for (QString fileName : mcSyntaxFiles) {
if (!removeFile(dataDir+"/"+MC_DIR+"/"+MC_SYNTAX_SUBDIR+"/"+fileName)) return false;
}
for (QString fileName : otherFiles) {
if (fileName == "gitconfig") fileName = "."+fileName;
if (!removeFile(dataDir+"/"+fileName)) return false;
}
if (!removeDirectory(dataDir+"/"+BIN_DIR+"/"+GIT_CORE_DIR)) return false;
if (!removeDirectory(dataDir+"/"+BIN_DIR)) return false;
if (!removeDirectory(dataDir+"/"+GIT_TEMPLATES_DIR)) return false;
if (!removeDirectory(dataDir+"/"+PHP_TMP_DIR)) return false;
if (!removeDirectory(dataDir+"/"+TERMINFO_DIR)) return false;
if (!removeDirectory(dataDir+"/"+MC_DIR+"/"+MC_THEMES_SUBDIR)) return false;
if (!removeDirectory(dataDir+"/"+MC_DIR+"/"+MC_SYNTAX_SUBDIR)) return false;
if (!removeDirectory(dataDir+"/"+MC_DIR)) return false;
return true;
}
bool MainWindow::createDirectory(QDir rootDir, QString path)
{
if (!rootDir.mkpath(path)) return false;
if (!setPermissions(path)) return false;
return true;
}
bool MainWindow::removeDirectory(QString path)
{
QDir dir(path);
if (!dir.exists()) return true;
if (!dir.removeRecursively()) return false;
return true;
}
bool MainWindow::installBinaryFile(QString fileName, QString installDir)
{
QFile f("assets:/"+fileName);
if (!f.exists()) return false;
QFile fi(installDir+"/"+fileName);
if (fi.exists()) fi.remove();
if (!f.copy(installDir+"/"+fileName)) return false;
QFile pf(installDir+"/"+fileName);
if (!setPermissions(pf)) return false;
return true;
}
bool MainWindow::removeFile(QString fileName)
{
QFile f(fileName);
if (!f.exists()) return true;
if (!f.remove()) return false;
return true;
}
bool MainWindow::setPermissions(QFile &f)
{
return f.setPermissions(
QFileDevice::ReadOwner |
QFileDevice::ReadGroup |
QFileDevice::ReadOther |
QFileDevice::WriteOwner |
QFileDevice::WriteGroup |
QFileDevice::WriteOther |
QFileDevice::ExeOwner |
QFileDevice::ExeGroup |
QFileDevice::ExeOther
);
}
bool MainWindow::setPermissions(QString path)
{
QFile f(path);
return setPermissions(f);
}
bool MainWindow::isPackInstalled()
{
for (QString fileName : binFiles) {
QFileInfo fileInfo(dataDir+"/"+BIN_DIR+"/"+fileName);
if (!fileInfo.exists() || !fileInfo.isExecutable()) return false;
}
for (QString fileName : gitFiles) {
QFileInfo fileInfo(dataDir+"/"+BIN_DIR+"/"+GIT_CORE_DIR+"/"+fileName);
if (!fileInfo.exists() || !fileInfo.isExecutable()) return false;
}
return true;
}
void MainWindow::copyPathClicked(bool)
{
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(ui->envTextEdit->toPlainText());
QMessageBox msgBox(this);
msgBox.setWindowTitle(QObject::tr("Message"));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setText("Copied to clipboard");
msgBox.exec();
}
void MainWindow::showVersions()
{
QString versions = "<div>";
QProcess phpProcess(this);
phpProcess.start(dataDir+"/"+BIN_DIR+"/php", QStringList() << "-v");
if (!phpProcess.waitForFinished()) return;
QByteArray phpResult = phpProcess.readAllStandardError();
phpResult += phpProcess.readAllStandardOutput();
versions += "<p>"+QString(phpResult).trimmed().split("\n").at(0) + "</p>";
QProcess gitProcess(this);
gitProcess.start(dataDir+"/"+BIN_DIR+"/git", QStringList() << "--version");
if (!gitProcess.waitForFinished()) return;
QByteArray gitResult = gitProcess.readAllStandardError();
gitResult += gitProcess.readAllStandardOutput();
versions += "<p>"+QString(gitResult).trimmed().split("\n").at(0) + "</p>";
QProcess sasscProcess(this);
sasscProcess.start(dataDir+"/"+BIN_DIR+"/sassc", QStringList() << "--version");
if (!sasscProcess.waitForFinished()) return;
QByteArray sasscResult = sasscProcess.readAllStandardError();
sasscResult += sasscProcess.readAllStandardOutput();
versions += "<p>"+QString(sasscResult).trimmed().split("\n").at(0) + "</p>";
QProcess sqlite3Process(this);
sqlite3Process.start(dataDir+"/"+BIN_DIR+"/sqlite3", QStringList() << "--version");
if (!sqlite3Process.waitForFinished()) return;
QByteArray sqlite3Result = sqlite3Process.readAllStandardError();
sqlite3Result += sqlite3Process.readAllStandardOutput();
versions += "<p>SQLite " + QString(sqlite3Result).trimmed().split("\n").at(0) + "</p>";
QProcess nanoProcess(this);
nanoProcess.start(dataDir+"/"+BIN_DIR+"/nano", QStringList() << "--version");
if (!nanoProcess.waitForFinished()) return;
QByteArray nanoResult = nanoProcess.readAllStandardError();
nanoResult += nanoProcess.readAllStandardOutput();
versions += "<p>"+QString(nanoResult).trimmed().split("\n").at(0) + "</p>";
QProcess vimProcess(this);
vimProcess.start(dataDir+"/"+BIN_DIR+"/vim", QStringList() << "--version");
if (!vimProcess.waitForFinished()) return;
QByteArray vimResult = vimProcess.readAllStandardError();
vimResult += vimProcess.readAllStandardOutput();
versions += "<p>"+QString(vimResult).trimmed().split("\n").at(0) + "</p>";
QProcess mcProcess(this);
mcProcess.start(dataDir+"/"+BIN_DIR+"/mc", QStringList() << "--version");
if (!mcProcess.waitForFinished()) return;
QByteArray mcResult = mcProcess.readAllStandardError();
mcResult += mcProcess.readAllStandardOutput();
versions += "<p>"+QString(mcResult).trimmed().split("\n").at(0) + "</p>";
versions += "</div>";
ui->versionsLabel->setText(versions);
}
void MainWindow::showPHPInfo()
{
QProcess phpProcess(this);
phpProcess.start(dataDir+"/"+BIN_DIR+"/php", QStringList() << "-r" << "phpinfo();");
if (!phpProcess.waitForFinished()) return;
QByteArray phpResult = phpProcess.readAllStandardOutput();
QStringList phpResultList = QString(phpResult).trimmed().split("\n");
QString phpResultStr = "<div>";
for (QString phpResultListItem : phpResultList) {
QStringList phpResultListItemParts = phpResultListItem.split("=>");
if (phpResultListItemParts.size() == 1) {
phpResultStr += "<h2>" + phpResultListItemParts.at(0) + "</h2>";
} else if (phpResultListItemParts.size() > 1) {
phpResultStr += "<p><b>" + phpResultListItemParts.at(0) + ":</b> " + "<span>" + phpResultListItemParts.at(1) + "</span></p>";
}
}
phpResultStr += "</div>";
ui->versionsLabel->setText(phpResultStr);
}
void MainWindow::applyStyle()
{
QFile f(":/styles/dark");
if (!f.exists()) return;
QString style = "";
f.open(QIODevice::ReadOnly);
QTextStream in(&f);
style += in.readAll() + "\n";
f.close();
setStyleSheet(style);
}