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
|
--- src/mymodel.cpp.orig 2012-07-11 18:21:24.302333223 -0500
+++ src/mymodel.cpp 2012-07-11 18:59:06.428956406 -0500
@@ -21,7 +21,6 @@
#include <mainwindow.h>
#include "mymodel.h"
-#include <sys/inotify.h>
#include <unistd.h>
#include <sys/ioctl.h>
@@ -63,10 +62,8 @@ myModel::myModel(bool realMime)
iconFactory = new QFileIconProvider();
- inotifyFD = inotify_init();
- notifier = new QSocketNotifier(inotifyFD, QSocketNotifier::Read, this);
- connect(notifier, SIGNAL(activated(int)), this, SLOT(notifyChange()));
- connect(&eventTimer,SIGNAL(timeout()),this,SLOT(eventTimeout()));
+ watcher = new QFileSystemWatcher(this);
+ connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(notifyChange(QString)));
realMimeTypes = realMime;
}
@@ -189,62 +186,9 @@ QString myModel::getMimeType(const QMode
}
//---------------------------------------------------------------------------------------
-void myModel::notifyChange()
+void myModel::notifyChange(QString const& path)
{
- notifier->setEnabled(0);
-
- int buffSize = 0;
- ioctl(inotifyFD, FIONREAD, (char *) &buffSize);
-
- QByteArray buffer;
- buffer.resize(buffSize);
- read(inotifyFD,buffer.data(),buffSize);
- const char *at = buffer.data();
- const char * const end = at + buffSize;
-
- while (at < end)
- {
- const inotify_event *event = reinterpret_cast<const inotify_event *>(at);
-
- int w = event->wd;
-
- if(eventTimer.isActive())
- {
- if(w == lastEventID)
- eventTimer.start(40);
- else
- {
- eventTimer.stop();
- notifyProcess(lastEventID);
- lastEventID = w;
- eventTimer.start(40);
- }
- }
- else
- {
- lastEventID = w;
- eventTimer.start(40);
- }
-
- at += sizeof(inotify_event) + event->len;
- }
-
- notifier->setEnabled(1);
-}
-
-//---------------------------------------------------------------------------------------
-void myModel::eventTimeout()
-{
- notifyProcess(lastEventID);
- eventTimer.stop();
-}
-
-//---------------------------------------------------------------------------------------
-void myModel::notifyProcess(int eventID)
-{
- if(watchers.contains(eventID))
- {
- myModelItem *parent = rootItem->matchPath(watchers.value(eventID).split(SEPARATOR));
+ myModelItem *parent = rootItem->matchPath(path.split(SEPARATOR));
if(parent)
{
@@ -265,9 +209,7 @@ void myModel::notifyProcess(int eventID)
//must of been deleted, remove from model
if(child->fileInfo().isDir())
{
- int wd = watchers.key(child->absoluteFilePath());
- inotify_rm_watch(inotifyFD,wd);
- watchers.remove(wd);
+ watcher->removePath(child->absoluteFilePath());
}
beginRemoveRows(index(parent->absoluteFilePath()),child->childNumber(),child->childNumber());
parent->removeChild(child);
@@ -282,20 +224,19 @@ void myModel::notifyProcess(int eventID)
endInsertRows();
}
}
- }
- else
- {
- inotify_rm_watch(inotifyFD,eventID);
- watchers.remove(eventID);
- }
+ else
+ {
+ watcher->removePath(path);
+ }
}
//---------------------------------------------------------------------------------
-void myModel::addWatcher(myModelItem *item)
+void myModel::addToWatcher(myModelItem *item)
{
while(item != rootItem)
{
- watchers.insert(inotify_add_watch(inotifyFD, item->absoluteFilePath().toLocal8Bit(), IN_MOVE | IN_CREATE | IN_DELETE),item->absoluteFilePath()); //IN_ONESHOT | IN_ALL_EVENTS)
+ if (!watcher->directories().contains(item->absoluteFilePath()))
+ watcher->addPath(item->absoluteFilePath());
item->watched = 1;
item = item->parent();
}
@@ -308,7 +249,7 @@ bool myModel::setRootPath(const QString&
myModelItem *item = rootItem->matchPath(path.split(SEPARATOR));
- if(item->watched == 0) addWatcher(item);
+ if(item->watched == 0) addToWatcher(item);
if(item->walked == 0)
{
@@ -382,10 +323,7 @@ void myModel::refresh()
{
myModelItem *item = rootItem->matchPath(QStringList("/"));
- //free all inotify watches
- foreach(int w, watchers.keys())
- inotify_rm_watch(inotifyFD,w);
- watchers.clear();
+ watcher->removePaths(watcher->directories());
beginResetModel();
item->clearAll();
@@ -840,9 +778,7 @@ bool myModel::remove(const QModelIndex &
QFileInfo info(children.at(i));
if(info.isDir())
{
- int wd = watchers.key(info.filePath());
- inotify_rm_watch(inotifyFD,wd);
- watchers.remove(wd);
+ watcher->removePath(info.filePath());
error |= QDir().rmdir(info.filePath());
}
else error |= QFile::remove(info.filePath());
|