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