File indexing completed on 2024-12-08 04:34:11

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 "conferencedirectcalldialog.h"
0008 #include "connection.h"
0009 #include "ddpapi/ddpclient.h"
0010 #include "rocketchataccount.h"
0011 #include "ruqolawidgets_debug.h"
0012 #include "video-conference/videoconferencecanceljob.h"
0013 #include "video-conference/videoconferencejoinjob.h"
0014 #include "video-conference/videoconferencestartjob.h"
0015 #include "videoconference/videoconferencemanager.h"
0016 
0017 #include <KLocalizedString>
0018 #include <QDesktopServices>
0019 #include <QDialogButtonBox>
0020 #include <QPushButton>
0021 #include <QTimer>
0022 #include <QVBoxLayout>
0023 #include <chrono>
0024 
0025 using namespace std::chrono_literals;
0026 
0027 ConferenceDirectCallDialog::ConferenceDirectCallDialog(RocketChatAccount *account, QWidget *parent)
0028     : QDialog(parent)
0029     , mConferenceCallWidget(new ConferenceCallWidget(account, this))
0030     , mRocketChatAccount(account)
0031 {
0032     setWindowTitle(i18nc("@title:window", "Start a Direct Call"));
0033     auto mainLayout = new QVBoxLayout(this);
0034     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0035 
0036     mConferenceCallWidget->setObjectName(QStringLiteral("mConferenceCallWidget"));
0037     mainLayout->addWidget(mConferenceCallWidget);
0038 
0039     auto button = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0040     button->setObjectName(QStringLiteral("button"));
0041     mOkButton = button->button(QDialogButtonBox::Ok);
0042     mOkButton->setText(i18n("Start"));
0043     mainLayout->addWidget(button);
0044     connect(button, &QDialogButtonBox::rejected, this, &ConferenceDirectCallDialog::slotRejected);
0045     connect(button, &QDialogButtonBox::accepted, this, &ConferenceDirectCallDialog::slotStartVideoConference);
0046 }
0047 
0048 ConferenceDirectCallDialog::~ConferenceDirectCallDialog() = default;
0049 
0050 void ConferenceDirectCallDialog::slotRejected()
0051 {
0052     auto job = new RocketChatRestApi::VideoConferenceCancelJob(this);
0053     job->setCallId(mCallId);
0054     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0055     connect(job, &RocketChatRestApi::VideoConferenceCancelJob::videoConferenceCancelDone, this, [](const QJsonObject &obj) {
0056         qDebug() << "stop obj " << obj;
0057     });
0058     if (!job->start()) {
0059         qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start VideoConferenceCancelJob job";
0060     }
0061     reject();
0062 }
0063 
0064 void ConferenceDirectCallDialog::slotStartVideoConference()
0065 {
0066     // Disable start button
0067     mOkButton->setEnabled(false);
0068     auto job = new RocketChatRestApi::VideoConferenceStartJob(this);
0069     RocketChatRestApi::VideoConferenceStartJob::VideoConferenceStartInfo startInfo;
0070     startInfo.roomId = mRoomId;
0071     startInfo.allowRinging = mAllowRinging;
0072     job->setInfo(startInfo);
0073     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0074     connect(job, &RocketChatRestApi::VideoConferenceStartJob::videoConferenceStartDone, this, [this](const QJsonObject &obj) {
0075         mCallId = obj[QLatin1String("callId")].toString();
0076         callUser();
0077     });
0078     if (!job->start()) {
0079         qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start VideoConferenceStartJob job";
0080     }
0081     connect(mRocketChatAccount->videoConferenceManager(),
0082             &VideoConferenceManager::videoConferenceCallAccepted,
0083             this,
0084             &ConferenceDirectCallDialog::slotVideoConferenceAccepted);
0085     connect(mRocketChatAccount->videoConferenceManager(),
0086             &VideoConferenceManager::videoConferenceCallCanceled,
0087             this,
0088             &ConferenceDirectCallDialog::slotVideoConferenceCanceled);
0089 }
0090 
0091 void ConferenceDirectCallDialog::slotVideoConferenceCanceled(const VideoConference &videoConference)
0092 {
0093     if (videoConference.callId() == mCallId) {
0094         cancelCall();
0095         reject();
0096     }
0097 }
0098 
0099 void ConferenceDirectCallDialog::slotVideoConferenceAccepted(const VideoConference &videoConference)
0100 {
0101     if (videoConference.callId() == mCallId) {
0102         // qDebug() << " videoConference " << videoConference;
0103         mRocketChatAccount->ddp()->videoConferenceConfirmed(mRoomId, mCallId, mRocketChatAccount->userId());
0104         mWasAccepted = true;
0105         auto conferenceJoinJob = new RocketChatRestApi::VideoConferenceJoinJob(this);
0106         RocketChatRestApi::VideoConferenceJoinJob::VideoConferenceJoinInfo joinInfo;
0107         joinInfo.callId = mCallId;
0108         const auto confCallInfo = mConferenceCallWidget->conferenceCallInfo();
0109         joinInfo.useCamera = confCallInfo.useCamera;
0110         joinInfo.useMicro = confCallInfo.useMic;
0111         conferenceJoinJob->setInfo(joinInfo);
0112         mRocketChatAccount->restApi()->initializeRestApiJob(conferenceJoinJob);
0113         connect(conferenceJoinJob, &RocketChatRestApi::VideoConferenceJoinJob::videoConferenceJoinDone, this, [this](const QJsonObject &obj) {
0114             // qDebug() << " join info " << obj;
0115             QDesktopServices::openUrl(QUrl(obj[QLatin1String("url")].toString()));
0116             accept();
0117         });
0118         if (!conferenceJoinJob->start()) {
0119             qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start VideoConferenceJoinJob job";
0120         }
0121     }
0122 }
0123 
0124 void ConferenceDirectCallDialog::callUser()
0125 {
0126     if (mWasAccepted) {
0127         return;
0128     }
0129     mRocketChatAccount->ddp()->videoConferenceCall(mRoomId, mCallId, mRocketChatAccount->userId());
0130     if (mNumberOfCall <= 3) {
0131         QTimer::singleShot(3s, this, &ConferenceDirectCallDialog::callUser);
0132     } else {
0133         cancelCall();
0134         reject();
0135     }
0136     mNumberOfCall++;
0137 }
0138 
0139 void ConferenceDirectCallDialog::cancelCall()
0140 {
0141     mRocketChatAccount->ddp()->videoConferenceRejected(mRoomId, mCallId, mRocketChatAccount->userId());
0142 }
0143 
0144 bool ConferenceDirectCallDialog::allowRinging() const
0145 {
0146     return mAllowRinging;
0147 }
0148 
0149 void ConferenceDirectCallDialog::setAllowRinging(bool newAllowRinging)
0150 {
0151     mAllowRinging = newAllowRinging;
0152 }
0153 
0154 QString ConferenceDirectCallDialog::roomId() const
0155 {
0156     return mRoomId;
0157 }
0158 
0159 void ConferenceDirectCallDialog::setRoomId(const QString &newRoomId)
0160 {
0161     mRoomId = newRoomId;
0162 }
0163 
0164 #include "moc_conferencedirectcalldialog.cpp"