1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/fswatcher.h
3 // Purpose: wxFileSystemWatcherBase
4 // Author: Bartosz Bekier
7 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_FSWATCHER_BASE_H_
12 #define _WX_FSWATCHER_BASE_H_
20 #include "wx/evtloop.h"
21 #include "wx/filename.h"
23 #include "wx/hashmap.h"
25 #define wxTRACE_FSWATCHER "fswatcher"
27 // ----------------------------------------------------------------------------
28 // wxFileSystemWatcherEventType & wxFileSystemWatcherEvent
29 // ----------------------------------------------------------------------------
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).
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
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,
49 wxFSW_EVENT_WARNING
= 0x20,
50 wxFSW_EVENT_ERROR
= 0x40,
52 wxFSW_EVENT_ALL
= wxFSW_EVENT_CREATE
| wxFSW_EVENT_DELETE
|
53 wxFSW_EVENT_RENAME
| wxFSW_EVENT_MODIFY
|
55 wxFSW_EVENT_WARNING
| wxFSW_EVENT_ERROR
59 * Event containing information about file system change.
61 class WXDLLIMPEXP_FWD_BASE wxFileSystemWatcherEvent
;
62 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE
, wxEVT_FSWATCHER
,
63 wxFileSystemWatcherEvent
);
65 class WXDLLIMPEXP_BASE wxFileSystemWatcherEvent
: public wxEvent
68 wxFileSystemWatcherEvent(int changeType
, int watchid
= wxID_ANY
) :
69 wxEvent(watchid
, wxEVT_FSWATCHER
),
70 m_changeType(changeType
)
74 wxFileSystemWatcherEvent(int changeType
, const wxString
& errorMsg
,
75 int watchid
= wxID_ANY
) :
76 wxEvent(watchid
, wxEVT_FSWATCHER
),
77 m_changeType(changeType
), m_errorMsg(errorMsg
)
81 wxFileSystemWatcherEvent(int changeType
,
82 const wxFileName
& path
, const wxFileName
& newPath
,
83 int watchid
= wxID_ANY
) :
84 wxEvent(watchid
, wxEVT_FSWATCHER
),
85 m_changeType(changeType
), m_path(path
), m_newPath(newPath
)
90 * Returns the path at which the event occurred.
92 const wxFileName
& GetPath() const
98 * Sets the path at which the event occurred
100 void SetPath(const wxFileName
& path
)
106 * In case of rename(move?) events, returns the new path related to the
107 * event. The "new" means newer in the sense of time. In case of other
108 * events it returns the same path as GetPath().
110 const wxFileName
& GetNewPath() const
116 * Sets the new path related to the event. See above.
118 void SetNewPath(const wxFileName
& path
)
124 * Returns the type of file system event that occurred.
126 int GetChangeType() const
131 virtual wxEvent
* Clone() const
133 wxFileSystemWatcherEvent
* evt
= new wxFileSystemWatcherEvent(*this);
134 evt
->m_errorMsg
= m_errorMsg
.Clone();
135 evt
->m_path
= wxFileName(m_path
.GetFullPath().Clone());
136 evt
->m_newPath
= wxFileName(m_newPath
.GetFullPath().Clone());
140 virtual wxEventCategory
GetEventCategory() const
142 // TODO this has to be merged with "similiar" categories and changed
143 return wxEVT_CATEGORY_UNKNOWN
;
147 * Returns if this error is an error event
151 return (m_changeType
& (wxFSW_EVENT_ERROR
| wxFSW_EVENT_WARNING
)) != 0;
154 wxString
GetErrorDescription() const
160 * Returns a wxString describing an event useful for debugging or testing
162 wxString
ToString() const;
167 wxFileName m_newPath
;
171 typedef void (wxEvtHandler::*wxFileSystemWatcherEventFunction
)
172 (wxFileSystemWatcherEvent
&);
174 #define wxFileSystemWatcherEventHandler(func) \
175 wxEVENT_HANDLER_CAST(wxFileSystemWatcherEventFunction, func)
178 // ----------------------------------------------------------------------------
179 // wxFileSystemWatcherBase: interface for wxFileSystemWatcher
180 // ----------------------------------------------------------------------------
183 * Simple container to store information about one watched file
189 m_path(wxEmptyString
), m_events(-1)
193 wxFSWatchInfo(const wxString
& path
, int events
) :
194 m_path(path
), m_events(events
)
198 const wxString
& GetPath() const
213 WX_DECLARE_STRING_HASH_MAP(wxFSWatchInfo
, wxFSWatchInfoMap
);
216 * Encapsulation of platform-specific file system event mechanism
218 class wxFSWatcherImpl
;
221 * Main entry point for clients interested in file system events.
222 * Defines interface that can be used to receive that kind of events.
224 class WXDLLIMPEXP_BASE wxFileSystemWatcherBase
: public wxEvtHandler
227 wxFileSystemWatcherBase();
229 virtual ~wxFileSystemWatcherBase();
232 * Adds path to currently watched files. Any events concerning this
233 * particular path will be sent to handler. Optionally a filter can be
234 * specified to receive only events of particular type.
236 * Please note that when adding a dir, immediate children will be watched
239 virtual bool Add(const wxFileName
& path
, int events
= wxFSW_EVENT_ALL
);
242 * Like above, but recursively adds every file/dir in the tree rooted in
243 * path. Additionally a file mask can be specified to include only files
244 * of particular type.
246 virtual bool AddTree(const wxFileName
& path
, int events
= wxFSW_EVENT_ALL
,
247 const wxString
& filter
= wxEmptyString
);
250 * Removes path from the list of watched paths.
252 virtual bool Remove(const wxFileName
& path
);
255 * Same as above, but also removes every file belonging to the tree rooted
258 virtual bool RemoveTree(const wxFileName
& path
);
261 * Clears the list of currently watched paths.
263 virtual bool RemoveAll();
266 * Returns the number of watched paths
268 int GetWatchedPathsCount() const;
271 * Retrevies all watched paths and places them in wxArrayString. Returns
272 * the number of paths.
274 * TODO think about API here: we need to return more information (like is
275 * the path watched recursively)
277 int GetWatchedPaths(wxArrayString
* paths
) const;
279 wxEvtHandler
* GetOwner() const
284 void SetOwner(wxEvtHandler
* handler
)
294 static wxString
GetCanonicalPath(const wxFileName
& path
)
296 wxFileName path_copy
= wxFileName(path
);
297 if ( !path_copy
.Normalize() )
299 wxFAIL_MSG(wxString::Format("Unable to normalize path '%s'",
300 path
.GetFullPath()));
301 return wxEmptyString
;
304 return path_copy
.GetFullPath();
307 wxFSWatchInfoMap m_watches
; // path=>wxFSWatchInfo map
308 wxFSWatcherImpl
* m_service
; // file system events service
309 wxEvtHandler
* m_owner
; // handler for file system events
311 friend class wxFSWatcherImpl
;
314 // include the platform specific file defining wxFileSystemWatcher
315 // inheriting from wxFileSystemWatcherBase
318 #include "wx/unix/fswatcher_inotify.h"
319 #define wxFileSystemWatcher wxInotifyFileSystemWatcher
320 #elif defined(wxHAS_KQUEUE)
321 #include "wx/unix/fswatcher_kqueue.h"
322 #define wxFileSystemWatcher wxKqueueFileSystemWatcher
323 #elif defined(__WXMSW__)
324 #include "wx/msw/fswatcher.h"
325 #define wxFileSystemWatcher wxMSWFileSystemWatcher
327 #include "wx/generic/fswatcher.h"
328 #define wxFileSystemWatcher wxPollingFileSystemWatcher
331 #endif // wxUSE_FSWATCHER
333 #endif /* _WX_FSWATCHER_BASE_H_ */