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...) --- smdirmodel.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 9 deletions(-) (limited to 'smdirmodel.cpp') diff --git a/smdirmodel.cpp b/smdirmodel.cpp index 6a7ac39..6c84784 100644 --- a/smdirmodel.cpp +++ b/smdirmodel.cpp @@ -16,6 +16,7 @@ #include "helper.h" SmDirModel::SmDirModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mHeaders(headers){ + mDb = QSqlDatabase::database("treedb"); mWatch = new SmDirWatcher(NumFields, this); connect(mWatch, SIGNAL(needRefresh()), this, SLOT(refresh())); mRunTimer = new QTimer(this); @@ -25,9 +26,12 @@ SmDirModel::SmDirModel(const QStringList &headers, QObject *parent) : SmTreeMode mRefreshTimer = new QTimer(this); readSettings(); - mCollector = mWatch->collector(); - connect(mCollector, SIGNAL(population(SmTreeItem*)), this, SLOT(populate(SmTreeItem*))); - connect(mCollector, SIGNAL(newData(QList,int)), this, SLOT(dirEvent(QList,int)), Qt::BlockingQueuedConnection); + connect(this, SIGNAL(modelReset()), mWatch, SLOT(startAsyncJobs())); + connect(mWatch, SIGNAL(population(SmTreeItem*)), this, SLOT(populate(SmTreeItem*))); + connect(mWatch, SIGNAL(setMd5Sum(QString,QString)), this, SLOT(setMd5Sum(QString,QString))); + connect(mWatch, SIGNAL(setFfmpeg(QString,QVariantMap)), this, SLOT(setFfmpeg(QString,QVariantMap))); + connect(mWatch, SIGNAL(setPicSize(QString,QVariant)), this, SLOT(setPicSize(QString,QVariant))); + connect(mWatch, SIGNAL(newData(QList,int)), this, SLOT(dirEvent(QList,int))); } SmDirModel::~SmDirModel(){ @@ -127,7 +131,6 @@ QFileInfo SmDirModel::fileInfo(const QModelIndex &idx) const { void SmDirModel::setDir(const QString &dir){ mCurrentDir = dir; - mCollector->start(); mWatch->setDir(mCurrentDir); } @@ -135,9 +138,11 @@ void SmDirModel::dirEvent(const QList &data, int e){ if(e == SmDirWatcher::Added){ /* for some reason SmTreeModel::addRow() doesn't work, * couldn't figure it out in 5 hours, so customize it - * and reset the model - */ + * and reset the model... gatherAsync is done by + * modelReset(); + */ addFile(data); + return; } QModelIndex idx = find(data.at(Name), Name, rootIndex()); if(!idx.isValid()){ @@ -146,10 +151,13 @@ void SmDirModel::dirEvent(const QList &data, int e){ if(e == SmDirWatcher::Deleted){ removeRow(idx.row()); } - if(e == SmDirWatcher::Modified){ + if(e == SmDirWatcher::Modified || e == SmDirWatcher::CloseWrite){ for(int i = 0; i < mHeaders.count(); ++i){ QModelIndex c = index(idx.row(), i, QModelIndex()); setData(c, data.at(i), Qt::EditRole); + if(e == SmDirWatcher::CloseWrite){ + mWatch->gatherAsync(data.at(FullPath).toString()); + } } } emit needResize(); @@ -175,8 +183,53 @@ void SmDirModel::refresh(){ setDir(mCurrentDir); } -void SmDirModel::setCheckForPresent(bool check){ - mCollector->setCheckForPresent(check); +void SmDirModel::setMd5Sum(QString path, QString md5){ + QModelIndex idx = find(path, FullPath); + if(idx.isValid()){ + QModelIndex md5Idx = createIndex(idx.column(), Md5sum, idx.internalPointer()); + setData(md5Idx, md5, Qt::EditRole); + QString mimeType = idx.data(TypeRole).toString(); + int present = 0; + QSqlQuery presentQ(mDb); + if(mimeType.startsWith("video")){ + presentQ.prepare("SELECT COUNT(*) FROM files WHERE cmd5sum = :md5"); + }else if(mimeType.startsWith("image")){ + presentQ.prepare("SELECT COUNT(*) FROM pics WHERE cmd5sum = :md5"); + }else{ + goto out; + } + presentQ.bindValue(":md5", md5); + presentQ.exec(); + while(presentQ.next()){ + present = presentQ.value(0).toInt(); + } + if(present){ + QModelIndex presentIdx = createIndex(idx.column(), Present, idx.internalPointer()); + setData(presentIdx, present, Qt::EditRole); + } + } + out: + emit needResize(); +} + +void SmDirModel::setFfmpeg(QString path, QVariantMap data){ + QModelIndex idx = find(path, FullPath); + if(idx.isValid()){ + QModelIndex durIdx = createIndex(idx.column(), DurSize, idx.internalPointer()); + setData(durIdx, data["duration"].toDouble(), Qt::EditRole); + QModelIndex bitrateIdx = createIndex(idx.column(), Bitrate, idx.internalPointer()); + setData(bitrateIdx, data["bit_rate"], Qt::EditRole); + } + emit needResize(); +} + +void SmDirModel::setPicSize(QString path, QVariant data){ + QModelIndex idx = find(path, FullPath); + if(idx.isValid()){ + QModelIndex psIdx = createIndex(idx.column(), DurSize, idx.internalPointer()); + setData(psIdx, data, Qt::EditRole); + } + emit needResize(); } void SmDirModel::populate(SmTreeItem *root){ -- cgit v1.2.3-70-g09d2