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