From 34075dd73d2d8ad44b2ef317a846f7f7d1bcd8de Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Sun, 12 Dec 2021 17:40:08 +0200 Subject: [PATCH] [app] Delete orphaned marks files too --- app/src/BooksBookModel.cpp | 3 ++- app/src/BooksDefs.h | 5 +++-- app/src/BooksShelf.cpp | 44 ++++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/app/src/BooksBookModel.cpp b/app/src/BooksBookModel.cpp index 460cfae..f548b68 100644 --- a/app/src/BooksBookModel.cpp +++ b/app/src/BooksBookModel.cpp @@ -34,6 +34,7 @@ #include "BooksBookModel.h" #include "BooksTextStyle.h" #include "BooksUtil.h" +#include "BooksDefs.h" #include "HarbourDebug.h" #include "HarbourTask.h" @@ -130,7 +131,7 @@ BooksBookModel::PagingTask::~PagingTask() QString BooksBookModel::PagingTask::pageMarksFile(BooksBookModel* aModel) { - return aModel->book()->storageFile(QString(".%1x%2.marks"). + return aModel->book()->storageFile(QString(".%1x%2" BOOKS_MARKS_FILE_SUFFIX). arg(aModel->width()).arg(aModel->height())); } diff --git a/app/src/BooksDefs.h b/app/src/BooksDefs.h index 75bdc62..581c678 100644 --- a/app/src/BooksDefs.h +++ b/app/src/BooksDefs.h @@ -1,6 +1,6 @@ /* - * Copyright (C) 2015-2020 Jolla Ltd. - * Copyright (C) 2015-2020 Slava Monich + * Copyright (C) 2015-2021 Jolla Ltd. + * Copyright (C) 2015-2021 Slava Monich * * You may use this file under the terms of the BSD license as follows: * @@ -66,6 +66,7 @@ BOOKS_QML_PLUGIN_V2, name, klass::createSingleton) #define BOOKS_STATE_FILE_SUFFIX ".state" +#define BOOKS_MARKS_FILE_SUFFIX ".marks" extern int booksPPI; #define BOOKS_PPI booksPPI diff --git a/app/src/BooksShelf.cpp b/app/src/BooksShelf.cpp index 51dc0ff..37f50bd 100644 --- a/app/src/BooksShelf.cpp +++ b/app/src/BooksShelf.cpp @@ -100,7 +100,7 @@ public: void performTask(); - int findBook(QString aFileName) const; + int findBook(const QStringRef* aFileName) const; static int find(QFileInfoList aList, QString aFileName, int aStart); public: @@ -129,15 +129,14 @@ int BooksShelf::LoadTask::find(QFileInfoList aList, QString aName, int aStart) return -1; } -int BooksShelf::LoadTask::findBook(QString aFileName) const +int BooksShelf::LoadTask::findBook(const QStringRef* aFileName) const { - if (!aFileName.isEmpty()) { - const int n = iItems.count(); - for (int i=0; ibook() && item->fileName() == aFileName) { - return i; - } + // Caller makes sure that aFileName is not empty + const int n = iItems.count(); + for (int i=0; ibook() && aFileName->compare(item->fileName()) == 0) { + return i; } } return -1; @@ -202,19 +201,32 @@ void BooksShelf::LoadTask::performTask() } } - // Cleanup the state files + // Delete orphaned state and marks files if (!isCanceled()) { + static const QString stateSuffix(BOOKS_STATE_FILE_SUFFIX); + static const QString marksSuffix(BOOKS_MARKS_FILE_SUFFIX); QStringList deleteMe; - const QString suffix(BOOKS_STATE_FILE_SUFFIX); QDirIterator configIt(iStorage.fullConfigPath(iRelativePath)); while (configIt.hasNext() && !isCanceled()) { - QString path(configIt.next()); - if (path.endsWith(suffix)) { - QString fileName(configIt.fileName()); - QString name(fileName.left(fileName.length() - suffix.length())); - if (!name.isEmpty() && findBook(name) < 0) { + const QString path(configIt.next()); + if (path.endsWith(stateSuffix)) { + const QString fileName(configIt.fileName()); + // State file is named .state + QStringRef name(fileName.leftRef(fileName.length() - stateSuffix.length())); + if (!name.isEmpty() && findBook(&name) < 0) { deleteMe.append(path); } + } else if (path.endsWith(marksSuffix)) { + const QString fileName(configIt.fileName()); + // Marks file is named .x.marks + QStringRef name(fileName.leftRef(fileName.length() - stateSuffix.length())); + const int nextSeparator = name.lastIndexOf('.'); + if (nextSeparator > 0) { + name = name.left(nextSeparator); + if (findBook(&name) < 0) { + deleteMe.append(path); + } + } } } while (!deleteMe.isEmpty() && !isCanceled()) {