File indexing completed on 2025-01-05 04:35:36

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003     SPDX-FileCopyrightText: 2021 Slava Aseev <nullptrnine@basealt.ru>
0004     SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0005 */
0006 #pragma once
0007 
0008 #include <QAbstractTableModel>
0009 #include <QFile>
0010 #include <QObject>
0011 #include <QVariant>
0012 
0013 #include <QCoro/Task>
0014 
0015 class QFileInfo;
0016 class PermissionsHelper;
0017 class UserPermissionModel;
0018 class UserManager;
0019 
0020 class PermissionsHelperModel : public QAbstractTableModel
0021 {
0022     Q_OBJECT
0023 public:
0024     enum Column { ColumnPath, ColumnOldPermissions, ColumnNewPermissions };
0025     Q_ENUM(Column)
0026 
0027     explicit PermissionsHelperModel(PermissionsHelper *helper);
0028 
0029     int rowCount(const QModelIndex &parent = {}) const override;
0030     Q_INVOKABLE int columnCount(const QModelIndex &parent = {}) const override;
0031 
0032     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0033 
0034     Qt::ItemFlags flags(const QModelIndex &index) const override;
0035     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0036 
0037 private:
0038     const PermissionsHelper *parent;
0039 };
0040 
0041 class PermissionsHelper : public QObject
0042 {
0043     Q_OBJECT
0044 
0045     Q_PROPERTY(bool permissionsChangeRequired READ permissionsChangeRequired NOTIFY permissionsChanged)
0046     Q_PROPERTY(bool hasPosixACL READ hasPosixACL NOTIFY permissionsChanged)
0047     Q_PROPERTY(QStringList pathsWithPosixACL MEMBER m_filesWithPosixACL NOTIFY permissionsChanged)
0048     Q_PROPERTY(PermissionsHelperModel *model MEMBER m_model CONSTANT)
0049 
0050 public:
0051     struct PermissionsChangeInfo {
0052         QString path;
0053         QFile::Permissions oldPerm;
0054         QFile::Permissions newPerm;
0055     };
0056 
0057     explicit PermissionsHelper(const QString &path, const UserManager *userManager, const UserPermissionModel *permissionModel, QObject *parent = nullptr);
0058 
0059     const QList<PermissionsChangeInfo> &affectedPaths() const;
0060     bool permissionsChangeRequired() const;
0061     bool hasPosixACL() const;
0062 
0063     Q_INVOKABLE QStringList changePermissions();
0064     Q_INVOKABLE void reload();
0065 
0066 Q_SIGNALS:
0067     void permissionsChanged();
0068 
0069 private:
0070     void addPath(const QFileInfo &fileInfo, QFile::Permissions requiredPermissions);
0071     QCoro::Task<void> reloadInternal();
0072 
0073 private:
0074     const QString m_path;
0075     const UserManager *m_userManager;
0076     const UserPermissionModel *m_permissionModel;
0077 
0078     PermissionsHelperModel *m_model = nullptr;
0079     QList<PermissionsChangeInfo> m_affectedPaths;
0080     QStringList m_filesWithPosixACL;
0081 };