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

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 "ownuser/ownuser.h"
0008 #include "utils.h"
0009 #include <QJsonArray>
0010 
0011 OwnUser::OwnUser() = default;
0012 
0013 OwnUser::~OwnUser() = default;
0014 
0015 void OwnUser::parseOwnUserInfo(const QJsonObject &replyObject)
0016 {
0017     // qDebug() << "replyObject "<<replyObject;
0018     mUserId = replyObject.value(QLatin1String("_id")).toString();
0019     mUserName = replyObject.value(QLatin1String("username")).toString();
0020     mStatus = replyObject.value(QLatin1String("status")).toString();
0021     mEmail = replyObject.value(QLatin1String("email")).toString();
0022     mStatusText = replyObject.value(QLatin1String("statusText")).toString();
0023     mName = replyObject.value(QLatin1String("name")).toString();
0024     mAvatarUrl = replyObject.value(QLatin1String("avatarUrl")).toString();
0025     mUtcOffset = replyObject.value(QLatin1String("utcOffset")).toDouble();
0026     mStatusDefault = replyObject.value(QLatin1String("statusDefault")).toString();
0027     mNickName = replyObject.value(QLatin1String("nickname")).toString();
0028     const QJsonArray array = replyObject.value(QLatin1String("roles")).toArray();
0029     const QVariantList rolesLst = array.toVariantList();
0030     mRoles.clear();
0031     mRoles.reserve(rolesLst.count());
0032     for (const auto &role : rolesLst) {
0033         mRoles.append(role.toString());
0034     }
0035     mServicePassword.parseService(replyObject.value(QLatin1String("services")).toObject());
0036     mOwnUserPreferences.parsePreferences(replyObject.value(QLatin1String("settings")).toObject().value(QLatin1String("preferences")).toObject());
0037 }
0038 
0039 bool OwnUser::isAdministrator() const
0040 {
0041     return mRoles.contains(QStringLiteral("admin"));
0042 }
0043 
0044 ServicePassword OwnUser::servicePassword() const
0045 {
0046     return mServicePassword;
0047 }
0048 
0049 void OwnUser::setServicePassword(const ServicePassword &servicePassword)
0050 {
0051     mServicePassword = servicePassword;
0052 }
0053 
0054 OwnUserPreferences OwnUser::ownUserPreferences() const
0055 {
0056     return mOwnUserPreferences;
0057 }
0058 
0059 void OwnUser::setOwnUserPreferences(const OwnUserPreferences &ownUserPreferences)
0060 {
0061     mOwnUserPreferences = ownUserPreferences;
0062 }
0063 
0064 QString OwnUser::userId() const
0065 {
0066     return mUserId;
0067 }
0068 
0069 void OwnUser::setUserId(const QString &userId)
0070 {
0071     mUserId = userId;
0072 }
0073 
0074 QString OwnUser::userName() const
0075 {
0076     return mUserName;
0077 }
0078 
0079 void OwnUser::setUserName(const QString &userName)
0080 {
0081     mUserName = userName;
0082 }
0083 
0084 QString OwnUser::status() const
0085 {
0086     return mStatus;
0087 }
0088 
0089 void OwnUser::setStatus(const QString &status)
0090 {
0091     mStatus = status;
0092 }
0093 
0094 QDebug operator<<(QDebug d, const OwnUser &t)
0095 {
0096     d << "UserId " << t.userId();
0097     d << "Status " << t.status();
0098     d << "UserName " << t.userName();
0099     d << "Email " << t.email();
0100     d << "StatusText " << t.statusText();
0101     d << "Name " << t.name();
0102     d << "AvatarUrl " << t.avatarUrl();
0103     d << "utcOffset " << t.utcOffset();
0104     d << "defaultStatus " << t.statusDefault();
0105     d << "nickname " << t.nickName();
0106     d << "roles " << t.roles();
0107     d << "servicePassword " << t.servicePassword();
0108     d << "OwnUserPreferences " << t.ownUserPreferences();
0109     return d;
0110 }
0111 
0112 bool OwnUser::operator==(const OwnUser &other) const
0113 {
0114     return (mUserId == other.userId()) && (mStatus == other.status()) && (mUserName == other.userName()) && (mEmail == other.email())
0115         && (mStatusText == other.statusText()) && (mName == other.name()) && (mAvatarUrl == other.avatarUrl()) && (mUtcOffset == other.utcOffset())
0116         && (mStatusDefault == other.statusDefault()) && (mNickName == other.nickName()) && (mRoles == other.roles())
0117         && (mServicePassword == other.servicePassword()) && (mOwnUserPreferences == other.ownUserPreferences());
0118 }
0119 
0120 QString OwnUser::email() const
0121 {
0122     return mEmail;
0123 }
0124 
0125 void OwnUser::setEmail(const QString &email)
0126 {
0127     mEmail = email;
0128 }
0129 
0130 QString OwnUser::statusText() const
0131 {
0132     return mStatusText;
0133 }
0134 
0135 void OwnUser::setStatusText(const QString &statusText)
0136 {
0137     mStatusText = statusText;
0138 }
0139 
0140 QString OwnUser::name() const
0141 {
0142     return mName;
0143 }
0144 
0145 void OwnUser::setName(const QString &name)
0146 {
0147     mName = name;
0148 }
0149 
0150 QString OwnUser::avatarUrl() const
0151 {
0152     return mAvatarUrl;
0153 }
0154 
0155 void OwnUser::setAvatarUrl(const QString &avatarUrl)
0156 {
0157     mAvatarUrl = avatarUrl;
0158 }
0159 
0160 double OwnUser::utcOffset() const
0161 {
0162     return mUtcOffset;
0163 }
0164 
0165 void OwnUser::setUtcOffset(double utcOffset)
0166 {
0167     mUtcOffset = utcOffset;
0168 }
0169 
0170 QString OwnUser::statusDefault() const
0171 {
0172     return mStatusDefault;
0173 }
0174 
0175 void OwnUser::setStatusDefault(const QString &statusDefault)
0176 {
0177     mStatusDefault = statusDefault;
0178 }
0179 
0180 User OwnUser::user() const
0181 {
0182     User user;
0183     user.setUserId(mUserId);
0184     user.setUserName(mUserName);
0185     user.setStatus(Utils::presenceStatusFromString(mStatus));
0186     return user;
0187 }
0188 
0189 QString OwnUser::nickName() const
0190 {
0191     return mNickName;
0192 }
0193 
0194 void OwnUser::setNickName(const QString &nickName)
0195 {
0196     mNickName = nickName;
0197 }
0198 
0199 QStringList OwnUser::roles() const
0200 {
0201     return mRoles;
0202 }
0203 
0204 void OwnUser::setRoles(const QStringList &roles)
0205 {
0206     mRoles = roles;
0207 }