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

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "videoconferencenotificationjob.h"
0008 #include "rocketchataccount.h"
0009 #include "ruqola_videoconference_core_debug.h"
0010 #include <KLocalizedString>
0011 #include <KNotification>
0012 
0013 VideoConferenceNotificationJob::VideoConferenceNotificationJob(QObject *parent)
0014     : QObject{parent}
0015 {
0016 }
0017 
0018 VideoConferenceNotificationJob::~VideoConferenceNotificationJob() = default;
0019 
0020 void VideoConferenceNotificationJob::start()
0021 {
0022     if (!canStart()) {
0023         qCWarning(RUQOLA_VIDEO_CONFERENCE_LOG) << "Impossible to start VideoConferenceNotificationJob";
0024         deleteLater();
0025         return;
0026     }
0027     switch (mVideoConference.action()) {
0028     case VideoConference::IncomingCall:
0029         inComingCall();
0030         return;
0031     case VideoConference::Unknown:
0032     case VideoConference::Canceled:
0033     case VideoConference::Confirmed:
0034     case VideoConference::Accepted:
0035     case VideoConference::Rejected:
0036         break;
0037     }
0038     deleteLater();
0039 }
0040 
0041 QString VideoConferenceNotificationJob::generateText() const
0042 {
0043     const QString str = mRocketChatAccount->accountName() + QLatin1Char('\n');
0044     // Add user name!
0045     return str;
0046 }
0047 
0048 void VideoConferenceNotificationJob::inComingCall()
0049 {
0050     auto notification = new KNotification(QStringLiteral("VideoConference-Incoming"), KNotification::CloseOnTimeout);
0051     notification->setTitle(i18n("InComing Call"));
0052     // notification->setIconName(QStringLiteral("network-connect"));
0053     notification->setText(generateText());
0054 
0055 #if QT_VERSION_MAJOR == 6
0056     auto acceptAction = notification->addAction(i18n("Accept"));
0057     connect(acceptAction, &KNotificationAction::activated, this, [this] {
0058         Q_EMIT acceptVideoConference();
0059         deleteLater();
0060     });
0061 
0062     auto rejectAction = notification->addAction(i18n("Reject"));
0063     connect(rejectAction, &KNotificationAction::activated, this, [this] {
0064         Q_EMIT rejectVideoConference();
0065         deleteLater();
0066     });
0067 #else
0068     const QStringList lstActions{i18n("Accept"), i18n("Reject")};
0069     notification->setActions(lstActions);
0070     connect(notification, &KNotification::activated, this, [this](uint val) {
0071         // Index == 0 => is the default action. We don't have it.
0072         switch (val) {
0073         case 0:
0074             break;
0075         case 1:
0076             Q_EMIT acceptVideoConference();
0077             break;
0078         case 2:
0079             Q_EMIT rejectVideoConference();
0080             break;
0081         }
0082         deleteLater();
0083     });
0084 #endif
0085 
0086     connect(notification, &KNotification::closed, this, &VideoConferenceNotificationJob::deleteLater);
0087     notification->sendEvent();
0088 }
0089 
0090 bool VideoConferenceNotificationJob::canStart() const
0091 {
0092     return mVideoConference.isValid();
0093 }
0094 
0095 void VideoConferenceNotificationJob::setRocketChatAccount(RocketChatAccount *account)
0096 {
0097     mRocketChatAccount = account;
0098 }
0099 
0100 VideoConference VideoConferenceNotificationJob::videoConference() const
0101 {
0102     return mVideoConference;
0103 }
0104 
0105 void VideoConferenceNotificationJob::setVideoConference(const VideoConference &newVideoConference)
0106 {
0107     mVideoConference = newVideoConference;
0108 }
0109 
0110 #include "moc_videoconferencenotificationjob.cpp"