From 130e0a3115cd66e38ec57846e06f69e7fd284400 Mon Sep 17 00:00:00 2001 From: Arno Date: Tue, 3 Sep 2013 06:12:33 +0200 Subject: Switch to QRunnable + QThreadPool Get rid of SmDataCollector and do its job in small, QRunnable tasks and let QThreadPool manage the treads. Works well with a local Filesystem. Yet to see how it works over networked Filesystems. Ah, before I forget: NEVER, EVER USE QPixmap in THREADS -> Random crashes! (Yes, I know, it's documented...) --- smdirwatcher.h | 93 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 35 deletions(-) (limited to 'smdirwatcher.h') diff --git a/smdirwatcher.h b/smdirwatcher.h index fccc1ed..9ec9181 100644 --- a/smdirwatcher.h +++ b/smdirwatcher.h @@ -1,4 +1,4 @@ - /* +/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version @@ -16,73 +16,96 @@ #include #include #include +#include +#include class SmTreeItem; class QSemaphore; class SmDataColletor; +class AsyncTask; +class Md5Summer; class SmDirWatcher : public QThread { Q_OBJECT public: - enum DWEvent { None, Added, Deleted, Modified, Populate }; + enum DWEvent { None, Added, Deleted, Modified, Populate, CloseWrite }; explicit SmDirWatcher(int numFields, QObject *parent = 0); - SmDataColletor *collector() { return mCollector; } signals: void dwEvent(const QString& file, int event); + void setMd5Sum(QString path, QString md5); + void setFfmpeg(QString path, QVariantMap data); + void setPicSize(QString path, QVariant size); void needRefresh(); + void population(SmTreeItem*); + void newData(const QList,int); public slots: void run(); void stop(); void setDir(const QString &dir); + void startAsyncJobs(); + void gatherAsync(const QString &path); private: + QList generalData(const QString &path); int mFd; int mDescr; QString mCurrent; - SmDataColletor *mCollector; - QSemaphore *mSemFree; - QSemaphore *mSemUsed; - QQueue > *mDataQueue; - QMutex *mQueueMx; char *mINdata; int mBufLen; + QThreadPool *mAsyncPool; + int mNumFields; + QList mAsyncTasks; }; -class SmDataColletor : public QThread { +class AsyncTask : public QObject, public QRunnable { Q_OBJECT public: - explicit SmDataColletor(const int numFields, QObject *parent = 0); - void init(QSemaphore *set, QSemaphore *get, QQueue > *data, QMutex *queueMx); + explicit AsyncTask(const QString &path = QString()); + bool skipMe(); - public slots: - void setCheckForPresent(bool present); - void run(); - void stop() { mCancel = true; } + protected: + virtual void run() = 0; + bool mSkip; + QMutex mStatusMx; + const QString mPath; +}; + +class Md5Summer : public AsyncTask { + Q_OBJECT + public: + explicit Md5Summer(const QString &path); signals: - void newData(const QList,int); - void population(SmTreeItem*); - void needRefresh(); - void totalFiles(int numFiles); - void progress(); + void md5sumDone(QString, QString); - private: - SmTreeItem *populate(const QString &dir); - const QList fileData(const QFileInfo &fi); - QString mCurrent; - QSemaphore *mSemFree; - QSemaphore *mSemUsed; - QMutex *mQueueMx; - QQueue > *mDataQueue; - int mMode; - const int mNumFields; - bool mCheckForPresent; - bool mCancel; - QMutex mCheckForPresentMx; - QSqlQuery *mPicPresentQ; - QSqlQuery *mMovPresentQ; + protected: + virtual void run(); +}; + +class FfmpegGatherer : public AsyncTask { + Q_OBJECT + public: + explicit FfmpegGatherer(const QString &path); + + signals: + void ffmpegDone(QString, QVariantMap); + + protected: + virtual void run(); +}; + +class PicSizeGatherer : public AsyncTask { + Q_OBJECT + public: + explicit PicSizeGatherer(const QString &path); + + signals: + void picSizeDone(QString, QVariant); + + protected: + virtual void run(); }; #endif // SMDIRWATCHER_H -- cgit v1.2.3-70-g09d2