File indexing completed on 2024-07-14 09:00:22

0001 /*
0002     SPDX-FileCopyrightText: 2011 Ilia Kats <ilia-kats@gmx.net>
0003     SPDX-FileCopyrightText: 2013 Jan Grulich <jgrulich@redhat.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "advancedpermissionswidget.h"
0009 #include "ui_advancedpermissionswidget.h"
0010 
0011 #include <KAcceleratorManager>
0012 #include <KLocalizedString>
0013 #include <KUser>
0014 #include <QList>
0015 
0016 #define UserTagRole Qt::UserRole + 1
0017 
0018 class AdvancedPermissionsWidgetPrivate
0019 {
0020 public:
0021     Ui_AdvancedPermissions ui;
0022 };
0023 
0024 AdvancedPermissionsWidget::AdvancedPermissionsWidget(QWidget *parent)
0025     : QDialog(parent)
0026     , d_ptr(new AdvancedPermissionsWidgetPrivate)
0027 {
0028     Q_D(AdvancedPermissionsWidget);
0029     d->ui.setupUi(this);
0030 
0031     for (const KUser &user : KUser::allUsers()) {
0032         if (user.userId().nativeId() >= 1000 && user.loginName() != QLatin1String("nobody"))
0033             d->ui.availUsers->addTopLevelItem(constructItem(user));
0034     }
0035     setupCommon();
0036 }
0037 
0038 AdvancedPermissionsWidget::AdvancedPermissionsWidget(const QHash<QString, QString> &users, QWidget *parent)
0039     : QDialog(parent)
0040     , d_ptr(new AdvancedPermissionsWidgetPrivate)
0041 {
0042     Q_D(AdvancedPermissionsWidget);
0043     d->ui.setupUi(this);
0044 
0045     for (const KUser &user : KUser::allUsers()) {
0046         const QString name = user.loginName();
0047         if (!users.contains(name) && user.userId().nativeId() >= 1000 && user.loginName() != QLatin1String("nobody"))
0048             d->ui.availUsers->addTopLevelItem(constructItem(user));
0049         else if (users.contains(name))
0050             d->ui.currentUsers->addTopLevelItem(constructItem(user, users.value(name)));
0051     }
0052     setupCommon();
0053 }
0054 
0055 AdvancedPermissionsWidget::~AdvancedPermissionsWidget()
0056 {
0057     Q_D(AdvancedPermissionsWidget);
0058     while (QTreeWidgetItem *item = d->ui.currentUsers->takeTopLevelItem(0)) {
0059         delete item;
0060     }
0061     while (QTreeWidgetItem *item = d->ui.availUsers->takeTopLevelItem(0)) {
0062         delete item;
0063     }
0064     delete d_ptr;
0065 }
0066 
0067 void AdvancedPermissionsWidget::setupCommon()
0068 {
0069     Q_D(AdvancedPermissionsWidget);
0070     connect(d->ui.arrowLeft, &QPushButton::clicked, this, &AdvancedPermissionsWidget::leftArrowClicked);
0071     connect(d->ui.arrowRight, &QPushButton::clicked, this, &AdvancedPermissionsWidget::rightArrowClicked);
0072     d->ui.availUsers->sortByColumn(FullName, Qt::AscendingOrder);
0073     d->ui.currentUsers->sortByColumn(FullName, Qt::AscendingOrder);
0074     d->ui.availUsers->setSortingEnabled(true);
0075     d->ui.currentUsers->setSortingEnabled(true);
0076 
0077     KAcceleratorManager::manage(this);
0078 }
0079 
0080 QTreeWidgetItem *AdvancedPermissionsWidget::constructItem(const KUser &user, const QString &itemData)
0081 {
0082     QStringList data;
0083     QString name = user.property(KUser::FullName).toString();
0084     QString nametooltip;
0085     if (name.isEmpty()) {
0086         name = i18nc("@item:intable shortcut for Not Available", "N/A");
0087         nametooltip = i18nc("@info:tooltip real user name is not available", "Not Available");
0088     } else {
0089         nametooltip = name;
0090     }
0091     data << name << user.loginName();
0092     auto item = new QTreeWidgetItem(data);
0093     item->setData(LoginName, UserTagRole, itemData);
0094     item->setToolTip(FullName, nametooltip);
0095     item->setToolTip(LoginName, user.loginName());
0096     return item;
0097 }
0098 
0099 QHash<QString, QString> AdvancedPermissionsWidget::currentUsers() const
0100 {
0101     Q_D(const AdvancedPermissionsWidget);
0102     QHash<QString, QString> permissions;
0103     const int itemNumber = d->ui.currentUsers->topLevelItemCount();
0104     for (int i = 0; i < itemNumber; i++) {
0105         QTreeWidgetItem *item = d->ui.currentUsers->topLevelItem(i);
0106         QString username = item->data(LoginName, Qt::DisplayRole).toString();
0107         QString tags = item->data(LoginName, UserTagRole).toString();
0108         permissions.insert(username, tags);
0109     }
0110     return permissions;
0111 }
0112 
0113 void AdvancedPermissionsWidget::rightArrowClicked()
0114 {
0115     Q_D(AdvancedPermissionsWidget);
0116     for (QTreeWidgetItem *item : d->ui.availUsers->selectedItems()) {
0117         const int index = d->ui.availUsers->indexOfTopLevelItem(item);
0118         d->ui.availUsers->takeTopLevelItem(index);
0119         d->ui.currentUsers->addTopLevelItem(item);
0120     }
0121 }
0122 
0123 void AdvancedPermissionsWidget::leftArrowClicked()
0124 {
0125     Q_D(AdvancedPermissionsWidget);
0126     for (QTreeWidgetItem *item : d->ui.currentUsers->selectedItems()) {
0127         if (item->data(LoginName, Qt::DisplayRole) != KUser().loginName()) {
0128             const int index = d->ui.currentUsers->indexOfTopLevelItem(item);
0129             d->ui.currentUsers->takeTopLevelItem(index);
0130             d->ui.availUsers->addTopLevelItem(item);
0131         }
0132     }
0133 }
0134 
0135 #include "moc_advancedpermissionswidget.cpp"