mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Always monitor download directory
This change puts download directory in an always monitoring state. For any files downloaded using kiwix downloader (and hence saved in download directory), this will automatically update the library if file is restored after deletion.
This commit is contained in:
parent
ea5173706b
commit
5ee18f1b40
@ -99,13 +99,17 @@ void KiwixApp::init()
|
||||
}
|
||||
});
|
||||
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, [=](QString monitorDir) {
|
||||
m_library.asyncLoadMonitorDir(monitorDir);
|
||||
m_library.asyncUpdateFromDir(monitorDir);
|
||||
});
|
||||
QString monitorDir = m_settingsManager.getMonitorDir();
|
||||
if (monitorDir != "") {
|
||||
m_library.setMonitorDirZims(m_library.getLibraryZimsFromDir(monitorDir));
|
||||
m_watcher.addPath(monitorDir);
|
||||
m_library.asyncLoadMonitorDir(monitorDir);
|
||||
QString downloadDir = m_settingsManager.getDownloadDir();
|
||||
auto dirList = QSet<QString>({monitorDir, downloadDir});
|
||||
for (auto dir : dirList) {
|
||||
if (dir != "") {
|
||||
m_library.setMonitorDirZims(dir, m_library.getLibraryZimsFromDir(dir));
|
||||
m_watcher.addPath(dir);
|
||||
m_library.asyncUpdateFromDir(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -284,13 +288,14 @@ bool KiwixApp::isCurrentArticleBookmarked()
|
||||
|
||||
void KiwixApp::setMonitorDir(const QString &dir) {
|
||||
m_settingsManager.setMonitorDir(dir);
|
||||
m_library.setMonitorDirZims(QStringList());
|
||||
m_library.setMonitorDirZims(dir, QStringList());
|
||||
for (auto path : m_watcher.directories()) {
|
||||
m_watcher.removePath(path);
|
||||
}
|
||||
if (dir != "") {
|
||||
m_watcher.addPath(dir);
|
||||
m_library.asyncLoadMonitorDir(dir);
|
||||
m_watcher.addPath(m_settingsManager.getDownloadDir());
|
||||
m_library.asyncUpdateFromDir(dir);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,9 +115,9 @@ void Library::save()
|
||||
m_library.writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
|
||||
}
|
||||
|
||||
void Library::setMonitorDirZims(QStringList zimList)
|
||||
void Library::setMonitorDirZims(QString monitorDir, QStringList zimList)
|
||||
{
|
||||
m_monitorDirZims = zimList;
|
||||
m_knownZimsInDir[monitorDir] = zimList;
|
||||
}
|
||||
|
||||
QStringList Library::getLibraryZimsFromDir(QString dir) const
|
||||
@ -133,13 +133,12 @@ QStringList Library::getLibraryZimsFromDir(QString dir) const
|
||||
return zimsInDir;
|
||||
}
|
||||
|
||||
void Library::loadMonitorDir(QString monitorDir)
|
||||
void Library::updateFromDir(QString monitorDir)
|
||||
{
|
||||
QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
QMutexLocker locker(&m_updateFromDirMutex);
|
||||
const QDir dir(monitorDir);
|
||||
QStringList newDirEntries = dir.entryList({"*.zim"});
|
||||
QStringList oldDirEntries = m_monitorDirZims;
|
||||
QStringList oldDirEntries = m_knownZimsInDir[monitorDir];
|
||||
for (auto &str : newDirEntries) {
|
||||
str = QDir::toNativeSeparators(monitorDir + "/" + str);
|
||||
}
|
||||
@ -160,18 +159,20 @@ void Library::loadMonitorDir(QString monitorDir)
|
||||
needsRefresh |= manager.addBookFromPath(book.toStdString());
|
||||
}
|
||||
for (auto bookPath : removedZims) {
|
||||
removeBookFromLibraryById(QString::fromStdString(m_library.getBookByPath(bookPath.toStdString()).getId()));
|
||||
try {
|
||||
removeBookFromLibraryById(QString::fromStdString(m_library.getBookByPath(bookPath.toStdString()).getId()));
|
||||
} catch (...) {}
|
||||
}
|
||||
if (needsRefresh) {
|
||||
setMonitorDirZims(newDir.values());
|
||||
emit(booksChanged());
|
||||
setMonitorDirZims(monitorDir, newDir.values());
|
||||
}
|
||||
}
|
||||
|
||||
void Library::asyncLoadMonitorDir(QString dir)
|
||||
void Library::asyncUpdateFromDir(QString dir)
|
||||
{
|
||||
QtConcurrent::run( [=]() {
|
||||
loadMonitorDir(dir);
|
||||
updateFromDir(dir);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <QObject>
|
||||
#include <QSharedPointer>
|
||||
#include <QMap>
|
||||
#include <QMutex>
|
||||
|
||||
#define TQS(v) (QString::fromStdString(v))
|
||||
#define FORWARD_GETTER(METH) QString METH() const { return TQS(mp_book->METH()); }
|
||||
@ -34,14 +35,14 @@ public:
|
||||
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 setMonitorDirZims(QString monitorDir, 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);
|
||||
void updateFromDir(QString dir);
|
||||
void asyncUpdateFromDir(QString dir);
|
||||
kiwix::Library& getKiwixLibrary() { return m_library; }
|
||||
public slots:
|
||||
const kiwix::Book& getBookById(QString id) const;
|
||||
@ -51,9 +52,10 @@ signals:
|
||||
void bookmarksChanged();
|
||||
|
||||
private:
|
||||
QMutex m_updateFromDirMutex;
|
||||
kiwix::Library m_library;
|
||||
QString m_libraryDirectory;
|
||||
QStringList m_monitorDirZims;
|
||||
QMap<QString, QStringList> m_knownZimsInDir;
|
||||
friend class LibraryManipulator;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user