1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/*
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
2 of the License, or (at your option) any later version.
*/
#include <QDateTime>
#include <QDir>
#include <QImage>
#include <sys/inotify.h>
#include <unistd.h>
#include <poll.h>
#include "smdirmodel.h"
#include "smdirwatcher.h"
#include "smtreeitem.h"
#include "helper.h"
SmDirWatcher::SmDirWatcher(int numFields, QObject *parent) : QThread(parent), mFd(0), mDescr(0), mNumFields(numFields), mExpensiveOps(true) {
mBufLen = 1024 * (sizeof(struct inotify_event) + 16);
mINdata = new char[mBufLen];
mFd = inotify_init();
mAsyncPool = new QThreadPool(this);
}
void SmDirWatcher::setDir(const QString &dir){
if(mDescr){
inotify_rm_watch(mFd, mDescr); //generates IN_IGNORE ???
}
foreach(AsyncTask *s, mAsyncTasks){
s->skipMe();
}
mAsyncPool->waitForDone();
foreach(AsyncTask *s, mAsyncTasks){
s->deleteLater();
}
mAsyncTasks.clear();
mCurrent = dir;
QDir d(mCurrent);
SmTreeItem *rootItem = new SmTreeItem(mNumFields);
foreach(QFileInfo fi, d.entryInfoList()){
if(fi.fileName() == "." || fi.fileName() == ".."){
continue;
}
QList<QVariant> data = generalData(fi.absoluteFilePath());
QString mime = data.at(SmDirModel::Type).toString();
SmTreeItem *newItem = new SmTreeItem(data, rootItem);
rootItem->appendChild(newItem);
if(mExpensiveOps){
Md5Summer *s = new Md5Summer(fi.absoluteFilePath());
s->setAutoDelete(false);
connect(s, SIGNAL(md5sumDone(QString,QString)), this, SIGNAL(setMd5Sum(QString,QString)));
mAsyncTasks.append(s);
if(mime.startsWith("video")){
FfmpegGatherer *g = new FfmpegGatherer(fi.absoluteFilePath());
g->setAutoDelete(false);
connect(g, SIGNAL(ffmpegDone(QString,QVariantMap)), this, SIGNAL(setFfmpeg(QString,QVariantMap)));
mAsyncTasks.append(g);
}else if(mime.startsWith("image")){
PicSizeGatherer *pg = new PicSizeGatherer(fi.absoluteFilePath());
pg->setAutoDelete(false);
connect(pg, SIGNAL(picSizeDone(QString,QVariant)), this, SIGNAL(setPicSize(QString,QVariant)));
mAsyncTasks.append(pg);
}
}
}
emit population(rootItem);
/* mask rationale:
* IN_DELETE_SELF cannot happen since we're only
* watching one directory at all time.
* We don't care about the other events
*/
mDescr = inotify_add_watch(mFd, qPrintable(dir), IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO);
}
void SmDirWatcher::startAsyncJobs(){
foreach(AsyncTask *s, mAsyncTasks){
mAsyncPool->start(s);
}
}
void SmDirWatcher::gatherAsync(const QString &path){
if(!mExpensiveOps){
return;
}
QString mimeType = Helper::mimeType(path);
Md5Summer *s = new Md5Summer(path);
connect(s, SIGNAL(md5sumDone(QString,QString)), this, SIGNAL(setMd5Sum(QString,QString)));
mAsyncPool->start(s);
if(mimeType.startsWith("video")){
FfmpegGatherer *g = new FfmpegGatherer(path);
connect(g, SIGNAL(ffmpegDone(QString,QVariantMap)), this, SIGNAL(setFfmpeg(QString,QVariantMap)));
mAsyncPool->start(g);
}else if(mimeType.startsWith("image")){
PicSizeGatherer *ps = new PicSizeGatherer(path);
connect(ps, SIGNAL(picSizeDone(QString,QVariant)), this, SIGNAL(setPicSize(QString,QVariant)));
mAsyncPool->start(ps);
}
}
void SmDirWatcher::setExpensiveOps(bool expensiveOps){
mExpensiveOps = expensiveOps;
}
QList<QVariant> SmDirWatcher::generalData(const QString &path){
QFileInfo fi(path);
QList<QVariant> data;
data << fi.fileName() << fi.size();
QString mime = Helper::mimeType(fi.absoluteFilePath());
data << mime;
data << fi.lastModified();
data << QVariant() << QVariant() << QVariant();
data << fi.absoluteFilePath() << 0;
return data;
}
void SmDirWatcher::run(){
struct pollfd pfd[1];
pfd[0].fd = mFd;
pfd[0].events = POLLIN;
pfd[0].revents = 0;
int pr = poll(pfd, 1, 0);
if(pr <= 0){
return;
}
int r = read(mFd, mINdata, mBufLen);
if(r <= 0){
return;
}
int i = 0;
while(i < r){
inotify_event *e = reinterpret_cast<inotify_event*>(&mINdata[i]);
if((!e->len) || (e->mask & IN_IGNORED)){
i += sizeof(inotify_event) + e->len;
continue;
}
quint32 mask = e->mask;
QString name = QString("%1/%2").arg(mCurrent).arg(e->name);
QList<QVariant> d = generalData(name);
if(mask & IN_CREATE || mask & IN_MOVED_TO){
emit newData(d, Added);
}else if(mask & IN_DELETE || mask & IN_MOVED_FROM){
emit newData(d, Deleted);
}else if(mask & IN_MODIFY){
emit newData(d, Modified);
}else if(mask & IN_CLOSE_WRITE){
emit newData(d, CloseWrite);
}
i += sizeof(inotify_event) + e->len;
}
}
void SmDirWatcher::stop(){
quit();
wait();
}
AsyncTask::AsyncTask(const QString &path) : mSkip(false), mPath(path) {}
bool AsyncTask::skipMe(){
if(mStatusMx.tryLock()){
mSkip = true;
mStatusMx.unlock();
return true;
}
return false;
}
#include <QDebug>
Md5Summer::Md5Summer(const QString &path) : AsyncTask(path) {}
void Md5Summer::run(){
mStatusMx.lock();
bool skip = mSkip;
mStatusMx.unlock();
if(skip){
return;
}
QString md5 = Helper::md5Sum(mPath);
emit md5sumDone(mPath, md5);
}
FfmpegGatherer::FfmpegGatherer(const QString &path) : AsyncTask(path) {}
void FfmpegGatherer::run(){
mStatusMx.lock();
bool skip = mSkip;
mStatusMx.unlock();
if(skip){
return;
}
QVariantMap retval = Helper::ffmpegData(mPath);
emit ffmpegDone(mPath, retval);
}
PicSizeGatherer::PicSizeGatherer(const QString &path) : AsyncTask(path) {}
void PicSizeGatherer::run(){
mStatusMx.lock();
bool skip = mSkip;
mStatusMx.unlock();
if(skip){
return;
}
QVariant retval = Helper::picSize(mPath);
emit picSizeDone(mPath, retval);
}
|