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