File indexing completed on 2024-05-19 05:04:04
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 "directchannelinfowidget.h" 0008 #include "connection.h" 0009 #include "rocketchataccount.h" 0010 #include "ruqolawidgets_debug.h" 0011 #include "user.h" 0012 #include "users/userinfojob.h" 0013 0014 #include <KLocalizedString> 0015 #include <QFormLayout> 0016 #include <QIcon> 0017 #include <QLabel> 0018 #include <QScreen> 0019 0020 DirectChannelInfoWidget::DirectChannelInfoWidget(RocketChatAccount *account, QWidget *parent) 0021 : QWidget(parent) 0022 , mAvatar(new QLabel(this)) 0023 , mMainLayout(new QFormLayout(this)) 0024 , mRocketChatAccount(account) 0025 { 0026 mMainLayout->setObjectName(QStringLiteral("mainLayout")); 0027 mMainLayout->setContentsMargins({}); 0028 0029 mAvatar->setObjectName(QStringLiteral("mAvatar")); 0030 mMainLayout->addWidget(mAvatar); 0031 } 0032 0033 DirectChannelInfoWidget::~DirectChannelInfoWidget() = default; 0034 0035 void DirectChannelInfoWidget::setUserName(const QString &userName) 0036 { 0037 fetchUserInfo(userName); 0038 } 0039 0040 void DirectChannelInfoWidget::setRoles(const QVector<RoleInfo> &newRoles) 0041 { 0042 mListRoleInfos = newRoles; 0043 } 0044 0045 void DirectChannelInfoWidget::fetchUserInfo(const QString &userName) 0046 { 0047 auto userJob = new RocketChatRestApi::UserInfoJob(this); 0048 mRocketChatAccount->restApi()->initializeRestApiJob(userJob); 0049 RocketChatRestApi::UserInfoJob::UserInfo info; 0050 info.userIdentifier = userName; 0051 info.userInfoType = RocketChatRestApi::UserInfoJob::UserInfoType::UserName; 0052 userJob->setUserInfo(info); 0053 connect(userJob, &RocketChatRestApi::UserInfoJob::userInfoDone, this, &DirectChannelInfoWidget::slotUserInfoDone); 0054 if (!userJob->start()) { 0055 qCDebug(RUQOLAWIDGETS_LOG) << "Impossible to start UserInfoJob"; 0056 } 0057 } 0058 0059 void DirectChannelInfoWidget::slotUserInfoDone(const QJsonObject &obj) 0060 { 0061 const QJsonObject userJson = obj.value(QStringLiteral("user")).toObject(); 0062 User user; 0063 user.parseUserRestApi(userJson, mListRoleInfos); 0064 if (user.isValid()) { 0065 setUser(user); 0066 } else { 0067 qCDebug(RUQOLAWIDGETS_LOG) << "Invalid user parsing" << obj; 0068 } 0069 } 0070 0071 void DirectChannelInfoWidget::setUser(const User &user) 0072 { 0073 // Download avatar ? 0074 Utils::AvatarInfo info; 0075 info.avatarType = Utils::AvatarType::User; 0076 info.identifier = user.userName(); 0077 const QUrl iconUrlStr = QUrl(mRocketChatAccount->avatarUrl(info)); 0078 if (!iconUrlStr.isEmpty()) { 0079 const QSize pixmapAvatarSize = QSize(80, 80) * screen()->devicePixelRatio(); 0080 mAvatar->setPixmap(QIcon(iconUrlStr.toLocalFile()).pixmap(pixmapAvatarSize)); 0081 } 0082 0083 const QString name = user.name(); 0084 if (!name.isEmpty()) { 0085 auto nameLabel = new QLabel(this); 0086 nameLabel->setText(name); 0087 nameLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0088 mMainLayout->addRow(i18n("Name:"), nameLabel); 0089 } 0090 0091 const QString userName = user.userName(); 0092 if (!userName.isEmpty()) { 0093 auto userNameLabel = new QLabel(userName, this); 0094 userNameLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0095 mMainLayout->addRow(i18n("Username:"), userNameLabel); 0096 } 0097 0098 auto statusLabel = new QLabel(Utils::displaytextFromPresenceStatus(user.status()), this); 0099 statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0100 mMainLayout->addRow(i18n("Status:"), statusLabel); 0101 0102 const QString statusText = user.statusText(); 0103 if (!statusText.isEmpty()) { 0104 auto customStatusLabel = new QLabel(statusText, this); 0105 customStatusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0106 mMainLayout->addRow(i18n("Custom Status:"), customStatusLabel); 0107 } 0108 0109 if (!user.bio().isEmpty()) { 0110 auto bioLabel = new QLabel(user.bio(), this); 0111 bioLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0112 mMainLayout->addRow(i18n("Bio:"), bioLabel); 0113 } 0114 0115 auto timeZoneLabel = new QLabel(this); 0116 QString urlStr = QStringLiteral("UTC"); 0117 if (user.utcOffset() > 0) { 0118 urlStr += QLatin1Char('+') + QString::number(user.utcOffset()); 0119 } else if (user.utcOffset() < 0) { 0120 urlStr += QString::number(user.utcOffset()); 0121 } 0122 timeZoneLabel->setText(urlStr); 0123 timeZoneLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0124 mMainLayout->addRow(i18n("Timezone:"), timeZoneLabel); 0125 0126 const QStringList roles{user.roles()}; 0127 0128 if (!roles.isEmpty()) { 0129 auto rolesLabel = new QLabel(this); 0130 QStringList newRolesList; 0131 for (const QString &rolestr : roles) { 0132 for (const RoleInfo &roleInfo : std::as_const(mListRoleInfos)) { 0133 if (roleInfo.identifier() == rolestr) { 0134 newRolesList.append(User::roleI18n(roleInfo.name(), mListRoleInfos)); 0135 break; 0136 } 0137 } 0138 } 0139 rolesLabel->setText(newRolesList.join(QStringLiteral(", "))); 0140 rolesLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0141 mMainLayout->addRow(i18n("Roles:"), rolesLabel); 0142 } 0143 0144 const auto createdAt = user.createdAt(); 0145 if (createdAt.isValid()) { 0146 auto createAtLabel = new QLabel(createdAt.date().toString(), this); 0147 createAtLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0148 mMainLayout->addRow(i18n("Created At:"), createAtLabel); 0149 } 0150 0151 const auto lastLogin = user.lastLogin(); 0152 if (lastLogin.isValid()) { 0153 auto lastLoginLabel = new QLabel(lastLogin.toString(), this); 0154 lastLoginLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0155 mMainLayout->addRow(i18n("Last Login:"), lastLoginLabel); 0156 } 0157 0158 const auto userEmailsInfo = user.userEmailsInfo(); 0159 if (userEmailsInfo.isValid()) { 0160 const QString generateEmail = QStringLiteral("<a href=\'mailto:%1\'>%1</a>").arg(user.userEmailsInfo().email); 0161 const QString infoStr = i18n("%1 [%2]", generateEmail, user.userEmailsInfo().verified ? i18n("Verified") : i18n("Not verified")); 0162 auto emailsInfoLabel = new QLabel(infoStr, this); 0163 emailsInfoLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); 0164 emailsInfoLabel->setTextFormat(Qt::RichText); 0165 emailsInfoLabel->setOpenExternalLinks(true); 0166 mMainLayout->addRow(i18n("Email:"), emailsInfoLabel); 0167 } 0168 } 0169 0170 #include "moc_directchannelinfowidget.cpp"