File indexing completed on 2024-05-12 16:27:24

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "myaccountmanagedeviceconfigurewidget.h"
0008 #include "connection.h"
0009 #include "misc/searchwithdelaylineedit.h"
0010 #include "model/deviceinfomodel.h"
0011 #include "model/searchtreebasefilterproxymodel.h"
0012 #include "rocketchataccount.h"
0013 #include "ruqolawidgets_debug.h"
0014 #include "sessions/sessionslistjob.h"
0015 #include "sessions/sessionslogoutmejob.h"
0016 #include <KLocalizedString>
0017 #include <KMessageBox>
0018 #include <QLabel>
0019 #include <QMenu>
0020 #include <QPointer>
0021 #include <QTreeView>
0022 
0023 MyAccountManageDeviceConfigureWidget::MyAccountManageDeviceConfigureWidget(RocketChatAccount *account, QWidget *parent)
0024     : SearchTreeBaseWidget(account, parent)
0025 {
0026     mModel = new DeviceInfoModel(this);
0027     mModel->setObjectName(QStringLiteral("mModel"));
0028     mSearchLineEdit->setPlaceholderText(i18n("Search device..."));
0029 
0030     mProxyModelModel = new SearchTreeBaseFilterProxyModel(mModel, this);
0031     mProxyModelModel->setObjectName(QStringLiteral("mProxyModelModel"));
0032     mTreeView->setModel(mProxyModelModel);
0033     hideColumns();
0034     connectModel();
0035 }
0036 
0037 MyAccountManageDeviceConfigureWidget::~MyAccountManageDeviceConfigureWidget() = default;
0038 
0039 void MyAccountManageDeviceConfigureWidget::updateLabel()
0040 {
0041     mLabelResultSearch->setText(mModel->total() == 0 ? i18n("No device found") : displayShowMessage());
0042 }
0043 
0044 QString MyAccountManageDeviceConfigureWidget::displayShowMessage() const
0045 {
0046     QString displayMessageStr = i18np("%1 device (Total: %2)", "%1 devices (Total: %2)", mModel->rowCount(), mModel->total());
0047     if (!mModel->hasFullList()) {
0048         displayMessageStr += clickableStr();
0049     }
0050     return displayMessageStr;
0051 }
0052 
0053 void MyAccountManageDeviceConfigureWidget::slotLoadElements(int offset, int count, const QString &searchName)
0054 {
0055     auto job = new RocketChatRestApi::SessionsListJob(this);
0056 
0057     RocketChatRestApi::QueryParameters parameters;
0058     //    QMap<QString, RocketChatRestApi::QueryParameters::SortOrder> map;
0059     //    map.insert(QStringLiteral("name"), RocketChatRestApi::QueryParameters::SortOrder::Ascendant);
0060     //    parameters.setSorting(map);
0061     if (offset != -1) {
0062         parameters.setOffset(offset);
0063     }
0064     if (count != -1) {
0065         parameters.setCount(count);
0066     }
0067     if (!searchName.isEmpty()) {
0068         parameters.setFilter(searchName);
0069     }
0070 
0071     job->setQueryParameters(parameters);
0072 
0073     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0074     if (offset != -1) {
0075         connect(job, &RocketChatRestApi::SessionsListJob::sessionsListDone, this, &MyAccountManageDeviceConfigureWidget::slotLoadMoreElementDone);
0076     } else {
0077         connect(job, &RocketChatRestApi::SessionsListJob::sessionsListDone, this, &MyAccountManageDeviceConfigureWidget::slotSearchDone);
0078     }
0079     if (!job->start()) {
0080         qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start SessionsListJob job";
0081     }
0082 }
0083 
0084 void MyAccountManageDeviceConfigureWidget::slotDeviceRemoved(const QString &emojiId)
0085 {
0086     mModel->removeElement(emojiId);
0087 }
0088 
0089 void MyAccountManageDeviceConfigureWidget::slotCustomContextMenuRequested(const QPoint &pos)
0090 {
0091     const QModelIndex index = mTreeView->indexAt(pos);
0092     if (index.isValid()) {
0093         QMenu menu(this);
0094         const QModelIndex newModelIndex = mProxyModelModel->mapToSource(index);
0095         menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Disconnect"), this, [this, newModelIndex]() {
0096             const QModelIndex modelIndex = mModel->index(newModelIndex.row(), DeviceInfoModel::Identifier);
0097             slotDisconnectDevice(modelIndex);
0098         });
0099         menu.exec(mTreeView->viewport()->mapToGlobal(pos));
0100     }
0101 }
0102 
0103 void MyAccountManageDeviceConfigureWidget::slotDisconnectDevice(const QModelIndex &index)
0104 {
0105     auto job = new RocketChatRestApi::SessionsLogoutMeJob(this);
0106     const QModelIndex modelIndex = mModel->index(index.row(), DeviceInfoModel::SessionId);
0107     const QString sessionsId = modelIndex.data().toString();
0108     job->setSessionId(sessionsId);
0109     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0110     connect(job, &RocketChatRestApi::SessionsLogoutMeJob::logoutMeDone, this, [this, sessionsId]() {
0111         slotDeviceRemoved(sessionsId);
0112     });
0113     if (!job->start()) {
0114         qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start SessionsLogoutMeJob job";
0115     }
0116 }
0117 
0118 #include "moc_myaccountmanagedeviceconfigurewidget.cpp"