File indexing completed on 2024-12-22 04:45:36

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 "administratorinviteswidget.h"
0008 #include "administratorinvitesfilterproxymodel.h"
0009 #include "connection.h"
0010 #include "invite/inviteinfo.h"
0011 #include "invite/listinvitejob.h"
0012 #include "invite/removeinvitejob.h"
0013 #include "invitetreeview.h"
0014 #include "model/admininvitemodel.h"
0015 #include "model/searchtreebasefilterproxymodel.h"
0016 #include "rocketchataccount.h"
0017 #include "ruqolawidgets_debug.h"
0018 #include <KLocalizedString>
0019 #include <QLineEdit>
0020 #include <QVBoxLayout>
0021 
0022 AdministratorInvitesWidget::AdministratorInvitesWidget(RocketChatAccount *account, QWidget *parent)
0023     : QWidget(parent)
0024     , mInviteTreeView(new InviteTreeView(this))
0025     , mSearchLineWidget(new QLineEdit(this))
0026     , mAdminInviteModel(new AdminInviteModel(this))
0027     , mRocketChatAccount(account)
0028 {
0029     mAdminInviteFilterProxyModel = new AdministratorInvitesFilterProxyModel(mAdminInviteModel, this);
0030     mAdminInviteFilterProxyModel->setObjectName(QStringLiteral("mAdminInviteFilterProxyModel"));
0031 
0032     auto mainLayout = new QVBoxLayout(this);
0033     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0034     mainLayout->setContentsMargins({});
0035     mainLayout->setSpacing(0);
0036 
0037     mSearchLineWidget->setObjectName(QStringLiteral("mSearchLineWidget"));
0038     mainLayout->addWidget(mSearchLineWidget);
0039     mSearchLineWidget->setPlaceholderText(i18n("Search invite..."));
0040 
0041     mInviteTreeView->setObjectName(QStringLiteral("mInviteTreeWidget"));
0042     mainLayout->addWidget(mInviteTreeView);
0043     mInviteTreeView->setModel(mAdminInviteFilterProxyModel);
0044     connect(mInviteTreeView, &InviteTreeView::removeInvite, this, &AdministratorInvitesWidget::slotRemoveInvite);
0045     connect(mSearchLineWidget, &QLineEdit::textChanged, this, &AdministratorInvitesWidget::slotTextChanged);
0046 
0047     // Hide not useful columns
0048     mInviteTreeView->setColumnHidden(AdminInviteModel::AdminInviteRoles::UserIdentifier, true);
0049     mInviteTreeView->setColumnHidden(AdminInviteModel::AdminInviteRoles::RoomId, true);
0050     mInviteTreeView->setColumnHidden(AdminInviteModel::AdminInviteRoles::CreateAt, true);
0051 }
0052 
0053 AdministratorInvitesWidget::~AdministratorInvitesWidget() = default;
0054 
0055 void AdministratorInvitesWidget::slotTextChanged(const QString &str)
0056 {
0057     mAdminInviteFilterProxyModel->setFilterString(str);
0058 }
0059 
0060 void AdministratorInvitesWidget::initialize()
0061 {
0062     auto inviteJob = new RocketChatRestApi::ListInviteJob(this);
0063     mRocketChatAccount->restApi()->initializeRestApiJob(inviteJob);
0064     connect(inviteJob, &RocketChatRestApi::ListInviteJob::listInviteDone, this, &AdministratorInvitesWidget::slotListInviteDone);
0065     if (!inviteJob->start()) {
0066         qCDebug(RUQOLAWIDGETS_LOG) << "Impossible to start ListInviteJob";
0067     }
0068 }
0069 
0070 void AdministratorInvitesWidget::slotListInviteDone(const QJsonDocument &obj)
0071 {
0072     QVector<InviteInfo> lstInvite;
0073     const QJsonArray array = obj.array();
0074     const auto arrayCount{array.count()};
0075     lstInvite.reserve(arrayCount);
0076     for (auto i = 0; i < arrayCount; ++i) {
0077         const QJsonObject o = array.at(i).toObject();
0078         InviteInfo invite;
0079         invite.parseInviteInfo(o);
0080         lstInvite.append(std::move(invite));
0081     }
0082     mAdminInviteModel->setAdminInvites(lstInvite);
0083     // qDebug() << " lstInvite " << lstInvite;
0084     // qDebug() << " obj " << obj;
0085     for (int i : {AdminInviteModel::AdminInviteRoles::CreateAtStr, AdminInviteModel::AdminInviteRoles::Identifier}) {
0086         mInviteTreeView->resizeColumnToContents(i);
0087     }
0088 }
0089 
0090 void AdministratorInvitesWidget::slotRemoveInvite(const QString &identifier)
0091 {
0092     auto removeInviteJob = new RocketChatRestApi::RemoveInviteJob(this);
0093     removeInviteJob->setIdentifier(identifier);
0094     mRocketChatAccount->restApi()->initializeRestApiJob(removeInviteJob);
0095     connect(removeInviteJob, &RocketChatRestApi::RemoveInviteJob::removeInviteDone, this, &AdministratorInvitesWidget::slotRemoveInviteDone);
0096     if (!removeInviteJob->start()) {
0097         qCDebug(RUQOLAWIDGETS_LOG) << "Impossible to start RemoveInviteJob";
0098     }
0099 }
0100 
0101 void AdministratorInvitesWidget::slotRemoveInviteDone(const QString &identifier)
0102 {
0103     mAdminInviteModel->removeInvite(identifier);
0104 }
0105 
0106 #include "moc_administratorinviteswidget.cpp"