]> Some of my projects - localmylist.git/commitdiff
Add FileLocationCheckTask.
authorAPTX <marek321@gmail.com>
Sun, 21 Apr 2013 17:58:02 +0000 (19:58 +0200)
committerAPTX <marek321@gmail.com>
Sun, 21 Apr 2013 17:58:02 +0000 (19:58 +0200)
FileLocationCheckTask checks all file locations on the current host, removing all locations where the file does not exist on path.

localmylist-management/mainwindow.cpp
localmylist-management/mainwindow.h
localmylist-management/mainwindow.ui
localmylist/database.cpp
localmylist/database.h
localmylist/filelocationchecktask.cpp [new file with mode: 0644]
localmylist/filelocationchecktask.h [new file with mode: 0644]
localmylist/include/LocalMyList/FileLocationCheckTask [new file with mode: 0644]
localmylist/localmylist.pro

index 7c0a84a0fb24c6757b1b8aea96e25ce30f037e2a..899debc50785f649d20a62e9b367c9ec35ad1964 100644 (file)
@@ -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());
index 989cd7cae77ed2d387a846f02213daccb3cda4c1..61e5c6137f8ec21eef5f780878d43835049edd64 100644 (file)
@@ -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();
index 635818628fb52514cabb076b8b249918d8f8a8ee..40ea1327ffd8cdc4660d7b4656950c521e836067 100644 (file)
     <addaction name="actionImportMyList"/>
     <addaction name="actionImportTitlesFromWeb"/>
     <addaction name="actionImportTitles"/>
+    <addaction name="actionAddRelatedEpisodeInfo"/>
     <addaction name="separator"/>
     <addaction name="actionHandleRequests"/>
     <addaction name="actionRenameFiles"/>
     <addaction name="actionClearStartedPendingRequests"/>
     <addaction name="actionClearStartedMyListUpdates"/>
     <addaction name="actionClearFileRenames"/>
-    <addaction name="actionAddRelatedEpisodeInfo"/>
+    <addaction name="actionCheckFileLocations"/>
     <addaction name="separator"/>
     <addaction name="actionClearDatabase"/>
     <addaction name="actionClearMyListData"/>
     <string>About LocalMyList...</string>
    </property>
   </action>
+  <action name="actionCheckFileLocations">
+   <property name="text">
+    <string>Check File Locations</string>
+   </property>
+  </action>
  </widget>
  <layoutdefault spacing="6" margin="11"/>
  <customwidgets>
index 72b771bc4d79782f40210160e78b662b38d6b992..0c3b14399fd19835e0371b8bf95da11a6304eda3 100644 (file)
@@ -349,6 +349,37 @@ bool Database::removeFileLocation(int locationId)
        return exec(q);
 }
 
+QList<FileLocation> 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<FileLocation> 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;
index 0d254686d38c4ebe4a1c9e8f1ee804bc49307ce2..d6464b446d67b21b4d27cc5d2ed072cd6c8d7d5f 100644 (file)
@@ -64,6 +64,8 @@ public slots:
        bool setFileLocation(const LocalMyList::FileLocation &fileLocation);
        bool removeFileLocation(int locationId);
 
+       QList<LocalMyList::FileLocation> getFileLocationBatch(int startLocationId, int limit);
+
        LocalMyList::Anime getAnime(int aid);
        QList<LocalMyList::Episode> getEpisodes(int aid);
        LocalMyList::Episode getEpisode(int eid);
diff --git a/localmylist/filelocationchecktask.cpp b/localmylist/filelocationchecktask.cpp
new file mode 100644 (file)
index 0000000..1e8b584
--- /dev/null
@@ -0,0 +1,52 @@
+#include "filelocationchecktask.h"
+
+#include "database.h"
+#include <QFileInfo>
+
+#include <QDebug>
+
+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<FileLocation> 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 (file)
index 0000000..1a616fe
--- /dev/null
@@ -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 (file)
index 0000000..e829e78
--- /dev/null
@@ -0,0 +1 @@
+#include "../../filelocationchecktask.h"
\ No newline at end of file
index 279b4782479f1cec9b6148920cc4071a2212ac5e..9fd40868085bc38e6387b5d2de84887323c49734 100644 (file)
@@ -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