]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/fswatcher.h
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 There are three different ways to use this class:
29 - You may derive a new class from wxFileSystemWatcher and override the
30 wxFileSystemWatcher::OnChange member to perform the required action
31 when file system change occurrs. Additionally you may also want to
32 override wxFileSystemWatcher::OnWarning and
33 wxFileSystemWatcher::OnError to be notified when an error condition
35 - You may use a derived class and the @c EVT_FSWATCHER macro or
36 wxEvtHandler::Connect to redirect events to an event handler defined in
37 the derived class. If the default constructor is used, the file system
38 watcher object will be its own owner object, since it is derived from
40 - You may redirect the notifications of file system changes as well as of
41 error conditions to any wxEvtHandler derived object by using
42 wxFileSystemWatcher::SetOwner.
43 Then use the @c EVT_FSWATCHER macro or wxEvtHandler::Connect to send the
44 events to the event handler which will receive wxFileSystemWatcherEvent.
49 class MyWatcher : public wxFileSystemWatcher
52 void OnChange(int changeType, const wxFileName& path, const wxFileName& newPath)
54 // do whatever you like with the event
58 class MyApp : public wxApp
62 void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
64 // you have to construct the watcher here, because it needs an active loop
65 m_watcher = new MyWatcher();
67 // please notify me when a new log file is created
68 m_watcher->Add(wxFileName::DirName("/var/log", wxFSW_EVENT_CREATE);
81 class wxFileSystemWatcher
: public wxEvtHandler
85 Default constructor. If you create file system watcher using it you have
86 to either call SetOwner() and connect an event handler or override
87 OnChange(), OnWarning() and OnError().
89 wxFileSystemWatcher();
92 Destructor. Stops all paths from being watched and frees any system
93 resources used by this file system watcher object.
95 virtual ~wxFileSystemWatcher();
98 Adds @a path to currently watched files. Optionally a filter can be
99 specified to receive only events of particular type.
101 Any events concerning this particular path will be sent either to
102 connected handler or passed to OnChange(), OnWarning() or OnError().
104 @note When adding a directory, immediate children will be watched
107 virtual bool Add(const wxFileName
& path
, int events
= wxFSW_EVENT_ALL
);
110 This is the same as Add(), but recursively adds every file/directory in
111 the tree rooted at @a path. Additionally a file mask can be specified to
112 include only files matching that particular mask.
114 virtual bool AddTree(const wxFileName
& path
, int events
= wxFSW_EVENT_ALL
,
115 const wxString
& filter
= wxEmptyString
) = 0;
118 Removes @a path from the list of watched paths.
120 virtual bool Remove(const wxFileName
& path
);
123 Same as Remove(), but also removes every file/directory belonging to
124 the tree rooted at @a path.
126 virtual bool RemoveTree(const wxFileName
& path
);
129 Clears the list of currently watched paths.
131 virtual bool RemoveAll();
134 Returns the number of watched paths
136 int GetWatchedPathCount() const;
139 Retrieves all watched paths and places them in @a paths. Returns
140 the number of watched paths, which is also the number of entries added
143 int GetWatchedPaths(wxArrayString
* paths
) const;
146 Associates the file system watcher with the given @a handler object.
148 Basically this means that all events will be passed to this handler
149 object unless you have change the default behaviour by overriding
150 OnChange(), OnWarning() or OnError().
152 void SetOwner(wxEvtHandler
* handler
);
156 You may either connect your event handler to intercept file system
157 watcher events or override this member and handle them here.
159 Perform whatever action which is to be taken on file system change.
161 virtual void OnChange(int changeType
, const wxFileName
& path
,
162 const wxFileName
& newPath
);
165 You may either connect your event handler to intercept file system
166 watcher events or override this member and handle them here.
168 Perform whatever action which is to be taken when a warning condition
171 virtual void OnWarning(const wxString
& errorMessage
);
174 You may either connect your event handler to intercept file system
175 watcher events or override this member and handle them here.
177 Perform whatever action which is to be taken when an error condition
180 virtual void OnError(const wxString
& errorMessage
);
186 @class wxFileSystemWatcherEvent
188 A class of events sent when a file system event occurs. Types of events
189 reported may vary depending on a platfrom, however all platforms report
190 at least creation of new file/directory and access, modification, move
191 (rename) or deletion of an existing one.
196 @see wxFileSystemWatcher
197 @see @ref overview_events
201 class wxFileSystemWatcherEvent
: public wxEvent
205 Returns the path at which the event occurred.
207 const wxFileName
& GetPath() const;
210 Returns the new path of the renamed file/directory if this is a rename
213 Otherwise it returns the same path as GetPath().
215 const wxFileName
& GetNewPath() const;
218 Returns the type of file system change that occurred. See wxFSWFlags for
219 the list of possible file system change types.
221 int GetChangeType() const;
224 Returns @c true if this error is an error event
226 Error event is an event generated when a warning or error condition
229 bool IsError() const;
232 Return a description of the warning or error if this is an error event.
234 wxString
GetErrorDescription() const;
237 Returns a wxString describing an event, useful for logging, debugging
240 wxString
ToString() const;
245 These are the possible types of file system change events.
246 All of these events are reported on all supported platforms.
252 wxFSW_EVENT_CREATE
= 0x01, ///< File or directory was created
253 wxFSW_EVENT_DELETE
= 0x02, ///< File or directory was deleted
254 wxFSW_EVENT_RENAME
= 0x04, ///< File or directory was renamed
255 wxFSW_EVENT_MODIFY
= 0x08, ///< File or directory was modified
256 wxFSW_EVENT_ACCESS
= 0x10, ///< File or directory was accessed
258 wxFSW_EVENT_WARNING
= 0x20, ///< A warning condition arose.
259 wxFSW_EVENT_ERROR
= 0x40, ///< An error condition arose.
261 wxFSW_EVENT_ALL
= wxFSW_EVENT_CREATE
| wxFSW_EVENT_DELETE
|
262 wxFSW_EVENT_RENAME
| wxFSW_EVENT_MODIFY
|
264 wxFSW_EVENT_WARNING
| wxFSW_EVENT_ERROR