Make wxFileSystemWatcher watch entries reference-counted.
[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 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_FSWATCHER
19
20 #include "wx/fswatcher.h"
21 #include "wx/private/fswatcher.h"
22
23 // ============================================================================
24 // helpers
25 // ============================================================================
26
27 wxDEFINE_EVENT(wxEVT_FSWATCHER, wxFileSystemWatcherEvent);
28
29 static wxString GetFSWEventChangeTypeName(int type)
30 {
31 switch (type)
32 {
33 case wxFSW_EVENT_CREATE:
34 return "CREATE";
35 case wxFSW_EVENT_DELETE:
36 return "DELETE";
37 case wxFSW_EVENT_RENAME:
38 return "RENAME";
39 case wxFSW_EVENT_MODIFY:
40 return "MODIFY";
41 case wxFSW_EVENT_ACCESS:
42 return "ACCESS";
43 }
44
45 // should never be reached!
46 wxFAIL_MSG("Unknown change type");
47 return "INVALID_TYPE";
48 }
49
50
51 // ============================================================================
52 // wxFileSystemWatcherEvent implementation
53 // ============================================================================
54
55 wxString wxFileSystemWatcherEvent::ToString() const
56 {
57 return wxString::Format("FSW_EVT type=%d (%s) path='%s'", m_changeType,
58 GetFSWEventChangeTypeName(m_changeType), GetPath().GetFullPath());
59 }
60
61
62 // ============================================================================
63 // wxFileSystemWatcherEvent implementation
64 // ============================================================================
65
66 wxFileSystemWatcherBase::wxFileSystemWatcherBase() :
67 m_service(0), m_owner(this)
68 {
69 }
70
71 wxFileSystemWatcherBase::~wxFileSystemWatcherBase()
72 {
73 RemoveAll();
74 if (m_service)
75 {
76 delete m_service;
77 }
78 }
79
80 bool wxFileSystemWatcherBase::Add(const wxFileName& path, int events)
81 {
82 wxFSWPathType type = wxFSWPath_None;
83 if ( path.FileExists() )
84 {
85 type = wxFSWPath_File;
86 }
87 else if ( path.DirExists() )
88 {
89 type = wxFSWPath_Dir;
90 }
91 else
92 {
93 wxLogError(_("Can't monitor non-existent path \"%s\" for changes."),
94 path.GetFullPath());
95 return false;
96 }
97
98 return AddAny(path, events, type);
99 }
100
101 bool
102 wxFileSystemWatcherBase::AddAny(const wxFileName& path,
103 int events,
104 wxFSWPathType type,
105 const wxString& filespec)
106 {
107 wxString canonical = GetCanonicalPath(path);
108 if (canonical.IsEmpty())
109 return false;
110
111 // adding a path in a platform specific way
112 wxFSWatchInfo watch(canonical, events, type, filespec);
113 if ( !m_service->Add(watch) )
114 return false;
115
116 // on success, either add path to our 'watch-list'
117 // or, if already watched, inc the refcount. This may happen if
118 // a dir is Add()ed, then later AddTree() is called on a parent dir
119 wxFSWatchInfoMap::iterator it = m_watches.find(canonical);
120 if ( it == m_watches.end() )
121 {
122 wxFSWatchInfoMap::value_type val(canonical, watch);
123 m_watches.insert(val).second;
124 }
125 else
126 {
127 wxFSWatchInfo& watch = it->second;
128 int count = watch.IncRef();
129 wxLogTrace(wxTRACE_FSWATCHER,
130 "'%s' is now watched %d times", canonical, count);
131 }
132 return true;
133 }
134
135 bool wxFileSystemWatcherBase::Remove(const wxFileName& path)
136 {
137 // args validation & consistency checks
138 wxString canonical = GetCanonicalPath(path);
139 if (canonical.IsEmpty())
140 return false;
141
142 wxFSWatchInfoMap::iterator it = m_watches.find(canonical);
143 wxCHECK_MSG(it != m_watches.end(), false,
144 wxString::Format("Path '%s' is not watched", canonical));
145
146 // Decrement the watch's refcount and remove from watch-list if 0
147 bool ret = true;
148 wxFSWatchInfo& watch = it->second;
149 if ( !watch.DecRef() )
150 {
151 // remove in a platform specific way
152 ret = m_service->Remove(watch);
153
154 m_watches.erase(it);
155 }
156 return ret;
157 }
158
159 bool wxFileSystemWatcherBase::AddTree(const wxFileName& path, int events,
160 const wxString& filespec)
161 {
162 if (!path.DirExists())
163 return false;
164
165 // OPT could be optimised if we stored information about relationships
166 // between paths
167 class AddTraverser : public wxDirTraverser
168 {
169 public:
170 AddTraverser(wxFileSystemWatcherBase* watcher, int events,
171 const wxString& filespec) :
172 m_watcher(watcher), m_events(events), m_filespec(filespec)
173 {
174 }
175
176 // CHECK we choose which files to delegate to Add(), maybe we should pass
177 // all of them to Add() and let it choose? this is useful when adding a
178 // file to a dir that is already watched, then not only should we know
179 // about that, but Add() should also behave well then
180 virtual wxDirTraverseResult OnFile(const wxString& filename)
181 {
182 if ( m_watcher->AddAny(wxFileName::FileName(filename),
183 m_events, wxFSWPath_File) )
184 {
185 wxLogTrace(wxTRACE_FSWATCHER,
186 "--- AddTree adding file '%s' ---", filename);
187 }
188 return wxDIR_CONTINUE;
189 }
190
191 virtual wxDirTraverseResult OnDir(const wxString& dirname)
192 {
193 // We can't currently watch only the files with the given filespec
194 // in the subdirectories so we only watch subdirectories at all if
195 // we want to watch everything.
196 if ( m_filespec.empty() )
197 {
198 if ( m_watcher->AddAny(wxFileName::DirName(dirname),
199 m_events, wxFSWPath_Dir) )
200 {
201 wxLogTrace(wxTRACE_FSWATCHER,
202 "--- AddTree adding directory '%s' ---", dirname);
203 }
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 AddTraverser traverser(this, events, filespec);
216 dir.Traverse(traverser, filespec);
217
218 // Add the path itself explicitly as Traverse() doesn't return it.
219 AddAny(path.GetPathWithSep(), events, wxFSWPath_Dir, filespec);
220
221 return true;
222 }
223
224 bool wxFileSystemWatcherBase::RemoveTree(const wxFileName& path)
225 {
226 if (!path.DirExists())
227 return false;
228
229 // OPT could be optimised if we stored information about relationships
230 // between paths
231 class RemoveTraverser : public wxDirTraverser
232 {
233 public:
234 RemoveTraverser(wxFileSystemWatcherBase* watcher,
235 const wxString& filespec) :
236 m_watcher(watcher), m_filespec(filespec)
237 {
238 }
239
240 virtual wxDirTraverseResult OnFile(const wxString& filename)
241 {
242 m_watcher->Remove(wxFileName(filename));
243 return wxDIR_CONTINUE;
244 }
245
246 virtual wxDirTraverseResult OnDir(const wxString& dirname)
247 {
248 // Currently the subdirectories would have been added only if there
249 // is no filespec.
250 //
251 // Notice that we still need to recurse into them even if we're
252 // using a filespec because they can contain files matching it.
253 if ( m_filespec.empty() )
254 {
255 m_watcher->Remove(wxFileName::DirName(dirname));
256 }
257
258 return wxDIR_CONTINUE;
259 }
260
261 private:
262 wxFileSystemWatcherBase* m_watcher;
263 wxString m_filespec;
264 };
265
266 // If AddTree() used a filespec, we must use the same one
267 wxString canonical = GetCanonicalPath(path);
268 wxFSWatchInfoMap::iterator it = m_watches.find(canonical);
269 wxCHECK_MSG( it != m_watches.end(), false,
270 wxString::Format("Path '%s' is not watched", canonical) );
271 wxFSWatchInfo watch = it->second;
272 const wxString filespec = watch.GetFilespec();
273
274 #if defined(__WINDOWS__)
275 // When there's no filespec, the wxMSW AddTree() would have set a watch
276 // on only the passed 'path'. We must therefore remove only this
277 if (filespec.empty())
278 {
279 return Remove(path);
280 }
281 // Otherwise fall through to the generic implementation
282 #endif // __WINDOWS__
283
284 wxDir dir(path.GetFullPath());
285 RemoveTraverser traverser(this, filespec);
286 dir.Traverse(traverser, filespec);
287
288 // As in AddTree() above, handle the path itself explicitly.
289 Remove(path);
290
291 return true;
292 }
293
294 bool wxFileSystemWatcherBase::RemoveAll()
295 {
296 m_service->RemoveAll();
297 m_watches.clear();
298 return true;
299 }
300
301 int wxFileSystemWatcherBase::GetWatchedPathsCount() const
302 {
303 return m_watches.size();
304 }
305
306 int wxFileSystemWatcherBase::GetWatchedPaths(wxArrayString* paths) const
307 {
308 wxCHECK_MSG( paths != NULL, -1, "Null array passed to retrieve paths");
309
310 wxFSWatchInfoMap::const_iterator it = m_watches.begin();
311 for ( ; it != m_watches.end(); ++it)
312 {
313 paths->push_back(it->first);
314 }
315
316 return m_watches.size();
317 }
318
319 #endif // wxUSE_FSWATCHER