Merge pull request #1089 from laanwj/2012_04_translationupdate
[novacoin.git] / src / qt / bitcoingui.cpp
1 /*
2  * Qt4 bitcoin GUI.
3  *
4  * W.J. van der Laan 20011-2012
5  * The Bitcoin Developers 20011-2012
6  */
7 #include "bitcoingui.h"
8 #include "transactiontablemodel.h"
9 #include "addressbookpage.h"
10 #include "sendcoinsdialog.h"
11 #include "messagepage.h"
12 #include "optionsdialog.h"
13 #include "aboutdialog.h"
14 #include "clientmodel.h"
15 #include "walletmodel.h"
16 #include "editaddressdialog.h"
17 #include "optionsmodel.h"
18 #include "transactiondescdialog.h"
19 #include "addresstablemodel.h"
20 #include "transactionview.h"
21 #include "overviewpage.h"
22 #include "bitcoinunits.h"
23 #include "guiconstants.h"
24 #include "askpassphrasedialog.h"
25 #include "notificator.h"
26 #include "guiutil.h"
27
28 #ifdef Q_WS_MAC
29 #include "macdockiconhandler.h"
30 #endif
31
32 #include <QApplication>
33 #include <QMainWindow>
34 #include <QMenuBar>
35 #include <QMenu>
36 #include <QIcon>
37 #include <QTabWidget>
38 #include <QVBoxLayout>
39 #include <QToolBar>
40 #include <QStatusBar>
41 #include <QLabel>
42 #include <QLineEdit>
43 #include <QPushButton>
44 #include <QLocale>
45 #include <QMessageBox>
46 #include <QProgressBar>
47 #include <QStackedWidget>
48 #include <QDateTime>
49 #include <QMovie>
50 #include <QFileDialog>
51 #include <QDesktopServices>
52 #include <QTimer>
53
54 #include <QDragEnterEvent>
55 #include <QUrl>
56
57 #include <iostream>
58
59 BitcoinGUI::BitcoinGUI(QWidget *parent):
60     QMainWindow(parent),
61     clientModel(0),
62     walletModel(0),
63     encryptWalletAction(0),
64     changePassphraseAction(0),
65     aboutQtAction(0),
66     trayIcon(0),
67     notificator(0)
68 {
69     resize(850, 550);
70     setWindowTitle(tr("Bitcoin Wallet"));
71 #ifndef Q_WS_MAC
72     setWindowIcon(QIcon(":icons/bitcoin"));
73 #else
74     setUnifiedTitleAndToolBarOnMac(true);
75     QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
76 #endif
77     // Accept D&D of URIs
78     setAcceptDrops(true);
79
80     // Create actions for the toolbar, menu bar and tray/dock icon
81     createActions();
82
83     // Create application menu bar
84     createMenuBar();
85
86     // Create the toolbars
87     createToolBars();
88
89     // Create the tray icon (or setup the dock icon)
90     createTrayIcon();
91
92     // Create tabs
93     overviewPage = new OverviewPage();
94
95     transactionsPage = new QWidget(this);
96     QVBoxLayout *vbox = new QVBoxLayout();
97     transactionView = new TransactionView(this);
98     vbox->addWidget(transactionView);
99     transactionsPage->setLayout(vbox);
100
101     addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
102
103     receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
104
105     sendCoinsPage = new SendCoinsDialog(this);
106
107     messagePage = new MessagePage(this);
108
109     centralWidget = new QStackedWidget(this);
110     centralWidget->addWidget(overviewPage);
111     centralWidget->addWidget(transactionsPage);
112     centralWidget->addWidget(addressBookPage);
113     centralWidget->addWidget(receiveCoinsPage);
114     centralWidget->addWidget(sendCoinsPage);
115 #ifdef FIRST_CLASS_MESSAGING
116     centralWidget->addWidget(messagePage);
117 #endif
118     setCentralWidget(centralWidget);
119
120     // Create status bar
121     statusBar();
122
123     // Status bar notification icons
124     QFrame *frameBlocks = new QFrame();
125     frameBlocks->setContentsMargins(0,0,0,0);
126     frameBlocks->setMinimumWidth(56);
127     frameBlocks->setMaximumWidth(56);
128     QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
129     frameBlocksLayout->setContentsMargins(3,0,3,0);
130     frameBlocksLayout->setSpacing(3);
131     labelEncryptionIcon = new QLabel();
132     labelConnectionsIcon = new QLabel();
133     labelBlocksIcon = new QLabel();
134     frameBlocksLayout->addStretch();
135     frameBlocksLayout->addWidget(labelEncryptionIcon);
136     frameBlocksLayout->addStretch();
137     frameBlocksLayout->addWidget(labelConnectionsIcon);
138     frameBlocksLayout->addStretch();
139     frameBlocksLayout->addWidget(labelBlocksIcon);
140     frameBlocksLayout->addStretch();
141
142     // Progress bar and label for blocks download
143     progressBarLabel = new QLabel();
144     progressBarLabel->setVisible(false);
145     progressBar = new QProgressBar();
146     progressBar->setVisible(false);
147
148     statusBar()->addWidget(progressBarLabel);
149     statusBar()->addWidget(progressBar);
150     statusBar()->addPermanentWidget(frameBlocks);
151
152     // define OS independent progress bar style (has to be placed after addWidget(), otherwise we crash)
153     // we did this, because with some OSes default style, text on the progress bar is unreadable
154     progressBar->setStyleSheet("QProgressBar { background-color: transparent; border: 1px solid grey; border-radius: 2px; padding: 1px; text-align: center; } QProgressBar::chunk { background: QLinearGradient(x1: 0, y1: 0, x2: 1, y2: 0, stop: 0 #FF8000, stop: 1 orange); margin: 0px; }");
155
156     syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
157
158     // Clicking on a transaction on the overview page simply sends you to transaction history page
159     connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
160
161     // Doubleclicking on a transaction on the transaction history page shows details
162     connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
163
164     gotoOverviewPage();
165 }
166
167 BitcoinGUI::~BitcoinGUI()
168 {
169     if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
170         trayIcon->hide();
171 #ifdef Q_WS_MAC
172     delete appMenuBar;
173 #endif
174 }
175
176 void BitcoinGUI::createActions()
177 {
178     QActionGroup *tabGroup = new QActionGroup(this);
179
180     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
181     overviewAction->setToolTip(tr("Show general overview of wallet"));
182     overviewAction->setCheckable(true);
183     overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
184     tabGroup->addAction(overviewAction);
185
186     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
187     historyAction->setToolTip(tr("Browse transaction history"));
188     historyAction->setCheckable(true);
189     historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
190     tabGroup->addAction(historyAction);
191
192     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
193     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
194     addressBookAction->setCheckable(true);
195     addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
196     tabGroup->addAction(addressBookAction);
197
198     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
199     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
200     receiveCoinsAction->setCheckable(true);
201     receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
202     tabGroup->addAction(receiveCoinsAction);
203
204     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
205     sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
206     sendCoinsAction->setCheckable(true);
207     sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
208     tabGroup->addAction(sendCoinsAction);
209
210     messageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message"), this);
211     messageAction->setToolTip(tr("Prove you control an address"));
212 #ifdef FIRST_CLASS_MESSAGING
213     messageAction->setCheckable(true);
214 #endif
215     tabGroup->addAction(messageAction);
216
217     connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
218     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
219     connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
220     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
221     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
222     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
223     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
224     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
225     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
226     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
227     connect(messageAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
228     connect(messageAction, SIGNAL(triggered()), this, SLOT(gotoMessagePage()));
229
230     quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
231     quitAction->setToolTip(tr("Quit application"));
232     quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
233     quitAction->setMenuRole(QAction::QuitRole);
234     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About %1").arg(qApp->applicationName()), this);
235     aboutAction->setToolTip(tr("Show information about Bitcoin"));
236     aboutAction->setMenuRole(QAction::AboutRole);
237     aboutQtAction = new QAction(tr("About &Qt"), this);
238     aboutQtAction->setToolTip(tr("Show information about Qt"));
239     aboutQtAction->setMenuRole(QAction::AboutQtRole);
240     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
241     optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
242     optionsAction->setMenuRole(QAction::PreferencesRole);
243     toggleHideAction = new QAction(QIcon(":/icons/bitcoin"), tr("Show/Hide &Bitcoin"), this);
244     toggleHideAction->setToolTip(tr("Show or hide the Bitcoin window"));
245     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
246     exportAction->setToolTip(tr("Export the data in the current tab to a file"));
247     encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
248     encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
249     encryptWalletAction->setCheckable(true);
250     backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet"), this);
251     backupWalletAction->setToolTip(tr("Backup wallet to another location"));
252     changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
253     changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
254
255     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
256     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
257     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
258     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
259     connect(toggleHideAction, SIGNAL(triggered()), this, SLOT(toggleHidden()));
260     connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
261     connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
262     connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
263 }
264
265 void BitcoinGUI::createMenuBar()
266 {
267 #ifdef Q_WS_MAC
268     // Create a decoupled menu bar on Mac which stays even if the window is closed
269     appMenuBar = new QMenuBar();
270 #else
271     // Get the main window's menu bar on other platforms
272     appMenuBar = menuBar();
273 #endif
274
275     // Configure the menus
276     QMenu *file = appMenuBar->addMenu(tr("&File"));
277     file->addAction(backupWalletAction);
278     file->addAction(exportAction);
279 #ifndef FIRST_CLASS_MESSAGING
280     file->addAction(messageAction);
281 #endif
282     file->addSeparator();
283     file->addAction(quitAction);
284
285     QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
286     settings->addAction(encryptWalletAction);
287     settings->addAction(changePassphraseAction);
288     settings->addSeparator();
289     settings->addAction(optionsAction);
290
291     QMenu *help = appMenuBar->addMenu(tr("&Help"));
292     help->addAction(aboutAction);
293     help->addAction(aboutQtAction);
294 }
295
296 void BitcoinGUI::createToolBars()
297 {
298     QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
299     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
300     toolbar->addAction(overviewAction);
301     toolbar->addAction(sendCoinsAction);
302     toolbar->addAction(receiveCoinsAction);
303     toolbar->addAction(historyAction);
304     toolbar->addAction(addressBookAction);
305 #ifdef FIRST_CLASS_MESSAGING
306     toolbar->addAction(messageAction);
307 #endif
308
309     QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
310     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
311     toolbar2->addAction(exportAction);
312 }
313
314 void BitcoinGUI::setClientModel(ClientModel *clientModel)
315 {
316     this->clientModel = clientModel;
317     if(clientModel)
318     {
319         if(clientModel->isTestNet())
320         {
321             QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
322             setWindowTitle(title_testnet);
323 #ifndef Q_WS_MAC
324             setWindowIcon(QIcon(":icons/bitcoin_testnet"));
325 #else
326             MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
327 #endif
328             if(trayIcon)
329             {
330                 trayIcon->setToolTip(title_testnet);
331                 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
332             }
333         }
334
335         // Keep up to date with client
336         setNumConnections(clientModel->getNumConnections());
337         connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
338
339         setNumBlocks(clientModel->getNumBlocks());
340         connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
341
342         // Report errors from network/worker thread
343         connect(clientModel, SIGNAL(error(QString,QString, bool)), this, SLOT(error(QString,QString,bool)));
344     }
345 }
346
347 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
348 {
349     this->walletModel = walletModel;
350     if(walletModel)
351     {
352         // Report errors from wallet thread
353         connect(walletModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
354
355         // Put transaction list in tabs
356         transactionView->setModel(walletModel);
357
358         overviewPage->setModel(walletModel);
359         addressBookPage->setModel(walletModel->getAddressTableModel());
360         receiveCoinsPage->setModel(walletModel->getAddressTableModel());
361         sendCoinsPage->setModel(walletModel);
362         messagePage->setModel(walletModel);
363
364         setEncryptionStatus(walletModel->getEncryptionStatus());
365         connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
366
367         // Balloon popup for new transaction
368         connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
369                 this, SLOT(incomingTransaction(QModelIndex,int,int)));
370
371         // Ask for passphrase if needed
372         connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
373     }
374 }
375
376 void BitcoinGUI::createTrayIcon()
377 {
378     QMenu *trayIconMenu;
379 #ifndef Q_WS_MAC
380     trayIcon = new QSystemTrayIcon(this);
381     trayIconMenu = new QMenu(this);
382     trayIcon->setContextMenu(trayIconMenu);
383     trayIcon->setToolTip(tr("Bitcoin client"));
384     trayIcon->setIcon(QIcon(":/icons/toolbar"));
385     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
386             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
387     trayIcon->show();
388 #else
389     // Note: On Mac, the dock icon is used to provide the tray's functionality.
390     MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
391     connect(dockIconHandler, SIGNAL(dockIconClicked()), toggleHideAction, SLOT(trigger()));
392     trayIconMenu = dockIconHandler->dockMenu();
393 #endif
394
395     // Configuration of the tray icon (or dock icon) icon menu
396     trayIconMenu->addAction(toggleHideAction);
397     trayIconMenu->addSeparator();
398     trayIconMenu->addAction(messageAction);
399 #ifndef FIRST_CLASS_MESSAGING
400     trayIconMenu->addSeparator();
401 #endif
402     trayIconMenu->addAction(receiveCoinsAction);
403     trayIconMenu->addAction(sendCoinsAction);
404     trayIconMenu->addSeparator();
405     trayIconMenu->addAction(optionsAction);
406 #ifndef Q_WS_MAC // This is built-in on Mac
407     trayIconMenu->addSeparator();
408     trayIconMenu->addAction(quitAction);
409 #endif
410
411     notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
412 }
413
414 #ifndef Q_WS_MAC
415 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
416 {
417     if(reason == QSystemTrayIcon::Trigger)
418     {
419         // Click on system tray icon triggers "open bitcoin"
420         toggleHideAction->trigger();
421     }
422 }
423 #endif
424
425 void BitcoinGUI::toggleHidden()
426 {
427     // activateWindow() (sometimes) helps with keyboard focus on Windows
428     if(isHidden())
429     {
430         show();
431         activateWindow();
432     }
433     else if(isMinimized())
434     {
435         showNormal();
436         activateWindow();
437     }
438     else if(GUIUtil::isObscured(this))
439     {
440         raise();
441         activateWindow();
442     }
443     else
444         hide();
445 }
446
447 void BitcoinGUI::optionsClicked()
448 {
449     if(!clientModel || !clientModel->getOptionsModel())
450         return;
451     OptionsDialog dlg;
452     dlg.setModel(clientModel->getOptionsModel());
453     dlg.exec();
454 }
455
456 void BitcoinGUI::aboutClicked()
457 {
458     AboutDialog dlg;
459     dlg.setModel(clientModel);
460     dlg.exec();
461 }
462
463 void BitcoinGUI::setNumConnections(int count)
464 {
465     QString icon;
466     switch(count)
467     {
468     case 0: icon = ":/icons/connect_0"; break;
469     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
470     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
471     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
472     default: icon = ":/icons/connect_4"; break;
473     }
474     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
475     labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
476 }
477
478 void BitcoinGUI::setNumBlocks(int count)
479 {
480     // don't show / hide progressBar and it's label if we have no connection(s) to the network
481     if (!clientModel || clientModel->getNumConnections() == 0)
482     {
483         progressBarLabel->setVisible(false);
484         progressBar->setVisible(false);
485
486         return;
487     }
488
489     int nTotalBlocks = clientModel->getNumBlocksOfPeers();
490     QString tooltip;
491
492     if(count < nTotalBlocks)
493     {
494         int nRemainingBlocks = nTotalBlocks - count;
495         float nPercentageDone = count / (nTotalBlocks * 0.01f);
496
497         if (clientModel->getStatusBarWarnings() == "")
498         {
499             progressBarLabel->setText(tr("Synchronizing with network..."));
500             progressBarLabel->setVisible(true);
501             progressBar->setFormat(tr("~%n block(s) remaining", "", nRemainingBlocks));
502             progressBar->setMaximum(nTotalBlocks);
503             progressBar->setValue(count);
504             progressBar->setVisible(true);
505         }
506         else
507         {
508             progressBarLabel->setText(clientModel->getStatusBarWarnings());
509             progressBarLabel->setVisible(true);
510             progressBar->setVisible(false);
511         }
512         tooltip = tr("Downloaded %1 of %2 blocks of transaction history (%3% done).").arg(count).arg(nTotalBlocks).arg(nPercentageDone, 0, 'f', 2);
513     }
514     else
515     {
516         if (clientModel->getStatusBarWarnings() == "")
517             progressBarLabel->setVisible(false);
518         else
519         {
520             progressBarLabel->setText(clientModel->getStatusBarWarnings());
521             progressBarLabel->setVisible(true);
522         }
523         progressBar->setVisible(false);
524         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
525     }
526
527     QDateTime now = QDateTime::currentDateTime();
528     QDateTime lastBlockDate = clientModel->getLastBlockDate();
529     int secs = lastBlockDate.secsTo(now);
530     QString text;
531
532     // Represent time from last generated block in human readable text
533     if(secs <= 0)
534     {
535         // Fully up to date. Leave text empty.
536     }
537     else if(secs < 60)
538     {
539         text = tr("%n second(s) ago","",secs);
540     }
541     else if(secs < 60*60)
542     {
543         text = tr("%n minute(s) ago","",secs/60);
544     }
545     else if(secs < 24*60*60)
546     {
547         text = tr("%n hour(s) ago","",secs/(60*60));
548     }
549     else
550     {
551         text = tr("%n day(s) ago","",secs/(60*60*24));
552     }
553
554     // Set icon state: spinning if catching up, tick otherwise
555     if(secs < 90*60 && count >= nTotalBlocks)
556     {
557         tooltip = tr("Up to date") + QString(".\n") + tooltip;
558         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
559     }
560     else
561     {
562         tooltip = tr("Catching up...") + QString("\n") + tooltip;
563         labelBlocksIcon->setMovie(syncIconMovie);
564         syncIconMovie->start();
565     }
566
567     if(!text.isEmpty())
568     {
569         tooltip += QString("\n");
570         tooltip += tr("Last received block was generated %1.").arg(text);
571     }
572
573     labelBlocksIcon->setToolTip(tooltip);
574     progressBarLabel->setToolTip(tooltip);
575     progressBar->setToolTip(tooltip);
576 }
577
578 void BitcoinGUI::error(const QString &title, const QString &message, bool modal)
579 {
580     // Report errors from network/worker thread
581     if(modal)
582     {
583         QMessageBox::critical(this, title, message, QMessageBox::Ok, QMessageBox::Ok);
584     } else {
585         notificator->notify(Notificator::Critical, title, message);
586     }
587 }
588
589 void BitcoinGUI::changeEvent(QEvent *e)
590 {
591     QMainWindow::changeEvent(e);
592 #ifndef Q_WS_MAC // Ignored on Mac
593     if(e->type() == QEvent::WindowStateChange)
594     {
595         if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
596         {
597             QWindowStateChangeEvent *wsevt = static_cast<QWindowStateChangeEvent*>(e);
598             if(!(wsevt->oldState() & Qt::WindowMinimized) && isMinimized())
599             {
600                 QTimer::singleShot(0, this, SLOT(hide()));
601                 e->ignore();
602             }
603         }
604     }
605 #endif
606 }
607
608 void BitcoinGUI::closeEvent(QCloseEvent *event)
609 {
610     if(clientModel)
611     {
612 #ifndef Q_WS_MAC // Ignored on Mac
613         if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
614            !clientModel->getOptionsModel()->getMinimizeOnClose())
615         {
616             qApp->quit();
617         }
618 #endif
619     }
620     QMainWindow::closeEvent(event);
621 }
622
623 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
624 {
625     QString strMessage =
626         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
627           "which goes to the nodes that process your transaction and helps to support the network.  "
628           "Do you want to pay the fee?").arg(
629                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
630     QMessageBox::StandardButton retval = QMessageBox::question(
631           this, tr("Sending..."), strMessage,
632           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
633     *payFee = (retval == QMessageBox::Yes);
634 }
635
636 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
637 {
638     if(!walletModel || !clientModel)
639         return;
640     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
641     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
642                     .data(Qt::EditRole).toULongLong();
643     if(!clientModel->inInitialBlockDownload())
644     {
645         // On new transaction, make an info balloon
646         // Unless the initial block download is in progress, to prevent balloon-spam
647         QString date = ttm->index(start, TransactionTableModel::Date, parent)
648                         .data().toString();
649         QString type = ttm->index(start, TransactionTableModel::Type, parent)
650                         .data().toString();
651         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
652                         .data().toString();
653         QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
654                             TransactionTableModel::ToAddress, parent)
655                         .data(Qt::DecorationRole));
656
657         notificator->notify(Notificator::Information,
658                             (amount)<0 ? tr("Sent transaction") :
659                                          tr("Incoming transaction"),
660                               tr("Date: %1\n"
661                                  "Amount: %2\n"
662                                  "Type: %3\n"
663                                  "Address: %4\n")
664                               .arg(date)
665                               .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
666                               .arg(type)
667                               .arg(address), icon);
668     }
669 }
670
671 void BitcoinGUI::gotoOverviewPage()
672 {
673     overviewAction->setChecked(true);
674     centralWidget->setCurrentWidget(overviewPage);
675
676     exportAction->setEnabled(false);
677     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
678 }
679
680 void BitcoinGUI::gotoHistoryPage()
681 {
682     historyAction->setChecked(true);
683     centralWidget->setCurrentWidget(transactionsPage);
684
685     exportAction->setEnabled(true);
686     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
687     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
688 }
689
690 void BitcoinGUI::gotoAddressBookPage()
691 {
692     addressBookAction->setChecked(true);
693     centralWidget->setCurrentWidget(addressBookPage);
694
695     exportAction->setEnabled(true);
696     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
697     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
698 }
699
700 void BitcoinGUI::gotoReceiveCoinsPage()
701 {
702     receiveCoinsAction->setChecked(true);
703     centralWidget->setCurrentWidget(receiveCoinsPage);
704
705     exportAction->setEnabled(true);
706     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
707     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
708 }
709
710 void BitcoinGUI::gotoSendCoinsPage()
711 {
712     sendCoinsAction->setChecked(true);
713     centralWidget->setCurrentWidget(sendCoinsPage);
714
715     exportAction->setEnabled(false);
716     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
717 }
718
719 void BitcoinGUI::gotoMessagePage()
720 {
721 #ifdef FIRST_CLASS_MESSAGING
722     messageAction->setChecked(true);
723     centralWidget->setCurrentWidget(messagePage);
724
725     exportAction->setEnabled(false);
726     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
727 #else
728     messagePage->show();
729     messagePage->setFocus();
730 #endif
731 }
732
733 void BitcoinGUI::gotoMessagePage(QString addr)
734 {
735     gotoMessagePage();
736     messagePage->setAddress(addr);
737 }
738
739 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
740 {
741     // Accept only URIs
742     if(event->mimeData()->hasUrls())
743         event->acceptProposedAction();
744 }
745
746 void BitcoinGUI::dropEvent(QDropEvent *event)
747 {
748     if(event->mimeData()->hasUrls())
749     {
750         gotoSendCoinsPage();
751         QList<QUrl> uris = event->mimeData()->urls();
752         foreach(const QUrl &uri, uris)
753         {
754             sendCoinsPage->handleURI(uri.toString());
755         }
756     }
757
758     event->acceptProposedAction();
759 }
760
761 void BitcoinGUI::handleURI(QString strURI)
762 {
763     gotoSendCoinsPage();
764     sendCoinsPage->handleURI(strURI);
765
766     if(!isActiveWindow())
767         activateWindow();
768
769     showNormalIfMinimized();
770 }
771
772 void BitcoinGUI::setEncryptionStatus(int status)
773 {
774     switch(status)
775     {
776     case WalletModel::Unencrypted:
777         labelEncryptionIcon->hide();
778         encryptWalletAction->setChecked(false);
779         changePassphraseAction->setEnabled(false);
780         encryptWalletAction->setEnabled(true);
781         break;
782     case WalletModel::Unlocked:
783         labelEncryptionIcon->show();
784         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
785         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
786         encryptWalletAction->setChecked(true);
787         changePassphraseAction->setEnabled(true);
788         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
789         break;
790     case WalletModel::Locked:
791         labelEncryptionIcon->show();
792         labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
793         labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
794         encryptWalletAction->setChecked(true);
795         changePassphraseAction->setEnabled(true);
796         encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
797         break;
798     }
799 }
800
801 void BitcoinGUI::encryptWallet(bool status)
802 {
803     if(!walletModel)
804         return;
805     AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
806                                      AskPassphraseDialog::Decrypt, this);
807     dlg.setModel(walletModel);
808     dlg.exec();
809
810     setEncryptionStatus(walletModel->getEncryptionStatus());
811 }
812
813 void BitcoinGUI::backupWallet()
814 {
815     QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
816     QString filename = QFileDialog::getSaveFileName(this, tr("Backup Wallet"), saveDir, tr("Wallet Data (*.dat)"));
817     if(!filename.isEmpty()) {
818         if(!walletModel->backupWallet(filename)) {
819             QMessageBox::warning(this, tr("Backup Failed"), tr("There was an error trying to save the wallet data to the new location."));
820         }
821     }
822 }
823
824 void BitcoinGUI::changePassphrase()
825 {
826     AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
827     dlg.setModel(walletModel);
828     dlg.exec();
829 }
830
831 void BitcoinGUI::unlockWallet()
832 {
833     if(!walletModel)
834         return;
835     // Unlock wallet when requested by wallet model
836     if(walletModel->getEncryptionStatus() == WalletModel::Locked)
837     {
838         AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
839         dlg.setModel(walletModel);
840         dlg.exec();
841     }
842 }
843
844 void BitcoinGUI::showNormalIfMinimized()
845 {
846     if(!isVisible()) // Show, if hidden
847         show();
848     if(isMinimized()) // Unminimize, if minimized
849         showNormal();
850 }