ContentManager::handleDisappearedZimFiles()

This commit is contained in:
Veloman Yunkan 2024-07-29 16:03:11 +04:00 committed by Kelson
parent 40f00901e1
commit c912fe9d34
2 changed files with 24 additions and 15 deletions

View File

@ -864,6 +864,21 @@ void ContentManager::asyncUpdateLibraryFromDir(QString dir)
}); });
} }
void ContentManager::handleDisappearedZimFiles(const QStringSet& zimPaths)
{
const auto kiwixLib = mp_library->getKiwixLibrary();
for (auto bookPath : zimPaths) {
try {
DBGOUT("directory monitoring: file disappeared: " << bookPath);
const auto book = kiwixLib->getBookByPath(bookPath.toStdString());
handleDisappearedZimFile(QString::fromStdString(book.getId()));
} catch (const std::exception& err) {
DBGOUT("directory monitoring: "
"error while dropping the disappeared book: " << err.what());
}
}
}
void ContentManager::updateLibraryFromDir(QString monitorDir) void ContentManager::updateLibraryFromDir(QString monitorDir)
{ {
typedef Library::QStringSet QStringSet; typedef Library::QStringSet QStringSet;
@ -880,25 +895,17 @@ void ContentManager::updateLibraryFromDir(QString monitorDir)
const auto kiwixLib = mp_library->getKiwixLibrary(); const auto kiwixLib = mp_library->getKiwixLibrary();
kiwix::Manager manager(kiwixLib); kiwix::Manager manager(kiwixLib);
bool needsRefresh = !removedZims.empty(); bool needsRefresh = !removedZims.empty();
for (auto bookPath : removedZims) { handleDisappearedZimFiles(removedZims);
try {
DBGOUT("ContentManager::updateLibraryFromDir(): "
<< "file disappeared: " << bookPath);
const auto book = kiwixLib->getBookByPath(bookPath.toStdString());
handleDisappearedZimFile(QString::fromStdString(book.getId()));
} catch (...) {}
}
for (auto bookPath : addedZims) { for (auto bookPath : addedZims) {
if ( mp_library->isBeingDownloadedByUs(bookPath) ) { if ( mp_library->isBeingDownloadedByUs(bookPath) ) {
DBGOUT("ContentManager::updateLibraryFromDir(): " << bookPath DBGOUT("directory monitoring: " << bookPath
<< " ignored since it is being downloaded by us."); << " ignored since it is being downloaded by us.");
} else { } else {
DBGOUT("ContentManager::updateLibraryFromDir(): " DBGOUT("directory monitoring: file appeared: " << bookPath);
<< "file appeared: " << bookPath);
const bool added = manager.addBookFromPath(bookPath.toStdString()); const bool added = manager.addBookFromPath(bookPath.toStdString());
DBGOUT(" " DBGOUT(" "
<< (added ? "and was added" : "but could not be added") << (added ? "and was added" : "but could not be added")
<< " to the library"); << "to the library");
needsRefresh |= added; needsRefresh |= added;
} }
} }

View File

@ -19,6 +19,7 @@ public: // types
typedef QList<QPair<QString, QString>> FilterList; typedef QList<QPair<QString, QString>> FilterList;
typedef ContentManagerModel::BookInfo BookInfo; typedef ContentManagerModel::BookInfo BookInfo;
typedef ContentManagerModel::BookInfoList BookInfoList; typedef ContentManagerModel::BookInfoList BookInfoList;
typedef Library::QStringSet QStringSet;
enum class BookState enum class BookState
{ {
@ -69,7 +70,7 @@ public: // functions
QStringList getCategories() const { return m_categories; } QStringList getCategories() const { return m_categories; }
LanguageList getLanguages() const { return m_languages; } LanguageList getLanguages() const { return m_languages; }
void setMonitorDirZims(QString monitorDir, Library::QStringSet zimList); void setMonitorDirZims(QString monitorDir, QStringSet zimList);
void asyncUpdateLibraryFromDir(QString dir); void asyncUpdateLibraryFromDir(QString dir);
signals: signals:
@ -117,6 +118,7 @@ private: // functions
void setCategories(); void setCategories();
void setLanguages(); void setLanguages();
void updateLibraryFromDir(QString dir); void updateLibraryFromDir(QString dir);
void handleDisappearedZimFiles(const QStringSet& zimPaths);
void handleDisappearedZimFile(QString bookId); void handleDisappearedZimFile(QString bookId);
// Get the book with the specified id from // Get the book with the specified id from
@ -148,7 +150,7 @@ private: // data
QMutex remoteLibraryLocker; QMutex remoteLibraryLocker;
QMutex m_updateFromDirMutex; QMutex m_updateFromDirMutex;
QMap<QString, Library::QStringSet> m_knownZimsInDir; QMap<QString, QStringSet> m_knownZimsInDir;
}; };
#endif // CONTENTMANAGER_H #endif // CONTENTMANAGER_H