]>
git.saurik.com Git - wxWidgets.git/blob - src/common/fswatchercmn.cpp
a417a82ee7146b9d34048d3a6d0f0430b5ab962e
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 IMPLEMENT_DYNAMIC_CLASS(wxFileSystemWatcherEvent
, wxEvent
);
67 wxString
wxFileSystemWatcherEvent::ToString() const
69 return wxString::Format("FSW_EVT type=%d (%s) path='%s'", m_changeType
,
70 GetFSWEventChangeTypeName(m_changeType
), GetPath().GetFullPath());
74 // ============================================================================
75 // wxFileSystemWatcherEvent implementation
76 // ============================================================================
78 wxFileSystemWatcherBase::wxFileSystemWatcherBase() :
79 m_service(0), m_owner(this)
83 wxFileSystemWatcherBase::~wxFileSystemWatcherBase()
92 bool wxFileSystemWatcherBase::Add(const wxFileName
& path
, int events
)
94 wxFSWPathType type
= wxFSWPath_None
;
95 if ( path
.FileExists() )
97 type
= wxFSWPath_File
;
99 else if ( path
.DirExists() )
101 type
= wxFSWPath_Dir
;
105 // Don't overreact to being passed a non-existent item. It may have
106 // only just been deleted, in which case doing nothing is correct
107 wxLogTrace(wxTRACE_FSWATCHER
,
108 "Can't monitor non-existent path \"%s\" for changes.",
113 return AddAny(path
, events
, type
);
117 wxFileSystemWatcherBase::AddAny(const wxFileName
& path
,
120 const wxString
& filespec
)
122 wxString canonical
= GetCanonicalPath(path
);
123 if (canonical
.IsEmpty())
126 // adding a path in a platform specific way
127 wxFSWatchInfo
watch(canonical
, events
, type
, filespec
);
128 if ( !m_service
->Add(watch
) )
131 // on success, either add path to our 'watch-list'
132 // or, if already watched, inc the refcount. This may happen if
133 // a dir is Add()ed, then later AddTree() is called on a parent dir
134 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
135 if ( it
== m_watches
.end() )
137 wxFSWatchInfoMap::value_type
val(canonical
, watch
);
138 m_watches
.insert(val
);
142 wxFSWatchInfo
& watch
= it
->second
;
143 int count
= watch
.IncRef();
144 wxLogTrace(wxTRACE_FSWATCHER
,
145 "'%s' is now watched %d times", canonical
, count
);
150 bool wxFileSystemWatcherBase::Remove(const wxFileName
& path
)
152 // args validation & consistency checks
153 wxString canonical
= GetCanonicalPath(path
);
154 if (canonical
.IsEmpty())
157 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
158 wxCHECK_MSG(it
!= m_watches
.end(), false,
159 wxString::Format("Path '%s' is not watched", canonical
));
161 // Decrement the watch's refcount and remove from watch-list if 0
163 wxFSWatchInfo
& watch
= it
->second
;
164 if ( !watch
.DecRef() )
166 // remove in a platform specific way
167 ret
= m_service
->Remove(watch
);
174 bool wxFileSystemWatcherBase::AddTree(const wxFileName
& path
, int events
,
175 const wxString
& filespec
)
177 if (!path
.DirExists())
180 // OPT could be optimised if we stored information about relationships
182 class AddTraverser
: public wxDirTraverser
185 AddTraverser(wxFileSystemWatcherBase
* watcher
, int events
,
186 const wxString
& filespec
) :
187 m_watcher(watcher
), m_events(events
), m_filespec(filespec
)
191 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
193 // There is no need to watch individual files as we watch the
194 // parent directory which will notify us about any changes in them.
195 return wxDIR_CONTINUE
;
198 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
200 if ( m_watcher
->AddAny(wxFileName::DirName(dirname
),
201 m_events
, wxFSWPath_Tree
, m_filespec
) )
203 wxLogTrace(wxTRACE_FSWATCHER
,
204 "--- AddTree adding directory '%s' ---", dirname
);
206 return wxDIR_CONTINUE
;
210 wxFileSystemWatcherBase
* m_watcher
;
215 wxDir
dir(path
.GetFullPath());
216 // Prevent asserts or infinite loops in trees containing symlinks
217 int flags
= wxDIR_DIRS
;
218 if ( !path
.ShouldFollowLink() )
220 flags
|= wxDIR_NO_FOLLOW
;
222 AddTraverser
traverser(this, events
, filespec
);
223 dir
.Traverse(traverser
, filespec
, flags
);
225 // Add the path itself explicitly as Traverse() doesn't return it.
226 AddAny(path
.GetPathWithSep(), events
, wxFSWPath_Tree
, filespec
);
231 bool wxFileSystemWatcherBase::RemoveTree(const wxFileName
& path
)
233 if (!path
.DirExists())
236 // OPT could be optimised if we stored information about relationships
238 class RemoveTraverser
: public wxDirTraverser
241 RemoveTraverser(wxFileSystemWatcherBase
* watcher
,
242 const wxString
& filespec
) :
243 m_watcher(watcher
), m_filespec(filespec
)
247 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
249 // We never watch the individual files when watching the tree, so
250 // nothing to do here.
251 return wxDIR_CONTINUE
;
254 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
256 m_watcher
->Remove(wxFileName::DirName(dirname
));
257 return wxDIR_CONTINUE
;
261 wxFileSystemWatcherBase
* m_watcher
;
265 // If AddTree() used a filespec, we must use the same one
266 wxString canonical
= GetCanonicalPath(path
);
267 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
268 wxCHECK_MSG( it
!= m_watches
.end(), false,
269 wxString::Format("Path '%s' is not watched", canonical
) );
270 wxFSWatchInfo watch
= it
->second
;
271 const wxString filespec
= watch
.GetFilespec();
273 #if defined(__WINDOWS__)
274 // When there's no filespec, the wxMSW AddTree() would have set a watch
275 // on only the passed 'path'. We must therefore remove only this
276 if (filespec
.empty())
280 // Otherwise fall through to the generic implementation
281 #endif // __WINDOWS__
283 wxDir
dir(path
.GetFullPath());
284 // AddTree() might have used the wxDIR_NO_FOLLOW to prevent asserts or
285 // infinite loops in trees containing symlinks. We need to do the same
286 // or we'll try to remove unwatched items. Let's hope the caller used
287 // the same ShouldFollowLink() setting as in AddTree()...
288 int flags
= wxDIR_DIRS
;
289 if ( !path
.ShouldFollowLink() )
291 flags
|= wxDIR_NO_FOLLOW
;
293 RemoveTraverser
traverser(this, filespec
);
294 dir
.Traverse(traverser
, filespec
, flags
);
296 // As in AddTree() above, handle the path itself explicitly.
302 bool wxFileSystemWatcherBase::RemoveAll()
304 m_service
->RemoveAll();
309 int wxFileSystemWatcherBase::GetWatchedPathsCount() const
311 return m_watches
.size();
314 int wxFileSystemWatcherBase::GetWatchedPaths(wxArrayString
* paths
) const
316 wxCHECK_MSG( paths
!= NULL
, -1, "Null array passed to retrieve paths");
318 wxFSWatchInfoMap::const_iterator it
= m_watches
.begin();
319 for ( ; it
!= m_watches
.end(); ++it
)
321 paths
->push_back(it
->first
);
324 return m_watches
.size();
327 #endif // wxUSE_FSWATCHER