-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathWindowManager.cpp
More file actions
821 lines (678 loc) · 28.8 KB
/
WindowManager.cpp
File metadata and controls
821 lines (678 loc) · 28.8 KB
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
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: The Monero Project
#include "WindowManager.h"
#include <QDialogButtonBox>
#include <QInputDialog>
#include <QMessageBox>
#include <QWindow>
#include "Application.h"
#include "constants.h"
#include "MainWindow.h"
#include "dialog/DocsDialog.h"
#include "dialog/PasswordDialog.h"
#include "dialog/SplashDialog.h"
#include "dialog/TorInfoDialog.h"
#include "libwalletqt/WalletManager.h"
#include "libwalletqt/Wallet.h"
#include "utils/Icons.h"
#include "utils/NetworkManager.h"
#include "utils/os/tails.h"
#include "utils/os/whonix.h"
#include "utils/TorManager.h"
#include "utils/WebsocketNotifier.h"
#include "utils/AppData.h"
WindowManager::WindowManager(QObject *parent)
: QObject(parent)
{
m_walletManager = WalletManager::instance();
m_splashDialog = new SplashDialog();
m_cleanupThread = new QThread(this);
connect(m_walletManager, &WalletManager::walletOpened, this, &WindowManager::onWalletOpened);
connect(m_walletManager, &WalletManager::walletCreated, this, &WindowManager::onWalletCreated);
connect(m_walletManager, &WalletManager::deviceButtonRequest, this, &WindowManager::onDeviceButtonRequest);
connect(m_walletManager, &WalletManager::deviceButtonPressed, this, &WindowManager::onDeviceButtonPressed);
connect(m_walletManager, &WalletManager::deviceError, this, &WindowManager::onDeviceError);
connect(m_walletManager, &WalletManager::walletPassphraseNeeded, this, &WindowManager::onWalletPassphraseNeeded);
connect(qApp, SIGNAL(anotherInstanceStarted()), this, SLOT(raise()));
connect(qApp, &QGuiApplication::lastWindowClosed, this, &WindowManager::quitAfterLastWindow);
m_tray = new QSystemTrayIcon(icons()->icon("appicons/64x64.png"));
m_tray->setToolTip("Feather Wallet");
this->buildTrayMenu();
m_tray->setVisible(conf()->get(Config::showTrayIcon).toBool());
this->initSkins();
this->patchMacStylesheet();
this->showCrashLogs();
if (!conf()->get(Config::firstRun).toBool() || TailsOS::detect() || WhonixOS::detect()) {
this->onInitialNetworkConfigured();
}
this->startupWarning();
if (!this->autoOpenWallet()) {
this->initWizard();
}
}
QPointer<WindowManager> WindowManager::m_instance(nullptr);
void WindowManager::setEventFilter(EventFilter *ef) {
eventFilter = ef;
}
WindowManager::~WindowManager() {
qDebug() << "~WindowManager";
m_cleanupThread->quit();
m_cleanupThread->wait();
qDebug() << "WindowManager: cleanup thread done" << QThread::currentThreadId();
}
// ######################## APPLICATION LIFECYCLE ########################
void WindowManager::quitAfterLastWindow() {
if (m_windows.length() > 0 || m_openingWallet) {
return;
}
qDebug() << "No wizards in progress and no wallets open, quitting application.";
this->close();
}
void WindowManager::close() {
qDebug() << Q_FUNC_INFO << QThread::currentThreadId();
for (const auto &window: m_windows) {
window->close();
}
if (m_splashDialog) {
m_splashDialog->deleteLater();
}
if (m_tray) {
m_tray->deleteLater();
}
if (m_wizard) {
m_wizard->deleteLater();
}
if (m_docsDialog) {
m_docsDialog->deleteLater();
}
torManager()->stop();
deleteLater();
qDebug() << "Calling QApplication::quit()";
QApplication::quit();
}
void WindowManager::closeWindow(MainWindow *window) {
qDebug() << "WindowManager: closing Window";
m_windows.removeOne(window);
// Move Wallet to a different thread for cleanup, so it doesn't block GUI thread
window->m_wallet->moveToThread(m_cleanupThread);
m_cleanupThread->start();
window->m_wallet->deleteLater();
window->deleteLater();
}
void WindowManager::restartApplication(const QString &binaryFilename) {
QProcess::startDetached(binaryFilename, qApp->arguments());
this->close();
}
void WindowManager::startupWarning() {
// Stagenet / Testnet
auto worthlessWarning = QString("Feather wallet is currently running in %1 mode. This is meant "
"for developers only. Your coins are WORTHLESS.");
if (constants::networkType == NetworkType::STAGENET && conf()->get(Config::warnOnStagenet).toBool()) {
this->showWarningMessageBox("Warning", worthlessWarning.arg("stagenet"));
conf()->set(Config::warnOnStagenet, false);
}
else if (constants::networkType == NetworkType::TESTNET && conf()->get(Config::warnOnTestnet).toBool()){
this->showWarningMessageBox("Warning", worthlessWarning.arg("testnet"));
conf()->set(Config::warnOnTestnet, false);
}
}
void WindowManager::showWarningMessageBox(const QString &title, const QString &message) {
QMessageBox msgBox;
msgBox.setWindowIcon(icons()->icon("appicons/64x64.png"));
msgBox.setIcon(QMessageBox::Warning);
msgBox.setText(message);
msgBox.setWindowTitle(title);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
void WindowManager::raise() {
if (!m_windows.isEmpty()) {
m_windows.first()->bringToFront();
}
else if (m_wizard) {
m_wizard->show();
m_wizard->raise();
m_wizard->activateWindow();
}
else {
// This shouldn't happen
this->close();
}
}
// ######################## SETTINGS ########################
void WindowManager::showSettings(Nodes *nodes, QWidget *parent, bool showProxyTab) {
Settings settings{nodes, parent};
connect(&settings, &Settings::preferredFiatCurrencyChanged, [this]{
for (const auto &window : m_windows) {
window->onPreferredFiatCurrencyChanged();
}
emit preferredFiatCurrencyChanged();
});
connect(&settings, &Settings::skinChanged, this, &WindowManager::onChangeTheme);
connect(&settings, &Settings::updateBalance, this, &WindowManager::updateBalance);
connect(&settings, &Settings::proxySettingsChanged, this, &WindowManager::onProxySettingsChanged);
connect(&settings, &Settings::websocketStatusChanged, this, &WindowManager::onWebsocketStatusChanged);
connect(&settings, &Settings::offlineMode, this, &WindowManager::offlineMode);
connect(&settings, &Settings::manualFeeSelectionEnabled, this, &WindowManager::manualFeeSelectionEnabled);
connect(&settings, &Settings::subtractFeeFromAmountEnabled, this, &WindowManager::subtractFeeFromAmountEnabled);
connect(&settings, &Settings::hideUpdateNotifications, [this](bool hidden){
for (const auto &window : m_windows) {
window->onHideUpdateNotifications(hidden);
}
});
connect(&settings, &Settings::showTrayIcon, [this](bool visible) {
m_tray->setVisible(visible);
});
connect(&settings, &Settings::pluginConfigured, [this](const QString &id) {
emit pluginConfigured(id);
});
if (showProxyTab) {
settings.showNetworkProxyTab();
}
settings.exec();
}
// ######################## DOCS ########################
void WindowManager::showDocs(QObject *parent, const QString &doc, bool modal) {
if (!m_docsDialog) {
m_docsDialog = new DocsDialog();
}
m_docsDialog->setModal(modal);
m_docsDialog->show();
if (m_docsDialog->isMinimized()) {
m_docsDialog->showNormal();
}
m_docsDialog->raise();
m_docsDialog->activateWindow();
QWindow *window = Utils::windowForQObject(parent);
if (window != nullptr) {
QPoint centerOfParent = window->frameGeometry().center();
m_docsDialog->move(centerOfParent.x() - m_docsDialog->width() / 2, centerOfParent.y() - m_docsDialog->height() / 2);
} else {
qDebug() << "Unable to center docs window";
}
if (!doc.isEmpty()) {
m_docsDialog->showDoc(doc);
}
}
void WindowManager::setDocsHighlight(const QString &highlight) {
if (!m_docsDialog) {
return;
}
m_docsDialog->updateHighlights(highlight);
}
// ######################## WALLET OPEN ########################
void WindowManager::tryOpenWallet(const QString &path, const QString &password) {
// Path : path to .keys file
QString absolutePath = path;
if (absolutePath.startsWith("~")) {
absolutePath.replace(0, 1, QDir::homePath());
}
// If the wallet is already open, just bring window to front
for (const auto &window : m_windows) {
if (absolutePath == window->walletKeysPath() || absolutePath == window->walletCachePath()) {
window->bringToFront();
return;
}
}
if (!Utils::fileExists(path)) {
this->handleWalletError({nullptr, Utils::ERROR, "Unable to open wallet", QString("Wallet not found: %1").arg(path)});
return;
}
m_openingWallet = true;
m_walletManager->openWalletAsync(path, password, constants::networkType, constants::kdfRounds, Utils::ringDatabasePath());
}
void WindowManager::onWalletOpened(Wallet *wallet) {
if (!wallet) {
this->handleWalletError({nullptr, Utils::ERROR, "Unable to open wallet", "This should never happen. If you encounter this error, please report it to the developers.", {}, "report_an_issue"});
return;
}
auto status = wallet->status();
if (status != Wallet::Status_Ok) {
QString errMsg = wallet->errorString();
wallet->deleteLater();
if (status == Wallet::Status_BadPassword) {
// Don't show incorrect password when we try with empty password for the first time
bool showIncorrectPassword = m_openWalletTriedOnce;
m_openWalletTriedOnce = true;
this->onWalletOpenPasswordRequired(showIncorrectPassword, wallet->keysPath());
return; // Do not remove this
}
else if (errMsg == QString("basic_string::_M_replace_aux") || errMsg == QString("std::bad_alloc") || errMsg == "invalid signature") {
qCritical() << errMsg;
WalletManager::clearWalletCache(wallet->cachePath());
errMsg = QString("%1\n\nWallet cache is unusable, moving it.").arg(errMsg);
this->handleWalletError({nullptr, Utils::ERROR, "Unable to open wallet", errMsg, {"Try opening this wallet again.", "If this keeps happening, please file a bug report."}, "report_an_issue"});
}
else if (errMsg.startsWith("failed to read file")) {
#if defined(Q_OS_MACOS)
Utils::Message message{nullptr, Utils::ERROR, "Unable to open wallet", errMsg, {"You may need to give Feather permission to access the folder", "In the System Settings app, go to 'Privacy & Security' -> 'Files & Folders'"}};
#else
Utils::Message message{nullptr, Utils::ERROR, "Unable to open wallet", errMsg, {"You may need to change the permissions on the wallet directory."}};
#endif
this->handleWalletError(message);
}
else {
Utils::Message message{nullptr, Utils::ERROR, "Unable to open wallet"};
this->handleDeviceError(errMsg, message);
this->handleWalletError(message);
}
m_openingWallet = false;
return;
}
this->onInitialNetworkConfigured();
// Create new mainwindow with wallet
m_splashDialog->hide();
m_openWalletTriedOnce = false;
auto *window = new MainWindow(this, wallet);
m_windows.append(window);
this->buildTrayMenu();
m_openingWallet = false;
}
void WindowManager::onWalletOpenPasswordRequired(bool invalidPassword, const QString &path) {
QFileInfo fileInfo(path);
PasswordDialog dialog{fileInfo.fileName(), invalidPassword};
switch (dialog.exec()) {
case QDialog::Rejected:
{
m_openWalletTriedOnce = false;
m_openingWallet = false;
if (m_wizard) {
m_wizard->show();
} else {
this->showWizard(WalletWizard::Page_Menu);
}
return;
}
}
this->tryOpenWallet(path, dialog.password);
}
bool WindowManager::autoOpenWallet() {
QString autoPath = conf()->get(Config::autoOpenWalletPath).toString();
if (!autoPath.isEmpty() && autoPath.startsWith(QString::number(constants::networkType))) {
autoPath.remove(0, 1);
}
if (!autoPath.isEmpty() && Utils::fileExists(autoPath)) {
this->tryOpenWallet(autoPath, ""); // TODO: get password from --password
return true;
}
return false;
}
// ######################## WALLET CREATION ########################
void WindowManager::tryCreateWallet(Seed seed, const QString &path, const QString &password, const QString &seedLanguage,
const QString &seedOffset, const QString &subaddressLookahead, bool newWallet) {
if (Utils::fileExists(path)) {
this->handleWalletError({nullptr, Utils::ERROR, "Failed to create wallet", QString("File already exists: %1").arg(path)});
return;
}
if (seed.mnemonic.isEmpty()) {
this->handleWalletError({nullptr, Utils::ERROR, "Failed to create wallet", "Mnemonic seed is empty"});
return;
}
Wallet *wallet = nullptr;
if (seed.type == Seed::Type::POLYSEED || seed.type == Seed::Type::TEVADOR) {
wallet = m_walletManager->createDeterministicWalletFromSpendKey(path, password, seed.language, constants::networkType, seed.spendKey, seed.restoreHeight, constants::kdfRounds, seedOffset, subaddressLookahead);
}
else if (seed.type == Seed::Type::MONERO) {
wallet = m_walletManager->recoveryWallet(path, password, seed.mnemonic.join(" "), seedOffset, constants::networkType, seed.restoreHeight, constants::kdfRounds);
}
if (!wallet) {
this->handleWalletError({nullptr, Utils::ERROR, "Failed to create wallet"});
return;
}
wallet->setCacheAttribute("feather.seed", seed.mnemonic.join(" "));
wallet->setCacheAttribute("feather.seedoffset", seedOffset);
// Store attributes now, so we don't lose them on crash / forced exit
wallet->store();
if (newWallet) {
wallet->setNewWallet();
}
this->onWalletOpened(wallet);
}
void WindowManager::tryCreateWalletFromDevice(const QString &path, const QString &password, const QString &deviceName, int restoreHeight, const QString &subaddressLookahead)
{
if (Utils::fileExists(path)) {
this->handleWalletError({nullptr, Utils::ERROR, "Failed to create wallet from device", QString("File already exists: %1").arg(path)});
return;
}
m_openingWallet = true;
m_walletManager->createWalletFromDeviceAsync(path, password, constants::networkType, deviceName, restoreHeight, subaddressLookahead);
}
void WindowManager::tryCreateWalletFromKeys(const QString &path, const QString &password, const QString &address,
const QString &viewkey, const QString &spendkey, quint64 restoreHeight, const QString &subaddressLookahead) {
if (Utils::fileExists(path)) {
this->handleWalletError({nullptr, Utils::ERROR, "Failed to create wallet", QString("File already exists: %1").arg(path)});
return;
}
Wallet *wallet;
if (address.isEmpty() && viewkey.isEmpty() && !spendkey.isEmpty()) {
wallet = m_walletManager->createDeterministicWalletFromSpendKey(path, password, constants::seedLanguage, constants::networkType, spendkey, restoreHeight, constants::kdfRounds, "", subaddressLookahead);
}
else {
if (!spendkey.isEmpty() && !WalletManager::keyValid(spendkey, address, false, constants::networkType)) {
this->handleWalletError({nullptr, Utils::ERROR, "Failed to create wallet", "Invalid spendkey provided"});
return;
}
if (!WalletManager::addressValid(address, constants::networkType)) {
this->handleWalletError({nullptr, Utils::ERROR, "Failed to create wallet", "Invalid address provided"});
return;
}
if (!WalletManager::keyValid(viewkey, address, true, constants::networkType)) {
this->handleWalletError({nullptr, Utils::ERROR, "Failed to create wallet", "Invalid viewkey provided"});
return;
}
wallet = m_walletManager->createWalletFromKeys(path, password, constants::seedLanguage, constants::networkType, address, viewkey, spendkey, restoreHeight, constants::kdfRounds, subaddressLookahead);
}
m_openingWallet = true;
this->onWalletOpened(wallet);
}
void WindowManager::onWalletCreated(Wallet *wallet) {
// Currently only called when a wallet is created from device.
auto state = wallet->status();
if (state != Wallet::Status_Ok) {
QString error = wallet->errorString();
Utils::Message message{m_wizard, Utils::ERROR, "Failed to create wallet from device"};
this->handleDeviceError(error, message);
this->showWizard(WalletWizard::Page_Menu);
Utils::showMsg(message);
m_splashDialog->hide();
m_openingWallet = false;
return;
}
this->onWalletOpened(wallet);
}
// ######################## ERROR HANDLING ########################
void WindowManager::handleWalletError(const Utils::Message &message) {
Utils::showMsg(message);
this->initWizard();
}
void WindowManager::handleDeviceError(const QString &error, Utils::Message &msg) {
msg.description = error;
// Ledger
if (error.contains("No device found")) {
msg.description = "No Ledger device found.";
msg.helpItems = {"Make sure the Monero app is open on the device.", "If the problem persists, try restarting Feather."};
msg.doc = "create_wallet_hardware_device";
}
else if (error.contains("Unable to open device")) {
msg.description = "Unable to open device.";
msg.helpItems = {"The device might be in use by a different application."};
#if defined(Q_OS_LINUX)
msg.helpItems.append("On Linux you may need to follow the instructions in the link below before the device can be opened:\n"
"https://support.ledger.com/article/115005165269-zd");
msg.link = "https://support.ledger.com/article/115005165269-zd";
#endif
}
// Trezor
else if (error.contains("Unable to claim libusb device")) {
msg.description = "Unable to claim Trezor device";
msg.helpItems = {"Please make sure the device is not used by another program, like Trezor Suite or trezord, and try again."};
}
else if (error.contains("Cannot get a device address")) {
msg.description = "Cannot get a device address";
msg.helpItems = {"Reattach the Trezor device and try again"};
}
else if (error.contains("Could not connect to the device Trezor") || error.contains("Device connect failed")) {
msg.description = "Could not connect to the Trezor device";
msg.helpItems = {"Make sure the device is connected to your computer and unlocked."};
#if defined(Q_OS_LINUX)
msg.helpItems.append("On Linux you may need to follow the instructions in the link below before the device can be opened:\n"
"https://trezor.io/guides/trezorctl/udev-rules");
msg.link = "https://trezor.io/guides/trezorctl/udev-rules";
#endif
}
else if (error.contains("Failed to acquire device")) {
msg.helpItems = {"Make sure Trezor Suite and trezord are closed."};
}
else if (error.contains("SW_CLIENT_NOT_SUPPORTED")) {
msg.helpItems = {"Upgrade your Ledger device firmware to the latest version using Ledger Live.\n"
"Then upgrade the Monero app for the Ledger device to the latest version."};
}
else if (error.contains("Wrong Device Status")) {
msg.helpItems = {"The device may need to be unlocked."};
}
else if (error.contains("Wrong Channel")) {
msg.helpItems = {"Restart the hardware device and try again."};
}
else {
msg.doc = "report_an_issue";
}
}
void WindowManager::showCrashLogs() {
QString crashLogPath{Config::defaultConfigDir().path() + "/crash_report.txt"};
QFile crashLogFile{crashLogPath};
if (!crashLogFile.exists()) {
return;
}
bool r = crashLogFile.open(QIODevice::ReadOnly);
if (!r) {
qWarning() << "Unable to open crash log file: " << crashLogPath;
return;
}
QTextStream log(&crashLogFile);
QString logString = log.readAll();
crashLogFile.close();
bool renamed = false;
for (int i = 1; i < 999; i++) {
QString name{QString("/crash_report_%1.txt").arg(QString::number(i))};
if (crashLogFile.rename(Config::defaultConfigDir().path() + name)) {
renamed = true;
break;
}
}
if (!renamed) {
crashLogFile.remove();
}
QDialog dialog(nullptr);
dialog.setWindowTitle("Crash report");
QVBoxLayout layout;
QLabel msg{"Feather encountered an unrecoverable error.\n\nPlease send a copy of these logs to the developers. Logs are not automatically reported.\n"};
QTextEdit logs;
logs.setText(logString);
layout.addWidget(&msg);
layout.addWidget(&logs);
QDialogButtonBox buttons(QDialogButtonBox::Ok);
layout.addWidget(&buttons);
dialog.setLayout(&layout);
QObject::connect(&buttons, &QDialogButtonBox::accepted, [&dialog]{
dialog.close();
});
dialog.exec();
exit(0);
}
// ######################## DEVICE ########################
void WindowManager::onDeviceButtonRequest(quint64 code) {
QString message;
switch (code) {
case 1: // Trezor
message = "Action required on device: enter your PIN to continue.";
break;
case 8: // Trezor
message = "Action required on device: Export watch-only credentials to open the wallet.";
break;
case 19: // Trezor
message = "Action required on device: Enter passphrase to open the wallet.";
break;
default:
message = "Action required on device: Export the view key to open the wallet.";
}
m_splashDialog->setMessage(message);
m_splashDialog->setIcon(QPixmap(":/assets/images/key.png"));
m_splashDialog->show();
m_splashDialog->setEnabled(true);
}
void WindowManager::onDeviceButtonPressed() {
m_splashDialog->hide();
}
void WindowManager::onDeviceError(const QString &errorMessage, quint64 errorCode) {
// TODO: when does this get called?
qCritical() << Q_FUNC_INFO << errorMessage;
}
void WindowManager::onWalletPassphraseNeeded(bool on_device) {
auto button = QMessageBox::question(nullptr, "Wallet Passphrase Needed", "Enter passphrase on hardware wallet?\n\n"
"It is recommended to enter passphrase on "
"the hardware wallet for better security.",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (button == QMessageBox::Yes) {
m_walletManager->onPassphraseEntered("", true, false);
return;
}
bool ok;
QString passphrase = QInputDialog::getText(nullptr, "Wallet Passphrase Needed", "Enter passphrase:", QLineEdit::EchoMode::Password, "", &ok);
m_walletManager->onPassphraseEntered(passphrase, false, false);
}
// ######################## TRAY ########################
void WindowManager::buildTrayMenu() {
QMenu *menu;
if (!m_tray->contextMenu()) {
menu = new QMenu();
m_tray->setContextMenu(menu);
} else {
menu = m_tray->contextMenu();
menu->clear();
}
for (const auto &window : m_windows) {
QString name = window->walletName();
QMenu *submenu = menu->addMenu(name);
submenu->addAction("Show/Hide", window, &MainWindow::showOrHide);
submenu->addAction("Close", window, &MainWindow::close);
}
menu->addSeparator();
menu->addAction("Exit Feather", this, &WindowManager::close);
}
void WindowManager::notify(const QString &title, const QString &message, int duration) {
if (!m_tray || !QSystemTrayIcon::supportsMessages()) {
return;
}
if (conf()->get(Config::hideNotifications).toBool()) {
return;
}
m_tray->showMessage(title, message, icons()->icon("appicons/64x64.png"), duration);
}
// ######################## NETWORKING ########################
void WindowManager::onInitialNetworkConfigured() {
if (!m_initialNetworkConfigured) {
m_initialNetworkConfigured = true;
appData();
this->onProxySettingsChanged();
}
}
void WindowManager::onProxySettingsChanged() {
if (Utils::isTorsocks()) {
return;
}
// Will kill the process if necessary
torManager()->init();
torManager()->start();
QNetworkProxy proxy{QNetworkProxy::NoProxy};
if (conf()->get(Config::proxy).toInt() != Config::Proxy::None) {
QString host = conf()->get(Config::socks5Host).toString();
quint16 port = conf()->get(Config::socks5Port).toString().toUShort();
if (conf()->get(Config::proxy).toInt() == Config::Proxy::Tor && (!torManager()->isLocalTor() || torManager()->isAlreadyRunning())) {
host = torManager()->featherTorHost;
port = torManager()->featherTorPort;
}
proxy = QNetworkProxy{QNetworkProxy::Socks5Proxy, host, port};
getNetworkSocks5()->setProxy(proxy);
}
qWarning() << "Proxy: " << proxy.hostName() << " " << proxy.port();
// Switch websocket to new proxy and update URL
websocketNotifier()->websocketClient->stop();
websocketNotifier()->websocketClient->webSocket->setProxy(proxy);
websocketNotifier()->websocketClient->nextWebsocketUrl();
websocketNotifier()->websocketClient->restart();
emit proxySettingsChanged();
}
void WindowManager::onWebsocketStatusChanged(bool enabled) {
emit websocketStatusChanged(enabled);
}
// ######################## WIZARD ########################
WalletWizard* WindowManager::createWizard(WalletWizard::Page startPage) {
auto *wizard = new WalletWizard;
connect(wizard, &WalletWizard::initialNetworkConfigured, this, &WindowManager::onInitialNetworkConfigured);
connect(wizard, &WalletWizard::showSettings, [this, wizard]{
this->showSettings(nullptr, wizard);
});
connect(wizard, &WalletWizard::openWallet, this, &WindowManager::tryOpenWallet);
connect(wizard, &WalletWizard::createWallet, this, &WindowManager::tryCreateWallet);
connect(wizard, &WalletWizard::createWalletFromKeys, this, &WindowManager::tryCreateWalletFromKeys);
connect(wizard, &WalletWizard::createWalletFromDevice, this, &WindowManager::tryCreateWalletFromDevice);
return wizard;
}
void WindowManager::initWizard() {
auto startPage = WalletWizard::Page_Menu;
if (conf()->get(Config::firstRun).toBool() && !(TailsOS::detect() || WhonixOS::detect())) {
startPage = WalletWizard::Page_Network;
}
this->showWizard(startPage);
}
void WindowManager::showWizard(WalletWizard::Page startPage) {
if (!m_wizard) {
m_wizard = this->createWizard(startPage);
}
m_wizard->resetFields();
m_wizard->setStartId(startPage);
m_wizard->restart();
m_wizard->setEnabled(true);
m_wizard->show();
}
void WindowManager::wizardOpenWallet() {
this->showWizard(WalletWizard::Page_OpenWallet);
}
// ######################## SKINS ########################
void WindowManager::initSkins() {
m_skins.insert("Native", "");
QString qdarkstyle = this->loadStylesheet(":qdarkstyle/style.qss");
if (!qdarkstyle.isEmpty())
m_skins.insert("QDarkStyle", qdarkstyle);
QString breeze_dark = this->loadStylesheet(":/dark.qss");
if (!breeze_dark.isEmpty())
m_skins.insert("Breeze/Dark", breeze_dark);
QString breeze_light = this->loadStylesheet(":/light.qss");
if (!breeze_light.isEmpty())
m_skins.insert("Breeze/Light", breeze_light);
QString skin = conf()->get(Config::skin).toString();
qApp->setStyleSheet(m_skins[skin]);
}
QString WindowManager::loadStylesheet(const QString &resource) {
QFile f(resource);
if (!f.exists()) {
printf("Unable to set stylesheet, file not found\n");
f.close();
return "";
}
f.open(QFile::ReadOnly | QFile::Text);
QTextStream ts(&f);
QString data = ts.readAll();
f.close();
return data;
}
void WindowManager::onChangeTheme(const QString &skinName) {
if (!m_skins.contains(skinName)) {
qWarning() << QString("No such skin %1").arg(skinName);
return;
}
conf()->set(Config::skin, skinName);
qApp->setStyleSheet(m_skins[skinName]);
qDebug() << QString("Skin changed to %1").arg(skinName);
this->patchMacStylesheet();
for (const auto &window : m_windows) {
window->skinChanged(skinName);
}
}
void WindowManager::patchMacStylesheet() {
#if defined(Q_OS_MACOS)
QString styleSheet = qApp->styleSheet();
auto patch = Utils::fileOpenQRC(":assets/macStylesheet.patch");
auto patch_text = Utils::barrayToString(patch);
styleSheet += patch_text;
qApp->setStyleSheet(styleSheet);
#endif
}
WindowManager* WindowManager::instance()
{
if (!m_instance) {
m_instance = new WindowManager(QCoreApplication::instance());
}
return m_instance;
}