]>
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
:
45 // should never be reached!
46 wxFAIL_MSG("Unknown change type");
47 return "INVALID_TYPE";
51 // ============================================================================
52 // wxFileSystemWatcherEvent implementation
53 // ============================================================================
55 wxString
wxFileSystemWatcherEvent::ToString() const
57 return wxString::Format("FSW_EVT type=%d (%s) path='%s'", m_changeType
,
58 GetFSWEventChangeTypeName(m_changeType
), GetPath().GetFullPath());
62 // ============================================================================
63 // wxFileSystemWatcherEvent implementation
64 // ============================================================================
66 wxFileSystemWatcherBase::wxFileSystemWatcherBase() :
67 m_service(0), m_owner(this)
71 wxFileSystemWatcherBase::~wxFileSystemWatcherBase()
80 bool wxFileSystemWatcherBase::Add(const wxFileName
& path
, int events
)
82 wxFSWPathType type
= wxFSWPath_None
;
83 if ( path
.FileExists() )
85 type
= wxFSWPath_File
;
87 else if ( path
.DirExists() )
93 wxLogError(_("Can't monitor non-existent path \"%s\" for changes."),
98 return AddAny(path
, events
, type
);
102 wxFileSystemWatcherBase::AddAny(const wxFileName
& path
,
106 wxString canonical
= GetCanonicalPath(path
);
107 if (canonical
.IsEmpty())
110 wxCHECK_MSG(m_watches
.find(canonical
) == m_watches
.end(), false,
111 wxString::Format("Path '%s' is already watched", canonical
));
113 // adding a path in a platform specific way
114 wxFSWatchInfo
watch(canonical
, events
, type
);
115 if ( !m_service
->Add(watch
) )
118 // on success, add path to our 'watch-list'
119 wxFSWatchInfoMap::value_type
val(canonical
, watch
);
120 return m_watches
.insert(val
).second
;
123 bool wxFileSystemWatcherBase::Remove(const wxFileName
& path
)
125 // args validation & consistency checks
126 wxString canonical
= GetCanonicalPath(path
);
127 if (canonical
.IsEmpty())
130 wxFSWatchInfoMap::iterator it
= m_watches
.find(canonical
);
131 wxCHECK_MSG(it
!= m_watches
.end(), false,
132 wxString::Format("Path '%s' is not watched", canonical
));
134 // remove from watch-list
135 wxFSWatchInfo watch
= it
->second
;
138 // remove in a platform specific way
139 return m_service
->Remove(watch
);
142 bool wxFileSystemWatcherBase::AddTree(const wxFileName
& path
, int events
,
143 const wxString
& filter
)
145 if (!path
.DirExists())
148 // OPT could be optimised if we stored information about relationships
150 class AddTraverser
: public wxDirTraverser
153 AddTraverser(wxFileSystemWatcherBase
* watcher
, int events
) :
154 m_watcher(watcher
), m_events(events
)
158 // CHECK we choose which files to delegate to Add(), maybe we should pass
159 // all of them to Add() and let it choose? this is useful when adding a
160 // file to a dir that is already watched, then not only should we know
161 // about that, but Add() should also behave well then
162 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
164 wxLogTrace(wxTRACE_FSWATCHER
,
165 "--- AddTree adding file '%s' ---", filename
);
166 m_watcher
->AddAny(wxFileName::FileName(filename
),
167 m_events
, wxFSWPath_File
);
168 return wxDIR_CONTINUE
;
171 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
173 wxLogTrace(wxTRACE_FSWATCHER
,
174 "--- AddTree adding directory '%s' ---", dirname
);
175 // we add as much as possible and ignore errors
176 m_watcher
->AddAny(wxFileName::DirName(dirname
),
177 m_events
, wxFSWPath_Dir
);
178 return wxDIR_CONTINUE
;
182 wxFileSystemWatcherBase
* m_watcher
;
187 wxDir
dir(path
.GetFullPath());
188 AddTraverser
traverser(this, events
);
189 dir
.Traverse(traverser
, filter
);
191 // Add the path itself explicitly as Traverse() doesn't return it.
192 Add(path
.GetPathWithSep(), events
);
197 bool wxFileSystemWatcherBase::RemoveTree(const wxFileName
& path
)
199 if (!path
.DirExists())
202 // OPT could be optimised if we stored information about relationships
204 class RemoveTraverser
: public wxDirTraverser
207 RemoveTraverser(wxFileSystemWatcherBase
* watcher
) :
212 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
214 m_watcher
->Remove(wxFileName(filename
));
215 return wxDIR_CONTINUE
;
218 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
220 m_watcher
->Remove(wxFileName::DirName(dirname
));
221 return wxDIR_CONTINUE
;
225 wxFileSystemWatcherBase
* m_watcher
;
228 wxDir
dir(path
.GetFullPath());
229 RemoveTraverser
traverser(this);
230 dir
.Traverse(traverser
);
232 // As in AddTree() above, handle the path itself explicitly.
238 bool wxFileSystemWatcherBase::RemoveAll()
240 m_service
->RemoveAll();
245 int wxFileSystemWatcherBase::GetWatchedPathsCount() const
247 return m_watches
.size();
250 int wxFileSystemWatcherBase::GetWatchedPaths(wxArrayString
* paths
) const
252 wxCHECK_MSG( paths
!= NULL
, -1, "Null array passed to retrieve paths");
254 wxFSWatchInfoMap::const_iterator it
= m_watches
.begin();
255 for ( ; it
!= m_watches
.end(); ++it
)
257 paths
->push_back(it
->first
);
260 return m_watches
.size();
263 #endif // wxUSE_FSWATCHER