Make sure QDBusConnection::connect() returns false if already connected

QDBusConnection::connect() behaves like QObject::connect with a
connection type of Qt::UniqueConnection | Qt::QueuedConnection. So
return false if it's already connected.

[ChangeLog][QtDBus][QDBusConnection] Fixed a bug that would cause
QDBusConnection::connect() to return true if a slot was already
connected to the same D-Bus signal. QtDBus does not support multiple
connections.

Change-Id: I87e17314d8b24ae983b1fffd1453aef5a7c9ad0b
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Thiago Macieira 2016-05-31 11:57:53 -03:00
parent e24f89f266
commit 6f275a4beb
3 changed files with 26 additions and 10 deletions

View File

@ -280,7 +280,7 @@ public slots:
void socketWrite(int);
void objectDestroyed(QObject *o);
void relaySignal(QObject *obj, const QMetaObject *, int signalId, const QVariantList &args);
void addSignalHook(const QString &key, const SignalHook &hook);
bool addSignalHook(const QString &key, const SignalHook &hook);
bool removeSignalHook(const QString &key, const SignalHook &hook);
private slots:
@ -293,7 +293,7 @@ signals:
void dispatchStatusChanged();
void spyHooksFinished(const QDBusMessage &msg);
void messageNeedsSending(QDBusPendingCallPrivate *pcall, void *msg, int timeout = -1);
void signalNeedsConnecting(const QString &key, const QDBusConnectionPrivate::SignalHook &hook);
bool signalNeedsConnecting(const QString &key, const QDBusConnectionPrivate::SignalHook &hook);
bool signalNeedsDisconnecting(const QString &key, const QDBusConnectionPrivate::SignalHook &hook);
void serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner);
void callWithCallbackFailed(const QDBusError &error, const QDBusMessage &message);

View File

@ -2187,20 +2187,16 @@ bool QDBusConnectionPrivate::connectSignal(const QString &service,
// check the slot
QDBusConnectionPrivate::SignalHook hook;
QString key;
QString name2 = name;
if (name2.isNull())
name2.detach();
hook.signature = signature;
if (!prepareHook(hook, key, service, path, interface, name, argumentMatch, receiver, slot, 0, false))
return false; // don't connect
Q_ASSERT(thread() != QThread::currentThread());
emit signalNeedsConnecting(key, hook);
return true;
return emit signalNeedsConnecting(key, hook);
}
void QDBusConnectionPrivate::addSignalHook(const QString &key, const SignalHook &hook)
bool QDBusConnectionPrivate::addSignalHook(const QString &key, const SignalHook &hook)
{
QDBusWriteLocker locker(ConnectAction, this);
@ -2216,7 +2212,7 @@ void QDBusConnectionPrivate::addSignalHook(const QString &key, const SignalHook
entry.midx == hook.midx &&
entry.argumentMatch == hook.argumentMatch) {
// no need to compare the parameters if it's the same slot
return; // already there
return false; // already there
}
}
@ -2228,7 +2224,7 @@ void QDBusConnectionPrivate::addSignalHook(const QString &key, const SignalHook
if (mit != matchRefCounts.end()) { // Match already present
mit.value() = mit.value() + 1;
return;
return true;
}
matchRefCounts.insert(hook.matchRule, 1);
@ -2255,6 +2251,7 @@ void QDBusConnectionPrivate::addSignalHook(const QString &key, const SignalHook
}
}
}
return true;
}
bool QDBusConnectionPrivate::disconnectSignal(const QString &service,

View File

@ -1087,6 +1087,16 @@ void tst_QDBusConnection::connectSignal()
QTest::qWait(100);
QCOMPARE(recv.argumentReceived, signal.arguments().at(0).toString());
QCOMPARE(recv.signalsReceived, 1);
// confirm that we are, indeed, a unique connection
recv.argumentReceived.clear();
recv.signalsReceived = 0;
QVERIFY(!con.connect(con.baseService(), signal.path(), signal.interface(),
signal.member(), "s", &recv, SLOT(oneSlot(QString))));
QVERIFY(con.send(signal));
QTest::qWait(100);
QCOMPARE(recv.argumentReceived, signal.arguments().at(0).toString());
QCOMPARE(recv.signalsReceived, 1);
}
void tst_QDBusConnection::slotsWithLessParameters()
@ -1118,6 +1128,15 @@ void tst_QDBusConnection::slotsWithLessParameters()
QTest::qWait(100);
QCOMPARE(recv.argumentReceived, QString());
QCOMPARE(recv.signalsReceived, 1);
// confirm that we are, indeed, a unique connection
recv.signalsReceived = 0;
QVERIFY(!con.connect(con.baseService(), signal.path(), signal.interface(),
signal.member(), "s", &recv, SLOT(oneSlot())));
QVERIFY(con.send(signal));
QTest::qWait(100);
QCOMPARE(recv.argumentReceived, QString());
QCOMPARE(recv.signalsReceived, 1);
}
void SignalReceiver::secondCallWithCallback()