mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Preparing to move directory monitoring to ContentManager
Moved 3 member functions of Library related to directory from library.cpp to contentmanager.cpp as is so that the changes made to them when those functions are transferred to ContentManager are easier to spot.
This commit is contained in:
parent
f38b7480f6
commit
52583ad6a9
@ -849,3 +849,49 @@ void ContentManager::setSortBy(const QString& sortBy, const bool sortOrderAsc)
|
|||||||
m_sortOrderAsc = sortOrderAsc;
|
m_sortOrderAsc = sortOrderAsc;
|
||||||
emit(booksChanged());
|
emit(booksChanged());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Library::setMonitorDirZims(QString monitorDir, QStringSet zimList)
|
||||||
|
{
|
||||||
|
m_knownZimsInDir[monitorDir] = zimList;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Library::asyncUpdateFromDir(QString dir)
|
||||||
|
{
|
||||||
|
(void) QtConcurrent::run([=]() {
|
||||||
|
updateFromDir(dir);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void Library::updateFromDir(QString monitorDir)
|
||||||
|
{
|
||||||
|
QMutexLocker locker(&m_updateFromDirMutex);
|
||||||
|
const QDir dir(monitorDir);
|
||||||
|
const QStringSet oldDirEntries = m_knownZimsInDir[monitorDir];
|
||||||
|
QStringSet newDirEntries;
|
||||||
|
for (const auto &file : dir.entryList({"*.zim"})) {
|
||||||
|
newDirEntries.insert(QDir::toNativeSeparators(monitorDir + "/" + file));
|
||||||
|
}
|
||||||
|
const QStringSet addedZims = newDirEntries - oldDirEntries;
|
||||||
|
const QStringSet removedZims = oldDirEntries - newDirEntries;
|
||||||
|
kiwix::Manager manager(getKiwixLibrary());
|
||||||
|
bool needsRefresh = !removedZims.empty();
|
||||||
|
for (auto bookPath : addedZims) {
|
||||||
|
if ( isBeingDownloadedByUs(bookPath) ) {
|
||||||
|
// qDebug() << "DBG: Library::updateFromDir(): "
|
||||||
|
// << bookPath
|
||||||
|
// << " ignored since it is being downloaded by us.";
|
||||||
|
} else {
|
||||||
|
needsRefresh |= manager.addBookFromPath(bookPath.toStdString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto bookPath : removedZims) {
|
||||||
|
try {
|
||||||
|
const auto book = mp_library->getBookByPath(bookPath.toStdString());
|
||||||
|
removeBookFromLibraryById(QString::fromStdString(book.getId()));
|
||||||
|
} catch (...) {}
|
||||||
|
}
|
||||||
|
if (needsRefresh) {
|
||||||
|
emit(booksChanged());
|
||||||
|
setMonitorDirZims(monitorDir, newDirEntries);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -194,11 +194,6 @@ void Library::save()
|
|||||||
mp_library->writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
|
mp_library->writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Library::setMonitorDirZims(QString monitorDir, QStringSet zimList)
|
|
||||||
{
|
|
||||||
m_knownZimsInDir[monitorDir] = zimList;
|
|
||||||
}
|
|
||||||
|
|
||||||
Library::QStringSet Library::getLibraryZimsFromDir(QString dir) const
|
Library::QStringSet Library::getLibraryZimsFromDir(QString dir) const
|
||||||
{
|
{
|
||||||
QStringSet zimsInDir;
|
QStringSet zimsInDir;
|
||||||
@ -214,47 +209,6 @@ Library::QStringSet Library::getLibraryZimsFromDir(QString dir) const
|
|||||||
return zimsInDir;
|
return zimsInDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Library::updateFromDir(QString monitorDir)
|
|
||||||
{
|
|
||||||
QMutexLocker locker(&m_updateFromDirMutex);
|
|
||||||
const QDir dir(monitorDir);
|
|
||||||
const QStringSet oldDirEntries = m_knownZimsInDir[monitorDir];
|
|
||||||
QStringSet newDirEntries;
|
|
||||||
for (const auto &file : dir.entryList({"*.zim"})) {
|
|
||||||
newDirEntries.insert(QDir::toNativeSeparators(monitorDir + "/" + file));
|
|
||||||
}
|
|
||||||
const QStringSet addedZims = newDirEntries - oldDirEntries;
|
|
||||||
const QStringSet removedZims = oldDirEntries - newDirEntries;
|
|
||||||
kiwix::Manager manager(getKiwixLibrary());
|
|
||||||
bool needsRefresh = !removedZims.empty();
|
|
||||||
for (auto bookPath : addedZims) {
|
|
||||||
if ( isBeingDownloadedByUs(bookPath) ) {
|
|
||||||
// qDebug() << "DBG: Library::updateFromDir(): "
|
|
||||||
// << bookPath
|
|
||||||
// << " ignored since it is being downloaded by us.";
|
|
||||||
} else {
|
|
||||||
needsRefresh |= manager.addBookFromPath(bookPath.toStdString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (auto bookPath : removedZims) {
|
|
||||||
try {
|
|
||||||
const auto book = mp_library->getBookByPath(bookPath.toStdString());
|
|
||||||
removeBookFromLibraryById(QString::fromStdString(book.getId()));
|
|
||||||
} catch (...) {}
|
|
||||||
}
|
|
||||||
if (needsRefresh) {
|
|
||||||
emit(booksChanged());
|
|
||||||
setMonitorDirZims(monitorDir, newDirEntries);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Library::asyncUpdateFromDir(QString dir)
|
|
||||||
{
|
|
||||||
(void) QtConcurrent::run([=]() {
|
|
||||||
updateFromDir(dir);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const kiwix::Book &Library::getBookById(QString id) const
|
const kiwix::Book &Library::getBookById(QString id) const
|
||||||
{
|
{
|
||||||
return mp_library->getBookById(id.toStdString());
|
return mp_library->getBookById(id.toStdString());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user