Improve handling of file spec in wxFileSystemWatcher::AddTree().
[wxWidgets.git] / include / wx / fswatcher.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/fswatcher.h
3 // Purpose: wxFileSystemWatcherBase
4 // Author: Bartosz Bekier
5 // Created: 2009-05-23
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_FSWATCHER_BASE_H_
12 #define _WX_FSWATCHER_BASE_H_
13
14 #include "wx/defs.h"
15
16 #if wxUSE_FSWATCHER
17
18 #include "wx/log.h"
19 #include "wx/event.h"
20 #include "wx/evtloop.h"
21 #include "wx/filename.h"
22 #include "wx/dir.h"
23 #include "wx/hashmap.h"
24
25 #define wxTRACE_FSWATCHER "fswatcher"
26
27 // ----------------------------------------------------------------------------
28 // wxFileSystemWatcherEventType & wxFileSystemWatcherEvent
29 // ----------------------------------------------------------------------------
30
31 /**
32 * Possible types of file system events.
33 * This is a subset that will work fine an all platforms (actually, we will
34 * see how it works on Mac).
35 *
36 * We got 2 types of error events:
37 * - warning: these are not fatal and further events can still be generated
38 * - error: indicates fatal error and causes that no more events will happen
39 */
40 enum
41 {
42 wxFSW_EVENT_CREATE = 0x01,
43 wxFSW_EVENT_DELETE = 0x02,
44 wxFSW_EVENT_RENAME = 0x04,
45 wxFSW_EVENT_MODIFY = 0x08,
46 wxFSW_EVENT_ACCESS = 0x10,
47
48 // error events
49 wxFSW_EVENT_WARNING = 0x20,
50 wxFSW_EVENT_ERROR = 0x40,
51
52 wxFSW_EVENT_ALL = wxFSW_EVENT_CREATE | wxFSW_EVENT_DELETE |
53 wxFSW_EVENT_RENAME | wxFSW_EVENT_MODIFY |
54 wxFSW_EVENT_ACCESS |
55 wxFSW_EVENT_WARNING | wxFSW_EVENT_ERROR
56 };
57
58 // Type of the path watched, used only internally for now.
59 enum wxFSWPathType
60 {
61 wxFSWPath_None, // Invalid value for an initialized watch.
62 wxFSWPath_File, // Plain file.
63 wxFSWPath_Dir, // Watch a directory and the files in it.
64 wxFSWPath_Tree // Watch a directory and all its children recursively.
65 };
66
67
68 /**
69 * Event containing information about file system change.
70 */
71 class WXDLLIMPEXP_FWD_BASE wxFileSystemWatcherEvent;
72 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_FSWATCHER,
73 wxFileSystemWatcherEvent);
74
75 class WXDLLIMPEXP_BASE wxFileSystemWatcherEvent: public wxEvent
76 {
77 public:
78 wxFileSystemWatcherEvent(int changeType, int watchid = wxID_ANY) :
79 wxEvent(watchid, wxEVT_FSWATCHER),
80 m_changeType(changeType)
81 {
82 }
83
84 wxFileSystemWatcherEvent(int changeType, const wxString& errorMsg,
85 int watchid = wxID_ANY) :
86 wxEvent(watchid, wxEVT_FSWATCHER),
87 m_changeType(changeType), m_errorMsg(errorMsg)
88 {
89 }
90
91 wxFileSystemWatcherEvent(int changeType,
92 const wxFileName& path, const wxFileName& newPath,
93 int watchid = wxID_ANY) :
94 wxEvent(watchid, wxEVT_FSWATCHER),
95 m_changeType(changeType), m_path(path), m_newPath(newPath)
96 {
97 }
98
99 /**
100 * Returns the path at which the event occurred.
101 */
102 const wxFileName& GetPath() const
103 {
104 return m_path;
105 }
106
107 /**
108 * Sets the path at which the event occurred
109 */
110 void SetPath(const wxFileName& path)
111 {
112 m_path = path;
113 }
114
115 /**
116 * In case of rename(move?) events, returns the new path related to the
117 * event. The "new" means newer in the sense of time. In case of other
118 * events it returns the same path as GetPath().
119 */
120 const wxFileName& GetNewPath() const
121 {
122 return m_newPath;
123 }
124
125 /**
126 * Sets the new path related to the event. See above.
127 */
128 void SetNewPath(const wxFileName& path)
129 {
130 m_newPath = path;
131 }
132
133 /**
134 * Returns the type of file system event that occurred.
135 */
136 int GetChangeType() const
137 {
138 return m_changeType;
139 }
140
141 virtual wxEvent* Clone() const
142 {
143 wxFileSystemWatcherEvent* evt = new wxFileSystemWatcherEvent(*this);
144 evt->m_errorMsg = m_errorMsg.Clone();
145 evt->m_path = wxFileName(m_path.GetFullPath().Clone());
146 evt->m_newPath = wxFileName(m_newPath.GetFullPath().Clone());
147 return evt;
148 }
149
150 virtual wxEventCategory GetEventCategory() const
151 {
152 // TODO this has to be merged with "similiar" categories and changed
153 return wxEVT_CATEGORY_UNKNOWN;
154 }
155
156 /**
157 * Returns if this error is an error event
158 */
159 bool IsError() const
160 {
161 return (m_changeType & (wxFSW_EVENT_ERROR | wxFSW_EVENT_WARNING)) != 0;
162 }
163
164 wxString GetErrorDescription() const
165 {
166 return m_errorMsg;
167 }
168
169 /**
170 * Returns a wxString describing an event useful for debugging or testing
171 */
172 wxString ToString() const;
173
174 protected:
175 int m_changeType;
176 wxFileName m_path;
177 wxFileName m_newPath;
178 wxString m_errorMsg;
179 };
180
181 typedef void (wxEvtHandler::*wxFileSystemWatcherEventFunction)
182 (wxFileSystemWatcherEvent&);
183
184 #define wxFileSystemWatcherEventHandler(func) \
185 wxEVENT_HANDLER_CAST(wxFileSystemWatcherEventFunction, func)
186
187 #define EVT_FSWATCHER(winid, func) \
188 wx__DECLARE_EVT1(wxEVT_FSWATCHER, winid, wxFileSystemWatcherEventHandler(func))
189
190 // ----------------------------------------------------------------------------
191 // wxFileSystemWatcherBase: interface for wxFileSystemWatcher
192 // ----------------------------------------------------------------------------
193
194 // Simple container to store information about one watched path.
195 class wxFSWatchInfo
196 {
197 public:
198 wxFSWatchInfo() :
199 m_events(-1), m_type(wxFSWPath_None)
200 {
201 }
202
203 wxFSWatchInfo(const wxString& path,
204 int events,
205 wxFSWPathType type,
206 const wxString& filespec = wxString()) :
207 m_path(path), m_filespec(filespec), m_events(events), m_type(type)
208 {
209 }
210
211 const wxString& GetPath() const
212 {
213 return m_path;
214 }
215
216 const wxString& GetFilespec() const { return m_filespec; }
217
218 int GetFlags() const
219 {
220 return m_events;
221 }
222
223 wxFSWPathType GetType() const
224 {
225 return m_type;
226 }
227
228 protected:
229 wxString m_path;
230 wxString m_filespec; // For tree watches, holds any filespec to apply
231 int m_events;
232 wxFSWPathType m_type;
233 };
234
235 WX_DECLARE_STRING_HASH_MAP(wxFSWatchInfo, wxFSWatchInfoMap);
236
237 /**
238 * Encapsulation of platform-specific file system event mechanism
239 */
240 class wxFSWatcherImpl;
241
242 /**
243 * Main entry point for clients interested in file system events.
244 * Defines interface that can be used to receive that kind of events.
245 */
246 class WXDLLIMPEXP_BASE wxFileSystemWatcherBase: public wxEvtHandler
247 {
248 public:
249 wxFileSystemWatcherBase();
250
251 virtual ~wxFileSystemWatcherBase();
252
253 /**
254 * Adds path to currently watched files. Any events concerning this
255 * particular path will be sent to handler. Optionally a filter can be
256 * specified to receive only events of particular type.
257 *
258 * Please note that when adding a dir, immediate children will be watched
259 * as well.
260 */
261 virtual bool Add(const wxFileName& path, int events = wxFSW_EVENT_ALL);
262
263 /**
264 * Like above, but recursively adds every file/dir in the tree rooted in
265 * path. Additionally a file mask can be specified to include only files
266 * of particular type.
267 */
268 virtual bool AddTree(const wxFileName& path, int events = wxFSW_EVENT_ALL,
269 const wxString& filespec = wxEmptyString);
270
271 /**
272 * Removes path from the list of watched paths.
273 */
274 virtual bool Remove(const wxFileName& path);
275
276 /**
277 * Same as above, but also removes every file belonging to the tree rooted
278 * at path.
279 */
280 virtual bool RemoveTree(const wxFileName& path);
281
282 /**
283 * Clears the list of currently watched paths.
284 */
285 virtual bool RemoveAll();
286
287 /**
288 * Returns the number of watched paths
289 */
290 int GetWatchedPathsCount() const;
291
292 /**
293 * Retrevies all watched paths and places them in wxArrayString. Returns
294 * the number of paths.
295 *
296 * TODO think about API here: we need to return more information (like is
297 * the path watched recursively)
298 */
299 int GetWatchedPaths(wxArrayString* paths) const;
300
301 wxEvtHandler* GetOwner() const
302 {
303 return m_owner;
304 }
305
306 void SetOwner(wxEvtHandler* handler)
307 {
308 if (!handler)
309 m_owner = this;
310 else
311 m_owner = handler;
312 }
313
314
315 // This is a semi-private function used by wxWidgets itself only.
316 //
317 // Delegates the real work of adding the path to wxFSWatcherImpl::Add() and
318 // updates m_watches if the new path was successfully added.
319 bool AddAny(const wxFileName& path, int events, wxFSWPathType type,
320 const wxString& filespec = wxString());
321
322 protected:
323
324 static wxString GetCanonicalPath(const wxFileName& path)
325 {
326 wxFileName path_copy = wxFileName(path);
327 if ( !path_copy.Normalize() )
328 {
329 wxFAIL_MSG(wxString::Format("Unable to normalize path '%s'",
330 path.GetFullPath()));
331 return wxEmptyString;
332 }
333
334 return path_copy.GetFullPath();
335 }
336
337
338 wxFSWatchInfoMap m_watches; // path=>wxFSWatchInfo map
339 wxFSWatcherImpl* m_service; // file system events service
340 wxEvtHandler* m_owner; // handler for file system events
341
342 friend class wxFSWatcherImpl;
343 };
344
345 // include the platform specific file defining wxFileSystemWatcher
346 // inheriting from wxFileSystemWatcherBase
347
348 #ifdef wxHAS_INOTIFY
349 #include "wx/unix/fswatcher_inotify.h"
350 #define wxFileSystemWatcher wxInotifyFileSystemWatcher
351 #elif defined(wxHAS_KQUEUE)
352 #include "wx/unix/fswatcher_kqueue.h"
353 #define wxFileSystemWatcher wxKqueueFileSystemWatcher
354 #elif defined(__WINDOWS__)
355 #include "wx/msw/fswatcher.h"
356 #define wxFileSystemWatcher wxMSWFileSystemWatcher
357 #else
358 #include "wx/generic/fswatcher.h"
359 #define wxFileSystemWatcher wxPollingFileSystemWatcher
360 #endif
361
362 #endif // wxUSE_FSWATCHER
363
364 #endif /* _WX_FSWATCHER_BASE_H_ */