aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/connection.cpp30
-rw-r--r--lib/connection.h78
2 files changed, 79 insertions, 29 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp
index cf3446ff..a1fbd903 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -730,7 +730,7 @@ User* Connection::user(const QString& userId)
}
if( d->userMap.contains(userId) )
return d->userMap.value(userId);
- auto* user = userFactory(this, userId);
+ auto* user = userFactory()(this, userId);
d->userMap.insert(userId, user);
emit newUser(user);
return user;
@@ -987,7 +987,7 @@ Room* Connection::provideRoom(const QString& id, JoinState joinState)
}
else
{
- room = roomFactory(this, id, joinState);
+ room = roomFactory()(this, id, joinState);
if (!room)
{
qCCritical(MAIN) << "Failed to create a room" << id;
@@ -1025,12 +1025,28 @@ Room* Connection::provideRoom(const QString& id, JoinState joinState)
return room;
}
-Connection::room_factory_t Connection::roomFactory =
- [](Connection* c, const QString& id, JoinState joinState)
- { return new Room(c, id, joinState); };
+void Connection::setRoomFactory(room_factory_t f)
+{
+ _roomFactory = std::move(f);
+}
+
+void Connection::setUserFactory(user_factory_t f)
+{
+ _userFactory = std::move(f);
+}
+
+room_factory_t Connection::roomFactory()
+{
+ return _roomFactory;
+}
+
+user_factory_t Connection::userFactory()
+{
+ return _userFactory;
+}
-Connection::user_factory_t Connection::userFactory =
- [](Connection* c, const QString& id) { return new User(id, c); };
+room_factory_t Connection::_roomFactory = defaultRoomFactory<>();
+user_factory_t Connection::_userFactory = defaultUserFactory<>();
QByteArray Connection::generateTxnId() const
{
diff --git a/lib/connection.h b/lib/connection.h
index ea7c62c0..3eefb625 100644
--- a/lib/connection.h
+++ b/lib/connection.h
@@ -69,6 +69,40 @@ namespace QMatrixClient
return connection;
}
+ class Connection;
+
+ using room_factory_t = std::function<Room*(Connection*, const QString&,
+ JoinState)>;
+ using user_factory_t = std::function<User*(Connection*, const QString&)>;
+
+ /** The default factory to create room objects
+ *
+ * Just a wrapper around operator new.
+ * \sa Connection::setRoomFactory, Connection::setRoomType
+ */
+ template <typename T = Room>
+ static inline room_factory_t defaultRoomFactory()
+ {
+ return [](Connection* c, const QString& id, JoinState js)
+ {
+ return new T(c, id, js);
+ };
+ }
+
+ /** The default factory to create user objects
+ *
+ * Just a wrapper around operator new.
+ * \sa Connection::setUserFactory, Connection::setUserType
+ */
+ template <typename T = User>
+ static inline user_factory_t defaultUserFactory()
+ {
+ return [](Connection* c, const QString& id)
+ {
+ return new T(id, c);
+ };
+ }
+
/** Enumeration with flags defining the network job running policy
* So far only background/foreground flags are available.
*
@@ -89,11 +123,6 @@ namespace QMatrixClient
Q_PROPERTY(QUrl homeserver READ homeserver WRITE setHomeserver NOTIFY homeserverChanged)
Q_PROPERTY(bool cacheState READ cacheState WRITE setCacheState NOTIFY cacheStateChanged)
public:
- using room_factory_t =
- std::function<Room*(Connection*, const QString&, JoinState joinState)>;
- using user_factory_t =
- std::function<User*(Connection*, const QString&)>;
-
// Room ids, rather than room pointers, are used in the direct chat
// map types because the library keeps Invite rooms separate from
// rooms in Join and Leave state; and direct chats in account data
@@ -308,25 +337,30 @@ namespace QMatrixClient
std::forward<JobArgTs>(jobArgs)...);
}
- /** Generates a new transaction id. Transaction id's are unique within
+ /** Generate a new transaction id. Transaction id's are unique within
* a single Connection object
*/
Q_INVOKABLE QByteArray generateTxnId() const;
- template <typename T = Room>
- static void setRoomType()
- {
- roomFactory =
- [](Connection* c, const QString& id, JoinState joinState)
- { return new T(c, id, joinState); };
- }
+ /// Set a room factory function
+ static void setRoomFactory(room_factory_t f);
- template <typename T = User>
- static void setUserType()
- {
- userFactory =
- [](Connection* c, const QString& id) { return new T(id, c); };
- }
+ /// Set a user factory function
+ static void setUserFactory(user_factory_t f);
+
+ /// Get a room factory function
+ static room_factory_t roomFactory();
+
+ /// Get a user factory function
+ static user_factory_t userFactory();
+
+ /// Set the room factory to default with the overriden room type
+ template <typename T>
+ static void setRoomType() { setRoomFactory(defaultRoomFactory<T>()); }
+
+ /// Set the user factory to default with the overriden user type
+ template <typename T>
+ static void setUserType() { setUserFactory(defaultUserFactory<T>()); }
public slots:
/** Set the homeserver base URL */
@@ -630,7 +664,7 @@ namespace QMatrixClient
* the server; in particular, does not automatically create rooms
* on the server.
* @return a pointer to a Room object with the specified id; nullptr
- * if roomId is empty if roomFactory() failed to create a Room object.
+ * if roomId is empty or roomFactory() failed to create a Room object.
*/
Room* provideRoom(const QString& roomId, JoinState joinState);
@@ -660,8 +694,8 @@ namespace QMatrixClient
const QString& initialDeviceName,
const QString& deviceId = {});
- static room_factory_t roomFactory;
- static user_factory_t userFactory;
+ static room_factory_t _roomFactory;
+ static user_factory_t _userFactory;
};
} // namespace QMatrixClient
Q_DECLARE_METATYPE(QMatrixClient::Connection*)