File indexing completed on 2024-05-19 16:00:35

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "usersinroommenu.h"
0008 #include "dialogs/directchannelinfodialog.h"
0009 #include "rocketchataccount.h"
0010 #include "roomutil.h"
0011 #include "ruqola.h"
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QAction>
0015 #include <QMenu>
0016 
0017 UsersInRoomMenu::UsersInRoomMenu(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 UsersInRoomMenu::~UsersInRoomMenu() = default;
0023 
0024 void UsersInRoomMenu::slotOpenConversation()
0025 {
0026     Q_EMIT Ruqola::self()->rocketChatAccount()->openLinkRequested(RoomUtil::generateUserLink(mUserName));
0027 }
0028 
0029 void UsersInRoomMenu::slotBlockUser()
0030 {
0031     const bool userIsBlocked = mRoom->blocker();
0032     if (!userIsBlocked) {
0033         if (KMessageBox::ButtonCode::SecondaryAction
0034             == KMessageBox::questionTwoActions(mParentWidget,
0035                                                i18n("Do you want to block this user?"),
0036                                                i18nc("@title", "Block User"),
0037                                                KGuiItem(i18nc("@action:button", "Block User"), QStringLiteral("dialog-ok")),
0038                                                KStandardGuiItem::cancel())) {
0039             return;
0040         }
0041     }
0042     Ruqola::self()->rocketChatAccount()->blockUser(mRoom->roomId(), !userIsBlocked);
0043 }
0044 
0045 void UsersInRoomMenu::slotIgnoreUser()
0046 {
0047     const bool userIsIgnored = mRoom->userIsIgnored(mUserId);
0048     if (!userIsIgnored) {
0049         if (KMessageBox::ButtonCode::SecondaryAction
0050             == KMessageBox::questionTwoActions(mParentWidget,
0051                                                i18n("Do you want to ignore this user?"),
0052                                                i18nc("@title", "Ignore User"),
0053                                                KGuiItem(i18nc("@action:button", "Ignore User"), QStringLiteral("dialog-ok")),
0054                                                KStandardGuiItem::cancel())) {
0055             return;
0056         }
0057     }
0058     Ruqola::self()->rocketChatAccount()->ignoreUser(mRoom->roomId(), mUserId, !userIsIgnored);
0059 }
0060 
0061 void UsersInRoomMenu::slotRemoveFromRoom()
0062 {
0063     Ruqola::self()->rocketChatAccount()->kickUser(mRoom->roomId(), mUserId, mRoom->channelType());
0064 }
0065 
0066 void UsersInRoomMenu::slotCustomContextMenuRequested(const QPoint &pos)
0067 {
0068     const bool canManageUsersInRoom = mRoom->canChangeRoles();
0069     const bool isAdministrator = Ruqola::self()->rocketChatAccount()->ownUser().isAdministrator();
0070     auto account = Ruqola::self()->rocketChatAccount();
0071     const QString ownUserId = account->userId();
0072     const bool isAdirectChannel = mRoom->channelType() == Room::RoomType::Direct;
0073     const bool isNotMe = mUserId != ownUserId;
0074     QMenu menu(mParentWidget);
0075 
0076     if (account->hasPermission(QStringLiteral("create-d"))) {
0077         if (isNotMe && !isAdirectChannel) {
0078             auto conversationAction = new QAction(i18n("Start Conversation"), &menu);
0079             connect(conversationAction, &QAction::triggered, this, &UsersInRoomMenu::slotOpenConversation);
0080             menu.addAction(conversationAction);
0081         }
0082     }
0083     if (!menu.isEmpty()) {
0084         menu.addSeparator();
0085     }
0086     auto userInfoAction = new QAction(QIcon::fromTheme(QStringLiteral("documentinfo")), i18n("User Info"), &menu);
0087     connect(userInfoAction, &QAction::triggered, this, &UsersInRoomMenu::slotUserInfo);
0088     menu.addAction(userInfoAction);
0089     if ((isAdministrator || canManageUsersInRoom) && !isAdirectChannel) {
0090         if (!menu.isEmpty()) {
0091             menu.addSeparator();
0092         }
0093         if (isAdministrator || mRoom->hasPermission(QStringLiteral("set-owner"))) {
0094             const bool hasOwnerRole = mRoom->userHasOwnerRole(mUserId);
0095             auto removeAsOwner = new QAction(hasOwnerRole ? i18n("Remove as Owner") : i18n("Add as Owner"), &menu);
0096             connect(removeAsOwner, &QAction::triggered, this, [this, hasOwnerRole, account]() {
0097                 account->changeRoles(mRoom->roomId(),
0098                                      mUserId,
0099                                      mRoom->channelType(),
0100                                      hasOwnerRole ? RocketChatAccount::RemoveOwner : RocketChatAccount::AddOwner);
0101             });
0102 
0103             menu.addAction(removeAsOwner);
0104         }
0105 
0106         if (isAdministrator || mRoom->hasPermission(QStringLiteral("set-leader"))) {
0107             const bool hasLeaderRole = mRoom->userHasLeaderRole(mUserId);
0108             auto removeAsLeader = new QAction(hasLeaderRole ? i18n("Remove as Leader") : i18n("Add as Leader"), &menu);
0109             connect(removeAsLeader, &QAction::triggered, this, [this, hasLeaderRole, account]() {
0110                 account->changeRoles(mRoom->roomId(),
0111                                      mUserId,
0112                                      mRoom->channelType(),
0113                                      hasLeaderRole ? RocketChatAccount::RemoveLeader : RocketChatAccount::AddLeader);
0114             });
0115             menu.addAction(removeAsLeader);
0116         }
0117 
0118         if (isAdministrator || mRoom->hasPermission(QStringLiteral("set-moderator"))) {
0119             const bool hasModeratorRole = mRoom->userHasModeratorRole(mUserId);
0120             auto removeAsModerator = new QAction(hasModeratorRole ? i18n("Remove as Moderator") : i18n("Add as Moderator"), &menu);
0121             connect(removeAsModerator, &QAction::triggered, this, [this, hasModeratorRole, account]() {
0122                 account->changeRoles(mRoom->roomId(),
0123                                      mUserId,
0124                                      mRoom->channelType(),
0125                                      hasModeratorRole ? RocketChatAccount::RemoveModerator : RocketChatAccount::AddModerator);
0126             });
0127             menu.addAction(removeAsModerator);
0128         }
0129         if (isAdministrator || mRoom->hasPermission(QStringLiteral("remove-user"))) {
0130             menu.addSeparator();
0131             auto removeFromRoom = new QAction(i18n("Remove from Room"), &menu);
0132             connect(removeFromRoom, &QAction::triggered, this, &UsersInRoomMenu::slotRemoveFromRoom);
0133             menu.addAction(removeFromRoom);
0134         }
0135     }
0136     if (isNotMe) {
0137         if (isAdirectChannel) {
0138             if (!menu.isEmpty()) {
0139                 menu.addSeparator();
0140             }
0141             const bool userIsBlocked = mRoom->blocker();
0142             auto blockAction = new QAction(userIsBlocked ? i18n("Unblock User") : i18n("Block User"), &menu);
0143             connect(blockAction, &QAction::triggered, this, &UsersInRoomMenu::slotBlockUser);
0144             menu.addAction(blockAction);
0145         } else {
0146             if (!menu.isEmpty()) {
0147                 menu.addSeparator();
0148             }
0149 
0150             const bool userIsIgnored = mRoom->userIsIgnored(mUserId);
0151             auto ignoreAction = new QAction(userIsIgnored ? i18n("Unignore") : i18n("Ignore"), &menu);
0152             connect(ignoreAction, &QAction::triggered, this, &UsersInRoomMenu::slotIgnoreUser);
0153             menu.addAction(ignoreAction);
0154         }
0155     }
0156     menu.exec(mParentWidget->mapToGlobal(pos));
0157 }
0158 
0159 void UsersInRoomMenu::slotUserInfo()
0160 {
0161     auto rcAccount = Ruqola::self()->rocketChatAccount();
0162     DirectChannelInfoDialog dlg(rcAccount, mParentWidget);
0163     dlg.setUserName(mUserName);
0164     dlg.setRoles(rcAccount->roleInfo());
0165     dlg.exec();
0166 }
0167 
0168 void UsersInRoomMenu::setParentWidget(QWidget *parentWidget)
0169 {
0170     mParentWidget = parentWidget;
0171 }
0172 
0173 void UsersInRoomMenu::setUserId(const QString &userId)
0174 {
0175     mUserId = userId;
0176 }
0177 
0178 void UsersInRoomMenu::setUserName(const QString &userName)
0179 {
0180     mUserName = userName;
0181 }
0182 
0183 void UsersInRoomMenu::setRoom(Room *room)
0184 {
0185     mRoom = room;
0186 }
0187 
0188 #include "moc_usersinroommenu.cpp"