]> git.saurik.com Git - wxWidgets.git/blob - src/common/fswatchercmn.cpp
Don't document wxSortedArrayString as deriving from wxArrayString.
[wxWidgets.git] / src / common / fswatchercmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fswatchercmn.cpp
3 // Purpose: wxMswFileSystemWatcher
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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #if wxUSE_FSWATCHER
18
19 #include "wx/fswatcher.h"
20 #include "wx/private/fswatcher.h"
21
22 // ============================================================================
23 // helpers
24 // ============================================================================
25
26 wxDEFINE_EVENT(wxEVT_FSWATCHER, wxFileSystemWatcherEvent);
27
28 static wxString GetFSWEventChangeTypeName(int type)
29 {
30 switch (type)
31 {
32 case wxFSW_EVENT_CREATE:
33 return "CREATE";
34 case wxFSW_EVENT_DELETE:
35 return "DELETE";
36 case wxFSW_EVENT_RENAME:
37 return "RENAME";
38 case wxFSW_EVENT_MODIFY:
39 return "MODIFY";
40 case wxFSW_EVENT_ACCESS:
41 return "ACCESS";
42 case wxFSW_EVENT_ATTRIB: // Currently this is wxGTK-only
43 return "ATTRIBUTE";
44 #ifdef wxHAS_INOTIFY
45 case wxFSW_EVENT_UNMOUNT: // Currently this is wxGTK-only
46 return "UNMOUNT";
47 #endif
48 case wxFSW_EVENT_WARNING:
49 return "WARNING";
50 case wxFSW_EVENT_ERROR:
51 return "ERROR";
52 }
53
54 // should never be reached!
55 wxFAIL_MSG("Unknown change type");
56 return "INVALID_TYPE";
57 }
58
59
60 // ============================================================================
61 // wxFileSystemWatcherEvent implementation
62 // ============================================================================
63
64 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemWatcherEvent, wxEvent);
65
66 wxString wxFileSystemWatcherEvent::ToString() const
67 {
68 return wxString::Format("FSW_EVT type=%d (%s) path='%s'", m_changeType,
69 GetFSWEventChangeTypeName(m_changeType), GetPath().GetFullPath());
70 }
71
72
73 // ============================================================================
74 // wxFileSystemWatcherEvent implementation
75 // ============================================================================
76
77 wxFileSystemWatcherBase::wxFileSystemWatcherBase() :
78 m_service(0), m_owner(this)
79 {
80 }
81
82 wxFileSystemWatcherBase::~wxFileSystemWatcherBase()
83 {
84 RemoveAll();
85 if (m_service)
86 {
87 delete m_service;
88 }
89 }
90
91 bool wxFileSystemWatcherBase::Add(const wxFileName& path, int events)
92 {
93 wxFSWPathType type = wxFSWPath_None;
94 if ( path.FileExists() )
95 {
96 type = wxFSWPath_File;
97 }
98 else if ( path.DirExists() )
99 {
100 type = wxFSWPath_Dir;
101 }
102 else
103 {
104 // Don't overreact to being passed a non-existent item. It may have
105 // only just been deleted, in which case doing nothing is correct
106 wxLogTrace(wxTRACE_FSWATCHER,
107 "Can't monitor non-existent path \"%s\" for changes.",
108 path.GetFullPath());
109 return false;
110 }
111
112 return AddAny(path, events, type);
113 }
114
115 bool
116 wxFileSystemWatcherBase::AddAny(const wxFileName& path,
117 int events,
118 wxFSWPathType type,
119 const wxString& filespec)
120 {
121 wxString canonical = GetCanonicalPath(path);
122 if (canonical.IsEmpty())
123 return false;
124
125 // adding a path in a platform specific way
126 wxFSWatchInfo watch(canonical, events, type, filespec);
127 if ( !m_service->Add(watch) )
128 return false;
129
130 // on success, either add path to our 'watch-list'
131 // or, if already watched, inc the refcount. This may happen if
132 // a dir is Add()ed, then later AddTree() is called on a parent dir
133 wxFSWatchInfoMap::iterator it = m_watches.find(canonical);
134 if ( it == m_watches.end() )
135 {
136 wxFSWatchInfoMap::value_type val(canonical, watch);
137 m_watches.insert(val);
138 }
139 else
140 {
141 wxFSWatchInfo& watch = it->second;
142 int count = watch.IncRef();
143 wxLogTrace(wxTRACE_FSWATCHER,
144 "'%s' is now watched %d times", canonical, count);
145 }
146 return true;
147 }
148
149 bool wxFileSystemWatcherBase::Remove(const wxFileName& path)
150 {
151 // args validation & consistency checks
152 wxString canonical = GetCanonicalPath(path);
153 if (canonical.IsEmpty())
154 return false;
155
156 wxFSWatchInfoMap::iterator it = m_watches.find(canonical);
157 wxCHECK_MSG(it != m_watches.end(), false,
158 wxString::Format("Path '%s' is not watched", canonical));
159
160 // Decrement the watch's refcount and remove from watch-list if 0
161 bool ret = true;
162 wxFSWatchInfo& watch = it->second;
163 if ( !watch.DecRef() )
164 {
165 // remove in a platform specific way
166 ret = m_service->Remove(watch);
167
168 m_watches.erase(it);
169 }
170 return ret;
171 }
172
173 bool wxFileSystemWatcherBase::AddTree(const wxFileName& path, int events,
174 const wxString& filespec)
175 {
176 if (!path.DirExists())
177 return false;
178
179 // OPT could be optimised if we stored information about relationships
180 // between paths
181 class AddTraverser : public wxDirTraverser
182 {
183 public:
184 AddTraverser(wxFileSystemWatcherBase* watcher, int events,
185 const wxString& filespec) :
186 m_watcher(watcher), m_events(events), m_filespec(filespec)
187 {
188 }
189
190 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
191 {
192 // There is no need to watch individual files as we watch the
193 // parent directory which will notify us about any changes in them.
194 return wxDIR_CONTINUE;
195 }
196
197 virtual wxDirTraverseResult OnDir(const wxString& dirname)
198 {
199 if ( m_watcher->AddAny(wxFileName::DirName(dirname),
200 m_events, wxFSWPath_Tree, m_filespec) )
201 {
202 wxLogTrace(wxTRACE_FSWATCHER,
203 "--- AddTree adding directory '%s' ---", dirname);
204 }
205 return wxDIR_CONTINUE;
206 }
207
208 private:
209 wxFileSystemWatcherBase* m_watcher;
210 int m_events;
211 wxString m_filespec;
212 };
213
214 wxDir dir(path.GetFullPath());
215 // Prevent asserts or infinite loops in trees containing symlinks
216 int flags = wxDIR_DIRS;
217 if ( !path.ShouldFollowLink() )
218 {
219 flags |= wxDIR_NO_FOLLOW;
220 }
221 AddTraverser traverser(this, events, filespec);
222 dir.Traverse(traverser, filespec, flags);
223
224 // Add the path itself explicitly as Traverse() doesn't return it.
225 AddAny(path.GetPathWithSep(), events, wxFSWPath_Tree, filespec);
226
227 return true;
228 }
229
230 bool wxFileSystemWatcherBase::RemoveTree(const wxFileName& path)
231 {
232 if (!path.DirExists())
233 return false;
234
235 // OPT could be optimised if we stored information about relationships
236 // between paths
237 class RemoveTraverser : public wxDirTraverser
238 {
239 public:
240 RemoveTraverser(wxFileSystemWatcherBase* watcher,
241 const wxString& filespec) :
242 m_watcher(watcher), m_filespec(filespec)
243 {
244 }
245
246 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
247 {
248 // We never watch the individual files when watching the tree, so
249 // nothing to do here.
250 return wxDIR_CONTINUE;
251 }
252
253 virtual wxDirTraverseResult OnDir(const wxString& dirname)
254 {
255 m_watcher->Remove(wxFileName::DirName(dirname));
256 return wxDIR_CONTINUE;
257 }
258
259 private:
260 wxFileSystemWatcherBase* m_watcher;
261 wxString m_filespec;
262 };
263
264 // If AddTree() used a filespec, we must use the same one
265 wxString canonical = GetCanonicalPath(path);
266 wxFSWatchInfoMap::iterator it = m_watches.find(canonical);
267 wxCHECK_MSG( it != m_watches.end(), false,
268 wxString::Format("Path '%s' is not watched", canonical) );
269 wxFSWatchInfo watch = it->second;
270 const wxString filespec = watch.GetFilespec();
271
272 #if defined(__WINDOWS__)
273 // When there's no filespec, the wxMSW AddTree() would have set a watch
274 // on only the passed 'path'. We must therefore remove only this
275 if (filespec.empty())
276 {
277 return Remove(path);
278 }
279 // Otherwise fall through to the generic implementation
280 #endif // __WINDOWS__
281
282 wxDir dir(path.GetFullPath());
283 // AddTree() might have used the wxDIR_NO_FOLLOW to prevent asserts or
284 // infinite loops in trees containing symlinks. We need to do the same
285 // or we'll try to remove unwatched items. Let's hope the caller used
286 // the same ShouldFollowLink() setting as in AddTree()...
287 int flags = wxDIR_DIRS;
288 if ( !path.ShouldFollowLink() )
289 {
290 flags |= wxDIR_NO_FOLLOW;
291 }
292 RemoveTraverser traverser(this, filespec);
293 dir.Traverse(traverser, filespec, flags);
294
295 // As in AddTree() above, handle the path itself explicitly.
296 Remove(path);
297
298 return true;
299 }
300
301 bool wxFileSystemWatcherBase::RemoveAll()
302 {
303 m_service->RemoveAll();
304 m_watches.clear();
305 return true;
306 }
307
308 int wxFileSystemWatcherBase::GetWatchedPathsCount() const
309 {
310 return m_watches.size();
311 }
312
313 int wxFileSystemWatcherBase::GetWatchedPaths(wxArrayString* paths) const
314 {
315 wxCHECK_MSG( paths != NULL, -1, "Null array passed to retrieve paths");
316
317 wxFSWatchInfoMap::const_iterator it = m_watches.begin();
318 for ( ; it != m_watches.end(); ++it)
319 {
320 paths->push_back(it->first);
321 }
322
323 return m_watches.size();
324 }
325
326 #endif // wxUSE_FSWATCHER