Merge pull request #854 from laanwj/2012_02_qtipc
[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     dummyWidget(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     // Dummy widget used when restoring window state after minimization
92     dummyWidget = new QWidget();
93
94     // Create tabs
95     overviewPage = new OverviewPage();
96
97     transactionsPage = new QWidget(this);
98     QVBoxLayout *vbox = new QVBoxLayout();
99     transactionView = new TransactionView(this);
100     vbox->addWidget(transactionView);
101     transactionsPage->setLayout(vbox);
102
103     addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
104
105     receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
106
107     sendCoinsPage = new SendCoinsDialog(this);
108
109     messagePage = new MessagePage(this);
110
111     centralWidget = new QStackedWidget(this);
112     centralWidget->addWidget(overviewPage);
113     centralWidget->addWidget(transactionsPage);
114     centralWidget->addWidget(addressBookPage);
115     centralWidget->addWidget(receiveCoinsPage);
116     centralWidget->addWidget(sendCoinsPage);
117 #ifdef FIRST_CLASS_MESSAGING
118     centralWidget->addWidget(messagePage);
119 #endif
120     setCentralWidget(centralWidget);
121
122     // Create status bar
123     statusBar();
124
125     // Status bar notification icons
126     QFrame *frameBlocks = new QFrame();
127     frameBlocks->setContentsMargins(0,0,0,0);
128     frameBlocks->setMinimumWidth(56);
129     frameBlocks->setMaximumWidth(56);
130     QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
131     frameBlocksLayout->setContentsMargins(3,0,3,0);
132     frameBlocksLayout->setSpacing(3);
133     labelEncryptionIcon = new QLabel();
134     labelConnectionsIcon = new QLabel();
135     labelBlocksIcon = new QLabel();
136     frameBlocksLayout->addStretch();
137     frameBlocksLayout->addWidget(labelEncryptionIcon);
138     frameBlocksLayout->addStretch();
139     frameBlocksLayout->addWidget(labelConnectionsIcon);
140     frameBlocksLayout->addStretch();
141     frameBlocksLayout->addWidget(labelBlocksIcon);
142     frameBlocksLayout->addStretch();
143
144     // Progress bar for blocks download
145     progressBarLabel = new QLabel(tr("Synchronizing with network..."));
146     progressBarLabel->setVisible(false);
147     progressBar = new QProgressBar();
148     progressBar->setToolTip(tr("Block chain synchronization in progress"));
149     progressBar->setVisible(false);
150
151     statusBar()->addWidget(progressBarLabel);
152     statusBar()->addWidget(progressBar);
153     statusBar()->addPermanentWidget(frameBlocks);
154
155     syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
156
157     // Clicking on a transaction on the overview page simply sends you to transaction history page
158     connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
159
160     // Doubleclicking on a transaction on the transaction history page shows details
161     connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
162
163     gotoOverviewPage();
164 }
165
166 BitcoinGUI::~BitcoinGUI()
167 {
168 #ifdef Q_WS_MAC
169     delete appMenuBar;
170 #endif
171     delete dummyWidget;
172 }
173
174 void BitcoinGUI::createActions()
175 {
176     QActionGroup *tabGroup = new QActionGroup(this);
177
178     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
179     overviewAction->setToolTip(tr("Show general overview of wallet"));
180     overviewAction->setCheckable(true);
181     overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
182     tabGroup->addAction(overviewAction);
183
184     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
185     historyAction->setToolTip(tr("Browse transaction history"));
186     historyAction->setCheckable(true);
187     historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
188     tabGroup->addAction(historyAction);
189
190     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
191     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
192     addressBookAction->setCheckable(true);
193     addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
194     tabGroup->addAction(addressBookAction);
195
196     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
197     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
198     receiveCoinsAction->setCheckable(true);
199     receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
200     tabGroup->addAction(receiveCoinsAction);
201
202     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
203     sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
204     sendCoinsAction->setCheckable(true);
205     sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
206     tabGroup->addAction(sendCoinsAction);
207
208     messageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message"), this);
209     messageAction->setToolTip(tr("Prove you control an address"));
210 #ifdef FIRST_CLASS_MESSAGING
211     messageAction->setCheckable(true);
212 #endif
213     tabGroup->addAction(messageAction);
214
215     connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormal()));
216     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
217     connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormal()));
218     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
219     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormal()));
220     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
221     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormal()));
222     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
223     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormal()));
224     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
225     connect(messageAction, SIGNAL(triggered()), this, SLOT(showNormal()));
226     connect(messageAction, SIGNAL(triggered()), this, SLOT(gotoMessagePage()));
227
228     quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
229     quitAction->setToolTip(tr("Quit application"));
230     quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
231     quitAction->setMenuRole(QAction::QuitRole);
232     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About %1").arg(qApp->applicationName()), this);
233     aboutAction->setToolTip(tr("Show information about Bitcoin"));
234     aboutAction->setMenuRole(QAction::AboutRole);
235     aboutQtAction = new QAction(tr("About &Qt"), this);
236     aboutQtAction->setToolTip(tr("Show information about Qt"));
237     aboutQtAction->setMenuRole(QAction::AboutQtRole);
238     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
239     optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
240     optionsAction->setMenuRole(QAction::PreferencesRole);
241     openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
242     openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
243     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
244     exportAction->setToolTip(tr("Export the data in the current tab to a file"));
245     encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
246     encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
247     encryptWalletAction->setCheckable(true);
248     backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet"), this);
249     backupWalletAction->setToolTip(tr("Backup wallet to another location"));
250     changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
251     changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
252
253     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
254     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
255     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
256     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
257     connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
258     connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
259     connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
260     connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
261 }
262
263 void BitcoinGUI::createMenuBar()
264 {
265 #ifdef Q_WS_MAC
266     // Create a decoupled menu bar on Mac which stays even if the window is closed
267     appMenuBar = new QMenuBar();
268 #else
269     // Get the main window's menu bar on other platforms
270     appMenuBar = menuBar();
271 #endif
272
273     // Configure the menus
274     QMenu *file = appMenuBar->addMenu(tr("&File"));
275     file->addAction(exportAction);
276 #ifndef FIRST_CLASS_MESSAGING
277     file->addAction(messageAction);
278     file->addSeparator();
279 #endif
280     file->addAction(quitAction);
281
282     QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
283     settings->addAction(encryptWalletAction);
284     settings->addAction(changePassphraseAction);
285     settings->addAction(backupWalletAction);
286     settings->addSeparator();
287     settings->addAction(optionsAction);
288
289     QMenu *help = appMenuBar->addMenu(tr("&Help"));
290     help->addAction(aboutAction);
291     help->addAction(aboutQtAction);
292 }
293
294 void BitcoinGUI::createToolBars()
295 {
296     QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
297     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
298     toolbar->addAction(overviewAction);
299     toolbar->addAction(sendCoinsAction);
300     toolbar->addAction(receiveCoinsAction);
301     toolbar->addAction(historyAction);
302     toolbar->addAction(addressBookAction);
303 #ifdef FIRST_CLASS_MESSAGING
304     toolbar->addAction(messageAction);
305 #endif
306
307     QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
308     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
309     toolbar2->addAction(exportAction);
310 }
311
312 void BitcoinGUI::setClientModel(ClientModel *clientModel)
313 {
314     this->clientModel = clientModel;
315     if(clientModel)
316     {
317         if(clientModel->isTestNet())
318         {
319             QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
320             setWindowTitle(title_testnet);
321 #ifndef Q_WS_MAC
322             setWindowIcon(QIcon(":icons/bitcoin_testnet"));
323 #else
324             MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
325 #endif
326             if(trayIcon)
327             {
328                 trayIcon->setToolTip(title_testnet);
329                 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
330             }
331         }
332
333         // Keep up to date with client
334         setNumConnections(clientModel->getNumConnections());
335         connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
336
337         setNumBlocks(clientModel->getNumBlocks());
338         connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
339
340         // Report errors from network/worker thread
341         connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
342     }
343 }
344
345 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
346 {
347     this->walletModel = walletModel;
348     if(walletModel)
349     {
350         // Report errors from wallet thread
351         connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
352
353         // Put transaction list in tabs
354         transactionView->setModel(walletModel);
355
356         overviewPage->setModel(walletModel);
357         addressBookPage->setModel(walletModel->getAddressTableModel());
358         receiveCoinsPage->setModel(walletModel->getAddressTableModel());
359         sendCoinsPage->setModel(walletModel);
360         messagePage->setModel(walletModel);
361
362         setEncryptionStatus(walletModel->getEncryptionStatus());
363         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
364
365         // Balloon popup for new transaction
366         connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
367                 this, SLOT(incomingTransaction(QModelIndex,int,int)));
368
369         // Ask for passphrase if needed
370         connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
371     }
372 }
373
374 void BitcoinGUI::createTrayIcon()
375 {
376     QMenu *trayIconMenu;
377 #ifndef Q_WS_MAC
378     trayIcon = new QSystemTrayIcon(this);
379     trayIconMenu = new QMenu(this);
380     trayIcon->setContextMenu(trayIconMenu);
381     trayIcon->setToolTip("Bitcoin client");
382     trayIcon->setIcon(QIcon(":/icons/toolbar"));
383     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
384             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
385     trayIcon->show();
386 #else
387     // Note: On Mac, the dock icon is used to provide the tray's functionality.
388     MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
389     connect(dockIconHandler, SIGNAL(dockIconClicked()), openBitcoinAction, SLOT(trigger()));
390     trayIconMenu = dockIconHandler->dockMenu();
391 #endif
392
393     // Configuration of the tray icon (or dock icon) icon menu
394     trayIconMenu->addAction(openBitcoinAction);
395     trayIconMenu->addSeparator();
396     trayIconMenu->addAction(messageAction);
397 #ifndef FIRST_CLASS_MESSAGING
398     trayIconMenu->addSeparator();
399 #endif
400     trayIconMenu->addAction(receiveCoinsAction);
401     trayIconMenu->addAction(sendCoinsAction);
402     trayIconMenu->addSeparator();
403     trayIconMenu->addAction(optionsAction);
404 #ifndef Q_WS_MAC // This is built-in on Mac
405     trayIconMenu->addSeparator();
406     trayIconMenu->addAction(quitAction);
407 #endif
408
409     notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
410 }
411
412 #ifndef Q_WS_MAC
413 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
414 {
415     if(reason == QSystemTrayIcon::Trigger)
416     {
417         // Click on system tray icon triggers "open bitcoin"
418         openBitcoinAction->trigger();
419     }
420 }
421 #endif
422
423 void BitcoinGUI::showNormal()
424 {
425     // Reparent window to the desktop (in case it was hidden on minimize)
426     if(parent() != NULL)
427         setParent(NULL, Qt::Window);
428     QMainWindow::showNormal();
429 }
430
431 void BitcoinGUI::optionsClicked()
432 {
433     if(!clientModel || !clientModel->getOptionsModel())
434         return;
435     OptionsDialog dlg;
436     dlg.setModel(clientModel->getOptionsModel());
437     dlg.exec();
438 }
439
440 void BitcoinGUI::aboutClicked()
441 {
442     AboutDialog dlg;
443     dlg.setModel(clientModel);
444     dlg.exec();
445 }
446
447 void BitcoinGUI::setNumConnections(int count)
448 {
449     QString icon;
450     switch(count)
451     {
452     case 0: icon = ":/icons/connect_0"; break;
453     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
454     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
455     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
456     default: icon = ":/icons/connect_4"; break;
457     }
458     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
459     labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
460 }
461
462 void BitcoinGUI::setNumBlocks(int count)
463 {
464     if(!clientModel)
465         return;
466     int total = clientModel->getNumBlocksOfPeers();
467     QString tooltip;
468
469     if(count < total)
470     {
471         if (clientModel->getStatusBarWarnings() == "")
472         {
473             progressBarLabel->setVisible(true);
474             progressBarLabel->setText(tr("Synchronizing with network..."));
475             progressBar->setVisible(true);
476             progressBar->setMaximum(total);
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.").arg(count).arg(total);
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 < 60)
507     {
508         text = tr("%n second(s) ago","",secs);
509     }
510     else if(secs < 60*60)
511     {
512         text = tr("%n minute(s) ago","",secs/60);
513     }
514     else if(secs < 24*60*60)
515     {
516         text = tr("%n hour(s) ago","",secs/(60*60));
517     }
518     else
519     {
520         text = tr("%n day(s) ago","",secs/(60*60*24));
521     }
522
523     // Set icon state: spinning if catching up, tick otherwise
524     if(secs < 30*60)
525     {
526         tooltip = tr("Up to date") + QString("\n") + tooltip;
527         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
528     }
529     else
530     {
531         tooltip = tr("Catching up...") + QString("\n") + tooltip;
532         labelBlocksIcon->setMovie(syncIconMovie);
533         syncIconMovie->start();
534     }
535
536     tooltip += QString("\n");
537     tooltip += tr("Last received block was generated %1.").arg(text);
538
539     labelBlocksIcon->setToolTip(tooltip);
540     progressBarLabel->setToolTip(tooltip);
541     progressBar->setToolTip(tooltip);
542 }
543
544 void BitcoinGUI::refreshStatusBar()
545 {
546     /* Might display multiple times in the case of multiple alerts
547     static QString prevStatusBar;
548     QString newStatusBar = clientModel->getStatusBarWarnings();
549     if (prevStatusBar != newStatusBar)
550     {
551         prevStatusBar = newStatusBar;
552         error(tr("Network Alert"), newStatusBar);
553     }*/
554     setNumBlocks(clientModel->getNumBlocks());
555 }
556
557 void BitcoinGUI::error(const QString &title, const QString &message)
558 {
559     // Report errors from network/worker thread
560     notificator->notify(Notificator::Critical, title, message);
561 }
562
563 void BitcoinGUI::changeEvent(QEvent *e)
564 {
565 #ifndef Q_WS_MAC // Ignored on Mac
566     if(e->type() == QEvent::WindowStateChange)
567     {
568         if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
569         {
570             if(isMinimized())
571             {
572                 // Hiding the window from taskbar
573                 setParent(dummyWidget, Qt::SubWindow);
574                 return;
575             }
576             else
577             {
578                 showNormal();
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 }