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