2022-04-14 18:02:55 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2023-01-02 06:25:33 +03:00
|
|
|
// SPDX-FileCopyrightText: 2023 Nheko Contributors
|
2022-04-14 18:02:55 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "NhekoDBusBackend.h"
|
|
|
|
|
2022-07-11 16:14:30 +03:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include "Cache.h"
|
2022-04-14 18:02:55 +03:00
|
|
|
#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}
|
2022-09-25 21:05:08 +03:00
|
|
|
{
|
|
|
|
}
|
2022-04-14 18:02:55 +03:00
|
|
|
|
2022-07-11 16:14:30 +03:00
|
|
|
namespace {
|
|
|
|
struct RoomReplyState
|
|
|
|
{
|
|
|
|
QVector<nheko::dbus::RoomInfoItem> model;
|
|
|
|
std::map<QString, RoomInfo> roominfos;
|
2022-07-11 17:52:05 +03:00
|
|
|
std::mutex m;
|
2022-07-11 16:14:30 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-14 18:02:55 +03:00
|
|
|
QVector<nheko::dbus::RoomInfoItem>
|
2022-10-09 01:56:02 +03:00
|
|
|
NhekoDBusBackend::rooms() const
|
2022-04-14 18:02:55 +03:00
|
|
|
{
|
2022-07-11 16:14:30 +03:00
|
|
|
nhlog::ui()->debug("Rooms requested over D-Bus.");
|
2022-07-10 16:25:41 +03:00
|
|
|
|
2022-04-14 18:02:55 +03:00
|
|
|
const auto roomListModel = m_parent->models;
|
2022-10-09 01:56:02 +03:00
|
|
|
QVector<nheko::dbus::RoomInfoItem> model;
|
2022-04-14 18:02:55 +03:00
|
|
|
|
2022-07-11 16:14:30 +03:00
|
|
|
for (const auto &room : roomListModel) {
|
2022-10-09 01:56:02 +03:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
model.push_back(nheko::dbus::RoomInfoItem{room->roomId(),
|
|
|
|
alias,
|
|
|
|
room->plainRoomName(),
|
|
|
|
room->roomAvatarUrl(),
|
|
|
|
room->notificationCount()});
|
2022-07-11 16:14:30 +03:00
|
|
|
}
|
|
|
|
|
2022-10-09 01:56:02 +03:00
|
|
|
nhlog::ui()->debug("Sending {} rooms over D-Bus...", model.size());
|
|
|
|
return model;
|
|
|
|
}
|
2022-04-14 18:02:55 +03:00
|
|
|
|
2022-10-09 01:56:02 +03:00
|
|
|
QImage
|
|
|
|
NhekoDBusBackend::image(const QString &uri, const QDBusMessage &message) const
|
|
|
|
{
|
|
|
|
message.setDelayedReply(true);
|
|
|
|
nhlog::ui()->debug("Rooms requested over D-Bus.");
|
|
|
|
MainWindow::instance()->imageProvider()->download(
|
|
|
|
QString(uri).remove("mxc://"),
|
|
|
|
{96, 96},
|
|
|
|
[message](const QString &, const QSize &, const QImage &image, const QString &) {
|
|
|
|
auto reply = message.createReply();
|
|
|
|
reply << QVariant::fromValue(image);
|
|
|
|
QDBusConnection::sessionBus().send(reply);
|
|
|
|
},
|
|
|
|
true);
|
2022-04-14 18:02:55 +03:00
|
|
|
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
|
2022-04-18 19:50:15 +03:00
|
|
|
NhekoDBusBackend::directChat(const QString &userId) const
|
2022-04-14 18:02:55 +03:00
|
|
|
{
|
|
|
|
bringWindowToTop();
|
|
|
|
ChatPage::instance()->startChat(userId);
|
|
|
|
}
|
|
|
|
|
2022-11-05 20:13:18 +03:00
|
|
|
void
|
|
|
|
NhekoDBusBackend::setStatusMessage(const QString &message)
|
|
|
|
{
|
|
|
|
ChatPage::instance()->setStatus(message);
|
|
|
|
}
|
|
|
|
|
2022-04-14 18:02:55 +03:00
|
|
|
void
|
|
|
|
NhekoDBusBackend::bringWindowToTop() const
|
|
|
|
{
|
|
|
|
MainWindow::instance()->show();
|
|
|
|
MainWindow::instance()->raise();
|
|
|
|
}
|