| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "NhekoDBusApi.h" |
| 6 | |
| 7 | #include <QDBusInterface> |
| 8 | #include <QDBusMetaType> |
| 9 | #include <QDBusReply> |
| 10 | |
| 11 | namespace nheko::dbus { |
| 12 | void |
| 13 | init() |
| 14 | { |
| 15 | qDBusRegisterMetaType<RoomInfoItem>(); |
| 16 | qDBusRegisterMetaType<QVector<RoomInfoItem>>(); |
| 17 | qDBusRegisterMetaType<QImage>(); |
| 18 | } |
| 19 | |
| 20 | bool |
| 21 | apiVersionIsCompatible(const QVersionNumber &clientAppVersion) |
| 22 | { |
| 23 | if (clientAppVersion.majorVersion() != nheko::dbus::dbusApiVersion.majorVersion()) |
| 24 | return false; |
| 25 | if (clientAppVersion.minorVersion() > nheko::dbus::dbusApiVersion.minorVersion()) |
| 26 | return false; |
| 27 | if (clientAppVersion.minorVersion() == nheko::dbus::dbusApiVersion.minorVersion() && |
| 28 | clientAppVersion.microVersion() < nheko::dbus::dbusApiVersion.microVersion()) |
| 29 | return false; |
| 30 | |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | RoomInfoItem::RoomInfoItem(const QString &roomId, |
| 35 | const QString &alias, |
| 36 | const QString &title, |
| 37 | const QString &avatarUrl, |
| 38 | const int unreadNotifications, |
| 39 | QObject *parent) |
| 40 | : QObject{parent} |
| 41 | , roomId_{roomId} |
| 42 | , alias_{alias} |
| 43 | , roomName_{title} |
| 44 | , avatarUrl_{avatarUrl} |
| 45 | , unreadNotifications_{unreadNotifications} |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | RoomInfoItem::RoomInfoItem(const RoomInfoItem &other) |
| 50 | : QObject{other.parent()} |
| 51 | , roomId_{other.roomId_} |
| 52 | , alias_{other.alias_} |
| 53 | , roomName_{other.roomName_} |
| 54 | , avatarUrl_{other.avatarUrl_} |
| 55 | , unreadNotifications_{other.unreadNotifications_} |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | RoomInfoItem & |
| 60 | RoomInfoItem::operator=(const RoomInfoItem &other) |
| 61 | { |
| 62 | roomId_ = other.roomId_; |
| 63 | alias_ = other.alias_; |
| 64 | roomName_ = other.roomName_; |
| 65 | avatarUrl_ = other.avatarUrl_; |
| 66 | unreadNotifications_ = other.unreadNotifications_; |
| 67 | return *this; |
| 68 | } |
| 69 | |
| 70 | QDBusArgument & |
| 71 | operator<<(QDBusArgument &arg, const RoomInfoItem &item) |
| 72 | { |
| 73 | arg.beginStructure(); |
| 74 | arg << item.roomId_ << item.alias_ << item.roomName_ << item.avatarUrl_ |
| 75 | << item.unreadNotifications_; |
| 76 | arg.endStructure(); |
| 77 | return arg; |
| 78 | } |
| 79 | |
| 80 | const QDBusArgument & |
| 81 | operator>>(const QDBusArgument &arg, RoomInfoItem &item) |
| 82 | { |
| 83 | arg.beginStructure(); |
| 84 | arg >> item.roomId_ >> item.alias_ >> item.roomName_ >> item.avatarUrl_ >> |
| 85 | item.unreadNotifications_; |
| 86 | |
| 87 | arg.endStructure(); |
| 88 | return arg; |
| 89 | } |
| 90 | |
| 91 | QString |
| 92 | apiVersion() |
| 93 | { |
| 94 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 95 | interface.isValid()) |
| 96 | return QDBusReply<QString>{interface.call(QStringLiteral("apiVersion"))}.value(); |
| 97 | else |
| 98 | return {}; |
| 99 | } |
| 100 | |
| 101 | QString |
| 102 | nhekoVersion() |
| 103 | { |
| 104 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 105 | interface.isValid()) |
| 106 | return QDBusReply<QString>{interface.call(QStringLiteral("nhekoVersion"))}.value(); |
| 107 | else |
| 108 | return {}; |
| 109 | } |
| 110 | |
| 111 | QVector<RoomInfoItem> |
| 112 | rooms() |
| 113 | { |
| 114 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 115 | interface.isValid()) |
| 116 | return QDBusReply<QVector<RoomInfoItem>>{interface.call(QStringLiteral("rooms"))}.value(); |
| 117 | else |
| 118 | return {}; |
| 119 | } |
| 120 | |
| 121 | QImage |
| 122 | image(const QString &mxcuri) |
| 123 | { |
| 124 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 125 | interface.isValid()) |
| 126 | return QDBusReply<QImage>{interface.call(QStringLiteral("image"), mxcuri)}.value(); |
| 127 | else |
| 128 | return {}; |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | activateRoom(const QString &alias) |
| 133 | { |
| 134 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 135 | interface.isValid()) |
| 136 | interface.call(QDBus::NoBlock, QStringLiteral("activateRoom"), alias); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | joinRoom(const QString &alias) |
| 141 | { |
| 142 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 143 | interface.isValid()) |
| 144 | interface.call(QDBus::NoBlock, QStringLiteral("joinRoom"), alias); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | directChat(const QString &userId) |
| 149 | { |
| 150 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 151 | interface.isValid()) |
| 152 | interface.call(QDBus::NoBlock, QStringLiteral("directChat"), userId); |
| 153 | } |
| 154 | |
| 155 | QString |
| 156 | statusMessage() |
| 157 | { |
| 158 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 159 | interface.isValid()) |
| 160 | return QDBusReply<QString>{interface.call(QStringLiteral("statusMessage"))}.value(); |
| 161 | else |
| 162 | return {}; |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | setStatusMessage(const QString &message) |
| 167 | { |
| 168 | if (QDBusInterface interface{QStringLiteral(NHEKO_DBUS_SERVICE_NAME), QStringLiteral("/")}; |
| 169 | interface.isValid()) |
| 170 | interface.call(QDBus::NoBlock, QStringLiteral("setStatusMessage"), message); |
| 171 | } |
| 172 | |
| 173 | } // nheko::dbus |
| 174 | |
| 175 | /** |
| 176 | * Automatic marshaling of a QImage for org.freedesktop.Notifications.Notify |
| 177 | * |
| 178 | * This function is heavily based on a function from the Clementine project (see |
| 179 | * http://www.clementine-player.org) and licensed under the GNU General Public |
| 180 | * License, version 3 or later. |
| 181 | * |
| 182 | */ |
| 183 | QDBusArgument & |
| 184 | operator<<(QDBusArgument &arg, const QImage &image) |
| 185 | { |
| 186 | if (image.isNull()) { |
| 187 | arg.beginStructure(); |
| 188 | arg << 0 << 0 << 0 << false << 0 << 0 << QByteArray(); |
| 189 | arg.endStructure(); |
| 190 | return arg; |
| 191 | } |
| 192 | |
| 193 | QImage i = image.height() > 100 || image.width() > 100 |
| 194 | ? image.scaledToHeight(100, Qt::SmoothTransformation) |
| 195 | : image; |
| 196 | bool hasAlpha = i.hasAlphaChannel(); |
| 197 | i = std::move(i).convertToFormat(hasAlpha ? QImage::Format_RGBA8888 : QImage::Format_RGB888); |
| 198 | |
| 199 | int channels = hasAlpha ? 4 : 3; |
| 200 | QByteArray arr(reinterpret_cast<const char *>(i.bits()), static_cast<int>(i.sizeInBytes())); |
| 201 | arg.beginStructure(); |
| 202 | arg << i.width() << i.height() << (int)i.bytesPerLine() << i.hasAlphaChannel() |
| 203 | << i.depth() / channels << channels << arr; |
| 204 | arg.endStructure(); |
| 205 | |
| 206 | return arg; |
| 207 | } |
| 208 | |
| 209 | // This function, however, was merely reverse-engineered from the above function |
| 210 | // and is not from the Clementine project. |
| 211 | const QDBusArgument & |
| 212 | operator>>(const QDBusArgument &arg, QImage &image) |
| 213 | { |
| 214 | // garbage is used as a sort of /dev/null |
| 215 | int width, height, garbage; |
| 216 | QByteArray bits; |
| 217 | |
| 218 | arg.beginStructure(); |
| 219 | arg >> width >> height >> garbage >> garbage >> garbage >> garbage >> bits; |
| 220 | arg.endStructure(); |
| 221 | |
| 222 | // Unfortunately, this copy-and-detach is necessary to ensure that the source buffer |
| 223 | // is copied properly. If anybody finds a better solution, please implement it. |
| 224 | auto temp = |
| 225 | QImage(reinterpret_cast<uchar *>(bits.data()), width, height, QImage::Format_RGBA8888); |
| 226 | image = temp; |
| 227 | image.detach(); |
| 228 | |
| 229 | return arg; |
| 230 | } |
| 231 | |
| 232 | #include "moc_NhekoDBusApi.cpp" |