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

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 "teaminfo.h"
0008 
0009 TeamInfo::TeamInfo() = default;
0010 
0011 void TeamInfo::parseTeamInfo(const QJsonObject &replyObject)
0012 {
0013     mTeamId = replyObject.value(QStringLiteral("teamId")).toString();
0014     mMainTeam = replyObject.value(QStringLiteral("teamMain")).toBool(false);
0015     mAutoJoin = replyObject.value(QStringLiteral("teamDefault")).toBool(false);
0016     mRoomsCount = replyObject.value(QStringLiteral("roomsCount")).toInt(0);
0017     //    if (isValid()) {
0018     //        // TODO add specific debug category ?
0019     //        qDebug() << " END team info " << *this;
0020     //    }
0021 }
0022 
0023 void TeamInfo::serialize(const TeamInfo &teams, QJsonObject &obj)
0024 {
0025     if (teams.isValid()) {
0026         obj[QLatin1String("teamId")] = teams.teamId();
0027         obj[QLatin1String("teamMain")] = teams.mainTeam();
0028         if (teams.autoJoin()) {
0029             obj[QLatin1String("teamDefault")] = true;
0030         }
0031         const auto roomsCount = teams.roomsCount();
0032         if (roomsCount > 0) {
0033             obj[QLatin1String("roomsCount")] = roomsCount;
0034         }
0035     }
0036 }
0037 
0038 TeamInfo TeamInfo::deserialize(const QJsonObject &obj)
0039 {
0040     TeamInfo info;
0041     info.setMainTeam(obj[QLatin1String("teamMain")].toBool());
0042     info.setTeamId(obj[QLatin1String("teamId")].toString());
0043     info.setAutoJoin(obj[QLatin1String("teamDefault")].toBool());
0044     info.setRoomsCount(obj[QLatin1String("roomsCount")].toInt(0));
0045     return info;
0046 }
0047 
0048 bool TeamInfo::isValid() const
0049 {
0050     return !mTeamId.isEmpty();
0051 }
0052 
0053 bool TeamInfo::operator==(const TeamInfo &other) const
0054 {
0055     return mTeamId == other.teamId() && mMainTeam == other.mainTeam() && mAutoJoin == other.autoJoin() && mRoomsCount == other.roomsCount();
0056 }
0057 
0058 bool TeamInfo::operator!=(const TeamInfo &other) const
0059 {
0060     return !operator==(other);
0061 }
0062 
0063 bool TeamInfo::autoJoin() const
0064 {
0065     return mAutoJoin;
0066 }
0067 
0068 void TeamInfo::setAutoJoin(bool autoJoin)
0069 {
0070     mAutoJoin = autoJoin;
0071 }
0072 
0073 int TeamInfo::roomsCount() const
0074 {
0075     return mRoomsCount;
0076 }
0077 
0078 void TeamInfo::setRoomsCount(int newRoomsCount)
0079 {
0080     mRoomsCount = newRoomsCount;
0081 }
0082 
0083 QString TeamInfo::teamId() const
0084 {
0085     return mTeamId;
0086 }
0087 
0088 void TeamInfo::setTeamId(const QString &teamId)
0089 {
0090     mTeamId = teamId;
0091 }
0092 
0093 bool TeamInfo::mainTeam() const
0094 {
0095     return mMainTeam;
0096 }
0097 
0098 void TeamInfo::setMainTeam(bool mainTeam)
0099 {
0100     mMainTeam = mainTeam;
0101 }
0102 
0103 bool TeamInfo::hasTeamRoom() const
0104 {
0105     return !mMainTeam && !mTeamId.isEmpty();
0106 }
0107 
0108 QDebug operator<<(QDebug d, const TeamInfo &t)
0109 {
0110     d << "team id: " << t.teamId();
0111     d << "is Main Team: " << t.mainTeam();
0112     d << "autojoin: " << t.autoJoin();
0113     d << "roomCount: " << t.roomsCount();
0114     return d;
0115 }