bdc9821844c024bce7ef307f18d0f7f1612e2cfa
[novacoin.git] / src / qt / bitcoingui.cpp
1 /*
2  * Qt4 bitcoin GUI.
3  *
4  * W.J. van der Laan 2011
5  */
6 #include "bitcoingui.h"
7 #include "transactiontablemodel.h"
8 #include "addressbookpage.h"
9 #include "sendcoinsdialog.h"
10 #include "optionsdialog.h"
11 #include "aboutdialog.h"
12 #include "clientmodel.h"
13 #include "walletmodel.h"
14 #include "editaddressdialog.h"
15 #include "optionsmodel.h"
16 #include "transactiondescdialog.h"
17 #include "addresstablemodel.h"
18 #include "transactionview.h"
19 #include "overviewpage.h"
20 #include "bitcoinunits.h"
21 #include "guiconstants.h"
22 #include "askpassphrasedialog.h"
23 #include "notificator.h"
24
25 #ifdef Q_WS_MAC
26 #include "macdockiconhandler.h"
27 #endif
28
29 #include <QApplication>
30 #include <QMainWindow>
31 #include <QMenuBar>
32 #include <QMenu>
33 #include <QIcon>
34 #include <QTabWidget>
35 #include <QVBoxLayout>
36 #include <QToolBar>
37 #include <QStatusBar>
38 #include <QLabel>
39 #include <QLineEdit>
40 #include <QPushButton>
41 #include <QLocale>
42 #include <QMessageBox>
43 #include <QProgressBar>
44 #include <QStackedWidget>
45 #include <QDateTime>
46 #include <QMovie>
47
48 #include <QDragEnterEvent>
49 #include <QUrl>
50
51 #include <iostream>
52
53 BitcoinGUI::BitcoinGUI(QWidget *parent):
54     QMainWindow(parent),
55     clientModel(0),
56     walletModel(0),
57     encryptWalletAction(0),
58     changePassphraseAction(0),
59     trayIcon(0),
60     notificator(0)
61 {
62     resize(850, 550);
63     setWindowTitle(tr("Bitcoin Wallet"));
64 #ifndef Q_WS_MAC
65     setWindowIcon(QIcon(":icons/bitcoin"));
66 #else
67     setUnifiedTitleAndToolBarOnMac(true);
68     QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
69 #endif
70     // Accept D&D of URIs
71     setAcceptDrops(true);
72
73     // Create actions for the toolbar, menu bar and tray/dock icon
74     createActions();
75
76     // Create application menu bar
77     createMenuBar();
78
79     // Create the toolbars
80     createToolBars();
81
82     // Create the tray icon (or setup the dock icon)
83     createTrayIcon();
84
85     // Create tabs
86     overviewPage = new OverviewPage();
87
88     transactionsPage = new QWidget(this);
89     QVBoxLayout *vbox = new QVBoxLayout();
90     transactionView = new TransactionView(this);
91     vbox->addWidget(transactionView);
92     transactionsPage->setLayout(vbox);
93
94     addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
95
96     receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
97
98     sendCoinsPage = new SendCoinsDialog(this);
99
100     centralWidget = new QStackedWidget(this);
101     centralWidget->addWidget(overviewPage);
102     centralWidget->addWidget(transactionsPage);
103     centralWidget->addWidget(addressBookPage);
104     centralWidget->addWidget(receiveCoinsPage);
105     centralWidget->addWidget(sendCoinsPage);
106     setCentralWidget(centralWidget);
107
108     // Create status bar
109     statusBar();
110
111     // Status bar notification icons
112     QFrame *frameBlocks = new QFrame();
113     //frameBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
114     frameBlocks->setContentsMargins(0,0,0,0);
115     frameBlocks->setMinimumWidth(56);
116     frameBlocks->setMaximumWidth(56);
117     QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
118     frameBlocksLayout->setContentsMargins(3,0,3,0);
119     frameBlocksLayout->setSpacing(3);
120     labelEncryptionIcon = new QLabel();
121     labelConnectionsIcon = new QLabel();
122     labelBlocksIcon = new QLabel();
123     frameBlocksLayout->addStretch();
124     frameBlocksLayout->addWidget(labelEncryptionIcon);
125     frameBlocksLayout->addStretch();
126     frameBlocksLayout->addWidget(labelConnectionsIcon);
127     frameBlocksLayout->addStretch();
128     frameBlocksLayout->addWidget(labelBlocksIcon);
129     frameBlocksLayout->addStretch();
130
131     // Progress bar for blocks download
132     progressBarLabel = new QLabel(tr("Synchronizing with network..."));
133     progressBarLabel->setVisible(false);
134     progressBar = new QProgressBar();
135     progressBar->setToolTip(tr("Block chain synchronization in progress"));
136     progressBar->setVisible(false);
137
138     statusBar()->addWidget(progressBarLabel);
139     statusBar()->addWidget(progressBar);
140     statusBar()->addPermanentWidget(frameBlocks);
141
142     syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
143
144     // Clicking on a transaction on the overview page simply sends you to transaction history page
145     connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
146
147     // Doubleclicking on a transaction on the transaction history page shows details
148     connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
149
150     gotoOverviewPage();
151 }
152
153 BitcoinGUI::~BitcoinGUI()
154 {
155 #ifdef Q_WS_MAC
156     delete appMenuBar;
157 #endif
158 }
159
160 void BitcoinGUI::createActions()
161 {
162     QActionGroup *tabGroup = new QActionGroup(this);
163
164     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
165     overviewAction->setToolTip(tr("Show general overview of wallet"));
166     overviewAction->setCheckable(true);
167     overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
168     tabGroup->addAction(overviewAction);
169
170     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
171     historyAction->setToolTip(tr("Browse transaction history"));
172     historyAction->setCheckable(true);
173     historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
174     tabGroup->addAction(historyAction);
175
176     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
177     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
178     addressBookAction->setCheckable(true);
179     addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
180     tabGroup->addAction(addressBookAction);
181
182     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
183     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
184     receiveCoinsAction->setCheckable(true);
185     receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
186     tabGroup->addAction(receiveCoinsAction);
187
188     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
189     sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
190     sendCoinsAction->setCheckable(true);
191     sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
192     tabGroup->addAction(sendCoinsAction);
193
194     connect(overviewAction, SIGNAL(triggered()), this, SLOT(show()));
195     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
196     connect(historyAction, SIGNAL(triggered()), this, SLOT(show()));
197     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
198     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(show()));
199     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
200     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(show()));
201     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
202     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(show()));
203     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
204
205     quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
206     quitAction->setToolTip(tr("Quit application"));
207     quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
208     quitAction->setMenuRole(QAction::QuitRole);
209     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About %1").arg(qApp->applicationName()), this);
210     aboutAction->setToolTip(tr("Show information about Bitcoin"));
211     aboutAction->setMenuRole(QAction::AboutQtRole);
212     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
213     optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
214     optionsAction->setMenuRole(QAction::PreferencesRole);
215     openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
216     openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
217     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
218     exportAction->setToolTip(tr("Export the current view to a file"));
219     encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
220     encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
221     encryptWalletAction->setCheckable(true);
222     changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
223     changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
224
225     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
226     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
227     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
228     connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
229     connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
230     connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
231 }
232
233 void BitcoinGUI::createMenuBar()
234 {
235 #ifdef Q_WS_MAC
236     // Create a decoupled menu bar on Mac which stays even if the window is closed
237     appMenuBar = new QMenuBar();
238 #else
239     // Get the main window's menu bar on other platforms
240     appMenuBar = menuBar();
241 #endif
242
243     // Configure the menus
244     QMenu *file = appMenuBar->addMenu(tr("&File"));
245     file->addAction(quitAction);
246
247     QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
248     settings->addAction(encryptWalletAction);
249     settings->addAction(changePassphraseAction);
250     settings->addSeparator();
251     settings->addAction(optionsAction);
252
253     QMenu *help = appMenuBar->addMenu(tr("&Help"));
254     help->addAction(aboutAction);
255 }
256
257 void BitcoinGUI::createToolBars()
258 {
259     QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
260     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
261     toolbar->addAction(overviewAction);
262     toolbar->addAction(sendCoinsAction);
263     toolbar->addAction(receiveCoinsAction);
264     toolbar->addAction(historyAction);
265     toolbar->addAction(addressBookAction);
266
267     QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
268     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
269     toolbar2->addAction(exportAction);
270 }
271
272 void BitcoinGUI::setClientModel(ClientModel *clientModel)
273 {
274     this->clientModel = clientModel;
275     if(clientModel)
276     {
277         if(clientModel->isTestNet())
278         {
279             QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
280             setWindowTitle(title_testnet);
281 #ifndef Q_WS_MAC
282             setWindowIcon(QIcon(":icons/bitcoin_testnet"));
283 #else
284             MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
285 #endif
286             if(trayIcon)
287             {
288                 trayIcon->setToolTip(title_testnet);
289                 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
290             }
291         }
292
293         // Keep up to date with client
294         setNumConnections(clientModel->getNumConnections());
295         connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
296
297         setNumBlocks(clientModel->getNumBlocks());
298         connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
299
300         // Report errors from network/worker thread
301         connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
302     }
303 }
304
305 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
306 {
307     this->walletModel = walletModel;
308     if(walletModel)
309     {
310         // Report errors from wallet thread
311         connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
312
313         // Put transaction list in tabs
314         transactionView->setModel(walletModel);
315
316         overviewPage->setModel(walletModel);
317         addressBookPage->setModel(walletModel->getAddressTableModel());
318         receiveCoinsPage->setModel(walletModel->getAddressTableModel());
319         sendCoinsPage->setModel(walletModel);
320
321         setEncryptionStatus(walletModel->getEncryptionStatus());
322         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
323
324         // Balloon popup for new transaction
325         connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
326                 this, SLOT(incomingTransaction(QModelIndex,int,int)));
327
328         // Ask for passphrase if needed
329         connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
330     }
331 }
332
333 void BitcoinGUI::createTrayIcon()
334 {
335     QMenu *trayIconMenu;
336 #ifndef Q_WS_MAC
337     trayIcon = new QSystemTrayIcon(this);
338     trayIconMenu = new QMenu(this);
339     trayIcon->setContextMenu(trayIconMenu);
340     trayIcon->setToolTip("Bitcoin client");
341     trayIcon->setIcon(QIcon(":/icons/toolbar"));
342     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
343             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
344     trayIcon->show();
345 #else
346     // Note: On Mac, the dock icon is used to provide the tray's functionality.
347     MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
348     connect(dockIconHandler, SIGNAL(dockIconClicked()), openBitcoinAction, SLOT(trigger()));
349     trayIconMenu = dockIconHandler->dockMenu();
350 #endif
351
352     // Configuration of the tray icon (or dock icon) icon menu
353     trayIconMenu->addAction(openBitcoinAction);
354     trayIconMenu->addSeparator();
355     trayIconMenu->addAction(receiveCoinsAction);
356     trayIconMenu->addAction(sendCoinsAction);
357     trayIconMenu->addSeparator();
358     trayIconMenu->addAction(optionsAction);
359 #ifndef Q_WS_MAC // This is built-in on Mac
360     trayIconMenu->addSeparator();
361     trayIconMenu->addAction(quitAction);
362 #endif
363
364     notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
365 }
366
367 #ifndef Q_WS_MAC
368 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
369 {
370     if(reason == QSystemTrayIcon::Trigger)
371     {
372         // Click on system tray icon triggers "open bitcoin"
373         openBitcoinAction->trigger();
374     }
375
376 }
377 #endif
378
379 void BitcoinGUI::optionsClicked()
380 {
381     if(!clientModel || !clientModel->getOptionsModel())
382         return;
383     OptionsDialog dlg;
384     dlg.setModel(clientModel->getOptionsModel());
385     dlg.exec();
386 }
387
388 void BitcoinGUI::aboutClicked()
389 {
390     AboutDialog dlg;
391     dlg.setModel(clientModel);
392     dlg.exec();
393 }
394
395 void BitcoinGUI::setNumConnections(int count)
396 {
397     QString icon;
398     switch(count)
399     {
400     case 0: icon = ":/icons/connect_0"; break;
401     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
402     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
403     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
404     default: icon = ":/icons/connect_4"; break;
405     }
406     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
407     labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
408 }
409
410 void BitcoinGUI::setNumBlocks(int count)
411 {
412     if(!clientModel)
413         return;
414     int initTotal = clientModel->getNumBlocksAtStartup();
415     int total = clientModel->getNumBlocksOfPeers();
416     QString tooltip;
417
418     if(count < total)
419     {
420         progressBarLabel->setVisible(true);
421         progressBar->setVisible(true);
422         progressBar->setMaximum(total - initTotal);
423         progressBar->setValue(count - initTotal);
424         tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
425     }
426     else
427     {
428         progressBarLabel->setVisible(false);
429         progressBar->setVisible(false);
430         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
431     }
432
433     QDateTime now = QDateTime::currentDateTime();
434     QDateTime lastBlockDate = clientModel->getLastBlockDate();
435     int secs = lastBlockDate.secsTo(now);
436     QString text;
437
438     // Represent time from last generated block in human readable text
439     if(secs < 60)
440     {
441         text = tr("%n second(s) ago","",secs);
442     }
443     else if(secs < 60*60)
444     {
445         text = tr("%n minute(s) ago","",secs/60);
446     }
447     else if(secs < 24*60*60)
448     {
449         text = tr("%n hour(s) ago","",secs/(60*60));
450     }
451     else
452     {
453         text = tr("%n day(s) ago","",secs/(60*60*24));
454     }
455
456     // Set icon state: spinning if catching up, tick otherwise
457     if(secs < 30*60)
458     {
459         tooltip = tr("Up to date") + QString("\n") + tooltip;
460         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
461     }
462     else
463     {
464         tooltip = tr("Catching up...") + QString("\n") + tooltip;
465         labelBlocksIcon->setMovie(syncIconMovie);
466         syncIconMovie->start();
467     }
468
469     tooltip += QString("\n");
470     tooltip += tr("Last received block was generated %1.").arg(text);
471
472     labelBlocksIcon->setToolTip(tooltip);
473     progressBarLabel->setToolTip(tooltip);
474     progressBar->setToolTip(tooltip);
475 }
476
477 void BitcoinGUI::error(const QString &title, const QString &message)
478 {
479     // Report errors from network/worker thread
480     notificator->notify(Notificator::Critical, title, message);
481 }
482
483 void BitcoinGUI::changeEvent(QEvent *e)
484 {
485 #ifndef Q_WS_MAC // Ignored on Mac
486     if(e->type() == QEvent::WindowStateChange)
487     {
488         if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
489         {
490             if(isMinimized())
491             {
492                 hide();
493                 e->ignore();
494             }
495             else
496             {
497                 show();
498                 e->accept();
499             }
500         }
501     }
502 #endif
503     QMainWindow::changeEvent(e);
504 }
505
506 void BitcoinGUI::closeEvent(QCloseEvent *event)
507 {
508     if(clientModel)
509     {
510 #ifndef Q_WS_MAC // Ignored on Mac
511         if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
512            !clientModel->getOptionsModel()->getMinimizeOnClose())
513         {
514             qApp->quit();
515         }
516 #endif
517     }
518     QMainWindow::closeEvent(event);
519 }
520
521 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
522 {
523     QString strMessage =
524         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
525           "which goes to the nodes that process your transaction and helps to support the network.  "
526           "Do you want to pay the fee?").arg(
527                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
528     QMessageBox::StandardButton retval = QMessageBox::question(
529           this, tr("Sending..."), strMessage,
530           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
531     *payFee = (retval == QMessageBox::Yes);
532 }
533
534 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
535 {
536     if(!walletModel || !clientModel)
537         return;
538     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
539     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
540                     .data(Qt::EditRole).toULongLong();
541     if(!clientModel->inInitialBlockDownload())
542     {
543         // On new transaction, make an info balloon
544         // Unless the initial block download is in progress, to prevent balloon-spam
545         QString date = ttm->index(start, TransactionTableModel::Date, parent)
546                         .data().toString();
547         QString type = ttm->index(start, TransactionTableModel::Type, parent)
548                         .data().toString();
549         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
550                         .data().toString();
551         QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
552                             TransactionTableModel::ToAddress, parent)
553                         .data(Qt::DecorationRole));
554
555         notificator->notify(Notificator::Information,
556                             (amount)<0 ? tr("Sent transaction") :
557                                          tr("Incoming transaction"),
558                               tr("Date: %1\n"
559                                  "Amount: %2\n"
560                                  "Type: %3\n"
561                                  "Address: %4\n")
562                               .arg(date)
563                               .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
564                               .arg(type)
565                               .arg(address), icon);
566     }
567 }
568
569 void BitcoinGUI::gotoOverviewPage()
570 {
571     overviewAction->setChecked(true);
572     centralWidget->setCurrentWidget(overviewPage);
573
574     exportAction->setEnabled(false);
575     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
576 }
577
578 void BitcoinGUI::gotoHistoryPage()
579 {
580     historyAction->setChecked(true);
581     centralWidget->setCurrentWidget(transactionsPage);
582
583     exportAction->setEnabled(true);
584     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
585     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
586 }
587
588 void BitcoinGUI::gotoAddressBookPage()
589 {
590     addressBookAction->setChecked(true);
591     centralWidget->setCurrentWidget(addressBookPage);
592
593     exportAction->setEnabled(true);
594     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
595     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
596 }
597
598 void BitcoinGUI::gotoReceiveCoinsPage()
599 {
600     receiveCoinsAction->setChecked(true);
601     centralWidget->setCurrentWidget(receiveCoinsPage);
602
603     exportAction->setEnabled(true);
604     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
605     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
606 }
607
608 void BitcoinGUI::gotoSendCoinsPage()
609 {
610     sendCoinsAction->setChecked(true);
611     centralWidget->setCurrentWidget(sendCoinsPage);
612
613     exportAction->setEnabled(false);
614     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
615 }
616
617 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
618 {
619     // Accept only URLs
620     if(event->mimeData()->hasUrls())
621         event->acceptProposedAction();
622 }
623
624 void BitcoinGUI::dropEvent(QDropEvent *event)
625 {
626     if(event->mimeData()->hasUrls())
627     {
628         gotoSendCoinsPage();
629         QList<QUrl> urls = event->mimeData()->urls();
630         foreach(const QUrl &url, urls)
631         {
632             sendCoinsPage->handleURL(&url);
633         }
634     }
635
636     event->acceptProposedAction();
637 }
638
639 void BitcoinGUI::setEncryptionStatus(int status)
640 {
641     switch(status)
642     {
643     case WalletModel::Unencrypted:
644         labelEncryptionIcon->hide();
645         encryptWalletAction->setChecked(false);
646         changePassphraseAction->setEnabled(false);
647         encryptWalletAction->setEnabled(true);
648         break;
649     case WalletModel::Unlocked:
650         labelEncryptionIcon->show();
651         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
652         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
653         encryptWalletAction->setChecked(true);
654         changePassphraseAction->setEnabled(true);
655         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
656         break;
657     case WalletModel::Locked:
658         labelEncryptionIcon->show();
659         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
660         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
661         encryptWalletAction->setChecked(true);
662         changePassphraseAction->setEnabled(true);
663         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
664         break;
665     }
666 }
667
668 void BitcoinGUI::encryptWallet(bool status)
669 {
670     if(!walletModel)
671         return;
672     AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
673                                      AskPassphraseDialog::Decrypt, this);
674     dlg.setModel(walletModel);
675     dlg.exec();
676
677     setEncryptionStatus(walletModel->getEncryptionStatus());
678 }
679
680 void BitcoinGUI::changePassphrase()
681 {
682     AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
683     dlg.setModel(walletModel);
684     dlg.exec();
685 }
686
687 void BitcoinGUI::unlockWallet()
688 {
689     if(!walletModel)
690         return;
691     // Unlock wallet when requested by wallet model
692     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
693     {
694         AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
695         dlg.setModel(walletModel);
696         dlg.exec();
697     }
698 }