File indexing completed on 2024-12-22 04:15:08

0001 /*
0002  * This file is part of PyKrita, Krita' Python scripting plugin.
0003  *
0004  * SPDX-FileCopyrightText: 2013 Alex Turbov <i.zaufi@gmail.com>
0005  * SPDX-FileCopyrightText: 2014-2016 Boudewijn Rempt <boud@valdyas.org>
0006  * SPDX-FileCopyrightText: 2017 Jouni Pentikäinen (joupent@gmail.com)
0007  *
0008  * SPDX-License-Identifier: LGPL-2.0-or-later
0009  */
0010 #ifndef PYTHONMODULEMANAGER_H
0011 #define PYTHONMODULEMANAGER_H
0012 
0013 #include <QObject>
0014 #include "version_checker.h"
0015 #include "PythonPluginsModel.h"
0016 
0017 class PythonPluginsModel;
0018 
0019 /**
0020  * Represents a Python described in the plugin's .desktop file.
0021  */
0022 class PythonPlugin
0023 {
0024 public:
0025     /**
0026      * Transforms the Python module name into a file path part
0027      */
0028     QString moduleFilePathPart() const;
0029 
0030     bool isValid() const;
0031 
0032     inline const QString& errorReason() const
0033     {
0034         return m_errorReason;
0035     }
0036 
0037     inline bool isEnabled() const
0038     {
0039         return m_enabled;
0040     }
0041 
0042     inline bool isBroken() const
0043     {
0044         return m_broken;
0045     }
0046 
0047     inline bool isUnstable() const
0048     {
0049         return m_unstable;
0050     }
0051 
0052     QString name() const
0053     {
0054         return m_name;
0055     }
0056 
0057     QString moduleName() const
0058     {
0059         return m_moduleName;
0060     }
0061 
0062     QVariant property(const QString &name) const
0063     {
0064         return m_properties.value(name, "");
0065     }
0066 
0067     QString comment() const
0068     {
0069         return m_comment;
0070     }
0071 
0072     QString manual() const
0073     {
0074         return m_manual;
0075     }
0076 
0077 private:
0078     friend class PythonPluginManager;
0079 
0080     PythonPlugin() {
0081         m_properties["X-Python-Dependencies"] = QStringList();
0082         m_properties["X-Python-2-Dependencies"] = QStringList();
0083     }
0084 
0085     QString m_errorReason;
0086     bool m_enabled{false};
0087     bool m_broken{false};
0088     bool m_unstable{false};
0089     bool m_loaded{false};
0090 
0091     QString m_name;
0092     QString m_moduleName;
0093     QString m_comment;
0094     QString m_manual;
0095 
0096     QMap<QString, QVariant> m_properties;
0097 };
0098 
0099 /**
0100  * The Python plugin manager handles discovery, loading and unloading of Python plugins.
0101  * To get a reference to the manager, use PyKrita::pluginManager().
0102  */
0103 class PythonPluginManager : public QObject
0104 {
0105     Q_OBJECT
0106 
0107 public:
0108     PythonPluginManager();
0109 
0110     const QList<PythonPlugin>& plugins() const;
0111     PythonPlugin *plugin(int index);
0112 
0113     void scanPlugins();
0114     void tryLoadEnabledPlugins();
0115     void setPluginEnabled(PythonPlugin &plugin, bool enabled);
0116 
0117     PythonPluginsModel *model();
0118 
0119 public Q_SLOTS:
0120     void unloadAllModules();
0121 
0122 private:
0123     void loadModule(PythonPlugin &plugin);
0124     void unloadModule(PythonPlugin &plugin);
0125 
0126     static bool verifyModuleExists(PythonPlugin &);
0127     static void verifyDependenciesSetStatus(PythonPlugin&);
0128     static QPair<QString, PyKrita::version_checker> parseDependency(const QString&);
0129 
0130     QList<PythonPlugin> m_plugins;
0131 
0132     PythonPluginsModel m_model;
0133 };
0134 
0135 #endif //PYTHONMODULEMANAGER_H