mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-24 04:32:15 -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) {
|
void KiwixApp::setMonitorDir(const QString &dir) {
|
||||||
m_settingsManager.setMonitorDir(dir);
|
m_settingsManager.setMonitorDir(dir);
|
||||||
|
m_library.setMonitorDirZims(QStringList());
|
||||||
|
if (dir != "") {
|
||||||
|
m_library.asyncLoadMonitorDir(dir);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CREATE_ACTION(ID, TEXT) \
|
#define CREATE_ACTION(ID, TEXT) \
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#include <kiwix/tools.h>
|
#include <kiwix/tools.h>
|
||||||
|
|
||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
|
#include <QtConcurrent/QtConcurrentRun>
|
||||||
|
|
||||||
|
|
||||||
class LibraryManipulator: public kiwix::LibraryManipulator {
|
class LibraryManipulator: public kiwix::LibraryManipulator {
|
||||||
public:
|
public:
|
||||||
@ -123,6 +125,45 @@ void Library::save()
|
|||||||
m_library.writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
|
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
|
const kiwix::Book &Library::getBookById(QString id) const
|
||||||
{
|
{
|
||||||
return m_library.getBookById(id.toStdString());
|
return m_library.getBookById(id.toStdString());
|
||||||
|
@ -33,11 +33,15 @@ public:
|
|||||||
QStringList getBookIds() const;
|
QStringList getBookIds() const;
|
||||||
QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) 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); }
|
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 addBookToLibrary(kiwix::Book& book);
|
||||||
void removeBookFromLibraryById(const QString& id);
|
void removeBookFromLibraryById(const QString& id);
|
||||||
void addBookmark(kiwix::Bookmark& bookmark);
|
void addBookmark(kiwix::Bookmark& bookmark);
|
||||||
void removeBookmark(const QString& zimId, const QString& url);
|
void removeBookmark(const QString& zimId, const QString& url);
|
||||||
void save();
|
void save();
|
||||||
|
void loadMonitorDir(QString dir);
|
||||||
|
void asyncLoadMonitorDir(QString dir);
|
||||||
kiwix::Library& getKiwixLibrary() { return m_library; }
|
kiwix::Library& getKiwixLibrary() { return m_library; }
|
||||||
public slots:
|
public slots:
|
||||||
const kiwix::Book& getBookById(QString id) const;
|
const kiwix::Book& getBookById(QString id) const;
|
||||||
@ -49,6 +53,7 @@ signals:
|
|||||||
private:
|
private:
|
||||||
kiwix::Library m_library;
|
kiwix::Library m_library;
|
||||||
QString m_libraryDirectory;
|
QString m_libraryDirectory;
|
||||||
|
QStringList m_monitorDirZims;
|
||||||
friend class LibraryManipulator;
|
friend class LibraryManipulator;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user