-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_window.cpp
1222 lines (1044 loc) · 44 KB
/
main_window.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "main_window.h"
#include "job_options.h"
#include "job_widget.h"
#include "list_of_job_options.h"
#include "mount_widget.h"
#include "preferences_dialog.h"
#include "remote_widget.h"
#include "stream_widget.h"
#include "transfer_dialog.h"
#include "utils.h"
#ifdef Q_OS_MACOS
#include "osx_helper.h"
#endif
MainWindow::MainWindow() {
ui.setupUi(this);
if (IsPortableMode()) {
this->setWindowTitle("Rclone Browser - portable mode");
} else {
this->setWindowTitle("Rclone Browser");
}
#if defined(Q_OS_WIN)
// disable "?" WindowContextHelpButton
QApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton);
#endif
#if !defined(Q_OS_MACOS)
auto settings = GetSettings();
bool darkMode = settings->value("Settings/darkMode").toBool();
// enable dark mode for Windows and Linux
if (darkMode) {
qApp->setStyle(QStyleFactory::create("Fusion"));
QPalette darkPalette;
darkPalette.setColor(QPalette::Window, QColor(53, 53, 53));
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(25, 25, 25));
darkPalette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
darkPalette.setColor(QPalette::ToolTipBase, Qt::white);
darkPalette.setColor(QPalette::ToolTipText, Qt::white);
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Button, QColor(53, 53, 53));
darkPalette.setColor(QPalette::ButtonText, Qt::white);
darkPalette.setColor(QPalette::BrightText, Qt::red);
darkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
darkPalette.setColor(QPalette::Highlight, QColor(42, 130, 218));
darkPalette.setColor(QPalette::HighlightedText, Qt::black);
qApp->setPalette(darkPalette);
qApp->setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; "
"border: 1px solid white; }");
}
#else
// enable dark mode for older macOS
QString sysInfo = QSysInfo::productVersion();
if (sysInfo == "10.9" || sysInfo == "10.10" || sysInfo == "10.11" ||
sysInfo == "10.12" || sysInfo == "10.13") {
auto settings = GetSettings();
bool darkMode = settings->value("Settings/darkMode").toBool();
if (darkMode) {
qApp->setStyle(QStyleFactory::create("Fusion"));
QPalette darkPalette;
darkPalette.setColor(QPalette::Window, QColor(53, 53, 53));
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(25, 25, 25));
darkPalette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
darkPalette.setColor(QPalette::ToolTipBase, Qt::white);
darkPalette.setColor(QPalette::ToolTipText, Qt::white);
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Button, QColor(53, 53, 53));
darkPalette.setColor(QPalette::ButtonText, Qt::white);
darkPalette.setColor(QPalette::BrightText, Qt::red);
darkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
darkPalette.setColor(QPalette::Highlight, QColor(42, 130, 218));
darkPalette.setColor(QPalette::HighlightedText, Qt::black);
qApp->setPalette(darkPalette);
qApp->setStyleSheet("QToolTip { color: #ffffff; background-color: "
"#2a82da; border: 1px solid white; }");
}
}
#endif
mSystemTray.setIcon(qApp->windowIcon());
{
auto settings = GetSettings();
if (settings->contains("MainWindow/geometry")) {
restoreGeometry(settings->value("MainWindow/geometry").toByteArray());
}
SetRclone(settings->value("Settings/rclone").toString());
SetRcloneConf(settings->value("Settings/rcloneConf").toString());
mAlwaysShowInTray =
settings->value("Settings/alwaysShowInTray", false).toBool();
mCloseToTray = settings->value("Settings/closeToTray", false).toBool();
mNotifyFinishedTransfers =
settings->value("Settings/notifyFinishedTransfers", true).toBool();
mSystemTray.setVisible(mAlwaysShowInTray);
// during first run the lastUsed keys might not exist
if (!(settings->contains("Settings/lastUsedSourceFolder"))) {
// if lastUsedSourceFolder does not exist create new empty key
settings->setValue("Settings/lastUsedSourceFolder", "");
};
if (!(settings->contains("Settings/lastUsedDestFolder"))) {
// if lastUsedDestFolder does not exist create new empty key
settings->setValue("Settings/lastUsedDestFolder", "");
};
if (!(settings->contains("Settings/defaultDownloadOptions"))) {
// if defaultDownloadOptions does not exist create new empty key
settings->setValue("Settings/defaultDownloadOptions", "");
};
#ifdef Q_OS_MACOS
// for macOS by default exclude .DS_Store files from uploads
if (!(settings->contains("Settings/defaultUploadOptions"))) {
// if defaultDownloadOptions does not exist create new empty key
settings->setValue("Settings/defaultUploadOptions",
"--exclude .DS_Store");
};
#else
if (!(settings->contains("Settings/defaultUploadOptions"))) {
// if defaultDownloadOptions does not exist create new empty key
settings->setValue("Settings/defaultUploadOptions", "");
};
#endif
if (!(settings->contains("Settings/defaultRcloneOptions"))) {
// if defaultRcloneOptions does not exist create new empty key
settings->setValue("Settings/defaultRcloneOptions", "--fast-list");
};
}
QObject::connect(ui.preferences, &QAction::triggered, this, [=]() {
PreferencesDialog dialog(this);
if (dialog.exec() == QDialog::Accepted) {
auto settings = GetSettings();
settings->setValue("Settings/rclone", dialog.getRclone().trimmed());
settings->setValue("Settings/rcloneConf",
dialog.getRcloneConf().trimmed());
settings->setValue("Settings/stream", dialog.getStream());
settings->setValue("Settings/mount", dialog.getMount());
settings->setValue("Settings/defaultDownloadDir",
dialog.getDefaultDownloadDir().trimmed());
settings->setValue("Settings/defaultUploadDir",
dialog.getDefaultUploadDir().trimmed());
settings->setValue("Settings/defaultDownloadOptions",
dialog.getDefaultDownloadOptions().trimmed());
settings->setValue("Settings/defaultUploadOptions",
dialog.getDefaultUploadOptions().trimmed());
settings->setValue("Settings/defaultRcloneOptions",
dialog.getDefaultRcloneOptions().trimmed());
settings->setValue("Settings/checkRcloneBrowserUpdates",
dialog.getCheckRcloneBrowserUpdates());
settings->setValue("Settings/checkRcloneUpdates",
dialog.getCheckRcloneUpdates());
settings->setValue("Settings/alwaysShowInTray",
dialog.getAlwaysShowInTray());
settings->setValue("Settings/closeToTray", dialog.getCloseToTray());
settings->setValue("Settings/notifyFinishedTransfers",
dialog.getNotifyFinishedTransfers());
settings->setValue("Settings/showFolderIcons",
dialog.getShowFolderIcons());
settings->setValue("Settings/showFileIcons", dialog.getShowFileIcons());
settings->setValue("Settings/rowColors", dialog.getRowColors());
settings->setValue("Settings/showHidden", dialog.getShowHidden());
settings->setValue("Settings/darkMode", dialog.getDarkMode());
settings->setValue("Settings/iconSize", dialog.getIconSize().trimmed());
settings->setValue("Settings/useProxy", dialog.getUseProxy());
settings->setValue("Settings/http_proxy",
dialog.getHttpProxy().trimmed());
settings->setValue("Settings/https_proxy",
dialog.getHttpsProxy().trimmed());
settings->setValue("Settings/no_proxy", dialog.getNoProxy().trimmed());
SetRclone(dialog.getRclone());
SetRcloneConf(dialog.getRcloneConf());
mFirstTime = true;
rcloneGetVersion();
mAlwaysShowInTray = dialog.getAlwaysShowInTray();
mCloseToTray = dialog.getCloseToTray();
mNotifyFinishedTransfers = dialog.getNotifyFinishedTransfers();
mSystemTray.setVisible(mAlwaysShowInTray);
}
});
QObject::connect(ui.quit, &QAction::triggered, this, [=]() {
mCloseToTray = false;
close();
});
QObject::connect(ui.about, &QAction::triggered, this, [=]() {
QMessageBox::about(
this, "Rclone Browser",
QString(
R"(<h3>GUI for rclone, v)" RCLONE_BROWSER_VERSION "</h3>"
R"(<p>Copyright © 2019</p>)"
R"(<p>Current development and maintenance<br /><a href="https://github.com/kapitainsky/RcloneBrowser">kapitainsky</a></p>)"
R"(<p>New features and fixes<br /><a href="https://github.com/kapitainsky/RcloneBrowser/graphs/contributors">contributors</a></p>)"
R"(<p>Original version<br /><a href="https://mmozeiko.github.io/RcloneBrowser">Martins Mozeiko</a></p>)"));
});
QObject::connect(ui.aboutQt, &QAction::triggered, qApp,
&QApplication::aboutQt);
QObject::connect(
ui.remotes, &QListWidget::currentItemChanged, this,
[=](QListWidgetItem *current) { ui.open->setEnabled(current != NULL); });
QObject::connect(ui.remotes, &QListWidget::itemActivated, ui.open,
&QPushButton::clicked);
QObject::connect(ui.config, &QPushButton::clicked, this,
&MainWindow::rcloneConfig);
QObject::connect(ui.refresh, &QPushButton::clicked, this,
&MainWindow::rcloneListRemotes);
QObject::connect(ui.open, &QPushButton::clicked, this, [=]() {
auto item = ui.remotes->selectedItems().front();
QString type = item->data(Qt::UserRole).toString();
QString name = item->text();
bool isLocal = type == "local";
bool isGoogle = type == "drive";
auto remote = new RemoteWidget(&mIcons, name, isLocal, isGoogle, ui.tabs);
QObject::connect(remote, &RemoteWidget::addMount, this,
&MainWindow::addMount);
QObject::connect(remote, &RemoteWidget::addStream, this,
&MainWindow::addStream);
QObject::connect(remote, &RemoteWidget::addTransfer, this,
&MainWindow::addTransfer);
int index = ui.tabs->addTab(remote, name);
ui.tabs->setCurrentIndex(index);
});
QObject::connect(ui.tabs, &QTabWidget::tabCloseRequested, ui.tabs,
&QTabWidget::removeTab);
QObject::connect(ui.tasksListWidget, &QListWidget::currentItemChanged, this,
[=](QListWidgetItem *current) {
ui.buttonDeleteTask->setEnabled(current != nullptr);
ui.buttonEditTask->setEnabled(current != nullptr);
ui.buttonRunTask->setEnabled(current != nullptr);
ui.buttonDryrunTask->setEnabled(current != nullptr);
});
QObject::connect(ui.buttonRunTask, &QPushButton::clicked, this, [=]() {
JobOptionsListWidgetItem *item = static_cast<JobOptionsListWidgetItem *>(
ui.tasksListWidget->currentItem());
runItem(item);
});
QObject::connect(ui.buttonDryrunTask, &QPushButton::clicked, this, [=]() {
JobOptionsListWidgetItem *item = static_cast<JobOptionsListWidgetItem *>(
ui.tasksListWidget->currentItem());
runItem(item, true);
});
// QObject::connect(ui.tasksListWidget, &QListWidget::itemDoubleClicked,
// this, [=]()
// {
// editSelectedTask();
// });
QObject::connect(ui.buttonEditTask, &QPushButton::clicked, this,
[=]() { editSelectedTask(); });
QObject::connect(ui.buttonDeleteTask, &QPushButton::clicked, this, [=]() {
JobOptionsListWidgetItem *item = static_cast<JobOptionsListWidgetItem *>(
ui.tasksListWidget->currentItem());
JobOptions *jo = item->GetData();
ListOfJobOptions::getInstance()->Forget(jo);
});
QObject::connect(ListOfJobOptions::getInstance(),
&ListOfJobOptions::tasksListUpdated, this,
&MainWindow::listTasks);
QStyle *style = QApplication::style();
ui.buttonDeleteTask->setIcon(style->standardIcon(QStyle::SP_TrashIcon));
ui.buttonEditTask->setIcon(style->standardIcon(QStyle::SP_FileIcon));
ui.buttonRunTask->setIcon(style->standardIcon(QStyle::SP_CommandLink));
mUploadIcon = style->standardIcon(QStyle::SP_ArrowUp);
mDownloadIcon = style->standardIcon(QStyle::SP_ArrowDown);
ui.tabs->tabBar()->setTabButton(0, QTabBar::RightSide, nullptr);
ui.tabs->tabBar()->setTabButton(0, QTabBar::LeftSide, nullptr);
ui.tabs->tabBar()->setTabButton(1, QTabBar::RightSide, nullptr);
ui.tabs->tabBar()->setTabButton(1, QTabBar::LeftSide, nullptr);
ui.tabs->tabBar()->setTabButton(2, QTabBar::RightSide, nullptr);
ui.tabs->tabBar()->setTabButton(2, QTabBar::LeftSide, nullptr);
ui.tabs->setCurrentIndex(0);
listTasks();
QObject::connect(&mSystemTray, &QSystemTrayIcon::activated, this,
[=](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::DoubleClick ||
reason == QSystemTrayIcon::Trigger) {
showNormal();
mSystemTray.setVisible(mAlwaysShowInTray);
#ifdef Q_OS_MACOS
osxShowDockIcon();
#endif
}
});
QObject::connect(&mSystemTray, &QSystemTrayIcon::messageClicked, this, [=]() {
showNormal();
mSystemTray.setVisible(mAlwaysShowInTray);
#ifdef Q_OS_MACOS
osxShowDockIcon();
#endif
ui.tabs->setCurrentIndex(1);
if (mLastFinished) {
mLastFinished->showDetails();
ui.jobsArea->ensureWidgetVisible(mLastFinished);
}
});
QMenu *trayMenu = new QMenu(this);
QObject::connect(
trayMenu->addAction("&Show"), &QAction::triggered, this, [=]() {
MainWindow::setWindowState((windowState() & ~Qt::WindowMinimized) |
Qt::WindowActive);
MainWindow::show(); // bring window to top on macOS
MainWindow::raise(); // bring window from minimized state on macOS
MainWindow::activateWindow(); // bring window to front/unminimize on
// windows
mSystemTray.setVisible(mAlwaysShowInTray);
#ifdef Q_OS_MACOS
osxShowDockIcon();
#endif
});
QObject::connect(trayMenu->addAction("&Quit"), &QAction::triggered, this,
&QWidget::close);
mSystemTray.setContextMenu(trayMenu);
mStatusMessage = new QLabel();
ui.statusBar->addWidget(mStatusMessage);
ui.statusBar->setStyleSheet("QStatusBar::item { border: 0; }");
QTimer::singleShot(0, ui.remotes, SLOT(setFocus()));
QString rclone = GetRclone();
if (rclone.isEmpty()) {
rclone = QStandardPaths::findExecutable("rclone");
if (rclone.isEmpty()) {
QMessageBox::information(
this, "Error",
"Cannot check rclone version!\nPlease verify rclone location.");
emit ui.preferences->trigger();
} else {
auto settings = GetSettings();
settings->setValue("Settings/rclone", rclone);
SetRclone(rclone);
}
} else {
rcloneGetVersion();
}
}
MainWindow::~MainWindow() {
auto settings = GetSettings();
settings->setValue("MainWindow/geometry", saveGeometry());
}
void MainWindow::rcloneGetVersion() {
bool firstTime = mFirstTime;
mFirstTime = false;
QProcess *p = new QProcess();
QObject::connect(
p,
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
&QProcess::finished),
this, [=](int code, QProcess::ExitStatus) {
if (code == 0) {
QString version = p->readAllStandardOutput().trimmed();
// extract rclone version - numbers only
QString rclone_info1 = version;
QString rclone_version_no;
int lineBreak = rclone_info1.indexOf('\n');
if (lineBreak != -1) {
rclone_info1.remove(lineBreak, rclone_info1.length() - lineBreak);
rclone_version_no = rclone_info1;
rclone_version_no.replace("rclone v", "");
rclone_version_no.replace("-DEV", "");
} else {
// for very old rclone versions format was one line only
rclone_version_no = rclone_info1.trimmed();
rclone_version_no.replace("rclone v", "");
rclone_version_no.replace("-DEV", "");
}
// save current version no in settings
auto settings = GetSettings();
settings->setValue("Settings/rcloneVersion", rclone_version_no);
#if defined(Q_OS_WIN32)
// check if required version
unsigned int result =
compareVersion(rclone_version_no.toStdString(), "1.50");
if (result == 2) {
QMessageBox::warning(
this, "",
"For mount functionality to work you need "
"rclone version at least v1.50 "
"and your current version is v" +
rclone_version_no +
". Mount will be disabled. \n\nPlease consider upgrading.");
};
#endif
QStringList lines = version.split("\n", Qt::SkipEmptyParts);//QStringList lines = version.split("\n", QString::SkipEmptyParts);
QString rclone_info2;
QString rclone_info3;
int counter = 0;
foreach (QString line, lines) {
line = line.trimmed();
if (counter == 1)
rclone_info2 = line.replace("- ", "");
if (counter == 2)
rclone_info3 = line.replace("- ", "");
counter++;
};
QFileInfo appBundlePath;
#ifdef Q_OS_MACOS
if (IsPortableMode()) {
QFileInfo applicationPath = qApp->applicationFilePath();
QFileInfo MacOSPath = applicationPath.dir().path();
QFileInfo ContentsPath = MacOSPath.dir().path();
appBundlePath = ContentsPath.dir().path();
mStatusMessage->setText(
rclone_info1 + " in " +
QDir::toNativeSeparators(GetRclone().replace(
appBundlePath.fileName() + "/Contents/MacOS/../../../",
"")) +
", " + rclone_info2 + ", " + rclone_info3);
} else {
mStatusMessage->setText(rclone_info1 + " in " +
QDir::toNativeSeparators(GetRclone()) +
", " + rclone_info2 + ", " + rclone_info3);
}
#else
#ifdef Q_OS_WIN
mStatusMessage->setText(rclone_info1 + " in " +
QDir::toNativeSeparators(GetRclone()) + ", " +
rclone_info2 + ", " + rclone_info3);
#else
if (IsPortableMode()) {
QString xdg_config_home = qgetenv("XDG_CONFIG_HOME");
QString appImageConfigFolder = xdg_config_home.right(xdg_config_home.length()-xdg_config_home.lastIndexOf("/"));
mStatusMessage->setText(rclone_info1 + " in " +
QDir::toNativeSeparators(GetRclone().replace(appImageConfigFolder + "/..", "")) + ", " +
rclone_info2 + ", " + rclone_info3);
} else {
mStatusMessage->setText(rclone_info1 + " in " +
QDir::toNativeSeparators(GetRclone()) + ", " +
rclone_info2 + ", " + rclone_info3);
}
#endif
#endif
rcloneListRemotes();
} else {
if (p->error() != QProcess::FailedToStart) {
if (getConfigPassword(p)) {
rcloneGetVersion();
} else {
close();
}
p->deleteLater();
return;
}
if (firstTime) {
if (p->error() == QProcess::FailedToStart) {
QMessageBox::information(
this, "Error",
"Wrong rclone executable or rclone not found!\nPlease select "
"its location in next dialog.");
} else {
QMessageBox::information(this, "Error",
"Cannot check rclone version!\nPlease "
"verify rclone location.");
}
emit ui.preferences->trigger();
}
}
auto settings = GetSettings();
/// check rclone version
// get already stored rclone version no
QString rclone_version_no =
settings->value("Settings/rcloneVersion").toString();
// during first run the key might not exist yet
if (!(settings->contains("Settings/checkRcloneUpdates"))) {
// if checkRcloneUpdates does not exist create new key
settings->setValue("Settings/checkRcloneUpdates", true);
};
bool checkRcloneUpdates =
settings->value("Settings/checkRcloneUpdates").toBool();
// if check updates enabled in settings
if (checkRcloneUpdates) {
QString last_check;
QString current_date = QDate::currentDate().toString();
if (!(settings->contains("Settings/lastRcloneUpdateCheck"))) {
// if lastRcloneUpdateCheck does not exist create new key
settings->setValue("Settings/lastRcloneUpdateCheck", current_date);
} else { // read last check date
last_check =
settings->value("Settings/lastRcloneUpdateCheck").toString();
};
// dont check if already checked today (once per day only)
if (!(last_check == current_date)) {
// remmber when last checked
settings->setValue("Settings/lastRcloneUpdateCheck", current_date);
QString url =
"https://api.github.com/repos/rclone/rclone/releases/latest";
QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
QEventLoop event;
connect(response, SIGNAL(finished()), &event, SLOT(quit()));
event.exec();
QByteArray content = response->readAll();
QJsonParseError jsonError;
QJsonDocument document = QJsonDocument::fromJson(
content, &jsonError); // parse and capture the error flag
if (jsonError.error == QJsonParseError::NoError) {
if (document.object().contains("tag_name")) {
QJsonValue tag_name = document.object().value("tag_name");
QString rclone_latest_version_no = tag_name.toString(QString());
rclone_latest_version_no.replace("v", "");
rclone_latest_version_no.replace("-DEV", "");
rclone_latest_version_no = rclone_latest_version_no.trimmed();
// check if new version available and if yes display information
unsigned int result =
compareVersion(rclone_latest_version_no.toStdString(),
rclone_version_no.toStdString());
// latest version is greater than current
if (result == 1) {
QMessageBox::information(
this, "",
QString(
R"(<p>New rclone version is available</p>)"
R"(<p>You have: v)" +
rclone_version_no +
"<br />"
R"(New version: v)" +
rclone_latest_version_no +
"</p>"
R"(<p>Visit rclone <a href="https://rclone.org/downloads/">downloads</a> page to upgrade</p>)"));
};
};
};
};
};
/// check rclone browser version
// during first run the key might not exist yet
if (!(settings->contains("Settings/checkRcloneBrowserUpdates"))) {
// if checkRcloneBrowserUpdates does not exist create new key
settings->setValue("Settings/checkRcloneBrowserUpdates", true);
};
bool checkRcloneBrowserUpdates =
settings->value("Settings/checkRcloneBrowserUpdates").toBool();
// if check updates enabled in settings
if (checkRcloneBrowserUpdates) {
QString last_check;
QString current_date = QDate::currentDate().toString();
if (!(settings->contains("Settings/lastRcloneBrowserUpdateCheck"))) {
// if lastRcloneBrowserUpdateCheck does not exist create new key
settings->setValue("Settings/lastRcloneBrowserUpdateCheck",
current_date);
} else { // read last check date
last_check =
settings->value("Settings/lastRcloneBrowserUpdateCheck")
.toString();
};
// dont check if already checked today (once per day only)
if (!(last_check == current_date)) {
// remmber when last checked
settings->setValue("Settings/lastRcloneBrowserUpdateCheck",
current_date);
// get latest version available
QString url = "https://api.github.com/repos/kapitainsky/"
"rclonebrowser/releases/latest";
QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
QEventLoop event;
connect(response, SIGNAL(finished()), &event, SLOT(quit()));
event.exec();
QByteArray content = response->readAll();
QJsonParseError jsonError;
QJsonDocument document = QJsonDocument::fromJson(
content, &jsonError); // parse and capture the error flag
if (jsonError.error == QJsonParseError::NoError) {
if (document.object().contains("tag_name")) {
QJsonValue tag_name = document.object().value("tag_name");
QString rclone_browser_latest_version_no =
tag_name.toString(QString());
rclone_browser_latest_version_no =
rclone_browser_latest_version_no.trimmed();
// check if new version available and if yes display information
unsigned int result = compareVersion(
rclone_browser_latest_version_no.toStdString(),
RCLONE_BROWSER_VERSION);
// latest version is greater than current
if (result == 1) {
QMessageBox::information(
this, "",
QString(
R"(<p>New Rclone Browser version is available</p>)"
R"(<p>You have: v)" RCLONE_BROWSER_VERSION "<br />"
R"(New version: v)" +
rclone_browser_latest_version_no +
"</p>"
R"(<p>Visit <a href="https://github.com/kapitainsky/RcloneBrowser/releases/latest">releases</a> page to download</p>)"));
};
};
};
};
};
p->deleteLater();
});
UseRclonePassword(p);
p->start(GetRclone(),
QStringList() << "version"
<< "--ask-password=false",
QIODevice::ReadOnly);
}
void MainWindow::rcloneConfig() {
// for macOS and Linux we have to take care of possible spaces in rclone and
// rclone.conf paths by using "" around them
QString terminalRcloneCmd;
if (!GetRcloneConf().isEmpty()) {
terminalRcloneCmd = "\"" + GetRclone() + "\"" + " config" + " --config " +
"\"" + GetRcloneConf().at(1) + "\"";
} else {
terminalRcloneCmd = "\"" + GetRclone() + "\"" + " config";
}
#if defined(Q_OS_WIN32) && (QT_VERSION < QT_VERSION_CHECK(5, 7, 0))
QProcess::startDetached(GetRclone(), QStringList()
<< "config" << GetRcloneConf());
return;
#else
QProcess *p = new QProcess(this);
QObject::connect(p,
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
&QProcess::finished),
this, [=](int code, QProcess::ExitStatus) {
if (code == 0) {
emit rcloneListRemotes();
}
p->deleteLater();
});
#endif
#if defined(Q_OS_WIN32)
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
p->setCreateProcessArgumentsModifier(
[](QProcess::CreateProcessArguments *args) {
args->flags |= CREATE_NEW_CONSOLE;
args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
});
p->setProgram(GetRclone());
p->setArguments(QStringList() << "config" << GetRcloneConf());
#endif
#elif defined(Q_OS_MACOS)
auto tmp = new QFile("/tmp/rclone_config.command");
tmp->open(QIODevice::WriteOnly);
QTextStream(tmp) << "#!/bin/sh\n" << terminalRcloneCmd << "\n";
tmp->close();
tmp->setPermissions(QFileDevice::ReadUser | QFileDevice::WriteUser |
QFileDevice::ExeUser | QFileDevice::ReadGroup |
QFileDevice::ExeGroup | QFileDevice::ReadOther |
QFileDevice::ExeOther);
p->setProgram("open");
p->setArguments(QStringList() << tmp->fileName());
#else
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString terminal = env.value("TERMINAL");
if (terminal.isEmpty()) {
terminal = QStandardPaths::findExecutable("gnome-terminal");
if (terminal.isEmpty()) {
terminal = QStandardPaths::findExecutable("xfce4-terminal");
if (terminal.isEmpty()) {
terminal = QStandardPaths::findExecutable("xterm");
if (terminal.isEmpty()) {
terminal = QStandardPaths::findExecutable("x-terminal-emulator");
if (terminal.isEmpty()) {
terminal = QStandardPaths::findExecutable("konsole");
if (terminal.isEmpty()) {
QMessageBox::critical(this, "Error",
"Not sure how to launch terminal!\n"
"Please set path to terminal executable in "
"$TERMINAL environment variable.",
QMessageBox::Ok);
return;
}
}
}
}
}
}
p->setArguments(QStringList() << "-e" << terminalRcloneCmd);
p->setProgram(terminal);
#endif
#if !defined(Q_OS_WIN32) || (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0))
UseRclonePassword(p);
p->start(QIODevice::NotOpen);
#endif
}
void MainWindow::rcloneListRemotes() {
ui.remotes->clear();
QProcess *p = new QProcess();
QObject::connect(
p,
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
&QProcess::finished),
this, [=](int code, QProcess::ExitStatus) {
if (code == 0) {
QStyle *style = qApp->style();
QString bytes = p->readAllStandardOutput().trimmed();
QStringList items = bytes.split('\n');
auto settings = GetSettings();
bool darkModeIni = settings->value("Settings/darkModeIni").toBool();
QString iconSize = settings->value("Settings/iconSize").toString();
for (const QString &line : items) {
if (line.isEmpty()) {
continue;
}
QStringList parts = line.split(':');
if (parts.count() != 2) {
continue;
}
QString name = parts[0].trimmed();
QString type = parts[1].trimmed();
QString tooltip = type;
QString img_add = "";
int size;
// medium scale by default
double darkModeIconScale = 1.333;
double lightModeiconScale = 2;
// to avoid "variable not used" compiler error
if (darkModeIconScale == lightModeiconScale) {
};
// set icons scale based on iconSize value
if (iconSize == "small") {
lightModeiconScale = 1.5;
darkModeIconScale = 1;
}
if (iconSize == "medium") {
lightModeiconScale = 2;
darkModeIconScale = 1.333;
}
if (iconSize == "large") {
lightModeiconScale = 3;
darkModeIconScale = 2;
}
#if !defined(Q_OS_MACOS)
// _inv only for dark mode
// we use darkModeIni to apply mode active at startup
if (darkModeIni) {
img_add = "_inv";
} else {
img_add = "";
}
#if defined(Q_OS_WIN)
// on Windows dark theme changes PM_ListViewIconSize size
// so we have to adjust
if (darkModeIni) {
size = darkModeIconScale *
style->pixelMetric(QStyle::PM_ListViewIconSize);
} else {
size = lightModeiconScale *
style->pixelMetric(QStyle::PM_ListViewIconSize);
}
#else
// for Linux/BSD PM_ListViewIconSize stays the same
size = lightModeiconScale * style->pixelMetric(QStyle::PM_ListViewIconSize);
#endif
#else
QString sysInfo = QSysInfo::productVersion();
// dark mode on older macOS
if (sysInfo == "10.9" ||
sysInfo == "10.10" ||
sysInfo == "10.11" ||
sysInfo == "10.12" ||
sysInfo == "10.13") {
// on older macOS we also have to adjust icon size per mode
if (darkModeIni) {
size = darkModeIconScale * style->pixelMetric(QStyle::PM_ListViewIconSize);
img_add = "_inv";
} else {
size = lightModeiconScale * style->pixelMetric(QStyle::PM_ListViewIconSize);
img_add = "";
}
} else {
// for macOS > 10.13 native dark mode does not change IconSize base
size = 1.5 * lightModeiconScale * style->pixelMetric(QStyle::PM_ListViewIconSize);
}
#endif
ui.remotes->setIconSize(QSize(size, size));
QString path =
":/remotes/images/" + type.replace(' ', '_') + img_add + ".png";
QIcon icon(QFile(path).exists()
? path
: ":/remotes/images/unknown" + img_add + ".png");
QListWidgetItem *item = new QListWidgetItem(icon, name);
item->setData(Qt::UserRole, type);
item->setToolTip(tooltip);
ui.remotes->addItem(item);
}
} else {
if (p->error() != QProcess::FailedToStart) {
if (getConfigPassword(p)) {
rcloneListRemotes();
}
}
}
p->deleteLater();
});
UseRclonePassword(p);
p->start(GetRclone(),
QStringList() << "listremotes" << GetRcloneConf() << "--long"
<< "--ask-password=false",
QIODevice::ReadOnly);
}
bool MainWindow::getConfigPassword(QProcess *p) {
QString output = p->readAllStandardError().trimmed();
if (output.indexOf("RCLONE_CONFIG_PASS") > 0) {
bool ok;
QString password = QInputDialog::getText(
this, qApp->applicationDisplayName(),
"Enter password for .rclone.conf configuration file:",
QLineEdit::Password, QString(), &ok);
if (ok) {
SetRclonePassword(password);
return true;
}
} else if (output.indexOf("unknown command \"listremotes\"") > 0) {
QMessageBox::critical(this, qApp->applicationDisplayName(),
"It seems rclone version you are using is too "
"old.\nPlease upgrade to the latest version");
return false;
}
return false;
}
bool MainWindow::canClose() {
if (mJobCount == 0) {
return true;
}
bool wasVisible = isVisible();
ui.tabs->setCurrentIndex(1);
showNormal();
int button =
QMessageBox::question(this, "Rclone Browser",
QString("There are %1 job(s) running.\n"
"Do you want to stop them and quit?")
.arg(mJobCount),
QMessageBox::Yes | QMessageBox::No);
if (!wasVisible) {
hide();
}
if (button == QMessageBox::Yes) {
for (int i = 0; i < ui.jobs->count(); i++) {
QWidget *widget = ui.jobs->itemAt(i)->widget();
if (auto mount = qobject_cast<MountWidget *>(widget)) {
mount->cancel();
} else if (auto transfer = qobject_cast<JobWidget *>(widget)) {
transfer->cancel();
} else if (auto stream = qobject_cast<StreamWidget *>(widget)) {
stream->cancel();
}
}
return true;
}
return false;
}
void MainWindow::closeEvent(QCloseEvent *ev) {
if (mCloseToTray && isVisible()) {
#ifdef Q_OS_MACOS
osxHideDockIcon();
#endif
mSystemTray.show();
hide();
ev->ignore();
return;
}
if (canClose()) {
QApplication::quit();
} else {
ev->ignore();
}
}
void MainWindow::listTasks() {
ui.tasksListWidget->clear();
ListOfJobOptions *ljo = ListOfJobOptions::getInstance();
for (JobOptions *jo : ljo->getTasks()) {
JobOptionsListWidgetItem *item = new JobOptionsListWidgetItem(
jo,
jo->jobType == JobOptions::JobType::Download ? mDownloadIcon
: mUploadIcon,