From ef6d7661def5bb536a51c562d0053642256920db Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Fri, 5 Apr 2024 11:12:34 +0400 Subject: [PATCH] Better ContentManager::eraseBookFilesFromComputer() Note that this change drops the protection against accidentally removing all files in a book's directory. The risk of a such a destructive operation is still present if an invalid path is passed into `eraseBookFilesFromComputer()` but that will be addressed in a separate commit. --- src/contentmanager.cpp | 17 ++++++----------- src/contentmanager.h | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index e2adb49..b2213e4 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -566,15 +566,14 @@ void ContentManager::downloadBook(const QString &id) downloadStarted(book, downloadId); } -void ContentManager::eraseBookFilesFromComputer(const QString dirPath, const QString fileName, const bool moveToTrash) +void ContentManager::eraseBookFilesFromComputer(const std::string& bookPath, bool moveToTrash) { #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) Q_UNUSED(moveToTrash); #endif - if (fileName == "*") { - return; - } - QDir dir(dirPath, fileName); + const std::string dirPath = kiwix::removeLastPathElement(bookPath); + const std::string fileGlob = kiwix::getLastPathElement(bookPath) + "*"; + QDir dir(QString::fromStdString(dirPath), QString::fromStdString(fileGlob)); for(const QString& file: dir.entryList()) { #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) if (moveToTrash) @@ -598,9 +597,7 @@ void ContentManager::reallyEraseBook(const QString& id, bool moveToTrash) auto tabBar = KiwixApp::instance()->getTabWidget(); tabBar->closeTabsByZimId(id); kiwix::Book book = mp_library->getBookById(id); - QString dirPath = QString::fromStdString(kiwix::removeLastPathElement(book.getPath())); - QString fileName = QString::fromStdString(kiwix::getLastPathElement(book.getPath())) + "*"; - eraseBookFilesFromComputer(dirPath, fileName, moveToTrash); + eraseBookFilesFromComputer(book.getPath(), moveToTrash); mp_library->removeBookFromLibraryById(id); mp_library->save(); emit mp_library->bookmarksChanged(); @@ -672,10 +669,8 @@ void ContentManager::reallyCancelBook(const QString& id) } removeDownload(id); - QString dirPath = QString::fromStdString(kiwix::removeLastPathElement(download->getPath())); - QString filename = QString::fromStdString(kiwix::getLastPathElement(download->getPath())) + "*"; // incompleted downloaded file should be perma deleted - eraseBookFilesFromComputer(dirPath, filename, false); + eraseBookFilesFromComputer(download->getPath(), false); mp_library->removeBookFromLibraryById(id); mp_library->save(); emit(oneBookChanged(id)); diff --git a/src/contentmanager.h b/src/contentmanager.h index 25f4488..0d3d067 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -73,7 +73,7 @@ private: // functions void reallyCancelBook(const QString& id); // reallyEraseBook() doesn't ask for confirmation (unlike eraseBook()) void reallyEraseBook(const QString& id, bool moveToTrash); - void eraseBookFilesFromComputer(const QString dirPath, const QString filename, const bool moveToTrash); + void eraseBookFilesFromComputer(const std::string& bookPath, bool moveToTrash); void updateModel(); void setCategories(); void setLanguages();