File indexing completed on 2024-06-09 04:58:49

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "administratoroauthwidget.h"
0007 #include "administratoroauthfilterproxymodel.h"
0008 #include "connection.h"
0009 #include "ddpapi/ddpclient.h"
0010 #include "misc/listoauthappsjob.h"
0011 #include "model/adminoauthmodel.h"
0012 #include "oauth/oauthinfo.h"
0013 #include "oauthtreeview.h"
0014 #include "rocketchataccount.h"
0015 #include "ruqolawidgets_debug.h"
0016 #include <KLineEditEventHandler>
0017 #include <KLocalizedString>
0018 #include <QLineEdit>
0019 #include <QVBoxLayout>
0020 
0021 AdministratorOauthWidget::AdministratorOauthWidget(RocketChatAccount *account, QWidget *parent)
0022     : QWidget{parent}
0023     , mRocketChatAccount(account)
0024     , mSearchLineWidget(new QLineEdit(this))
0025     , mOauthTreeWidget(new OauthTreeView(mRocketChatAccount, this))
0026     , mAdminOauthModel(new AdminOauthModel(this))
0027 {
0028     auto mainLayout = new QVBoxLayout(this);
0029     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0030     mainLayout->setContentsMargins({});
0031     mainLayout->setSpacing(0);
0032 
0033     mSearchLineWidget->setObjectName(QStringLiteral("mSearchLineWidget"));
0034     mainLayout->addWidget(mSearchLineWidget);
0035     mSearchLineWidget->setPlaceholderText(i18n("Search Oauth apps..."));
0036     KLineEditEventHandler::catchReturnKey(mSearchLineWidget);
0037     mOauthTreeWidget->setObjectName(QStringLiteral("mOauthTreeWidget"));
0038     mainLayout->addWidget(mOauthTreeWidget);
0039     mAdminInviteFilterProxyModel = new AdministratorOauthFilterProxyModel(mAdminOauthModel, this);
0040     mAdminInviteFilterProxyModel->setObjectName(QStringLiteral("mAdminInviteFilterProxyModel"));
0041     mOauthTreeWidget->setModel(mAdminInviteFilterProxyModel);
0042     connect(mSearchLineWidget, &QLineEdit::textChanged, this, &AdministratorOauthWidget::slotTextChanged);
0043     connect(mOauthTreeWidget, &OauthTreeView::removeOauth, this, &AdministratorOauthWidget::slotRemoveOauth);
0044     connect(mOauthTreeWidget, &OauthTreeView::oauthAdded, this, &AdministratorOauthWidget::slotOauthAppAdded);
0045 
0046     // Hide not useful columns
0047     mOauthTreeWidget->setColumnHidden(AdminOauthModel::AdminOauthRoles::ClientId, true);
0048     mOauthTreeWidget->setColumnHidden(AdminOauthModel::AdminOauthRoles::ClientSecret, true);
0049     mOauthTreeWidget->setColumnHidden(AdminOauthModel::AdminOauthRoles::RedirectUri, true);
0050     mOauthTreeWidget->setColumnHidden(AdminOauthModel::AdminOauthRoles::CreatedAt, true);
0051     mOauthTreeWidget->setColumnHidden(AdminOauthModel::AdminOauthRoles::Identifier, true);
0052 
0053     connect(mRocketChatAccount, &RocketChatAccount::oauthAppAdded, this, &AdministratorOauthWidget::slotOauthAppAdded);
0054     connect(mRocketChatAccount, &RocketChatAccount::oauthAppUpdated, this, &AdministratorOauthWidget::slotOauthAppUpdated);
0055 }
0056 
0057 AdministratorOauthWidget::~AdministratorOauthWidget() = default;
0058 
0059 void AdministratorOauthWidget::initialize()
0060 {
0061     auto oauthListJob = new RocketChatRestApi::ListOauthAppsJob(this);
0062     mRocketChatAccount->restApi()->initializeRestApiJob(oauthListJob);
0063     connect(oauthListJob, &RocketChatRestApi::ListOauthAppsJob::listOauthDone, this, &AdministratorOauthWidget::slotListOauthDone);
0064     if (!oauthListJob->start()) {
0065         qCDebug(RUQOLAWIDGETS_LOG) << "Impossible to start ListInviteJob";
0066     }
0067 }
0068 
0069 void AdministratorOauthWidget::slotOauthAppAdded(const QJsonObject &obj)
0070 {
0071     OauthInfo info;
0072     info.parseOauthInfo(std::move(obj), false); // We got it from ddpclient
0073     mAdminOauthModel->addMoreOauth(info);
0074 }
0075 
0076 void AdministratorOauthWidget::slotOauthAppUpdated(const QJsonObject &obj)
0077 {
0078     OauthInfo info;
0079     info.parseOauthInfo(std::move(obj), false); // We got it from ddpclient
0080     // TODO mAdminOauthModel->addMoreOauth(info);
0081 }
0082 
0083 void AdministratorOauthWidget::slotListOauthDone(const QJsonObject &obj)
0084 {
0085     QVector<OauthInfo> lstOauth;
0086     const QJsonArray array = obj[QLatin1String("oauthApps")].toArray();
0087     const auto arrayCount{array.count()};
0088     lstOauth.reserve(arrayCount);
0089     for (auto i = 0; i < arrayCount; ++i) {
0090         const QJsonObject o = array.at(i).toObject();
0091         OauthInfo info;
0092         info.parseOauthInfo(o);
0093         lstOauth.append(std::move(info));
0094     }
0095     mAdminOauthModel->setAdminOauth(lstOauth);
0096     // qDebug() << " lstOauth " << lstOauth;
0097     // qDebug() << " obj " << obj;
0098     for (int i : {AdminOauthModel::AdminOauthRoles::Identifier, AdminOauthModel::AdminOauthRoles::CreatedAtStr}) {
0099         mOauthTreeWidget->resizeColumnToContents(i);
0100     }
0101 }
0102 
0103 void AdministratorOauthWidget::slotTextChanged(const QString &str)
0104 {
0105     mAdminInviteFilterProxyModel->setFilterString(str);
0106 }
0107 
0108 void AdministratorOauthWidget::slotRemoveOauth(const QString &identifier)
0109 {
0110     mRocketChatAccount->ddp()->deleteOAuthApp(identifier);
0111     // TODO make sure that identifier removed
0112     mAdminOauthModel->removeOauth(identifier);
0113 }
0114 
0115 #include "moc_administratoroauthwidget.cpp"