File indexing completed on 2024-10-06 12:54:07

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 "controller.h"
0009 #include "neochatroom.h"
0010 #include "roommanager.h"
0011 #include "windowcontroller.h"
0012 
0013 RemoteImage Runner::serializeImage(const QImage &image)
0014 {
0015     QImage convertedImage = image.convertToFormat(QImage::Format_RGBA8888);
0016     RemoteImage remoteImage{
0017         convertedImage.width(),
0018         convertedImage.height(),
0019         convertedImage.bytesPerLine(),
0020         true, // hasAlpha
0021         8, // bitsPerSample
0022         4, // channels
0023         QByteArray(reinterpret_cast<const char *>(convertedImage.constBits()), convertedImage.sizeInBytes()),
0024     };
0025     return remoteImage;
0026 }
0027 
0028 Runner::Runner()
0029     : QObject()
0030 {
0031     qDBusRegisterMetaType<RemoteMatch>();
0032     qDBusRegisterMetaType<RemoteMatches>();
0033     qDBusRegisterMetaType<RemoteAction>();
0034     qDBusRegisterMetaType<RemoteActions>();
0035     qDBusRegisterMetaType<RemoteImage>();
0036 
0037     m_model.setSourceModel(&m_sourceModel);
0038 
0039     connect(&Controller::instance(), &Controller::activeConnectionChanged, this, &Runner::activeConnectionChanged);
0040 }
0041 
0042 void Runner::activeConnectionChanged()
0043 {
0044     m_sourceModel.setConnection(Controller::instance().activeConnection());
0045 }
0046 
0047 RemoteActions Runner::Actions()
0048 {
0049     return {};
0050 }
0051 
0052 RemoteMatches Runner::Match(const QString &searchTerm)
0053 {
0054     m_model.setFilterText(searchTerm);
0055 
0056     RemoteMatches matches;
0057 
0058     for (int i = 0; i < m_model.rowCount(); ++i) {
0059         RemoteMatch match;
0060 
0061         const QString name = m_model.data(m_model.index(i, 0), RoomListModel::DisplayNameRole).toString();
0062 
0063         match.iconName = QStringLiteral("org.kde.neochat");
0064         match.id = m_model.data(m_model.index(i, 0), RoomListModel::RoomIdRole).toString();
0065         match.text = name;
0066         match.relevance = 1;
0067         const RemoteImage remoteImage = serializeImage(m_model.data(m_model.index(i, 0), RoomListModel::AvatarImageRole).value<QImage>());
0068         match.properties.insert(QStringLiteral("icon-data"), QVariant::fromValue(remoteImage));
0069         match.properties.insert(QStringLiteral("subtext"), m_model.data(m_model.index(i, 0), RoomListModel::TopicRole).toString());
0070 
0071         if (name.compare(searchTerm, Qt::CaseInsensitive) == 0) {
0072             match.type = ExactMatch;
0073         } else {
0074             match.type = CompletionMatch;
0075         }
0076 
0077         matches << match;
0078     }
0079 
0080     return matches;
0081 }
0082 
0083 void Runner::Run(const QString &id, const QString &actionId)
0084 {
0085     Q_UNUSED(actionId);
0086 
0087     NeoChatRoom *room = qobject_cast<NeoChatRoom *>(Controller::instance().activeConnection()->room(id));
0088 
0089     if (!room) {
0090         return;
0091     }
0092 
0093     RoomManager::instance().enterRoom(room);
0094 
0095     WindowController::instance().showAndRaiseWindow(QString());
0096 }
0097 
0098 #include "moc_runner.cpp"