diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/connection.cpp | 6 | ||||
-rw-r--r-- | lib/connection.h | 20 |
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. * |