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