1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "NhekoDBusBackend.h"
#include <mutex>
#include "Cache.h"
#include "Cache_p.h"
#include "ChatPage.h"
#include "Logging.h"
#include "MainWindow.h"
#include "MxcImageProvider.h"
#include "timeline/RoomlistModel.h"
#include <QDBusConnection>
NhekoDBusBackend::NhekoDBusBackend(RoomlistModel *parent)
: QObject{parent}
, m_parent{parent}
{
}
namespace {
struct RoomReplyState
{
QVector<nheko::dbus::RoomInfoItem> model;
std::map<QString, RoomInfo> roominfos;
std::mutex m;
};
}
QVector<nheko::dbus::RoomInfoItem>
NhekoDBusBackend::rooms(const QDBusMessage &message)
{
message.setDelayedReply(true);
nhlog::ui()->debug("Rooms requested over D-Bus.");
const auto roomListModel = m_parent->models;
auto state = QSharedPointer<RoomReplyState>::create();
std::vector<std::string> roomids;
roomids.reserve(roomids.size());
for (const auto &room : roomListModel) {
roomids.push_back(room->roomId().toStdString());
}
state->roominfos = cache::getRoomInfo(roomids);
for (const auto &room : roomListModel) {
auto addRoom = [room, roomListModelSize = roomListModel.size(), message, state](
const QImage &image) {
const auto aliases = cache::client()->getStateEvent<mtx::events::state::CanonicalAlias>(
room->roomId().toStdString());
QString alias;
if (aliases.has_value()) {
const auto &val = aliases.value().content;
if (!val.alias.empty())
alias = QString::fromStdString(val.alias);
else if (val.alt_aliases.size() > 0)
alias = QString::fromStdString(val.alt_aliases.front());
}
std::lock_guard<std::mutex> childLock(state->m);
state->model.push_back(nheko::dbus::RoomInfoItem{
room->roomId(),
alias,
QString::fromStdString(state->roominfos[room->roomId()].name),
image,
room->notificationCount()});
if (state->model.size() == roomListModelSize) {
nhlog::ui()->debug("Sending {} rooms over D-Bus...", state->model.size());
auto reply = message.createReply();
reply << QVariant::fromValue(state->model);
QDBusConnection::sessionBus().send(reply);
nhlog::ui()->debug("Rooms successfully sent to D-Bus.");
} else {
// nhlog::ui()->debug("DBUS: {}/{}", state->model.size(), roomListModelSize);
}
};
if (state->roominfos[room->roomId()].avatar_url.empty())
addRoom(QImage());
else
MainWindow::instance()->imageProvider()->download(
QString::fromStdString(state->roominfos[room->roomId()].avatar_url).remove("mxc://"),
{96, 96},
[addRoom](const QString &, const QSize &, const QImage &image, const QString &) {
addRoom(image);
},
true);
}
return {};
}
void
NhekoDBusBackend::activateRoom(const QString &alias) const
{
bringWindowToTop();
m_parent->setCurrentRoom(alias);
}
void
NhekoDBusBackend::joinRoom(const QString &alias) const
{
bringWindowToTop();
ChatPage::instance()->joinRoom(alias);
}
void
NhekoDBusBackend::directChat(const QString &userId) const
{
bringWindowToTop();
ChatPage::instance()->startChat(userId);
}
void
NhekoDBusBackend::bringWindowToTop() const
{
MainWindow::instance()->show();
MainWindow::instance()->raise();
}
|