[app] Delete orphaned marks files too

This commit is contained in:
Slava Monich 2021-12-12 17:40:08 +02:00
parent 79753923cc
commit 34075dd73d
3 changed files with 33 additions and 19 deletions

View file

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

View file

@ -1,6 +1,6 @@
/*
* Copyright (C) 2015-2020 Jolla Ltd.
* Copyright (C) 2015-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2015-2021 Jolla Ltd.
* Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
*
* 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

View file

@ -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; i<n; i++) {
BooksItem* item = iItems.at(i);
if (item->book() && item->fileName() == aFileName) {
return i;
}
// Caller makes sure that aFileName is not empty
const int n = iItems.count();
for (int i=0; i<n; i++) {
BooksItem* item = iItems.at(i);
if (item->book() && 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 <BOOK>.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 <BOOK>.<width>x<height>.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()) {