Restructure IPC URL handling (fixes #851)
[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 data in the current tab to a file"));
243     encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
244     encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
245     encryptWalletAction->setCheckable(true);
246     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     file->addAction(exportAction);
271 #ifndef FIRST_CLASS_MESSAGING
272     file->addAction(messageAction);
273     file->addSeparator();
274 #endif
275     file->addAction(quitAction);
276
277     QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
278     settings->addAction(encryptWalletAction);
279     settings->addAction(changePassphraseAction);
280     settings->addSeparator();
281     settings->addAction(optionsAction);
282
283     QMenu *help = appMenuBar->addMenu(tr("&Help"));
284     help->addAction(aboutAction);
285     help->addAction(aboutQtAction);
286 }
287
288 void BitcoinGUI::createToolBars()
289 {
290     QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
291     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
292     toolbar->addAction(overviewAction);
293     toolbar->addAction(sendCoinsAction);
294     toolbar->addAction(receiveCoinsAction);
295     toolbar->addAction(historyAction);
296     toolbar->addAction(addressBookAction);
297 #ifdef FIRST_CLASS_MESSAGING
298     toolbar->addAction(messageAction);
299 #endif
300
301     QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
302     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
303     toolbar2->addAction(exportAction);
304 }
305
306 void BitcoinGUI::setClientModel(ClientModel *clientModel)
307 {
308     this->clientModel = clientModel;
309     if(clientModel)
310     {
311         if(clientModel->isTestNet())
312         {
313             QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
314             setWindowTitle(title_testnet);
315 #ifndef Q_WS_MAC
316             setWindowIcon(QIcon(":icons/bitcoin_testnet"));
317 #else
318             MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
319 #endif
320             if(trayIcon)
321             {
322                 trayIcon->setToolTip(title_testnet);
323                 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
324             }
325         }
326
327         // Keep up to date with client
328         setNumConnections(clientModel->getNumConnections());
329         connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
330
331         setNumBlocks(clientModel->getNumBlocks());
332         connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
333
334         // Report errors from network/worker thread
335         connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
336     }
337 }
338
339 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
340 {
341     this->walletModel = walletModel;
342     if(walletModel)
343     {
344         // Report errors from wallet thread
345         connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
346
347         // Put transaction list in tabs
348         transactionView->setModel(walletModel);
349
350         overviewPage->setModel(walletModel);
351         addressBookPage->setModel(walletModel->getAddressTableModel());
352         receiveCoinsPage->setModel(walletModel->getAddressTableModel());
353         sendCoinsPage->setModel(walletModel);
354         messagePage->setModel(walletModel);
355
356         setEncryptionStatus(walletModel->getEncryptionStatus());
357         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
358
359         // Balloon popup for new transaction
360         connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
361                 this, SLOT(incomingTransaction(QModelIndex,int,int)));
362
363         // Ask for passphrase if needed
364         connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
365     }
366 }
367
368 void BitcoinGUI::createTrayIcon()
369 {
370     QMenu *trayIconMenu;
371 #ifndef Q_WS_MAC
372     trayIcon = new QSystemTrayIcon(this);
373     trayIconMenu = new QMenu(this);
374     trayIcon->setContextMenu(trayIconMenu);
375     trayIcon->setToolTip("Bitcoin client");
376     trayIcon->setIcon(QIcon(":/icons/toolbar"));
377     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
378             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
379     trayIcon->show();
380 #else
381     // Note: On Mac, the dock icon is used to provide the tray's functionality.
382     MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
383     connect(dockIconHandler, SIGNAL(dockIconClicked()), openBitcoinAction, SLOT(trigger()));
384     trayIconMenu = dockIconHandler->dockMenu();
385 #endif
386
387     // Configuration of the tray icon (or dock icon) icon menu
388     trayIconMenu->addAction(openBitcoinAction);
389     trayIconMenu->addSeparator();
390     trayIconMenu->addAction(messageAction);
391 #ifndef FIRST_CLASS_MESSAGING
392     trayIconMenu->addSeparator();
393 #endif
394     trayIconMenu->addAction(receiveCoinsAction);
395     trayIconMenu->addAction(sendCoinsAction);
396     trayIconMenu->addSeparator();
397     trayIconMenu->addAction(optionsAction);
398 #ifndef Q_WS_MAC // This is built-in on Mac
399     trayIconMenu->addSeparator();
400     trayIconMenu->addAction(quitAction);
401 #endif
402
403     notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
404 }
405
406 #ifndef Q_WS_MAC
407 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
408 {
409     if(reason == QSystemTrayIcon::Trigger)
410     {
411         // Click on system tray icon triggers "open bitcoin"
412         openBitcoinAction->trigger();
413     }
414 }
415 #endif
416
417 void BitcoinGUI::showNormal()
418 {
419     // Reparent window to the desktop (in case it was hidden on minimize)
420     if(parent() != NULL)
421         setParent(NULL, Qt::Window);
422     QMainWindow::showNormal();
423 }
424
425 void BitcoinGUI::optionsClicked()
426 {
427     if(!clientModel || !clientModel->getOptionsModel())
428         return;
429     OptionsDialog dlg;
430     dlg.setModel(clientModel->getOptionsModel());
431     dlg.exec();
432 }
433
434 void BitcoinGUI::aboutClicked()
435 {
436     AboutDialog dlg;
437     dlg.setModel(clientModel);
438     dlg.exec();
439 }
440
441 void BitcoinGUI::setNumConnections(int count)
442 {
443     QString icon;
444     switch(count)
445     {
446     case 0: icon = ":/icons/connect_0"; break;
447     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
448     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
449     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
450     default: icon = ":/icons/connect_4"; break;
451     }
452     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
453     labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
454 }
455
456 void BitcoinGUI::setNumBlocks(int count)
457 {
458     if(!clientModel)
459         return;
460     int total = clientModel->getNumBlocksOfPeers();
461     QString tooltip;
462
463     if(count < total)
464     {
465         if (clientModel->getStatusBarWarnings() == "")
466         {
467             progressBarLabel->setVisible(true);
468             progressBarLabel->setText(tr("Synchronizing with network..."));
469             progressBar->setVisible(true);
470             progressBar->setMaximum(total);
471             progressBar->setValue(count);
472         }
473         else
474         {
475             progressBarLabel->setText(clientModel->getStatusBarWarnings());
476             progressBarLabel->setVisible(true);
477             progressBar->setVisible(false);
478         }
479         tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
480     }
481     else
482     {
483         if (clientModel->getStatusBarWarnings() == "")
484             progressBarLabel->setVisible(false);
485         else
486         {
487             progressBarLabel->setText(clientModel->getStatusBarWarnings());
488             progressBarLabel->setVisible(true);
489         }
490         progressBar->setVisible(false);
491         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
492     }
493
494     QDateTime now = QDateTime::currentDateTime();
495     QDateTime lastBlockDate = clientModel->getLastBlockDate();
496     int secs = lastBlockDate.secsTo(now);
497     QString text;
498
499     // Represent time from last generated block in human readable text
500     if(secs < 60)
501     {
502         text = tr("%n second(s) ago","",secs);
503     }
504     else if(secs < 60*60)
505     {
506         text = tr("%n minute(s) ago","",secs/60);
507     }
508     else if(secs < 24*60*60)
509     {
510         text = tr("%n hour(s) ago","",secs/(60*60));
511     }
512     else
513     {
514         text = tr("%n day(s) ago","",secs/(60*60*24));
515     }
516
517     // Set icon state: spinning if catching up, tick otherwise
518     if(secs < 30*60)
519     {
520         tooltip = tr("Up to date") + QString("\n") + tooltip;
521         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
522     }
523     else
524     {
525         tooltip = tr("Catching up...") + QString("\n") + tooltip;
526         labelBlocksIcon->setMovie(syncIconMovie);
527         syncIconMovie->start();
528     }
529
530     tooltip += QString("\n");
531     tooltip += tr("Last received block was generated %1.").arg(text);
532
533     labelBlocksIcon->setToolTip(tooltip);
534     progressBarLabel->setToolTip(tooltip);
535     progressBar->setToolTip(tooltip);
536 }
537
538 void BitcoinGUI::refreshStatusBar()
539 {
540     /* Might display multiple times in the case of multiple alerts
541     static QString prevStatusBar;
542     QString newStatusBar = clientModel->getStatusBarWarnings();
543     if (prevStatusBar != newStatusBar)
544     {
545         prevStatusBar = newStatusBar;
546         error(tr("Network Alert"), newStatusBar);
547     }*/
548     setNumBlocks(clientModel->getNumBlocks());
549 }
550
551 void BitcoinGUI::error(const QString &title, const QString &message)
552 {
553     // Report errors from network/worker thread
554     notificator->notify(Notificator::Critical, title, message);
555 }
556
557 void BitcoinGUI::changeEvent(QEvent *e)
558 {
559 #ifndef Q_WS_MAC // Ignored on Mac
560     if(e->type() == QEvent::WindowStateChange)
561     {
562         if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
563         {
564             if(isMinimized())
565             {
566                 // Hiding the window from taskbar
567                 setParent(dummyWidget, Qt::SubWindow);
568                 return;
569             }
570             else
571             {
572                 showNormal();
573             }
574         }
575     }
576 #endif
577     QMainWindow::changeEvent(e);
578 }
579
580 void BitcoinGUI::closeEvent(QCloseEvent *event)
581 {
582     if(clientModel)
583     {
584 #ifndef Q_WS_MAC // Ignored on Mac
585         if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
586            !clientModel->getOptionsModel()->getMinimizeOnClose())
587         {
588             qApp->quit();
589         }
590 #endif
591     }
592     QMainWindow::closeEvent(event);
593 }
594
595 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
596 {
597     QString strMessage =
598         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
599           "which goes to the nodes that process your transaction and helps to support the network.  "
600           "Do you want to pay the fee?").arg(
601                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
602     QMessageBox::StandardButton retval = QMessageBox::question(
603           this, tr("Sending..."), strMessage,
604           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
605     *payFee = (retval == QMessageBox::Yes);
606 }
607
608 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
609 {
610     if(!walletModel || !clientModel)
611         return;
612     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
613     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
614                     .data(Qt::EditRole).toULongLong();
615     if(!clientModel->inInitialBlockDownload())
616     {
617         // On new transaction, make an info balloon
618         // Unless the initial block download is in progress, to prevent balloon-spam
619         QString date = ttm->index(start, TransactionTableModel::Date, parent)
620                         .data().toString();
621         QString type = ttm->index(start, TransactionTableModel::Type, parent)
622                         .data().toString();
623         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
624                         .data().toString();
625         QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
626                             TransactionTableModel::ToAddress, parent)
627                         .data(Qt::DecorationRole));
628
629         notificator->notify(Notificator::Information,
630                             (amount)<0 ? tr("Sent transaction") :
631                                          tr("Incoming transaction"),
632                               tr("Date: %1\n"
633                                  "Amount: %2\n"
634                                  "Type: %3\n"
635                                  "Address: %4\n")
636                               .arg(date)
637                               .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
638                               .arg(type)
639                               .arg(address), icon);
640     }
641 }
642
643 void BitcoinGUI::gotoOverviewPage()
644 {
645     overviewAction->setChecked(true);
646     centralWidget->setCurrentWidget(overviewPage);
647
648     exportAction->setEnabled(false);
649     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
650 }
651
652 void BitcoinGUI::gotoHistoryPage()
653 {
654     historyAction->setChecked(true);
655     centralWidget->setCurrentWidget(transactionsPage);
656
657     exportAction->setEnabled(true);
658     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
659     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
660 }
661
662 void BitcoinGUI::gotoAddressBookPage()
663 {
664     addressBookAction->setChecked(true);
665     centralWidget->setCurrentWidget(addressBookPage);
666
667     exportAction->setEnabled(true);
668     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
669     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
670 }
671
672 void BitcoinGUI::gotoReceiveCoinsPage()
673 {
674     receiveCoinsAction->setChecked(true);
675     centralWidget->setCurrentWidget(receiveCoinsPage);
676
677     exportAction->setEnabled(true);
678     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
679     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
680 }
681
682 void BitcoinGUI::gotoSendCoinsPage()
683 {
684     sendCoinsAction->setChecked(true);
685     centralWidget->setCurrentWidget(sendCoinsPage);
686
687     exportAction->setEnabled(false);
688     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
689 }
690
691 void BitcoinGUI::gotoMessagePage()
692 {
693 #ifdef FIRST_CLASS_MESSAGING
694     messageAction->setChecked(true);
695     centralWidget->setCurrentWidget(messagePage);
696
697     exportAction->setEnabled(false);
698     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
699 #else
700     messagePage->show();
701     messagePage->setFocus();
702 #endif
703 }
704
705 void BitcoinGUI::gotoMessagePage(QString addr)
706 {
707     gotoMessagePage();
708     messagePage->setAddress(addr);
709 }
710
711 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
712 {
713     // Accept only URLs
714     if(event->mimeData()->hasUrls())
715         event->acceptProposedAction();
716 }
717
718 void BitcoinGUI::dropEvent(QDropEvent *event)
719 {
720     if(event->mimeData()->hasUrls())
721     {
722         gotoSendCoinsPage();
723         QList<QUrl> urls = event->mimeData()->urls();
724         foreach(const QUrl &url, urls)
725         {
726             sendCoinsPage->handleURL(url.toString());
727         }
728     }
729
730     event->acceptProposedAction();
731 }
732
733 void BitcoinGUI::handleURL(QString strURL)
734 {
735     gotoSendCoinsPage();
736     sendCoinsPage->handleURL(strURL);
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 }