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);
}
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));

View File

@ -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();