File indexing completed on 2024-10-06 04:34:00

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 "block.h"
0008 #include "ruqola_debug.h"
0009 #include <KLocalizedString>
0010 #include <QJsonObject>
0011 
0012 Block::Block() = default;
0013 
0014 void Block::parseBlock(const QJsonObject &block)
0015 {
0016     // "blocks":[{"appId":"videoconf-core","blockId":"63981f8a4ef3f3baa965a0d8","callId":"63981f8a4ef3f3baa965a0d8","type":"video_conf"}]
0017     mBlockId = block[QLatin1String("blockId")].toString();
0018     mCallId = block[QLatin1String("callId")].toString();
0019     mAppId = block[QLatin1String("appId")].toString();
0020     setBlockTypeStr(block[QLatin1String("type")].toString());
0021     if (mBlockType == Unknown) {
0022         qCWarning(RUQOLA_LOG) << " Unknown type " << block;
0023     }
0024 }
0025 
0026 QString Block::convertEnumToStr(BlockType newBlockType) const
0027 {
0028     switch (newBlockType) {
0029     case Unknown:
0030         return {};
0031     case VideoConf:
0032         return QStringLiteral("video_conf");
0033     }
0034     return {};
0035 }
0036 
0037 Block::BlockType Block::convertBlockTypeToEnum(const QString &typeStr)
0038 {
0039     if (typeStr == QLatin1String("video_conf")) {
0040         return VideoConf;
0041     }
0042     qCWarning(RUQOLA_LOG) << " Invalid BlockType " << typeStr;
0043     return Unknown;
0044 }
0045 
0046 VideoConferenceInfo Block::videoConferenceInfo() const
0047 {
0048     return mVideoConferenceInfo;
0049 }
0050 
0051 void Block::setVideoConferenceInfo(const VideoConferenceInfo &newInfo)
0052 {
0053     mVideoConferenceInfo = newInfo;
0054 }
0055 
0056 QString Block::blockTypeStr() const
0057 {
0058     return mBlockStr;
0059 }
0060 
0061 void Block::setBlockTypeStr(const QString &newBlockStr)
0062 {
0063     if (mBlockStr != newBlockStr) {
0064         mBlockStr = newBlockStr;
0065         mBlockType = convertBlockTypeToEnum(mBlockStr);
0066     }
0067 }
0068 
0069 bool Block::isValid() const
0070 {
0071     return mBlockType != Unknown;
0072 }
0073 
0074 QString Block::title() const
0075 {
0076     switch (mBlockType) {
0077     case Block::BlockType::Unknown:
0078         break;
0079     case Block::BlockType::VideoConf: {
0080         if (mVideoConferenceInfo.isValid()) {
0081             const QString title = mVideoConferenceInfo.title();
0082             if (!title.isEmpty()) {
0083                 return title;
0084             }
0085         }
0086         return i18n("Conference Call");
0087     }
0088     }
0089     return {};
0090 }
0091 
0092 QString Block::blockId() const
0093 {
0094     return mBlockId;
0095 }
0096 
0097 void Block::setBlockId(const QString &newBlockId)
0098 {
0099     mBlockId = newBlockId;
0100 }
0101 
0102 QString Block::callId() const
0103 {
0104     return mCallId;
0105 }
0106 
0107 void Block::setCallId(const QString &newCallId)
0108 {
0109     mCallId = newCallId;
0110 }
0111 
0112 QString Block::appId() const
0113 {
0114     return mAppId;
0115 }
0116 
0117 void Block::setAppId(const QString &newAppId)
0118 {
0119     mAppId = newAppId;
0120 }
0121 
0122 Block::BlockType Block::blockType() const
0123 {
0124     return mBlockType;
0125 }
0126 
0127 void Block::setBlockType(BlockType newBlockType)
0128 {
0129     mBlockType = newBlockType;
0130     mBlockStr = convertEnumToStr(mBlockType);
0131 }
0132 
0133 bool Block::operator==(const Block &other) const
0134 {
0135     return mBlockId == other.blockId() && mCallId == other.callId() && mAppId == other.appId() && mBlockStr == other.blockTypeStr();
0136 }
0137 
0138 QJsonObject Block::serialize(const Block &block)
0139 {
0140     QJsonObject o;
0141     o[QLatin1String("blockId")] = block.blockId();
0142     o[QLatin1String("callId")] = block.callId();
0143     o[QLatin1String("appId")] = block.appId();
0144     o[QLatin1String("type")] = block.blockTypeStr();
0145     if (block.mVideoConferenceInfo.isValid()) {
0146         o[QLatin1String("videoconferenceinfo")] = VideoConferenceInfo::serialize(block.mVideoConferenceInfo);
0147     } else {
0148         qCWarning(RUQOLA_LOG) << "block.mVideoConferenceInfo is invalid " << block.mVideoConferenceInfo;
0149     }
0150     return o;
0151 }
0152 
0153 Block Block::deserialize(const QJsonObject &o)
0154 {
0155     Block block;
0156     block.setBlockId(o[QLatin1String("blockId")].toString());
0157     block.setCallId(o[QLatin1String("callId")].toString());
0158     block.setAppId(o[QLatin1String("appId")].toString());
0159     block.setBlockTypeStr(o[QLatin1String("type")].toString());
0160     const VideoConferenceInfo info = VideoConferenceInfo::deserialize(o[QLatin1String("videoconferenceinfo")].toObject());
0161     if (info.isValid()) {
0162         block.mVideoConferenceInfo = info;
0163     } else {
0164         qCWarning(RUQOLA_LOG) << "info is invalid " << info;
0165     }
0166     return block;
0167 }
0168 
0169 QDebug operator<<(QDebug d, const Block &t)
0170 {
0171     d.space() << "blockId" << t.blockId();
0172     d.space() << "callId" << t.callId();
0173     d.space() << "appId" << t.appId();
0174     d.space() << "blockTypeStr" << t.blockTypeStr();
0175     d.space() << "mBlockType" << t.blockType();
0176     d.space() << "Video conf info" << t.videoConferenceInfo();
0177     return d;
0178 }
0179 
0180 #include "moc_block.cpp"