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 "customuserstatustreewidget.h"
0008 #include "administratorcustomuserstatuscreatedialog.h"
0009 #include "rocketchataccount.h"
0010 #include "utils.h"
0011 #include <KLocalizedString>
0012 #include <KMessageBox>
0013 #include <QHeaderView>
0014 #include <QMenu>
0015 #include <QPointer>
0016 
0017 CustomUserStatusTreeWidgetItem::CustomUserStatusTreeWidgetItem(QTreeWidget *parent)
0018     : QTreeWidgetItem(parent)
0019 {
0020 }
0021 
0022 CustomUserStatusTreeWidgetItem::~CustomUserStatusTreeWidgetItem() = default;
0023 
0024 CustomUserStatus CustomUserStatusTreeWidgetItem::userStatus() const
0025 {
0026     return mUserStatus;
0027 }
0028 
0029 void CustomUserStatusTreeWidgetItem::setUserStatus(const CustomUserStatus &userStatus)
0030 {
0031     mUserStatus = userStatus;
0032 }
0033 
0034 CustomUserStatusTreeWidget::CustomUserStatusTreeWidget(RocketChatAccount *account, QWidget *parent)
0035     : QTreeWidget(parent)
0036     , mRocketChatAccount(account)
0037 {
0038     setColumnCount(2);
0039     setHeaderLabels({i18n("Name"), i18n("Presence")});
0040     setAlternatingRowColors(true);
0041     header()->setSectionsMovable(false);
0042     header()->setSectionResizeMode(QHeaderView::ResizeToContents);
0043     setSelectionMode(SingleSelection);
0044     setContextMenuPolicy(Qt::CustomContextMenu);
0045     setRootIsDecorated(false);
0046     connect(this, &CustomUserStatusTreeWidget::customContextMenuRequested, this, &CustomUserStatusTreeWidget::slotCustomContextMenuRequested);
0047     connect(mRocketChatAccount, &RocketChatAccount::customUserStatusChanged, this, &CustomUserStatusTreeWidget::initialize);
0048     connect(this, &CustomUserStatusTreeWidget::itemDoubleClicked, this, &CustomUserStatusTreeWidget::editClicked);
0049 }
0050 
0051 CustomUserStatusTreeWidget::~CustomUserStatusTreeWidget() = default;
0052 
0053 void CustomUserStatusTreeWidget::initialize()
0054 {
0055     clear();
0056     const CustomUserStatuses statuses = mRocketChatAccount->customUserStatuses();
0057     const QVector<CustomUserStatus> customUserses = statuses.customUserses();
0058     for (const CustomUserStatus &status : customUserses) {
0059         auto item = new CustomUserStatusTreeWidgetItem(this);
0060         item->setUserStatus(status);
0061         item->setText(CustomUserStatusTreeWidget::Name, status.name());
0062         item->setText(CustomUserStatusTreeWidget::Presence, Utils::presenceStatusToString(status.statusType()));
0063     }
0064     setSortingEnabled(true);
0065     header()->setSortIndicatorShown(true);
0066     header()->setSectionsClickable(true);
0067     sortByColumn(0, Qt::AscendingOrder);
0068 }
0069 
0070 void CustomUserStatusTreeWidget::addClicked()
0071 {
0072     QPointer<AdministratorCustomUserStatusCreateDialog> dlg = new AdministratorCustomUserStatusCreateDialog(this);
0073     if (dlg->exec()) {
0074         const AdministratorCustomUserStatusCreateWidget::UserStatusInfo info = dlg->userStatusInfo();
0075         RocketChatRestApi::CustomUserStatusCreateJob::StatusCreateInfo statusCreateInfo;
0076         statusCreateInfo.name = info.name;
0077         statusCreateInfo.statusType = Utils::presenceStatusToString(info.statusType);
0078         mRocketChatAccount->createCustomUserStatus(statusCreateInfo);
0079     }
0080     delete dlg;
0081 }
0082 
0083 void CustomUserStatusTreeWidget::editClicked()
0084 {
0085     if (!currentItem()) {
0086         return;
0087     }
0088 
0089     QPointer<AdministratorCustomUserStatusCreateDialog> dlg = new AdministratorCustomUserStatusCreateDialog(this);
0090     auto customUserStatusItem = static_cast<CustomUserStatusTreeWidgetItem *>(currentItem());
0091     const CustomUserStatus userStatus = customUserStatusItem->userStatus();
0092     AdministratorCustomUserStatusCreateWidget::UserStatusInfo userStatusinfo;
0093     userStatusinfo.name = userStatus.name();
0094     userStatusinfo.statusType = userStatus.statusType();
0095     dlg->setUserStatusInfo(userStatusinfo);
0096     if (dlg->exec()) {
0097         const AdministratorCustomUserStatusCreateWidget::UserStatusInfo info = dlg->userStatusInfo();
0098         RocketChatRestApi::CustomUserStatusUpdateJob::StatusUpdateInfo statusUpdateInfo;
0099         statusUpdateInfo.name = info.name;
0100         statusUpdateInfo.statusType = Utils::presenceStatusToString(info.statusType);
0101         statusUpdateInfo.identifier = userStatus.identifier();
0102         mRocketChatAccount->updateCustomUserStatus(statusUpdateInfo);
0103     }
0104     delete dlg;
0105 }
0106 
0107 void CustomUserStatusTreeWidget::removeClicked()
0108 {
0109     if (!currentItem()) {
0110         return;
0111     }
0112     auto customUserStatusItem = static_cast<CustomUserStatusTreeWidgetItem *>(currentItem());
0113     const CustomUserStatus userStatus = customUserStatusItem->userStatus();
0114     if (KMessageBox::ButtonCode::PrimaryAction
0115         == KMessageBox::questionTwoActions(this,
0116                                            i18n("Do you want to remove \"%1\"?", userStatus.name()),
0117                                            i18nc("@title", "Remove Custom User Status"),
0118                                            KStandardGuiItem::remove(),
0119                                            KStandardGuiItem::cancel())) {
0120         mRocketChatAccount->removeCustomUserStatus(userStatus.identifier());
0121     }
0122 }
0123 
0124 void CustomUserStatusTreeWidget::slotCustomContextMenuRequested(const QPoint &pos)
0125 {
0126     QMenu menu(this);
0127     menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, &CustomUserStatusTreeWidget::addClicked);
0128     QTreeWidgetItem *item = itemAt(pos);
0129     if (item) {
0130         menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Modify..."), this, &CustomUserStatusTreeWidget::editClicked);
0131         menu.addSeparator();
0132         menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove"), this, &CustomUserStatusTreeWidget::removeClicked);
0133     }
0134     menu.exec(viewport()->mapToGlobal(pos));
0135 }
0136 
0137 #include "moc_customuserstatustreewidget.cpp"