File indexing completed on 2024-04-28 16:11:03

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "channelusercompleter.h"
0008 #include <QJsonObject>
0009 
0010 ChannelUserCompleter::ChannelUserCompleter() = default;
0011 
0012 ChannelUserCompleter::~ChannelUserCompleter() = default;
0013 
0014 QDebug operator<<(QDebug d, const ChannelUserCompleter &t)
0015 {
0016     d.space() << "Type" << t.type();
0017     d.space() << "Name" << t.name();
0018     d.space() << "description" << t.description();
0019     d.space() << "userName" << t.userName();
0020     d.space() << "AvatarTag" << t.avatarTag();
0021     d.space() << "outsideRoom" << t.outsideRoom();
0022     d.space() << "identifie " << t.identifier();
0023     d.space() << "avatarinfo" << t.avatarInfo();
0024     return d;
0025 }
0026 
0027 QString ChannelUserCompleter::completerName() const
0028 {
0029     switch (mType) {
0030     case ChannelUserCompleterType::DirectChannel:
0031         return userName();
0032     case ChannelUserCompleterType::Room:
0033     case ChannelUserCompleterType::Notification:
0034         return name();
0035     case ChannelUserCompleterType::Unknown:
0036         return {};
0037     }
0038 
0039     return {};
0040 }
0041 
0042 QString ChannelUserCompleter::identifier() const
0043 {
0044     return mIdentifier;
0045 }
0046 
0047 void ChannelUserCompleter::setIdentifier(const QString &newIdentifier)
0048 {
0049     mIdentifier = newIdentifier;
0050 }
0051 
0052 Utils::AvatarInfo ChannelUserCompleter::avatarInfo() const
0053 {
0054     return mAvatarInfo;
0055 }
0056 
0057 void ChannelUserCompleter::parseChannel(const QJsonObject &object, ChannelUserCompleterType type)
0058 {
0059     // qDebug() << " object " << object;
0060     mType = type;
0061     mName = object.value(QLatin1String("name")).toString();
0062     mIdentifier = object.value(QLatin1String("_id")).toString();
0063     if (mType == ChannelUserCompleterType::DirectChannel) {
0064         mAvatarTag = object.value(QLatin1String("avatarETag")).toString();
0065         mUserName = object.value(QLatin1String("username")).toString();
0066         mStatusIcon = QIcon::fromTheme(Utils::iconFromStatus(object.value(QLatin1String("status")).toString()));
0067     } else {
0068         const QString roomType = object.value(QLatin1String("t")).toString();
0069         if (roomType == QLatin1Char('c')) {
0070             setChannelIcon();
0071         } else if (roomType == QLatin1Char('p')) {
0072             mStatusIcon = QIcon::fromTheme(QStringLiteral("lock"));
0073         }
0074     }
0075     mOutsideRoom = object.value(QLatin1String("outside")).toBool();
0076     createAvatarInfo();
0077 }
0078 
0079 void ChannelUserCompleter::setChannelIcon()
0080 {
0081     mStatusIcon = QIcon::fromTheme(QStringLiteral("irc-channel-inactive"));
0082 }
0083 
0084 void ChannelUserCompleter::createAvatarInfo()
0085 {
0086     mAvatarInfo.avatarType = (mType == ChannelUserCompleter::ChannelUserCompleterType::Room ? Utils::AvatarType::Room : Utils::AvatarType::User);
0087     mAvatarInfo.etag = mAvatarTag;
0088     mAvatarInfo.identifier = (mType == ChannelUserCompleter::ChannelUserCompleterType::Room ? mIdentifier : mUserName);
0089 }
0090 
0091 void ChannelUserCompleter::setAvatarInfo(const Utils::AvatarInfo &newAvatarInfo)
0092 {
0093     mAvatarInfo = newAvatarInfo;
0094 }
0095 
0096 bool ChannelUserCompleter::operator==(const ChannelUserCompleter &other) const
0097 {
0098     return (mType == other.type()) && (mName == other.name())
0099         && (mDescription == other.description() && (mUserName == other.userName()) && (mAvatarTag == other.avatarTag()) && (mOutsideRoom == other.outsideRoom())
0100             && (mIdentifier == other.identifier()));
0101 }
0102 
0103 QString ChannelUserCompleter::description() const
0104 {
0105     return mDescription;
0106 }
0107 
0108 void ChannelUserCompleter::setDescription(const QString &newDescription)
0109 {
0110     mDescription = newDescription;
0111 }
0112 
0113 QString ChannelUserCompleter::name() const
0114 {
0115     return mName;
0116 }
0117 
0118 void ChannelUserCompleter::setName(const QString &newName)
0119 {
0120     mName = newName;
0121 }
0122 
0123 ChannelUserCompleter::ChannelUserCompleterType ChannelUserCompleter::type() const
0124 {
0125     return mType;
0126 }
0127 
0128 void ChannelUserCompleter::setType(ChannelUserCompleterType newType)
0129 {
0130     mType = newType;
0131 }
0132 
0133 QIcon ChannelUserCompleter::statusIcon() const
0134 {
0135     return mStatusIcon;
0136 }
0137 
0138 QString ChannelUserCompleter::userName() const
0139 {
0140     return mUserName;
0141 }
0142 
0143 void ChannelUserCompleter::setUserName(const QString &newUserName)
0144 {
0145     mUserName = newUserName;
0146 }
0147 
0148 QString ChannelUserCompleter::avatarTag() const
0149 {
0150     return mAvatarTag;
0151 }
0152 
0153 void ChannelUserCompleter::setAvatarTag(const QString &newAvatarTag)
0154 {
0155     mAvatarTag = newAvatarTag;
0156 }
0157 
0158 bool ChannelUserCompleter::outsideRoom() const
0159 {
0160     return mOutsideRoom;
0161 }
0162 
0163 void ChannelUserCompleter::setOutsideRoom(bool newOutsideRoom)
0164 {
0165     mOutsideRoom = newOutsideRoom;
0166 }
0167 
0168 #include "moc_channelusercompleter.cpp"