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 "videoconferenceinfo.h"
0008 #include "ruqola_videoconference_core_debug.h"
0009 #include "utils.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QJsonObject>
0014 
0015 VideoConferenceInfo::VideoConferenceInfo() = default;
0016 
0017 VideoConferenceInfo::~VideoConferenceInfo() = default;
0018 
0019 void VideoConferenceInfo::parse(const QJsonObject &content)
0020 {
0021     qCDebug(RUQOLA_VIDEO_CONFERENCE_LOG) << " content " << content;
0022     mBlockId = content[QLatin1String("_id")].toString();
0023     mStatus = content[QLatin1String("status")].toInt();
0024     mUrl = content[QLatin1String("url")].toString();
0025     mRoomId = content[QLatin1String("rid")].toString();
0026     if (content.contains(QLatin1String("createdAt"))) {
0027         setCreatedAtDateTime(Utils::parseIsoDate(QStringLiteral("createdAt"), content));
0028     }
0029     if (content.contains(QLatin1String("endedAt"))) {
0030         setEndedAtDateTime(Utils::parseIsoDate(QStringLiteral("endedAt"), content));
0031     }
0032     const QJsonObject messageObj = content[QLatin1String("messages")].toObject();
0033     mMessageId = messageObj[QLatin1String("started")].toString();
0034     // TODO ended ???
0035 
0036     mConferenceType = convertTypeToEnum(content[QLatin1String("type")].toString());
0037     mProviderName = content[QLatin1String("providerName")].toString();
0038     // Users
0039     const QJsonArray usersArray = content[QLatin1String("users")].toArray();
0040     mUsers.reserve(usersArray.count());
0041     for (const QJsonValue &current : usersArray) {
0042         if (current.type() == QJsonValue::Object) {
0043             const QJsonObject userObject = current.toObject();
0044             User m;
0045             m.parseUserRestApi(userObject, {});
0046             if (m.isValid()) {
0047                 mUsers.append(std::move(m));
0048             }
0049         } else {
0050             qCWarning(RUQOLA_VIDEO_CONFERENCE_LOG) << "Problem when parsing Users" << current;
0051         }
0052     }
0053 }
0054 
0055 VideoConferenceInfo::VideoConferenceType VideoConferenceInfo::convertTypeToEnum(const QString &str) const
0056 {
0057     if (str == QLatin1String("videoconference")) {
0058         return VideoConferenceInfo::VideoConferenceType::Conference;
0059     } else if (str == QLatin1String("direct")) {
0060         return VideoConferenceInfo::VideoConferenceType::Direct;
0061     }
0062     qCWarning(RUQOLA_VIDEO_CONFERENCE_LOG) << "VideoConferenceInfo::convertTypeToEnum invalid " << str;
0063     return VideoConferenceInfo::VideoConferenceType::Unknown;
0064 }
0065 
0066 QString VideoConferenceInfo::convertEnumToString(const VideoConferenceInfo &info)
0067 {
0068     switch (info.conferenceType()) {
0069     case VideoConferenceInfo::VideoConferenceType::Conference:
0070         return QStringLiteral("videoconference");
0071     case VideoConferenceInfo::VideoConferenceType::Direct:
0072         return QStringLiteral("direct");
0073     case VideoConferenceInfo::VideoConferenceType::Unknown:
0074         return {};
0075     }
0076     qCWarning(RUQOLA_VIDEO_CONFERENCE_LOG) << "VideoConferenceInfo::convertEnumToString invalid ";
0077     return {};
0078 }
0079 
0080 QString VideoConferenceInfo::blockId() const
0081 {
0082     return mBlockId;
0083 }
0084 
0085 void VideoConferenceInfo::setBlockId(const QString &newBlockId)
0086 {
0087     mBlockId = newBlockId;
0088 }
0089 
0090 bool VideoConferenceInfo::isValid() const
0091 {
0092     return conferenceType() != VideoConferenceInfo::VideoConferenceType::Unknown;
0093 }
0094 
0095 bool VideoConferenceInfo::canJoin() const
0096 {
0097     return createdAtDateTime() != -1 && endedAtDateTime() == -1;
0098 }
0099 
0100 QString VideoConferenceInfo::title() const
0101 {
0102     return statusInformation();
0103 }
0104 
0105 QJsonObject VideoConferenceInfo::serialize(const VideoConferenceInfo &videoConfInfo)
0106 {
0107     QJsonObject obj;
0108     obj[QLatin1String("_id")] = videoConfInfo.mBlockId;
0109     obj[QLatin1String("status")] = videoConfInfo.mStatus;
0110     obj[QLatin1String("url")] = videoConfInfo.mUrl;
0111     obj[QLatin1String("rid")] = videoConfInfo.mRoomId;
0112     obj[QLatin1String("providerName")] = videoConfInfo.mProviderName;
0113     obj[QLatin1String("messageId")] = videoConfInfo.mMessageId;
0114     obj[QLatin1String("type")] = VideoConferenceInfo::convertEnumToString(videoConfInfo);
0115     obj[QLatin1String("createdAt")] = videoConfInfo.createdAtDateTime();
0116     obj[QLatin1String("endedAt")] = videoConfInfo.endedAtDateTime();
0117     if (!videoConfInfo.mUsers.isEmpty()) {
0118         QJsonArray userArray;
0119         for (const User &user : videoConfInfo.mUsers) {
0120             userArray.append(User::serialize(user));
0121         }
0122         obj[QLatin1String("users")] = userArray;
0123     }
0124     return obj;
0125 }
0126 
0127 VideoConferenceInfo VideoConferenceInfo::deserialize(const QJsonObject &o)
0128 {
0129     VideoConferenceInfo info;
0130     info.mBlockId = o[QLatin1String("_id")].toString();
0131     info.mStatus = o[QLatin1String("status")].toInt();
0132     info.mUrl = o[QLatin1String("url")].toString();
0133     info.mRoomId = o[QLatin1String("rid")].toString();
0134     info.mProviderName = o[QLatin1String("providerName")].toString();
0135     info.mMessageId = o[QLatin1String("messageId")].toString();
0136     info.mCreatedAtDateTime = o[QLatin1String("createdAt")].toInt();
0137     info.mEndedAtDateTime = o[QLatin1String("endedAt")].toInt();
0138 
0139     info.mConferenceType = info.convertTypeToEnum(o[QLatin1String("type")].toString());
0140     const QJsonArray usersArray = o[QLatin1String("users")].toArray();
0141     info.mUsers.reserve(usersArray.count());
0142     for (const QJsonValue &current : usersArray) {
0143         if (current.type() == QJsonValue::Object) {
0144             const QJsonObject userObject = current.toObject();
0145             User m;
0146             m.parseUserRestApi(userObject, {});
0147             if (m.isValid()) {
0148                 info.mUsers.append(std::move(m));
0149             }
0150         } else {
0151             qCWarning(RUQOLA_VIDEO_CONFERENCE_LOG) << "Problem when parsing Users" << current;
0152         }
0153     }
0154     return info;
0155 }
0156 
0157 QString VideoConferenceInfo::messageId() const
0158 {
0159     return mMessageId;
0160 }
0161 
0162 void VideoConferenceInfo::setMessageId(const QString &newMessageId)
0163 {
0164     mMessageId = newMessageId;
0165 }
0166 
0167 QVector<User> VideoConferenceInfo::users() const
0168 {
0169     return mUsers;
0170 }
0171 
0172 void VideoConferenceInfo::setUsers(const QVector<User> &newUsers)
0173 {
0174     mUsers = newUsers;
0175 }
0176 
0177 QString VideoConferenceInfo::providerName() const
0178 {
0179     return mProviderName;
0180 }
0181 
0182 void VideoConferenceInfo::setProviderName(const QString &newProviderName)
0183 {
0184     mProviderName = newProviderName;
0185 }
0186 
0187 QString VideoConferenceInfo::url() const
0188 {
0189     return mUrl;
0190 }
0191 
0192 void VideoConferenceInfo::setUrl(const QString &newUrl)
0193 {
0194     mUrl = newUrl;
0195 }
0196 
0197 int VideoConferenceInfo::status() const
0198 {
0199     return mStatus;
0200 }
0201 
0202 void VideoConferenceInfo::setStatus(int newStatus)
0203 {
0204     mStatus = newStatus;
0205 }
0206 
0207 bool VideoConferenceInfo::ringing() const
0208 {
0209     return mRinging;
0210 }
0211 
0212 void VideoConferenceInfo::setRinging(bool newRinging)
0213 {
0214     mRinging = newRinging;
0215 }
0216 
0217 QString VideoConferenceInfo::roomId() const
0218 {
0219     return mRoomId;
0220 }
0221 
0222 void VideoConferenceInfo::setRoomId(const QString &newRoomId)
0223 {
0224     mRoomId = newRoomId;
0225 }
0226 
0227 qint64 VideoConferenceInfo::createdAtDateTime() const
0228 {
0229     return mCreatedAtDateTime;
0230 }
0231 
0232 void VideoConferenceInfo::setCreatedAtDateTime(qint64 newCreatedAtDateTime)
0233 {
0234     mCreatedAtDateTime = newCreatedAtDateTime;
0235 }
0236 
0237 qint64 VideoConferenceInfo::endedAtDateTime() const
0238 {
0239     return mEndedAtDateTime;
0240 }
0241 
0242 void VideoConferenceInfo::setEndedAtDateTime(qint64 newEndedAtDateTime)
0243 {
0244     mEndedAtDateTime = newEndedAtDateTime;
0245 }
0246 
0247 VideoConferenceInfo::VideoConferenceType VideoConferenceInfo::conferenceType() const
0248 {
0249     return mConferenceType;
0250 }
0251 
0252 void VideoConferenceInfo::setConferenceType(VideoConferenceType newConferenceType)
0253 {
0254     mConferenceType = newConferenceType;
0255 }
0256 
0257 QDebug operator<<(QDebug d, const VideoConferenceInfo &t)
0258 {
0259     d << "mUrl " << t.url();
0260     d << "mStatus " << t.status();
0261     d << "mRinging " << t.ringing();
0262     d << "mRoomId " << t.roomId();
0263     d << "mCreatedAtDateTime " << t.createdAtDateTime();
0264     d << "mEndedAtDateTime " << t.endedAtDateTime();
0265     d << "mProviderName " << t.providerName();
0266     d << "mUsers " << t.users();
0267     d << "mConferenceType " << t.conferenceType();
0268     d << "mMessageId " << t.messageId();
0269     d << "mBlockId " << t.blockId();
0270     return d;
0271 }
0272 
0273 bool VideoConferenceInfo::operator==(const VideoConferenceInfo &other) const
0274 {
0275     return mCreatedAtDateTime == other.createdAtDateTime() && mEndedAtDateTime == other.endedAtDateTime() && mUrl == other.url() && mRoomId == other.roomId()
0276         && mProviderName == other.providerName() && mConferenceType == other.conferenceType() && mStatus == other.status() && mRinging == other.ringing()
0277         && mUsers == other.users() && mMessageId == other.messageId() && mBlockId == other.blockId();
0278 }
0279 
0280 QString VideoConferenceInfo::statusInformation() const
0281 {
0282     if (endedAtDateTime() >= 0) {
0283         if (conferenceType() == VideoConferenceInfo::VideoConferenceType::Direct) {
0284             return i18n("Call was not answered");
0285         } else if (conferenceType() == VideoConferenceInfo::VideoConferenceType::Conference && users().isEmpty()) {
0286             return i18n("Call was not answered");
0287         } else {
0288             return i18n("Call ended");
0289         }
0290     } else {
0291         if (conferenceType() == VideoConferenceInfo::VideoConferenceType::Direct && status() == 0) {
0292             return i18n("Waiting for answer");
0293         } else {
0294             return i18n("Call ongoing");
0295         }
0296     }
0297     return {};
0298 }
0299 
0300 #include "moc_videoconferenceinfo.cpp"