aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-07-27 14:32:52 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-07-27 14:33:17 +0900
commit284b751ee424985341812a32721227112160a905 (patch)
treedd4617783038bc9063fd0caba840a936e84ad153 /lib
parentd532b3f532c2daf45f6a03a7b48e923b6b7651ee (diff)
downloadlibquotient-284b751ee424985341812a32721227112160a905.tar.gz
libquotient-284b751ee424985341812a32721227112160a905.zip
connectSingleShot()
Time and again I need one-off slots that disconnect once they are done. The code has been inside Connection implementation for quite some time, now it's put to the interface for usage in other places (notably qmc-example).
Diffstat (limited to 'lib')
-rw-r--r--lib/connection.cpp6
-rw-r--r--lib/connection.h20
2 files changed, 21 insertions, 5 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp
index 2881f7f4..8007cea1 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -264,13 +264,9 @@ void Connection::checkAndConnect(const QString& userId,
// Not good to go, try to fix the homeserver URL.
if (userId.startsWith('@') && userId.indexOf(':') != -1)
{
- // The below construct makes a single-shot connection that triggers
- // on the signal and then self-disconnects.
+ connectSingleShot(this, &Connection::homeserverChanged, this, connectFn);
// NB: doResolveServer can emit resolveError, so this is a part of
// checkAndConnect function contract.
- QMetaObject::Connection connection;
- connection = connect(this, &Connection::homeserverChanged,
- this, [=] { connectFn(); disconnect(connection); });
resolveServer(userId);
} else
emit resolveError(
diff --git a/lib/connection.h b/lib/connection.h
index c7e28282..7adab883 100644
--- a/lib/connection.h
+++ b/lib/connection.h
@@ -48,6 +48,26 @@ namespace QMatrixClient
class DownloadFileJob;
class SendToDeviceJob;
+ /** Create a single-shot connection that triggers on the signal and
+ * then self-disconnects
+ *
+ * Only supports DirectConnection type.
+ */
+ template <typename SenderT1, typename SignalT,
+ typename ReceiverT2, typename SlotT>
+ inline auto connectSingleShot(SenderT1* sender, SignalT signal,
+ ReceiverT2* receiver, SlotT slot)
+ {
+ QMetaObject::Connection connection;
+ connection = QObject::connect(sender, signal, receiver, slot,
+ Qt::DirectConnection);
+ Q_ASSERT(connection);
+ QObject::connect(sender, signal, receiver,
+ [connection] { QObject::disconnect(connection); },
+ Qt::DirectConnection);
+ return connection;
+ }
+
/** Enumeration with flags defining the network job running policy
* So far only background/foreground flags are available.
*