File indexing completed on 2024-05-12 16:25:54

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 #if QT_VERSION_MAJOR == 6
0038         auto defaultAction = notification->addDefaultAction(i18nc("Open channel", "Open Channel"));
0039         connect(defaultAction, &KNotificationAction::activated, this, openChannel);
0040 #else
0041         notification->setDefaultAction(i18nc("Open channel", "Open Channel"));
0042         connect(notification, &KNotification::defaultActivated, this, openChannel);
0043 #endif
0044         connect(notification, &KNotification::closed, this, &NotifierJob::deleteLater);
0045 
0046         std::unique_ptr<KNotificationReplyAction> replyAction(new KNotificationReplyAction(i18n("Reply")));
0047         replyAction->setPlaceholderText(i18n("Reply..."));
0048         QObject::connect(replyAction.get(), &KNotificationReplyAction::replied, this, [this](const QString &text) {
0049             // qDebug() << " mInfo " << mInfo;
0050             Q_EMIT sendReply(text, mInfo.roomId(), mInfo.tmId());
0051             // qDebug() << " reply " << text;
0052         });
0053         notification->setReplyAction(std::move(replyAction));
0054         notification->sendEvent();
0055     } else {
0056         qCWarning(RUQOLA_LOG) << "Info is invalid";
0057         deleteLater();
0058     }
0059 }
0060 
0061 NotificationInfo NotifierJob::info() const
0062 {
0063     return mInfo;
0064 }
0065 
0066 void NotifierJob::setInfo(const NotificationInfo &info)
0067 {
0068     mInfo = info;
0069 }
0070 
0071 #include "moc_notifierjob.cpp"