bitcoingui.cpp 28.9 KB
Newer Older
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
1
/*
2
3
 * Qt4 bitcoin GUI.
 *
Fordy's avatar
Fordy committed
4
 * W.J. van der Laan 2011-2012
Fordy's avatar
Fordy committed
5
 * The Bitcoin Developers 2011-2012
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
6
 */
7
8
9

#include <QApplication>

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
10
#include "bitcoingui.h"
11

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
12
13
14
#include "transactiontablemodel.h"
#include "optionsdialog.h"
#include "aboutdialog.h"
15
#include "clientmodel.h"
16
#include "walletmodel.h"
17
#include "walletframe.h"
18
#include "optionsmodel.h"
19
#include "transactiondescdialog.h"
20
#include "bitcoinunits.h"
21
#include "guiconstants.h"
22
#include "notificator.h"
23
#include "guiutil.h"
24
#include "rpcconsole.h"
25
#include "ui_interface.h"
26
#include "wallet.h"
27
#include "init.h"
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
28

Philip Kaufmann's avatar
Philip Kaufmann committed
29
#ifdef Q_OS_MAC
30
31
32
#include "macdockiconhandler.h"
#endif

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
33
34
35
36
37
38
39
#include <QMenuBar>
#include <QMenu>
#include <QIcon>
#include <QVBoxLayout>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
40
#include <QMessageBox>
41
#include <QProgressBar>
42
#include <QStackedWidget>
43
#include <QDateTime>
44
#include <QMovie>
sje397's avatar
sje397 committed
45
46
#include <QFileDialog>
#include <QDesktopServices>
47
#include <QTimer>
48
49
#include <QDragEnterEvent>
#include <QUrl>
50
#include <QMimeData>
51
#include <QStyle>
52
#include <QSettings>
53
#include <QDesktopWidget>
54
#include <QListWidget>
55

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
56
57
#include <iostream>

58
59
const QString BitcoinGUI::DEFAULT_WALLET = "~Default";

60
BitcoinGUI::BitcoinGUI(QWidget *parent) :
61
62
    QMainWindow(parent),
    clientModel(0),
63
64
    encryptWalletAction(0),
    changePassphraseAction(0),
65
    aboutQtAction(0),
66
    trayIcon(0),
67
    notificator(0),
68
69
    rpcConsole(0),
    prevBlocks(0)
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
70
{
71
    restoreWindowGeometry();
72
    setWindowTitle(tr("Bitcoin") + " - " + tr("Wallet"));
Philip Kaufmann's avatar
Philip Kaufmann committed
73
#ifndef Q_OS_MAC
74
    QApplication::setWindowIcon(QIcon(":icons/bitcoin"));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
75
    setWindowIcon(QIcon(":icons/bitcoin"));
76
77
78
79
#else
    setUnifiedTitleAndToolBarOnMac(true);
    QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
#endif
80
81
    // Accept D&D of URIs
    setAcceptDrops(true);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
82

83
    // Create actions for the toolbar, menu bar and tray/dock icon
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
84
    createActions();
85

86
87
    // Create application menu bar
    createMenuBar();
88

89
90
    // Create the toolbars
    createToolBars();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
91

92
    // Create system tray icon and notification
93
    createTrayIcon();
94

95
96
97
    // Create wallet frame and make it the central widget
    walletFrame = new WalletFrame(this);
    setCentralWidget(walletFrame);
98

99
    // Create status bar
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
100
    statusBar();
101

102
    // Status bar notification icons
103
    QFrame *frameBlocks = new QFrame();
104
105
106
    frameBlocks->setContentsMargins(0,0,0,0);
    frameBlocks->setMinimumWidth(56);
    frameBlocks->setMaximumWidth(56);
107
108
109
    QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
    frameBlocksLayout->setContentsMargins(3,0,3,0);
    frameBlocksLayout->setSpacing(3);
110
    labelEncryptionIcon = new QLabel();
111
    labelConnectionsIcon = new QLabel();
112
    labelBlocksIcon = new QLabel();
113
    frameBlocksLayout->addStretch();
114
115
    frameBlocksLayout->addWidget(labelEncryptionIcon);
    frameBlocksLayout->addStretch();
116
117
    frameBlocksLayout->addWidget(labelConnectionsIcon);
    frameBlocksLayout->addStretch();
118
119
    frameBlocksLayout->addWidget(labelBlocksIcon);
    frameBlocksLayout->addStretch();
120

121
122
    // Progress bar and label for blocks download
    progressBarLabel = new QLabel();
123
124
    progressBarLabel->setVisible(false);
    progressBar = new QProgressBar();
125
    progressBar->setAlignment(Qt::AlignCenter);
126
127
    progressBar->setVisible(false);

128
129
130
    // Override style sheet for progress bar for styles that have a segmented progress bar,
    // as they make the text unreadable (workaround for issue #1071)
    // See https://qt-project.org/doc/qt-4.8/gallery.html
131
    QString curStyle = QApplication::style()->metaObject()->className();
132
133
134
135
136
    if(curStyle == "QWindowsStyle" || curStyle == "QWindowsXPStyle")
    {
        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; }");
    }

137
138
    statusBar()->addWidget(progressBarLabel);
    statusBar()->addWidget(progressBar);
139
    statusBar()->addPermanentWidget(frameBlocks);
140

141
142
    syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);

143
144
145
    rpcConsole = new RPCConsole(this);
    connect(openRPCConsoleAction, SIGNAL(triggered()), rpcConsole, SLOT(show()));

146
147
    // Install event filter to be able to catch status tip events (QEvent::StatusTip)
    this->installEventFilter(this);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
148
149
}

150
151
BitcoinGUI::~BitcoinGUI()
{
152
    saveWindowGeometry();
153
154
    if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
        trayIcon->hide();
Philip Kaufmann's avatar
Philip Kaufmann committed
155
#ifdef Q_OS_MAC
156
157
158
159
    delete appMenuBar;
#endif
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
160
161
void BitcoinGUI::createActions()
{
162
    QActionGroup *tabGroup = new QActionGroup(this);
163

164
    overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
165
166
    overviewAction->setStatusTip(tr("Show general overview of wallet"));
    overviewAction->setToolTip(overviewAction->statusTip());
167
    overviewAction->setCheckable(true);
168
    overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
169
    tabGroup->addAction(overviewAction);
170

171
    sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send"), this);
172
173
    sendCoinsAction->setStatusTip(tr("Send coins to a Bitcoin address"));
    sendCoinsAction->setToolTip(sendCoinsAction->statusTip());
Philip Kaufmann's avatar
Philip Kaufmann committed
174
175
176
177
    sendCoinsAction->setCheckable(true);
    sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
    tabGroup->addAction(sendCoinsAction);

178
    receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive"), this);
179
180
    receiveCoinsAction->setStatusTip(tr("Show the list of addresses for receiving payments"));
    receiveCoinsAction->setToolTip(receiveCoinsAction->statusTip());
Philip Kaufmann's avatar
Philip Kaufmann committed
181
182
183
184
    receiveCoinsAction->setCheckable(true);
    receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
    tabGroup->addAction(receiveCoinsAction);

185
    historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
186
187
    historyAction->setStatusTip(tr("Browse transaction history"));
    historyAction->setToolTip(historyAction->statusTip());
188
    historyAction->setCheckable(true);
189
    historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
190
191
    tabGroup->addAction(historyAction);

192
    addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Addresses"), this);
193
194
    addressBookAction->setStatusTip(tr("Edit the list of stored addresses and labels"));
    addressBookAction->setToolTip(addressBookAction->statusTip());
195
    addressBookAction->setCheckable(true);
196
    addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
197
198
    tabGroup->addAction(addressBookAction);

199
    connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
200
    connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
Philip Kaufmann's avatar
Philip Kaufmann committed
201
202
203
204
    connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
    connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
    connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
    connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
205
    connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
206
    connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
207
    connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
208
    connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
209

210
    quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
211
    quitAction->setStatusTip(tr("Quit application"));
212
    quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
213
    quitAction->setMenuRole(QAction::QuitRole);
214
    aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About Bitcoin"), this);
215
    aboutAction->setStatusTip(tr("Show information about Bitcoin"));
216
    aboutAction->setMenuRole(QAction::AboutRole);
217
    aboutQtAction = new QAction(QIcon(":/trolltech/qmessagebox/images/qtlogo-64.png"), tr("About &Qt"), this);
218
    aboutQtAction->setStatusTip(tr("Show information about Qt"));
219
    aboutQtAction->setMenuRole(QAction::AboutQtRole);
220
    optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
221
    optionsAction->setStatusTip(tr("Modify configuration options for Bitcoin"));
222
    optionsAction->setMenuRole(QAction::PreferencesRole);
223
    toggleHideAction = new QAction(QIcon(":/icons/bitcoin"), tr("&Show / Hide"), this);
224
    toggleHideAction->setStatusTip(tr("Show or hide the main Window"));
225

226
    encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet..."), this);
227
    encryptWalletAction->setStatusTip(tr("Encrypt the private keys that belong to your wallet"));
228
    encryptWalletAction->setCheckable(true);
229
    backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet..."), this);
230
    backupWalletAction->setStatusTip(tr("Backup wallet to another location"));
231
    changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase..."), this);
232
    changePassphraseAction->setStatusTip(tr("Change the passphrase used for wallet encryption"));
Philip Kaufmann's avatar
Philip Kaufmann committed
233
    signMessageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message..."), this);
234
    signMessageAction->setStatusTip(tr("Sign messages with your Bitcoin addresses to prove you own them"));
Philip Kaufmann's avatar
Philip Kaufmann committed
235
    verifyMessageAction = new QAction(QIcon(":/icons/transaction_0"), tr("&Verify message..."), this);
236
    verifyMessageAction->setStatusTip(tr("Verify messages to ensure they were signed with specified Bitcoin addresses"));
Philip Kaufmann's avatar
Philip Kaufmann committed
237
238

    exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
239
240
    exportAction->setStatusTip(tr("Export the data in the current tab to a file"));
    exportAction->setToolTip(exportAction->statusTip());
241
    openRPCConsoleAction = new QAction(QIcon(":/icons/debugwindow"), tr("&Debug window"), this);
242
    openRPCConsoleAction->setStatusTip(tr("Open debugging and diagnostic console"));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
243

244
245
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
246
    connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
Philip Kaufmann's avatar
Philip Kaufmann committed
247
    connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
248
    connect(toggleHideAction, SIGNAL(triggered()), this, SLOT(toggleHidden()));
249
    connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
sje397's avatar
sje397 committed
250
    connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
251
    connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
Philip Kaufmann's avatar
Philip Kaufmann committed
252
253
    connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
    connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
254
255
}

256
257
void BitcoinGUI::createMenuBar()
{
Philip Kaufmann's avatar
Philip Kaufmann committed
258
#ifdef Q_OS_MAC
259
260
261
262
263
264
265
266
267
    // Create a decoupled menu bar on Mac which stays even if the window is closed
    appMenuBar = new QMenuBar();
#else
    // Get the main window's menu bar on other platforms
    appMenuBar = menuBar();
#endif

    // Configure the menus
    QMenu *file = appMenuBar->addMenu(tr("&File"));
268
    file->addAction(backupWalletAction);
269
    file->addAction(exportAction);
270
    file->addAction(signMessageAction);
271
    file->addAction(verifyMessageAction);
272
    file->addSeparator();
273
274
275
276
277
278
279
280
281
    file->addAction(quitAction);

    QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
    settings->addAction(encryptWalletAction);
    settings->addAction(changePassphraseAction);
    settings->addSeparator();
    settings->addAction(optionsAction);

    QMenu *help = appMenuBar->addMenu(tr("&Help"));
282
283
    help->addAction(openRPCConsoleAction);
    help->addSeparator();
284
    help->addAction(aboutAction);
285
    help->addAction(aboutQtAction);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
}

void BitcoinGUI::createToolBars()
{
    QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
    toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolbar->addAction(overviewAction);
    toolbar->addAction(sendCoinsAction);
    toolbar->addAction(receiveCoinsAction);
    toolbar->addAction(historyAction);
    toolbar->addAction(addressBookAction);

    QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
    toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolbar2->addAction(exportAction);
}

303
void BitcoinGUI::setClientModel(ClientModel *clientModel)
304
{
305
    this->clientModel = clientModel;
306
    if(clientModel)
307
    {
308
        // Replace some strings and icons, when using the testnet
309
310
        if(clientModel->isTestNet())
        {
311
            setWindowTitle(windowTitle() + QString(" ") + tr("[testnet]"));
Philip Kaufmann's avatar
Philip Kaufmann committed
312
#ifndef Q_OS_MAC
313
            QApplication::setWindowIcon(QIcon(":icons/bitcoin_testnet"));
314
            setWindowIcon(QIcon(":icons/bitcoin_testnet"));
315
#else
316
            MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
317
#endif
318
319
            if(trayIcon)
            {
320
321
                // Just attach " [testnet]" to the existing tooltip
                trayIcon->setToolTip(trayIcon->toolTip() + QString(" ") + tr("[testnet]"));
322
323
                trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
            }
324

325
            toggleHideAction->setIcon(QIcon(":/icons/toolbar_testnet"));
326
            aboutAction->setIcon(QIcon(":/icons/toolbar_testnet"));
327
328
        }

329
330
        // Create system tray menu (or setup the dock menu) that late to prevent users from calling actions,
        // while the client has not yet fully loaded
331
        createTrayIconMenu();
332

333
334
335
        // Keep up to date with client
        setNumConnections(clientModel->getNumConnections());
        connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
336

337
338
        setNumBlocks(clientModel->getNumBlocks(), clientModel->getNumBlocksOfPeers());
        connect(clientModel, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
339

340
        // Receive and report messages from network/worker thread
341
        connect(clientModel, SIGNAL(message(QString,QString,unsigned int)), this, SLOT(message(QString,QString,unsigned int)));
342

Philip Kaufmann's avatar
Philip Kaufmann committed
343
        rpcConsole->setClientModel(clientModel);
344
        walletFrame->setClientModel(clientModel);
345
    }
346
347
}

348
bool BitcoinGUI::addWallet(const QString& name, WalletModel *walletModel)
349
{
350
351
352
353
354
355
356
357
358
359
360
    return walletFrame->addWallet(name, walletModel);
}

bool BitcoinGUI::setCurrentWallet(const QString& name)
{
    return walletFrame->setCurrentWallet(name);
}

void BitcoinGUI::removeAllWallets()
{
    walletFrame->removeAllWallets();
361
362
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
363
364
void BitcoinGUI::createTrayIcon()
{
Philip Kaufmann's avatar
Philip Kaufmann committed
365
#ifndef Q_OS_MAC
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
366
    trayIcon = new QSystemTrayIcon(this);
367

368
    trayIcon->setToolTip(tr("Bitcoin client"));
369
    trayIcon->setIcon(QIcon(":/icons/toolbar"));
370
371
372
    trayIcon->show();
#endif

373
    notificator = new Notificator(QApplication::applicationName(), trayIcon);
374
375
376
377
378
379
}

void BitcoinGUI::createTrayIconMenu()
{
    QMenu *trayIconMenu;
#ifndef Q_OS_MAC
380
381
382
383
    // return if trayIcon is unset (only on non-Mac OSes)
    if (!trayIcon)
        return;

384
385
386
    trayIconMenu = new QMenu(this);
    trayIcon->setContextMenu(trayIconMenu);

387
388
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
389
390
391
392
393
394
395
#else
    // Note: On Mac, the dock icon is used to provide the tray's functionality.
    MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
    trayIconMenu = dockIconHandler->dockMenu();
#endif

    // Configuration of the tray icon (or dock icon) icon menu
396
    trayIconMenu->addAction(toggleHideAction);
397
    trayIconMenu->addSeparator();
398
399
    trayIconMenu->addAction(sendCoinsAction);
    trayIconMenu->addAction(receiveCoinsAction);
400
    trayIconMenu->addSeparator();
401
    trayIconMenu->addAction(signMessageAction);
402
    trayIconMenu->addAction(verifyMessageAction);
403
404
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(optionsAction);
405
    trayIconMenu->addAction(openRPCConsoleAction);
Philip Kaufmann's avatar
Philip Kaufmann committed
406
#ifndef Q_OS_MAC // This is built-in on Mac
407
408
409
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
#endif
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
410
411
}

Philip Kaufmann's avatar
Philip Kaufmann committed
412
#ifndef Q_OS_MAC
413
414
void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
415
    if(reason == QSystemTrayIcon::Trigger)
416
    {
417
        // Click on system tray icon triggers show/hide of the main window
418
        toggleHideAction->trigger();
419
420
    }
}
421
#endif
422

423
424
425
426
427
428
429
430
431
432
433
434
void BitcoinGUI::saveWindowGeometry()
{
    QSettings settings;
    settings.setValue("nWindowPos", pos());
    settings.setValue("nWindowSize", size());
}

void BitcoinGUI::restoreWindowGeometry()
{
    QSettings settings;
    QPoint pos = settings.value("nWindowPos").toPoint();
    QSize size = settings.value("nWindowSize", QSize(850, 550)).toSize();
435
436
    if (!pos.x() && !pos.y())
    {
437
        QRect screen = QApplication::desktop()->screenGeometry();
438
439
440
        pos.setX((screen.width()-size.width())/2);
        pos.setY((screen.height()-size.height())/2);
    }
441
442
443
444
    resize(size);
    move(pos);
}

445
446
void BitcoinGUI::optionsClicked()
{
447
448
    if(!clientModel || !clientModel->getOptionsModel())
        return;
449
    OptionsDialog dlg;
450
    dlg.setModel(clientModel->getOptionsModel());
451
    dlg.exec();
452
453
}

454
void BitcoinGUI::aboutClicked()
455
{
456
    AboutDialog dlg;
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
457
    dlg.setModel(clientModel);
458
    dlg.exec();
459
460
}

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
void BitcoinGUI::gotoOverviewPage()
{
    if (walletFrame) walletFrame->gotoOverviewPage();
}

void BitcoinGUI::gotoHistoryPage()
{
    if (walletFrame) walletFrame->gotoHistoryPage();
}

void BitcoinGUI::gotoAddressBookPage()
{
    if (walletFrame) walletFrame->gotoAddressBookPage();
}

void BitcoinGUI::gotoReceiveCoinsPage()
{
    if (walletFrame) walletFrame->gotoReceiveCoinsPage();
}

481
void BitcoinGUI::gotoSendCoinsPage(QString addr)
482
{
483
    if (walletFrame) walletFrame->gotoSendCoinsPage(addr);
484
485
486
487
488
489
490
491
492
}

void BitcoinGUI::gotoSignMessageTab(QString addr)
{
    if (walletFrame) walletFrame->gotoSignMessageTab(addr);
}

void BitcoinGUI::gotoVerifyMessageTab(QString addr)
{
493
    if (walletFrame) walletFrame->gotoVerifyMessageTab(addr);
494
495
}

496
497
void BitcoinGUI::setNumConnections(int count)
{
498
499
500
    QString icon;
    switch(count)
    {
501
502
503
504
505
    case 0: icon = ":/icons/connect_0"; break;
    case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
    case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
    case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
    default: icon = ":/icons/connect_4"; break;
506
    }
507
    labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
508
    labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
509
510
}

511
void BitcoinGUI::setNumBlocks(int count, int nTotalBlocks)
512
{
513
514
515
    // Prevent orphan statusbar messages (e.g. hover Quit in main menu, wait until chain-sync starts -> garbelled text)
    statusBar()->clearMessage();

516
    // don't show / hide progress bar and its label if we have no connection to the network
517
518
    enum BlockSource blockSource = clientModel ? clientModel->getBlockSource() : BLOCK_SOURCE_NONE;
    if (blockSource == BLOCK_SOURCE_NONE || (blockSource == BLOCK_SOURCE_NETWORK && clientModel->getNumConnections() == 0))
519
520
521
522
    {
        progressBarLabel->setVisible(false);
        progressBar->setVisible(false);

523
        return;
524
525
    }

526
527
    QString tooltip;

528
529
530
531
532
    QString importText;
    switch (blockSource) {
    case BLOCK_SOURCE_NONE:
    case BLOCK_SOURCE_NETWORK:
        importText = tr("Synchronizing with network...");
Pieter Wuille's avatar
Pieter Wuille committed
533
        break;
534
535
    case BLOCK_SOURCE_DISK:
        importText = tr("Importing blocks from disk...");
Pieter Wuille's avatar
Pieter Wuille committed
536
        break;
537
538
539
540
    case BLOCK_SOURCE_REINDEX:
        importText = tr("Reindexing blocks on disk...");
    }

541
    QDateTime lastBlockDate = clientModel->getLastBlockDate();
542
543
    QDateTime currentDate = QDateTime::currentDateTime();
    int secs = lastBlockDate.secsTo(currentDate);
544

545
    if(count < nTotalBlocks)
546
    {
547
        tooltip = tr("Processed %1 of %2 (estimated) blocks of transaction history.").arg(count).arg(nTotalBlocks);
548
549
550
    }
    else
    {
551
        tooltip = tr("Processed %1 blocks of transaction history.").arg(count);
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(".<br>") + tooltip;
558
        labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
559

560
        walletFrame->showOutOfSyncWarning(false);
561
562
563

        progressBarLabel->setVisible(false);
        progressBar->setVisible(false);
564
    }
565
566
    else
    {
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
        // Represent time from last generated block in human readable text
        QString timeBehindText;
        if(secs < 48*60*60)
        {
            timeBehindText = tr("%n hour(s)","",secs/(60*60));
        }
        else if(secs < 14*24*60*60)
        {
            timeBehindText = tr("%n day(s)","",secs/(24*60*60));
        }
        else
        {
            timeBehindText = tr("%n week(s)","",secs/(7*24*60*60));
        }

        progressBarLabel->setText(importText);
        progressBarLabel->setVisible(true);
        progressBar->setFormat(tr("%1 behind").arg(timeBehindText));
585
586
        progressBar->setMaximum(1000000000);
        progressBar->setValue(clientModel->getVerificationProgress() * 1000000000.0 + 0.5);
587
588
        progressBar->setVisible(true);

589
        tooltip = tr("Catching up...") + QString("<br>") + tooltip;
590
        labelBlocksIcon->setMovie(syncIconMovie);
591
592
593
        if(count != prevBlocks)
            syncIconMovie->jumpToNextFrame();
        prevBlocks = count;
594

595
        walletFrame->showOutOfSyncWarning(true);
596

597
        tooltip += QString("<br>");
598
599
600
        tooltip += tr("Last received block was generated %1 ago.").arg(timeBehindText);
        tooltip += QString("<br>");
        tooltip += tr("Transactions after this will not yet be visible.");
601
    }
602

603
604
605
    // Don't word-wrap this (fixed-width) tooltip
    tooltip = QString("<nobr>") + tooltip + QString("</nobr>");

606
    labelBlocksIcon->setToolTip(tooltip);
607
608
    progressBarLabel->setToolTip(tooltip);
    progressBar->setToolTip(tooltip);
609
610
}

611
void BitcoinGUI::message(const QString &title, const QString &message, unsigned int style, bool *ret)
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
612
{
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
    QString strTitle = tr("Bitcoin") + " - ";
    // Default to information icon
    int nMBoxIcon = QMessageBox::Information;
    int nNotifyIcon = Notificator::Information;

    // Check for usage of predefined title
    switch (style) {
    case CClientUIInterface::MSG_ERROR:
        strTitle += tr("Error");
        break;
    case CClientUIInterface::MSG_WARNING:
        strTitle += tr("Warning");
        break;
    case CClientUIInterface::MSG_INFORMATION:
        strTitle += tr("Information");
        break;
    default:
        strTitle += title; // Use supplied title
    }

    // Check for error/warning icon
    if (style & CClientUIInterface::ICON_ERROR) {
        nMBoxIcon = QMessageBox::Critical;
        nNotifyIcon = Notificator::Critical;
637
    }
638
639
640
641
642
643
    else if (style & CClientUIInterface::ICON_WARNING) {
        nMBoxIcon = QMessageBox::Warning;
        nNotifyIcon = Notificator::Warning;
    }

    // Display message
644
    if (style & CClientUIInterface::MODAL) {
645
646
647
648
649
650
        // Check for buttons, use OK as default, if none was supplied
        QMessageBox::StandardButton buttons;
        if (!(buttons = (QMessageBox::StandardButton)(style & CClientUIInterface::BTN_MASK)))
            buttons = QMessageBox::Ok;

        QMessageBox mBox((QMessageBox::Icon)nMBoxIcon, strTitle, message, buttons);
651
652
653
        int r = mBox.exec();
        if (ret != NULL)
            *ret = r == QMessageBox::Ok;
654
655
656
    }
    else
        notificator->notify((Notificator::Class)nNotifyIcon, strTitle, message);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
657
}
658
659
660

void BitcoinGUI::changeEvent(QEvent *e)
{
661
    QMainWindow::changeEvent(e);
Philip Kaufmann's avatar
Philip Kaufmann committed
662
#ifndef Q_OS_MAC // Ignored on Mac
663
    if(e->type() == QEvent::WindowStateChange)
664
    {
665
        if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
666
        {
667
            QWindowStateChangeEvent *wsevt = static_cast<QWindowStateChangeEvent*>(e);
668
            if(!(wsevt->oldState() & Qt::WindowMinimized) && isMinimized())
669
            {
670
671
                QTimer::singleShot(0, this, SLOT(hide()));
                e->ignore();
672
673
674
            }
        }
    }
675
#endif
676
677
678
679
}

void BitcoinGUI::closeEvent(QCloseEvent *event)
{
680
    if(clientModel)
681
    {
Philip Kaufmann's avatar
Philip Kaufmann committed
682
#ifndef Q_OS_MAC // Ignored on Mac
683
684
685
        if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
           !clientModel->getOptionsModel()->getMinimizeOnClose())
        {
686
            QApplication::quit();
687
        }
688
#endif
689
    }
690
691
    QMainWindow::closeEvent(event);
}
Wladimir J. van der Laan's avatar
ask fee    
Wladimir J. van der Laan committed
692
693
694

void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
{
695
696
697
    QString strMessage = tr("This transaction is over the size limit. You can still send it for a fee of %1, "
        "which goes to the nodes that process your transaction and helps to support the network. "
        "Do you want to pay the fee?").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
Wladimir J. van der Laan's avatar
ask fee    
Wladimir J. van der Laan committed
698
    QMessageBox::StandardButton retval = QMessageBox::question(
699
          this, tr("Confirm transaction fee"), strMessage,
Wladimir J. van der Laan's avatar
ask fee    
Wladimir J. van der Laan committed
700
701
702
          QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
    *payFee = (retval == QMessageBox::Yes);
}
703

704
void BitcoinGUI::incomingTransaction(const QString& date, int unit, qint64 amount, const QString& type, const QString& address)
705
{
706
    // On new transaction, make an info balloon
707
708
709
710
711
712
    message((amount)<0 ? tr("Sent transaction") : tr("Incoming transaction"),
             tr("Date: %1\n"
                "Amount: %2\n"
                "Type: %3\n"
                "Address: %4\n")
                  .arg(date)
713
                  .arg(BitcoinUnits::formatWithUnit(unit, amount, true))
714
715
                  .arg(type)
                  .arg(address), CClientUIInterface::MSG_INFORMATION);
716
}
717

718
719
void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
{
720
    // Accept only URIs
721
722
723
724
725
726
727
728
    if(event->mimeData()->hasUrls())
        event->acceptProposedAction();
}

void BitcoinGUI::dropEvent(QDropEvent *event)
{
    if(event->mimeData()->hasUrls())
    {
729
        int nValidUrisFound = 0;
730
731
        QList<QUrl> uris = event->mimeData()->urls();
        foreach(const QUrl &uri, uris)
732
        {
733
            if (walletFrame->handleURI(uri.toString()))
734
                nValidUrisFound++;
735
        }
736
737
738

        // if valid URIs were found
        if (nValidUrisFound)
739
            walletFrame->gotoSendCoinsPage();
740
        else
741
742
            message(tr("URI handling"), tr("URI can not be parsed! This can be caused by an invalid Bitcoin address or malformed URI parameters."),
                      CClientUIInterface::ICON_WARNING);
743
744
745
746
747
    }

    event->acceptProposedAction();
}

748
749
750
751
752
753
754
755
756
757
758
759
bool BitcoinGUI::eventFilter(QObject *object, QEvent *event)
{
    // Catch status tip events
    if (event->type() == QEvent::StatusTip)
    {
        // Prevent adding text from setStatusTip(), if we currently use the status bar for displaying other stuff
        if (progressBarLabel->isVisible() && progressBar->isVisible())
            return true;
    }
    return QMainWindow::eventFilter(object, event);
}

760
void BitcoinGUI::handleURI(QString strURI)
761
{
762
    // URI has to be valid
763
    if (!walletFrame->handleURI(strURI))
764
765
        message(tr("URI handling"), tr("URI can not be parsed! This can be caused by an invalid Bitcoin address or malformed URI parameters."),
                  CClientUIInterface::ICON_WARNING);
766
767
}

768
769
770
771
772
773
void BitcoinGUI::setEncryptionStatus(int status)
{
    switch(status)
    {
    case WalletModel::Unencrypted:
        labelEncryptionIcon->hide();
774
775
776
        encryptWalletAction->setChecked(false);
        changePassphraseAction->setEnabled(false);
        encryptWalletAction->setEnabled(true);
777
778
779
780
781
        break;
    case WalletModel::Unlocked:
        labelEncryptionIcon->show();
        labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
        labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
782
783
784
        encryptWalletAction->setChecked(true);
        changePassphraseAction->setEnabled(true);
        encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
785
786
787
788
789
        break;
    case WalletModel::Locked:
        labelEncryptionIcon->show();
        labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
        labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
790
791
792
        encryptWalletAction->setChecked(true);
        changePassphraseAction->setEnabled(true);
        encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
793
794
795
        break;
    }
}
796
797
798

void BitcoinGUI::encryptWallet(bool status)
{
799
    walletFrame->encryptWallet(status);
800
801
}

sje397's avatar
sje397 committed
802
803
void BitcoinGUI::backupWallet()
{
804
    walletFrame->backupWallet();
sje397's avatar
sje397 committed
805
806
}

807
808
void BitcoinGUI::changePassphrase()
{
809
    walletFrame->changePassphrase();
810
811
812
813
}

void BitcoinGUI::unlockWallet()
{
814
    walletFrame->unlockWallet();
815
}
816

817
void BitcoinGUI::showNormalIfMinimized(bool fToggleHidden)
818
{
819
820
821
    // activateWindow() (sometimes) helps with keyboard focus on Windows
    if (isHidden())
    {
822
        show();
823
824
825
826
        activateWindow();
    }
    else if (isMinimized())
    {
827
        showNormal();
828
829
830
831
832
833
834
835
836
837
838
839
840
841
        activateWindow();
    }
    else if (GUIUtil::isObscured(this))
    {
        raise();
        activateWindow();
    }
    else if(fToggleHidden)
        hide();
}

void BitcoinGUI::toggleHidden()
{
    showNormalIfMinimized(true);
842
}
843
844
845
846
847
848

void BitcoinGUI::detectShutdown()
{
    if (ShutdownRequested())
        QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
}