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,
47 wxFSW_EVENT_ATTRIB
= 0x20, // Currently this is wxGTK-only
50 wxFSW_EVENT_WARNING
= 0x40,
51 wxFSW_EVENT_ERROR
= 0x80,
52 wxFSW_EVENT_ALL
= wxFSW_EVENT_CREATE
| wxFSW_EVENT_DELETE
|
53 wxFSW_EVENT_RENAME
| wxFSW_EVENT_MODIFY
|
54 wxFSW_EVENT_ACCESS
| wxFSW_EVENT_ATTRIB
|
55 wxFSW_EVENT_WARNING
| wxFSW_EVENT_ERROR
57 ,wxFSW_EVENT_UNMOUNT
= 0x2000
61 // Type of the path watched, used only internally for now.
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.
72 * Event containing information about file system change.
74 class WXDLLIMPEXP_FWD_BASE wxFileSystemWatcherEvent
;
75 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_BASE
, wxEVT_FSWATCHER
,
76 wxFileSystemWatcherEvent
);
78 class WXDLLIMPEXP_BASE wxFileSystemWatcherEvent
: public wxEvent
81 wxFileSystemWatcherEvent(int changeType
= 0, int watchid
= wxID_ANY
) :
82 wxEvent(watchid
, wxEVT_FSWATCHER
),
83 m_changeType(changeType
)
87 wxFileSystemWatcherEvent(int changeType
, const wxString
& errorMsg
,
88 int watchid
= wxID_ANY
) :
89 wxEvent(watchid
, wxEVT_FSWATCHER
),
90 m_changeType(changeType
), m_errorMsg(errorMsg
)
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
)
103 * Returns the path at which the event occurred.
105 const wxFileName
& GetPath() const
111 * Sets the path at which the event occurred
113 void SetPath(const wxFileName
& path
)
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().
123 const wxFileName
& GetNewPath() const
129 * Sets the new path related to the event. See above.
131 void SetNewPath(const wxFileName
& path
)
137 * Returns the type of file system event that occurred.
139 int GetChangeType() const
144 virtual wxEvent
* Clone() const
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());
153 virtual wxEventCategory
GetEventCategory() const
155 // TODO this has to be merged with "similar" categories and changed
156 return wxEVT_CATEGORY_UNKNOWN
;
160 * Returns if this error is an error event
164 return (m_changeType
& (wxFSW_EVENT_ERROR
| wxFSW_EVENT_WARNING
)) != 0;
167 wxString
GetErrorDescription() const
173 * Returns a wxString describing an event useful for debugging or testing
175 wxString
ToString() const;
180 wxFileName m_newPath
;
183 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxFileSystemWatcherEvent
)
186 typedef void (wxEvtHandler::*wxFileSystemWatcherEventFunction
)
187 (wxFileSystemWatcherEvent
&);
189 #define wxFileSystemWatcherEventHandler(func) \
190 wxEVENT_HANDLER_CAST(wxFileSystemWatcherEventFunction, func)
192 #define EVT_FSWATCHER(winid, func) \
193 wx__DECLARE_EVT1(wxEVT_FSWATCHER, winid, wxFileSystemWatcherEventHandler(func))
195 // ----------------------------------------------------------------------------
196 // wxFileSystemWatcherBase: interface for wxFileSystemWatcher
197 // ----------------------------------------------------------------------------
199 // Simple container to store information about one watched path.
204 m_events(-1), m_type(wxFSWPath_None
), m_refcount(-1)
208 wxFSWatchInfo(const wxString
& path
,
211 const wxString
& filespec
= wxString()) :
212 m_path(path
), m_filespec(filespec
), m_events(events
), m_type(type
),
217 const wxString
& GetPath() const
222 const wxString
& GetFilespec() const { return m_filespec
; }
229 wxFSWPathType
GetType() const
234 // Reference counting of watch entries is used to avoid watching the same
235 // file system path multiple times (this can happen even accidentally, e.g.
236 // when you have a recursive watch and then decide to watch some file or
237 // directory under it separately).
245 wxASSERT_MSG( m_refcount
> 0, wxS("Trying to decrement a zero count") );
251 wxString m_filespec
; // For tree watches, holds any filespec to apply
253 wxFSWPathType m_type
;
257 WX_DECLARE_STRING_HASH_MAP(wxFSWatchInfo
, wxFSWatchInfoMap
);
260 * Encapsulation of platform-specific file system event mechanism
262 class wxFSWatcherImpl
;
265 * Main entry point for clients interested in file system events.
266 * Defines interface that can be used to receive that kind of events.
268 class WXDLLIMPEXP_BASE wxFileSystemWatcherBase
: public wxEvtHandler
271 wxFileSystemWatcherBase();
273 virtual ~wxFileSystemWatcherBase();
276 * Adds path to currently watched files. Any events concerning this
277 * particular path will be sent to handler. Optionally a filter can be
278 * specified to receive only events of particular type.
280 * Please note that when adding a dir, immediate children will be watched
283 virtual bool Add(const wxFileName
& path
, int events
= wxFSW_EVENT_ALL
);
286 * Like above, but recursively adds every file/dir in the tree rooted in
287 * path. Additionally a file mask can be specified to include only files
288 * of particular type.
290 virtual bool AddTree(const wxFileName
& path
, int events
= wxFSW_EVENT_ALL
,
291 const wxString
& filespec
= wxEmptyString
);
294 * Removes path from the list of watched paths.
296 virtual bool Remove(const wxFileName
& path
);
299 * Same as above, but also removes every file belonging to the tree rooted
302 virtual bool RemoveTree(const wxFileName
& path
);
305 * Clears the list of currently watched paths.
307 virtual bool RemoveAll();
310 * Returns the number of watched paths
312 int GetWatchedPathsCount() const;
315 * Retrevies all watched paths and places them in wxArrayString. Returns
316 * the number of paths.
318 * TODO think about API here: we need to return more information (like is
319 * the path watched recursively)
321 int GetWatchedPaths(wxArrayString
* paths
) const;
323 wxEvtHandler
* GetOwner() const
328 void SetOwner(wxEvtHandler
* handler
)
337 // This is a semi-private function used by wxWidgets itself only.
339 // Delegates the real work of adding the path to wxFSWatcherImpl::Add() and
340 // updates m_watches if the new path was successfully added.
341 bool AddAny(const wxFileName
& path
, int events
, wxFSWPathType type
,
342 const wxString
& filespec
= wxString());
346 static wxString
GetCanonicalPath(const wxFileName
& path
)
348 wxFileName path_copy
= wxFileName(path
);
349 if ( !path_copy
.Normalize() )
351 wxFAIL_MSG(wxString::Format("Unable to normalize path '%s'",
352 path
.GetFullPath()));
353 return wxEmptyString
;
356 return path_copy
.GetFullPath();
360 wxFSWatchInfoMap m_watches
; // path=>wxFSWatchInfo map
361 wxFSWatcherImpl
* m_service
; // file system events service
362 wxEvtHandler
* m_owner
; // handler for file system events
364 friend class wxFSWatcherImpl
;
367 // include the platform specific file defining wxFileSystemWatcher
368 // inheriting from wxFileSystemWatcherBase
371 #include "wx/unix/fswatcher_inotify.h"
372 #define wxFileSystemWatcher wxInotifyFileSystemWatcher
373 #elif defined(wxHAS_KQUEUE)
374 #include "wx/unix/fswatcher_kqueue.h"
375 #define wxFileSystemWatcher wxKqueueFileSystemWatcher
376 #elif defined(__WINDOWS__)
377 #include "wx/msw/fswatcher.h"
378 #define wxFileSystemWatcher wxMSWFileSystemWatcher
380 #include "wx/generic/fswatcher.h"
381 #define wxFileSystemWatcher wxPollingFileSystemWatcher
384 #endif // wxUSE_FSWATCHER
386 #endif /* _WX_FSWATCHER_BASE_H_ */