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