PPCoin: Qt: display coinstake as single 'mint by stake' transaction
[novacoin.git] / src / qt / bitcoingui.cpp
1 /*
2  * Qt4 ppcoin GUI.
3  *
4  * W.J. van der Laan 2011-2012
5  * The Bitcoin Developers 2011-2012
6  * The PPCoin Developers 2011-2012
7  */
8 #include "bitcoingui.h"
9 #include "transactiontablemodel.h"
10 #include "addressbookpage.h"
11 #include "sendcoinsdialog.h"
12 #include "messagepage.h"
13 #include "optionsdialog.h"
14 #include "aboutdialog.h"
15 #include "clientmodel.h"
16 #include "walletmodel.h"
17 #include "editaddressdialog.h"
18 #include "optionsmodel.h"
19 #include "transactiondescdialog.h"
20 #include "addresstablemodel.h"
21 #include "transactionview.h"
22 #include "overviewpage.h"
23 #include "bitcoinunits.h"
24 #include "guiconstants.h"
25 #include "askpassphrasedialog.h"
26 #include "notificator.h"
27 #include "guiutil.h"
28
29 #ifdef Q_WS_MAC
30 #include "macdockiconhandler.h"
31 #endif
32
33 #include <QApplication>
34 #include <QMainWindow>
35 #include <QMenuBar>
36 #include <QMenu>
37 #include <QIcon>
38 #include <QTabWidget>
39 #include <QVBoxLayout>
40 #include <QToolBar>
41 #include <QStatusBar>
42 #include <QLabel>
43 #include <QLineEdit>
44 #include <QPushButton>
45 #include <QLocale>
46 #include <QMessageBox>
47 #include <QProgressBar>
48 #include <QStackedWidget>
49 #include <QDateTime>
50 #include <QMovie>
51 #include <QFileDialog>
52 #include <QDesktopServices>
53 #include <QTimer>
54
55 #include <QDragEnterEvent>
56 #include <QUrl>
57
58 #include <iostream>
59
60 BitcoinGUI::BitcoinGUI(QWidget *parent):
61     QMainWindow(parent),
62     clientModel(0),
63     walletModel(0),
64     encryptWalletAction(0),
65     changePassphraseAction(0),
66     aboutQtAction(0),
67     trayIcon(0),
68     notificator(0)
69 {
70     resize(850, 550);
71     setWindowTitle(tr("PPCoin Wallet"));
72 #ifndef Q_WS_MAC
73     setWindowIcon(QIcon(":icons/bitcoin"));
74 #else
75     setUnifiedTitleAndToolBarOnMac(true);
76     QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
77 #endif
78     // Accept D&D of URIs
79     setAcceptDrops(true);
80
81     // Create actions for the toolbar, menu bar and tray/dock icon
82     createActions();
83
84     // Create application menu bar
85     createMenuBar();
86
87     // Create the toolbars
88     createToolBars();
89
90     // Create the tray icon (or setup the dock icon)
91     createTrayIcon();
92
93     // Create tabs
94     overviewPage = new OverviewPage();
95
96     transactionsPage = new QWidget(this);
97     QVBoxLayout *vbox = new QVBoxLayout();
98     transactionView = new TransactionView(this);
99     vbox->addWidget(transactionView);
100     transactionsPage->setLayout(vbox);
101
102     addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
103
104     receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
105
106     sendCoinsPage = new SendCoinsDialog(this);
107
108     messagePage = new MessagePage(this);
109
110     centralWidget = new QStackedWidget(this);
111     centralWidget->addWidget(overviewPage);
112     centralWidget->addWidget(transactionsPage);
113     centralWidget->addWidget(addressBookPage);
114     centralWidget->addWidget(receiveCoinsPage);
115     centralWidget->addWidget(sendCoinsPage);
116 #ifdef FIRST_CLASS_MESSAGING
117     centralWidget->addWidget(messagePage);
118 #endif
119     setCentralWidget(centralWidget);
120
121     // Create status bar
122     statusBar();
123
124     // Status bar notification icons
125     QFrame *frameBlocks = new QFrame();
126     frameBlocks->setContentsMargins(0,0,0,0);
127     frameBlocks->setMinimumWidth(56);
128     frameBlocks->setMaximumWidth(56);
129     QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
130     frameBlocksLayout->setContentsMargins(3,0,3,0);
131     frameBlocksLayout->setSpacing(3);
132     labelEncryptionIcon = new QLabel();
133     labelConnectionsIcon = new QLabel();
134     labelBlocksIcon = new QLabel();
135     frameBlocksLayout->addStretch();
136     frameBlocksLayout->addWidget(labelEncryptionIcon);
137     frameBlocksLayout->addStretch();
138     frameBlocksLayout->addWidget(labelConnectionsIcon);
139     frameBlocksLayout->addStretch();
140     frameBlocksLayout->addWidget(labelBlocksIcon);
141     frameBlocksLayout->addStretch();
142
143     // Progress bar and label for blocks download
144     progressBarLabel = new QLabel();
145     progressBarLabel->setVisible(false);
146     progressBar = new QProgressBar();
147     progressBar->setAlignment(Qt::AlignCenter);
148     progressBar->setVisible(false);
149
150     statusBar()->addWidget(progressBarLabel);
151     statusBar()->addWidget(progressBar);
152     statusBar()->addPermanentWidget(frameBlocks);
153
154     syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
155
156     // Clicking on a transaction on the overview page simply sends you to transaction history page
157     connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
158
159     // Doubleclicking on a transaction on the transaction history page shows details
160     connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
161
162     gotoOverviewPage();
163 }
164
165 BitcoinGUI::~BitcoinGUI()
166 {
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     toggleHideAction = new QAction(QIcon(":/icons/bitcoin"), tr("Show/Hide &Bitcoin"), this);
240     toggleHideAction->setToolTip(tr("Show or hide 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(toggleHideAction, SIGNAL(triggered()), this, SLOT(toggleHidden()));
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, bool)), this, SLOT(error(QString,QString,bool)));
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,bool)), this, SLOT(error(QString,QString,bool)));
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     trayIconMenu = dockIconHandler->dockMenu();
388 #endif
389
390     // Configuration of the tray icon (or dock icon) icon menu
391     trayIconMenu->addAction(toggleHideAction);
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 "show/hide bitcoin"
415         toggleHideAction->trigger();
416     }
417 }
418 #endif
419
420 void BitcoinGUI::toggleHidden()
421 {
422     // activateWindow() (sometimes) helps with keyboard focus on Windows
423     if (isHidden())
424     {
425         show();
426         activateWindow();
427     }
428     else if (isMinimized())
429     {
430         showNormal();
431         activateWindow();
432     }
433     else if (GUIUtil::isObscured(this))
434     {
435         raise();
436         activateWindow();
437     }
438     else
439         hide();
440 }
441
442 void BitcoinGUI::optionsClicked()
443 {
444     if(!clientModel || !clientModel->getOptionsModel())
445         return;
446     OptionsDialog dlg;
447     dlg.setModel(clientModel->getOptionsModel());
448     dlg.exec();
449 }
450
451 void BitcoinGUI::aboutClicked()
452 {
453     AboutDialog dlg;
454     dlg.setModel(clientModel);
455     dlg.exec();
456 }
457
458 void BitcoinGUI::setNumConnections(int count)
459 {
460     QString icon;
461     switch(count)
462     {
463     case 0: icon = ":/icons/connect_0"; break;
464     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
465     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
466     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
467     default: icon = ":/icons/connect_4"; break;
468     }
469     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
470     labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
471 }
472
473 void BitcoinGUI::setNumBlocks(int count)
474 {
475     // don't show / hide progressBar and it's label if we have no connection(s) to the network
476     if (!clientModel || clientModel->getNumConnections() == 0)
477     {
478         progressBarLabel->setVisible(false);
479         progressBar->setVisible(false);
480
481         return;
482     }
483
484     int nTotalBlocks = clientModel->getNumBlocksOfPeers();
485     QString tooltip;
486
487     if(count < nTotalBlocks)
488     {
489         int nRemainingBlocks = nTotalBlocks - count;
490         float nPercentageDone = count / (nTotalBlocks * 0.01f);
491
492         if (clientModel->getStatusBarWarnings() == "")
493         {
494             progressBarLabel->setText(tr("Synchronizing with network..."));
495             progressBarLabel->setVisible(true);
496             progressBar->setFormat(tr("~%n block(s) remaining", "", nRemainingBlocks));
497             progressBar->setMaximum(nTotalBlocks);
498             progressBar->setValue(count);
499             progressBar->setVisible(true);
500         }
501         else
502         {
503             progressBarLabel->setText(clientModel->getStatusBarWarnings());
504             progressBarLabel->setVisible(true);
505             progressBar->setVisible(false);
506         }
507         tooltip = tr("Downloaded %1 of %2 blocks of transaction history (%3% done).").arg(count).arg(nTotalBlocks).arg(nPercentageDone, 0, 'f', 2);
508     }
509     else
510     {
511         if (clientModel->getStatusBarWarnings() == "")
512             progressBarLabel->setVisible(false);
513         else
514         {
515             progressBarLabel->setText(clientModel->getStatusBarWarnings());
516             progressBarLabel->setVisible(true);
517         }
518         progressBar->setVisible(false);
519         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
520     }
521
522     QDateTime now = QDateTime::currentDateTime();
523     QDateTime lastBlockDate = clientModel->getLastBlockDate();
524     int secs = lastBlockDate.secsTo(now);
525     QString text;
526
527     // Represent time from last generated block in human readable text
528     if(secs <= 0)
529     {
530         // Fully up to date. Leave text empty.
531     }
532     else if(secs < 60)
533     {
534         text = tr("%n second(s) ago","",secs);
535     }
536     else if(secs < 60*60)
537     {
538         text = tr("%n minute(s) ago","",secs/60);
539     }
540     else if(secs < 24*60*60)
541     {
542         text = tr("%n hour(s) ago","",secs/(60*60));
543     }
544     else
545     {
546         text = tr("%n day(s) ago","",secs/(60*60*24));
547     }
548
549     // Set icon state: spinning if catching up, tick otherwise
550     if(secs < 90*60 && count >= nTotalBlocks)
551     {
552         tooltip = tr("Up to date") + QString(".\n") + tooltip;
553         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
554     }
555     else
556     {
557         tooltip = tr("Catching up...") + QString("\n") + tooltip;
558         labelBlocksIcon->setMovie(syncIconMovie);
559         syncIconMovie->start();
560     }
561
562     if(!text.isEmpty())
563     {
564         tooltip += QString("\n");
565         tooltip += tr("Last received block was generated %1.").arg(text);
566     }
567
568     labelBlocksIcon->setToolTip(tooltip);
569     progressBarLabel->setToolTip(tooltip);
570     progressBar->setToolTip(tooltip);
571 }
572
573 void BitcoinGUI::error(const QString &title, const QString &message, bool modal)
574 {
575     // Report errors from network/worker thread
576     if(modal)
577     {
578         QMessageBox::critical(this, title, message, QMessageBox::Ok, QMessageBox::Ok);
579     } else {
580         notificator->notify(Notificator::Critical, title, message);
581     }
582 }
583
584 void BitcoinGUI::changeEvent(QEvent *e)
585 {
586     QMainWindow::changeEvent(e);
587 #ifndef Q_WS_MAC // Ignored on Mac
588     if(e->type() == QEvent::WindowStateChange)
589     {
590         if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
591         {
592             QWindowStateChangeEvent *wsevt = static_cast<QWindowStateChangeEvent*>(e);
593             if(!(wsevt->oldState() & Qt::WindowMinimized) && isMinimized())
594             {
595                 QTimer::singleShot(0, this, SLOT(hide()));
596                 e->ignore();
597             }
598         }
599     }
600 #endif
601 }
602
603 void BitcoinGUI::closeEvent(QCloseEvent *event)
604 {
605     if(clientModel)
606     {
607 #ifndef Q_WS_MAC // Ignored on Mac
608         if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
609            !clientModel->getOptionsModel()->getMinimizeOnClose())
610         {
611             qApp->quit();
612         }
613 #endif
614     }
615     QMainWindow::closeEvent(event);
616 }
617
618 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
619 {
620     QString strMessage =
621         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
622           "which goes to the nodes that process your transaction and helps to support the network.  "
623           "Do you want to pay the fee?").arg(
624                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
625     QMessageBox::StandardButton retval = QMessageBox::question(
626           this, tr("Sending..."), strMessage,
627           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
628     *payFee = (retval == QMessageBox::Yes);
629 }
630
631 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
632 {
633     if(!walletModel || !clientModel)
634         return;
635     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
636     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
637                     .data(Qt::EditRole).toULongLong();
638     if(!clientModel->inInitialBlockDownload())
639     {
640         // On new transaction, make an info balloon
641         // Unless the initial block download is in progress, to prevent balloon-spam
642         QString date = ttm->index(start, TransactionTableModel::Date, parent)
643                         .data().toString();
644         QString type = ttm->index(start, TransactionTableModel::Type, parent)
645                         .data().toString();
646         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
647                         .data().toString();
648         QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
649                             TransactionTableModel::ToAddress, parent)
650                         .data(Qt::DecorationRole));
651
652         notificator->notify(Notificator::Information,
653                             (amount)<0 ? tr("Sent transaction") :
654                                          tr("Incoming transaction"),
655                               tr("Date: %1\n"
656                                  "Amount: %2\n"
657                                  "Type: %3\n"
658                                  "Address: %4\n")
659                               .arg(date)
660                               .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
661                               .arg(type)
662                               .arg(address), icon);
663     }
664 }
665
666 void BitcoinGUI::gotoOverviewPage()
667 {
668     overviewAction->setChecked(true);
669     centralWidget->setCurrentWidget(overviewPage);
670
671     exportAction->setEnabled(false);
672     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
673 }
674
675 void BitcoinGUI::gotoHistoryPage()
676 {
677     historyAction->setChecked(true);
678     centralWidget->setCurrentWidget(transactionsPage);
679
680     exportAction->setEnabled(true);
681     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
682     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
683 }
684
685 void BitcoinGUI::gotoAddressBookPage()
686 {
687     addressBookAction->setChecked(true);
688     centralWidget->setCurrentWidget(addressBookPage);
689
690     exportAction->setEnabled(true);
691     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
692     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
693 }
694
695 void BitcoinGUI::gotoReceiveCoinsPage()
696 {
697     receiveCoinsAction->setChecked(true);
698     centralWidget->setCurrentWidget(receiveCoinsPage);
699
700     exportAction->setEnabled(true);
701     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
702     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
703 }
704
705 void BitcoinGUI::gotoSendCoinsPage()
706 {
707     sendCoinsAction->setChecked(true);
708     centralWidget->setCurrentWidget(sendCoinsPage);
709
710     exportAction->setEnabled(false);
711     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
712 }
713
714 void BitcoinGUI::gotoMessagePage()
715 {
716 #ifdef FIRST_CLASS_MESSAGING
717     messageAction->setChecked(true);
718     centralWidget->setCurrentWidget(messagePage);
719
720     exportAction->setEnabled(false);
721     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
722 #else
723     messagePage->show();
724     messagePage->setFocus();
725 #endif
726 }
727
728 void BitcoinGUI::gotoMessagePage(QString addr)
729 {
730     gotoMessagePage();
731     messagePage->setAddress(addr);
732 }
733
734 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
735 {
736     // Accept only URIs
737     if(event->mimeData()->hasUrls())
738         event->acceptProposedAction();
739 }
740
741 void BitcoinGUI::dropEvent(QDropEvent *event)
742 {
743     if(event->mimeData()->hasUrls())
744     {
745         gotoSendCoinsPage();
746         QList<QUrl> uris = event->mimeData()->urls();
747         foreach(const QUrl &uri, uris)
748         {
749             sendCoinsPage->handleURI(uri.toString());
750         }
751     }
752
753     event->acceptProposedAction();
754 }
755
756 void BitcoinGUI::handleURI(QString strURI)
757 {
758     gotoSendCoinsPage();
759     sendCoinsPage->handleURI(strURI);
760
761     if(!isActiveWindow())
762         activateWindow();
763
764     showNormalIfMinimized();
765 }
766
767 void BitcoinGUI::setEncryptionStatus(int status)
768 {
769     switch(status)
770     {
771     case WalletModel::Unencrypted:
772         labelEncryptionIcon->hide();
773         encryptWalletAction->setChecked(false);
774         changePassphraseAction->setEnabled(false);
775         encryptWalletAction->setEnabled(true);
776         break;
777     case WalletModel::Unlocked:
778         labelEncryptionIcon->show();
779         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
780         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
781         encryptWalletAction->setChecked(true);
782         changePassphraseAction->setEnabled(true);
783         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
784         break;
785     case WalletModel::Locked:
786         labelEncryptionIcon->show();
787         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
788         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
789         encryptWalletAction->setChecked(true);
790         changePassphraseAction->setEnabled(true);
791         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
792         break;
793     }
794 }
795
796 void BitcoinGUI::encryptWallet(bool status)
797 {
798     if(!walletModel)
799         return;
800     AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
801                                      AskPassphraseDialog::Decrypt, this);
802     dlg.setModel(walletModel);
803     dlg.exec();
804
805     setEncryptionStatus(walletModel->getEncryptionStatus());
806 }
807
808 void BitcoinGUI::backupWallet()
809 {
810     QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
811     QString filename = QFileDialog::getSaveFileName(this, tr("Backup Wallet"), saveDir, tr("Wallet Data (*.dat)"));
812     if(!filename.isEmpty()) {
813         if(!walletModel->backupWallet(filename)) {
814             QMessageBox::warning(this, tr("Backup Failed"), tr("There was an error trying to save the wallet data to the new location."));
815         }
816     }
817 }
818
819 void BitcoinGUI::changePassphrase()
820 {
821     AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
822     dlg.setModel(walletModel);
823     dlg.exec();
824 }
825
826 void BitcoinGUI::unlockWallet()
827 {
828     if(!walletModel)
829         return;
830     // Unlock wallet when requested by wallet model
831     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
832     {
833         AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
834         dlg.setModel(walletModel);
835         dlg.exec();
836     }
837 }
838
839 void BitcoinGUI::showNormalIfMinimized()
840 {
841     if(!isVisible()) // Show, if hidden
842         show();
843     if(isMinimized()) // Unminimize, if minimized
844         showNormal();
845 }