File indexing completed on 2024-04-14 04:54:21

0001 // SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "runner.h"
0005 
0006 #include <QDBusMetaType>
0007 
0008 #include "neochatroom.h"
0009 #include "roommanager.h"
0010 #include "windowcontroller.h"
0011 
0012 RemoteImage Runner::serializeImage(const QImage &image)
0013 {
0014     QImage convertedImage = image.convertToFormat(QImage::Format_RGBA8888);
0015     RemoteImage remoteImage{
0016         convertedImage.width(),
0017         convertedImage.height(),
0018         static_cast<int>(convertedImage.bytesPerLine()),
0019         true, // hasAlpha
0020         8, // bitsPerSample
0021         4, // channels
0022         QByteArray(reinterpret_cast<const char *>(convertedImage.constBits()), convertedImage.sizeInBytes()),
0023     };
0024     return remoteImage;
0025 }
0026 
0027 Runner::Runner()
0028     : QObject()
0029 {
0030     qDBusRegisterMetaType<RemoteMatch>();
0031     qDBusRegisterMetaType<RemoteMatches>();
0032     qDBusRegisterMetaType<RemoteAction>();
0033     qDBusRegisterMetaType<RemoteActions>();
0034     qDBusRegisterMetaType<RemoteImage>();
0035 }
0036 
0037 void Runner::setRoomListModel(RoomListModel *roomListModel)
0038 {
0039     m_model.setSourceModel(roomListModel);
0040     Q_EMIT roomListModelChanged();
0041 }
0042 
0043 RoomListModel *Runner::roomListModel() const
0044 {
0045     return dynamic_cast<RoomListModel *>(m_model.sourceModel());
0046 }
0047 
0048 RemoteActions Runner::Actions()
0049 {
0050     return {};
0051 }
0052 
0053 RemoteMatches Runner::Match(const QString &searchTerm)
0054 {
0055     m_model.setFilterText(searchTerm);
0056 
0057     RemoteMatches matches;
0058 
0059     for (int i = 0; i < m_model.rowCount(); ++i) {
0060         RemoteMatch match;
0061 
0062         const QString name = m_model.data(m_model.index(i, 0), RoomListModel::DisplayNameRole).toString();
0063 
0064         match.iconName = QStringLiteral("org.kde.neochat");
0065         match.id = m_model.data(m_model.index(i, 0), RoomListModel::RoomIdRole).toString();
0066         match.text = name;
0067         match.relevance = 1;
0068         const RemoteImage remoteImage = serializeImage(m_model.data(m_model.index(i, 0), RoomListModel::AvatarImageRole).value<QImage>());
0069         match.properties.insert(QStringLiteral("icon-data"), QVariant::fromValue(remoteImage));
0070         match.properties.insert(QStringLiteral("subtext"), m_model.data(m_model.index(i, 0), RoomListModel::TopicRole).toString());
0071 
0072         if (name.compare(searchTerm, Qt::CaseInsensitive) == 0) {
0073             match.type = ExactMatch;
0074         } else {
0075             match.type = CompletionMatch;
0076         }
0077 
0078         matches << match;
0079     }
0080 
0081     return matches;
0082 }
0083 
0084 void Runner::Run(const QString &id, const QString &actionId)
0085 {
0086     Q_UNUSED(actionId);
0087 
0088     RoomManager::instance().resolveResource(id);
0089     WindowController::instance().showAndRaiseWindow(QString());
0090 }
0091 
0092 #include "moc_runner.cpp"