Debian Codegraph @

src/timeline/PresenceEmitter.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 #include <QObject>
8 #include <QQmlEngine>
9
10 #include <vector>
11
12 #include <mtx/events.hpp>
13 #include <mtx/events/presence.hpp>
14
15 class PresenceEmitter final : public QObject
16 {
17 Q_OBJECT
18
19 QML_NAMED_ELEMENT(Presence)
20 QML_SINGLETON
21
22 public:
23 PresenceEmitter(QObject *p)
24 : QObject(p)
25 {
26 instance_ = this;
27 }
28
29 static PresenceEmitter *create(QQmlEngine *qmlEngine, QJSEngine *)
30 {
31 // The instance has to exist before it is used. We cannot replace it.
32 Q_ASSERT(instance_);
33
34 // The engine has to have the same thread affinity as the singleton.
35 Q_ASSERT(qmlEngine->thread() == instance_->thread());
36
37 // There can only be one engine accessing the singleton.
38 static QJSEngine *s_engine = nullptr;
39 if (s_engine)
40 Q_ASSERT(qmlEngine == s_engine);
41 else
42 s_engine = qmlEngine;
43
44 QJSEngine::setObjectOwnership(instance_, QJSEngine::CppOwnership);
45 return instance_;
46 }
47
48 void sync(const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presences);
49
50 Q_INVOKABLE QString userPresence(QString id) const;
51 Q_INVOKABLE QString userStatus(QString id) const;
52
53 signals:
54 void presenceChanged(QString userid);
55
56 private:
57 inline static PresenceEmitter *instance_ = nullptr;
58 };