]>
git.saurik.com Git - wxWidgets.git/blob - src/common/fswatchercmn.cpp
dfa8730cf3a7c4e95603690705d17a86c5bc77c3
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fswatchercmn.cpp
3 // Purpose: wxMswFileSystemWatcher
4 // Author: Bartosz Bekier
7 // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/fswatcher.h"
21 #include "wx/private/fswatcher.h"
23 // ============================================================================
25 // ============================================================================
27 wxDEFINE_EVENT(wxEVT_FSWATCHER
, wxFileSystemWatcherEvent
);
29 static wxString
GetFSWEventChangeTypeName(int type
)
33 case wxFSW_EVENT_CREATE
:
35 case wxFSW_EVENT_DELETE
:
37 case wxFSW_EVENT_RENAME
:
39 case wxFSW_EVENT_MODIFY
:
41 case wxFSW_EVENT_ACCESS
:
43 case wxFSW_EVENT_ATTRIB
: // Currently this is wxGTK-only
45 case wxFSW_EVENT_WARNING
:
47 case wxFSW_EVENT_ERROR
:
51 // should never be reached!
52 wxFAIL_MSG("Unknown change type");
53 return "INVALID_TYPE";
57 // ============================================================================
58 // wxFileSystemWatcherEvent implementation
59 // ============================================================================
61 wxString
wxFileSystemWatcherEvent::ToString() const
63 return wxString::Format("FSW_EVT type=%d (%s) path='%s'", m_changeType
,
64 GetFSWEventChangeTypeName(m_changeType
), GetPath().GetFullPath());
68 // ============================================================================
69 // wxFileSystemWatcherEvent implementation
70 // ============================================================================
72 wxFileSystemWatcherBase::wxFileSystemWatcherBase() :
73 m_service(0), m_owner(this)
77 wxFileSystemWatcherBase::~wxFileSystemWatcherBase()
86 bool wxFileSystemWatcherBase::Add(const wxFileName
& path
, int events
)
88 wxFSWPathType type
= wxFSWPath_None
;
89 if ( path
.FileExists() )
91 type
= wxFSWPath_File
;
93 else if ( path
.DirExists() )
99 wxLogError(_("Can't monitor non-existent path \"%s\" for changes."),
104 return AddAny(path
, events
, type
);
108 wxFileSystemWatcherBase::AddAny(const wxFileName
& path
,
111 const wxString
& filespec
)
113 wxString canonical
= GetCanonicalPath(path
);
114 if (canonical
.IsEmpty())
117 // adding a path in a platform specific way
118 wxFSWatchInfo
watch(canonical
, events
, type
, filespec
);
119 if ( !m_service
->Add(watch
) )
122 // on success, either add path to our 'watch-list'
123 // or, if already watched, inc the refcount. This may happen if
124 // a dir is Add()ed, then later AddTree() is called on a parent dir
125 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
126 if ( it
== m_watches
.end() )
128 wxFSWatchInfoMap::value_type
val(canonical
, watch
);
129 m_watches
.insert(val
).second
;
133 wxFSWatchInfo
& watch
= it
->second
;
134 int count
= watch
.IncRef();
135 wxLogTrace(wxTRACE_FSWATCHER
,
136 "'%s' is now watched %d times", canonical
, count
);
141 bool wxFileSystemWatcherBase::Remove(const wxFileName
& path
)
143 // args validation & consistency checks
144 wxString canonical
= GetCanonicalPath(path
);
145 if (canonical
.IsEmpty())
148 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
149 wxCHECK_MSG(it
!= m_watches
.end(), false,
150 wxString::Format("Path '%s' is not watched", canonical
));
152 // Decrement the watch's refcount and remove from watch-list if 0
154 wxFSWatchInfo
& watch
= it
->second
;
155 if ( !watch
.DecRef() )
157 // remove in a platform specific way
158 ret
= m_service
->Remove(watch
);
165 bool wxFileSystemWatcherBase::AddTree(const wxFileName
& path
, int events
,
166 const wxString
& filespec
)
168 if (!path
.DirExists())
171 // OPT could be optimised if we stored information about relationships
173 class AddTraverser
: public wxDirTraverser
176 AddTraverser(wxFileSystemWatcherBase
* watcher
, int events
,
177 const wxString
& filespec
) :
178 m_watcher(watcher
), m_events(events
), m_filespec(filespec
)
182 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
184 // There is no need to watch individual files as we watch the
185 // parent directory which will notify us about any changes in them.
186 return wxDIR_CONTINUE
;
189 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
191 if ( m_watcher
->AddAny(wxFileName::DirName(dirname
),
192 m_events
, wxFSWPath_Tree
, m_filespec
) )
194 wxLogTrace(wxTRACE_FSWATCHER
,
195 "--- AddTree adding directory '%s' ---", dirname
);
197 return wxDIR_CONTINUE
;
201 wxFileSystemWatcherBase
* m_watcher
;
206 wxDir
dir(path
.GetFullPath());
207 // Prevent asserts or infinite loops in trees containing symlinks
208 int flags
= wxDIR_DIRS
;
209 if ( !path
.ShouldFollowLink() )
211 flags
|= wxDIR_NO_FOLLOW
;
213 AddTraverser
traverser(this, events
, filespec
);
214 dir
.Traverse(traverser
, filespec
, flags
);
216 // Add the path itself explicitly as Traverse() doesn't return it.
217 AddAny(path
.GetPathWithSep(), events
, wxFSWPath_Tree
, filespec
);
222 bool wxFileSystemWatcherBase::RemoveTree(const wxFileName
& path
)
224 if (!path
.DirExists())
227 // OPT could be optimised if we stored information about relationships
229 class RemoveTraverser
: public wxDirTraverser
232 RemoveTraverser(wxFileSystemWatcherBase
* watcher
,
233 const wxString
& filespec
) :
234 m_watcher(watcher
), m_filespec(filespec
)
238 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
240 // We never watch the individual files when watching the tree, so
241 // nothing to do here.
242 return wxDIR_CONTINUE
;
245 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
247 m_watcher
->Remove(wxFileName::DirName(dirname
));
248 return wxDIR_CONTINUE
;
252 wxFileSystemWatcherBase
* m_watcher
;
256 // If AddTree() used a filespec, we must use the same one
257 wxString canonical
= GetCanonicalPath(path
);
258 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
259 wxCHECK_MSG( it
!= m_watches
.end(), false,
260 wxString::Format("Path '%s' is not watched", canonical
) );
261 wxFSWatchInfo watch
= it
->second
;
262 const wxString filespec
= watch
.GetFilespec();
264 #if defined(__WINDOWS__)
265 // When there's no filespec, the wxMSW AddTree() would have set a watch
266 // on only the passed 'path'. We must therefore remove only this
267 if (filespec
.empty())
271 // Otherwise fall through to the generic implementation
272 #endif // __WINDOWS__
274 wxDir
dir(path
.GetFullPath());
275 // AddTree() might have used the wxDIR_NO_FOLLOW to prevent asserts or
276 // infinite loops in trees containing symlinks. We need to do the same
277 // or we'll try to remove unwatched items. Let's hope the caller used
278 // the same ShouldFollowLink() setting as in AddTree()...
279 int flags
= wxDIR_DIRS
;
280 if ( !path
.ShouldFollowLink() )
282 flags
|= wxDIR_NO_FOLLOW
;
284 RemoveTraverser
traverser(this, filespec
);
285 dir
.Traverse(traverser
, filespec
, flags
);
287 // As in AddTree() above, handle the path itself explicitly.
293 bool wxFileSystemWatcherBase::RemoveAll()
295 m_service
->RemoveAll();
300 int wxFileSystemWatcherBase::GetWatchedPathsCount() const
302 return m_watches
.size();
305 int wxFileSystemWatcherBase::GetWatchedPaths(wxArrayString
* paths
) const
307 wxCHECK_MSG( paths
!= NULL
, -1, "Null array passed to retrieve paths");
309 wxFSWatchInfoMap::const_iterator it
= m_watches
.begin();
310 for ( ; it
!= m_watches
.end(); ++it
)
312 paths
->push_back(it
->first
);
315 return m_watches
.size();
318 #endif // wxUSE_FSWATCHER