1 ///////////////////////////////////////////////////////////////////////////// 2 // Name: wx/private/fswatcher.h 3 // Purpose: File system watcher impl classes 4 // Author: Bartosz Bekier 5 // Created: 2009-05-26 6 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com> 7 // Licence: wxWindows licence 8 ///////////////////////////////////////////////////////////////////////////// 9 10 #ifndef WX_PRIVATE_FSWATCHER_H_ 11 #define WX_PRIVATE_FSWATCHER_H_ 12 13 #include "wx/sharedptr.h" 14 15 #ifdef wxHAS_INOTIFY 16 class wxFSWatchEntryUnix; 17 #define wxFSWatchEntry wxFSWatchEntryUnix 18 WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxFSWatchEntry>,wxFSWatchEntries); 19 #include "wx/unix/private/fswatcher_inotify.h" 20 #elif defined(wxHAS_KQUEUE) 21 class wxFSWatchEntryKq; 22 #define wxFSWatchEntry wxFSWatchEntryKq 23 WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxFSWatchEntry>,wxFSWatchEntries); 24 #include "wx/unix/private/fswatcher_kqueue.h" 25 #elif defined(__WINDOWS__) 26 class wxFSWatchEntryMSW; 27 #define wxFSWatchEntry wxFSWatchEntryMSW 28 WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxFSWatchEntry>,wxFSWatchEntries); 29 #include "wx/msw/private/fswatcher.h" 30 #else 31 #define wxFSWatchEntry wxFSWatchEntryPolling 32 #endif 33 34 class wxFSWatcherImpl 35 { 36 public: wxFSWatcherImpl(wxFileSystemWatcherBase * watcher)37 wxFSWatcherImpl(wxFileSystemWatcherBase* watcher) : 38 m_watcher(watcher) 39 { 40 } 41 ~wxFSWatcherImpl()42 virtual ~wxFSWatcherImpl() 43 { 44 (void) RemoveAll(); 45 } 46 47 virtual bool Init() = 0; 48 Add(const wxFSWatchInfo & winfo)49 virtual bool Add(const wxFSWatchInfo& winfo) 50 { 51 if ( m_watches.find(winfo.GetPath()) != m_watches.end() ) 52 { 53 wxLogTrace(wxTRACE_FSWATCHER, 54 "Path '%s' is already watched", winfo.GetPath()); 55 // This can happen if a dir is watched, then a parent tree added 56 return true; 57 } 58 59 // construct watch entry 60 wxSharedPtr<wxFSWatchEntry> watch(new wxFSWatchEntry(winfo)); 61 62 if (!DoAdd(watch)) 63 return false; 64 65 // add watch to our map (always succeedes, checked above) 66 wxFSWatchEntries::value_type val(watch->GetPath(), watch); 67 return m_watches.insert(val).second; 68 } 69 Remove(const wxFSWatchInfo & winfo)70 virtual bool Remove(const wxFSWatchInfo& winfo) 71 { 72 wxFSWatchEntries::iterator it = m_watches.find(winfo.GetPath()); 73 if ( it == m_watches.end() ) 74 { 75 wxLogTrace(wxTRACE_FSWATCHER, 76 "Path '%s' is not watched", winfo.GetPath()); 77 // This can happen if a dir is watched, then a parent tree added 78 return true; 79 } 80 wxSharedPtr<wxFSWatchEntry> watch = it->second; 81 m_watches.erase(it); 82 return DoRemove(watch); 83 } 84 RemoveAll()85 virtual bool RemoveAll() 86 { 87 bool ret = true; 88 for ( wxFSWatchEntries::iterator it = m_watches.begin(); 89 it != m_watches.end(); 90 ++it ) 91 { 92 if ( !DoRemove(it->second) ) 93 ret = false; 94 } 95 m_watches.clear(); 96 return ret; 97 } 98 99 // Check whether any filespec matches the file's ext (if present) MatchesFilespec(const wxFileName & fn,const wxString & filespec)100 bool MatchesFilespec(const wxFileName& fn, const wxString& filespec) const 101 { 102 return filespec.empty() || wxMatchWild(filespec, fn.GetFullName()); 103 } 104 105 protected: 106 virtual bool DoAdd(wxSharedPtr<wxFSWatchEntry> watch) = 0; 107 108 virtual bool DoRemove(wxSharedPtr<wxFSWatchEntry> watch) = 0; 109 110 wxFSWatchEntries m_watches; 111 wxFileSystemWatcherBase* m_watcher; 112 }; 113 114 115 #endif /* WX_PRIVATE_FSWATCHER_H_ */ 116