mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Add logic to refresh library with monitor directory changes
To check if there have been any changes in monitor directory, we compare the files before and after followed by adding/removing them from the library
This commit is contained in:
parent
a69e6a1f02
commit
45a09cfae6
@ -275,6 +275,10 @@ bool KiwixApp::isCurrentArticleBookmarked()
|
||||
|
||||
void KiwixApp::setMonitorDir(const QString &dir) {
|
||||
m_settingsManager.setMonitorDir(dir);
|
||||
m_library.setMonitorDirZims(QStringList());
|
||||
if (dir != "") {
|
||||
m_library.asyncLoadMonitorDir(dir);
|
||||
}
|
||||
}
|
||||
|
||||
#define CREATE_ACTION(ID, TEXT) \
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <kiwix/tools.h>
|
||||
|
||||
#include <QtDebug>
|
||||
#include <QtConcurrent/QtConcurrentRun>
|
||||
|
||||
|
||||
class LibraryManipulator: public kiwix::LibraryManipulator {
|
||||
public:
|
||||
@ -123,6 +125,45 @@ void Library::save()
|
||||
m_library.writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
|
||||
}
|
||||
|
||||
void Library::setMonitorDirZims(QStringList zimList)
|
||||
{
|
||||
m_monitorDirZims = zimList;
|
||||
}
|
||||
|
||||
void Library::loadMonitorDir(QString monitorDir)
|
||||
{
|
||||
QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
const QDir dir(monitorDir);
|
||||
QStringList newDirEntries = dir.entryList({"*.zim"});
|
||||
for (auto &str : newDirEntries) {
|
||||
str = monitorDir + QDir::separator() + str;
|
||||
}
|
||||
QSet<QString> newDir = QSet<QString>::fromList(newDirEntries);
|
||||
QStringList oldDirEntries = m_monitorDirZims;
|
||||
QSet<QString> oldDir = QSet<QString>::fromList(oldDirEntries);
|
||||
QStringList addedZims = (newDir - oldDir).values();
|
||||
QStringList removedZims = (oldDir - newDir).values();
|
||||
setMonitorDirZims(newDir.values());
|
||||
auto manipulator = LibraryManipulator(this);
|
||||
auto manager = kiwix::Manager(&manipulator);
|
||||
for (auto book : addedZims) {
|
||||
manager.addBookFromPath(book.toStdString());
|
||||
}
|
||||
for (auto bookPath : removedZims) {
|
||||
removeBookFromLibraryById(QString::fromStdString(m_library.getBookByPath(bookPath.toStdString()).getId()));
|
||||
}
|
||||
emit(booksChanged());
|
||||
save();
|
||||
}
|
||||
|
||||
void Library::asyncLoadMonitorDir(QString dir)
|
||||
{
|
||||
QtConcurrent::run( [=]() {
|
||||
loadMonitorDir(dir);
|
||||
});
|
||||
}
|
||||
|
||||
const kiwix::Book &Library::getBookById(QString id) const
|
||||
{
|
||||
return m_library.getBookById(id.toStdString());
|
||||
|
@ -33,11 +33,15 @@ public:
|
||||
QStringList getBookIds() const;
|
||||
QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const;
|
||||
const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = false) const { return m_library.getBookmarks(onlyValidBookmarks); }
|
||||
QStringList getLibraryZimsFromDir(QString dir) const;
|
||||
void setMonitorDirZims(QStringList zimList);
|
||||
void addBookToLibrary(kiwix::Book& book);
|
||||
void removeBookFromLibraryById(const QString& id);
|
||||
void addBookmark(kiwix::Bookmark& bookmark);
|
||||
void removeBookmark(const QString& zimId, const QString& url);
|
||||
void save();
|
||||
void loadMonitorDir(QString dir);
|
||||
void asyncLoadMonitorDir(QString dir);
|
||||
kiwix::Library& getKiwixLibrary() { return m_library; }
|
||||
public slots:
|
||||
const kiwix::Book& getBookById(QString id) const;
|
||||
@ -49,6 +53,7 @@ signals:
|
||||
private:
|
||||
kiwix::Library m_library;
|
||||
QString m_libraryDirectory;
|
||||
QStringList m_monitorDirZims;
|
||||
friend class LibraryManipulator;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user