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.
This commit is contained in:
Veloman Yunkan 2024-04-05 11:12:34 +04:00
parent 8969d9ade3
commit ef6d7661de
2 changed files with 7 additions and 12 deletions

View File

@ -566,15 +566,14 @@ void ContentManager::downloadBook(const QString &id)
downloadStarted(book, downloadId); 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) #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
Q_UNUSED(moveToTrash); Q_UNUSED(moveToTrash);
#endif #endif
if (fileName == "*") { const std::string dirPath = kiwix::removeLastPathElement(bookPath);
return; const std::string fileGlob = kiwix::getLastPathElement(bookPath) + "*";
} QDir dir(QString::fromStdString(dirPath), QString::fromStdString(fileGlob));
QDir dir(dirPath, fileName);
for(const QString& file: dir.entryList()) { for(const QString& file: dir.entryList()) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
if (moveToTrash) if (moveToTrash)
@ -598,9 +597,7 @@ void ContentManager::reallyEraseBook(const QString& id, bool moveToTrash)
auto tabBar = KiwixApp::instance()->getTabWidget(); auto tabBar = KiwixApp::instance()->getTabWidget();
tabBar->closeTabsByZimId(id); tabBar->closeTabsByZimId(id);
kiwix::Book book = mp_library->getBookById(id); kiwix::Book book = mp_library->getBookById(id);
QString dirPath = QString::fromStdString(kiwix::removeLastPathElement(book.getPath())); eraseBookFilesFromComputer(book.getPath(), moveToTrash);
QString fileName = QString::fromStdString(kiwix::getLastPathElement(book.getPath())) + "*";
eraseBookFilesFromComputer(dirPath, fileName, moveToTrash);
mp_library->removeBookFromLibraryById(id); mp_library->removeBookFromLibraryById(id);
mp_library->save(); mp_library->save();
emit mp_library->bookmarksChanged(); emit mp_library->bookmarksChanged();
@ -672,10 +669,8 @@ void ContentManager::reallyCancelBook(const QString& id)
} }
removeDownload(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 // incompleted downloaded file should be perma deleted
eraseBookFilesFromComputer(dirPath, filename, false); eraseBookFilesFromComputer(download->getPath(), false);
mp_library->removeBookFromLibraryById(id); mp_library->removeBookFromLibraryById(id);
mp_library->save(); mp_library->save();
emit(oneBookChanged(id)); emit(oneBookChanged(id));

View File

@ -73,7 +73,7 @@ private: // functions
void reallyCancelBook(const QString& id); void reallyCancelBook(const QString& id);
// reallyEraseBook() doesn't ask for confirmation (unlike eraseBook()) // reallyEraseBook() doesn't ask for confirmation (unlike eraseBook())
void reallyEraseBook(const QString& id, bool moveToTrash); 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 updateModel();
void setCategories(); void setCategories();
void setLanguages(); void setLanguages();