11#include <QtCore/QCryptographicHash>
12#include <QtCore/QHash>
13#include <QtCore/QJsonObject>
14#include <QtCore/QString>
15#include <QtCore/QStringList>
24class QLoggingCategory;
82template<
typename Factory>
85 static_assert(std::is_base_of_v<IFactoryBase, Factory>,
86 "MetadataPackLoader<T> requires T to derive from PhosphorRegistry::IFactoryBase");
94 std::function<std::shared_ptr<Factory>(
const QString& subdirPath,
const QJsonObject& root,
bool isUser)>;
140 : m_registry(registry)
141 , m_strategy(makeStrategy(std::move(parser)))
142 , m_watcher(std::make_unique<
PhosphorFsLoader::WatchedDirectorySet>(*m_strategy, nullptr))
149 if (m_registry ==
nullptr) {
150 qFatal(
"MetadataPackLoader: registry must not be null");
152 m_strategy->setLoggingCategory(logCat);
168 m_strategy->setSignatureContrib([contrib = fn](QCryptographicHash& hasher,
const Entry& e) {
169 if (contrib && e.factory) {
170 contrib(hasher, *e.factory);
179 m_strategy->setPerEntryWatchPaths([fn = std::move(fn)](
const Entry& e) -> QStringList {
180 return (fn && e.factory) ? fn(*e.factory) : QStringList{};
187 m_strategy->setPerDirectoryWatchPaths(std::move(fn));
193 m_strategy->setPerSubdirSkip(std::move(fn));
201 m_onCommitted = std::move(fn);
216 const QStringList toRegister =
218 if (toRegister.isEmpty()) {
221 m_watcher->registerDirectories(toRegister, liveReload, order);
231 return m_watcher->directories();
240 if (m_userPath == path) {
244 m_strategy->setUserPath(path);
245 if (!m_watcher->directories().isEmpty()) {
246 m_watcher->rescanNow();
254 m_watcher->rescanNow();
264 std::shared_ptr<Factory> factory;
267 std::unique_ptr<PhosphorFsLoader::MetadataPackScanStrategy<Entry>> makeStrategy(
Parser parser)
269 auto wrappedParser = [parser = std::move(parser)](
const QString& subdir,
const QJsonObject& root,
270 bool isUser) -> std::optional<Entry> {
271 std::shared_ptr<Factory> factory = parser ? parser(subdir, root, isUser) : nullptr;
275 return Entry{factory->id(), std::move(factory)};
277 return std::make_unique<PhosphorFsLoader::MetadataPackScanStrategy<Entry>>(std::move(wrappedParser), [
this]() {
289 QHash<QString, QByteArray> fresh;
290 const QList<Entry> packs = m_strategy->packs();
291 for (
const Entry& e : packs) {
292 QCryptographicHash hasher(QCryptographicHash::Sha1);
293 hasher.addData(e.id.toUtf8());
294 if (m_sigContrib && e.factory) {
295 m_sigContrib(hasher, *e.factory);
297 const QByteArray fp = hasher.result();
299 const auto prev = m_fingerprints.constFind(e.id);
300 if (prev == m_fingerprints.cend()) {
311 if (!m_registry->registerFactory(e.factory)) {
314 }
else if (prev.value() != fp) {
332 }
else if (!m_registry->factory(e.id)) {
346 if (!m_registry->registerFactory(e.factory)) {
355 fresh.insert(e.id, fp);
364 const QList<QString> registered = m_registry->ids();
365 for (
const QString&
id : registered) {
366 if (m_fingerprints.contains(
id) && !fresh.contains(
id)) {
367 m_registry->unregisterFactory(
id);
370 m_fingerprints = std::move(fresh);
375 Registry<Factory>* m_registry =
nullptr;
380 QHash<QString, QByteArray> m_fingerprints;
383 std::unique_ptr<PhosphorFsLoader::MetadataPackScanStrategy<Entry>> m_strategy;
384 std::unique_ptr<PhosphorFsLoader::WatchedDirectorySet> m_watcher;
static QStringList filterNewSearchPaths(const QStringList &candidates, const QStringList &alreadyRegistered)
Helper for consumers building search-path APIs on top of the set: canonicalises (QDir::cleanPath) eve...
Definition ContainedFileReader.h:15
RegistrationOrder
Caller's declared priority direction for registerDirectories / setDirectories input.
Definition WatchedDirectorySet.h:64
@ LowestPriorityFirst
[sys-lowest, ..., sys-highest, user] — the canonical strategy view.
Definition WatchedDirectorySet.h:69
LiveReload
Opt-in policy for directory watching.
Definition WatchedDirectorySet.h:40
@ On
Definition WatchedDirectorySet.h:42
Definition IBarWidgetFactory.h:16
@ Replace
Overwrite; fire factoryUnregistered(old) + factoryRegistered(new).
Definition Registry.h:83