Skip to content

Commit 9ac11a4

Browse files
committed
Merge pull request #3254
4cf3411 [Qt] misc PaymentServer changes (e.g. changes to eventFilter()) (Philip Kaufmann)
2 parents b4297c8 + 4cf3411 commit 9ac11a4

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

src/qt/paymentserver.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,10 @@ bool PaymentServer::ipcSendCommandLine(int argc, char* argv[])
232232
QLocalSocket* socket = new QLocalSocket();
233233
socket->connectToServer(ipcServerName(), QIODevice::WriteOnly);
234234
if (!socket->waitForConnected(BITCOIN_IPC_CONNECT_TIMEOUT))
235+
{
236+
delete socket;
235237
return false;
238+
}
236239

237240
QByteArray block;
238241
QDataStream out(&block, QIODevice::WriteOnly);
@@ -277,8 +280,11 @@ PaymentServer::PaymentServer(QObject* parent, bool startLocalServer) :
277280
{
278281
uriServer = new QLocalServer(this);
279282

280-
if (!uriServer->listen(name))
281-
qDebug() << "PaymentServer::PaymentServer : Cannot start bitcoin: click-to-pay handler";
283+
if (!uriServer->listen(name)) {
284+
// constructor is called early in init, so don't use "emit message()" here
285+
QMessageBox::critical(0, tr("Payment request error"),
286+
tr("Cannot start bitcoin: click-to-pay handler"));
287+
}
282288
else {
283289
connect(uriServer, SIGNAL(newConnection()), this, SLOT(handleURIConnection()));
284290
connect(this, SIGNAL(receivedPaymentACK(QString)), this, SLOT(handlePaymentACK(QString)));
@@ -295,20 +301,21 @@ PaymentServer::~PaymentServer()
295301
// OSX-specific way of handling bitcoin: URIs and
296302
// PaymentRequest mime types
297303
//
298-
bool PaymentServer::eventFilter(QObject *, QEvent *event)
304+
bool PaymentServer::eventFilter(QObject *object, QEvent *event)
299305
{
300-
// clicking on bitcoin: URIs creates FileOpen events on the Mac:
306+
// clicking on bitcoin: URIs creates FileOpen events on the Mac
301307
if (event->type() == QEvent::FileOpen)
302308
{
303-
QFileOpenEvent* fileEvent = static_cast<QFileOpenEvent*>(event);
309+
QFileOpenEvent *fileEvent = static_cast<QFileOpenEvent*>(event);
304310
if (!fileEvent->file().isEmpty())
305311
handleURIOrFile(fileEvent->file());
306312
else if (!fileEvent->url().isEmpty())
307313
handleURIOrFile(fileEvent->url().toString());
308314

309315
return true;
310316
}
311-
return false;
317+
318+
return QObject::eventFilter(object, event);
312319
}
313320

314321
void PaymentServer::initNetManager()
@@ -359,7 +366,7 @@ void PaymentServer::handleURIOrFile(const QString& s)
359366
return;
360367
}
361368

362-
if (s.startsWith(BITCOIN_IPC_PREFIX, Qt::CaseInsensitive)) // bitcoin:
369+
if (s.startsWith(BITCOIN_IPC_PREFIX, Qt::CaseInsensitive)) // bitcoin: URI
363370
{
364371
#if QT_VERSION < 0x050000
365372
QUrl uri(s);

src/qt/paymentserver.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ class PaymentServer : public QObject
7777
// Return certificate store
7878
static X509_STORE* getCertStore() { return certStore; }
7979

80-
// Constructor registers this on the parent QApplication to
81-
// receive QEvent::FileOpen events
82-
bool eventFilter(QObject *object, QEvent *event);
83-
8480
// OptionsModel is used for getting proxy settings and display unit
8581
void setOptionsModel(OptionsModel *optionsModel);
8682

@@ -111,6 +107,11 @@ private slots:
111107
void reportSslErrors(QNetworkReply*, const QList<QSslError> &);
112108
void handlePaymentACK(const QString& paymentACKMsg);
113109

110+
protected:
111+
// Constructor registers this on the parent QApplication to
112+
// receive QEvent::FileOpen and QEvent:Drop events
113+
bool eventFilter(QObject *object, QEvent *event);
114+
114115
private:
115116
static bool readPaymentRequest(const QString& filename, PaymentRequestPlus& request);
116117
bool processPaymentRequest(PaymentRequestPlus& request, SendCoinsRecipient& recipient);

src/qt/test/paymentservertests.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77

88
#include <openssl/x509.h>
99
#include <openssl/x509_vfy.h>
10-
#include <QCoreApplication>
11-
#include <QDebug>
10+
1211
#include <QFileOpenEvent>
1312
#include <QTemporaryFile>
14-
#include <QVariant>
15-
1613

1714
X509 *parse_b64der_cert(const char* cert_data)
1815
{
@@ -41,9 +38,14 @@ static SendCoinsRecipient handleRequest(PaymentServer* server, std::vector<unsig
4138
f.write((const char*)&data[0], data.size());
4239
f.close();
4340

44-
// Create a FileOpenEvent and send it directly to the server's event filter:
41+
// Create a QObject, install event filter from PaymentServer
42+
// and send a file open event to the object
43+
QObject object;
44+
object.installEventFilter(server);
4545
QFileOpenEvent event(f.fileName());
46-
server->eventFilter(NULL, &event);
46+
// If sending the event fails, this will cause sigCatcher to be empty,
47+
// which will lead to a test failure anyway.
48+
QCoreApplication::sendEvent(&object, &event);
4749

4850
QObject::disconnect(server, SIGNAL(receivedPaymentRequest(SendCoinsRecipient)),
4951
&sigCatcher, SLOT(getRecipient(SendCoinsRecipient)));

src/qt/test/paymentservertests.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ private slots:
2020
class RecipientCatcher : public QObject
2121
{
2222
Q_OBJECT
23+
2324
public slots:
2425
void getRecipient(SendCoinsRecipient r);
26+
2527
public:
2628
SendCoinsRecipient recipient;
2729
};

src/qt/test/test_main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
2-
31
#include "paymentservertests.h"
42
#include "uritests.h"
53

4+
#include <QCoreApplication>
65
#include <QObject>
76
#include <QTest>
87

@@ -11,6 +10,11 @@ int main(int argc, char *argv[])
1110
{
1211
bool fInvalid = false;
1312

13+
// Don't remove this, it's needed to access
14+
// QCoreApplication:: in the tests
15+
QCoreApplication app(argc, argv);
16+
app.setApplicationName("Bitcoin-Qt-test");
17+
1418
URITests test1;
1519
if (QTest::qExec(&test1) != 0)
1620
fInvalid = true;

0 commit comments

Comments
 (0)