fswatcher.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/private/fswatcher.h
  3. // Purpose: File system watcher impl classes
  4. // Author: Bartosz Bekier
  5. // Created: 2009-05-26
  6. // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef WX_PRIVATE_FSWATCHER_H_
  10. #define WX_PRIVATE_FSWATCHER_H_
  11. #include "wx/sharedptr.h"
  12. #ifdef wxHAS_INOTIFY
  13. class wxFSWatchEntryUnix;
  14. #define wxFSWatchEntry wxFSWatchEntryUnix
  15. WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxFSWatchEntry>,wxFSWatchEntries);
  16. #include "wx/unix/private/fswatcher_inotify.h"
  17. #elif defined(wxHAS_KQUEUE)
  18. class wxFSWatchEntryKq;
  19. #define wxFSWatchEntry wxFSWatchEntryKq
  20. WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxFSWatchEntry>,wxFSWatchEntries);
  21. #include "wx/unix/private/fswatcher_kqueue.h"
  22. #elif defined(__WINDOWS__)
  23. class wxFSWatchEntryMSW;
  24. #define wxFSWatchEntry wxFSWatchEntryMSW
  25. WX_DECLARE_STRING_HASH_MAP(wxSharedPtr<wxFSWatchEntry>,wxFSWatchEntries);
  26. #include "wx/msw/private/fswatcher.h"
  27. #else
  28. #define wxFSWatchEntry wxFSWatchEntryPolling
  29. #endif
  30. class wxFSWatcherImpl
  31. {
  32. public:
  33. wxFSWatcherImpl(wxFileSystemWatcherBase* watcher) :
  34. m_watcher(watcher)
  35. {
  36. }
  37. virtual ~wxFSWatcherImpl()
  38. {
  39. (void) RemoveAll();
  40. }
  41. virtual bool Init() = 0;
  42. virtual bool Add(const wxFSWatchInfo& winfo)
  43. {
  44. if ( m_watches.find(winfo.GetPath()) != m_watches.end() )
  45. {
  46. wxLogTrace(wxTRACE_FSWATCHER,
  47. "Path '%s' is already watched", winfo.GetPath());
  48. // This can happen if a dir is watched, then a parent tree added
  49. return true;
  50. }
  51. // construct watch entry
  52. wxSharedPtr<wxFSWatchEntry> watch(new wxFSWatchEntry(winfo));
  53. if (!DoAdd(watch))
  54. return false;
  55. // add watch to our map (always succeedes, checked above)
  56. wxFSWatchEntries::value_type val(watch->GetPath(), watch);
  57. return m_watches.insert(val).second;
  58. }
  59. virtual bool Remove(const wxFSWatchInfo& winfo)
  60. {
  61. wxFSWatchEntries::iterator it = m_watches.find(winfo.GetPath());
  62. if ( it == m_watches.end() )
  63. {
  64. wxLogTrace(wxTRACE_FSWATCHER,
  65. "Path '%s' is not watched", winfo.GetPath());
  66. // This can happen if a dir is watched, then a parent tree added
  67. return true;
  68. }
  69. wxSharedPtr<wxFSWatchEntry> watch = it->second;
  70. m_watches.erase(it);
  71. return DoRemove(watch);
  72. }
  73. virtual bool RemoveAll()
  74. {
  75. bool ret = true;
  76. for ( wxFSWatchEntries::iterator it = m_watches.begin();
  77. it != m_watches.end();
  78. ++it )
  79. {
  80. if ( !DoRemove(it->second) )
  81. ret = false;
  82. }
  83. m_watches.clear();
  84. return ret;
  85. }
  86. // Check whether any filespec matches the file's ext (if present)
  87. bool MatchesFilespec(const wxFileName& fn, const wxString& filespec) const
  88. {
  89. return filespec.empty() || wxMatchWild(filespec, fn.GetFullName());
  90. }
  91. protected:
  92. virtual bool DoAdd(wxSharedPtr<wxFSWatchEntry> watch) = 0;
  93. virtual bool DoRemove(wxSharedPtr<wxFSWatchEntry> watch) = 0;
  94. wxFSWatchEntries m_watches;
  95. wxFileSystemWatcherBase* m_watcher;
  96. };
  97. #endif /* WX_PRIVATE_FSWATCHER_H_ */