File indexing completed on 2024-05-19 05:38:00

0001 /*
0002     SPDX-FileCopyrightText: 2020 Méven Car <meven.car@enioka.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractListModel>
0010 #include <QDBusConnection>
0011 #include <QDBusObjectPath>
0012 #include <QDir>
0013 #include <QFileIconProvider>
0014 
0015 #include <KService>
0016 
0017 #include <optional>
0018 
0019 #include "unit.h"
0020 
0021 struct AutostartEntry;
0022 class QQuickItem;
0023 
0024 class AutostartModel : public QAbstractListModel
0025 {
0026     Q_OBJECT
0027     Q_PROPERTY(bool usingSystemdBoot READ usingSystemdBoot CONSTANT);
0028 
0029 public:
0030     explicit AutostartModel(QObject *parent = nullptr);
0031     ~AutostartModel() override;
0032 
0033     enum Roles {
0034         Name,
0035         IconName = Qt::DecorationRole,
0036         Enabled = Qt::UserRole + 1,
0037         TargetFileDirPath,
0038         Source,
0039         FileName,
0040         OnlyInPlasma,
0041         SystemdUnit,
0042     };
0043 
0044     enum AutostartEntrySource {
0045         XdgAutoStart = 0,
0046         XdgScripts = 1,
0047         PlasmaShutdown = 2,
0048         PlasmaEnvScripts = 3,
0049     };
0050     Q_ENUM(AutostartEntrySource)
0051 
0052     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0053     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0054     QHash<int, QByteArray> roleNames() const override;
0055 
0056     bool reloadEntry(const QModelIndex &index, const QString &fileName);
0057 
0058     Q_INVOKABLE void removeEntry(int row);
0059     Q_INVOKABLE void editApplication(int row, QQuickItem *context);
0060     Q_INVOKABLE void addScript(const QUrl &url, AutostartEntrySource kind);
0061     Q_INVOKABLE void showApplicationDialog(QQuickItem *context);
0062     Q_INVOKABLE void makeFileExecutable(const QString &fileName);
0063     bool usingSystemdBoot() const;
0064 
0065     void load();
0066 
0067 Q_SIGNALS:
0068     void error(const QString &message);
0069     void nonExecutableScript(const QString &fileName, AutostartModel::AutostartEntrySource kind);
0070 
0071 private:
0072     void addApplication(const KService::Ptr &service);
0073     void loadScriptsFromDir(const QString &subDir, AutostartEntrySource kind);
0074     void insertScriptEntry(int index, const QString &name, const QString &targetFileDirPath, const QString &path, AutostartModel::AutostartEntrySource kind);
0075     QString makeSuggestedName(const QString &oldName);
0076     QString suggestName(const QUrl &baseUrl, const QString &oldName);
0077     static std::optional<AutostartEntry> loadDesktopEntry(const QString &fileName);
0078     QString systemdEscape(const QString &name) const;
0079 
0080 #if HAVE_SYSTEMD
0081     static constexpr bool haveSystemd = true;
0082 #else
0083     static constexpr bool haveSystemd = false;
0084 #endif
0085 
0086     QDir m_xdgConfigPath;
0087     QDir m_xdgAutoStartPath;
0088     QList<AutostartEntry> m_entries;
0089     QFileIconProvider m_iconProvider;
0090 };
0091 
0092 struct AutostartEntry {
0093     QString name; // Human readable name of file
0094     QString targetFileDirPath; // Script file Path without filename. In case of symlinks the target file path without filename. In case of application, Just the
0095                                // name of the application
0096     AutostartModel::AutostartEntrySource source;
0097     bool enabled;
0098     QString fileName; // the file backing the entry
0099     bool onlyInPlasma;
0100     QString iconName;
0101     Unit *systemdUnit = nullptr; // nullptr for PlasmaEnvScripts and PlamsaShutdown
0102 };
0103 Q_DECLARE_TYPEINFO(AutostartEntry, Q_RELOCATABLE_TYPE);