Code cleanup
[novacoin.git] / src / qt / bitcoingui.cpp
1 /*
2  * Qt4 bitcoin GUI.
3  *
4  * W.J. van der Laan 2011-2012
5  * The Bitcoin Developers 2011-2012
6  */
7 #include "bitcoingui.h"
8 #include "transactiontablemodel.h"
9 #include "addressbookpage.h"
10 #include "sendcoinsdialog.h"
11 #include "signverifymessagedialog.h"
12 #include "multisigdialog.h"
13 #include "optionsdialog.h"
14 #include "aboutdialog.h"
15 #include "clientmodel.h"
16 #include "walletmodel.h"
17 #include "editaddressdialog.h"
18 #include "optionsmodel.h"
19 #include "transactiondescdialog.h"
20 #include "addresstablemodel.h"
21 #include "transactionview.h"
22 #include "overviewpage.h"
23 #include "bitcoinunits.h"
24 #include "guiconstants.h"
25 #include "askpassphrasedialog.h"
26 #include "notificator.h"
27 #include "guiutil.h"
28 #include "ui_interface.h"
29 #include "rpcconsole.h"
30 #include "mintingview.h"
31
32 #ifdef Q_OS_MAC
33 #include "macdockiconhandler.h"
34 #endif
35
36 #include <QApplication>
37 #if QT_VERSION < 0x050000
38 #include <QMainWindow>
39 #endif
40 #include <QMenuBar>
41 #include <QMenu>
42 #include <QIcon>
43 #include <QTabWidget>
44 #include <QVBoxLayout>
45 #include <QToolBar>
46 #include <QStatusBar>
47 #include <QLabel>
48 #include <QLineEdit>
49 #include <QPushButton>
50 #include <QLocale>
51 #include <QMessageBox>
52 #include <QProgressBar>
53 #include <QStackedWidget>
54 #include <QDateTime>
55 #include <QMovie>
56 #include <QFileDialog>
57 #if QT_VERSION < 0x050000
58 #include <QDesktopServices>
59 #else
60 #include <QStandardPaths>
61 #endif
62 #include <QTimer>
63 #include <QDragEnterEvent>
64 #if QT_VERSION < 0x050000
65 #include <QUrl>
66 #endif
67 #include <QStyle>
68 #include <QMimeData>
69
70 #include <iostream>
71
72 extern bool fWalletUnlockMintOnly;
73
74 BitcoinGUI::BitcoinGUI(QWidget *parent):
75     QMainWindow(parent),
76     clientModel(0),
77     walletModel(0),
78     encryptWalletAction(0),
79     lockWalletAction(0),
80     unlockWalletAction(0),
81     unlockWalletMiningAction(0),
82     changePassphraseAction(0),
83     aboutQtAction(0),
84     trayIcon(0),
85     notificator(0),
86     rpcConsole(0)
87 {
88     resize(850, 550);
89     setWindowTitle(tr("NovaCoin") + " - " + tr("Wallet"));
90 #ifndef Q_OS_MAC
91     qApp->setWindowIcon(QIcon(":icons/bitcoin"));
92     setWindowIcon(QIcon(":icons/bitcoin"));
93 #else
94     setUnifiedTitleAndToolBarOnMac(true);
95     QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
96 #endif
97     // Accept D&D of URIs
98     setAcceptDrops(true);
99
100     // Create actions for the toolbar, menu bar and tray/dock icon
101     createActions();
102
103     // Create application menu bar
104     createMenuBar();
105
106     // Create the toolbars
107     createToolBars();
108
109     // Create the tray icon (or setup the dock icon)
110     createTrayIcon();
111
112     // Create tabs
113     overviewPage = new OverviewPage();
114
115     transactionsPage = new QWidget(this);
116     QVBoxLayout *vbox = new QVBoxLayout();
117     transactionView = new TransactionView(this);
118     vbox->addWidget(transactionView);
119     transactionsPage->setLayout(vbox);
120
121     mintingPage = new QWidget(this);
122     QVBoxLayout *vboxMinting = new QVBoxLayout();
123     mintingView = new MintingView(this);
124     vboxMinting->addWidget(mintingView);
125     mintingPage->setLayout(vboxMinting);
126
127     addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
128
129     receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
130
131     sendCoinsPage = new SendCoinsDialog(this);
132
133     signVerifyMessageDialog = new SignVerifyMessageDialog(this);
134
135     multisigPage = new MultisigDialog(this);
136
137     centralWidget = new QStackedWidget(this);
138     centralWidget->addWidget(overviewPage);
139     centralWidget->addWidget(transactionsPage);
140     centralWidget->addWidget(mintingPage);
141     centralWidget->addWidget(addressBookPage);
142     centralWidget->addWidget(receiveCoinsPage);
143     centralWidget->addWidget(sendCoinsPage);
144     setCentralWidget(centralWidget);
145
146     // Create status bar
147     statusBar();
148
149     // Status bar notification icons
150     QFrame *frameBlocks = new QFrame();
151     frameBlocks->setContentsMargins(0,0,0,0);
152     frameBlocks->setMinimumWidth(72);
153     frameBlocks->setMaximumWidth(72);
154     QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
155     frameBlocksLayout->setContentsMargins(3,0,3,0);
156     frameBlocksLayout->setSpacing(3);
157     labelEncryptionIcon = new QLabel();
158     labelMiningIcon = new QLabel();
159     labelConnectionsIcon = new QLabel();
160     labelBlocksIcon = new QLabel();
161     frameBlocksLayout->addStretch();
162     frameBlocksLayout->addWidget(labelEncryptionIcon);
163     frameBlocksLayout->addStretch();
164     frameBlocksLayout->addWidget(labelMiningIcon);
165     frameBlocksLayout->addStretch();
166     frameBlocksLayout->addWidget(labelConnectionsIcon);
167     frameBlocksLayout->addStretch();
168     frameBlocksLayout->addWidget(labelBlocksIcon);
169     frameBlocksLayout->addStretch();
170
171     // Progress bar and label for blocks download
172     progressBarLabel = new QLabel();
173     progressBarLabel->setVisible(false);
174     progressBar = new QProgressBar();
175     progressBar->setAlignment(Qt::AlignCenter);
176     progressBar->setVisible(false);
177
178     // Override style sheet for progress bar for styles that have a segmented progress bar,
179     // as they make the text unreadable (workaround for issue #1071)
180     // See https://qt-project.org/doc/qt-4.8/gallery.html
181     QString curStyle = qApp->style()->metaObject()->className();
182     if(curStyle == "QWindowsStyle" || curStyle == "QWindowsXPStyle")
183     {
184         progressBar->setStyleSheet("QProgressBar { background-color: #e8e8e8; border: 1px solid grey; border-radius: 7px; padding: 1px; text-align: center; } QProgressBar::chunk { background: QLinearGradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #FF8000, stop: 1 orange); border-radius: 7px; margin: 0px; }");
185     }
186
187     statusBar()->addWidget(progressBarLabel);
188     statusBar()->addWidget(progressBar);
189     statusBar()->addPermanentWidget(frameBlocks);
190
191     syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
192
193     // Clicking on a transaction on the overview page simply sends you to transaction history page
194     connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
195     connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), transactionView, SLOT(focusTransaction(QModelIndex)));
196
197     // Double-clicking on a transaction on the transaction history page shows details
198     connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
199
200     rpcConsole = new RPCConsole(this);
201     connect(openRPCConsoleAction, SIGNAL(triggered()), rpcConsole, SLOT(show()));
202
203     // Clicking on "Verify Message" in the address book sends you to the verify message tab
204     connect(addressBookPage, SIGNAL(verifyMessage(QString)), this, SLOT(gotoVerifyMessageTab(QString)));
205     // Clicking on "Sign Message" in the receive coins page sends you to the sign message tab
206     connect(receiveCoinsPage, SIGNAL(signMessage(QString)), this, SLOT(gotoSignMessageTab(QString)));
207
208     gotoOverviewPage();
209 }
210
211 BitcoinGUI::~BitcoinGUI()
212 {
213     if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
214         trayIcon->hide();
215 #ifdef Q_OS_MAC
216     delete appMenuBar;
217 #endif
218 }
219
220 void BitcoinGUI::createActions()
221 {
222     QActionGroup *tabGroup = new QActionGroup(this);
223
224     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
225     overviewAction->setToolTip(tr("Show general overview of wallet"));
226     overviewAction->setCheckable(true);
227     overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
228     tabGroup->addAction(overviewAction);
229
230     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
231     sendCoinsAction->setToolTip(tr("Send coins to a NovaCoin address"));
232     sendCoinsAction->setCheckable(true);
233     sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
234     tabGroup->addAction(sendCoinsAction);
235
236     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
237     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
238     receiveCoinsAction->setCheckable(true);
239     receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
240     tabGroup->addAction(receiveCoinsAction);
241
242     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
243     historyAction->setToolTip(tr("Browse transaction history"));
244     historyAction->setCheckable(true);
245     historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
246     tabGroup->addAction(historyAction);
247
248     mintingAction = new QAction(QIcon(":/icons/history"), tr("&Minting"), this);
249     mintingAction->setToolTip(tr("Show your minting capacity"));
250     mintingAction->setCheckable(true);
251     mintingAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
252     tabGroup->addAction(mintingAction);
253
254     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
255     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
256     addressBookAction->setCheckable(true);
257     addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_6));
258     tabGroup->addAction(addressBookAction);
259
260     multisigAction = new QAction(QIcon(":/icons/send"), tr("Multisig"), this);
261     tabGroup->addAction(multisigAction);
262
263     connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
264     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
265     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
266     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
267     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
268     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
269     connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
270     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
271     connect(mintingAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
272     connect(mintingAction, SIGNAL(triggered()), this, SLOT(gotoMintingPage()));
273     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
274     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
275     connect(multisigAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
276     connect(multisigAction, SIGNAL(triggered()), this, SLOT(gotoMultisigPage()));
277
278     quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
279     quitAction->setToolTip(tr("Quit application"));
280     quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
281     quitAction->setMenuRole(QAction::QuitRole);
282     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About NovaCoin"), this);
283     aboutAction->setToolTip(tr("Show information about NovaCoin"));
284     aboutAction->setMenuRole(QAction::AboutRole);
285 #if QT_VERSION < 0x050000
286     aboutQtAction = new QAction(QIcon(":/trolltech/qmessagebox/images/qtlogo-64.png"), tr("About &Qt"), this);
287 #else
288     aboutQtAction = new QAction(QIcon(":/qt-project.org/qmessagebox/images/qtlogo-64.png"), tr("About &Qt"), this);
289 #endif
290     aboutQtAction->setToolTip(tr("Show information about Qt"));
291     aboutQtAction->setMenuRole(QAction::AboutQtRole);
292     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
293     optionsAction->setToolTip(tr("Modify configuration options for NovaCoin"));
294     optionsAction->setMenuRole(QAction::PreferencesRole);
295     toggleHideAction = new QAction(QIcon(":/icons/bitcoin"), tr("&Show / Hide"), this);
296     encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet..."), this);
297     encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
298     encryptWalletAction->setCheckable(true);
299     backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet..."), this);
300     backupWalletAction->setToolTip(tr("Backup wallet to another location"));
301     dumpWalletAction = new QAction(QIcon(":/icons/dump"), tr("&Dump Wallet..."), this);
302     dumpWalletAction->setStatusTip(tr("Dump keys to a text file"));
303     importWalletAction = new QAction(QIcon(":/icons/import"), tr("&Import Wallet..."), this);
304     importWalletAction->setStatusTip(tr("Import keys into a wallet"));
305     changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase..."), this);
306     changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
307     signMessageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message..."), this);
308     verifyMessageAction = new QAction(QIcon(":/icons/transaction_0"), tr("&Verify message..."), this);
309
310     lockWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Lock wallet"), this);
311     lockWalletAction->setToolTip(tr("Lock wallet"));
312     lockWalletAction->setCheckable(true);
313
314     unlockWalletAction = new QAction(QIcon(":/icons/lock_open"), tr("Unlo&ck wallet"), this);
315     unlockWalletAction->setToolTip(tr("Unlock wallet"));
316     unlockWalletAction->setCheckable(true);
317
318     unlockWalletMiningAction = new QAction(QIcon(":/icons/mining_active"), tr("Unlo&ck wallet for mining"), this);
319     unlockWalletMiningAction->setToolTip(tr("Unlock wallet for mining"));
320     unlockWalletMiningAction->setCheckable(true);
321
322     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
323     exportAction->setToolTip(tr("Export the data in the current tab to a file"));
324     openRPCConsoleAction = new QAction(QIcon(":/icons/debugwindow"), tr("&Debug window"), this);
325     openRPCConsoleAction->setToolTip(tr("Open debugging and diagnostic console"));
326
327     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
328     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
329     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
330     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
331     connect(toggleHideAction, SIGNAL(triggered()), this, SLOT(toggleHidden()));
332     connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
333     connect(lockWalletAction, SIGNAL(triggered(bool)), this, SLOT(lockWallet()));
334     connect(unlockWalletAction, SIGNAL(triggered(bool)), this, SLOT(unlockWallet()));
335     connect(unlockWalletMiningAction, SIGNAL(triggered(bool)), this, SLOT(unlockWalletMining(bool)));
336     connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
337     connect(dumpWalletAction, SIGNAL(triggered()), this, SLOT(dumpWallet()));
338     connect(importWalletAction, SIGNAL(triggered()), this, SLOT(importWallet()));
339     connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
340     connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
341     connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));
342 }
343
344 void BitcoinGUI::createMenuBar()
345 {
346 #ifdef Q_OS_MAC
347     // Create a decoupled menu bar on Mac which stays even if the window is closed
348     appMenuBar = new QMenuBar();
349 #else
350     // Get the main window's menu bar on other platforms
351     appMenuBar = menuBar();
352 #endif
353
354     // Configure the menus
355     QMenu *file = appMenuBar->addMenu(tr("&File"));
356     file->addAction(backupWalletAction);
357     file->addSeparator();
358     file->addAction(dumpWalletAction);
359     file->addAction(importWalletAction);
360     file->addAction(exportAction);
361     file->addAction(signMessageAction);
362     file->addAction(verifyMessageAction);
363     file->addAction(multisigAction);
364     file->addSeparator();
365     file->addAction(quitAction);
366
367     QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
368     QMenu *securityMenu = settings->addMenu(QIcon(":/icons/key"), tr("&Wallet security"));
369     securityMenu->addAction(encryptWalletAction);
370     securityMenu->addAction(changePassphraseAction);
371     securityMenu->addAction(unlockWalletAction);
372     securityMenu->addAction(unlockWalletMiningAction);
373     securityMenu->addAction(lockWalletAction);
374     settings->addAction(optionsAction);
375
376     QMenu *help = appMenuBar->addMenu(tr("&Help"));
377     help->addAction(openRPCConsoleAction);
378     help->addSeparator();
379     help->addAction(aboutAction);
380     help->addAction(aboutQtAction);
381 }
382
383 void BitcoinGUI::createToolBars()
384 {
385     QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
386     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
387     toolbar->addAction(overviewAction);
388     toolbar->addAction(sendCoinsAction);
389     toolbar->addAction(receiveCoinsAction);
390     toolbar->addAction(historyAction);
391     toolbar->addAction(mintingAction);
392     toolbar->addAction(addressBookAction);
393
394     QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
395     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
396     toolbar2->addAction(exportAction);
397     toolbar2->setVisible(false);
398     
399 }
400
401 void BitcoinGUI::setClientModel(ClientModel *clientModel)
402 {
403     this->clientModel = clientModel;
404     if(clientModel)
405     {
406         // Replace some strings and icons, when using the testnet
407         if(clientModel->isTestNet())
408         {
409             setWindowTitle(windowTitle() + QString(" ") + tr("[testnet]"));
410 #ifndef Q_OS_MAC
411             qApp->setWindowIcon(QIcon(":icons/bitcoin_testnet"));
412             setWindowIcon(QIcon(":icons/bitcoin_testnet"));
413 #else
414             MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
415 #endif
416             if(trayIcon)
417             {
418                 trayIcon->setToolTip(tr("NovaCoin client") + QString(" ") + tr("[testnet]"));
419                 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
420                 toggleHideAction->setIcon(QIcon(":/icons/toolbar_testnet"));
421             }
422
423             aboutAction->setIcon(QIcon(":/icons/toolbar_testnet"));
424         }
425
426         // Keep up to date with client
427         setNumConnections(clientModel->getNumConnections());
428         connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
429
430         setNumBlocks(clientModel->getNumBlocks(), clientModel->getNumBlocksOfPeers());
431         connect(clientModel, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
432
433         QTimer *timer = new QTimer(this);
434         connect(timer, SIGNAL(timeout()), this, SLOT(updateMining()));
435         timer->start(10*1000); //10 seconds
436
437         // Report errors from network/worker thread
438         connect(clientModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
439
440         rpcConsole->setClientModel(clientModel);
441         addressBookPage->setOptionsModel(clientModel->getOptionsModel());
442         receiveCoinsPage->setOptionsModel(clientModel->getOptionsModel());
443     }
444 }
445
446 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
447 {
448     this->walletModel = walletModel;
449     if(walletModel)
450     {
451         // Report errors from wallet thread
452         connect(walletModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
453
454         // Put transaction list in tabs
455         transactionView->setModel(walletModel);
456         mintingView->setModel(walletModel);
457
458         overviewPage->setModel(walletModel);
459         addressBookPage->setModel(walletModel->getAddressTableModel());
460         receiveCoinsPage->setModel(walletModel->getAddressTableModel());
461         sendCoinsPage->setModel(walletModel);
462         signVerifyMessageDialog->setModel(walletModel);
463         multisigPage->setModel(walletModel);
464
465         setEncryptionStatus(walletModel->getEncryptionStatus());
466         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
467         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(updateMining()));
468
469         // Balloon pop-up for new transaction
470         connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
471                 this, SLOT(incomingTransaction(QModelIndex,int,int)));
472
473         // Ask for passphrase if needed
474         connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
475     }
476 }
477
478 void BitcoinGUI::createTrayIcon()
479 {
480     QMenu *trayIconMenu;
481 #ifndef Q_OS_MAC
482     trayIcon = new QSystemTrayIcon(this);
483     trayIconMenu = new QMenu(this);
484     trayIcon->setContextMenu(trayIconMenu);
485     trayIcon->setToolTip(tr("NovaCoin client"));
486     trayIcon->setIcon(QIcon(":/icons/toolbar"));
487     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
488             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
489     trayIcon->show();
490 #else
491     // Note: On Mac, the dock icon is used to provide the tray's functionality.
492     MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
493     dockIconHandler->setMainWindow((QMainWindow *)this);
494     trayIconMenu = dockIconHandler->dockMenu();
495 #endif
496
497     // Configuration of the tray icon (or dock icon) icon menu
498     trayIconMenu->addAction(toggleHideAction);
499     trayIconMenu->addSeparator();
500     trayIconMenu->addAction(sendCoinsAction);
501     trayIconMenu->addAction(multisigAction);
502     trayIconMenu->addAction(receiveCoinsAction);
503     trayIconMenu->addSeparator();
504     trayIconMenu->addAction(signMessageAction);
505     trayIconMenu->addAction(verifyMessageAction);
506     trayIconMenu->addSeparator();
507     trayIconMenu->addAction(optionsAction);
508     trayIconMenu->addAction(openRPCConsoleAction);
509 #ifndef Q_OS_MAC
510     // This is built-in on Mac
511     trayIconMenu->addSeparator();
512     trayIconMenu->addAction(quitAction);    
513 #endif
514     notificator = new Notificator(QApplication::applicationName(), trayIcon, this);
515 }
516
517 #ifndef Q_OS_MAC
518 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
519 {
520     if(reason == QSystemTrayIcon::Trigger)
521     {
522         // Click on system tray icon triggers show/hide of the main window
523         toggleHideAction->trigger();
524     }
525 }
526 #endif
527
528 void BitcoinGUI::optionsClicked()
529 {
530     if(!clientModel || !clientModel->getOptionsModel())
531         return;
532     OptionsDialog dlg;
533     dlg.setModel(clientModel->getOptionsModel());
534     dlg.exec();
535 }
536
537 void BitcoinGUI::aboutClicked()
538 {
539     AboutDialog dlg;
540     dlg.setModel(clientModel);
541     dlg.exec();
542 }
543
544 void BitcoinGUI::setNumConnections(int count)
545 {
546     QString icon;
547     switch(count)
548     {
549     case 0: icon = ":/icons/connect_0"; break;
550     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
551     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
552     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
553     default: icon = ":/icons/connect_4"; break;
554     }
555     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
556     labelConnectionsIcon->setToolTip(tr("%n active connection(s) to NovaCoin network", "", count));
557 }
558
559 void BitcoinGUI::setNumBlocks(int count, int nTotalBlocks)
560 {
561     // don't show / hide progress bar and its label if we have no connection to the network
562     if (!clientModel || clientModel->getNumConnections() == 0)
563     {
564         progressBarLabel->setVisible(false);
565         progressBar->setVisible(false);
566
567         return;
568     }
569
570     QString strStatusBarWarnings = clientModel->getStatusBarWarnings();
571     QString tooltip;
572
573     if(count < nTotalBlocks)
574     {
575         int nRemainingBlocks = nTotalBlocks - count;
576         float nPercentageDone = count / (nTotalBlocks * 0.01f);
577
578         if (strStatusBarWarnings.isEmpty())
579         {
580             progressBarLabel->setText(tr("Synchronizing with network..."));
581             progressBarLabel->setVisible(true);
582             progressBar->setFormat(tr("~%n block(s) remaining", "", nRemainingBlocks));
583             progressBar->setMaximum(nTotalBlocks);
584             progressBar->setValue(count);
585             progressBar->setVisible(true);
586         }
587
588         tooltip = tr("Downloaded %1 of %2 blocks of transaction history (%3% done).").arg(count).arg(nTotalBlocks).arg(nPercentageDone, 0, 'f', 2);
589     }
590     else
591     {
592         if (strStatusBarWarnings.isEmpty())
593             progressBarLabel->setVisible(false);
594
595         progressBar->setVisible(false);
596         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
597     }
598
599     // Override progressBarLabel text and hide progress bar, when we have warnings to display
600     if (!strStatusBarWarnings.isEmpty())
601     {
602         progressBarLabel->setText(strStatusBarWarnings);
603         progressBarLabel->setVisible(true);
604         progressBar->setVisible(false);
605     }
606
607     QDateTime lastBlockDate = clientModel->getLastBlockDate();
608     int secs = lastBlockDate.secsTo(QDateTime::currentDateTime());
609     QString text;
610
611     // Represent time from last generated block in human readable text
612     if(secs <= 0)
613     {
614         // Fully up to date. Leave text empty.
615     }
616     else if(secs < 60)
617     {
618         text = tr("%n second(s) ago","",secs);
619     }
620     else if(secs < 60*60)
621     {
622         text = tr("%n minute(s) ago","",secs/60);
623     }
624     else if(secs < 24*60*60)
625     {
626         text = tr("%n hour(s) ago","",secs/(60*60));
627     }
628     else
629     {
630         text = tr("%n day(s) ago","",secs/(60*60*24));
631     }
632
633     // Set icon state: spinning if catching up, tick otherwise
634     if(secs < 90*60 && count >= nTotalBlocks)
635     {
636         tooltip = tr("Up to date") + QString(".<br>") + tooltip;
637         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
638
639         overviewPage->showOutOfSyncWarning(false);
640     }
641     else
642     {
643         tooltip = tr("Catching up...") + QString("<br>") + tooltip;
644         labelBlocksIcon->setMovie(syncIconMovie);
645         syncIconMovie->start();
646
647         overviewPage->showOutOfSyncWarning(true);
648     }
649
650     if(!text.isEmpty())
651     {
652         tooltip += QString("<br>");
653         tooltip += tr("Last received block was generated %1.").arg(text);
654     }
655
656     // Don't word-wrap this (fixed-width) tooltip
657     tooltip = QString("<nobr>") + tooltip + QString("</nobr>");
658
659     labelBlocksIcon->setToolTip(tooltip);
660     progressBarLabel->setToolTip(tooltip);
661     progressBar->setToolTip(tooltip);
662 }
663
664 void BitcoinGUI::updateMining()
665 {
666    if(!walletModel)
667       return;
668
669     labelMiningIcon->setPixmap(QIcon(":/icons/mining_inactive").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
670
671     if (!clientModel->getNumConnections())
672     {
673         labelMiningIcon->setToolTip(tr("Wallet is offline"));
674         return;
675     }
676
677     if (walletModel->getEncryptionStatus() == WalletModel::Locked)
678     {
679         labelMiningIcon->setToolTip(tr("Wallet is locked"));
680         return;
681     }
682
683     if (clientModel->inInitialBlockDownload() || clientModel->getNumBlocksOfPeers() > clientModel->getNumBlocks())
684     {
685         labelMiningIcon->setToolTip(tr("Blockchain download is in progress"));
686         return;
687     }
688
689     float nKernelsRate = 0, nCoinDaysRate = 0;
690     walletModel->getStakeStats(nKernelsRate, nCoinDaysRate);
691
692     if (nKernelsRate > 0)
693     {
694         labelMiningIcon->setPixmap(QIcon(":/icons/mining_active").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
695
696         uint64_t nNetworkWeight = clientModel->getPoSKernelPS();
697 /*
698         double dDifficulty = clientModel->getDifficulty(true);
699         QString msg;
700
701         int nApproxTime = 4294967297 * dDifficulty / nTotalWeight;
702
703         if (nApproxTime < 60)
704             msg = tr("%n second(s)", "", nApproxTime);
705         else if (nApproxTime < 60*60)
706             msg = tr("%n minute(s)", "", nApproxTime / 60);
707         else if (nApproxTime < 24*60*60)
708             msg = tr("%n hour(s)", "", nApproxTime / 3600);
709         else
710             msg = tr("%n day(s)", "", nApproxTime / 86400);
711
712         labelMiningIcon->setToolTip(tr("Stake miner is active\nYour current stake weight is %1\nNetwork weight is %2\nAverage block generation time is %3").arg(nTotalWeight).arg(dNetworkWeight).arg(msg));
713 */
714
715         labelMiningIcon->setToolTip(QString("<nobr>")+tr("Stake miner is active<br>Kernel rate is %1 k/s<br>CD rate is %2 CD/s<br>Network weight is %3").arg(nKernelsRate).arg(nCoinDaysRate).arg(nNetworkWeight)+QString("<\nobr>"));
716     }
717     else
718         labelMiningIcon->setToolTip(tr("No suitable inputs were found"));
719 }
720
721 void BitcoinGUI::error(const QString &title, const QString &message, bool modal)
722 {
723     // Report errors from network/worker thread
724     if(modal)
725     {
726         QMessageBox::critical(this, title, message, QMessageBox::Ok, QMessageBox::Ok);
727     } else {
728         notificator->notify(Notificator::Critical, title, message);
729     }
730 }
731
732 void BitcoinGUI::message(const QString &title, const QString &message, unsigned int style, const QString &detail)
733 {
734     QString strTitle = tr("NovaCoin") + " - ";
735     // Default to information icon
736     int nMBoxIcon = QMessageBox::Information;
737     int nNotifyIcon = Notificator::Information;
738
739
740     // Check for usage of predefined title
741     switch (style) {
742     case CClientUIInterface::MSG_ERROR:
743         strTitle += tr("Error");
744         break;
745     case CClientUIInterface::MSG_WARNING:
746         strTitle += tr("Warning");
747         break;
748     case CClientUIInterface::MSG_INFORMATION:
749         strTitle += tr("Information");
750         break;
751     default:
752         strTitle += title; // Use supplied title
753     }
754
755     // Check for error/warning icon
756     if (style & CClientUIInterface::ICON_ERROR) {
757         nMBoxIcon = QMessageBox::Critical;
758         nNotifyIcon = Notificator::Critical;
759     }
760     else if (style & CClientUIInterface::ICON_WARNING) {
761         nMBoxIcon = QMessageBox::Warning;
762         nNotifyIcon = Notificator::Warning;
763     }
764
765     // Display message
766     if (style & CClientUIInterface::MODAL) {
767         // Check for buttons, use OK as default, if none was supplied
768         QMessageBox::StandardButton buttons;
769         buttons = QMessageBox::Ok;
770
771         QMessageBox mBox((QMessageBox::Icon)nMBoxIcon, strTitle, message, buttons);
772
773         if(!detail.isEmpty()) { mBox.setDetailedText(detail); }
774
775         mBox.exec();
776     }
777     else
778         notificator->notify((Notificator::Class)nNotifyIcon, strTitle, message);
779 }
780
781
782 void BitcoinGUI::changeEvent(QEvent *e)
783 {
784     QMainWindow::changeEvent(e);
785 #ifndef Q_OS_MAC // Ignored on Mac
786     if(e->type() == QEvent::WindowStateChange)
787     {
788         if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
789         {
790             QWindowStateChangeEvent *wsevt = static_cast<QWindowStateChangeEvent*>(e);
791             if(!(wsevt->oldState() & Qt::WindowMinimized) && isMinimized())
792             {
793                 QTimer::singleShot(0, this, SLOT(hide()));
794                 e->ignore();
795             }
796         }
797     }
798 #endif
799 }
800
801 void BitcoinGUI::closeEvent(QCloseEvent *event)
802 {
803     if(clientModel)
804     {
805 #ifndef Q_OS_MAC // Ignored on Mac
806         if(!clientModel->getOptionsModel()->getMinimizeOnClose())
807         {
808             qApp->quit();
809         }
810 #endif
811     }
812     QMainWindow::closeEvent(event);
813 }
814
815 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
816 {
817     QString strMessage =
818         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
819           "which goes to the nodes that process your transaction and helps to support the network.  "
820           "Do you want to pay the fee?").arg(
821                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
822     QMessageBox::StandardButton retval = QMessageBox::question(
823           this, tr("Confirm transaction fee"), strMessage,
824           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
825     *payFee = (retval == QMessageBox::Yes);
826 }
827
828 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
829 {
830     if(!walletModel || !clientModel)
831         return;
832     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
833     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
834                     .data(Qt::EditRole).toULongLong();
835     if(!clientModel->inInitialBlockDownload())
836     {
837         // On new transaction, make an info balloon
838         // Unless the initial block download is in progress, to prevent balloon-spam
839         QString date = ttm->index(start, TransactionTableModel::Date, parent)
840                         .data().toString();
841         QString type = ttm->index(start, TransactionTableModel::Type, parent)
842                         .data().toString();
843         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
844                         .data().toString();
845         QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
846                             TransactionTableModel::ToAddress, parent)
847                         .data(Qt::DecorationRole));
848
849         notificator->notify(Notificator::Information,
850                             (amount)<0 ? tr("Sent transaction") :
851                                          tr("Incoming transaction"),
852                               tr("Date: %1\n"
853                                  "Amount: %2\n"
854                                  "Type: %3\n"
855                                  "Address: %4\n")
856                               .arg(date)
857                               .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
858                               .arg(type)
859                               .arg(address), icon);
860     }
861 }
862
863 void BitcoinGUI::gotoOverviewPage()
864 {
865     overviewAction->setChecked(true);
866     centralWidget->setCurrentWidget(overviewPage);
867
868     exportAction->setEnabled(false);
869     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
870 }
871
872 void BitcoinGUI::gotoHistoryPage()
873 {
874     historyAction->setChecked(true);
875     centralWidget->setCurrentWidget(transactionsPage);
876
877     exportAction->setEnabled(true);
878     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
879     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
880 }
881
882 void BitcoinGUI::gotoMintingPage()
883 {
884     mintingAction->setChecked(true);
885     centralWidget->setCurrentWidget(mintingPage);
886
887     exportAction->setEnabled(true);
888     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
889     connect(exportAction, SIGNAL(triggered()), mintingView, SLOT(exportClicked()));
890 }
891
892
893 void BitcoinGUI::gotoAddressBookPage()
894 {
895     addressBookAction->setChecked(true);
896     centralWidget->setCurrentWidget(addressBookPage);
897
898     exportAction->setEnabled(true);
899     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
900     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
901 }
902
903 void BitcoinGUI::gotoReceiveCoinsPage()
904 {
905     receiveCoinsAction->setChecked(true);
906     centralWidget->setCurrentWidget(receiveCoinsPage);
907
908     exportAction->setEnabled(true);
909     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
910     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
911 }
912
913 void BitcoinGUI::gotoSendCoinsPage()
914 {
915     sendCoinsAction->setChecked(true);
916     centralWidget->setCurrentWidget(sendCoinsPage);
917
918     exportAction->setEnabled(false);
919     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
920 }
921
922 void BitcoinGUI::gotoSignMessageTab(QString addr)
923 {
924     // call show() in showTab_SM()
925     signVerifyMessageDialog->showTab_SM(true);
926
927     if(!addr.isEmpty())
928         signVerifyMessageDialog->setAddress_SM(addr);
929 }
930
931 void BitcoinGUI::gotoVerifyMessageTab(QString addr)
932 {
933     // call show() in showTab_VM()
934     signVerifyMessageDialog->showTab_VM(true);
935
936     if(!addr.isEmpty())
937         signVerifyMessageDialog->setAddress_VM(addr);
938 }
939
940 void BitcoinGUI::gotoMultisigPage()
941 {
942     multisigPage->show();
943     multisigPage->setFocus();
944 }
945
946 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
947 {
948     // Accept only URIs
949     if(event->mimeData()->hasUrls())
950         event->acceptProposedAction();
951 }
952
953 void BitcoinGUI::dropEvent(QDropEvent *event)
954 {
955     if(event->mimeData()->hasUrls())
956     {
957         int nValidUrisFound = 0;
958         QList<QUrl> uris = event->mimeData()->urls();
959         foreach(const QUrl &uri, uris)
960         {
961             if (sendCoinsPage->handleURI(uri.toString()))
962                 nValidUrisFound++;
963         }
964
965         // if valid URIs were found
966         if (nValidUrisFound)
967             gotoSendCoinsPage();
968         else
969             notificator->notify(Notificator::Warning, tr("URI handling"), tr("URI can not be parsed! This can be caused by an invalid NovaCoin address or malformed URI parameters."));
970     }
971
972     event->acceptProposedAction();
973 }
974
975 void BitcoinGUI::handleURI(QString strURI)
976 {
977     // URI has to be valid
978     if (sendCoinsPage->handleURI(strURI))
979     {
980         showNormalIfMinimized();
981         gotoSendCoinsPage();
982     }
983     else
984         notificator->notify(Notificator::Warning, tr("URI handling"), tr("URI can not be parsed! This can be caused by an invalid NovaCoin address or malformed URI parameters."));
985 }
986
987 void BitcoinGUI::setEncryptionStatus(int status)
988 {
989     switch(status)
990     {
991     case WalletModel::Unencrypted:
992         labelEncryptionIcon->hide();
993         encryptWalletAction->setChecked(false);
994         changePassphraseAction->setEnabled(false);
995         lockWalletAction->setEnabled(false);
996         unlockWalletAction->setEnabled(false);
997         unlockWalletMiningAction->setEnabled(false);
998         encryptWalletAction->setEnabled(true);
999         break;
1000     case WalletModel::Unlocked:
1001         labelEncryptionIcon->show();
1002         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
1003         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
1004         encryptWalletAction->setChecked(true);
1005         changePassphraseAction->setEnabled(true);
1006         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
1007
1008         lockWalletAction->setEnabled(true);
1009         lockWalletAction->setChecked(false);
1010         unlockWalletAction->setEnabled(false);
1011         unlockWalletMiningAction->setEnabled(false);
1012
1013         if (fWalletUnlockMintOnly)
1014             unlockWalletMiningAction->setChecked(true);
1015         else
1016             unlockWalletAction->setChecked(true);
1017
1018         break;
1019     case WalletModel::Locked:
1020         labelEncryptionIcon->show();
1021         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
1022         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
1023         encryptWalletAction->setChecked(true);
1024         changePassphraseAction->setEnabled(true);
1025         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
1026
1027         lockWalletAction->setChecked(true);
1028         unlockWalletAction->setChecked(false);
1029         unlockWalletMiningAction->setChecked(false);
1030
1031         lockWalletAction->setEnabled(false);
1032         unlockWalletAction->setEnabled(true);
1033         unlockWalletMiningAction->setEnabled(true);
1034         break;
1035     }
1036 }
1037
1038 void BitcoinGUI::encryptWallet(bool status)
1039 {
1040     if(!walletModel)
1041         return;
1042     AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
1043                                      AskPassphraseDialog::Decrypt, this);
1044     dlg.setModel(walletModel);
1045     dlg.exec();
1046
1047     setEncryptionStatus(walletModel->getEncryptionStatus());
1048 }
1049
1050 void BitcoinGUI::unlockWalletMining(bool status)
1051 {
1052     if(!walletModel)
1053         return;
1054
1055     // Unlock wallet when requested by wallet model
1056     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
1057     {
1058         AskPassphraseDialog dlg(AskPassphraseDialog::UnlockMining, this);
1059         dlg.setModel(walletModel);
1060         dlg.exec();
1061     }
1062 }
1063
1064 void BitcoinGUI::backupWallet()
1065 {
1066 #if QT_VERSION < 0x050000
1067     QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
1068 #else
1069     QString saveDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
1070 #endif
1071     QString filename = QFileDialog::getSaveFileName(this, tr("Backup Wallet"), saveDir, tr("Wallet Data (*.dat)"));
1072     if(!filename.isEmpty()) {
1073         if(!walletModel->backupWallet(filename)) {
1074             QMessageBox::warning(this, tr("Backup Failed"), tr("There was an error trying to save the wallet data to the new location."));
1075         }
1076     }
1077 }
1078
1079 void BitcoinGUI::dumpWallet()
1080 {
1081    if(!walletModel)
1082       return;
1083
1084    WalletModel::UnlockContext ctx(walletModel->requestUnlock());
1085    if(!ctx.isValid())
1086    {
1087        // Unlock wallet failed or was cancelled
1088        return;
1089    }
1090
1091 #if QT_VERSION < 0x050000
1092     QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
1093 #else
1094     QString saveDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
1095 #endif
1096     QString filename = QFileDialog::getSaveFileName(this, tr("Dump Wallet"), saveDir, tr("Wallet dump (*.txt)"));
1097     if(!filename.isEmpty()) {
1098         if(!walletModel->dumpWallet(filename)) {
1099             error(tr("Dump failed"),
1100                          tr("An error happened while trying to save the keys to your location.\n"
1101                             "Keys were not saved.")
1102                       ,CClientUIInterface::MSG_ERROR);
1103         }
1104         else
1105           message(tr("Dump successful"),
1106                        tr("Keys were saved to this file:\n%2")
1107                        .arg(filename)
1108                       ,CClientUIInterface::MSG_INFORMATION);
1109     }
1110 }
1111
1112 void BitcoinGUI::importWallet()
1113 {
1114    if(!walletModel)
1115       return;
1116
1117    WalletModel::UnlockContext ctx(walletModel->requestUnlock());
1118    if(!ctx.isValid())
1119    {
1120        // Unlock wallet failed or was cancelled
1121        return;
1122    }
1123
1124 #if QT_VERSION < 0x050000
1125     QString openDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
1126 #else
1127     QString openDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
1128 #endif
1129     QString filename = QFileDialog::getOpenFileName(this, tr("Import Wallet"), openDir, tr("Wallet dump (*.txt)"));
1130     if(!filename.isEmpty()) {
1131         if(!walletModel->importWallet(filename)) {
1132             error(tr("Import Failed"),
1133                          tr("An error happened while trying to import the keys.\n"
1134                             "Some or all keys from:\n %1,\n were not imported into your wallet.")
1135                          .arg(filename)
1136                       ,CClientUIInterface::MSG_ERROR);
1137         }
1138         else
1139           message(tr("Import Successful"),
1140                        tr("All keys from:\n %1,\n were imported into your wallet.")
1141                        .arg(filename)
1142                       ,CClientUIInterface::MSG_INFORMATION);
1143     }
1144 }
1145
1146
1147 void BitcoinGUI::changePassphrase()
1148 {
1149     AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
1150     dlg.setModel(walletModel);
1151     dlg.exec();
1152 }
1153
1154 void BitcoinGUI::unlockWallet()
1155 {
1156     if(!walletModel)
1157         return;
1158     // Unlock wallet when requested by wallet model
1159     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
1160     {
1161         AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
1162         dlg.setModel(walletModel);
1163         dlg.exec();
1164     }
1165 }
1166
1167 void BitcoinGUI::lockWallet()
1168 {
1169     if(!walletModel)
1170         return;
1171
1172     walletModel->setWalletLocked(true);
1173 }
1174
1175 void BitcoinGUI::showNormalIfMinimized(bool fToggleHidden)
1176 {
1177     // activateWindow() (sometimes) helps with keyboard focus on Windows
1178     if (isHidden())
1179     {
1180         // Make sure the window is not minimized
1181         setWindowState(windowState() & (~Qt::WindowMinimized | Qt::WindowActive));
1182         // Then show it
1183         show();
1184         raise();
1185         activateWindow();
1186     }
1187     else if (isMinimized())
1188     {
1189         showNormal();
1190         raise();
1191         activateWindow();
1192     }
1193     else if (GUIUtil::isObscured(this))
1194     {
1195         raise();
1196         activateWindow();
1197         if(fToggleHidden)
1198         {
1199             Sleep(1);
1200             if (GUIUtil::isObscured(this))
1201                 hide();
1202         }
1203     }
1204     else if(fToggleHidden)
1205         hide();
1206 }
1207
1208 void BitcoinGUI::toggleHidden()
1209 {
1210     showNormalIfMinimized(true);
1211 }