File indexing completed on 2024-05-12 05:02:10

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "notifierjob.h"
0008 #include "ruqola_debug.h"
0009 #include <KLocalizedString>
0010 #include <KNotification>
0011 #include <KNotificationReplyAction>
0012 #include <KWindowSystem>
0013 
0014 NotifierJob::NotifierJob(QObject *parent)
0015     : QObject(parent)
0016 {
0017 }
0018 
0019 NotifierJob::~NotifierJob() = default;
0020 
0021 void NotifierJob::start()
0022 {
0023     if (mInfo.isValid()) {
0024         auto notification = new KNotification(QStringLiteral("new-notification"), KNotification::CloseOnTimeout);
0025         notification->setTitle(mInfo.title());
0026         const QString userName = mInfo.senderName().isEmpty() ? mInfo.senderUserName() : mInfo.senderName();
0027         notification->setText(i18n("%1: %2", userName, mInfo.message().toHtmlEscaped()));
0028         if (!mInfo.pixmap().isNull()) {
0029             notification->setPixmap(mInfo.pixmap());
0030         }
0031 
0032         auto openChannel = [this, notification] {
0033             KWindowSystem::setCurrentXdgActivationToken(notification->xdgActivationToken());
0034             Q_EMIT switchToAccountAndRoomName(mInfo.accountName(), mInfo.roomId(), mInfo.channelType());
0035         };
0036 
0037         auto defaultAction = notification->addDefaultAction(i18nc("Open channel", "Open Channel"));
0038         connect(defaultAction, &KNotificationAction::activated, this, openChannel);
0039         connect(notification, &KNotification::closed, this, &NotifierJob::deleteLater);
0040 
0041         std::unique_ptr<KNotificationReplyAction> replyAction(new KNotificationReplyAction(i18n("Reply")));
0042         replyAction->setPlaceholderText(i18n("Reply..."));
0043         QObject::connect(replyAction.get(), &KNotificationReplyAction::replied, this, [this](const QString &text) {
0044             // qDebug() << " mInfo " << mInfo;
0045             Q_EMIT sendReply(text, mInfo.roomId(), mInfo.tmId());
0046             // qDebug() << " reply " << text;
0047         });
0048         notification->setReplyAction(std::move(replyAction));
0049         notification->sendEvent();
0050     } else {
0051         qCWarning(RUQOLA_LOG) << "Info is invalid";
0052         deleteLater();
0053     }
0054 }
0055 
0056 NotificationInfo NotifierJob::info() const
0057 {
0058     return mInfo;
0059 }
0060 
0061 void NotifierJob::setInfo(const NotificationInfo &info)
0062 {
0063     mInfo = info;
0064 }
0065 
0066 #include "moc_notifierjob.cpp"