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