summary refs log tree commit diff
path: root/src/timeline/PresenceEmitter.h
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2023-06-19 01:38:40 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2023-06-19 01:38:40 +0200
commitce1a64bc19ffc21e115bdf2587bb053d7a417f3e (patch)
treea195d127228218772a535448de642f0bb7b6d053 /src/timeline/PresenceEmitter.h
parentRemove explicit link styling (diff)
downloadnheko-ce1a64bc19ffc21e115bdf2587bb053d7a417f3e.tar.xz
Move to automatic type registration
Diffstat (limited to 'src/timeline/PresenceEmitter.h')
-rw-r--r--src/timeline/PresenceEmitter.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/timeline/PresenceEmitter.h b/src/timeline/PresenceEmitter.h
index e89fb316..09ad1301 100644
--- a/src/timeline/PresenceEmitter.h
+++ b/src/timeline/PresenceEmitter.h
@@ -5,6 +5,7 @@
 #pragma once
 
 #include <QObject>
+#include <QQmlEngine>
 
 #include <vector>
 
@@ -15,10 +16,33 @@ class PresenceEmitter final : public QObject
 {
     Q_OBJECT
 
+    QML_NAMED_ELEMENT(Presence)
+    QML_SINGLETON
+
 public:
     PresenceEmitter(QObject *p = nullptr)
       : QObject(p)
     {
+        instance_ = this;
+    }
+
+    static PresenceEmitter *create(QQmlEngine *qmlEngine, QJSEngine *)
+    {
+        // The instance has to exist before it is used. We cannot replace it.
+        Q_ASSERT(instance_);
+
+        // The engine has to have the same thread affinity as the singleton.
+        Q_ASSERT(qmlEngine->thread() == instance_->thread());
+
+        // There can only be one engine accessing the singleton.
+        static QJSEngine *s_engine = nullptr;
+        if (s_engine)
+            Q_ASSERT(qmlEngine == s_engine);
+        else
+            s_engine = qmlEngine;
+
+        QJSEngine::setObjectOwnership(instance_, QJSEngine::CppOwnership);
+        return instance_;
     }
 
     void sync(const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presences);
@@ -28,4 +52,7 @@ public:
 
 signals:
     void presenceChanged(QString userid);
+
+private:
+    inline static PresenceEmitter *instance_ = nullptr;
 };