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