summaryrefslogtreecommitdiffstats
path: root/smdirwatcher.cpp
blob: 8650d3b506220f0cd0063a58611755919f626e5b (plain)
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
/*
  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 <QVarLengthArray>
#include <QMutexLocker>
#include <QDateTime>
#include <QDir>

#include <sys/inotify.h>
#include <stropts.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>

#include "smdirwatcher.h"
#include "smtreeitem.h"
#include "helper.h"

extern int errno;

SmDirWatcher::SmDirWatcher(QObject *parent) : QThread(parent), mFd(0), mDescr(0) {
    mFd = inotify_init1(IN_NONBLOCK);
}

SmDirWatcher::~SmDirWatcher(){
    if(mFd && mDescr){
        inotify_rm_watch(mFd, mDescr);
    }
}

void SmDirWatcher::setDir(const QString &dir){
    QMutexLocker lock(&mWatchMx);
    if(mDescr){
        inotify_rm_watch(mFd, mDescr); //generates IN_IGNORE ???
    }
    /* 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);
    mCurrent = dir;
}

void SmDirWatcher::run(){
    QMutexLocker lock(&mWatchMx);
    QVarLengthArray<char, 4096> data(4096);
    int r = read(mFd, data.data(), data.size());
    int err = errno;
    if(r < 0){
        if(err != EAGAIN){
            return;
        }
        r = read(mFd, data.data(), data.size());
    }
    // inspect data
    char *at = data.data();
    inotify_event *e = reinterpret_cast<inotify_event*>(at);
    if(!e->len){
        return;
    }
    char *end = at + data.size();
    while(at < end){
        if(e->mask & IN_IGNORED){
            at += sizeof(inotify_event) + e->len;
            continue;
        }
        quint32 mask = e->mask;
        QString name = QString("%1/%2").arg(mCurrent).arg(e->name);
        at += sizeof(inotify_event) + e->len;
        if(mask & IN_CREATE){
            emit dwEvent(name, Added);
            return;
        }
        if(mask & IN_DELETE){
            emit dwEvent(name, Deleted);
            return;
        }
        if(mask & IN_CLOSE_WRITE || e->mask & IN_MODIFY){
            emit dwEvent(name, Modified);
            return;
        }
        if(mask & IN_MOVED_FROM || e->mask & IN_MOVED_TO){
            emit needRefresh();
            return;
        }
    }
}

SmDataGatherer::SmDataGatherer(const int numFields, QObject *parent) : QThread(parent), mNumFields(numFields) {}

void SmDataGatherer::setCurrent(const QString &current, int mode){
    QMutexLocker lock(&mSetMx);
    mCurrent = current;
    mMode = mode;
}

void SmDataGatherer::run(){
    QMutexLocker lock(&mRunMx);
    QFileInfo fi(mCurrent);
    if(fi.isDir()){
        SmTreeItem *rv = populate();
        emit population(rv);
        return;
    }
    if(fi.isFile()){
        QList<QVariant> fd = fileData(fi);
        emit newData(fd, mMode);
    }
}

SmTreeItem * SmDataGatherer::populate(){
    SmTreeItem *retval = new SmTreeItem(mNumFields);
    QDir d = QDir(mCurrent);
    foreach(QFileInfo fi, d.entryInfoList()){
        if(fi.fileName() == "."){
            continue;
        }
        QList<QVariant> data = fileData(fi);
        SmTreeItem *newItem = new SmTreeItem(data, retval);
        retval->appendChild(newItem);
    }
    return retval;
}

const QList<QVariant> SmDataGatherer::fileData(const QFileInfo &fi) const{
    QList<QVariant> data;
    data << fi.fileName() << fi.size();
    QString mime = Helper::mimeType(fi.absoluteFilePath());
    data << mime;
    data << fi.lastModified();
    QList<QVariant> si = QList<QVariant>() << QVariant() << QVariant();
    if(mime.startsWith("video")){
        si = Helper::duration(fi.absoluteFilePath());
    }
    data << Helper::md5Sum(fi.absoluteFilePath());
    data << si << fi.absoluteFilePath();
    return data;
}