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