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

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "otrnotificationjob.h"
0008 #include "rocketchataccount.h"
0009 #include "ruqola_debug.h"
0010 
0011 #include <KLocalizedString>
0012 #include <KNotification>
0013 
0014 OtrNotificationJob::OtrNotificationJob(QObject *parent)
0015     : QObject{parent}
0016 {
0017 }
0018 
0019 OtrNotificationJob::~OtrNotificationJob() = default;
0020 
0021 void OtrNotificationJob::setRocketChatAccount(RocketChatAccount *account)
0022 {
0023     mRocketChatAccount = account;
0024 }
0025 
0026 bool OtrNotificationJob::canStart() const
0027 {
0028     return mOtr.isValid();
0029 }
0030 
0031 void OtrNotificationJob::start()
0032 {
0033     if (!canStart()) {
0034         qCWarning(RUQOLA_LOG) << "Impossible to start OtrNotificationJob";
0035         deleteLater();
0036         return;
0037     }
0038     switch (mOtr.type()) {
0039     case Otr::OtrType::Unknown:
0040         qCWarning(RUQOLA_LOG) << "It's a bug we can't have otrtype == Unknown";
0041         deleteLater();
0042         break;
0043     case Otr::OtrType::End: {
0044         auto notification = new KNotification(QStringLiteral("Otr-end"), KNotification::CloseOnTimeout);
0045         notification->setTitle(i18n("OTR"));
0046         notification->setIconName(QStringLiteral("network-connect"));
0047         notification->setText(generateText());
0048         notification->sendEvent();
0049         Q_EMIT endOtr(mOtr);
0050         deleteLater();
0051         break;
0052     }
0053     case Otr::OtrType::Handshake: {
0054         auto notification = new KNotification(QStringLiteral("Otr-handshake"), KNotification::CloseOnTimeout);
0055         notification->setTitle(i18n("OTR"));
0056         notification->setIconName(QStringLiteral("network-connect"));
0057         notification->setText(generateText());
0058 
0059         auto rejectAction = notification->addAction(i18n("Reject"));
0060         connect(rejectAction, &KNotificationAction::activated, this, &OtrNotificationJob::slotRejectOtr);
0061 
0062         auto okAction = notification->addAction(i18n("Ok"));
0063         connect(okAction, &KNotificationAction::activated, this, &OtrNotificationJob::slotAcceptOtr);
0064         connect(notification, &KNotification::closed, this, &OtrNotificationJob::deleteLater);
0065         notification->sendEvent();
0066         break;
0067     }
0068     case Otr::OtrType::Deny: {
0069         auto notification = new KNotification(QStringLiteral("Otr-deny"), KNotification::CloseOnTimeout);
0070         notification->setTitle(i18n("OTR"));
0071         notification->setIconName(QStringLiteral("network-connect"));
0072         notification->setText(generateText());
0073         notification->sendEvent();
0074         deleteLater();
0075         break;
0076     }
0077     case Otr::OtrType::AcknowLedge:
0078         Q_EMIT acknowLedgeOtr(mOtr);
0079         deleteLater();
0080         break;
0081     }
0082 }
0083 
0084 QString OtrNotificationJob::generateText()
0085 {
0086     QString str;
0087     // TODO search real name.
0088     const QString userId = mOtr.userId();
0089 
0090     switch (mOtr.type()) {
0091     case Otr::OtrType::Unknown:
0092         break;
0093     case Otr::OtrType::End: {
0094         str = mRocketChatAccount->accountName() + QLatin1Char('\n') + i18n("%1 ended the OTR session.", QStringLiteral("test")); // FIXME use correct name
0095         break;
0096     }
0097     case Otr::OtrType::Handshake: {
0098         str = mRocketChatAccount->accountName() + QLatin1Char('\n')
0099             + i18n("%1  wants to start OTR. Do you want to accept?.", QStringLiteral("test")); // FIXME use correct name
0100         break;
0101     }
0102     case Otr::OtrType::Deny: {
0103         str = mRocketChatAccount->accountName() + QLatin1Char('\n') + i18n("%1 denied the OTR session.", QStringLiteral("test")); // FIXME use correct name
0104         break;
0105     }
0106     case Otr::OtrType::AcknowLedge:
0107         break;
0108     }
0109     return str;
0110 }
0111 
0112 void OtrNotificationJob::slotAcceptOtr()
0113 {
0114     Q_EMIT acceptOtr(mOtr);
0115     qDebug() << " void OtrNotificationJob::acceptOtr()";
0116     deleteLater();
0117 }
0118 
0119 void OtrNotificationJob::slotRejectOtr()
0120 {
0121     Q_EMIT rejectOtr(mOtr);
0122     qDebug() << " Reject ";
0123     deleteLater();
0124 }
0125 
0126 const Otr &OtrNotificationJob::otr() const
0127 {
0128     return mOtr;
0129 }
0130 
0131 void OtrNotificationJob::setOtr(const Otr &newOtr)
0132 {
0133     mOtr = newOtr;
0134 }
0135 
0136 #include "moc_otrnotificationjob.cpp"