| 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_QTPLUGINS_PACKAGEMODEL_H |
| 7 |
#define AVOGADRO_QTPLUGINS_PACKAGEMODEL_H |
| 8 |
|
| 9 |
#include <QtCore/QAbstractTableModel> |
| 10 |
#include <QtCore/QDateTime> |
| 11 |
#include <QtCore/QList> |
| 12 |
#include <QtCore/QString> |
| 13 |
|
| 14 |
namespace Avogadro { |
| 15 |
namespace QtPlugins { |
| 16 |
|
| 17 |
/** |
| 18 |
* @brief Table model combining online catalog packages with locally installed |
| 19 |
* packages. Supports install, update, and remove workflows. |
| 20 |
*/ |
| 21 |
class PackageModel : public QAbstractTableModel |
| 22 |
{ |
| 23 |
Q_OBJECT |
| 24 |
|
| 25 |
public: |
| 26 |
/** Columns exposed by the model. */ |
| 27 |
enum Column |
| 28 |
{ |
| 29 |
StatusColumn = 0, |
| 30 |
NameColumn = 1, |
| 31 |
AvailableColumn = 2, |
| 32 |
FeaturesColumn = 3, |
| 33 |
DescriptionColumn = 4, |
| 34 |
ColumnCount = 5 |
| 35 |
}; |
| 36 |
|
| 37 |
/** Installation / update status for a package. */ |
| 38 |
enum class PackageStatus |
| 39 |
{ |
| 40 |
NotInstalled, ///< In catalog, not installed locally |
| 41 |
Installed, ///< Installed, version matches online (or no online version) |
| 42 |
UpdateAvailable, ///< Installed, newer version available online |
| 43 |
LocalOnly ///< Installed, not found in online catalog |
| 44 |
}; |
| 45 |
|
| 46 |
/** Combined representation of one package (online + installed state). */ |
| 47 |
struct PackageEntry |
| 48 |
{ |
| 49 |
// Online catalog fields |
| 50 |
QString name; |
| 51 |
QString description; |
| 52 |
QString onlineVersion; ///< "release_version" from JSON, "" if no release |
| 53 |
QString updatedAt; ///< Formatted date, fallback when no release version |
| 54 |
QDateTime onlineUpdatedAt; ///< Raw ISO timestamp for update heuristics. |
| 55 |
QString zipballUrl; |
| 56 |
QString baseUrl; ///< GitHub repo URL — primary match key |
| 57 |
QString readmeUrl; |
| 58 |
bool hasRelease = false; |
| 59 |
QString type; |
| 60 |
QString |
| 61 |
minimumAvogadroVersion; ///< from "minimum-avogadro-version", "" if none |
| 62 |
QStringList featureTypes; ///< from "feature-types" array |
| 63 |
|
| 64 |
// Installed state (populated from PackageManager) |
| 65 |
QString installedVersion; ///< from pyproject.toml [project.version] |
| 66 |
QString packageKey; ///< Registered package key used by PackageManager |
| 67 |
QString installedDir; ///< absolute path on disk, "" if not installed |
| 68 |
bool isSymlink = false; ///< true if installedDir is a symlink |
| 69 |
|
| 70 |
// UI state |
| 71 |
PackageStatus status = PackageStatus::NotInstalled; |
| 72 |
}; |
| 73 |
|
| 74 |
explicit PackageModel(QObject* parent = nullptr); |
| 75 |
~PackageModel() override = default; |
| 76 |
|
| 77 |
// QAbstractTableModel interface |
| 78 |
int rowCount(const QModelIndex& parent = QModelIndex()) const override; |
| 79 |
int columnCount(const QModelIndex& parent = QModelIndex()) const override; |
| 80 |
QVariant data(const QModelIndex& index, |
| 81 |
int role = Qt::DisplayRole) const override; |
| 82 |
bool setData(const QModelIndex& index, const QVariant& value, |
| 83 |
int role = Qt::EditRole) override; |
| 84 |
QVariant headerData(int section, Qt::Orientation orientation, |
| 85 |
int role = Qt::DisplayRole) const override; |
| 86 |
Qt::ItemFlags flags(const QModelIndex& index) const override; |
| 87 |
|
| 88 |
/** |
| 89 |
* Load the online package catalog from JSON bytes (plugins.json). |
| 90 |
* Resets the model; call mergeInstalledPackages() afterward. |
| 91 |
*/ |
| 92 |
void loadOnlineCatalog(const QByteArray& jsonBytes); |
| 93 |
|
| 94 |
/** |
| 95 |
* Overlay installed-package data from PackageManager onto the existing |
| 96 |
* entries. Also appends LocalOnly entries for installed packages not found |
| 97 |
* in the online catalog. |
| 98 |
*/ |
| 99 |
void mergeInstalledPackages(); |
| 100 |
|
| 101 |
// Accessors |
| 102 |
int entryCount() const { return static_cast<int>(m_entries.size()); } |
| 103 |
const PackageEntry& entry(int row) const { return m_entries[row]; } |
| 104 |
PackageEntry& entry(int row) { return m_entries[row]; } |
| 105 |
|
| 106 |
/** README URL for the given source-model row. */ |
| 107 |
QString readmeUrl(int row) const; |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns true when the running Avogadro version satisfies the entry's |
| 111 |
* minimum-avogadro-version requirement (or when no minimum is specified). |
| 112 |
*/ |
| 113 |
static bool versionCompatible(const PackageEntry& e); |
| 114 |
|
| 115 |
/** Short translated label for a feature type string. */ |
| 116 |
static QString featureTypeLabel(const QString& featureType); |
| 117 |
|
| 118 |
private: |
| 119 |
/** |
| 120 |
* Strip common Avogadro prefixes and normalise separators so that e.g. |
| 121 |
* "avogenerators" and "generators" map to the same key. |
| 122 |
*/ |
| 123 |
static QString normalizePackageName(const QString& name); |
| 124 |
|
| 125 |
/** Strip common Avogadro prefixes for display, preserving original case. */ |
| 126 |
static QString displayName(const QString& name); |
| 127 |
|
| 128 |
/** Compute (or recompute) the status of one entry. */ |
| 129 |
static PackageStatus computeStatus(const PackageEntry& e); |
| 130 |
static bool isOnlineNewerByDate(const PackageEntry& e); |
| 131 |
static bool parseSemVer(const QString& version, int& major, int& minor, |
| 132 |
int& patch); |
| 133 |
static int compareSemVer(const QString& lhs, const QString& rhs, bool& ok); |
| 134 |
|
| 135 |
/** Emoji glyph for the given status. */ |
| 136 |
static QString statusIcon(PackageStatus status); |
| 137 |
|
| 138 |
/** Unicode glyph for the given feature type string. */ |
| 139 |
static QString featureGlyph(const QString& featureType); |
| 140 |
|
| 141 |
QList<PackageEntry> m_entries; |
| 142 |
}; |
| 143 |
|
| 144 |
} // namespace QtPlugins |
| 145 |
} // namespace Avogadro |
| 146 |
|
| 147 |
#endif // AVOGADRO_QTPLUGINS_PACKAGEMODEL_H |