]> git.saurik.com Git - wxWidgets.git/blame - include/wx/fswatcher.h
support SDK < 10.6, fixes #14902
[wxWidgets.git] / include / wx / fswatcher.h
CommitLineData
6b8ef0b3
VZ
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 */
40enum
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,
f31f9900 47 wxFSW_EVENT_ATTRIB = 0x20, // Currently this is wxGTK-only
6b8ef0b3
VZ
48
49 // error events
f31f9900
VZ
50 wxFSW_EVENT_WARNING = 0x40,
51 wxFSW_EVENT_ERROR = 0x80,
6b8ef0b3
VZ
52 wxFSW_EVENT_ALL = wxFSW_EVENT_CREATE | wxFSW_EVENT_DELETE |
53 wxFSW_EVENT_RENAME | wxFSW_EVENT_MODIFY |
f31f9900 54 wxFSW_EVENT_ACCESS | wxFSW_EVENT_ATTRIB |
6b8ef0b3 55 wxFSW_EVENT_WARNING | wxFSW_EVENT_ERROR
092e08a8
VZ
56#ifdef wxHAS_INOTIFY
57 ,wxFSW_EVENT_UNMOUNT = 0x2000
58#endif
6b8ef0b3
VZ
59};
60
f8d37148
VZ
61// Type of the path watched, used only internally for now.
62enum wxFSWPathType
63{
64 wxFSWPath_None, // Invalid value for an initialized watch.
65 wxFSWPath_File, // Plain file.
66 wxFSWPath_Dir, // Watch a directory and the files in it.
67 wxFSWPath_Tree // Watch a directory and all its children recursively.
68};
69
70
6b8ef0b3
VZ
71/**
72 * Event containing information about file system change.
73 */
74class WXDLLIMPEXP_FWD_BASE wxFileSystemWatcherEvent;
75wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE, wxEVT_FSWATCHER,
76 wxFileSystemWatcherEvent);
77
78class WXDLLIMPEXP_BASE wxFileSystemWatcherEvent: public wxEvent
79{
80public:
81 wxFileSystemWatcherEvent(int changeType, int watchid = wxID_ANY) :
82 wxEvent(watchid, wxEVT_FSWATCHER),
83 m_changeType(changeType)
84 {
85 }
86
87 wxFileSystemWatcherEvent(int changeType, const wxString& errorMsg,
88 int watchid = wxID_ANY) :
89 wxEvent(watchid, wxEVT_FSWATCHER),
90 m_changeType(changeType), m_errorMsg(errorMsg)
91 {
92 }
93
94 wxFileSystemWatcherEvent(int changeType,
95 const wxFileName& path, const wxFileName& newPath,
96 int watchid = wxID_ANY) :
97 wxEvent(watchid, wxEVT_FSWATCHER),
98 m_changeType(changeType), m_path(path), m_newPath(newPath)
99 {
100 }
101
102 /**
103 * Returns the path at which the event occurred.
104 */
105 const wxFileName& GetPath() const
106 {
107 return m_path;
108 }
109
110 /**
111 * Sets the path at which the event occurred
112 */
113 void SetPath(const wxFileName& path)
114 {
115 m_path = path;
116 }
117
118 /**
119 * In case of rename(move?) events, returns the new path related to the
120 * event. The "new" means newer in the sense of time. In case of other
121 * events it returns the same path as GetPath().
122 */
123 const wxFileName& GetNewPath() const
124 {
125 return m_newPath;
126 }
127
128 /**
129 * Sets the new path related to the event. See above.
130 */
131 void SetNewPath(const wxFileName& path)
132 {
133 m_newPath = path;
134 }
135
136 /**
137 * Returns the type of file system event that occurred.
138 */
139 int GetChangeType() const
140 {
141 return m_changeType;
142 }
143
144 virtual wxEvent* Clone() const
145 {
146 wxFileSystemWatcherEvent* evt = new wxFileSystemWatcherEvent(*this);
147 evt->m_errorMsg = m_errorMsg.Clone();
148 evt->m_path = wxFileName(m_path.GetFullPath().Clone());
149 evt->m_newPath = wxFileName(m_newPath.GetFullPath().Clone());
150 return evt;
151 }
152
153 virtual wxEventCategory GetEventCategory() const
154 {
155 // TODO this has to be merged with "similiar" categories and changed
156 return wxEVT_CATEGORY_UNKNOWN;
157 }
158
159 /**
160 * Returns if this error is an error event
161 */
162 bool IsError() const
163 {
164 return (m_changeType & (wxFSW_EVENT_ERROR | wxFSW_EVENT_WARNING)) != 0;
165 }
166
167 wxString GetErrorDescription() const
168 {
169 return m_errorMsg;
170 }
171
172 /**
173 * Returns a wxString describing an event useful for debugging or testing
174 */
175 wxString ToString() const;
176
177protected:
178 int m_changeType;
179 wxFileName m_path;
180 wxFileName m_newPath;
181 wxString m_errorMsg;
182};
183
184typedef void (wxEvtHandler::*wxFileSystemWatcherEventFunction)
185 (wxFileSystemWatcherEvent&);
186
187#define wxFileSystemWatcherEventHandler(func) \
188 wxEVENT_HANDLER_CAST(wxFileSystemWatcherEventFunction, func)
189
70ef16f4
VZ
190#define EVT_FSWATCHER(winid, func) \
191 wx__DECLARE_EVT1(wxEVT_FSWATCHER, winid, wxFileSystemWatcherEventHandler(func))
6b8ef0b3
VZ
192
193// ----------------------------------------------------------------------------
194// wxFileSystemWatcherBase: interface for wxFileSystemWatcher
195// ----------------------------------------------------------------------------
196
f8d37148 197// Simple container to store information about one watched path.
6b8ef0b3
VZ
198class wxFSWatchInfo
199{
200public:
201 wxFSWatchInfo() :
76cfd1bf 202 m_events(-1), m_type(wxFSWPath_None), m_refcount(-1)
6b8ef0b3
VZ
203 {
204 }
205
227dee95
VZ
206 wxFSWatchInfo(const wxString& path,
207 int events,
208 wxFSWPathType type,
209 const wxString& filespec = wxString()) :
76cfd1bf
VZ
210 m_path(path), m_filespec(filespec), m_events(events), m_type(type),
211 m_refcount(1)
6b8ef0b3
VZ
212 {
213 }
214
215 const wxString& GetPath() const
216 {
217 return m_path;
218 }
219
227dee95
VZ
220 const wxString& GetFilespec() const { return m_filespec; }
221
6b8ef0b3
VZ
222 int GetFlags() const
223 {
224 return m_events;
225 }
226
f8d37148
VZ
227 wxFSWPathType GetType() const
228 {
229 return m_type;
230 }
231
76cfd1bf
VZ
232 // Reference counting of watch entries is used to avoid watching the same
233 // file system path multiple times (this can happen even accidentally, e.g.
234 // when you have a recursive watch and then decide to watch some file or
235 // directory under it separately).
236 int IncRef()
237 {
238 return ++m_refcount;
239 }
240
241 int DecRef()
242 {
243 wxASSERT_MSG( m_refcount > 0, wxS("Trying to decrement a zero count") );
244 return --m_refcount;
245 }
246
6b8ef0b3
VZ
247protected:
248 wxString m_path;
227dee95 249 wxString m_filespec; // For tree watches, holds any filespec to apply
6b8ef0b3 250 int m_events;
f8d37148 251 wxFSWPathType m_type;
76cfd1bf 252 int m_refcount;
6b8ef0b3
VZ
253};
254
255WX_DECLARE_STRING_HASH_MAP(wxFSWatchInfo, wxFSWatchInfoMap);
256
257/**
258 * Encapsulation of platform-specific file system event mechanism
259 */
260class wxFSWatcherImpl;
261
262/**
263 * Main entry point for clients interested in file system events.
264 * Defines interface that can be used to receive that kind of events.
265 */
266class WXDLLIMPEXP_BASE wxFileSystemWatcherBase: public wxEvtHandler
267{
268public:
269 wxFileSystemWatcherBase();
270
271 virtual ~wxFileSystemWatcherBase();
272
273 /**
274 * Adds path to currently watched files. Any events concerning this
275 * particular path will be sent to handler. Optionally a filter can be
276 * specified to receive only events of particular type.
277 *
278 * Please note that when adding a dir, immediate children will be watched
279 * as well.
280 */
281 virtual bool Add(const wxFileName& path, int events = wxFSW_EVENT_ALL);
282
283 /**
284 * Like above, but recursively adds every file/dir in the tree rooted in
285 * path. Additionally a file mask can be specified to include only files
286 * of particular type.
287 */
288 virtual bool AddTree(const wxFileName& path, int events = wxFSW_EVENT_ALL,
227dee95 289 const wxString& filespec = wxEmptyString);
6b8ef0b3
VZ
290
291 /**
292 * Removes path from the list of watched paths.
293 */
294 virtual bool Remove(const wxFileName& path);
295
296 /**
297 * Same as above, but also removes every file belonging to the tree rooted
298 * at path.
299 */
300 virtual bool RemoveTree(const wxFileName& path);
301
302 /**
303 * Clears the list of currently watched paths.
304 */
305 virtual bool RemoveAll();
306
307 /**
308 * Returns the number of watched paths
309 */
310 int GetWatchedPathsCount() const;
311
312 /**
313 * Retrevies all watched paths and places them in wxArrayString. Returns
314 * the number of paths.
315 *
316 * TODO think about API here: we need to return more information (like is
317 * the path watched recursively)
318 */
319 int GetWatchedPaths(wxArrayString* paths) const;
320
321 wxEvtHandler* GetOwner() const
322 {
323 return m_owner;
324 }
325
326 void SetOwner(wxEvtHandler* handler)
327 {
328 if (!handler)
329 m_owner = this;
330 else
331 m_owner = handler;
332 }
333
3a2b3701
VZ
334
335 // This is a semi-private function used by wxWidgets itself only.
336 //
337 // Delegates the real work of adding the path to wxFSWatcherImpl::Add() and
338 // updates m_watches if the new path was successfully added.
227dee95
VZ
339 bool AddAny(const wxFileName& path, int events, wxFSWPathType type,
340 const wxString& filespec = wxString());
3a2b3701 341
6b8ef0b3
VZ
342protected:
343
344 static wxString GetCanonicalPath(const wxFileName& path)
345 {
346 wxFileName path_copy = wxFileName(path);
347 if ( !path_copy.Normalize() )
348 {
349 wxFAIL_MSG(wxString::Format("Unable to normalize path '%s'",
350 path.GetFullPath()));
351 return wxEmptyString;
352 }
353
354 return path_copy.GetFullPath();
355 }
356
f8d37148 357
6b8ef0b3
VZ
358 wxFSWatchInfoMap m_watches; // path=>wxFSWatchInfo map
359 wxFSWatcherImpl* m_service; // file system events service
360 wxEvtHandler* m_owner; // handler for file system events
361
362 friend class wxFSWatcherImpl;
363};
364
365// include the platform specific file defining wxFileSystemWatcher
366// inheriting from wxFileSystemWatcherBase
367
368#ifdef wxHAS_INOTIFY
369 #include "wx/unix/fswatcher_inotify.h"
370 #define wxFileSystemWatcher wxInotifyFileSystemWatcher
371#elif defined(wxHAS_KQUEUE)
372 #include "wx/unix/fswatcher_kqueue.h"
373 #define wxFileSystemWatcher wxKqueueFileSystemWatcher
d98a58c5 374#elif defined(__WINDOWS__)
6b8ef0b3
VZ
375 #include "wx/msw/fswatcher.h"
376 #define wxFileSystemWatcher wxMSWFileSystemWatcher
377#else
378 #include "wx/generic/fswatcher.h"
379 #define wxFileSystemWatcher wxPollingFileSystemWatcher
380#endif
381
382#endif // wxUSE_FSWATCHER
383
384#endif /* _WX_FSWATCHER_BASE_H_ */