-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdertimegeraet.cpp
693 lines (585 loc) · 20.3 KB
/
dertimegeraet.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#include "dertimegeraet.h"
#include "ui_dertimegeraet.h"
#include <QFileDialog>
#include <QFileSystemModel>
#include <QDebug>
#include <QDesktopServices>
#include <QDateTime>
#include <QProcess>
#include <QCloseEvent>
#include <QMessageBox>
#include <QTimer>
#include <QMenu>
derTimeGeraet::derTimeGeraet(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::derTimeGeraet)
{
ui->setupUi(this);
ui->tabWidget->setCurrentIndex(1);
// Setup Settings:
settingsFile = QApplication::applicationDirPath() + "/settings.ini";
// Locate Borg:
locateBorg();
// Load Destination List: //TODO output should go to loadListofBackups (right now loadDestination must be called before load Backup list)
loadDestinationsList();
// Load List of Backups:
loadBackupList();
// Load Ignore List:
loadIgnoreList();
// Setup Tray:
setupTrayIcon();
// Prevent close from closing:
//this->setWindowFlags(Qt::Tool);
//QApplication::setQuitOnLastWindowClosed(false);
}
void derTimeGeraet::loadBackupList()
{
bool settingsAvailable = loadSettings();
// Setup Dir Model:
QString dir = ui->comboBoxDestination->currentText();
if(settingsAvailable == true && QDir(dir).exists())
{
ui->listWidget->clear();
QSettings settings(settingsFile, QSettings::NativeFormat);
QString pass = settings.value("pass", "").toString();
QStringList cmdList;
cmdList.append("list");
cmdList.append(dir);
QProcess pList;
qDebug() << cmdList;
// Add pass as environment variable:
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("BORG_PASSPHRASE", pass);
pList.setProcessEnvironment(env);
pList.start(borg, cmdList);
pList.waitForFinished(-1);
QString listString = pList.readAllStandardOutput();
QStringList list = listString.split('\n');
// Create model
for(int i = 0; i < list.size(); i++)
{
qDebug() << list.at(i);
QRegExp rx("([^\\s]+)\\s+\\w\\w\\w, (.+)\\s+\\[(.+)\\]");
QString name = "";
QString key = "";
bool match = rx.exactMatch(list.at(i));
if (match) {
name = rx.cap(2);
key = rx.cap(1);
}
auto *item = new QListWidgetItem(name);
QVariant v;
v.setValue(key);
item->setData(Qt::UserRole, v);
ui->listWidget->addItem(item);
}
ui->tabWidget->setCurrentIndex(0);
}
else
{
ui->listWidget->setEnabled(false);
ui->treeView->setEnabled(false);
ui->pushButtonStart->setEnabled(false);
}
}
derTimeGeraet::~derTimeGeraet()
{
qDebug() << "UMOUNT!";
umount();
delete ui;
}
void derTimeGeraet::closeEvent(QCloseEvent *event)
{
#ifdef Q_OS_OSX
if (!event->spontaneous() || !isVisible()) {
return;
}
#endif
if (trayIcon->isVisible())
{
QSettings settings(settingsFile, QSettings::NativeFormat);
if(settings.value("closeHint", "") == 0)
{
QMessageBox::information(this, tr("Systray"),
tr("The program will keep running in the "
"system tray. To terminate the program, "
"choose <b>Quit</b> in the context menu "
"of the system tray entry."));
settings.value("closeHint", 1);
}
hide();
event->ignore();
}
}
void derTimeGeraet::setupTrayIcon()
{
trayFrames.push_back(QIcon(":/img/gear-white.svg"));
trayFrames.push_back(QIcon(":/img/gear-white2.svg"));
trayIcon = new QSystemTrayIcon(trayFrames.at(0), this);
trayIconNumber = 0;
restoreAction = new QAction(tr("&Show"), this);
connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
QMenu *tray_icon_menu = new QMenu;
tray_icon_menu->addAction(quitAction);
tray_icon_menu->addAction(restoreAction);
trayIcon->setContextMenu( tray_icon_menu );
trayIcon->show();
timer = new QTimer(this);
timer->setSingleShot(false);
connect(timer, SIGNAL(timeout()), this, SLOT(updateTrayIcon()));
}
void derTimeGeraet::updateTrayIcon()
{
if(trayIconNumber == 0)
{
trayIconNumber = 1;
trayIcon->setIcon(trayFrames.at(trayIconNumber));
}
else
{
trayIconNumber = 0;
trayIcon->setIcon(trayFrames.at(trayIconNumber));
}
}
void derTimeGeraet::setTrayIcon(bool animated)
{
if(animated == true)
{
timer->start(500); // 500 ms
}
else
{
timer->stop();
}
}
bool derTimeGeraet::loadSettings()
{
if (QFile(settingsFile).exists())
{
QSettings settings(settingsFile, QSettings::NativeFormat);
QString source = settings.value("source", "").toString();
QString pass = settings.value("pass", "").toString();
QString hourly = settings.value("hourly", "").toString();
QString daily = settings.value("daily", "").toString();
QString weekly = settings.value("weekly", "").toString();
QString monthly = settings.value("monthly", "").toString();
QString yearly = settings.value("yearly", "").toString();
ui->lineEditSource->setText(source);
ui->linePassword->setText(pass);
ui->lineEditHourly->setText(hourly);
ui->lineEditDaily->setText(daily);
ui->lineEditWeekly->setText(weekly);
ui->lineEditMonthly->setText(monthly);
ui->lineEditYearly->setText(yearly);
ui->tab1->setEnabled(true);
ui->pushButtonStart->setEnabled(true);
return true;
}
else
{
ui->tab1->setEnabled(false);
return false;
}
}
void derTimeGeraet::saveSettings()
{
QSettings settings(settingsFile, QSettings::NativeFormat);
if(!ui->lineEditSource->text().isEmpty())
{
QString source = ui->lineEditSource->text();
settings.setValue("source", source);
}
if(!ui->linePassword->text().isEmpty())
{
QString pass = ui->linePassword->text();
settings.setValue("pass", pass);
}
if(!ui->lineEditHourly->text().isEmpty())
{
QString hourly = ui->lineEditHourly->text();
settings.setValue("hourly", hourly);
}
if(!ui->lineEditDaily->text().isEmpty())
{
QString daily = ui->lineEditDaily->text();
settings.setValue("daily", daily);
}
if(!ui->lineEditWeekly->text().isEmpty())
{
QString weekly = ui->lineEditWeekly->text();
settings.setValue("weekly", weekly);
}
if(!ui->lineEditMonthly->text().isEmpty())
{
QString monthly = ui->lineEditMonthly->text();
settings.setValue("monthly", monthly);
}
if(!ui->lineEditYearly->text().isEmpty())
{
QString yearly = ui->lineEditYearly->text();
settings.setValue("yearly", yearly);
}
}
void derTimeGeraet::on_pushButtonSource_clicked()
{
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select Directory"),
"/",
QFileDialog::ShowDirsOnly);
ui->lineEditSource->setText(dir);
saveSettings();
}
void derTimeGeraet::on_treeView_doubleClicked(const QModelIndex &index)
{
QString file = ui->treeView->model()->data(index, QFileSystemModel::FilePathRole).toString();
QDesktopServices::openUrl(QUrl("file://" + file, QUrl::TolerantMode));
}
void derTimeGeraet::on_pushButtonStart_clicked()
{
// Unmount
umount();
// Deactivate parts of UI:
ui->treeView->setDisabled(true);
ui->listWidget->setDisabled(true);
// Get stuff from config:
QString dest = ui->comboBoxDestination->currentText();
QString source = ui->lineEditSource->text();
QString pass = ui->linePassword->text();
QString log = "";
ui->plainTextEdit->setPlainText(log);
if(QDir(dest).exists() && QDir(source).exists())
{
/*
* borg create \
* --verbose \
* --filter AME \
* --list \
* --stats \
* --show-rc \
* --compression lz4 \
* --exclude-caches \
* --exclude '/Users/myzinsky/Desktop/Bilder' \
* --exclude '/Users/myzinsky/EMS/Programming/gem5.traces' \
* --exclude '/Users/myzinsky/Zeugs/steg/vid' \
* ::'{hostname}-{now}' \
* /Users/myzinsky \
*/
time = QDateTime::currentDateTime().toString(Qt::DateFormat::ISODate);
QStringList cmdBorg;
cmdBorg.append("create");
cmdBorg.append("--filter");
cmdBorg.append("AME");
cmdBorg.append("--stats");
cmdBorg.append("--show-rc");
cmdBorg.append("--compression");
cmdBorg.append("lz4");
cmdBorg.append("--exclude-caches");
for(int i = 0; i < ui->listWidgetExeptions->count(); i++)
{
QString ignore = ui->listWidgetExeptions->item(i)->text();
cmdBorg.append("--exclude");
cmdBorg.append(ignore);
}
//cmdBorg.append(dest + "::{hostname}-{now}");
cmdBorg.append(dest + "::pizza.jung.ms-{now}");
cmdBorg.append(source);
qDebug() << cmdBorg;
ui->pushButtonStart->setText("Preparing Backup (This may take a while)");
ui->pushButtonStart->setDisabled(true);
setTrayIcon(true);
pBorg = new QProcess(this);
// Add pass as environment variable:
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("BORG_PASSPHRASE", pass);
pBorg->setProcessEnvironment(env);
connect(pBorg,
SIGNAL(finished(int , QProcess::ExitStatus )),
this,
SLOT(on_borgFinished(int , QProcess::ExitStatus )));
pBorg->start(borg, cmdBorg);
}
}
void derTimeGeraet::on_pushButtonExeptionsAdd_clicked()
{
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select Directory"),
"/",
QFileDialog::ShowDirsOnly);
// TODO: check if item already exists ...
ui->listWidgetExeptions->addItem(dir);
saveIgnoreList();
}
void derTimeGeraet::loadIgnoreList()
{
QSettings settings(settingsFile, QSettings::NativeFormat);
int size = settings.beginReadArray("Ignores");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
QString ignore = settings.value("Ignore").toString();
ui->listWidgetExeptions->addItem(ignore);
}
settings.endArray();
}
void derTimeGeraet::saveIgnoreList()
{
QSettings settings(settingsFile, QSettings::NativeFormat);
settings.beginWriteArray("Ignores");
for(int i = 0; i < ui->listWidgetExeptions->count(); i++)
{
QString ignore = ui->listWidgetExeptions->item(i)->text();
settings.setArrayIndex(i);
settings.setValue("Ignore", ignore);
}
settings.endArray();
}
void derTimeGeraet::loadDestinationsList()
{
QSettings settings(settingsFile, QSettings::NativeFormat);
int size = settings.beginReadArray("Destinations");
for (int i = 0; i < size; ++i) {
settings.setArrayIndex(i);
QString ignore = settings.value("Destination").toString();
ui->listWidgetDestinations->addItem(ignore);
ui->comboBoxDestination->addItem(ignore);
}
settings.endArray();
}
void derTimeGeraet::saveDestinationsList()
{
QSettings settings(settingsFile, QSettings::NativeFormat);
settings.beginWriteArray("Destinations");
for(int i = 0; i < ui->listWidgetDestinations->count(); i++)
{
QString ignore = ui->listWidgetDestinations->item(i)->text();
settings.setArrayIndex(i);
settings.setValue("Destination", ignore);
}
settings.endArray();
}
void derTimeGeraet::locateBorg()
{
if(QFile::exists("/usr/local/bin/borg"))
{
borg = "/usr/local/bin/borg";
}
else if(QFile::exists("/usr/bin/borg"))
{
borg = "/usr/bin/borg";
}
else if(QFile::exists("/bin/borg"))
{
borg = "/bin/borg";
}
else
{
QMessageBox::critical(this,
tr("Der Time Gerät"),
tr("Cannot find borg"));
exit(-1);
}
}
void derTimeGeraet::on_pushButtonExeptionsRemove_clicked()
{
QList<QListWidgetItem*> items = ui->listWidgetExeptions->selectedItems();
foreach(QListWidgetItem * item, items)
{
delete ui->listWidgetExeptions->takeItem(ui->listWidgetExeptions->row(item));
}
saveIgnoreList();
}
void derTimeGeraet::on_pushButtonDestRemove_clicked()
{
QList<QListWidgetItem*> items = ui->listWidgetDestinations->selectedItems();
foreach(QListWidgetItem * item, items)
{
delete ui->listWidgetDestinations->takeItem(ui->listWidgetDestinations->row(item));
}
saveDestinationsList();
}
void derTimeGeraet::on_borgFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
QString log;
log = "Borg Return Code: #####################################\n";
log += QString::number(exitCode);
log += "\n\nError String: ##########################################\n";
log += pBorg->errorString();
log += "\n\nStandard Error: ########################################\n";
log += pBorg->readAllStandardError();
log += "\n\nStandard Output: #######################################\n";
log += pBorg->readAllStandardOutput();
ui->plainTextEdit->setPlainText(log);
if(exitStatus == QProcess::NormalExit && (exitCode == 0 || exitCode == 1))
{
prune();
}
else
{
QMessageBox::critical(this,
tr("Der Time Gerät"),
tr("Synchronisation Failed"));
// TODO cleanup again
}
}
void derTimeGeraet::prune()
{
QString dest = ui->comboBoxDestination->currentText();
QString source = ui->lineEditSource->text();
QString pass = ui->linePassword->text();
QString hourly = ui->lineEditHourly->text();
QString daily = ui->lineEditDaily->text();
QString weekly = ui->lineEditWeekly->text();
QString monthly = ui->lineEditMonthly->text();
QString yearly = ui->lineEditYearly->text();
if(QDir(dest).exists() && QDir(source).exists())
{
/*
* borg prune \
* --list \
* --prefix '{hostname}-' \
* --show-rc \
* --keep-daily 7 \
* --keep-weekly 4 \
* --keep-monthly 6 \
* --keep-yearly 1 \
*/
time = QDateTime::currentDateTime().toString(Qt::DateFormat::ISODate);
QStringList cmdBorg;
cmdBorg.append("prune");
cmdBorg.append("--list");
cmdBorg.append("--prefix");
//cmdBorg.append("{hostname}-");
cmdBorg.append("pizza.jung.ms-");
cmdBorg.append("--show-rc");
cmdBorg.append("--keep-hourly");
cmdBorg.append(hourly);
cmdBorg.append("--keep-daily");
cmdBorg.append(daily);
cmdBorg.append("--keep-weekly");
cmdBorg.append(weekly);
cmdBorg.append("--keep-monthly");
cmdBorg.append(monthly);
cmdBorg.append("--keep-yearly");
cmdBorg.append(yearly);
cmdBorg.append(dest);
qDebug() << cmdBorg;
ui->pushButtonStart->setText("Pruning (This may take a while)");
ui->pushButtonStart->setDisabled(true);
pPrune = new QProcess(this);
// Add pass as environment variable:
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("BORG_PASSPHRASE", pass);
pPrune->setProcessEnvironment(env);
connect(pPrune,
SIGNAL(finished(int , QProcess::ExitStatus )),
this,
SLOT(on_pruneFinished(int , QProcess::ExitStatus )));
// TODO detect borg:
pPrune->start(borg, cmdBorg);
}
}
void derTimeGeraet::on_pruneFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
QString log = ui->plainTextEdit->toPlainText()+"\n\n";
log += "Prune Return Code: #####################################\n";
log += QString::number(exitCode);
log += "\n\nError String:###########################################\n";
log += pBorg->errorString();
log += "\n\nStandard Error: ########################################\n";
log += pBorg->readAllStandardError();
log += "\n\nStandard Output: #######################################\n";
log += pBorg->readAllStandardOutput();
ui->plainTextEdit->setPlainText(log);
loadBackupList();
// Restore parts of UI:
ui->treeView->setEnabled(true);
ui->listWidget->setEnabled(true);
ui->pushButtonStart->setEnabled(true);
ui->pushButtonStart->setText("Start Backup");
setTrayIcon(false);
exitStatus = QProcess::ExitStatus::NormalExit;
}
void derTimeGeraet::on_pushButtonPassword_clicked()
{
saveSettings();
}
void derTimeGeraet::on_pushButtonPruning_clicked()
{
saveSettings();
}
void derTimeGeraet::mount(QString key)
{
QSettings settings(settingsFile, QSettings::NativeFormat);
QString dest = ui->comboBoxDestination->currentText();
QString source = ui->lineEditSource->text();
QString pass = settings.value("pass", "").toString();
QString dir = "/tmp/derTimeGeraet/";
// MOUNT:
if(!QDir(dir).exists())
{
QDir().mkdir(dir);
}
QStringList cmdMount;
cmdMount.append("mount");
cmdMount.append(dest+"::"+key);
cmdMount.append("/tmp/derTimeGeraet/");
QProcess pMount;
qDebug() << cmdMount;
// Add pass as environment variable:
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("BORG_PASSPHRASE", pass);
pMount.setProcessEnvironment(env);
pMount.start(borg, cmdMount);
pMount.waitForFinished(-1);
qDebug() << pMount.readAllStandardError();
dirModel = new QFileSystemModel(this);
dirModel->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
dirModel->setRootPath("/");
ui->treeView->setModel(dirModel);
ui->treeView->setRootIndex(dirModel->index(dir+source));
ui->treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
}
void derTimeGeraet::umount()
{
QSettings settings(settingsFile, QSettings::NativeFormat);
QString dest = ui->comboBoxDestination->currentText();
QString pass = settings.value("pass", "").toString();
// UMOUNT:
QStringList cmdUmount;
cmdUmount.append("umount");
cmdUmount.append("/tmp/derTimeGeraet/");
QProcess pUmount;
qDebug() << cmdUmount;
// Add pass as environment variable:
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("BORG_PASSPHRASE", pass);
pUmount.setProcessEnvironment(env);
pUmount.start(borg, cmdUmount);
pUmount.waitForFinished(-1);
qDebug() << pUmount.readAllStandardError();
ui->treeView->setModel(nullptr);
}
void derTimeGeraet::on_listWidget_itemClicked(QListWidgetItem *item)
{
umount();
QVariant v = item->data(Qt::UserRole);
QString key = v.value<QString>();
mount(key);
}
void derTimeGeraet::on_pushButtonDestAdd_clicked()
{
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select Directory"),
"/",
QFileDialog::ShowDirsOnly);
ui->listWidgetDestinations->addItem(dir);
ui->comboBoxDestination->addItem(dir);
saveDestinationsList();
}
void derTimeGeraet::on_comboBoxDestination_currentIndexChanged(const QString &arg1)
{
qDebug() << arg1;
umount();
loadBackupList();
}