File indexing completed on 2024-07-14 03:54:25

0001 /*
0002     SPDX-FileCopyrightText: 2005 Sean Harmer <sh@rama.homelinux.org>
0003     SPDX-FileCopyrightText: 2005-2007 Till Adam <adam@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KACLEDITWIDGET_P_H
0009 #define KACLEDITWIDGET_P_H
0010 
0011 #include <config-kiowidgets.h>
0012 
0013 #if HAVE_POSIX_ACL || defined(Q_MOC_RUN)
0014 #include <kacl.h>
0015 #include <sys/acl.h>
0016 
0017 #include <QDialog>
0018 #include <QHash>
0019 #include <QTreeWidget>
0020 
0021 #include <QComboBox>
0022 #include <kfileitem.h>
0023 
0024 class KACLListViewItem;
0025 class QButtonGroup;
0026 class KACLListView;
0027 class QStackedWidget;
0028 class QCheckBox;
0029 class QAbstractButton;
0030 class QColorGroup;
0031 
0032 /**
0033 @author Sean Harmer
0034 */
0035 class KACLListView : public QTreeWidget
0036 {
0037     Q_OBJECT
0038     friend class KACLListViewItem;
0039 
0040 public:
0041     enum Types {
0042         OWNER_IDX = 0,
0043         GROUP_IDX,
0044         OTHERS_IDX,
0045         MASK_IDX,
0046         NAMED_USER_IDX,
0047         NAMED_GROUP_IDX,
0048         LAST_IDX,
0049     };
0050     enum EntryType {
0051         User = 1,
0052         Group = 2,
0053         Others = 4,
0054         Mask = 8,
0055         NamedUser = 16,
0056         NamedGroup = 32,
0057         AllTypes = 63,
0058     };
0059 
0060     explicit KACLListView(QWidget *parent = nullptr);
0061     ~KACLListView() override;
0062 
0063     bool hasMaskEntry() const
0064     {
0065         return m_hasMask;
0066     }
0067     bool hasDefaultEntries() const;
0068     bool allowDefaults() const
0069     {
0070         return m_allowDefaults;
0071     }
0072     void setAllowDefaults(bool v)
0073     {
0074         m_allowDefaults = v;
0075     }
0076     unsigned short maskPermissions() const;
0077     void setMaskPermissions(unsigned short maskPerms);
0078     acl_perm_t maskPartialPermissions() const;
0079     void setMaskPartialPermissions(acl_perm_t maskPerms);
0080 
0081     bool maskCanBeDeleted() const;
0082     bool defaultMaskCanBeDeleted() const;
0083 
0084     const KACLListViewItem *findDefaultItemByType(EntryType type) const;
0085     const KACLListViewItem *findItemByType(EntryType type, bool defaults = false) const;
0086     unsigned short calculateMaskValue(bool defaults) const;
0087     void calculateEffectiveRights();
0088 
0089     QStringList allowedUsers(bool defaults, KACLListViewItem *allowedItem = nullptr);
0090     QStringList allowedGroups(bool defaults, KACLListViewItem *allowedItem = nullptr);
0091 
0092     KACL getACL();
0093     KACL getDefaultACL();
0094 
0095 public Q_SLOTS:
0096     void slotAddEntry();
0097     void slotEditEntry();
0098     void slotRemoveEntry();
0099     void setACL(const KACL &anACL);
0100     void setDefaultACL(const KACL &anACL);
0101 
0102     QSize sizeHint() const override;
0103 
0104 protected Q_SLOTS:
0105     void slotItemClicked(QTreeWidgetItem *pItem, int col);
0106     void slotItemDoubleClicked(QTreeWidgetItem *item, int col);
0107 
0108 protected:
0109     void contentsMousePressEvent(QMouseEvent *e);
0110 
0111 private:
0112     void fillItemsFromACL(const KACL &pACL, bool defaults = false);
0113     KACL itemsToACL(bool defaults) const;
0114 
0115     KACL m_ACL;
0116     KACL m_defaultACL;
0117     unsigned short m_mask;
0118     bool m_hasMask;
0119     bool m_allowDefaults;
0120     QStringList m_allUsers;
0121     QStringList m_allGroups;
0122 };
0123 
0124 class EditACLEntryDialog : public QDialog
0125 {
0126     Q_OBJECT
0127 public:
0128     EditACLEntryDialog(KACLListView *listView,
0129                        KACLListViewItem *item,
0130                        const QStringList &users,
0131                        const QStringList &groups,
0132                        const QStringList &defaultUsers,
0133                        const QStringList &defaultGroups,
0134                        int allowedTypes = KACLListView::AllTypes,
0135                        int allowedDefaultTypes = KACLListView::AllTypes,
0136                        bool allowDefault = false);
0137     KACLListViewItem *item() const
0138     {
0139         return m_item;
0140     }
0141 public Q_SLOTS:
0142     void slotOk();
0143     void slotSelectionChanged(QAbstractButton *);
0144 private Q_SLOTS:
0145     void slotUpdateAllowedUsersAndGroups();
0146     void slotUpdateAllowedTypes();
0147 
0148 private:
0149     KACLListView *m_listView;
0150     KACLListViewItem *m_item;
0151     QStringList m_users;
0152     QStringList m_groups;
0153     QStringList m_defaultUsers;
0154     QStringList m_defaultGroups;
0155     int m_allowedTypes;
0156     int m_allowedDefaultTypes;
0157     QButtonGroup *m_buttonGroup;
0158     QComboBox *m_usersCombo;
0159     QComboBox *m_groupsCombo;
0160     QStackedWidget *m_widgetStack;
0161     QCheckBox *m_defaultCB;
0162     QHash<QAbstractButton *, int> m_buttonIds;
0163 };
0164 
0165 class KACLListViewItem : public QTreeWidgetItem
0166 {
0167 public:
0168     KACLListViewItem(QTreeWidget *parent, KACLListView::EntryType type, unsigned short value, bool defaultEntry, const QString &qualifier = QString());
0169     ~KACLListViewItem() override;
0170     QString key() const;
0171     bool operator<(const QTreeWidgetItem &other) const override;
0172 
0173     void calcEffectiveRights();
0174 
0175     bool isDeletable() const;
0176     bool isAllowedToChangeType() const;
0177 
0178     void togglePerm(acl_perm_t perm);
0179 
0180     void updatePermissionIcons();
0181     void repaint();
0182 
0183     KACLListView::EntryType type;
0184     unsigned short value;
0185     bool isDefault;
0186     QString qualifier;
0187     bool isPartial;
0188 
0189 private:
0190     KACLListView *m_pACLListView;
0191 };
0192 
0193 #endif
0194 #endif