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