File indexing completed on 2024-05-26 05:05:27

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 "administratorcustomsoundswidget.h"
0008 #include "administratorcustomsoundscreatedialog.h"
0009 #include "connection.h"
0010 #include "custom/customsoundslistjob.h"
0011 #include "misc/searchwithdelaylineedit.h"
0012 #include "model/admincustomsoundmodel.h"
0013 #include "model/searchtreebasefilterproxymodel.h"
0014 #include "rocketchataccount.h"
0015 #include "ruqolawidgets_debug.h"
0016 #include <KLocalizedString>
0017 #include <KMessageBox>
0018 #include <QLabel>
0019 #include <QMenu>
0020 #include <QPointer>
0021 #include <QTreeView>
0022 
0023 AdministratorCustomSoundsWidget::AdministratorCustomSoundsWidget(RocketChatAccount *account, QWidget *parent)
0024     : SearchTreeBaseWidget(account, parent)
0025 {
0026     mModel = new AdminCustomSoundModel(this);
0027     mModel->setObjectName(QStringLiteral("mAdminCustomSoundModel"));
0028     mSearchLineEdit->setPlaceholderText(i18n("Search Custom Sounds"));
0029 
0030     mProxyModelModel = new SearchTreeBaseFilterProxyModel(mModel, this);
0031     mProxyModelModel->setObjectName(QStringLiteral("mCustomSoundProxyModel"));
0032     mTreeView->setModel(mProxyModelModel);
0033     hideColumns();
0034     connectModel();
0035     connect(mTreeView, &QTreeView::doubleClicked, this, &AdministratorCustomSoundsWidget::slotModifyCustomSound);
0036     connect(mRocketChatAccount, &RocketChatAccount::customSoundRemoved, this, &AdministratorCustomSoundsWidget::slotCustomSoundRemoved);
0037     connect(mRocketChatAccount, &RocketChatAccount::customSoundAdded, this, &AdministratorCustomSoundsWidget::slotCustomSoundAdded);
0038     connect(mRocketChatAccount, &RocketChatAccount::customSoundUpdated, this, &AdministratorCustomSoundsWidget::slotCustomSoundUpdated);
0039 }
0040 
0041 AdministratorCustomSoundsWidget::~AdministratorCustomSoundsWidget() = default;
0042 
0043 void AdministratorCustomSoundsWidget::slotCustomSoundAdded()
0044 {
0045     // TODO
0046 }
0047 
0048 void AdministratorCustomSoundsWidget::slotCustomSoundUpdated()
0049 {
0050     // TODO
0051 }
0052 
0053 void AdministratorCustomSoundsWidget::slotCustomSoundRemoved(const QString &identifier)
0054 {
0055     mModel->removeElement(identifier);
0056 }
0057 
0058 void AdministratorCustomSoundsWidget::updateLabel()
0059 {
0060     mLabelResultSearch->setText(mModel->total() == 0 ? i18n("No custom sound found") : displayShowMessage());
0061 }
0062 
0063 QString AdministratorCustomSoundsWidget::displayShowMessage() const
0064 {
0065     QString displayMessageStr = i18np("%1 custom sound (Total: %2)", "%1 custom sounds (Total: %2)", mModel->rowCount(), mModel->total());
0066     if (!mModel->hasFullList()) {
0067         displayMessageStr += clickableStr();
0068     }
0069     return displayMessageStr;
0070 }
0071 
0072 void AdministratorCustomSoundsWidget::slotLoadElements(int offset, int count, const QString &searchName)
0073 {
0074     auto job = new RocketChatRestApi::CustomSoundsListJob(this);
0075     // https://<url>/api/v1/custom-sounds.list?query={"name":{"$regex":"d","$options":"i"}}&sort={"name":1}&count=25
0076     RocketChatRestApi::QueryParameters parameters;
0077     QMap<QString, RocketChatRestApi::QueryParameters::SortOrder> map;
0078     map.insert(QStringLiteral("name"), RocketChatRestApi::QueryParameters::SortOrder::Ascendant);
0079     parameters.setSorting(map);
0080     if (offset != -1) {
0081         parameters.setOffset(offset);
0082     }
0083     if (count != -1) {
0084         parameters.setCount(count);
0085     }
0086     parameters.setSearchString(searchName);
0087     job->setQueryParameters(parameters);
0088 
0089     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0090     if (offset != -1) {
0091         connect(job, &RocketChatRestApi::CustomSoundsListJob::customSoundsListDone, this, &AdministratorCustomSoundsWidget::slotLoadMoreElementDone);
0092     } else {
0093         connect(job, &RocketChatRestApi::CustomSoundsListJob::customSoundsListDone, this, &AdministratorCustomSoundsWidget::slotSearchDone);
0094     }
0095     if (!job->start()) {
0096         qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start CustomSoundsListJob job";
0097     }
0098 }
0099 
0100 void AdministratorCustomSoundsWidget::slotAddCustomSound()
0101 {
0102     // Comment for the moment. there is not restapi yet.
0103     return;
0104     QPointer<AdministratorCustomSoundsCreateDialog> dlg = new AdministratorCustomSoundsCreateDialog(this);
0105     if (dlg->exec()) {
0106         // TODO
0107     }
0108     delete dlg;
0109 }
0110 
0111 void AdministratorCustomSoundsWidget::slotModifyCustomSound(const QModelIndex &index)
0112 {
0113     // Comment for the moment. there is not restapi yet.
0114     return;
0115     QPointer<AdministratorCustomSoundsCreateDialog> dlg = new AdministratorCustomSoundsCreateDialog(this);
0116     // TODO add option
0117     if (dlg->exec()) {
0118         // TODO
0119     }
0120     delete dlg;
0121 }
0122 
0123 void AdministratorCustomSoundsWidget::slotRemoveCustomSound(const QModelIndex &index)
0124 {
0125     if (KMessageBox::warningTwoActions(this,
0126                                        i18n("Do you want to remove this sound?"),
0127                                        i18nc("@title", "Remove Custom Sound"),
0128                                        KStandardGuiItem::remove(),
0129                                        KStandardGuiItem::cancel())
0130         == KMessageBox::PrimaryAction) {
0131         const QModelIndex modelIndex = mModel->index(index.row(), AdminCustomSoundModel::Identifier);
0132         const QString soundIdentifier = modelIndex.data().toString();
0133         mRocketChatAccount->ddp()->deleteCustomSound(soundIdentifier);
0134     }
0135 }
0136 
0137 void AdministratorCustomSoundsWidget::slotCustomContextMenuRequested(const QPoint &pos)
0138 {
0139     QMenu menu(this);
0140     const QModelIndex index = mTreeView->indexAt(pos);
0141     menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, &AdministratorCustomSoundsWidget::slotAddCustomSound);
0142     if (index.isValid()) {
0143         menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Modify..."), this, [this, index]() {
0144             slotModifyCustomSound(index);
0145         });
0146         menu.addSeparator();
0147         menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove"), this, [this, index]() {
0148             slotRemoveCustomSound(index);
0149         });
0150     }
0151     menu.exec(mTreeView->viewport()->mapToGlobal(pos));
0152 }
0153 
0154 #include "moc_administratorcustomsoundswidget.cpp"