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
|
// event masks
#include <sys/inotify.h>
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QFlags>
#include <QDebug>
#include <QDir>
#include <QMutexLocker>
#include <QStringList>
#include "mirall/inotify.h"
#include "mirall/folderwatcher.h"
static const uint32_t standard_event_mask =
IN_ATTRIB | IN_CLOSE_WRITE | IN_CREATE |
IN_DELETE | IN_DELETE_SELF | IN_MOVED_FROM |
IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR;
namespace Mirall {
enum SubFolderListOption {
SubFolderNoOptions = 0x0,
SubFolderRecursive = 0x1,
};
Q_DECLARE_FLAGS(SubFolderListOptions, SubFolderListOption)
Q_DECLARE_OPERATORS_FOR_FLAGS(SubFolderListOptions)
// Forgive me using a bool as a flag
static QStringList subFoldersList(QString folder,
SubFolderListOptions options = SubFolderNoOptions )
{
QDir dir(folder);
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList list = dir.entryInfoList();
QStringList dirList;
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
dirList << fileInfo.absoluteFilePath();
if (options & SubFolderRecursive )
dirList << subFoldersList(fileInfo.absoluteFilePath(), options);
}
return dirList;
}
FolderWatcher::FolderWatcher(const QString &path, QObject *parent)
: QObject(parent)
{
_watcher = new QFileSystemWatcher(this);
_inotify = new INotify(standard_event_mask);
//_inotify->addPath(path);
// watch the path and all subdirectories
{
QMutexLocker locker(&_mutex);
QStringList subfolders(subFoldersList(path, SubFolderRecursive));
qDebug() << "adding watchers for " << subfolders;
QStringListIterator subfoldersIt(subfolders);
while (subfoldersIt.hasNext()) {
_watcher->addPath(subfoldersIt.next());
_inotify->addPath(subfoldersIt.next());
}
}
// QObject::connect(_watcher, SIGNAL(directoryChanged(const QString &)),
// SLOT(slotDirectoryChanged(const QString &)));
QObject::connect(_inotify, SIGNAL(notifyEvent(int, const QString &)),
SLOT(slotDirectoryChanged(int, const QString &)));
}
FolderWatcher::~FolderWatcher()
{
}
void FolderWatcher::slotDirectoryChanged(int mask, const QString &path)
{
QMutexLocker locker(&_mutex);
qDebug() << mask << " : changed: " << path;
qDebug() << "updating subdirectories";
QStringList watchedFolders(_inotify->directories());
QStringListIterator watchedFoldersIt(watchedFolders);
while (watchedFoldersIt.hasNext()) {
QDir folder (watchedFoldersIt.next());
if (!folder.exists()){
qDebug() << "Removing " << folder.path();
_inotify->removePath(folder.path());
}
}
QStringListIterator subfoldersIt(subFoldersList(path, SubFolderRecursive));
while (subfoldersIt.hasNext()) {
QDir folder (subfoldersIt.next());
if (folder.exists() && !watchedFolders.contains(folder.path())) {
qDebug() << "Adding " << folder.path();
_inotify->addPath(folder.path());
}
// Look if some of the subdirectories disappeared
}
emit folderChanged(path);
}
}
#include "folderwatcher.moc"
|