| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_QTGUI_PACKAGEMANAGER_H |
| 7 |
#define AVOGADRO_QTGUI_PACKAGEMANAGER_H |
| 8 |
|
| 9 |
#include "avogadroqtguiexport.h" |
| 10 |
|
| 11 |
#include <QtCore/QJsonObject> |
| 12 |
#include <QtCore/QList> |
| 13 |
#include <QtCore/QObject> |
| 14 |
#include <QtCore/QString> |
| 15 |
#include <QtCore/QStringList> |
| 16 |
#include <QtCore/QVariantMap> |
| 17 |
|
| 18 |
namespace Avogadro { |
| 19 |
namespace QtGui { |
| 20 |
|
| 21 |
/** |
| 22 |
* @brief Singleton that manages pyproject.toml-based plugin packages. |
| 23 |
* |
| 24 |
* Packages are registered once (at download time or manually) and cached in |
| 25 |
* QSettings. On startup, loadRegisteredPackages() replays the cached |
| 26 |
* registrations as featureRegistered() signals so that consumer plugins |
| 27 |
* (Command, ScriptCharges, Forcefield, etc.) can create their actions and |
| 28 |
* models without re-parsing TOML or calling scripts. |
| 29 |
* |
| 30 |
* During a session, registerPackage() can be called at any time (e.g. after |
| 31 |
* the plugin downloader installs a new package) and signals fire immediately. |
| 32 |
*/ |
| 33 |
class AVOGADROQTGUI_EXPORT PackageManager : public QObject |
| 34 |
{ |
| 35 |
Q_OBJECT |
| 36 |
public: |
| 37 |
static PackageManager* instance(); |
| 38 |
|
| 39 |
struct PackageInfo |
| 40 |
{ |
| 41 |
QString name; ///< from [project.name] |
| 42 |
QString version; ///< from [project.version] |
| 43 |
QString directory; ///< absolute path to the package directory |
| 44 |
QString command; ///< entry point from [project.scripts] |
| 45 |
QString description; ///< from [project.description] |
| 46 |
}; |
| 47 |
|
| 48 |
/** Known feature-type strings (TOML table names under [tool.avogadro]). */ |
| 49 |
static QStringList featureTypes(); |
| 50 |
|
| 51 |
/** |
| 52 |
* Build a stable key for one package-provided feature. |
| 53 |
* Consumers can use this as a map key to track registrations and removals. |
| 54 |
* Components must not be empty. |
| 55 |
*/ |
| 56 |
static QString packageFeatureKey(const QString& packageDir, |
| 57 |
const QString& command, |
| 58 |
const QString& identifier); |
| 59 |
|
| 60 |
/** |
| 61 |
* Load package user-options from JSON or TOML (selected by extension). |
| 62 |
* Returns an empty object on error. |
| 63 |
*/ |
| 64 |
static QJsonObject loadOptionsFromFile(const QString& userOptionsPath); |
| 65 |
|
| 66 |
/** |
| 67 |
* Merge package user-options into @p opts. Keys from the file override |
| 68 |
* existing values in @p opts. |
| 69 |
*/ |
| 70 |
static void mergeOptionsFromFile(QJsonObject& opts, |
| 71 |
const QString& userOptionsPath); |
| 72 |
|
| 73 |
/** |
| 74 |
* Run the package script with @c --user-options and parse the JSON output. |
| 75 |
* Uses pixi (preferred) or the venv-installed script as fallback. |
| 76 |
* Returns an empty object on error. |
| 77 |
*/ |
| 78 |
static QJsonObject loadOptionsFromScript(const QString& packageDir, |
| 79 |
const QString& command, |
| 80 |
const QString& identifier); |
| 81 |
|
| 82 |
/** |
| 83 |
* Resolve user-options for a package feature. If @p userOptionsValue is |
| 84 |
* the literal string "dynamic", runs the script with @c --user-options |
| 85 |
* via loadOptionsFromScript(). Otherwise treats it as a relative file |
| 86 |
* path and loads via loadOptionsFromFile(). |
| 87 |
* Returns an empty object on error or if @p userOptionsValue is empty. |
| 88 |
*/ |
| 89 |
static QJsonObject resolveUserOptions(const QString& userOptionsValue, |
| 90 |
const QString& packageDir, |
| 91 |
const QString& command, |
| 92 |
const QString& identifier); |
| 93 |
|
| 94 |
// --- Installation --- |
| 95 |
|
| 96 |
/** |
| 97 |
* Asynchronously run pixi (preferred) or pip install in each directory, |
| 98 |
* then call registerPackage() for each. |
| 99 |
* Emits packagesInstalled() when the background thread finishes. |
| 100 |
* Safe to call from the main thread. |
| 101 |
*/ |
| 102 |
void installPackages(const QStringList& packageDirs); |
| 103 |
|
| 104 |
// --- Registration --- |
| 105 |
|
| 106 |
/** |
| 107 |
* Parse pyproject.toml in @p packageDir, cache the result in QSettings, |
| 108 |
* and emit featureRegistered() for every feature found. |
| 109 |
* @return true on success. |
| 110 |
*/ |
| 111 |
bool registerPackage(const QString& packageDir); |
| 112 |
|
| 113 |
/** |
| 114 |
* Remove a package and all its feature registrations from the cache. |
| 115 |
* Emits featureRemoved() for each feature that was registered. |
| 116 |
*/ |
| 117 |
bool unregisterPackage(const QString& packageName); |
| 118 |
|
| 119 |
// --- Startup --- |
| 120 |
|
| 121 |
/** |
| 122 |
* Load all previously-registered packages from QSettings and emit |
| 123 |
* featureRegistered() for each cached feature. |
| 124 |
* Call this once after all consumer plugins have connected. |
| 125 |
*/ |
| 126 |
void loadRegisteredPackages(const QString& typeFilter = QString()); |
| 127 |
|
| 128 |
// --- Directory scanning --- |
| 129 |
|
| 130 |
/** |
| 131 |
* Scan @p directoryPath for subdirectories that contain a pyproject.toml. |
| 132 |
* Each discovered package is compared against the cached hash of its |
| 133 |
* pyproject.toml; new or modified packages are returned as a list of |
| 134 |
* absolute directory paths. The caller is responsible for calling |
| 135 |
* registerPackage() on any directories it wants to install. |
| 136 |
* @return list of package directories that are new or have been modified. |
| 137 |
*/ |
| 138 |
QStringList scanDirectory(const QString& directoryPath); |
| 139 |
|
| 140 |
// --- Queries --- |
| 141 |
|
| 142 |
QStringList registeredPackages() const; |
| 143 |
PackageInfo packageInfo(const QString& packageName) const; |
| 144 |
|
| 145 |
/** |
| 146 |
* Return the distinct feature-type strings (e.g. "menu-commands", |
| 147 |
* "file-formats") for a registered package, read from the QSettings cache. |
| 148 |
*/ |
| 149 |
QStringList packageFeatureTypes(const QString& packageName) const; |
| 150 |
|
| 151 |
signals: |
| 152 |
/** |
| 153 |
* Emitted after installPackages() finishes installing and registering |
| 154 |
* all requested packages. |
| 155 |
*/ |
| 156 |
void packagesInstalled(); |
| 157 |
|
| 158 |
/** |
| 159 |
* Emitted for each feature found in a package. |
| 160 |
* |
| 161 |
* @param type One of the featureTypes() strings, e.g. |
| 162 |
* "menu-commands", "electrostatic-models", etc. |
| 163 |
* @param packageDir Absolute path to the package directory. |
| 164 |
* @param command Entry-point name from [project.scripts]. |
| 165 |
* @param identifier The feature's unique identifier. |
| 166 |
* @param metadata Remaining TOML sub-table fields as a QVariantMap. |
| 167 |
*/ |
| 168 |
void featureRegistered(const QString& type, const QString& packageDir, |
| 169 |
const QString& command, const QString& identifier, |
| 170 |
const QVariantMap& metadata); |
| 171 |
|
| 172 |
/** |
| 173 |
* Emitted when a feature is removed (so consumers can clean up). |
| 174 |
* |
| 175 |
* @param type One of the featureTypes() strings. |
| 176 |
* @param packageDir Absolute path to the package directory. |
| 177 |
* @param command Entry-point name from [project.scripts]. |
| 178 |
* @param identifier The feature's unique identifier. |
| 179 |
*/ |
| 180 |
void featureRemoved(const QString& type, const QString& packageDir, |
| 181 |
const QString& command, const QString& identifier); |
| 182 |
|
| 183 |
private: |
| 184 |
explicit PackageManager(QObject* parent = nullptr); |
| 185 |
|
| 186 |
/** Internal representation of a single feature entry. */ |
| 187 |
struct FeatureEntry |
| 188 |
{ |
| 189 |
QString type; |
| 190 |
QString identifier; |
| 191 |
QVariantMap metadata; |
| 192 |
}; |
| 193 |
|
| 194 |
/** |
| 195 |
* Parse pyproject.toml at @p packageDir/pyproject.toml. |
| 196 |
* Populates @p info and @p features on success. |
| 197 |
*/ |
| 198 |
bool parsePackage(const QString& packageDir, PackageInfo& info, |
| 199 |
QList<FeatureEntry>& features); |
| 200 |
|
| 201 |
/** Emit featureRegistered() for every entry in @p features. */ |
| 202 |
void emitFeatures(const PackageInfo& info, |
| 203 |
const QList<FeatureEntry>& features); |
| 204 |
|
| 205 |
// JSON serialisation for QSettings cache |
| 206 |
static QJsonObject featureEntryToJson(const FeatureEntry& entry); |
| 207 |
static FeatureEntry featureEntryFromJson(const QJsonObject& obj); |
| 208 |
|
| 209 |
// QSettings helpers |
| 210 |
void saveToCache(const PackageInfo& info, |
| 211 |
const QList<FeatureEntry>& features); |
| 212 |
void removeFromCache(const QString& packageName); |
| 213 |
bool loadFromCache(const QString& packageName, PackageInfo& info, |
| 214 |
QList<FeatureEntry>& features); |
| 215 |
}; |
| 216 |
|
| 217 |
} // namespace QtGui |
| 218 |
} // namespace Avogadro |
| 219 |
|
| 220 |
#endif // AVOGADRO_QTGUI_PACKAGEMANAGER_H |