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

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "roominfo/roominfo.h"
0008 
0009 #include "ruqola_debug.h"
0010 #include "utils.h"
0011 #include <KLocalizedString>
0012 #include <QJsonArray>
0013 #include <QLocale>
0014 
0015 RoomInfo::RoomInfo() = default;
0016 
0017 RoomInfo::~RoomInfo() = default;
0018 
0019 void RoomInfo::parseRoomInfo(const QJsonObject &object)
0020 {
0021     // qDebug() << " void AdminRoom::parseAdminRoom(const QJsonObject &object)" << object;
0022     if (object.contains(QLatin1String("name"))) {
0023         setName(object[QLatin1String("name")].toString());
0024     }
0025     setTopic(object[QLatin1String("topic")].toString());
0026     setDescription(object[QLatin1String("description")].toString());
0027     setAnnouncement(object[QLatin1String("announcement")].toString());
0028     if (object.contains(QLatin1String("msgs"))) {
0029         setMessageCount(object[QLatin1String("msgs")].toInt());
0030     }
0031     if (object.contains(QLatin1String("t"))) {
0032         setChannelType(object[QLatin1String("t")].toString());
0033     }
0034     setBelongsTo(object[QLatin1String("belongsTo")].toString());
0035     setDefaultRoom(object[QLatin1String("default")].toBool(false));
0036     if (object.contains(QLatin1String("lastMessage"))) {
0037         setLastMessage(Utils::parseIsoDate(QStringLiteral("_updatedAt"), object[QLatin1String("lastMessage")].toObject()));
0038     }
0039     if (object.contains(QLatin1String("ts"))) {
0040         setCreatedRoom(Utils::parseIsoDate(QStringLiteral("ts"), object));
0041     }
0042     setIdentifier(object[QLatin1String("_id")].toString());
0043     setReadOnly(object[QLatin1String("ro")].toBool());
0044 
0045     setFeatured(object[QLatin1String("featured")].toBool(false));
0046     if (object.contains(QLatin1String("usersCount"))) {
0047         setUsersCount(object[QLatin1String("usersCount")].toInt());
0048     }
0049     const QJsonArray userNamesArray = object[QLatin1String("usernames")].toArray();
0050     const QVariantList userNamesList = userNamesArray.toVariantList();
0051     mUserNames.reserve(userNamesList.count());
0052     for (const auto &var : userNamesList) {
0053         mUserNames.append(var.toString());
0054     }
0055     mTeamInfo.parseTeamInfo(object);
0056     generateDisplayChannelType();
0057 
0058     const QJsonValue ownerValue = object.value(QLatin1String("u"));
0059     if (!ownerValue.isUndefined()) {
0060         const QJsonObject objOwner = ownerValue.toObject();
0061         mOwnerName = objOwner.value(QLatin1String("username")).toString();
0062     }
0063 
0064     // TODO load team info
0065     // qDebug() << " * this " << *this;
0066     // Add users "u"
0067     // TODO
0068 }
0069 
0070 bool RoomInfo::defaultRoom() const
0071 {
0072     return mDefaultRoom;
0073 }
0074 
0075 void RoomInfo::setDefaultRoom(bool defaultRoom)
0076 {
0077     mDefaultRoom = defaultRoom;
0078 }
0079 
0080 int RoomInfo::usersCount() const
0081 {
0082     return mUsersCount;
0083 }
0084 
0085 void RoomInfo::setUsersCount(int usersCount)
0086 {
0087     mUsersCount = usersCount;
0088 }
0089 
0090 int RoomInfo::messageCount() const
0091 {
0092     return mMessageCount;
0093 }
0094 
0095 void RoomInfo::setMessageCount(int messageCount)
0096 {
0097     mMessageCount = messageCount;
0098 }
0099 
0100 QString RoomInfo::channelType() const
0101 {
0102     return mChannelType;
0103 }
0104 
0105 static QString convertChannelType(const QString &str, bool mainTeam)
0106 {
0107     if (str == QLatin1Char('p')) {
0108         if (mainTeam) {
0109             return i18n("Private Team");
0110         } else {
0111             return i18n("Group");
0112         }
0113     } else if (str == QLatin1Char('c')) {
0114         if (mainTeam) {
0115             return i18n("Public Team");
0116         } else {
0117             return i18n("Channel");
0118         }
0119     } else if (str == QLatin1Char('d')) {
0120         return i18n("Direct");
0121     } else if (str == QLatin1Char('l')) {
0122         return i18n("Omnichannel");
0123     } else {
0124         qCWarning(RUQOLA_LOG) << " Unkwnon channel type " << str;
0125         return str;
0126     }
0127 }
0128 
0129 bool RoomInfo::isTeam() const
0130 {
0131     return mTeamInfo.mainTeam();
0132 }
0133 
0134 void RoomInfo::generateDisplayChannelType()
0135 {
0136     mChannelTypeStr = convertChannelType(mChannelType, isTeam());
0137 }
0138 
0139 QString RoomInfo::belongsTo() const
0140 {
0141     return mBelongsTo;
0142 }
0143 
0144 void RoomInfo::setBelongsTo(const QString &newBelongsTo)
0145 {
0146     mBelongsTo = newBelongsTo;
0147 }
0148 
0149 const QString &RoomInfo::ownerName() const
0150 {
0151     return mOwnerName;
0152 }
0153 
0154 void RoomInfo::setOwnerName(const QString &newOwnerName)
0155 {
0156     mOwnerName = newOwnerName;
0157 }
0158 
0159 const QString &RoomInfo::announcement() const
0160 {
0161     return mAnnouncement;
0162 }
0163 
0164 void RoomInfo::setAnnouncement(const QString &newAnnouncement)
0165 {
0166     mAnnouncement = newAnnouncement;
0167 }
0168 
0169 const QString &RoomInfo::description() const
0170 {
0171     return mDescription;
0172 }
0173 
0174 void RoomInfo::setDescription(const QString &newDescription)
0175 {
0176     mDescription = newDescription;
0177 }
0178 
0179 bool RoomInfo::featured() const
0180 {
0181     return mFeatured;
0182 }
0183 
0184 void RoomInfo::setFeatured(bool newFeatured)
0185 {
0186     mFeatured = newFeatured;
0187 }
0188 
0189 qint64 RoomInfo::createdRoom() const
0190 {
0191     return mCreatedRoom;
0192 }
0193 
0194 void RoomInfo::setCreatedRoom(qint64 newCreatedRoom)
0195 {
0196     mCreatedRoom = newCreatedRoom;
0197     if (mCreatedRoom != -1) {
0198         QLocale l;
0199         mCreatedRoomDisplayTime = l.toString(QDateTime::fromMSecsSinceEpoch(mCreatedRoom), QLocale::LongFormat);
0200     }
0201 }
0202 
0203 QString RoomInfo::createdRoomDisplayDateTimeStr() const
0204 {
0205     return mCreatedRoomDisplayTime;
0206 }
0207 
0208 qint64 RoomInfo::lastMessage() const
0209 {
0210     return mLastMessage;
0211 }
0212 
0213 QString RoomInfo::lastMessageDisplayDateTimeStr() const
0214 {
0215     return mLastMessageDisplayTime;
0216 }
0217 
0218 void RoomInfo::setLastMessage(qint64 newLastMessage)
0219 {
0220     mLastMessage = newLastMessage;
0221     if (mLastMessage != -1) {
0222         QLocale l;
0223         mLastMessageDisplayTime = l.toString(QDateTime::fromMSecsSinceEpoch(mLastMessage), QLocale::LongFormat);
0224     }
0225 }
0226 
0227 void RoomInfo::setChannelType(const QString &channelType)
0228 {
0229     mChannelType = channelType;
0230 }
0231 
0232 QString RoomInfo::identifier() const
0233 {
0234     return mIdentifier;
0235 }
0236 
0237 void RoomInfo::setIdentifier(const QString &identifier)
0238 {
0239     mIdentifier = identifier;
0240 }
0241 
0242 QString RoomInfo::topic() const
0243 {
0244     return mTopic;
0245 }
0246 
0247 void RoomInfo::setTopic(const QString &topic)
0248 {
0249     mTopic = topic;
0250 }
0251 
0252 bool RoomInfo::readOnly() const
0253 {
0254     return mReadOnly;
0255 }
0256 
0257 void RoomInfo::setReadOnly(bool readOnly)
0258 {
0259     mReadOnly = readOnly;
0260 }
0261 
0262 QString RoomInfo::roomName() const
0263 {
0264     if (mName.isEmpty()) {
0265         return mUserNames.join(QStringLiteral(" x "));
0266     }
0267     return mName;
0268 }
0269 
0270 const TeamInfo &RoomInfo::teamInfo() const
0271 {
0272     return mTeamInfo;
0273 }
0274 
0275 void RoomInfo::setTeamInfo(const TeamInfo &newTeamInfo)
0276 {
0277     mTeamInfo = newTeamInfo;
0278 }
0279 
0280 QString RoomInfo::name() const
0281 {
0282     return mName;
0283 }
0284 
0285 void RoomInfo::setName(const QString &name)
0286 {
0287     mName = name;
0288 }
0289 
0290 QStringList RoomInfo::userNames() const
0291 {
0292     return mUserNames;
0293 }
0294 
0295 void RoomInfo::setUserNames(const QStringList &userNames)
0296 {
0297     mUserNames = userNames;
0298 }
0299 
0300 QStringList RoomInfo::users() const
0301 {
0302     return mUsers;
0303 }
0304 
0305 void RoomInfo::setUsers(const QStringList &users)
0306 {
0307     mUsers = users;
0308 }
0309 
0310 QString RoomInfo::channelTypeStr() const
0311 {
0312     return mChannelTypeStr;
0313 }
0314 
0315 bool RoomInfo::operator==(const RoomInfo &other) const
0316 {
0317     return mDefaultRoom == other.defaultRoom() && mUsersCount == other.usersCount() && mMessageCount == other.messageCount()
0318         && mChannelType == other.channelType() && mIdentifier == other.identifier() && mTopic == other.topic() && mName == other.name()
0319         && mUserNames == other.userNames() && mUsers == other.users() && mTeamInfo == other.teamInfo() && mLastMessage == other.lastMessage()
0320         && mCreatedRoom == other.createdRoom() && mDescription == other.description() && mAnnouncement == other.announcement()
0321         && mOwnerName == other.ownerName();
0322 }
0323 
0324 QDebug operator<<(QDebug d, const RoomInfo &t)
0325 {
0326     d << " default Room : " << t.defaultRoom();
0327     d << " user count : " << t.usersCount();
0328     d << " message count : " << t.messageCount();
0329     d << " channel type: " << t.channelType();
0330     d << " identifier: " << t.identifier();
0331     d << " topic: " << t.topic();
0332     d << " name: " << t.name();
0333     d << " usernames: " << t.userNames();
0334     d << " users: " << t.users();
0335     d << " teaminfo: " << t.teamInfo();
0336     d << " lastMessage : " << t.lastMessage();
0337     d << " created : " << t.createdRoom();
0338     d << " description : " << t.description();
0339     d << " announcement : " << t.announcement();
0340     d << " OwnerName : " << t.ownerName();
0341     return d;
0342 }