10#include <QApplication>
13#include <QStandardItemModel>
14#include <QSortFilterProxyModel>
15#include <QFileSystemWatcher>
25 , SubElemModel_ (
new QStandardItemModel (
this))
26 , SortModel_ (
new QSortFilterProxyModel (
this))
30 if (RelativePath_.startsWith (
'/'))
31 RelativePath_ = RelativePath_.mid (1);
32 if (!RelativePath_.endsWith (
'/'))
33 RelativePath_.append (
'/');
35 SortModel_->setDynamicSortFilter (
true);
36 SortModel_->setSourceModel (SubElemModel_);
40 &QFileSystemWatcher::directoryChanged,
42 &ResourceLoader::HandleDirectoryChanged);
56 LocalPrefixesChain_ <<
result;
59 if (!
testDir.exists (
".leechcraft/data/" +
prefix + RelativePath_))
62 <<
".leechcraft/data/" +
prefix + RelativePath_
63 <<
"doesn't exist, trying to create it...";
65 if (!
testDir.mkpath (
".leechcraft/data/" +
prefix + RelativePath_))
69 <<
".leechcraft/data/" +
prefix + RelativePath_;
73 ScanPath (
result + RelativePath_);
75 Watcher_->addPath (
result + RelativePath_);
80#if defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
81 const QStringList prefixes { QApplication::applicationDirPath () +
"/../Resources/share/" };
82#elif defined (Q_OS_WIN32)
84#elif defined (INSTALL_PREFIX)
89 "/usr/local/share/leechcraft/",
90 "/usr/share/leechcraft/"
96 GlobalPrefixesChain_ <<
prefix;
97 ScanPath (
prefix + RelativePath_);
99 if (QFile::exists (
prefix + RelativePath_))
101 Watcher_->addPath (
prefix + RelativePath_);
108 <<
"no prefixes have been added:"
116 if (
qApp->property (
"no-resource-caching").toBool ())
121 CacheFlushTimer_->stop ();
123 CachePathContents_.setMaxCost (0);
124 CachePixmaps_.setMaxCost (0);
129 CacheFlushTimer_->start (
timeout);
131 CachePathContents_.setMaxCost (size * 1024);
132 CachePixmaps_.setMaxCost (size * 1024);
138 CachePathContents_.clear ();
139 CachePixmaps_.clear ();
147 for (
const auto&
prefix : LocalPrefixesChain_ + GlobalPrefixesChain_)
151 for (
const auto&
info : list)
167 for (
const auto&
prefix : LocalPrefixesChain_ + GlobalPrefixesChain_)
177 <<
"absolute file path"
180 << QFile::exists (
can);
181 if (QFile::exists (
can))
213 if (CachePathContents_.contains (
path))
221 auto result = std::make_shared<QBuffer> ();
224 result->open (QIODevice::ReadOnly);
230 if (!
result->isSequential () &&
231 result->size () < CachePathContents_.maxCost () / 2)
233 if (
result->open (QIODevice::ReadOnly))
235 const auto& data =
result->readAll ();
236 CachePathContents_.insert (
path,
new QByteArray { data }, data.size ());
245 if (!
result->open (QIODevice::ReadOnly))
247 <<
"unable to open file"
249 <<
result->errorString ();
266 if (CachePixmaps_.contains (
basename))
273 const auto& data =
dev->readAll ();
276 result.loadFromData (data);
301 void ResourceLoader::ScanPath (
const QString&
path)
306 if (!SubElemModel_->findItems (
entry).isEmpty ())
313 void ResourceLoader::HandleDirectoryChanged (
const QString&
path)
317 for (
auto&
paths : Entry2Paths_)
326 for (
auto i = Entry2Paths_.begin ();
i != Entry2Paths_.end ();)
331 i = Entry2Paths_.erase (
i);
void SetCacheParams(int size, int timeout)
Sets the caching parameters of this loader.
void watchedDirectoriesChanged()
QAbstractItemModel * GetSubElemModel() const
Returns the subelement model with the contents of registered paths.
void FlushCache()
Forcefully flushes the cache.
QIODevice_ptr Load(const QStringList &pathVariants, bool open=false) const
Returns the QIODevice for the corresponding resource.
void SetAttrFilters(QDir::Filters)
Sets the attribute filters for the subelement model.
QString GetPath(const QStringList &pathVariants) const
Returns the first found path for the list of variants.
void AddGlobalPrefix()
Registers global OS-dependent prefixes.
ResourceLoader(const QString &relPath, QObject *obj=nullptr)
Initializes the loader with the given path.
QFileInfoList List(const QString &option, const QStringList &names={}, QDir::Filters filters=QDir::NoFilter) const
Lists the available files for the given option.
QString GetIconPath(const QString &basename) const
Calls GetPath() with standard variants for the icon extensions.
QPixmap LoadPixmap(const QString &basename) const
Returns the pixmap for the given basename.
QIODevice_ptr GetIconDevice(const QString &basename, bool open=false) const
Returns the QIODevice for the corresponding icon.
void SetNameFilters(const QStringList &)
Sets the name filters for the subelement model.
void SetVerbose(bool verbose)
Control whether the ResourceLoader prints what it is doing to the logs.
void AddLocalPrefix(QString prefix=QString())
Registers a local search prefix.
Container< T > Filter(const Container< T > &c, F f)
std::shared_ptr< QIODevice > QIODevice_ptr