comparison src/fswatcher.h @ 538:bdc9de794839 fswatcher

Start separate FsWatcher class to manage filesystem watch policy
author Chris Cannam
date Fri, 10 Feb 2012 17:53:04 +0000
parents
children 3935a7e621ca
comparison
equal deleted inserted replaced
537:a4e699d32a9a 538:bdc9de794839
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 EasyMercurial
5
6 Based on HgExplorer by Jari Korhonen
7 Copyright (c) 2010 Jari Korhonen
8 Copyright (c) 2011 Chris Cannam
9 Copyright (c) 2011 Queen Mary, University of London
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version. See the file
15 COPYING included with this distribution for more information.
16 */
17
18 #ifndef FSWATCHER_H
19 #define FSWATCHER_H
20
21 #include <QObject>
22 #include <QMutex>
23 #include <QString>
24 #include <QSet>
25 #include <QHash>
26 #include <QMap>
27 #include <QStringList>
28 #include <QFileSystemWatcher>
29
30 class FsWatcher : public QObject
31 {
32 Q_OBJECT
33
34 public:
35 FsWatcher();
36 virtual ~FsWatcher();
37
38 /**
39 * Set the root path of the work directory to be monitored.
40 */
41 void setWorkDirPath(QString path);
42
43 /**
44 * Provide a set of prefixes to ignore. Files whose names start
45 * with a prefix in this set will be ignored when they change.
46 */
47 void setIgnoredFilePrefixes(QStringList prefixes);
48
49 /**
50 * Provide a set of suffixes to ignore. Files whose names end
51 * with a suffix in this set will be ignored when they change.
52 */
53 void setIgnoredFileSuffixes(QStringList suffixes);
54
55 /**
56 * Return a token to identify the current caller in subsequent
57 * calls to getChangedPaths(). Only changes that occur after this
58 * has been called can be detected by the caller.
59 */
60 int getNewToken();
61
62 /**
63 * Return a list of all non-ignored file paths that have been
64 * observed to have changed since the last call to getChangedPaths
65 * with the same token.
66 */
67 QSet<QString> getChangedPaths(int token);
68
69 signals:
70 /**
71 * Emitted when something has changed. Use the asynchronous
72 * interface to find out what.
73 */
74 void changed();
75
76 private slots:
77 void fsDirectoryChanged(QString);
78 void fsFileChanged(QString);
79
80 private:
81 // call with lock already held
82 void addWorkDirectory(QString path);
83
84 // call with lock already held
85 bool shouldIgnore(QString path);
86
87 private:
88 /**
89 * A change associates a filename with a counter (the
90 * size_t). Each time a file is changed, its counter is assigned
91 * from m_lastCounter and m_lastCounter is incremented.
92 *
93 * This is not especially efficient -- we often want to find "all
94 * files whose counter is greater than X" which involves a
95 * traversal. Maybe something better later.
96 */
97 QHash<QString, size_t> m_changes;
98
99 /**
100 * Associates a token (the client identifier) with a counter. The
101 * counter represents the value of m_lastCounter at the moment
102 * getChangedPaths last completed for that token. Any files in
103 * m_changes whose counters are larger must have been modified.
104 */
105 QMap<int, size_t> m_tokenMap;
106
107 QStringList m_ignoredPrefixes;
108 QStringList m_ignoredSuffixes;
109
110 /// Everything in this class is synchronised.
111 QMutex m_mutex;
112
113 QString m_workDirPath;
114 int m_lastToken;
115 size_t m_lastCounter;
116 QFileSystemWatcher m_watcher;
117 };
118
119 #endif