bitcoingui.cpp 26.2 KB
Newer Older
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
1
/*
2
3
 * Qt4 bitcoin GUI.
 *
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
4
 * W.J. van der Laan 2011
5
 * The Bitcoin Developers 2011
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 "messagepage.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"
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
26

27
28
29
30
#ifdef Q_WS_MAC
#include "macdockiconhandler.h"
#endif

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
31
32
33
34
35
#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QIcon>
36
#include <QTabWidget>
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
37
38
39
40
41
42
#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
43
#include <QLocale>
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
44
#include <QMessageBox>
45
#include <QProgressBar>
46
#include <QStackedWidget>
47
#include <QDateTime>
48
#include <QMovie>
49

50
51
52
#include <QDragEnterEvent>
#include <QUrl>

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
53
54
55
#include <iostream>

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

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

79
80
    // Create application menu bar
    createMenuBar();
81

82
83
    // Create the toolbars
    createToolBars();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
84

85
86
    // Create the tray icon (or setup the dock icon)
    createTrayIcon();
87

88
    // Create tabs
89
    overviewPage = new OverviewPage();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
90

91
92
    transactionsPage = new QWidget(this);
    QVBoxLayout *vbox = new QVBoxLayout();
93
94
    transactionView = new TransactionView(this);
    vbox->addWidget(transactionView);
95
96
    transactionsPage->setLayout(vbox);

97
98
99
100
101
102
    addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);

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

    sendCoinsPage = new SendCoinsDialog(this);

103
104
    messagePage = new MessagePage(this);

105
106
107
    centralWidget = new QStackedWidget(this);
    centralWidget->addWidget(overviewPage);
    centralWidget->addWidget(transactionsPage);
108
109
110
    centralWidget->addWidget(addressBookPage);
    centralWidget->addWidget(receiveCoinsPage);
    centralWidget->addWidget(sendCoinsPage);
111
112
113
#ifdef FIRST_CLASS_MESSAGING
    centralWidget->addWidget(messagePage);
#endif
114
    setCentralWidget(centralWidget);
115

116
    // Create status bar
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
117
    statusBar();
118

119
    // Status bar notification icons
120
    QFrame *frameBlocks = new QFrame();
121
122
123
    frameBlocks->setContentsMargins(0,0,0,0);
    frameBlocks->setMinimumWidth(56);
    frameBlocks->setMaximumWidth(56);
124
125
126
    QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
    frameBlocksLayout->setContentsMargins(3,0,3,0);
    frameBlocksLayout->setSpacing(3);
127
    labelEncryptionIcon = new QLabel();
128
    labelConnectionsIcon = new QLabel();
129
    labelBlocksIcon = new QLabel();
130
    frameBlocksLayout->addStretch();
131
132
    frameBlocksLayout->addWidget(labelEncryptionIcon);
    frameBlocksLayout->addStretch();
133
134
    frameBlocksLayout->addWidget(labelConnectionsIcon);
    frameBlocksLayout->addStretch();
135
136
    frameBlocksLayout->addWidget(labelBlocksIcon);
    frameBlocksLayout->addStretch();
137

138
    // Progress bar for blocks download
139
    progressBarLabel = new QLabel(tr("Synchronizing with network..."));
140
141
    progressBarLabel->setVisible(false);
    progressBar = new QProgressBar();
142
    progressBar->setToolTip(tr("Block chain synchronization in progress"));
143
144
145
146
    progressBar->setVisible(false);

    statusBar()->addWidget(progressBarLabel);
    statusBar()->addWidget(progressBar);
147
    statusBar()->addPermanentWidget(frameBlocks);
148

149
150
    syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);

151
    // Clicking on a transaction on the overview page simply sends you to transaction history page
152
153
    connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));

154
155
    // Doubleclicking on a transaction on the transaction history page shows details
    connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
156
157

    gotoOverviewPage();
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
158
159
}

160
161
162
163
164
165
166
BitcoinGUI::~BitcoinGUI()
{
#ifdef Q_WS_MAC
    delete appMenuBar;
#endif
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
167
168
void BitcoinGUI::createActions()
{
169
    QActionGroup *tabGroup = new QActionGroup(this);
170

171
    overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
172
    overviewAction->setToolTip(tr("Show general overview of wallet"));
173
    overviewAction->setCheckable(true);
174
    overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
175
    tabGroup->addAction(overviewAction);
176

177
    historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
178
    historyAction->setToolTip(tr("Browse transaction history"));
179
    historyAction->setCheckable(true);
180
    historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
181
182
    tabGroup->addAction(historyAction);

183
184
185
    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);
186
    addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
187
188
189
190
191
    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);
192
    receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
193
194
195
196
197
    tabGroup->addAction(receiveCoinsAction);

    sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
    sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
    sendCoinsAction->setCheckable(true);
198
    sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
199
200
    tabGroup->addAction(sendCoinsAction);

201
202
203
204
205
206
207
    messageAction = new QAction(QIcon(":/icons/edit"), tr("Sign &message"), this);
    messageAction->setToolTip(tr("Prove you control an address"));
#ifdef FIRST_CLASS_MESSAGING
    messageAction->setCheckable(true);
#endif
    tabGroup->addAction(messageAction);

208
    connect(overviewAction, SIGNAL(triggered()), this, SLOT(show()));
209
    connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
210
    connect(historyAction, SIGNAL(triggered()), this, SLOT(show()));
211
    connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
212
    connect(addressBookAction, SIGNAL(triggered()), this, SLOT(show()));
213
    connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
214
    connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(show()));
215
    connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
216
    connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(show()));
217
    connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
218
219
    connect(messageAction, SIGNAL(triggered()), this, SLOT(show()));
    connect(messageAction, SIGNAL(triggered()), this, SLOT(gotoMessagePage()));
220

221
    quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
222
    quitAction->setToolTip(tr("Quit application"));
223
    quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
224
225
    quitAction->setMenuRole(QAction::QuitRole);
    aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About %1").arg(qApp->applicationName()), this);
226
    aboutAction->setToolTip(tr("Show information about Bitcoin"));
227
228
229
230
    aboutAction->setMenuRole(QAction::AboutRole);
    aboutQtAction = new QAction(tr("About &Qt"), this);
    aboutQtAction->setToolTip(tr("Show information about Qt"));
    aboutQtAction->setMenuRole(QAction::AboutQtRole);
231
232
    optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
    optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
233
    optionsAction->setMenuRole(QAction::PreferencesRole);
234
235
    openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
    openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
236
    exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
237
    exportAction->setToolTip(tr("Export the current view to a file"));
238
239
240
241
242
    encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
    encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
    encryptWalletAction->setCheckable(true);
    changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
    changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
243

244
245
246
    connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
    connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
247
    connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
248
    connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
249
250
    connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
    connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
251
252
}

253
254
255
256
257
258
259
260
261
262
263
264
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"));
265
266
267
268
#ifndef FIRST_CLASS_MESSAGING
    file->addAction(messageAction);
    file->addSeparator();
#endif
269
270
271
272
273
274
275
276
277
278
    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"));
    help->addAction(aboutAction);
279
    help->addAction(aboutQtAction);
280
281
282
283
284
285
286
287
288
289
290
}

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);
291
292
293
#ifdef FIRST_CLASS_MESSAGING
    toolbar->addAction(messageAction);
#endif
294
295
296
297
298
299

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

300
void BitcoinGUI::setClientModel(ClientModel *clientModel)
301
{
302
    this->clientModel = clientModel;
303
    if(clientModel)
304
    {
305
306
307
308
        if(clientModel->isTestNet())
        {
            QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
            setWindowTitle(title_testnet);
309
#ifndef Q_WS_MAC
310
            setWindowIcon(QIcon(":icons/bitcoin_testnet"));
311
#else
312
            MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
313
#endif
314
315
316
317
318
            if(trayIcon)
            {
                trayIcon->setToolTip(title_testnet);
                trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
            }
319
320
        }

321
322
323
        // Keep up to date with client
        setNumConnections(clientModel->getNumConnections());
        connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
324

325
326
        setNumBlocks(clientModel->getNumBlocks());
        connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
327

328
329
330
        // Report errors from network/worker thread
        connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
    }
331
332
333
334
335
}

void BitcoinGUI::setWalletModel(WalletModel *walletModel)
{
    this->walletModel = walletModel;
336
337
338
339
    if(walletModel)
    {
        // Report errors from wallet thread
        connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
340

341
342
        // Put transaction list in tabs
        transactionView->setModel(walletModel);
343

344
345
346
347
        overviewPage->setModel(walletModel);
        addressBookPage->setModel(walletModel->getAddressTableModel());
        receiveCoinsPage->setModel(walletModel->getAddressTableModel());
        sendCoinsPage->setModel(walletModel);
348
        messagePage->setModel(walletModel);
349

350
351
        setEncryptionStatus(walletModel->getEncryptionStatus());
        connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
352

353
354
355
        // Balloon popup for new transaction
        connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
                this, SLOT(incomingTransaction(QModelIndex,int,int)));
356

357
358
359
        // Ask for passphrase if needed
        connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
    }
360
361
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
362
363
void BitcoinGUI::createTrayIcon()
{
364
365
    QMenu *trayIconMenu;
#ifndef Q_WS_MAC
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
366
    trayIcon = new QSystemTrayIcon(this);
367
    trayIconMenu = new QMenu(this);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
368
    trayIcon->setContextMenu(trayIconMenu);
369
    trayIcon->setToolTip("Bitcoin client");
370
    trayIcon->setIcon(QIcon(":/icons/toolbar"));
371
372
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
373
    trayIcon->show();
374
375
376
377
378
379
380
381
382
383
#else
    // Note: On Mac, the dock icon is used to provide the tray's functionality.
    MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
    connect(dockIconHandler, SIGNAL(dockIconClicked()), openBitcoinAction, SLOT(trigger()));
    trayIconMenu = dockIconHandler->dockMenu();
#endif

    // Configuration of the tray icon (or dock icon) icon menu
    trayIconMenu->addAction(openBitcoinAction);
    trayIconMenu->addSeparator();
384
385
386
387
    trayIconMenu->addAction(messageAction);
#ifndef FIRST_CLASS_MESSAGING
    trayIconMenu->addSeparator();
#endif
388
389
390
391
392
393
394
395
    trayIconMenu->addAction(receiveCoinsAction);
    trayIconMenu->addAction(sendCoinsAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(optionsAction);
#ifndef Q_WS_MAC // This is built-in on Mac
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);
#endif
396
397

    notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
398
399
}

400
#ifndef Q_WS_MAC
401
402
void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
403
    if(reason == QSystemTrayIcon::Trigger)
404
    {
405
        // Click on system tray icon triggers "open bitcoin"
406
        openBitcoinAction->trigger();
407
    }
408

409
}
410
#endif
411

412
413
void BitcoinGUI::optionsClicked()
{
414
415
    if(!clientModel || !clientModel->getOptionsModel())
        return;
416
    OptionsDialog dlg;
417
    dlg.setModel(clientModel->getOptionsModel());
418
    dlg.exec();
419
420
}

421
void BitcoinGUI::aboutClicked()
422
{
423
    AboutDialog dlg;
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
424
    dlg.setModel(clientModel);
425
    dlg.exec();
426
427
}

428
429
void BitcoinGUI::setNumConnections(int count)
{
430
431
432
    QString icon;
    switch(count)
    {
433
434
435
436
437
    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;
438
    }
439
    labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
440
    labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
441
442
443
444
}

void BitcoinGUI::setNumBlocks(int count)
{
445
446
    if(!clientModel)
        return;
447
    int total = clientModel->getNumBlocksOfPeers();
448
449
    QString tooltip;

450
451
    if(count < total)
    {
452
453
454
455
456
        if (clientModel->getStatusBarWarnings() == "")
        {
            progressBarLabel->setVisible(true);
            progressBarLabel->setText(tr("Synchronizing with network..."));
            progressBar->setVisible(true);
457
458
            progressBar->setMaximum(total);
            progressBar->setValue(count);
459
460
461
462
463
464
465
        }
        else
        {
            progressBarLabel->setText(clientModel->getStatusBarWarnings());
            progressBarLabel->setVisible(true);
            progressBar->setVisible(false);
        }
466
        tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
467
468
469
    }
    else
    {
470
471
472
473
474
475
476
        if (clientModel->getStatusBarWarnings() == "")
            progressBarLabel->setVisible(false);
        else
        {
            progressBarLabel->setText(clientModel->getStatusBarWarnings());
            progressBarLabel->setVisible(true);
        }
477
        progressBar->setVisible(false);
478
        tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
479
480
    }

481
482
483
484
485
    QDateTime now = QDateTime::currentDateTime();
    QDateTime lastBlockDate = clientModel->getLastBlockDate();
    int secs = lastBlockDate.secsTo(now);
    QString text;

486
487
    // Represent time from last generated block in human readable text
    if(secs < 60)
488
    {
489
        text = tr("%n second(s) ago","",secs);
490
491
492
493
494
495
496
497
498
499
500
501
502
    }
    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));
    }
503

504
    // Set icon state: spinning if catching up, tick otherwise
505
506
    if(secs < 30*60)
    {
507
        tooltip = tr("Up to date") + QString("\n") + tooltip;
508
        labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
509
    }
510
511
512
    else
    {
        tooltip = tr("Catching up...") + QString("\n") + tooltip;
513
514
        labelBlocksIcon->setMovie(syncIconMovie);
        syncIconMovie->start();
515
    }
516

517
    tooltip += QString("\n");
518
519
520
    tooltip += tr("Last received block was generated %1.").arg(text);

    labelBlocksIcon->setToolTip(tooltip);
521
522
    progressBarLabel->setToolTip(tooltip);
    progressBar->setToolTip(tooltip);
523
524
}

525
526
527
528
529
530
531
532
533
534
535
536
537
void BitcoinGUI::refreshStatusBar()
{
    /* Might display multiple times in the case of multiple alerts
    static QString prevStatusBar;
    QString newStatusBar = clientModel->getStatusBarWarnings();
    if (prevStatusBar != newStatusBar)
    {
        prevStatusBar = newStatusBar;
        error(tr("Network Alert"), newStatusBar);
    }*/
    setNumBlocks(clientModel->getNumBlocks());
}

Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
538
539
void BitcoinGUI::error(const QString &title, const QString &message)
{
540
    // Report errors from network/worker thread
541
    notificator->notify(Notificator::Critical, title, message);
Wladimir J. van der Laan's avatar
Wladimir J. van der Laan committed
542
}
543
544
545

void BitcoinGUI::changeEvent(QEvent *e)
{
546
#ifndef Q_WS_MAC // Ignored on Mac
547
    if(e->type() == QEvent::WindowStateChange)
548
    {
549
        if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
550
        {
551
            if(isMinimized())
552
553
554
            {
                hide();
                e->ignore();
555
556
557
            }
            else
            {
558
                show();
559
560
561
562
                e->accept();
            }
        }
    }
563
#endif
564
565
566
567
568
    QMainWindow::changeEvent(e);
}

void BitcoinGUI::closeEvent(QCloseEvent *event)
{
569
    if(clientModel)
570
    {
571
572
573
574
575
576
#ifndef Q_WS_MAC // Ignored on Mac
        if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
           !clientModel->getOptionsModel()->getMinimizeOnClose())
        {
            qApp->quit();
        }
577
#endif
578
    }
579
580
    QMainWindow::closeEvent(event);
}
Wladimir J. van der Laan's avatar
ask fee    
Wladimir J. van der Laan committed
581
582
583
584
585
586

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.  "
587
588
          "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
589
590
591
592
593
    QMessageBox::StandardButton retval = QMessageBox::question(
          this, tr("Sending..."), strMessage,
          QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
    *payFee = (retval == QMessageBox::Yes);
}
594

595
596
void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
{
597
598
    if(!walletModel || !clientModel)
        return;
599
    TransactionTableModel *ttm = walletModel->getTransactionTableModel();
600
    qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
601
                    .data(Qt::EditRole).toULongLong();
602
    if(!clientModel->inInitialBlockDownload())
603
    {
604
        // On new transaction, make an info balloon
605
        // Unless the initial block download is in progress, to prevent balloon-spam
606
607
        QString date = ttm->index(start, TransactionTableModel::Date, parent)
                        .data().toString();
608
609
610
        QString type = ttm->index(start, TransactionTableModel::Type, parent)
                        .data().toString();
        QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
611
                        .data().toString();
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
        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);
627
628
    }
}
629

630
void BitcoinGUI::gotoOverviewPage()
631
632
633
{
    overviewAction->setChecked(true);
    centralWidget->setCurrentWidget(overviewPage);
634

635
    exportAction->setEnabled(false);
636
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
637
638
}

639
void BitcoinGUI::gotoHistoryPage()
640
641
642
{
    historyAction->setChecked(true);
    centralWidget->setCurrentWidget(transactionsPage);
643

644
    exportAction->setEnabled(true);
645
646
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
647
648
}

649
650
651
652
void BitcoinGUI::gotoAddressBookPage()
{
    addressBookAction->setChecked(true);
    centralWidget->setCurrentWidget(addressBookPage);
653
654
655
656

    exportAction->setEnabled(true);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
657
658
659
660
661
662
}

void BitcoinGUI::gotoReceiveCoinsPage()
{
    receiveCoinsAction->setChecked(true);
    centralWidget->setCurrentWidget(receiveCoinsPage);
663
664
665
666

    exportAction->setEnabled(true);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
    connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
667
668
669
670
671
672
673
}

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

674
675
    exportAction->setEnabled(false);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
676
}
677

678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
void BitcoinGUI::gotoMessagePage()
{
#ifdef FIRST_CLASS_MESSAGING
    messageAction->setChecked(true);
    centralWidget->setCurrentWidget(messagePage);

    exportAction->setEnabled(false);
    disconnect(exportAction, SIGNAL(triggered()), 0, 0);
#else
    messagePage->show();
    messagePage->setFocus();
#endif
}

void BitcoinGUI::gotoMessagePage(QString addr)
{
    gotoMessagePage();
    messagePage->setAddress(addr);
}

698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
{
    // Accept only URLs
    if(event->mimeData()->hasUrls())
        event->acceptProposedAction();
}

void BitcoinGUI::dropEvent(QDropEvent *event)
{
    if(event->mimeData()->hasUrls())
    {
        gotoSendCoinsPage();
        QList<QUrl> urls = event->mimeData()->urls();
        foreach(const QUrl &url, urls)
        {
            sendCoinsPage->handleURL(&url);
        }
    }

    event->acceptProposedAction();
}

720
721
722
723
724
725
726
void BitcoinGUI::handleURL(QString strURL)
{
    gotoSendCoinsPage();
    QUrl url = QUrl(strURL);
    sendCoinsPage->handleURL(&url);
}

727
728
729
730
731
732
void BitcoinGUI::setEncryptionStatus(int status)
{
    switch(status)
    {
    case WalletModel::Unencrypted:
        labelEncryptionIcon->hide();
733
734
735
        encryptWalletAction->setChecked(false);
        changePassphraseAction->setEnabled(false);
        encryptWalletAction->setEnabled(true);
736
737
738
739
740
        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>"));
741
742
743
        encryptWalletAction->setChecked(true);
        changePassphraseAction->setEnabled(true);
        encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
744
745
746
747
748
        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>"));
749
750
751
        encryptWalletAction->setChecked(true);
        changePassphraseAction->setEnabled(true);
        encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
752
753
754
        break;
    }
}
755
756
757

void BitcoinGUI::encryptWallet(bool status)
{
758
759
    if(!walletModel)
        return;
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
    AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
                                     AskPassphraseDialog::Decrypt, this);
    dlg.setModel(walletModel);
    dlg.exec();

    setEncryptionStatus(walletModel->getEncryptionStatus());
}

void BitcoinGUI::changePassphrase()
{
    AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
    dlg.setModel(walletModel);
    dlg.exec();
}

void BitcoinGUI::unlockWallet()
{
777
778
    if(!walletModel)
        return;
779
    // Unlock wallet when requested by wallet model
780
781
782
783
784
785
786
    if(walletModel->getEncryptionStatus() == WalletModel::Locked)
    {
        AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
        dlg.setModel(walletModel);
        dlg.exec();
    }
}