From 2137f56158e2eb1532bc259a6c139c963a48d39b Mon Sep 17 00:00:00 2001 From: APTX Date: Sun, 21 Apr 2013 19:58:02 +0200 Subject: [PATCH] Add FileLocationCheckTask. FileLocationCheckTask checks all file locations on the current host, removing all locations where the file does not exist on path. --- localmylist-management/mainwindow.cpp | 6 +++ localmylist-management/mainwindow.h | 2 + localmylist-management/mainwindow.ui | 8 ++- localmylist/database.cpp | 31 +++++++++++ localmylist/database.h | 2 + localmylist/filelocationchecktask.cpp | 52 +++++++++++++++++++ localmylist/filelocationchecktask.h | 28 ++++++++++ .../include/LocalMyList/FileLocationCheckTask | 1 + localmylist/localmylist.pro | 9 ++-- 9 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 localmylist/filelocationchecktask.cpp create mode 100644 localmylist/filelocationchecktask.h create mode 100644 localmylist/include/LocalMyList/FileLocationCheckTask diff --git a/localmylist-management/mainwindow.cpp b/localmylist-management/mainwindow.cpp index 7c0a84a..899debc 100644 --- a/localmylist-management/mainwindow.cpp +++ b/localmylist-management/mainwindow.cpp @@ -20,6 +20,7 @@ #include "mylistfiltermodel.h" #include "unknownfilelookuptask.h" #include "addrelatedepisodestask.h" +#include "filelocationchecktask.h" #include "reportengine.h" #ifndef LOCALMYLIST_NO_ANIDBUDPCLIENT # include "renamesettingsdialog.h" @@ -276,6 +277,11 @@ void MainWindow::on_actionClearFailedRequests_triggered() LocalMyList::instance()->database()->clearFailedPendingMyListUpdateRequests(); } +void MainWindow::on_actionCheckFileLocations_triggered() +{ + MyList::instance()->executeTask(new FileLocationCheckTask()); +} + void MainWindow::on_actionRemoveKnownUnknownFiles_triggered() { LocalMyList::instance()->executeTask(new UnknownFileLookupTask()); diff --git a/localmylist-management/mainwindow.h b/localmylist-management/mainwindow.h index 989cd7c..61e5c61 100644 --- a/localmylist-management/mainwindow.h +++ b/localmylist-management/mainwindow.h @@ -64,6 +64,8 @@ private slots: void on_actionClearAnimeTitleData_triggered(); void on_actionClearFailedRequests_triggered(); + void on_actionCheckFileLocations_triggered(); + void on_actionRemoveKnownUnknownFiles_triggered(); void on_actionRenameScript_triggered(); diff --git a/localmylist-management/mainwindow.ui b/localmylist-management/mainwindow.ui index 6358186..40ea132 100644 --- a/localmylist-management/mainwindow.ui +++ b/localmylist-management/mainwindow.ui @@ -221,6 +221,7 @@ + @@ -232,7 +233,7 @@ - + @@ -395,6 +396,11 @@ About LocalMyList... + + + Check File Locations + + diff --git a/localmylist/database.cpp b/localmylist/database.cpp index 72b771b..0c3b143 100644 --- a/localmylist/database.cpp +++ b/localmylist/database.cpp @@ -349,6 +349,37 @@ bool Database::removeFileLocation(int locationId) return exec(q); } +QList Database::getFileLocationBatch(int startLocationId, int limit) +{ + // TODO this is a seq scan + QSqlQuery q = prepareOneShot(QString( + "SELECT %1 " + " FROM file_location fl " + " WHERE location_id >= :startLocationId " + " AND host_id = :hostId " + " LIMIT :limit " + ).arg(fileLocationFields())); + + q.bindValue(":startLocationId", startLocationId); + q.bindValue(":hostId", MyList::instance()->hostId()); + q.bindValue(":limit", limit); + + QList ret; + + if (!exec(q)) + return ret; + + QSqlResultIterator it(q); + while (q.next()) + { + FileLocation fl; + readFileLocationData(it, fl); + ret << fl; + } + + return ret; +} + Anime Database::getAnime(int aid) { Anime a; diff --git a/localmylist/database.h b/localmylist/database.h index 0d25468..d6464b4 100644 --- a/localmylist/database.h +++ b/localmylist/database.h @@ -64,6 +64,8 @@ public slots: bool setFileLocation(const LocalMyList::FileLocation &fileLocation); bool removeFileLocation(int locationId); + QList getFileLocationBatch(int startLocationId, int limit); + LocalMyList::Anime getAnime(int aid); QList getEpisodes(int aid); LocalMyList::Episode getEpisode(int eid); diff --git a/localmylist/filelocationchecktask.cpp b/localmylist/filelocationchecktask.cpp new file mode 100644 index 0000000..1e8b584 --- /dev/null +++ b/localmylist/filelocationchecktask.cpp @@ -0,0 +1,52 @@ +#include "filelocationchecktask.h" + +#include "database.h" +#include + +#include + +namespace LocalMyList { + +FileLocationCheckTask::FileLocationCheckTask(QObject *parent) : + AbstractTask(parent) +{ +} + +bool FileLocationCheckTask::canUseThreads() const +{ + return true; +} + +void FileLocationCheckTask::start() +{ + lastLocationId = 1; + workUnit(); +} + +void FileLocationCheckTask::workUnit() +{ + db->transaction(); + QList fileLocations = db->getFileLocationBatch(lastLocationId, OPERATIONS_PER_UNIT); + + for (const FileLocation &fl : fileLocations) + { + if (!QFileInfo(fl.path).exists()) + { + db->log(QObject::tr("Removing file location %1: %2. File does not exist.") + .arg(fl.locationId) + .arg(fl.path)); + db->removeFileLocation(fl.locationId); + } + lastLocationId = fl.locationId; + } + db->commit(); + + ++lastLocationId; + + if (fileLocations.count() == OPERATIONS_PER_UNIT) + emit nextWorkUnit(); + else + emit finished(); +} + +} // namespace LocalMyList diff --git a/localmylist/filelocationchecktask.h b/localmylist/filelocationchecktask.h new file mode 100644 index 0000000..1a616fe --- /dev/null +++ b/localmylist/filelocationchecktask.h @@ -0,0 +1,28 @@ +#ifndef FILELOCATIONCHECKTASK_H +#define FILELOCATIONCHECKTASK_H + +#include "abstracttask.h" + +namespace LocalMyList { + +class LOCALMYLISTSHARED_EXPORT FileLocationCheckTask : public AbstractTask +{ + Q_OBJECT +public: + explicit FileLocationCheckTask(QObject *parent = 0); + + virtual bool canUseThreads() const; + +public slots: + virtual void start(); + +private slots: + void workUnit(); + +private: + int lastLocationId; +}; + +} // namespace LocalMyList + +#endif // FILELOCATIONCHECKTASK_H diff --git a/localmylist/include/LocalMyList/FileLocationCheckTask b/localmylist/include/LocalMyList/FileLocationCheckTask new file mode 100644 index 0000000..e829e78 --- /dev/null +++ b/localmylist/include/LocalMyList/FileLocationCheckTask @@ -0,0 +1 @@ +#include "../../filelocationchecktask.h" \ No newline at end of file diff --git a/localmylist/localmylist.pro b/localmylist/localmylist.pro index 279b478..9fd4086 100644 --- a/localmylist/localmylist.pro +++ b/localmylist/localmylist.pro @@ -31,7 +31,8 @@ SOURCES += \ sqlquery.cpp \ sqlasyncquery.cpp \ sqlasyncqueryinternal.cpp \ - asyncquerytask.cpp + asyncquerytask.cpp \ + filelocationchecktask.cpp HEADERS += \ localmylist_global.h \ @@ -57,7 +58,8 @@ HEADERS += \ sqlasyncquery.h \ sqlasyncqueryinternal.h \ asyncquerytask.h \ - sqlresultiteratorinterface.h + sqlresultiteratorinterface.h \ + filelocationchecktask.h CONV_HEADERS += \ include/LocalMyList/AbstractTask \ @@ -72,7 +74,8 @@ CONV_HEADERS += \ include/LocalMyList/MyListModel \ include/LocalMyList/MyListNode \ include/LocalMyList/Settings \ - include/LocalMyList/UnknownFileLookupTask + include/LocalMyList/UnknownFileLookupTask \ + include/LocalMyList/FileLocationCheckTask !noscript { QT *= script -- 2.52.0