-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystemwatcher.cpp
More file actions
441 lines (378 loc) · 11.9 KB
/
filesystemwatcher.cpp
File metadata and controls
441 lines (378 loc) · 11.9 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "filesystemwatcher.h"
#include <QtCore/QDir>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QMap>
#include <QtCore/QSet>
#include <QtGui/QApplication>
using namespace Utils;
/*! \cond false */
class FileSystemWatcher::Watcher : public QObject
{
Q_OBJECT
private:
Watcher();
public:
~Watcher();
public:
static Watcher *instance();
public:
void registerPaths(const QSet<QString> &paths, const FileSystemWatcher
*pathsOwner);
void unregisterPaths(const QSet<QString> &paths, const FileSystemWatcher
*pathsOwner);
QSet<QString> paths(const FileSystemWatcher *pathsOwner) const;
QSet<QString> activePaths(const FileSystemWatcher *pathsOwner) const;
bool active() const;
private:
QSet<const FileSystemWatcher *> pathOwners(const QString &path) const;
void synchronize();
private slots:
void onPathChanged(const QString &path);
void onFocusChanged(QWidget *old, QWidget *now);
void onPathOwnerDestroyed(QObject *object);
private:
static QPointer<Watcher> m_instance;
QPointer<QFileSystemWatcher> m_watcher;
// path path owners
QMap<QString, QSet<const FileSystemWatcher *> > m_map;
bool m_active;
QSet<QString> m_pathsChanged;
};
/*! \endcond */
/*!
* \class Utils::FileSystemWatcher
* \brief Provides an interface for monitoring file system for modifications
*
* The FileSystemWatcher provides an interface for monitoring files and
* directories for modifications similary to Qt's QFileSystemWatcher. It uses
* single instance of QFileSystemWatcher to save system resources. The biggest
* benefit is that FileSystemWatcher is active only when its application is
* focused.
*
* That mechanism is useful for instance in cases where editor has opened
* document, user changes that document from another editor and then activates
* first editor. Editor asks user whether to reload document modified outside or
* not only after editor is focused, not immediately after the change is made.
*
* Delayed notification when the application is not active helps a lot in cases
* where opened document which is modified outside the application doesn't store
* changed directly to the file but stores data to backup file, deletes original
* file and moves temporary file to regular one (e.g. <em>vim</em> editor).
*/
//! Creates watcher with given \a parent
FileSystemWatcher::FileSystemWatcher(QObject *parent)
: QObject(parent)
{
}
//! Created watched with given \a parent and adds \a paths
/*!
* \sa addPaths
*/
FileSystemWatcher::FileSystemWatcher(const QStringList &paths, QObject *parent)
: QObject(parent)
{
addPaths(paths);
}
//! Destroys the file system watcher
/*!
* All registered paths that were not removed are automatically unregistered.
* Compared to QFileSystemWatcher provided by Qt it is not necessary to take
* care about possible deadlocks on shutdown. It's handled internally.
*
* \sa ~QFileSystemWatcher
*/
FileSystemWatcher::~FileSystemWatcher()
{
}
//! Register new \a path to be watched
/*!
* Simplified method for single \a path given.
*
* \sa addPaths
* \sa removePath
* \sa removePaths
*/
void FileSystemWatcher::addPath(const QString &path)
{
if (path.isEmpty()) {
qWarning("%s: path is empty", Q_FUNC_INFO);
return;
}
addPaths(QStringList(path));
}
//! Register new \a paths to be watched
/*!
* Given \a paths are added to the list of paths that should be watched.
* For paths that do exist watching is started. If file or directory disappears
* active watching is stopped but path is not removed from internal list. Once
* the file/directory is recreated and internal synchronization is done watching
* is started again.
*
* Internal synchronization is done when some path is added/removed or
* application get back lost focus.
*
* Empty list or empty path strings are ignored (warning sent to output).
*
* \sa addPath
* \sa removePath
* \sa removePaths
*/
void FileSystemWatcher::addPaths(const QStringList &paths)
{
if (paths.isEmpty()) {
qWarning("%s: list with paths is empty", Q_FUNC_INFO);
return;
}
QSet<QString> _paths_ = paths.toSet();
if (_paths_.contains("")) {
qWarning("%s: list with paths contains empty paths", Q_FUNC_INFO);
_paths_.remove("");
}
Watcher::instance()->registerPaths(_paths_, this);
}
//! Removes \a path from watcher
/*!
* Simplified method for single \a path given.
*
* \sa addPath
* \sa addPaths
* \sa removePaths
*/
void FileSystemWatcher::removePath(const QString &path)
{
if (path.isEmpty()) {
qWarning("%s: path is empty", Q_FUNC_INFO);
return;
}
removePaths(QStringList(path));
}
//! Removes \a paths from watcher
/*!
* Given \a paths are removed from the list of paths that should be watched.
*
* Empty list or empty path strings are ignored (warning sent to output).
*
* \sa addPath
* \sa addPaths
* \sa removePath
*/
void FileSystemWatcher::removePaths(const QStringList &paths)
{
if (paths.isEmpty()) {
qWarning("%s: list with paths is empty", Q_FUNC_INFO);
return;
}
QSet<QString> _paths_ = paths.toSet();
if (_paths_.contains("")) {
qWarning("%s: list with paths contains empty paths", Q_FUNC_INFO);
_paths_.remove("");
}
Watcher::instance()->unregisterPaths(_paths_, this);
}
//! Returns all registered paths for this watcher
/*!
* Returned list contains all registered paths that should be watched. Those
* paths may or may not exist in file system.
*
* \sa activePaths
*/
QStringList FileSystemWatcher::paths() const
{
return Watcher::instance()->paths(this).toList();
}
//! Returns only active registered paths for this watcher
/*!
* Returned list contains only registered paths that are actively watched. Those
* paths always exists in file system.
*
* \sa paths
*/
QStringList FileSystemWatcher::activePaths() const
{
return Watcher::instance()->activePaths(this).toList();
}
//! Returns current state of the watcher
/*!
* File system watcher is active if one of application windows is active (has
* focus).
*/
bool FileSystemWatcher::active() const
{
return Watcher::instance()->active();
}
/*!
* \fn FileSystemWatcher::pathChanged(const QString &path) const
*
* Signal is emitted only in active state whenever any path returned by
* activePath method is modified, renamed or deleted.
*
* \sa active
* \sa activePaths
*/
/*
* \class Utils::FileSystemWatcher::Watcher
*/
/*! \cond false */
QPointer<FileSystemWatcher::Watcher> FileSystemWatcher::Watcher::m_instance = 0;
FileSystemWatcher::Watcher::Watcher()
: QObject(),
m_active(false)
{
connect(qApp, SIGNAL(focusChanged(QWidget*,QWidget*)),
this, SLOT(onFocusChanged(QWidget*,QWidget*)));
connect(qApp, SIGNAL(aboutToQuit()),
this, SLOT(deleteLater()));
m_watcher = new QFileSystemWatcher(this);
connect(m_watcher, SIGNAL(directoryChanged(QString)),
this, SLOT(onPathChanged(QString)));
connect(m_watcher, SIGNAL(fileChanged(QString)),
this, SLOT(onPathChanged(QString)));
}
FileSystemWatcher::Watcher::~Watcher()
{
m_instance = 0;
delete m_watcher;
m_watcher = 0;
}
FileSystemWatcher::Watcher *FileSystemWatcher::Watcher::instance()
{
if (m_instance == 0)
m_instance = new Watcher;
return m_instance;
}
void FileSystemWatcher::Watcher::registerPaths(const QSet<QString> &paths, const
FileSystemWatcher *pathsOwner)
{
if (this->paths(pathsOwner).isEmpty()) // Register new owner
connect(pathsOwner, SIGNAL(destroyed(QObject*)),
this, SLOT(onPathOwnerDestroyed(QObject*)));
foreach (const QString &path, paths) {
QSet<const FileSystemWatcher *> pathOwners = this->pathOwners(path);
pathOwners.insert(pathsOwner);
m_map[path] = pathOwners;
}
synchronize();
}
void FileSystemWatcher::Watcher::unregisterPaths(const QSet<QString> &paths,
const FileSystemWatcher *pathsOwner)
{
foreach (const QString &path, paths) {
QSet<const FileSystemWatcher *> pathOwners = this->pathOwners(path);
pathOwners.remove(pathsOwner);
if (pathOwners.isEmpty()) {
m_map.remove(path);
m_pathsChanged.remove(path);
}
else {
m_map[path] = pathOwners;
}
}
if (this->paths(pathsOwner).isEmpty()) // Unregister owner
disconnect(pathsOwner, SIGNAL(destroyed(QObject*)),
this, SLOT(onPathOwnerDestroyed(QObject*)));
synchronize();
}
QSet<QString> FileSystemWatcher::Watcher::paths(const FileSystemWatcher
*pathsOwner) const
{
QSet<QString> paths;
const QStringList keys = m_map.keys();
foreach (const QString &path, keys) {
if (m_map.value(path).contains(pathsOwner))
paths.insert(path);
}
return paths;
}
QSet<QString> FileSystemWatcher::Watcher::activePaths(const FileSystemWatcher
*pathsOwner) const
{
const QSet<QString> paths = this->paths(pathsOwner);
const QSet<QString> activeFiles =
paths & m_watcher->files().toSet();
const QSet<QString> activeDirectories =
paths & m_watcher->directories().toSet();
return activeFiles | activeDirectories;
}
bool FileSystemWatcher::Watcher::active() const
{
return m_active;
}
QSet<const FileSystemWatcher *> FileSystemWatcher::Watcher::pathOwners(const
QString &path) const
{
if (m_map.contains(path))
return m_map.value(path);
return QSet<const FileSystemWatcher *>();
}
void FileSystemWatcher::Watcher::synchronize()
{
const QSet<QString> allActivePaths =
m_watcher->files().toSet() | m_watcher->directories().toSet();
const QSet<QString> paths = m_map.keys().toSet();
const QSet<QString> pathsToRemove = allActivePaths - paths;
QSet<QString> pathsToAdd;
foreach (const QString &path, paths) {
if (!allActivePaths.contains(path) && QFileInfo(path).exists())
pathsToAdd.insert(path);
}
if (!pathsToRemove.isEmpty())
m_watcher->removePaths(pathsToRemove.toList());
if (!pathsToAdd.isEmpty())
m_watcher->addPaths(pathsToAdd.toList());
}
void FileSystemWatcher::Watcher::onPathChanged(const QString &path)
{
/*! \todo Qt doesn't remove paths immediately in some cases.
Change back to assert once it is fixed. */
if (!m_map.keys().contains(path))
return;
//Q_ASSERT(m_map.keys().contains(path));
/*! \todo Remove and add again otherwise it doesn't work properly in some
cases. That's a Qt bug. Remove those lines once it is fixed. */
m_watcher->removePath(path);
if (QFileInfo(path).exists())
m_watcher->addPath(path);
if (m_active) {
const QSet<const FileSystemWatcher *> pathOwners =
this->pathOwners(path);
foreach (const FileSystemWatcher *pathOwner, pathOwners) {
emit pathOwner->pathChanged(path);
}
}
else {
m_pathsChanged.insert(path);
}
}
void FileSystemWatcher::Watcher::onFocusChanged(QWidget *old, QWidget *now)
{
Q_UNUSED(old);
Q_UNUSED(now);
if (m_active == (qApp->activeWindow() != 0))
return;
m_active = qApp->activeWindow() != 0;
synchronize();
if (!m_active)
return;
foreach (const QString &path, m_pathsChanged) {
onPathChanged(path);
}
m_pathsChanged.clear();
}
void FileSystemWatcher::Watcher::onPathOwnerDestroyed(QObject *object)
{
const FileSystemWatcher *pathsOwner =
static_cast<const FileSystemWatcher *>(object);
Q_ASSERT(pathsOwner != 0);
const QStringList keys = m_map.keys();
QSet<QString> paths;
foreach (const QString &path, keys) {
QSet<const FileSystemWatcher *> pathOwners = m_map.value(path);
if (pathOwners.contains(pathsOwner))
paths.insert(path);
}
if (!paths.isEmpty())
unregisterPaths(paths, pathsOwner);
synchronize();
}
/*! \endcond */
#include "filesystemwatcher.moc"