File indexing completed on 2024-12-01 04:37:03
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 "usersinroomflowwidget.h" 0008 #include "common/flowlayout.h" 0009 #include "model/usersforroomfilterproxymodel.h" 0010 #include "model/usersforroommodel.h" 0011 #include "rocketchataccount.h" 0012 #include "room.h" 0013 #include "usersinroomdialog.h" 0014 #include "usersinroomlabel.h" 0015 #include <KLocalizedString> 0016 #include <QLabel> 0017 #define MAX_NUMBER_USER 120 0018 UsersInRoomFlowWidget::UsersInRoomFlowWidget(QWidget *parent) 0019 : QWidget(parent) 0020 , mFlowLayout(new FlowLayout(this)) 0021 , mUsersForRoomFilterProxyModel(new UsersForRoomFilterProxyModel(this)) 0022 { 0023 mFlowLayout->setObjectName(QStringLiteral("mFlowLayout")); 0024 mFlowLayout->setSpacing(0); 0025 mFlowLayout->setContentsMargins({}); 0026 connect(mUsersForRoomFilterProxyModel, &UsersForRoomFilterProxyModel::rowsInserted, this, &UsersInRoomFlowWidget::generateListUsersWidget); 0027 connect(mUsersForRoomFilterProxyModel, &UsersForRoomFilterProxyModel::rowsRemoved, this, &UsersInRoomFlowWidget::generateListUsersWidget); 0028 connect(mUsersForRoomFilterProxyModel, &UsersForRoomFilterProxyModel::dataChanged, this, &UsersInRoomFlowWidget::updateListUsersWidget); 0029 connect(mUsersForRoomFilterProxyModel, &UsersForRoomFilterProxyModel::modelReset, this, &UsersInRoomFlowWidget::generateListUsersWidget); 0030 connect(mUsersForRoomFilterProxyModel, &UsersForRoomFilterProxyModel::hasFullListChanged, this, &UsersInRoomFlowWidget::generateListUsersWidget); 0031 } 0032 0033 UsersInRoomFlowWidget::~UsersInRoomFlowWidget() = default; 0034 0035 void UsersInRoomFlowWidget::setRoom(Room *room) 0036 { 0037 mRoom = room; 0038 if (mRoom) { 0039 auto sourceModel = mUsersForRoomFilterProxyModel->sourceModel(); 0040 if (sourceModel) { 0041 auto usersForRoomModel = qobject_cast<UsersForRoomModel *>(mUsersForRoomFilterProxyModel->sourceModel()); 0042 disconnect(usersForRoomModel, 0043 &UsersForRoomModel::hasFullListChanged, 0044 mUsersForRoomFilterProxyModel, 0045 &UsersForRoomFilterProxyModel::hasFullListChanged); 0046 disconnect(usersForRoomModel, 0047 &UsersForRoomModel::loadingInProgressChanged, 0048 mUsersForRoomFilterProxyModel, 0049 &UsersForRoomFilterProxyModel::loadingInProgressChanged); 0050 } 0051 UsersForRoomModel *model = mCurrentRocketChatAccount->usersModelForRoom(mRoom->roomId()); 0052 mUsersForRoomFilterProxyModel->setSourceModel(model); 0053 0054 connect(model, &UsersForRoomModel::hasFullListChanged, mUsersForRoomFilterProxyModel, &UsersForRoomFilterProxyModel::hasFullListChanged); 0055 connect(model, &UsersForRoomModel::loadingInProgressChanged, mUsersForRoomFilterProxyModel, &UsersForRoomFilterProxyModel::loadingInProgressChanged); 0056 // generateListUsersWidget(); 0057 } else { 0058 mFlowLayout->clearAndDeleteWidgets(); 0059 } 0060 } 0061 0062 void UsersInRoomFlowWidget::setCurrentRocketChatAccount(RocketChatAccount *account) 0063 { 0064 mCurrentRocketChatAccount = account; 0065 } 0066 0067 void UsersInRoomFlowWidget::showEvent(QShowEvent *event) 0068 { 0069 generateListUsersWidget(); 0070 QWidget::showEvent(event); 0071 } 0072 0073 void UsersInRoomFlowWidget::updateListUsersWidget(const QModelIndex &topLeft, const QModelIndex &bottomRight) 0074 { 0075 if (isVisible()) { 0076 for (int row = topLeft.row(), total = bottomRight.row(); row <= total; ++row) { 0077 const QModelIndex userModelIndex = topLeft.sibling(row, 0); 0078 const QString userId = userModelIndex.data(UsersForRoomModel::UsersForRoomRoles::UserId).toString(); 0079 0080 UsersInRoomLabel *userLabel = mListUsersWidget.value(userId); 0081 if (userLabel) { 0082 const QString userDisplayName = userModelIndex.data(UsersForRoomModel::UsersForRoomRoles::DisplayName).toString(); 0083 const QString iconStatus = userModelIndex.data(UsersForRoomModel::UsersForRoomRoles::IconStatus).toString(); 0084 const QString userName = userModelIndex.data(UsersForRoomModel::UsersForRoomRoles::UserName).toString(); 0085 // qDebug() << " updating userdId " << userId << " userName " << userName << " info.iconStatus " << iconStatus; 0086 UsersInRoomLabel::UserInfo info; 0087 info.userDisplayName = userDisplayName; 0088 info.iconStatus = iconStatus; 0089 info.userId = userId; 0090 info.userName = userName; 0091 userLabel->setUserInfo(info); 0092 } 0093 } 0094 } 0095 } 0096 0097 void UsersInRoomFlowWidget::generateListUsersWidget() 0098 { 0099 if (isVisible()) { 0100 const auto count = mUsersForRoomFilterProxyModel->rowCount(); 0101 mFlowLayout->clearAndDeleteWidgets(); 0102 mListUsersWidget.clear(); 0103 int numberOfUsers = 0; 0104 for (; numberOfUsers < count && numberOfUsers < MAX_NUMBER_USER; ++numberOfUsers) { 0105 const auto userModelIndex = mUsersForRoomFilterProxyModel->index(numberOfUsers, 0); 0106 const QString userDisplayName = userModelIndex.data(UsersForRoomModel::UsersForRoomRoles::DisplayName).toString(); 0107 const QString iconStatus = userModelIndex.data(UsersForRoomModel::UsersForRoomRoles::IconStatus).toString(); 0108 const QString userId = userModelIndex.data(UsersForRoomModel::UsersForRoomRoles::UserId).toString(); 0109 const QString userName = userModelIndex.data(UsersForRoomModel::UsersForRoomRoles::UserName).toString(); 0110 UsersInRoomLabel::UserInfo info; 0111 info.userDisplayName = userDisplayName; 0112 info.iconStatus = iconStatus; 0113 info.userId = userId; 0114 info.userName = userName; 0115 auto userLabel = new UsersInRoomLabel(this); 0116 userLabel->setUserInfo(info); 0117 userLabel->setRoom(mRoom); 0118 mFlowLayout->addWidget(userLabel); 0119 mListUsersWidget.insert(userId, userLabel); 0120 } 0121 if (count > 0) { 0122 if (numberOfUsers >= MAX_NUMBER_USER) { 0123 auto openExternalDialogLabel = new QLabel(QStringLiteral("<a href=\"openexternaldialog\">%1</a>").arg(i18n("(Open External Dialog...)")), this); 0124 openExternalDialogLabel->setTextFormat(Qt::RichText); 0125 openExternalDialogLabel->setContextMenuPolicy(Qt::NoContextMenu); 0126 connect(openExternalDialogLabel, &QLabel::linkActivated, this, &UsersInRoomFlowWidget::loadExternalDialog); 0127 mFlowLayout->addWidget(openExternalDialogLabel); 0128 } else if (!mUsersForRoomFilterProxyModel->hasFullList()) { 0129 auto loadingMoreLabel = new QLabel(QStringLiteral("<a href=\"loadmoreelement\">%1</a>").arg(i18n("(Click here for Loading more...)")), this); 0130 loadingMoreLabel->setTextFormat(Qt::RichText); 0131 loadingMoreLabel->setContextMenuPolicy(Qt::NoContextMenu); 0132 connect(loadingMoreLabel, &QLabel::linkActivated, this, &UsersInRoomFlowWidget::loadMoreUsers); 0133 mFlowLayout->addWidget(loadingMoreLabel); 0134 } 0135 } 0136 } 0137 } 0138 0139 void UsersInRoomFlowWidget::loadExternalDialog() 0140 { 0141 UsersInRoomDialog dlg(mCurrentRocketChatAccount, this); 0142 dlg.setRoom(mRoom); 0143 dlg.exec(); 0144 } 0145 0146 void UsersInRoomFlowWidget::loadMoreUsers() 0147 { 0148 mCurrentRocketChatAccount->loadMoreUsersInRoom(mRoom->roomId(), mRoom->channelType()); 0149 } 0150 0151 #include "moc_usersinroomflowwidget.cpp"