File indexing completed on 2024-05-19 15:58:54
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 #if QT_VERSION_MAJOR == 6 0060 auto rejectAction = notification->addAction(i18n("Reject")); 0061 connect(rejectAction, &KNotificationAction::activated, this, &OtrNotificationJob::slotRejectOtr); 0062 0063 auto okAction = notification->addAction(i18n("Ok")); 0064 connect(okAction, &KNotificationAction::activated, this, &OtrNotificationJob::slotAcceptOtr); 0065 #else 0066 const QStringList lstActions{i18n("Reject"), i18n("Ok")}; 0067 notification->setActions(lstActions); 0068 0069 connect(notification, &KNotification::activated, this, [this](uint val) { 0070 // Index == 0 => is the default action. We don't have it. 0071 switch (val) { 0072 case 0: 0073 break; 0074 case 1: 0075 slotRejectOtr(); 0076 break; 0077 case 2: 0078 slotAcceptOtr(); 0079 break; 0080 } 0081 }); 0082 #endif 0083 0084 connect(notification, &KNotification::closed, this, &OtrNotificationJob::deleteLater); 0085 notification->sendEvent(); 0086 break; 0087 } 0088 case Otr::OtrType::Deny: { 0089 auto notification = new KNotification(QStringLiteral("Otr-deny"), KNotification::CloseOnTimeout); 0090 notification->setTitle(i18n("OTR")); 0091 notification->setIconName(QStringLiteral("network-connect")); 0092 notification->setText(generateText()); 0093 notification->sendEvent(); 0094 deleteLater(); 0095 break; 0096 } 0097 case Otr::OtrType::AcknowLedge: 0098 Q_EMIT acknowLedgeOtr(mOtr); 0099 deleteLater(); 0100 break; 0101 } 0102 } 0103 0104 QString OtrNotificationJob::generateText() 0105 { 0106 QString str; 0107 // TODO search real name. 0108 const QString userId = mOtr.userId(); 0109 0110 switch (mOtr.type()) { 0111 case Otr::OtrType::Unknown: 0112 break; 0113 case Otr::OtrType::End: { 0114 str = mRocketChatAccount->accountName() + QLatin1Char('\n') + i18n("%1 ended the OTR session.", QStringLiteral("test")); // FIXME use correct name 0115 break; 0116 } 0117 case Otr::OtrType::Handshake: { 0118 str = mRocketChatAccount->accountName() + QLatin1Char('\n') 0119 + i18n("%1 wants to start OTR. Do you want to accept?.", QStringLiteral("test")); // FIXME use correct name 0120 break; 0121 } 0122 case Otr::OtrType::Deny: { 0123 str = mRocketChatAccount->accountName() + QLatin1Char('\n') + i18n("%1 denied the OTR session.", QStringLiteral("test")); // FIXME use correct name 0124 break; 0125 } 0126 case Otr::OtrType::AcknowLedge: 0127 break; 0128 } 0129 return str; 0130 } 0131 0132 void OtrNotificationJob::slotAcceptOtr() 0133 { 0134 Q_EMIT acceptOtr(mOtr); 0135 qDebug() << " void OtrNotificationJob::acceptOtr()"; 0136 deleteLater(); 0137 } 0138 0139 void OtrNotificationJob::slotRejectOtr() 0140 { 0141 Q_EMIT rejectOtr(mOtr); 0142 qDebug() << " Reject "; 0143 deleteLater(); 0144 } 0145 0146 const Otr &OtrNotificationJob::otr() const 0147 { 0148 return mOtr; 0149 } 0150 0151 void OtrNotificationJob::setOtr(const Otr &newOtr) 0152 { 0153 mOtr = newOtr; 0154 } 0155 0156 #include "moc_otrnotificationjob.cpp"