1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/fswatcher.h
3 // Purpose: wxFileSystemWatcher
4 // Author: Bartosz Bekier
7 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 @class wxFileSystemWatcher
14 The wxFileSystemWatcher class allows to receive notifications of file
17 @note Implementation limitations: this class is currently implemented for
18 MSW, OS X and GTK ports but doesn't detect all changes correctly
19 everywhere: under MSW accessing the file is not detected (only
20 modifying it is) and under OS X neither accessing nor modifying is
21 detected (only creating and deleting files is). Moreover, OS X
22 version doesn't currently collapse pairs of create/delete events in a
23 rename event, unlike the other ones.
25 For the full list of change types that are reported see wxFSWFlags.
27 This class notifies the application about the file system changes by
28 sending events of wxFileSystemWatcherEvent class. By default these events
29 are sent to the wxFileSystemWatcher object itself so you can derive from it
30 and use the event table @c EVT_FSWATCHER macro to handle these events in a
31 derived class method. Alternatively, you can use
32 wxFileSystemWatcher::SetOwner() to send the events to another object. Or
33 you could use wxEvtHandler::Connect() with @c wxEVT_FSWATCHER to handle
34 these events in any other object. See the fswatcher sample for an example
35 of the latter approach.
42 class wxFileSystemWatcher
: public wxEvtHandler
48 wxFileSystemWatcher();
51 Destructor. Stops all paths from being watched and frees any system
52 resources used by this file system watcher object.
54 virtual ~wxFileSystemWatcher();
57 Adds @a path to currently watched files.
59 The @a path argument can currently only be a directory and any changes
60 to this directory itself or its immediate children will generate the
61 events. Use AddTree() to monitor the directory recursively.
63 Note that on platforms that use symbolic links, you should consider the
64 possibility that @a path is a symlink. To watch the symlink itself and
65 not its target you may call wxFileName::DontFollowLink() on @a path.
68 The name of the path to watch.
70 An optional filter to receive only events of particular types.
71 This is currently implemented only for GTK.
73 virtual bool Add(const wxFileName
& path
, int events
= wxFSW_EVENT_ALL
);
76 This is the same as Add(), but also recursively adds every
77 file/directory in the tree rooted at @a path.
79 Additionally a file mask can be specified to include only files
80 matching that particular mask.
82 This method is implemented efficiently on MSW, but should be used with
83 care on other platforms for directories with lots of children (e.g. the
84 root directory) as it calls Add() for each subdirectory, potentially
85 creating a lot of watches and taking a long time to execute.
87 Note that on platforms that use symbolic links, you will probably want
88 to have called wxFileName::DontFollowLink on @a path. This is especially
89 important if the symlink targets may themselves be watched.
91 virtual bool AddTree(const wxFileName
& path
, int events
= wxFSW_EVENT_ALL
,
92 const wxString
& filter
= wxEmptyString
);
95 Removes @a path from the list of watched paths.
97 See the comment in Add() about symbolic links. @a path should treat
98 symbolic links in the same way as in the original Add() call.
100 virtual bool Remove(const wxFileName
& path
);
103 This is the same as Remove(), but also removes every file/directory
104 belonging to the tree rooted at @a path.
106 See the comment in AddTree() about symbolic links. @a path should treat
107 symbolic links in the same way as in the original AddTree() call.
109 virtual bool RemoveTree(const wxFileName
& path
);
112 Clears the list of currently watched paths.
114 virtual bool RemoveAll();
117 Returns the number of currently watched paths.
119 @see GetWatchedPaths()
121 int GetWatchedPathsCount() const;
124 Retrieves all watched paths and places them in @a paths. Returns
125 the number of watched paths, which is also the number of entries added
128 int GetWatchedPaths(wxArrayString
* paths
) const;
131 Associates the file system watcher with the given @a handler object.
133 All the events generated by this object will be passed to the specified
136 void SetOwner(wxEvtHandler
* handler
);
142 @class wxFileSystemWatcherEvent
144 A class of events sent when a file system event occurs. Types of events
145 reported may vary depending on a platform, however all platforms report
146 at least creation of new file/directory and access, modification, move
147 (rename) or deletion of an existing one.
152 @see wxFileSystemWatcher
153 @see @ref overview_events
157 class wxFileSystemWatcherEvent
: public wxEvent
160 wxFileSystemWatcherEvent(int changeType
, int watchid
= wxID_ANY
);
161 wxFileSystemWatcherEvent(int changeType
, const wxString
& errorMsg
,
162 int watchid
= wxID_ANY
);
163 wxFileSystemWatcherEvent(int changeType
,
164 const wxFileName
& path
, const wxFileName
& newPath
,
165 int watchid
= wxID_ANY
);
168 Returns the path at which the event occurred.
170 const wxFileName
& GetPath() const;
173 Returns the new path of the renamed file/directory if this is a rename
176 Otherwise it returns the same path as GetPath().
178 const wxFileName
& GetNewPath() const;
181 Returns the type of file system change that occurred. See wxFSWFlags for
182 the list of possible file system change types.
184 int GetChangeType() const;
187 Returns @c true if this error is an error event
189 Error event is an event generated when a warning or error condition
192 bool IsError() const;
195 Return a description of the warning or error if this is an error event.
197 wxString
GetErrorDescription() const;
200 Returns a wxString describing an event, useful for logging, debugging
203 wxString
ToString() const;
206 wxEventType wxEVT_FSWATCHER
;
209 These are the possible types of file system change events.
211 Not all of these events are reported on all platforms currently.
217 /// File or directory was created.
218 wxFSW_EVENT_CREATE
= 0x01,
220 /// File or directory was deleted.
221 wxFSW_EVENT_DELETE
= 0x02,
224 File or directory was renamed.
226 Notice that under MSW this event is sometimes -- although not always --
227 followed by a ::wxFSW_EVENT_MODIFY for the new file.
229 Under OS X this event is currently not detected and instead separate
230 ::wxFSW_EVENT_CREATE and ::wxFSW_EVENT_DELETE events are.
232 wxFSW_EVENT_RENAME
= 0x04,
235 File or directory was modified.
237 Depending on the program doing the file modification, multiple such
238 events can be reported for a single logical file update.
240 Under OS X this event is currently not detected.
242 wxFSW_EVENT_MODIFY
= 0x08,
245 File or directory was accessed.
247 This event is currently only detected under Linux.
249 wxFSW_EVENT_ACCESS
= 0x10,
252 The item's metadata was changed, e.g.\ its permissions or timestamps.
254 This event is currently only detected under Linux.
258 wxFSW_EVENT_ATTRIB
= 0x20,
261 A warning condition arose.
263 This is something that probably needs to be shown to the user in an
264 interactive program as it can indicate a relatively serious problem,
265 e.g. some events could have been missed because of an overflow. But
266 more events will still be coming in the future, unlike for the error
269 wxFSW_EVENT_WARNING
= 0x40,
272 An error condition arose.
274 Errors are fatal, i.e. no more events will be reported after an error
275 and the program can stop watching the directories currently being
278 wxFSW_EVENT_ERROR
= 0x80,
280 wxFSW_EVENT_ALL
= wxFSW_EVENT_CREATE
| wxFSW_EVENT_DELETE
|
281 wxFSW_EVENT_RENAME
| wxFSW_EVENT_MODIFY
|
282 wxFSW_EVENT_ACCESS
| wxFSW_EVENT_ATTRIB
|
283 wxFSW_EVENT_WARNING
| wxFSW_EVENT_ERROR