bitcoingui.cpp 32.3 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
 */
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
7
8
#include "bitcoingui.h"
#include "transactiontablemodel.h"
9
#include "addressbookpage.h"
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
10
#include "sendcoinsdialog.h"
11
#include "signverifymessagedialog.h"
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
12
13
#include "optionsdialog.h"
#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
#include "rpcconsole.h"
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
28

29
30
31
32
#ifdef Q_WS_MAC
#include "macdockiconhandler.h"
#endif

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

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

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

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

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

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

91
92
    // Create the tray icon (or setup the dock icon)
    createTrayIcon();
93

94
    // Create tabs
95
    overviewPage = new OverviewPage();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
96

97
98
    transactionsPage = new QWidget(this);
    QVBoxLayout *vbox = new QVBoxLayout();
99
100
    transactionView = new TransactionView(this);
    vbox->addWidget(transactionView);
101
102
    transactionsPage->setLayout(vbox);

103
104
105
106
107
108
    addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);

    receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);

    sendCoinsPage = new SendCoinsDialog(this);

109
    signVerifyMessageDialog = new SignVerifyMessageDialog(this);
110

111
112
113
    centralWidget = new QStackedWidget(this);
    centralWidget->addWidget(overviewPage);
    centralWidget->addWidget(transactionsPage);
114
115
116
    centralWidget->addWidget(addressBookPage);
    centralWidget->addWidget(receiveCoinsPage);
    centralWidget->addWidget(sendCoinsPage);
117
#ifdef FIRST_CLASS_MESSAGING
118
    centralWidget->addWidget(signVerifyMessageDialog);
119
#endif
120
    setCentralWidget(centralWidget);
121

122
    // Create status bar
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
123
    statusBar();
124

125
    // Status bar notification icons
126
    QFrame *frameBlocks = new QFrame();
127
128
129
    frameBlocks->setContentsMargins(0,0,0,0);
    frameBlocks->setMinimumWidth(56);
    frameBlocks->setMaximumWidth(56);
130
131
132
    QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
    frameBlocksLayout->setContentsMargins(3,0,3,0);
    frameBlocksLayout->setSpacing(3);
133
    labelEncryptionIcon = new QLabel();
134
    labelConnectionsIcon = new QLabel();
135
    labelBlocksIcon = new QLabel();
136
    frameBlocksLayout->addStretch();
137
138
    frameBlocksLayout->addWidget(labelEncryptionIcon);
    frameBlocksLayout->addStretch();
139
140
    frameBlocksLayout->addWidget(labelConnectionsIcon);
    frameBlocksLayout->addStretch();
141
142
    frameBlocksLayout->addWidget(labelBlocksIcon);
    frameBlocksLayout->addStretch();
143

144
145
    // Progress bar and label for blocks download
    progressBarLabel = new QLabel();
146
147
    progressBarLabel->setVisible(false);
    progressBar = new QProgressBar();
148
    progressBar->setAlignment(Qt::AlignCenter);
149
150
151
152
    progressBar->setVisible(false);

    statusBar()->addWidget(progressBarLabel);
    statusBar()->addWidget(progressBar);
153
    statusBar()->addPermanentWidget(frameBlocks);
154

155
156
    syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);

157
    // Clicking on a transaction on the overview page simply sends you to transaction history page
158
    connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
159
    connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), transactionView, SLOT(focusTransaction(QModelIndex)));
160

161
162
    // Doubleclicking on a transaction on the transaction history page shows details
    connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
163

164
165
166
    rpcConsole = new RPCConsole(this);
    connect(openRPCConsoleAction, SIGNAL(triggered()), rpcConsole, SLOT(show()));

167
168
169
170
171
    // Clicking on "Verify Message" in the address book sends you to the verify message tab
    connect(addressBookPage, SIGNAL(verifyMessage(QString)), this, SLOT(gotoVerifyMessageTab(QString)));
    // Clicking on "Sign Message" in the receive coins page sends you to the sign message tab
    connect(receiveCoinsPage, SIGNAL(signMessage(QString)), this, SLOT(gotoSignMessageTab(QString)));

172
    gotoOverviewPage();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
173
174
}

175
176
BitcoinGUI::~BitcoinGUI()
{
177
178
    if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
        trayIcon->hide();
179
180
181
182
183
#ifdef Q_WS_MAC
    delete appMenuBar;
#endif
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
184
185
void BitcoinGUI::createActions()
{
186
    QActionGroup *tabGroup = new QActionGroup(this);
187

188
    overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
189
    overviewAction->setToolTip(tr("Show general overview of wallet"));
190
    overviewAction->setCheckable(true);
191
    overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
192
    tabGroup->addAction(overviewAction);
193

194
    historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
195
    historyAction->setToolTip(tr("Browse transaction history"));
196
    historyAction->setCheckable(true);
197
    historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
198
199
    tabGroup->addAction(historyAction);

200
201
202
    addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
    addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
    addressBookAction->setCheckable(true);
203
    addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
204
205
206
207
208
    tabGroup->addAction(addressBookAction);

    receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
    receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
    receiveCoinsAction->setCheckable(true);
209
    receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
210
211
212
    tabGroup->addAction(receiveCoinsAction);

    sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
213
    sendCoinsAction->setToolTip(tr("Send coins to a Bitcoin address"));
214
    sendCoinsAction->setCheckable(true);
215
    sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
216
217
    tabGroup->addAction(sendCoinsAction);

218
219
220
221
222
223
224
225
    signMessageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message..."), this);
    signMessageAction->setToolTip(tr("Sign a message to prove you own a Bitcoin address"));
    tabGroup->addAction(signMessageAction);

    verifyMessageAction = new QAction(QIcon(":/icons/transaction_0"), tr("&Verify message..."), this);
    verifyMessageAction->setToolTip(tr("Verify a message to ensure it was signed with a specified Bitcoin address"));
    tabGroup->addAction(verifyMessageAction);

226
#ifdef FIRST_CLASS_MESSAGING
227
228
229
230
    firstClassMessagingAction = new QAction(QIcon(":/icons/edit"), tr("S&ignatures"), this);
    firstClassMessagingAction->setToolTip(signMessageAction->toolTip() + QString(". / ") + verifyMessageAction->toolTip() + QString("."));
    firstClassMessagingAction->setCheckable(true);
    tabGroup->addAction(firstClassMessagingAction);
231
232
#endif

233
    connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
234
    connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
235
    connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
236
    connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
237
    connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
238
    connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
239
    connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
240
    connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
241
    connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
242
    connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
243
244
245
246
247
248
249
250
251
    connect(signMessageAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
    connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
    connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
    connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));
#ifdef FIRST_CLASS_MESSAGING
    connect(firstClassMessagingAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
    // Always start with the sign message tab for FIRST_CLASS_MESSAGING
    connect(firstClassMessagingAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
#endif
252

253
    quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
254
    quitAction->setToolTip(tr("Quit application"));
255
    quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
256
    quitAction->setMenuRole(QAction::QuitRole);
257
    aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About Bitcoin"), this);
258
    aboutAction->setToolTip(tr("Show information about Bitcoin"));
259
260
261
262
    aboutAction->setMenuRole(QAction::AboutRole);
    aboutQtAction = new QAction(tr("About &Qt"), this);
    aboutQtAction->setToolTip(tr("Show information about Qt"));
    aboutQtAction->setMenuRole(QAction::AboutQtRole);
263
    optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
264
    optionsAction->setToolTip(tr("Modify configuration options for Bitcoin"));
265
    optionsAction->setMenuRole(QAction::PreferencesRole);
266
    toggleHideAction = new QAction(QIcon(":/icons/bitcoin"), tr("Show/Hide &Bitcoin"), this);
267
    toggleHideAction->setToolTip(tr("Show or hide the Bitcoin window"));
268
    exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
269
    exportAction->setToolTip(tr("Export the data in the current tab to a file"));
270
    encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet..."), this);
271
272
    encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
    encryptWalletAction->setCheckable(true);
273
    backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet..."), this);
sje397's avatar
sje397 committed
274
    backupWalletAction->setToolTip(tr("Backup wallet to another location"));
275
    changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase..."), this);
276
    changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
277
    openRPCConsoleAction = new QAction(QIcon(":/icons/debugwindow"), tr("&Debug window"), this);
278
    openRPCConsoleAction->setToolTip(tr("Open debugging and diagnostic console"));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
279

280
281
282
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
283
    connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
284
    connect(toggleHideAction, SIGNAL(triggered()), this, SLOT(toggleHidden()));
285
    connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
sje397's avatar
sje397 committed
286
    connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
287
    connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
288
289
}

290
291
292
293
294
295
296
297
298
299
300
301
void BitcoinGUI::createMenuBar()
{
#ifdef Q_WS_MAC
    // 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"));
302
    file->addAction(backupWalletAction);
303
    file->addAction(exportAction);
304
#ifndef FIRST_CLASS_MESSAGING
305
    file->addAction(signMessageAction);
306
    file->addAction(verifyMessageAction);
307
#endif
308
    file->addSeparator();
309
310
311
312
313
314
315
316
317
    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"));
318
319
    help->addAction(openRPCConsoleAction);
    help->addSeparator();
320
    help->addAction(aboutAction);
321
    help->addAction(aboutQtAction);
322
323
324
325
326
327
328
329
330
331
332
}

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);
333
#ifdef FIRST_CLASS_MESSAGING
334
    toolbar->addAction(firstClassMessagingAction);
335
#endif
336
337
338
339
340
341

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

342
void BitcoinGUI::setClientModel(ClientModel *clientModel)
343
{
344
    this->clientModel = clientModel;
345
    if(clientModel)
346
    {
347
        // Replace some strings and icons, when using the testnet
348
349
        if(clientModel->isTestNet())
        {
350
            setWindowTitle(windowTitle() + QString(" ") + tr("[testnet]"));
351
#ifndef Q_WS_MAC
352
            qApp->setWindowIcon(QIcon(":icons/bitcoin_testnet"));
353
            setWindowIcon(QIcon(":icons/bitcoin_testnet"));
354
#else
355
            MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
356
#endif
357
358
            if(trayIcon)
            {
359
                trayIcon->setToolTip(tr("Bitcoin client") + QString(" ") + tr("[testnet]"));
360
                trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
361
                toggleHideAction->setIcon(QIcon(":/icons/toolbar_testnet"));
362
            }
363
364

            aboutAction->setIcon(QIcon(":/icons/toolbar_testnet"));
365
366
        }

367
368
369
        // Keep up to date with client
        setNumConnections(clientModel->getNumConnections());
        connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
370

371
372
        setNumBlocks(clientModel->getNumBlocks(), clientModel->getNumBlocksOfPeers());
        connect(clientModel, SIGNAL(numBlocksChanged(int,int)), this, SLOT(setNumBlocks(int,int)));
373

374
        // Report errors from network/worker thread
375
        connect(clientModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
376
377

        rpcConsole->setClientModel(clientModel);
Philip Kaufmann's avatar
Philip Kaufmann committed
378
379
        addressBookPage->setOptionsModel(clientModel->getOptionsModel());
        receiveCoinsPage->setOptionsModel(clientModel->getOptionsModel());
380
    }
381
382
383
384
385
}

void BitcoinGUI::setWalletModel(WalletModel *walletModel)
{
    this->walletModel = walletModel;
386
387
388
    if(walletModel)
    {
        // Report errors from wallet thread
389
        connect(walletModel, SIGNAL(error(QString,QString,bool)), this, SLOT(error(QString,QString,bool)));
390

391
392
        // Put transaction list in tabs
        transactionView->setModel(walletModel);
393

394
395
396
397
        overviewPage->setModel(walletModel);
        addressBookPage->setModel(walletModel->getAddressTableModel());
        receiveCoinsPage->setModel(walletModel->getAddressTableModel());
        sendCoinsPage->setModel(walletModel);
398
        signVerifyMessageDialog->setModel(walletModel);
399

400
401
        setEncryptionStatus(walletModel->getEncryptionStatus());
        connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
402

403
404
405
        // Balloon popup for new transaction
        connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
                this, SLOT(incomingTransaction(QModelIndex,int,int)));
406

407
408
409
        // Ask for passphrase if needed
        connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
    }
410
411
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
412
413
void BitcoinGUI::createTrayIcon()
{
414
415
    QMenu *trayIconMenu;
#ifndef Q_WS_MAC
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
416
    trayIcon = new QSystemTrayIcon(this);
417
    trayIconMenu = new QMenu(this);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
418
    trayIcon->setContextMenu(trayIconMenu);
419
    trayIcon->setToolTip(tr("Bitcoin client"));
420
    trayIcon->setIcon(QIcon(":/icons/toolbar"));
421
422
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
423
    trayIcon->show();
424
425
426
427
428
429
430
#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
431
    trayIconMenu->addAction(toggleHideAction);
432
    trayIconMenu->addSeparator();
433
434
    trayIconMenu->addAction(sendCoinsAction);
    trayIconMenu->addAction(receiveCoinsAction);
435
436
437
#ifndef FIRST_CLASS_MESSAGING
    trayIconMenu->addSeparator();
#endif
438
    trayIconMenu->addAction(signMessageAction);
439
    trayIconMenu->addAction(verifyMessageAction);
440
441
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(optionsAction);
442
    trayIconMenu->addAction(openRPCConsoleAction);
443
444
445
446
#ifndef Q_WS_MAC // This is built-in on Mac
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
#endif
447

448
    notificator = new Notificator(qApp->applicationName(), trayIcon);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
449
450
}

451
#ifndef Q_WS_MAC
452
453
void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
454
    if(reason == QSystemTrayIcon::Trigger)
455
    {
456
        // Click on system tray icon triggers "show/hide Bitcoin"
457
        toggleHideAction->trigger();
458
459
    }
}
460
#endif
461

462
463
void BitcoinGUI::optionsClicked()
{
464
465
    if(!clientModel || !clientModel->getOptionsModel())
        return;
466
    OptionsDialog dlg;
467
    dlg.setModel(clientModel->getOptionsModel());
468
    dlg.exec();
469
470
}

471
void BitcoinGUI::aboutClicked()
472
{
473
    AboutDialog dlg;
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
474
    dlg.setModel(clientModel);
475
    dlg.exec();
476
477
}

478
479
void BitcoinGUI::setNumConnections(int count)
{
480
481
482
    QString icon;
    switch(count)
    {
483
484
485
486
487
    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;
488
    }
489
    labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
490
    labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
491
492
}

493
void BitcoinGUI::setNumBlocks(int count, int nTotalBlocks)
494
{
495
496
497
498
499
500
    // don't show / hide progressBar and it's label if we have no connection(s) to the network
    if (!clientModel || clientModel->getNumConnections() == 0)
    {
        progressBarLabel->setVisible(false);
        progressBar->setVisible(false);

501
        return;
502
503
    }

504
    QString strStatusBarWarnings = clientModel->getStatusBarWarnings();
505
506
    QString tooltip;

507
    if(count < nTotalBlocks)
508
    {
509
        int nRemainingBlocks = nTotalBlocks - count;
510
        float nPercentageDone = count / (nTotalBlocks * 0.01f);
511

512
        if (strStatusBarWarnings.isEmpty())
513
514
        {
            progressBarLabel->setText(tr("Synchronizing with network..."));
515
            progressBarLabel->setVisible(true);
516
            progressBar->setFormat(tr("~%n block(s) remaining", "", nRemainingBlocks));
517
            progressBar->setMaximum(nTotalBlocks);
518
            progressBar->setValue(count);
519
            progressBar->setVisible(true);
520
        }
521

522
        tooltip = tr("Downloaded %1 of %2 blocks of transaction history (%3% done).").arg(count).arg(nTotalBlocks).arg(nPercentageDone, 0, 'f', 2);
523
524
525
    }
    else
    {
526
        if (strStatusBarWarnings.isEmpty())
527
            progressBarLabel->setVisible(false);
528

529
        progressBar->setVisible(false);
530
        tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
531
532
    }

533
534
535
536
537
538
539
540
    // Override progressBarLabel text and hide progressBar, when we have warnings to display
    if (!strStatusBarWarnings.isEmpty())
    {
        progressBarLabel->setText(strStatusBarWarnings);
        progressBarLabel->setVisible(true);
        progressBar->setVisible(false);
    }

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

545
    // Represent time from last generated block in human readable text
546
547
548
549
550
    if(secs <= 0)
    {
        // Fully up to date. Leave text empty.
    }
    else if(secs < 60)
551
    {
552
        text = tr("%n second(s) ago","",secs);
553
554
555
556
557
558
559
560
561
562
563
564
565
    }
    else if(secs < 60*60)
    {
        text = tr("%n minute(s) ago","",secs/60);
    }
    else if(secs < 24*60*60)
    {
        text = tr("%n hour(s) ago","",secs/(60*60));
    }
    else
    {
        text = tr("%n day(s) ago","",secs/(60*60*24));
    }
566

567
    // Set icon state: spinning if catching up, tick otherwise
568
    if(secs < 90*60 && count >= nTotalBlocks)
569
    {
570
        tooltip = tr("Up to date") + QString(".<br>") + tooltip;
571
        labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
572
573

        overviewPage->showOutOfSyncWarning(false);
574
    }
575
576
    else
    {
577
        tooltip = tr("Catching up...") + QString("<br>") + tooltip;
578
579
        labelBlocksIcon->setMovie(syncIconMovie);
        syncIconMovie->start();
580
581

        overviewPage->showOutOfSyncWarning(true);
582
    }
583

584
585
    if(!text.isEmpty())
    {
586
        tooltip += QString("<br>");
587
588
        tooltip += tr("Last received block was generated %1.").arg(text);
    }
589

590
591
592
    // Don't word-wrap this (fixed-width) tooltip
    tooltip = QString("<nobr>") + tooltip + QString("</nobr>");

593
    labelBlocksIcon->setToolTip(tooltip);
594
595
    progressBarLabel->setToolTip(tooltip);
    progressBar->setToolTip(tooltip);
596
597
}

598
void BitcoinGUI::error(const QString &title, const QString &message, bool modal)
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
599
{
600
    // Report errors from network/worker thread
601
602
603
604
605
606
    if(modal)
    {
        QMessageBox::critical(this, title, message, QMessageBox::Ok, QMessageBox::Ok);
    } else {
        notificator->notify(Notificator::Critical, title, message);
    }
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
607
}
608
609
610

void BitcoinGUI::changeEvent(QEvent *e)
{
611
    QMainWindow::changeEvent(e);
612
#ifndef Q_WS_MAC // Ignored on Mac
613
    if(e->type() == QEvent::WindowStateChange)
614
    {
615
        if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
616
        {
617
            QWindowStateChangeEvent *wsevt = static_cast<QWindowStateChangeEvent*>(e);
618
            if(!(wsevt->oldState() & Qt::WindowMinimized) && isMinimized())
619
            {
620
621
                QTimer::singleShot(0, this, SLOT(hide()));
                e->ignore();
622
623
624
            }
        }
    }
625
#endif
626
627
628
629
}

void BitcoinGUI::closeEvent(QCloseEvent *event)
{
630
    if(clientModel)
631
    {
632
633
634
635
636
637
#ifndef Q_WS_MAC // Ignored on Mac
        if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
           !clientModel->getOptionsModel()->getMinimizeOnClose())
        {
            qApp->quit();
        }
638
#endif
639
    }
640
641
    QMainWindow::closeEvent(event);
}
Wladimir J. van der Laan's avatar
ask fee    
Wladimir J. van der Laan committed
642
643
644
645
646
647

void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
{
    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.  "
648
649
          "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
650
    QMessageBox::StandardButton retval = QMessageBox::question(
651
          this, tr("Confirm transaction fee"), strMessage,
Wladimir J. van der Laan's avatar
ask fee    
Wladimir J. van der Laan committed
652
653
654
          QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
    *payFee = (retval == QMessageBox::Yes);
}
655

656
657
void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
{
658
659
    if(!walletModel || !clientModel)
        return;
660
    TransactionTableModel *ttm = walletModel->getTransactionTableModel();
661
    qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
662
                    .data(Qt::EditRole).toULongLong();
663
    if(!clientModel->inInitialBlockDownload())
664
    {
665
        // On new transaction, make an info balloon
666
        // Unless the initial block download is in progress, to prevent balloon-spam
667
668
        QString date = ttm->index(start, TransactionTableModel::Date, parent)
                        .data().toString();
669
670
671
        QString type = ttm->index(start, TransactionTableModel::Type, parent)
                        .data().toString();
        QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
672
                        .data().toString();
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
        QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
                            TransactionTableModel::ToAddress, parent)
                        .data(Qt::DecorationRole));

        notificator->notify(Notificator::Information,
                            (amount)<0 ? tr("Sent transaction") :
                                         tr("Incoming transaction"),
                              tr("Date: %1\n"
                                 "Amount: %2\n"
                                 "Type: %3\n"
                                 "Address: %4\n")
                              .arg(date)
                              .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
                              .arg(type)
                              .arg(address), icon);
688
689
    }
}
690

691
void BitcoinGUI::gotoOverviewPage()
692
693
694
{
    overviewAction->setChecked(true);
    centralWidget->setCurrentWidget(overviewPage);
695

696
    exportAction->setEnabled(false);
697
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
698
699
}

700
void BitcoinGUI::gotoHistoryPage()
701
702
703
{
    historyAction->setChecked(true);
    centralWidget->setCurrentWidget(transactionsPage);
704

705
    exportAction->setEnabled(true);
706
707
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
708
709
}

710
711
712
713
void BitcoinGUI::gotoAddressBookPage()
{
    addressBookAction->setChecked(true);
    centralWidget->setCurrentWidget(addressBookPage);
714
715
716
717

    exportAction->setEnabled(true);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
718
719
720
721
722
723
}

void BitcoinGUI::gotoReceiveCoinsPage()
{
    receiveCoinsAction->setChecked(true);
    centralWidget->setCurrentWidget(receiveCoinsPage);
724
725
726
727

    exportAction->setEnabled(true);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
728
729
730
731
732
733
734
}

void BitcoinGUI::gotoSendCoinsPage()
{
    sendCoinsAction->setChecked(true);
    centralWidget->setCurrentWidget(sendCoinsPage);

735
736
    exportAction->setEnabled(false);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
737
}
738

739
void BitcoinGUI::gotoSignMessageTab(QString addr)
740
{
741
742
743
744
745
746
747
748
749
750
751
752
753
#ifdef FIRST_CLASS_MESSAGING
    firstClassMessagingAction->setChecked(true);
    centralWidget->setCurrentWidget(signVerifyMessageDialog);

    exportAction->setEnabled(false);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);

    signVerifyMessageDialog->showTab_SM(false);
#else
    // call show() in showTab_SM()
    signVerifyMessageDialog->showTab_SM(true);
#endif

754
    if(!addr.isEmpty())
755
756
        signVerifyMessageDialog->setAddress_SM(addr);
}
757

758
759
void BitcoinGUI::gotoVerifyMessageTab(QString addr)
{
760
#ifdef FIRST_CLASS_MESSAGING
761
762
    firstClassMessagingAction->setChecked(true);
    centralWidget->setCurrentWidget(signVerifyMessageDialog);
763
764
765

    exportAction->setEnabled(false);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
766
767

    signVerifyMessageDialog->showTab_VM(false);
768
#else
769
770
    // call show() in showTab_VM()
    signVerifyMessageDialog->showTab_VM(true);
771
#endif
772
773
774

    if(!addr.isEmpty())
        signVerifyMessageDialog->setAddress_VM(addr);
775
776
}

777
778
void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
{
779
    // Accept only URIs
780
781
782
783
784
785
786
787
    if(event->mimeData()->hasUrls())
        event->acceptProposedAction();
}

void BitcoinGUI::dropEvent(QDropEvent *event)
{
    if(event->mimeData()->hasUrls())
    {
788
        int nValidUrisFound = 0;
789
790
        QList<QUrl> uris = event->mimeData()->urls();
        foreach(const QUrl &uri, uris)
791
        {
792
793
            if (sendCoinsPage->handleURI(uri.toString()))
                nValidUrisFound++;
794
        }
795
796
797
798
799
800

        // if valid URIs were found
        if (nValidUrisFound)
            gotoSendCoinsPage();
        else
            notificator->notify(Notificator::Warning, tr("URI handling"), tr("URI can not be parsed! This can be caused by an invalid Bitcoin address or malformed URI parameters."));
801
802
803
804
805
    }

    event->acceptProposedAction();
}

806
void BitcoinGUI::handleURI(QString strURI)
807
{
808
809
810
811
812
813
814
815
    // URI has to be valid
    if (sendCoinsPage->handleURI(strURI))
    {
        showNormalIfMinimized();
        gotoSendCoinsPage();
    }
    else
        notificator->notify(Notificator::Warning, tr("URI handling"), tr("URI can not be parsed! This can be caused by an invalid Bitcoin address or malformed URI parameters."));
816
817
}

818
819
820
821
822
823
void BitcoinGUI::setEncryptionStatus(int status)
{
    switch(status)
    {
    case WalletModel::Unencrypted:
        labelEncryptionIcon->hide();
824
825
826
        encryptWalletAction->setChecked(false);
        changePassphraseAction->setEnabled(false);
        encryptWalletAction->setEnabled(true);
827
828
829
830
831
        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>"));
832
833
834
        encryptWalletAction->setChecked(true);
        changePassphraseAction->setEnabled(true);
        encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
835
836
837
838
839
        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>"));
840
841
842
        encryptWalletAction->setChecked(true);
        changePassphraseAction->setEnabled(true);
        encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
843
844
845
        break;
    }
}
846
847
848

void BitcoinGUI::encryptWallet(bool status)
{
849
850
    if(!walletModel)
        return;
851
852
853
854
855
856
857
858
    AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
                                     AskPassphraseDialog::Decrypt, this);
    dlg.setModel(walletModel);
    dlg.exec();

    setEncryptionStatus(walletModel->getEncryptionStatus());
}

sje397's avatar
sje397 committed
859
860
861
862
863
864
865
866
867
868
869
void BitcoinGUI::backupWallet()
{
    QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
    QString filename = QFileDialog::getSaveFileName(this, tr("Backup Wallet"), saveDir, tr("Wallet Data (*.dat)"));
    if(!filename.isEmpty()) {
        if(!walletModel->backupWallet(filename)) {
            QMessageBox::warning(this, tr("Backup Failed"), tr("There was an error trying to save the wallet data to the new location."));
        }
    }
}

870
871
872
873
874
875
876
877
878
void BitcoinGUI::changePassphrase()
{
    AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
    dlg.setModel(walletModel);
    dlg.exec();
}

void BitcoinGUI::unlockWallet()
{
879
880
    if(!walletModel)
        return;
881
    // Unlock wallet when requested by wallet model
882
883
884
885
886
887
888
    if(walletModel->getEncryptionStatus() == WalletModel::Locked)
    {
        AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
        dlg.setModel(walletModel);
        dlg.exec();
    }
}
889

890
void BitcoinGUI::showNormalIfMinimized(bool fToggleHidden)
891
{
892
893
894
    // activateWindow() (sometimes) helps with keyboard focus on Windows
    if (isHidden())
    {
895
        show();
896
897
898
899
        activateWindow();
    }
    else if (isMinimized())
    {
900
        showNormal();
901
902
903
904
905
906
907
908
909
910
911
912
913
914
        activateWindow();
    }
    else if (GUIUtil::isObscured(this))
    {
        raise();
        activateWindow();
    }
    else if(fToggleHidden)
        hide();
}

void BitcoinGUI::toggleHidden()
{
    showNormalIfMinimized(true);
915
}