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