2022-04-14 18:02:55 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "NhekoDBusBackend.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}
|
2022-05-10 04:19:53 +03:00
|
|
|
{
|
|
|
|
}
|
2022-04-14 18:02:55 +03:00
|
|
|
|
|
|
|
QVector<nheko::dbus::RoomInfoItem>
|
2022-04-18 19:50:15 +03:00
|
|
|
NhekoDBusBackend::rooms(const QDBusMessage &message)
|
2022-04-14 18:02:55 +03:00
|
|
|
{
|
|
|
|
const auto roomListModel = m_parent->models;
|
|
|
|
QSharedPointer<QVector<nheko::dbus::RoomInfoItem>> model{
|
|
|
|
new QVector<nheko::dbus::RoomInfoItem>};
|
|
|
|
|
|
|
|
for (const auto &room : roomListModel) {
|
2022-04-14 18:33:28 +03:00
|
|
|
auto addRoom =
|
|
|
|
[room, roomListModelSize = roomListModel.size(), message, model](const QImage &image) {
|
2022-04-14 18:02:55 +03:00
|
|
|
const auto aliases = cache::client()->getRoomAliases(room->roomId().toStdString());
|
|
|
|
QString alias;
|
|
|
|
if (aliases.has_value()) {
|
|
|
|
const auto &val = aliases.value();
|
|
|
|
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{
|
2022-04-18 19:50:15 +03:00
|
|
|
room->roomId(), alias, room->roomName(), image, room->notificationCount()});
|
2022-04-14 18:02:55 +03:00
|
|
|
|
2022-04-14 18:33:28 +03:00
|
|
|
if (model->length() == roomListModelSize) {
|
2022-04-14 18:02:55 +03:00
|
|
|
auto reply = message.createReply();
|
|
|
|
nhlog::ui()->debug("Sending {} rooms over D-Bus...", model->size());
|
|
|
|
reply << QVariant::fromValue(*model);
|
|
|
|
QDBusConnection::sessionBus().send(reply);
|
|
|
|
nhlog::ui()->debug("Rooms successfully sent to D-Bus.");
|
|
|
|
}
|
2022-04-14 18:33:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto avatarUrl = room->roomAvatarUrl();
|
|
|
|
if (avatarUrl.isEmpty())
|
|
|
|
addRoom(QImage());
|
|
|
|
else
|
|
|
|
MainWindow::instance()->imageProvider()->download(
|
|
|
|
avatarUrl.remove("mxc://"),
|
|
|
|
{96, 96},
|
|
|
|
[addRoom](const QString &, const QSize &, const QImage &image, const QString &) {
|
|
|
|
addRoom(image);
|
|
|
|
},
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NhekoDBusBackend::bringWindowToTop() const
|
|
|
|
{
|
|
|
|
MainWindow::instance()->show();
|
|
|
|
MainWindow::instance()->raise();
|
|
|
|
}
|