the RPC Console should be a QWidget...
[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(0);
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     delete rpcConsole;
220 }
221
222 void BitcoinGUI::createActions()
223 {
224     QActionGroup *tabGroup = new QActionGroup(this);
225
226     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
227     overviewAction->setToolTip(tr("Show general overview of wallet"));
228     overviewAction->setCheckable(true);
229     overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
230     tabGroup->addAction(overviewAction);
231
232     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
233     sendCoinsAction->setToolTip(tr("Send coins to a NovaCoin address"));
234     sendCoinsAction->setCheckable(true);
235     sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
236     tabGroup->addAction(sendCoinsAction);
237
238     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
239     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
240     receiveCoinsAction->setCheckable(true);
241     receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
242     tabGroup->addAction(receiveCoinsAction);
243
244     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
245     historyAction->setToolTip(tr("Browse transaction history"));
246     historyAction->setCheckable(true);
247     historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
248     tabGroup->addAction(historyAction);
249
250     mintingAction = new QAction(QIcon(":/icons/history"), tr("&Minting"), this);
251     mintingAction->setToolTip(tr("Show your minting capacity"));
252     mintingAction->setCheckable(true);
253     mintingAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
254     tabGroup->addAction(mintingAction);
255
256     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
257     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
258     addressBookAction->setCheckable(true);
259     addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_6));
260     tabGroup->addAction(addressBookAction);
261
262     multisigAction = new QAction(QIcon(":/icons/send"), tr("Multisig"), this);
263     tabGroup->addAction(multisigAction);
264
265     connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
266     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
267     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
268     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
269     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
270     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
271     connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
272     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
273     connect(mintingAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
274     connect(mintingAction, SIGNAL(triggered()), this, SLOT(gotoMintingPage()));
275     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
276     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
277     connect(multisigAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
278     connect(multisigAction, SIGNAL(triggered()), this, SLOT(gotoMultisigPage()));
279
280     quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
281     quitAction->setToolTip(tr("Quit application"));
282     quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
283     quitAction->setMenuRole(QAction::QuitRole);
284     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About NovaCoin"), this);
285     aboutAction->setToolTip(tr("Show information about NovaCoin"));
286     aboutAction->setMenuRole(QAction::AboutRole);
287 #if QT_VERSION < 0x050000
288     aboutQtAction = new QAction(QIcon(":/trolltech/qmessagebox/images/qtlogo-64.png"), tr("About &Qt"), this);
289 #else
290     aboutQtAction = new QAction(QIcon(":/qt-project.org/qmessagebox/images/qtlogo-64.png"), tr("About &Qt"), this);
291 #endif
292     aboutQtAction->setToolTip(tr("Show information about Qt"));
293     aboutQtAction->setMenuRole(QAction::AboutQtRole);
294     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
295     optionsAction->setToolTip(tr("Modify configuration options for NovaCoin"));
296     optionsAction->setMenuRole(QAction::PreferencesRole);
297     toggleHideAction = new QAction(QIcon(":/icons/bitcoin"), tr("&Show / Hide"), this);
298     encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet..."), this);
299     encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
300     encryptWalletAction->setCheckable(true);
301     backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet..."), this);
302     backupWalletAction->setToolTip(tr("Backup wallet to another location"));
303     dumpWalletAction = new QAction(QIcon(":/icons/dump"), tr("&Dump Wallet..."), this);
304     dumpWalletAction->setStatusTip(tr("Dump keys to a text file"));
305     importWalletAction = new QAction(QIcon(":/icons/import"), tr("&Import Wallet..."), this);
306     importWalletAction->setStatusTip(tr("Import keys into a wallet"));
307     changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase..."), this);
308     changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
309     signMessageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message..."), this);
310     verifyMessageAction = new QAction(QIcon(":/icons/transaction_0"), tr("&Verify message..."), this);
311
312     lockWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Lock wallet"), this);
313     lockWalletAction->setToolTip(tr("Lock wallet"));
314     lockWalletAction->setCheckable(true);
315
316     unlockWalletAction = new QAction(QIcon(":/icons/lock_open"), tr("Unlo&ck wallet"), this);
317     unlockWalletAction->setToolTip(tr("Unlock wallet"));
318     unlockWalletAction->setCheckable(true);
319
320     unlockWalletMiningAction = new QAction(QIcon(":/icons/mining_active"), tr("Unlo&ck wallet for mining"), this);
321     unlockWalletMiningAction->setToolTip(tr("Unlock wallet for mining"));
322     unlockWalletMiningAction->setCheckable(true);
323
324     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
325     exportAction->setToolTip(tr("Export the data in the current tab to a file"));
326     openRPCConsoleAction = new QAction(QIcon(":/icons/debugwindow"), tr("&Debug window"), this);
327     openRPCConsoleAction->setToolTip(tr("Open debugging and diagnostic console"));
328
329     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
330     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
331     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
332     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
333     connect(toggleHideAction, SIGNAL(triggered()), this, SLOT(toggleHidden()));
334     connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
335     connect(lockWalletAction, SIGNAL(triggered(bool)), this, SLOT(lockWallet()));
336     connect(unlockWalletAction, SIGNAL(triggered(bool)), this, SLOT(unlockWallet()));
337     connect(unlockWalletMiningAction, SIGNAL(triggered(bool)), this, SLOT(unlockWalletMining(bool)));
338     connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
339     connect(dumpWalletAction, SIGNAL(triggered()), this, SLOT(dumpWallet()));
340     connect(importWalletAction, SIGNAL(triggered()), this, SLOT(importWallet()));
341     connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
342     connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
343     connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));
344 }
345
346 void BitcoinGUI::createMenuBar()
347 {
348 #ifdef Q_OS_MAC
349     // Create a decoupled menu bar on Mac which stays even if the window is closed
350     appMenuBar = new QMenuBar();
351 #else
352     // Get the main window's menu bar on other platforms
353     appMenuBar = menuBar();
354 #endif
355
356     // Configure the menus
357     QMenu *file = appMenuBar->addMenu(tr("&File"));
358     file->addAction(backupWalletAction);
359     file->addSeparator();
360     file->addAction(dumpWalletAction);
361     file->addAction(importWalletAction);
362     file->addAction(exportAction);
363     file->addAction(signMessageAction);
364     file->addAction(verifyMessageAction);
365     file->addAction(multisigAction);
366     file->addSeparator();
367     file->addAction(quitAction);
368
369     QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
370     QMenu *securityMenu = settings->addMenu(QIcon(":/icons/key"), tr("&Wallet security"));
371     securityMenu->addAction(encryptWalletAction);
372     securityMenu->addAction(changePassphraseAction);
373     securityMenu->addAction(unlockWalletAction);
374     securityMenu->addAction(unlockWalletMiningAction);
375     securityMenu->addAction(lockWalletAction);
376     settings->addAction(optionsAction);
377
378     QMenu *help = appMenuBar->addMenu(tr("&Help"));
379     help->addAction(openRPCConsoleAction);
380     help->addSeparator();
381     help->addAction(aboutAction);
382     help->addAction(aboutQtAction);
383 }
384
385 void BitcoinGUI::createToolBars()
386 {
387     QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
388     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
389     toolbar->addAction(overviewAction);
390     toolbar->addAction(sendCoinsAction);
391     toolbar->addAction(receiveCoinsAction);
392     toolbar->addAction(historyAction);
393     toolbar->addAction(mintingAction);
394     toolbar->addAction(addressBookAction);
395
396     QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
397     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
398     toolbar2->addAction(exportAction);
399     toolbar2->setVisible(false);
400     
401 }
402
403 void BitcoinGUI::setClientModel(ClientModel *clientModel)
404 {
405     this->clientModel = clientModel;
406     if(clientModel)
407     {
408         // Replace some strings and icons, when using the testnet
409         if(clientModel->isTestNet())
410         {
411             setWindowTitle(windowTitle() + QString(" ") + tr("[testnet]"));
412 #ifndef Q_OS_MAC
413             qApp->setWindowIcon(QIcon(":icons/bitcoin_testnet"));
414             setWindowIcon(QIcon(":icons/bitcoin_testnet"));
415 #else
416             MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
417 #endif
418             if(trayIcon)
419             {
420                 trayIcon->setToolTip(tr("NovaCoin client") + QString(" ") + tr("[testnet]"));
421                 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
422                 toggleHideAction->setIcon(QIcon(":/icons/toolbar_testnet"));
423             }
424
425             aboutAction->setIcon(QIcon(":/icons/toolbar_testnet"));
426         }
427
428         // Keep up to date with client
429         setNumConnections(clientModel->getNumConnections());
430         connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
431
432         setNumBlocks(clientModel->getNumBlocks(), clientModel->getNumBlocksOfPeers());
433         connect(clientModel, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
434
435         QTimer *timer = new QTimer(this);
436         connect(timer, SIGNAL(timeout()), this, SLOT(updateMining()));
437         timer->start(10*1000); //10 seconds
438
439         // Report errors from network/worker thread
440         connect(clientModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
441
442         rpcConsole->setClientModel(clientModel);
443         addressBookPage->setOptionsModel(clientModel->getOptionsModel());
444         receiveCoinsPage->setOptionsModel(clientModel->getOptionsModel());
445     }
446 }
447
448 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
449 {
450     this->walletModel = walletModel;
451     if(walletModel)
452     {
453         // Report errors from wallet thread
454         connect(walletModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
455
456         // Put transaction list in tabs
457         transactionView->setModel(walletModel);
458         mintingView->setModel(walletModel);
459
460         overviewPage->setModel(walletModel);
461         addressBookPage->setModel(walletModel->getAddressTableModel());
462         receiveCoinsPage->setModel(walletModel->getAddressTableModel());
463         sendCoinsPage->setModel(walletModel);
464         signVerifyMessageDialog->setModel(walletModel);
465         multisigPage->setModel(walletModel);
466
467         setEncryptionStatus(walletModel->getEncryptionStatus());
468         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
469         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(updateMining()));
470
471         // Balloon pop-up for new transaction
472         connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
473                 this, SLOT(incomingTransaction(QModelIndex,int,int)));
474
475         // Ask for passphrase if needed
476         connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
477     }
478 }
479
480 void BitcoinGUI::createTrayIcon()
481 {
482     QMenu *trayIconMenu;
483 #ifndef Q_OS_MAC
484     trayIcon = new QSystemTrayIcon(this);
485     trayIconMenu = new QMenu(this);
486     trayIcon->setContextMenu(trayIconMenu);
487     trayIcon->setToolTip(tr("NovaCoin client"));
488     trayIcon->setIcon(QIcon(":/icons/toolbar"));
489     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
490             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
491     trayIcon->show();
492 #else
493     // Note: On Mac, the dock icon is used to provide the tray's functionality.
494     MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
495     dockIconHandler->setMainWindow((QMainWindow *)this);
496     trayIconMenu = dockIconHandler->dockMenu();
497 #endif
498
499     // Configuration of the tray icon (or dock icon) icon menu
500     trayIconMenu->addAction(toggleHideAction);
501     trayIconMenu->addSeparator();
502     trayIconMenu->addAction(sendCoinsAction);
503     trayIconMenu->addAction(multisigAction);
504     trayIconMenu->addAction(receiveCoinsAction);
505     trayIconMenu->addSeparator();
506     trayIconMenu->addAction(signMessageAction);
507     trayIconMenu->addAction(verifyMessageAction);
508     trayIconMenu->addSeparator();
509     trayIconMenu->addAction(optionsAction);
510     trayIconMenu->addAction(openRPCConsoleAction);
511 #ifndef Q_OS_MAC
512     // This is built-in on Mac
513     trayIconMenu->addSeparator();
514     trayIconMenu->addAction(quitAction);    
515 #endif
516     notificator = new Notificator(QApplication::applicationName(), trayIcon, this);
517 }
518
519 #ifndef Q_OS_MAC
520 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
521 {
522     if(reason == QSystemTrayIcon::Trigger)
523     {
524         // Click on system tray icon triggers show/hide of the main window
525         toggleHideAction->trigger();
526     }
527 }
528 #endif
529
530 void BitcoinGUI::optionsClicked()
531 {
532     if(!clientModel || !clientModel->getOptionsModel())
533         return;
534     OptionsDialog dlg;
535     dlg.setModel(clientModel->getOptionsModel());
536     dlg.exec();
537 }
538
539 void BitcoinGUI::aboutClicked()
540 {
541     AboutDialog dlg;
542     dlg.setModel(clientModel);
543     dlg.exec();
544 }
545
546 void BitcoinGUI::setNumConnections(int count)
547 {
548     QString icon;
549     switch(count)
550     {
551     case 0: icon = ":/icons/connect_0"; break;
552     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
553     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
554     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
555     default: icon = ":/icons/connect_4"; break;
556     }
557     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
558     labelConnectionsIcon->setToolTip(tr("%n active connection(s) to NovaCoin network", "", count));
559 }
560
561 void BitcoinGUI::setNumBlocks(int count, int nTotalBlocks)
562 {
563     // don't show / hide progress bar and its label if we have no connection to the network
564     if (!clientModel || clientModel->getNumConnections() == 0)
565     {
566         progressBarLabel->setVisible(false);
567         progressBar->setVisible(false);
568
569         return;
570     }
571
572     QString strStatusBarWarnings = clientModel->getStatusBarWarnings();
573     QString tooltip;
574
575     if(count < nTotalBlocks)
576     {
577         int nRemainingBlocks = nTotalBlocks - count;
578         float nPercentageDone = count / (nTotalBlocks * 0.01f);
579
580         if (strStatusBarWarnings.isEmpty())
581         {
582             progressBarLabel->setText(tr("Synchronizing with network..."));
583             progressBarLabel->setVisible(true);
584             progressBar->setFormat(tr("~%n block(s) remaining", "", nRemainingBlocks));
585             progressBar->setMaximum(nTotalBlocks);
586             progressBar->setValue(count);
587             progressBar->setVisible(true);
588         }
589
590         tooltip = tr("Downloaded %1 of %2 blocks of transaction history (%3% done).").arg(count).arg(nTotalBlocks).arg(nPercentageDone, 0, 'f', 2);
591     }
592     else
593     {
594         if (strStatusBarWarnings.isEmpty())
595             progressBarLabel->setVisible(false);
596
597         progressBar->setVisible(false);
598         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
599     }
600
601     // Override progressBarLabel text and hide progress bar, when we have warnings to display
602     if (!strStatusBarWarnings.isEmpty())
603     {
604         progressBarLabel->setText(strStatusBarWarnings);
605         progressBarLabel->setVisible(true);
606         progressBar->setVisible(false);
607     }
608
609     QDateTime lastBlockDate = clientModel->getLastBlockDate();
610     int secs = lastBlockDate.secsTo(QDateTime::currentDateTime());
611     QString text;
612
613     // Represent time from last generated block in human readable text
614     if(secs <= 0)
615     {
616         // Fully up to date. Leave text empty.
617     }
618     else if(secs < 60)
619     {
620         text = tr("%n second(s) ago","",secs);
621     }
622     else if(secs < 60*60)
623     {
624         text = tr("%n minute(s) ago","",secs/60);
625     }
626     else if(secs < 24*60*60)
627     {
628         text = tr("%n hour(s) ago","",secs/(60*60));
629     }
630     else
631     {
632         text = tr("%n day(s) ago","",secs/(60*60*24));
633     }
634
635     // Set icon state: spinning if catching up, tick otherwise
636     if(secs < 90*60 && count >= nTotalBlocks)
637     {
638         tooltip = tr("Up to date") + QString(".<br>") + tooltip;
639         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
640
641         overviewPage->showOutOfSyncWarning(false);
642     }
643     else
644     {
645         tooltip = tr("Catching up...") + QString("<br>") + tooltip;
646         labelBlocksIcon->setMovie(syncIconMovie);
647         syncIconMovie->start();
648
649         overviewPage->showOutOfSyncWarning(true);
650     }
651
652     if(!text.isEmpty())
653     {
654         tooltip += QString("<br>");
655         tooltip += tr("Last received block was generated %1.").arg(text);
656     }
657
658     // Don't word-wrap this (fixed-width) tooltip
659     tooltip = QString("<nobr>") + tooltip + QString("</nobr>");
660
661     labelBlocksIcon->setToolTip(tooltip);
662     progressBarLabel->setToolTip(tooltip);
663     progressBar->setToolTip(tooltip);
664 }
665
666 void BitcoinGUI::updateMining()
667 {
668    if(!walletModel)
669       return;
670
671     labelMiningIcon->setPixmap(QIcon(":/icons/mining_inactive").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
672
673     if (!clientModel->getNumConnections())
674     {
675         labelMiningIcon->setToolTip(tr("Wallet is offline"));
676         return;
677     }
678
679     if (walletModel->getEncryptionStatus() == WalletModel::Locked)
680     {
681         labelMiningIcon->setToolTip(tr("Wallet is locked"));
682         return;
683     }
684
685     if (clientModel->inInitialBlockDownload() || clientModel->getNumBlocksOfPeers() > clientModel->getNumBlocks())
686     {
687         labelMiningIcon->setToolTip(tr("Blockchain download is in progress"));
688         return;
689     }
690
691     float nKernelsRate = 0, nCoinDaysRate = 0;
692     walletModel->getStakeStats(nKernelsRate, nCoinDaysRate);
693
694     if (nKernelsRate > 0)
695     {
696         labelMiningIcon->setPixmap(QIcon(":/icons/mining_active").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
697
698         uint64_t nNetworkWeight = clientModel->getPoSKernelPS();
699 /*
700         double dDifficulty = clientModel->getDifficulty(true);
701         QString msg;
702
703         int nApproxTime = 4294967297 * dDifficulty / nTotalWeight;
704
705         if (nApproxTime < 60)
706             msg = tr("%n second(s)", "", nApproxTime);
707         else if (nApproxTime < 60*60)
708             msg = tr("%n minute(s)", "", nApproxTime / 60);
709         else if (nApproxTime < 24*60*60)
710             msg = tr("%n hour(s)", "", nApproxTime / 3600);
711         else
712             msg = tr("%n day(s)", "", nApproxTime / 86400);
713
714         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));
715 */
716
717         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>"));
718     }
719     else
720         labelMiningIcon->setToolTip(tr("No suitable inputs were found"));
721 }
722
723 void BitcoinGUI::error(const QString &title, const QString &message, bool modal)
724 {
725     // Report errors from network/worker thread
726     if(modal)
727     {
728         QMessageBox::critical(this, title, message, QMessageBox::Ok, QMessageBox::Ok);
729     } else {
730         notificator->notify(Notificator::Critical, title, message);
731     }
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::gotoMultisigPage()
946 {
947     multisigPage->show();
948     multisigPage->setFocus();
949 }
950
951 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
952 {
953     // Accept only URIs
954     if(event->mimeData()->hasUrls())
955         event->acceptProposedAction();
956 }
957
958 void BitcoinGUI::dropEvent(QDropEvent *event)
959 {
960     if(event->mimeData()->hasUrls())
961     {
962         int nValidUrisFound = 0;
963         QList<QUrl> uris = event->mimeData()->urls();
964         foreach(const QUrl &uri, uris)
965         {
966             if (sendCoinsPage->handleURI(uri.toString()))
967                 nValidUrisFound++;
968         }
969
970         // if valid URIs were found
971         if (nValidUrisFound)
972             gotoSendCoinsPage();
973         else
974             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."));
975     }
976
977     event->acceptProposedAction();
978 }
979
980 void BitcoinGUI::handleURI(QString strURI)
981 {
982     // URI has to be valid
983     if (sendCoinsPage->handleURI(strURI))
984     {
985         showNormalIfMinimized();
986         gotoSendCoinsPage();
987     }
988     else
989         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."));
990 }
991
992 void BitcoinGUI::setEncryptionStatus(int status)
993 {
994     switch(status)
995     {
996     case WalletModel::Unencrypted:
997         labelEncryptionIcon->hide();
998         encryptWalletAction->setChecked(false);
999         changePassphraseAction->setEnabled(false);
1000         lockWalletAction->setEnabled(false);
1001         unlockWalletAction->setEnabled(false);
1002         unlockWalletMiningAction->setEnabled(false);
1003         encryptWalletAction->setEnabled(true);
1004         break;
1005     case WalletModel::Unlocked:
1006         labelEncryptionIcon->show();
1007         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
1008         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
1009         encryptWalletAction->setChecked(true);
1010         changePassphraseAction->setEnabled(true);
1011         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
1012
1013         lockWalletAction->setEnabled(true);
1014         lockWalletAction->setChecked(false);
1015         unlockWalletAction->setEnabled(false);
1016         unlockWalletMiningAction->setEnabled(false);
1017
1018         if (fWalletUnlockMintOnly)
1019             unlockWalletMiningAction->setChecked(true);
1020         else
1021             unlockWalletAction->setChecked(true);
1022
1023         break;
1024     case WalletModel::Locked:
1025         labelEncryptionIcon->show();
1026         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
1027         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
1028         encryptWalletAction->setChecked(true);
1029         changePassphraseAction->setEnabled(true);
1030         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
1031
1032         lockWalletAction->setChecked(true);
1033         unlockWalletAction->setChecked(false);
1034         unlockWalletMiningAction->setChecked(false);
1035
1036         lockWalletAction->setEnabled(false);
1037         unlockWalletAction->setEnabled(true);
1038         unlockWalletMiningAction->setEnabled(true);
1039         break;
1040     }
1041 }
1042
1043 void BitcoinGUI::encryptWallet(bool status)
1044 {
1045     if(!walletModel)
1046         return;
1047     AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
1048                                      AskPassphraseDialog::Decrypt, this);
1049     dlg.setModel(walletModel);
1050     dlg.exec();
1051
1052     setEncryptionStatus(walletModel->getEncryptionStatus());
1053 }
1054
1055 void BitcoinGUI::unlockWalletMining(bool status)
1056 {
1057     if(!walletModel)
1058         return;
1059
1060     // Unlock wallet when requested by wallet model
1061     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
1062     {
1063         AskPassphraseDialog dlg(AskPassphraseDialog::UnlockMining, this);
1064         dlg.setModel(walletModel);
1065         dlg.exec();
1066     }
1067 }
1068
1069 void BitcoinGUI::backupWallet()
1070 {
1071 #if QT_VERSION < 0x050000
1072     QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
1073 #else
1074     QString saveDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
1075 #endif
1076     QString filename = QFileDialog::getSaveFileName(this, tr("Backup Wallet"), saveDir, tr("Wallet Data (*.dat)"));
1077     if(!filename.isEmpty()) {
1078         if(!walletModel->backupWallet(filename)) {
1079             QMessageBox::warning(this, tr("Backup Failed"), tr("There was an error trying to save the wallet data to the new location."));
1080         }
1081     }
1082 }
1083
1084 void BitcoinGUI::dumpWallet()
1085 {
1086    if(!walletModel)
1087       return;
1088
1089    WalletModel::UnlockContext ctx(walletModel->requestUnlock());
1090    if(!ctx.isValid())
1091    {
1092        // Unlock wallet failed or was cancelled
1093        return;
1094    }
1095
1096 #if QT_VERSION < 0x050000
1097     QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
1098 #else
1099     QString saveDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
1100 #endif
1101     QString filename = QFileDialog::getSaveFileName(this, tr("Dump Wallet"), saveDir, tr("Wallet dump (*.txt)"));
1102     if(!filename.isEmpty()) {
1103         if(!walletModel->dumpWallet(filename)) {
1104             error(tr("Dump failed"),
1105                          tr("An error happened while trying to save the keys to your location.\n"
1106                             "Keys were not saved.")
1107                       ,CClientUIInterface::MSG_ERROR);
1108         }
1109         else
1110           message(tr("Dump successful"),
1111                        tr("Keys were saved to this file:\n%2")
1112                        .arg(filename)
1113                       ,CClientUIInterface::MSG_INFORMATION);
1114     }
1115 }
1116
1117 void BitcoinGUI::importWallet()
1118 {
1119    if(!walletModel)
1120       return;
1121
1122    WalletModel::UnlockContext ctx(walletModel->requestUnlock());
1123    if(!ctx.isValid())
1124    {
1125        // Unlock wallet failed or was cancelled
1126        return;
1127    }
1128
1129 #if QT_VERSION < 0x050000
1130     QString openDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
1131 #else
1132     QString openDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
1133 #endif
1134     QString filename = QFileDialog::getOpenFileName(this, tr("Import Wallet"), openDir, tr("Wallet dump (*.txt)"));
1135     if(!filename.isEmpty()) {
1136         if(!walletModel->importWallet(filename)) {
1137             error(tr("Import Failed"),
1138                          tr("An error happened while trying to import the keys.\n"
1139                             "Some or all keys from:\n %1,\n were not imported into your wallet.")
1140                          .arg(filename)
1141                       ,CClientUIInterface::MSG_ERROR);
1142         }
1143         else
1144           message(tr("Import Successful"),
1145                        tr("All keys from:\n %1,\n were imported into your wallet.")
1146                        .arg(filename)
1147                       ,CClientUIInterface::MSG_INFORMATION);
1148     }
1149 }
1150
1151
1152 void BitcoinGUI::changePassphrase()
1153 {
1154     AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
1155     dlg.setModel(walletModel);
1156     dlg.exec();
1157 }
1158
1159 void BitcoinGUI::unlockWallet()
1160 {
1161     if(!walletModel)
1162         return;
1163     // Unlock wallet when requested by wallet model
1164     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
1165     {
1166         AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
1167         dlg.setModel(walletModel);
1168         dlg.exec();
1169     }
1170 }
1171
1172 void BitcoinGUI::lockWallet()
1173 {
1174     if(!walletModel)
1175         return;
1176
1177     walletModel->setWalletLocked(true);
1178 }
1179
1180 void BitcoinGUI::showNormalIfMinimized(bool fToggleHidden)
1181 {
1182     // activateWindow() (sometimes) helps with keyboard focus on Windows
1183     if (isHidden())
1184     {
1185         // Make sure the window is not minimized
1186         setWindowState(windowState() & (~Qt::WindowMinimized | Qt::WindowActive));
1187         // Then show it
1188         show();
1189         raise();
1190         activateWindow();
1191     }
1192     else if (isMinimized())
1193     {
1194         showNormal();
1195         raise();
1196         activateWindow();
1197     }
1198     else if (GUIUtil::isObscured(this))
1199     {
1200         raise();
1201         activateWindow();
1202         if(fToggleHidden)
1203         {
1204             Sleep(1);
1205             if (GUIUtil::isObscured(this))
1206                 hide();
1207         }
1208     }
1209     else if(fToggleHidden)
1210         hide();
1211 }
1212
1213 void BitcoinGUI::toggleHidden()
1214 {
1215     showNormalIfMinimized(true);
1216 }