ecoingui.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // ECOin - Copyright (c) - 2014/2024 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "ecoingui.h"
  3. #include "transactiontablemodel.h"
  4. #include "addressbookpage.h"
  5. #include "sendcoinsdialog.h"
  6. #include "signverifymessagedialog.h"
  7. #include "optionsdialog.h"
  8. #include "aboutdialog.h"
  9. #include "clientmodel.h"
  10. #include "walletmodel.h"
  11. #include "editaddressdialog.h"
  12. #include "optionsmodel.h"
  13. #include "transactiondescdialog.h"
  14. #include "addresstablemodel.h"
  15. #include "transactionview.h"
  16. #include "overviewpage.h"
  17. #include "ecoinunits.h"
  18. #include "guiconstants.h"
  19. #include "askpassphrasedialog.h"
  20. #include "notificator.h"
  21. #include "guiutil.h"
  22. #include "rpcconsole.h"
  23. #include "wallet.h"
  24. #ifdef Q_OS_MAC
  25. #include "macdockiconhandler.h"
  26. #endif
  27. #include <QApplication>
  28. #include <QMainWindow>
  29. #include <QMenuBar>
  30. #include <QMenu>
  31. #include <QIcon>
  32. #include <QTabWidget>
  33. #include <QVBoxLayout>
  34. #include <QToolBar>
  35. #include <QStatusBar>
  36. #include <QLabel>
  37. #include <QLineEdit>
  38. #include <QPushButton>
  39. #include <QLocale>
  40. #include <QMessageBox>
  41. #include <QProgressBar>
  42. #include <QStackedWidget>
  43. #include <QDateTime>
  44. #include <QMovie>
  45. #include <QFileDialog>
  46. #include <QDesktopServices>
  47. #include <QTimer>
  48. #include <QDragEnterEvent>
  49. #include <QUrl>
  50. #include <QStyle>
  51. #include <iostream>
  52. #include <QMimeData>
  53. #include <QStandardPaths>
  54. extern CWallet* pwalletMain;
  55. extern int64 nLastCoinStakeSearchInterval;
  56. extern unsigned int nStakeTargetSpacing;
  57. double GetPoSKernelPS();
  58. EcoinGUI::EcoinGUI(QWidget *parent):
  59. QMainWindow(parent),
  60. clientModel(0),
  61. walletModel(0),
  62. encryptWalletAction(0),
  63. changePassphraseAction(0),
  64. unlockWalletAction(0),
  65. trayIcon(0),
  66. notificator(0),
  67. rpcConsole(0)
  68. {
  69. resize(850, 550);
  70. setWindowTitle(tr("Ecoin") + " - " + tr("Wallet"));
  71. #ifndef Q_OS_MAC
  72. qApp->setWindowIcon(QIcon(":icons/ecoin"));
  73. setWindowIcon(QIcon(":icons/ecoin"));
  74. #else
  75. setUnifiedTitleAndToolBarOnMac(true);
  76. QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
  77. #endif
  78. // Accept D&D of URIs
  79. setAcceptDrops(true);
  80. // Create actions for the toolbar, menu bar and tray/dock icon
  81. createActions();
  82. // Create application menu bar
  83. createMenuBar();
  84. // Create the toolbars
  85. createToolBars();
  86. // Create the tray icon (or setup the dock icon)
  87. createTrayIcon();
  88. // Create tabs
  89. overviewPage = new OverviewPage();
  90. transactionsPage = new QWidget(this);
  91. QVBoxLayout *vbox = new QVBoxLayout();
  92. transactionView = new TransactionView(this);
  93. vbox->addWidget(transactionView);
  94. transactionsPage->setLayout(vbox);
  95. addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
  96. receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
  97. sendCoinsPage = new SendCoinsDialog(this);
  98. signVerifyMessageDialog = new SignVerifyMessageDialog(this);
  99. centralWidget = new QStackedWidget(this);
  100. centralWidget->addWidget(overviewPage);
  101. centralWidget->addWidget(transactionsPage);
  102. centralWidget->addWidget(addressBookPage);
  103. centralWidget->addWidget(receiveCoinsPage);
  104. centralWidget->addWidget(sendCoinsPage);
  105. setCentralWidget(centralWidget);
  106. // Create status bar
  107. statusBar();
  108. // Status bar notification icons
  109. QFrame *frameBlocks = new QFrame();
  110. frameBlocks->setContentsMargins(0,0,0,0);
  111. frameBlocks->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
  112. QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
  113. frameBlocksLayout->setContentsMargins(3,0,3,0);
  114. frameBlocksLayout->setSpacing(3);
  115. labelEncryptionIcon = new QLabel();
  116. labelStakingIcon = new QLabel();
  117. labelConnectionsIcon = new QLabel();
  118. labelBlocksIcon = new QLabel();
  119. frameBlocksLayout->addStretch();
  120. frameBlocksLayout->addWidget(labelStakingIcon);
  121. frameBlocksLayout->addStretch();
  122. frameBlocksLayout->addWidget(labelEncryptionIcon);
  123. frameBlocksLayout->addStretch();
  124. frameBlocksLayout->addWidget(labelConnectionsIcon);
  125. frameBlocksLayout->addStretch();
  126. frameBlocksLayout->addWidget(labelBlocksIcon);
  127. frameBlocksLayout->addStretch();
  128. if (GetBoolArg("-staking", true))
  129. {
  130. QTimer *timerStakingIcon = new QTimer(labelStakingIcon);
  131. connect(timerStakingIcon, SIGNAL(timeout()), this, SLOT(updateStakingIcon()));
  132. timerStakingIcon->start(30 * 1000);
  133. updateStakingIcon();
  134. }
  135. progressBarLabel = new QLabel();
  136. progressBarLabel->setVisible(false);
  137. progressBar = new QProgressBar();
  138. progressBar->setAlignment(Qt::AlignCenter);
  139. progressBar->setVisible(false);
  140. QString curStyle = qApp->style()->metaObject()->className();
  141. if(curStyle == "QWindowsStyle" || curStyle == "QWindowsXPStyle")
  142. {
  143. progressBar->setStyleSheet("QProgressBar { background-color: #e8e8e8; border: 1px solid grey; border-radius: 7px; padding: 1px; text-align: center; } QProgressBar::chunk { background: QLinearGradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #FF8000, stop: 1 orange); border-radius: 7px; margin: 0px; }");
  144. }
  145. statusBar()->addWidget(progressBarLabel);
  146. statusBar()->addWidget(progressBar);
  147. statusBar()->addPermanentWidget(frameBlocks);
  148. syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
  149. // Clicking on a transaction on the overview page simply sends you to transaction history page
  150. connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
  151. connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), transactionView, SLOT(focusTransaction(QModelIndex)));
  152. // Double-clicking on a transaction on the transaction history page shows details
  153. connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
  154. rpcConsole = new RPCConsole(this);
  155. connect(openRPCConsoleAction, SIGNAL(triggered()), rpcConsole, SLOT(show()));
  156. // Clicking on "Verify Message" in the address book sends you to the verify message tab
  157. connect(addressBookPage, SIGNAL(verifyMessage(QString)), this, SLOT(gotoVerifyMessageTab(QString)));
  158. // Clicking on "Sign Message" in the receive coins page sends you to the sign message tab
  159. connect(receiveCoinsPage, SIGNAL(signMessage(QString)), this, SLOT(gotoSignMessageTab(QString)));
  160. gotoOverviewPage();
  161. }
  162. EcoinGUI::~EcoinGUI()
  163. {
  164. if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
  165. trayIcon->hide();
  166. #ifdef Q_OS_MAC
  167. delete appMenuBar;
  168. #endif
  169. }
  170. void EcoinGUI::createActions()
  171. {
  172. QActionGroup *tabGroup = new QActionGroup(this);
  173. overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
  174. overviewAction->setToolTip(tr("Show general overview of wallet"));
  175. overviewAction->setCheckable(true);
  176. overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
  177. tabGroup->addAction(overviewAction);
  178. sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
  179. sendCoinsAction->setToolTip(tr("Send coins to a Ecoin address"));
  180. sendCoinsAction->setCheckable(true);
  181. sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
  182. tabGroup->addAction(sendCoinsAction);
  183. receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
  184. receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
  185. receiveCoinsAction->setCheckable(true);
  186. receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
  187. tabGroup->addAction(receiveCoinsAction);
  188. historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
  189. historyAction->setToolTip(tr("Browse transaction history"));
  190. historyAction->setCheckable(true);
  191. historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
  192. tabGroup->addAction(historyAction);
  193. addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
  194. addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
  195. addressBookAction->setCheckable(true);
  196. addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
  197. tabGroup->addAction(addressBookAction);
  198. connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
  199. connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
  200. connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
  201. connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
  202. connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
  203. connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
  204. connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
  205. connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
  206. connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
  207. connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
  208. quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
  209. quitAction->setToolTip(tr("Quit application"));
  210. quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
  211. quitAction->setMenuRole(QAction::QuitRole);
  212. aboutAction = new QAction(QIcon(":/icons/ecoin"), tr("&About Ecoin"), this);
  213. aboutAction->setToolTip(tr("Show information about Ecoin"));
  214. aboutAction->setMenuRole(QAction::AboutRole);
  215. optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
  216. optionsAction->setToolTip(tr("Modify configuration options for Ecoin"));
  217. optionsAction->setMenuRole(QAction::PreferencesRole);
  218. toggleHideAction = new QAction(QIcon(":/icons/ecoin"), tr("&Show / Hide"), this);
  219. encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet..."), this);
  220. encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
  221. encryptWalletAction->setCheckable(true);
  222. backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet..."), this);
  223. backupWalletAction->setToolTip(tr("Backup wallet to another location"));
  224. changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase..."), this);
  225. changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
  226. unlockWalletAction = new QAction(QIcon(":/icons/lock_open"), tr("&Unlock Wallet..."), this);
  227. unlockWalletAction->setToolTip(tr("Unlock wallet for staking"));
  228. signMessageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message..."), this);
  229. verifyMessageAction = new QAction(QIcon(":/icons/transaction_0"), tr("&Verify message..."), this);
  230. exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
  231. exportAction->setToolTip(tr("Export the data in the current tab to a file"));
  232. openRPCConsoleAction = new QAction(QIcon(":/icons/debugwindow"), tr("&Debug window"), this);
  233. openRPCConsoleAction->setToolTip(tr("Open debugging and diagnostic console"));
  234. connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
  235. connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
  236. connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
  237. connect(toggleHideAction, SIGNAL(triggered()), this, SLOT(toggleHidden()));
  238. connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
  239. connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
  240. connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
  241. connect(unlockWalletAction, SIGNAL(triggered()), this, SLOT(unlockWallet()));
  242. connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
  243. connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));
  244. }
  245. void EcoinGUI::createMenuBar()
  246. {
  247. #ifdef Q_OS_MAC
  248. // Create a decoupled menu bar on Mac which stays even if the window is closed
  249. appMenuBar = new QMenuBar();
  250. #else
  251. // Get the main window's menu bar on other platforms
  252. appMenuBar = menuBar();
  253. #endif
  254. // Configure the menus
  255. QMenu *file = appMenuBar->addMenu(tr("&File"));
  256. file->addAction(backupWalletAction);
  257. file->addAction(exportAction);
  258. file->addAction(signMessageAction);
  259. file->addAction(verifyMessageAction);
  260. file->addSeparator();
  261. file->addAction(quitAction);
  262. QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
  263. settings->addAction(encryptWalletAction);
  264. settings->addAction(changePassphraseAction);
  265. settings->addAction(unlockWalletAction);
  266. settings->addSeparator();
  267. settings->addAction(optionsAction);
  268. QMenu *help = appMenuBar->addMenu(tr("&Help"));
  269. help->addAction(openRPCConsoleAction);
  270. help->addSeparator();
  271. help->addAction(aboutAction);
  272. }
  273. void EcoinGUI::createToolBars()
  274. {
  275. QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
  276. toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  277. toolbar->addAction(overviewAction);
  278. toolbar->addAction(sendCoinsAction);
  279. toolbar->addAction(receiveCoinsAction);
  280. toolbar->addAction(historyAction);
  281. toolbar->addAction(addressBookAction);
  282. QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
  283. toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
  284. toolbar2->addAction(exportAction);
  285. }
  286. void EcoinGUI::setClientModel(ClientModel *clientModel)
  287. {
  288. this->clientModel = clientModel;
  289. if(clientModel)
  290. {
  291. // Replace some strings and icons, when using the testnet
  292. if(clientModel->isTestNet())
  293. {
  294. setWindowTitle(windowTitle() + QString(" ") + tr("[testnet]"));
  295. #ifndef Q_OS_MAC
  296. qApp->setWindowIcon(QIcon(":icons/ecoin_testnet"));
  297. setWindowIcon(QIcon(":icons/ecoin_testnet"));
  298. #else
  299. MacDockIconHandler::instance()->setIcon(QIcon(":icons/ecoin_testnet"));
  300. #endif
  301. if(trayIcon)
  302. {
  303. trayIcon->setToolTip(tr("Ecoin client") + QString(" ") + tr("[testnet]"));
  304. trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
  305. toggleHideAction->setIcon(QIcon(":/icons/toolbar_testnet"));
  306. }
  307. aboutAction->setIcon(QIcon(":/icons/toolbar_testnet"));
  308. }
  309. // Keep up to date with client
  310. setNumConnections(clientModel->getNumConnections());
  311. connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
  312. setNumBlocks(clientModel->getNumBlocks(), clientModel->getNumBlocksOfPeers());
  313. connect(clientModel, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
  314. // Report errors from network/worker thread
  315. connect(clientModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
  316. rpcConsole->setClientModel(clientModel);
  317. addressBookPage->setOptionsModel(clientModel->getOptionsModel());
  318. receiveCoinsPage->setOptionsModel(clientModel->getOptionsModel());
  319. }
  320. }
  321. void EcoinGUI::setWalletModel(WalletModel *walletModel)
  322. {
  323. this->walletModel = walletModel;
  324. if(walletModel)
  325. {
  326. // Report errors from wallet thread
  327. connect(walletModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
  328. // Put transaction list in tabs
  329. transactionView->setModel(walletModel);
  330. overviewPage->setModel(walletModel);
  331. addressBookPage->setModel(walletModel->getAddressTableModel());
  332. receiveCoinsPage->setModel(walletModel->getAddressTableModel());
  333. sendCoinsPage->setModel(walletModel);
  334. signVerifyMessageDialog->setModel(walletModel);
  335. setEncryptionStatus(walletModel->getEncryptionStatus());
  336. connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
  337. // Balloon pop-up for new transaction
  338. connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
  339. this, SLOT(incomingTransaction(QModelIndex,int,int)));
  340. // Ask for passphrase if needed
  341. connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
  342. }
  343. }
  344. void EcoinGUI::createTrayIcon()
  345. {
  346. QMenu *trayIconMenu;
  347. #ifndef Q_OS_MAC
  348. trayIcon = new QSystemTrayIcon(this);
  349. trayIconMenu = new QMenu(this);
  350. trayIcon->setContextMenu(trayIconMenu);
  351. trayIcon->setToolTip(tr("Ecoin client"));
  352. trayIcon->setIcon(QIcon(":/icons/toolbar"));
  353. connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
  354. this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
  355. trayIcon->show();
  356. #else
  357. // Note: On Mac, the dock icon is used to provide the tray's functionality.
  358. MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
  359. trayIconMenu = dockIconHandler->dockMenu();
  360. #endif
  361. // Configuration of the tray icon (or dock icon) icon menu
  362. trayIconMenu->addAction(toggleHideAction);
  363. trayIconMenu->addSeparator();
  364. trayIconMenu->addAction(sendCoinsAction);
  365. trayIconMenu->addAction(receiveCoinsAction);
  366. trayIconMenu->addSeparator();
  367. trayIconMenu->addAction(signMessageAction);
  368. trayIconMenu->addAction(verifyMessageAction);
  369. trayIconMenu->addSeparator();
  370. trayIconMenu->addAction(optionsAction);
  371. trayIconMenu->addAction(openRPCConsoleAction);
  372. #ifndef Q_OS_MAC // This is built-in on Mac
  373. trayIconMenu->addSeparator();
  374. trayIconMenu->addAction(quitAction);
  375. #endif
  376. notificator = new Notificator(qApp->applicationName(), trayIcon);
  377. }
  378. #ifndef Q_OS_MAC
  379. void EcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
  380. {
  381. if(reason == QSystemTrayIcon::Trigger)
  382. {
  383. // Click on system tray icon triggers show/hide of the main window
  384. toggleHideAction->trigger();
  385. }
  386. }
  387. #endif
  388. void EcoinGUI::optionsClicked()
  389. {
  390. if(!clientModel || !clientModel->getOptionsModel())
  391. return;
  392. OptionsDialog dlg;
  393. dlg.setModel(clientModel->getOptionsModel());
  394. dlg.exec();
  395. }
  396. void EcoinGUI::aboutClicked()
  397. {
  398. AboutDialog dlg;
  399. dlg.setModel(clientModel);
  400. dlg.exec();
  401. }
  402. void EcoinGUI::setNumConnections(int count)
  403. {
  404. QString icon;
  405. switch(count)
  406. {
  407. case 0: icon = ":/icons/connect_0"; break;
  408. case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
  409. case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
  410. case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
  411. default: icon = ":/icons/connect_4"; break;
  412. }
  413. labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
  414. labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Ecoin network", "", count));
  415. }
  416. void EcoinGUI::setNumBlocks(int count, int nTotalBlocks)
  417. {
  418. // don't show / hide progress bar and its label if we have no connection to the network
  419. if (!clientModel || clientModel->getNumConnections() == 0)
  420. {
  421. progressBarLabel->setVisible(false);
  422. progressBar->setVisible(false);
  423. return;
  424. }
  425. QString strStatusBarWarnings = clientModel->getStatusBarWarnings();
  426. QString tooltip;
  427. if(count < nTotalBlocks)
  428. {
  429. int nRemainingBlocks = nTotalBlocks - count;
  430. float nPercentageDone = count / (nTotalBlocks * 0.01f);
  431. if (strStatusBarWarnings.isEmpty())
  432. {
  433. progressBarLabel->setText(tr("Synchronizing with network..."));
  434. progressBarLabel->setVisible(true);
  435. progressBar->setFormat(tr("~%n block(s) remaining", "", nRemainingBlocks));
  436. progressBar->setMaximum(nTotalBlocks);
  437. progressBar->setValue(count);
  438. progressBar->setVisible(true);
  439. }
  440. tooltip = tr("Downloaded %1 of %2 blocks of transaction history (%3% done).").arg(count).arg(nTotalBlocks).arg(nPercentageDone, 0, 'f', 2);
  441. }
  442. else
  443. {
  444. if (strStatusBarWarnings.isEmpty())
  445. progressBarLabel->setVisible(false);
  446. progressBar->setVisible(false);
  447. tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
  448. }
  449. // Override progressBarLabel text and hide progress bar, when we have warnings to display
  450. if (!strStatusBarWarnings.isEmpty())
  451. {
  452. progressBarLabel->setText(strStatusBarWarnings);
  453. progressBarLabel->setVisible(true);
  454. progressBar->setVisible(false);
  455. }
  456. QDateTime lastBlockDate = clientModel->getLastBlockDate();
  457. int secs = lastBlockDate.secsTo(QDateTime::currentDateTime());
  458. QString text;
  459. // Represent time from last generated block in human readable text
  460. if(secs <= 0)
  461. {
  462. // Fully up to date. Leave text empty.
  463. }
  464. else if(secs < 60)
  465. {
  466. text = tr("%n second(s) ago","",secs);
  467. }
  468. else if(secs < 60*60)
  469. {
  470. text = tr("%n minute(s) ago","",secs/60);
  471. }
  472. else if(secs < 24*60*60)
  473. {
  474. text = tr("%n hour(s) ago","",secs/(60*60));
  475. }
  476. else
  477. {
  478. text = tr("%n day(s) ago","",secs/(60*60*24));
  479. }
  480. // Set icon state: spinning if catching up, tick otherwise
  481. if(secs < 90*60 && count >= nTotalBlocks)
  482. {
  483. tooltip = tr("Up to date") + QString(".<br>") + tooltip;
  484. labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
  485. overviewPage->showOutOfSyncWarning(false);
  486. }
  487. else
  488. {
  489. tooltip = tr("Catching up...") + QString("<br>") + tooltip;
  490. labelBlocksIcon->setMovie(syncIconMovie);
  491. syncIconMovie->start();
  492. overviewPage->showOutOfSyncWarning(true);
  493. }
  494. if(!text.isEmpty())
  495. {
  496. tooltip += QString("<br>");
  497. tooltip += tr("Last received block was generated %1.").arg(text);
  498. }
  499. // Don't word-wrap this (fixed-width) tooltip
  500. tooltip = QString("<nobr>") + tooltip + QString("</nobr>");
  501. labelBlocksIcon->setToolTip(tooltip);
  502. progressBarLabel->setToolTip(tooltip);
  503. progressBar->setToolTip(tooltip);
  504. }
  505. void EcoinGUI::error(const QString &title, const QString &message, bool modal)
  506. {
  507. // Report errors from network/worker thread
  508. if(modal)
  509. {
  510. QMessageBox::critical(this, title, message, QMessageBox::Ok, QMessageBox::Ok);
  511. } else {
  512. notificator->notify(Notificator::Critical, title, message);
  513. }
  514. }
  515. void EcoinGUI::changeEvent(QEvent *e)
  516. {
  517. QMainWindow::changeEvent(e);
  518. #ifndef Q_OS_MAC // Ignored on Mac
  519. if(e->type() == QEvent::WindowStateChange)
  520. {
  521. if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
  522. {
  523. QWindowStateChangeEvent *wsevt = static_cast<QWindowStateChangeEvent*>(e);
  524. if(!(wsevt->oldState() & Qt::WindowMinimized) && isMinimized())
  525. {
  526. QTimer::singleShot(0, this, SLOT(hide()));
  527. e->ignore();
  528. }
  529. }
  530. }
  531. #endif
  532. }
  533. void EcoinGUI::closeEvent(QCloseEvent *event)
  534. {
  535. if(clientModel)
  536. {
  537. #ifndef Q_OS_MAC // Ignored on Mac
  538. if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
  539. !clientModel->getOptionsModel()->getMinimizeOnClose())
  540. {
  541. qApp->quit();
  542. }
  543. #endif
  544. }
  545. QMainWindow::closeEvent(event);
  546. }
  547. void EcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
  548. {
  549. QString strMessage =
  550. tr("This transaction is over the size limit. You can still send it for a fee of %1, "
  551. "which goes to the nodes that process your transaction and helps to support the network. "
  552. "Do you want to pay the fee?").arg(
  553. EcoinUnits::formatWithUnit(EcoinUnits::ECO, nFeeRequired));
  554. QMessageBox::StandardButton retval = QMessageBox::question(
  555. this, tr("Confirm transaction fee"), strMessage,
  556. QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
  557. *payFee = (retval == QMessageBox::Yes);
  558. }
  559. void EcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
  560. {
  561. if(!walletModel || !clientModel)
  562. return;
  563. TransactionTableModel *ttm = walletModel->getTransactionTableModel();
  564. qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
  565. .data(Qt::EditRole).toULongLong();
  566. if(!clientModel->inInitialBlockDownload())
  567. {
  568. // On new transaction, make an info balloon
  569. // Unless the initial block download is in progress, to prevent balloon-spam
  570. QString date = ttm->index(start, TransactionTableModel::Date, parent)
  571. .data().toString();
  572. QString type = ttm->index(start, TransactionTableModel::Type, parent)
  573. .data().toString();
  574. QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
  575. .data().toString();
  576. QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
  577. TransactionTableModel::ToAddress, parent)
  578. .data(Qt::DecorationRole));
  579. notificator->notify(Notificator::Information,
  580. (amount)<0 ? tr("Sent transaction") :
  581. tr("Incoming transaction"),
  582. tr("Date: %1\n"
  583. "Amount: %2\n"
  584. "Type: %3\n"
  585. "Address: %4\n")
  586. .arg(date)
  587. .arg(EcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
  588. .arg(type)
  589. .arg(address), icon);
  590. }
  591. }
  592. void EcoinGUI::gotoOverviewPage()
  593. {
  594. overviewAction->setChecked(true);
  595. centralWidget->setCurrentWidget(overviewPage);
  596. exportAction->setEnabled(false);
  597. disconnect(exportAction, SIGNAL(triggered()), 0, 0);
  598. }
  599. void EcoinGUI::gotoHistoryPage()
  600. {
  601. historyAction->setChecked(true);
  602. centralWidget->setCurrentWidget(transactionsPage);
  603. exportAction->setEnabled(true);
  604. disconnect(exportAction, SIGNAL(triggered()), 0, 0);
  605. connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
  606. }
  607. void EcoinGUI::gotoAddressBookPage()
  608. {
  609. addressBookAction->setChecked(true);
  610. centralWidget->setCurrentWidget(addressBookPage);
  611. exportAction->setEnabled(true);
  612. disconnect(exportAction, SIGNAL(triggered()), 0, 0);
  613. connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
  614. }
  615. void EcoinGUI::gotoReceiveCoinsPage()
  616. {
  617. receiveCoinsAction->setChecked(true);
  618. centralWidget->setCurrentWidget(receiveCoinsPage);
  619. exportAction->setEnabled(true);
  620. disconnect(exportAction, SIGNAL(triggered()), 0, 0);
  621. connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
  622. }
  623. void EcoinGUI::gotoSendCoinsPage()
  624. {
  625. sendCoinsAction->setChecked(true);
  626. centralWidget->setCurrentWidget(sendCoinsPage);
  627. exportAction->setEnabled(false);
  628. disconnect(exportAction, SIGNAL(triggered()), 0, 0);
  629. }
  630. void EcoinGUI::gotoSignMessageTab(QString addr)
  631. {
  632. // call show() in showTab_SM()
  633. signVerifyMessageDialog->showTab_SM(true);
  634. if(!addr.isEmpty())
  635. signVerifyMessageDialog->setAddress_SM(addr);
  636. }
  637. void EcoinGUI::gotoVerifyMessageTab(QString addr)
  638. {
  639. // call show() in showTab_VM()
  640. signVerifyMessageDialog->showTab_VM(true);
  641. if(!addr.isEmpty())
  642. signVerifyMessageDialog->setAddress_VM(addr);
  643. }
  644. void EcoinGUI::dragEnterEvent(QDragEnterEvent *event)
  645. {
  646. // Accept only URIs
  647. if(event->mimeData()->hasUrls())
  648. event->acceptProposedAction();
  649. }
  650. void EcoinGUI::dropEvent(QDropEvent *event)
  651. {
  652. if(event->mimeData()->hasUrls())
  653. {
  654. int nValidUrisFound = 0;
  655. QList<QUrl> uris = event->mimeData()->urls();
  656. foreach(const QUrl &uri, uris)
  657. {
  658. if (sendCoinsPage->handleURI(uri.toString()))
  659. nValidUrisFound++;
  660. }
  661. // if valid URIs were found
  662. if (nValidUrisFound)
  663. gotoSendCoinsPage();
  664. else
  665. notificator->notify(Notificator::Warning, tr("URI handling"), tr("URI can not be parsed! This can be caused by an invalid Ecoin address or malformed URI parameters."));
  666. }
  667. event->acceptProposedAction();
  668. }
  669. void EcoinGUI::handleURI(QString strURI)
  670. {
  671. // URI has to be valid
  672. if (sendCoinsPage->handleURI(strURI))
  673. {
  674. showNormalIfMinimized();
  675. gotoSendCoinsPage();
  676. }
  677. else
  678. notificator->notify(Notificator::Warning, tr("URI handling"), tr("URI can not be parsed! This can be caused by an invalid Ecoin address or malformed URI parameters."));
  679. }
  680. void EcoinGUI::setEncryptionStatus(int status)
  681. {
  682. switch(status)
  683. {
  684. case WalletModel::Unencrypted:
  685. labelEncryptionIcon->hide();
  686. encryptWalletAction->setChecked(false);
  687. changePassphraseAction->setEnabled(false);
  688. unlockWalletAction->setEnabled(false);
  689. encryptWalletAction->setEnabled(true);
  690. break;
  691. case WalletModel::Unlocked:
  692. labelEncryptionIcon->show();
  693. labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
  694. labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
  695. encryptWalletAction->setChecked(true);
  696. changePassphraseAction->setEnabled(true);
  697. unlockWalletAction->setEnabled(false);
  698. encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
  699. break;
  700. case WalletModel::Locked:
  701. labelEncryptionIcon->show();
  702. labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
  703. labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
  704. encryptWalletAction->setChecked(true);
  705. changePassphraseAction->setEnabled(true);
  706. unlockWalletAction->setEnabled(true);
  707. encryptWalletAction->setEnabled(false);
  708. break;
  709. }
  710. }
  711. void EcoinGUI::encryptWallet(bool status)
  712. {
  713. if(!walletModel)
  714. return;
  715. AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
  716. AskPassphraseDialog::Decrypt, this);
  717. dlg.setModel(walletModel);
  718. dlg.exec();
  719. setEncryptionStatus(walletModel->getEncryptionStatus());
  720. }
  721. void EcoinGUI::backupWallet()
  722. {
  723. QString saveDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
  724. QString filename = QFileDialog::getSaveFileName(this, tr("Backup Wallet"), saveDir, tr("Wallet Data (*.dat)"));
  725. if(!filename.isEmpty()) {
  726. if(!walletModel->backupWallet(filename)) {
  727. QMessageBox::warning(this, tr("Backup Failed"), tr("There was an error trying to save the wallet data to the new location."));
  728. }
  729. }
  730. }
  731. void EcoinGUI::changePassphrase()
  732. {
  733. AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
  734. dlg.setModel(walletModel);
  735. dlg.exec();
  736. }
  737. void EcoinGUI::unlockWallet()
  738. {
  739. if(!walletModel)
  740. return;
  741. // Unlock wallet when requested by wallet model
  742. if(walletModel->getEncryptionStatus() == WalletModel::Locked)
  743. {
  744. AskPassphraseDialog::Mode mode = sender() == unlockWalletAction ?
  745. AskPassphraseDialog::UnlockStaking : AskPassphraseDialog::Unlock;
  746. AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
  747. dlg.setModel(walletModel);
  748. dlg.exec();
  749. }
  750. }
  751. void EcoinGUI::showNormalIfMinimized(bool fToggleHidden)
  752. {
  753. if (isHidden())
  754. {
  755. show();
  756. activateWindow();
  757. }
  758. else if (isMinimized())
  759. {
  760. showNormal();
  761. activateWindow();
  762. }
  763. else if (GUIUtil::isObscured(this))
  764. {
  765. raise();
  766. activateWindow();
  767. }
  768. else if(fToggleHidden)
  769. hide();
  770. }
  771. void EcoinGUI::toggleHidden()
  772. {
  773. showNormalIfMinimized(true);
  774. }
  775. void EcoinGUI::updateStakingIcon()
  776. {
  777. uint64_t nMinWeight = 0, nMaxWeight = 0, nWeight = 0;
  778. if (pwalletMain)
  779. pwalletMain->GetStakeWeight(*pwalletMain, nMinWeight, nMaxWeight, nWeight);
  780. if (nLastCoinStakeSearchInterval && nWeight)
  781. {
  782. uint64_t nNetworkWeight = GetPoSKernelPS();
  783. unsigned nEstimateTime = nStakeTargetSpacing * nNetworkWeight / nWeight;
  784. QString text;
  785. if (nEstimateTime < 60)
  786. {
  787. text = tr("%n second(s)", "", nEstimateTime);
  788. }
  789. else if (nEstimateTime < 60*60)
  790. {
  791. text = tr("%n minute(s)", "", nEstimateTime/60);
  792. }
  793. else if (nEstimateTime < 24*60*60)
  794. {
  795. text = tr("%n hour(s)", "", nEstimateTime/(60*60));
  796. }
  797. else
  798. {
  799. text = tr("%n day(s)", "", nEstimateTime/(60*60*24));
  800. }
  801. labelStakingIcon->setPixmap(QIcon(":/icons/staking_on").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
  802. labelStakingIcon->setToolTip(tr("Staking.<br>Your weight is %1<br>Network weight is %2<br>Expected time to earn reward is %3").arg(nWeight).arg(nNetworkWeight).arg(text));
  803. }
  804. else
  805. {
  806. labelStakingIcon->setPixmap(QIcon(":/icons/staking_off").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
  807. if (pwalletMain && pwalletMain->IsLocked())
  808. labelStakingIcon->setToolTip(tr("Not staking because wallet is locked"));
  809. else if (vNodes.empty())
  810. labelStakingIcon->setToolTip(tr("Not staking because wallet is offline"));
  811. else if (IsInitialBlockDownload())
  812. labelStakingIcon->setToolTip(tr("Not staking because wallet is syncing"));
  813. else if (!nWeight)
  814. labelStakingIcon->setToolTip(tr("Not staking because you don't have mature coins"));
  815. else
  816. labelStakingIcon->setToolTip(tr("Not staking"));
  817. }
  818. }