quickmod/FuseMounter/archivemanager.cpp

150 lines
5.1 KiB
C++

#include "archivemanager.h"
#include "database.h"
#include <QDir>
ArchiveManager::ArchiveManager(Database *db, const QString &modsdir, int profileId, QObject *parent)
: QObject{parent},
m_database{db},
m_modsdir{modsdir},
m_profileId{profileId}
{
reloadArchives();
connect( &m_cacheTimer, &QTimer::timeout, this, &ArchiveManager::cleanCache );
m_cacheTimer.setInterval(60000);
m_cacheTimer.start();
//QObject::connect( m_database, &Database::databaseModified, this, &ArchiveManager::reloadArchives );
}
int ArchiveManager::reloadArchives()
{
qDebug() << "ArchiveManager::reloadArchives()";
QHash< ModLibrary *, Archive * > archives;
const QList<ModLibrary *> &entries = m_database->archiveList(m_profileId);
for( ModLibrary *lib : entries )
{
Archive *arc = new Archive(lib->m_filename);
//Archive *arc = new Archive(m_modsdir + QDir::separator() + lib->m_filename);
archives.insert(lib, arc);
}
// Release old:
const QList< ModLibrary * > &libs = m_archives.keys();
for( ModLibrary *lib : libs ) {
Archive *arc = m_archives[lib];
arc->killme();
lib->deleteLater();
}
m_archives = archives;
m_cache.clear();
return m_archives.count();
}
ArchiveManager::~ArchiveManager()
{
m_cacheTimer.stop();
const QList< Archive * > &arcs = m_archives.values();
for( Archive *arc : arcs )
arc->deleteLater();
}
QHash<ModLibrary *, Archive *> ArchiveManager::getArchives()
{
return m_archives;
}
bool ArchiveManager::findFile(const QString &qpath, ModLibrary **library, ModEntry **modentry, Archive **archive)
{
if( m_cache.contains(qpath) )
{
//qDebug() << "ArchiveManager::findFile: Entry [" << qpath << "] cached (Valid:" << m_cache[qpath].m_valid << ")";
*library = m_cache[qpath].m_library;
*modentry = m_cache[qpath].m_entry;
*archive = m_cache[qpath].m_archive;
return m_cache[qpath].m_valid;
}
//qDebug() << "ArchiveManager::findFile: Searching for file" << qpath;
ArchiveCachedEntry cacheEntry;
QStringList qparts = qpath.split(QDir::separator());
if( 0 == qpath.length() )
qparts.clear();
const QList< ModLibrary * > &keys = m_archives.keys();
for( ModLibrary *lib : keys )
{
//qDebug() << "ArchiveManager::findFile: Scanning library" << lib->m_filename;
for( ModEntry *mod : lib->m_entries )
{
/*
if( 0 != mod->m_installedPath.compare(qpath, Qt::CaseInsensitive) )
continue;
//dolog("getattr (3): \"%s\" exists as \"%s\"\n", mod.m_archivePath.toStdString().c_str(), mod.m_installedPath.toStdString().c_str());
dolog("getattr(4): Comparing: qpath=\"%s\" vs. path=\"%s\"...\n",
qparts.join(QDir::separator()).toStdString().c_str(),
parts.join(QDir::separator()).toStdString().c_str()
);
*/
QStringList parts = mod->m_installedPath.split(QDir::separator());
if( parts.length() < qparts.length() )
continue;
bool skipme = false;
for( int x=0; x < qparts.length(); x++ )
{
if( 0 != qparts.at(x).compare(parts.at(x), Qt::CaseInsensitive) )
{
skipme = true;
break;
}
}
if( skipme ) {
//qDebug() << "ArchiveManager::findFile: Skipping" << mod->m_installedPath;
continue;
}
if( parts.length() > qparts.length() )
{
//qDebug() << "ArchiveManager::findFile: Found directory in" << mod->m_archivePath << "aka" << m_archives[lib]->getPath();
*library = NULL;
*modentry = NULL;
*archive = NULL;
cacheEntry.m_valid = true;
//cacheEntry.m_isdir = true;
//qDebug() << "Caching...";
m_cache[qpath] = cacheEntry;
//qDebug() << "Done.";
return true;
}
//QMap<QString, ArchiveCacheEntry *> *arc = m_archives[lib]->getEntries();
*library = lib;
*modentry = mod;
*archive = m_archives[lib];
//qDebug() << "ArchiveManager::findFile: Found file" << mod->m_installedPath << "in mod" << mod->m_archivePath << "aka" << m_archives[lib]->getPath();
cacheEntry.m_valid = true;
cacheEntry.m_library = lib;
cacheEntry.m_entry = mod;
cacheEntry.m_archive = m_archives[lib];
m_cache[qpath] = cacheEntry;
//m_archives[lib]->cleanCache(time(NULL)-30);
return true;
}
}
//qDebug() << "ArchiveManager::findFile: Couldn't find file" << qpath << "(Cached)";
m_cache[qpath] = cacheEntry;
return false;
}
void ArchiveManager::cleanCache()
{
//qDebug() << "ArchiveManager::cleanCache...";
const QList< ModLibrary * > &keys = m_archives.keys();
for( ModLibrary *lib : keys )
m_archives[lib]->cleanCache(time(NULL)-300); // 5 mins seems like plenty
}