File indexing completed on 2024-12-01 04:37:03

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "usersinroomwidget.h"
0008 #include "dialogs/directchannelinfodialog.h"
0009 #include "model/usersforroomfilterproxymodel.h"
0010 #include "model/usersforroommodel.h"
0011 #include "rocketchataccount.h"
0012 #include "usersinroomcombobox.h"
0013 #include "usersinroommenu.h"
0014 #include <KLineEditEventHandler>
0015 #include <KLocalizedString>
0016 #include <QLabel>
0017 #include <QLineEdit>
0018 #include <QListView>
0019 #include <QVBoxLayout>
0020 
0021 UsersInRoomWidget::UsersInRoomWidget(RocketChatAccount *account, QWidget *parent)
0022     : QWidget(parent)
0023     , mListView(new QListView(this))
0024     , mSearchLineEdit(new QLineEdit(this))
0025     , mMessageListInfo(new QLabel(this))
0026     , mUsersForRoomFilterProxy(new UsersForRoomFilterProxyModel(this))
0027     , mUsersInRoomComboBox(new UsersInRoomComboBox(this))
0028     , mRocketChatAccount(account)
0029 {
0030     auto mainLayout = new QVBoxLayout(this);
0031     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0032     mainLayout->setContentsMargins({});
0033 
0034     auto hMainLayout = new QHBoxLayout;
0035     hMainLayout->setObjectName(QStringLiteral("hMainLayout"));
0036     hMainLayout->setContentsMargins({});
0037     mainLayout->addLayout(hMainLayout);
0038 
0039     mSearchLineEdit->setObjectName(QStringLiteral("mSearchLineEdit"));
0040     mSearchLineEdit->setPlaceholderText(i18n("Search users..."));
0041     mSearchLineEdit->setClearButtonEnabled(true);
0042     KLineEditEventHandler::catchReturnKey(mSearchLineEdit);
0043     connect(mSearchLineEdit, &QLineEdit::textChanged, this, &UsersInRoomWidget::slotTextChanged);
0044     hMainLayout->addWidget(mSearchLineEdit);
0045     mUsersInRoomComboBox->setObjectName(QStringLiteral("mUsersInRoomComboBox"));
0046     hMainLayout->addWidget(mUsersInRoomComboBox);
0047     connect(mUsersInRoomComboBox, &UsersInRoomComboBox::currentIndexChanged, this, &UsersInRoomWidget::slotChangeStatusType);
0048 
0049     mMessageListInfo->setObjectName(QStringLiteral("mMessageListInfo"));
0050     mMessageListInfo->setTextFormat(Qt::RichText);
0051     mMessageListInfo->setContextMenuPolicy(Qt::NoContextMenu);
0052     QFont labFont = mMessageListInfo->font();
0053     labFont.setBold(true);
0054     mMessageListInfo->setFont(labFont);
0055     connect(mMessageListInfo, &QLabel::linkActivated, this, &UsersInRoomWidget::loadMoreUsers);
0056     mainLayout->addWidget(mMessageListInfo);
0057 
0058     mListView->setObjectName(QStringLiteral("mListView"));
0059     mainLayout->addWidget(mListView);
0060     mListView->setContextMenuPolicy(Qt::CustomContextMenu);
0061     connect(mListView, &QListView::customContextMenuRequested, this, &UsersInRoomWidget::slotCustomContextMenuRequested);
0062     connect(mListView, &QListView::doubleClicked, this, &UsersInRoomWidget::slotShowUserInfo);
0063     mListView->setModel(mUsersForRoomFilterProxy);
0064     connect(mUsersForRoomFilterProxy, &UsersForRoomFilterProxyModel::hasFullListChanged, this, &UsersInRoomWidget::updateLabel);
0065     connect(mUsersForRoomFilterProxy, &UsersForRoomFilterProxyModel::loadingInProgressChanged, this, &UsersInRoomWidget::updateLabel);
0066 }
0067 
0068 UsersInRoomWidget::~UsersInRoomWidget()
0069 {
0070     mUsersForRoomFilterProxy->clearFilter();
0071 }
0072 
0073 void UsersInRoomWidget::slotChangeStatusType(int index)
0074 {
0075     mUsersForRoomFilterProxy->setStatusType(mUsersInRoomComboBox->itemData(index).toString());
0076 }
0077 
0078 void UsersInRoomWidget::slotTextChanged(const QString &str)
0079 {
0080     mUsersForRoomFilterProxy->setFilterString(str);
0081 }
0082 
0083 void UsersInRoomWidget::setRoom(Room *room)
0084 {
0085     mRoom = room;
0086     if (mRoom) {
0087         auto model = mRocketChatAccount->usersModelForRoom(mRoom->roomId());
0088         auto sourceModel = mUsersForRoomFilterProxy->sourceModel();
0089         if (sourceModel) {
0090             auto usersForRoomModel = qobject_cast<UsersForRoomModel *>(mUsersForRoomFilterProxy->sourceModel());
0091             disconnect(usersForRoomModel, &UsersForRoomModel::hasFullListChanged, mUsersForRoomFilterProxy, &UsersForRoomFilterProxyModel::hasFullListChanged);
0092             disconnect(usersForRoomModel,
0093                        &UsersForRoomModel::loadingInProgressChanged,
0094                        mUsersForRoomFilterProxy,
0095                        &UsersForRoomFilterProxyModel::loadingInProgressChanged);
0096         }
0097 
0098         connect(model, &UsersForRoomModel::hasFullListChanged, mUsersForRoomFilterProxy, &UsersForRoomFilterProxyModel::hasFullListChanged);
0099         connect(model, &UsersForRoomModel::loadingInProgressChanged, mUsersForRoomFilterProxy, &UsersForRoomFilterProxyModel::loadingInProgressChanged);
0100         mUsersForRoomFilterProxy->setSourceModel(model);
0101         updateLabel();
0102     }
0103 }
0104 
0105 void UsersInRoomWidget::slotCustomContextMenuRequested(const QPoint &pos)
0106 {
0107     if (!mMenu) {
0108         mMenu = new UsersInRoomMenu(this);
0109     }
0110     const QModelIndex index = mListView->indexAt(pos);
0111     if (index.isValid()) {
0112         const QString userId = index.data(UsersForRoomModel::UsersForRoomRoles::UserId).toString();
0113         const QString userName = index.data(UsersForRoomModel::UsersForRoomRoles::UserName).toString();
0114         mMenu->setUserId(userId);
0115         mMenu->setUserName(userName);
0116     }
0117     mMenu->setParentWidget(this);
0118     mMenu->setRoom(mRoom);
0119     mMenu->slotCustomContextMenuRequested(pos);
0120 }
0121 
0122 void UsersInRoomWidget::slotShowUserInfo(const QModelIndex &index)
0123 {
0124     if (index.isValid()) {
0125         const QString userName = index.data(UsersForRoomModel::UsersForRoomRoles::UserName).toString();
0126         DirectChannelInfoDialog dlg(mRocketChatAccount, this);
0127         dlg.setUserName(userName);
0128         dlg.exec();
0129     }
0130 }
0131 
0132 void UsersInRoomWidget::updateLabel()
0133 {
0134     if (mUsersForRoomFilterProxy->loadMoreUsersInProgress()) {
0135         mMessageListInfo->setText(i18n("Loading..."));
0136     } else {
0137         mMessageListInfo->setText(mUsersForRoomFilterProxy->numberOfUsers() == 0 ? i18n("No Message found") : displayShowMessageInRoom());
0138     }
0139 }
0140 
0141 QString UsersInRoomWidget::displayShowMessageInRoom() const
0142 {
0143     QString displayMessageStr =
0144         i18np("%1 User in room (Total: %2)", "%1 Users in room (Total: %2)", mUsersForRoomFilterProxy->numberOfUsers(), mUsersForRoomFilterProxy->total());
0145     if (!mUsersForRoomFilterProxy->hasFullList()) {
0146         displayMessageStr += QStringLiteral(" <a href=\"loadmoreelement\">%1</a>").arg(i18n("(Click here for Loading more...)"));
0147     }
0148     return displayMessageStr;
0149 }
0150 
0151 void UsersInRoomWidget::loadMoreUsers()
0152 {
0153     mRocketChatAccount->loadMoreUsersInRoom(mRoom->roomId(), mRoom->channelType());
0154 }
0155 
0156 #include "moc_usersinroomwidget.cpp"