]>
git.saurik.com Git - wxWidgets.git/blob - src/common/fswatchercmn.cpp
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
46 case wxFSW_EVENT_UNMOUNT
: // Currently this is wxGTK-only
49 case wxFSW_EVENT_WARNING
:
51 case wxFSW_EVENT_ERROR
:
55 // should never be reached!
56 wxFAIL_MSG("Unknown change type");
57 return "INVALID_TYPE";
61 // ============================================================================
62 // wxFileSystemWatcherEvent implementation
63 // ============================================================================
65 wxString
wxFileSystemWatcherEvent::ToString() const
67 return wxString::Format("FSW_EVT type=%d (%s) path='%s'", m_changeType
,
68 GetFSWEventChangeTypeName(m_changeType
), GetPath().GetFullPath());
72 // ============================================================================
73 // wxFileSystemWatcherEvent implementation
74 // ============================================================================
76 wxFileSystemWatcherBase::wxFileSystemWatcherBase() :
77 m_service(0), m_owner(this)
81 wxFileSystemWatcherBase::~wxFileSystemWatcherBase()
90 bool wxFileSystemWatcherBase::Add(const wxFileName
& path
, int events
)
92 wxFSWPathType type
= wxFSWPath_None
;
93 if ( path
.FileExists() )
95 type
= wxFSWPath_File
;
97 else if ( path
.DirExists() )
103 wxLogError(_("Can't monitor non-existent path \"%s\" for changes."),
108 return AddAny(path
, events
, type
);
112 wxFileSystemWatcherBase::AddAny(const wxFileName
& path
,
115 const wxString
& filespec
)
117 wxString canonical
= GetCanonicalPath(path
);
118 if (canonical
.IsEmpty())
121 // adding a path in a platform specific way
122 wxFSWatchInfo
watch(canonical
, events
, type
, filespec
);
123 if ( !m_service
->Add(watch
) )
126 // on success, either add path to our 'watch-list'
127 // or, if already watched, inc the refcount. This may happen if
128 // a dir is Add()ed, then later AddTree() is called on a parent dir
129 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
130 if ( it
== m_watches
.end() )
132 wxFSWatchInfoMap::value_type
val(canonical
, watch
);
133 m_watches
.insert(val
).second
;
137 wxFSWatchInfo
& watch
= it
->second
;
138 int count
= watch
.IncRef();
139 wxLogTrace(wxTRACE_FSWATCHER
,
140 "'%s' is now watched %d times", canonical
, count
);
145 bool wxFileSystemWatcherBase::Remove(const wxFileName
& path
)
147 // args validation & consistency checks
148 wxString canonical
= GetCanonicalPath(path
);
149 if (canonical
.IsEmpty())
152 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
153 wxCHECK_MSG(it
!= m_watches
.end(), false,
154 wxString::Format("Path '%s' is not watched", canonical
));
156 // Decrement the watch's refcount and remove from watch-list if 0
158 wxFSWatchInfo
& watch
= it
->second
;
159 if ( !watch
.DecRef() )
161 // remove in a platform specific way
162 ret
= m_service
->Remove(watch
);
169 bool wxFileSystemWatcherBase::AddTree(const wxFileName
& path
, int events
,
170 const wxString
& filespec
)
172 if (!path
.DirExists())
175 // OPT could be optimised if we stored information about relationships
177 class AddTraverser
: public wxDirTraverser
180 AddTraverser(wxFileSystemWatcherBase
* watcher
, int events
,
181 const wxString
& filespec
) :
182 m_watcher(watcher
), m_events(events
), m_filespec(filespec
)
186 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
188 // There is no need to watch individual files as we watch the
189 // parent directory which will notify us about any changes in them.
190 return wxDIR_CONTINUE
;
193 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
195 if ( m_watcher
->AddAny(wxFileName::DirName(dirname
),
196 m_events
, wxFSWPath_Tree
, m_filespec
) )
198 wxLogTrace(wxTRACE_FSWATCHER
,
199 "--- AddTree adding directory '%s' ---", dirname
);
201 return wxDIR_CONTINUE
;
205 wxFileSystemWatcherBase
* m_watcher
;
210 wxDir
dir(path
.GetFullPath());
211 // Prevent asserts or infinite loops in trees containing symlinks
212 int flags
= wxDIR_DIRS
;
213 if ( !path
.ShouldFollowLink() )
215 flags
|= wxDIR_NO_FOLLOW
;
217 AddTraverser
traverser(this, events
, filespec
);
218 dir
.Traverse(traverser
, filespec
, flags
);
220 // Add the path itself explicitly as Traverse() doesn't return it.
221 AddAny(path
.GetPathWithSep(), events
, wxFSWPath_Tree
, filespec
);
226 bool wxFileSystemWatcherBase::RemoveTree(const wxFileName
& path
)
228 if (!path
.DirExists())
231 // OPT could be optimised if we stored information about relationships
233 class RemoveTraverser
: public wxDirTraverser
236 RemoveTraverser(wxFileSystemWatcherBase
* watcher
,
237 const wxString
& filespec
) :
238 m_watcher(watcher
), m_filespec(filespec
)
242 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
244 // We never watch the individual files when watching the tree, so
245 // nothing to do here.
246 return wxDIR_CONTINUE
;
249 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
251 m_watcher
->Remove(wxFileName::DirName(dirname
));
252 return wxDIR_CONTINUE
;
256 wxFileSystemWatcherBase
* m_watcher
;
260 // If AddTree() used a filespec, we must use the same one
261 wxString canonical
= GetCanonicalPath(path
);
262 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
263 wxCHECK_MSG( it
!= m_watches
.end(), false,
264 wxString::Format("Path '%s' is not watched", canonical
) );
265 wxFSWatchInfo watch
= it
->second
;
266 const wxString filespec
= watch
.GetFilespec();
268 #if defined(__WINDOWS__)
269 // When there's no filespec, the wxMSW AddTree() would have set a watch
270 // on only the passed 'path'. We must therefore remove only this
271 if (filespec
.empty())
275 // Otherwise fall through to the generic implementation
276 #endif // __WINDOWS__
278 wxDir
dir(path
.GetFullPath());
279 // AddTree() might have used the wxDIR_NO_FOLLOW to prevent asserts or
280 // infinite loops in trees containing symlinks. We need to do the same
281 // or we'll try to remove unwatched items. Let's hope the caller used
282 // the same ShouldFollowLink() setting as in AddTree()...
283 int flags
= wxDIR_DIRS
;
284 if ( !path
.ShouldFollowLink() )
286 flags
|= wxDIR_NO_FOLLOW
;
288 RemoveTraverser
traverser(this, filespec
);
289 dir
.Traverse(traverser
, filespec
, flags
);
291 // As in AddTree() above, handle the path itself explicitly.
297 bool wxFileSystemWatcherBase::RemoveAll()
299 m_service
->RemoveAll();
304 int wxFileSystemWatcherBase::GetWatchedPathsCount() const
306 return m_watches
.size();
309 int wxFileSystemWatcherBase::GetWatchedPaths(wxArrayString
* paths
) const
311 wxCHECK_MSG( paths
!= NULL
, -1, "Null array passed to retrieve paths");
313 wxFSWatchInfoMap::const_iterator it
= m_watches
.begin();
314 for ( ; it
!= m_watches
.end(); ++it
)
316 paths
->push_back(it
->first
);
319 return m_watches
.size();
322 #endif // wxUSE_FSWATCHER