fswatcher.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/fswatcher.h
  3. // Purpose: wxFileSystemWatcher
  4. // Author: Bartosz Bekier
  5. // Created: 2009-05-23
  6. // Copyright: (c) 2009 Bartosz Bekier <bartosz.bekier@gmail.com>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. /**
  10. @class wxFileSystemWatcher
  11. The wxFileSystemWatcher class allows to receive notifications of file
  12. system changes.
  13. @note Implementation limitations: this class is currently implemented for
  14. MSW, OS X and GTK ports but doesn't detect all changes correctly
  15. everywhere: under MSW accessing the file is not detected (only
  16. modifying it is) and under OS X neither accessing nor modifying is
  17. detected (only creating and deleting files is). Moreover, OS X
  18. version doesn't currently collapse pairs of create/delete events in a
  19. rename event, unlike the other ones.
  20. For the full list of change types that are reported see wxFSWFlags.
  21. This class notifies the application about the file system changes by
  22. sending events of wxFileSystemWatcherEvent class. By default these events
  23. are sent to the wxFileSystemWatcher object itself so you can derive from it
  24. and use the event table @c EVT_FSWATCHER macro to handle these events in a
  25. derived class method. Alternatively, you can use
  26. wxFileSystemWatcher::SetOwner() to send the events to another object. Or
  27. you could use wxEvtHandler::Connect() with @c wxEVT_FSWATCHER to handle
  28. these events in any other object. See the fswatcher sample for an example
  29. of the latter approach.
  30. @library{wxbase}
  31. @category{file}
  32. @since 2.9.1
  33. */
  34. class wxFileSystemWatcher: public wxEvtHandler
  35. {
  36. public:
  37. /**
  38. Default constructor.
  39. */
  40. wxFileSystemWatcher();
  41. /**
  42. Destructor. Stops all paths from being watched and frees any system
  43. resources used by this file system watcher object.
  44. */
  45. virtual ~wxFileSystemWatcher();
  46. /**
  47. Adds @a path to currently watched files.
  48. The @a path argument can currently only be a directory and any changes
  49. to this directory itself or its immediate children will generate the
  50. events. Use AddTree() to monitor the directory recursively.
  51. Note that on platforms that use symbolic links, you should consider the
  52. possibility that @a path is a symlink. To watch the symlink itself and
  53. not its target you may call wxFileName::DontFollowLink() on @a path.
  54. @param path
  55. The name of the path to watch.
  56. @param events
  57. An optional filter to receive only events of particular types.
  58. This is currently implemented only for GTK.
  59. */
  60. virtual bool Add(const wxFileName& path, int events = wxFSW_EVENT_ALL);
  61. /**
  62. This is the same as Add(), but also recursively adds every
  63. file/directory in the tree rooted at @a path.
  64. Additionally a file mask can be specified to include only files
  65. matching that particular mask.
  66. This method is implemented efficiently on MSW, but should be used with
  67. care on other platforms for directories with lots of children (e.g. the
  68. root directory) as it calls Add() for each subdirectory, potentially
  69. creating a lot of watches and taking a long time to execute.
  70. Note that on platforms that use symbolic links, you will probably want
  71. to have called wxFileName::DontFollowLink on @a path. This is especially
  72. important if the symlink targets may themselves be watched.
  73. */
  74. virtual bool AddTree(const wxFileName& path, int events = wxFSW_EVENT_ALL,
  75. const wxString& filter = wxEmptyString);
  76. /**
  77. Removes @a path from the list of watched paths.
  78. See the comment in Add() about symbolic links. @a path should treat
  79. symbolic links in the same way as in the original Add() call.
  80. */
  81. virtual bool Remove(const wxFileName& path);
  82. /**
  83. This is the same as Remove(), but also removes every file/directory
  84. belonging to the tree rooted at @a path.
  85. See the comment in AddTree() about symbolic links. @a path should treat
  86. symbolic links in the same way as in the original AddTree() call.
  87. */
  88. virtual bool RemoveTree(const wxFileName& path);
  89. /**
  90. Clears the list of currently watched paths.
  91. */
  92. virtual bool RemoveAll();
  93. /**
  94. Returns the number of currently watched paths.
  95. @see GetWatchedPaths()
  96. */
  97. int GetWatchedPathsCount() const;
  98. /**
  99. Retrieves all watched paths and places them in @a paths. Returns
  100. the number of watched paths, which is also the number of entries added
  101. to @a paths.
  102. */
  103. int GetWatchedPaths(wxArrayString* paths) const;
  104. /**
  105. Associates the file system watcher with the given @a handler object.
  106. All the events generated by this object will be passed to the specified
  107. owner.
  108. */
  109. void SetOwner(wxEvtHandler* handler);
  110. };
  111. /**
  112. @class wxFileSystemWatcherEvent
  113. A class of events sent when a file system event occurs. Types of events
  114. reported may vary depending on a platform, however all platforms report
  115. at least creation of new file/directory and access, modification, move
  116. (rename) or deletion of an existing one.
  117. @library{wxbase}
  118. @category{events}
  119. @see wxFileSystemWatcher
  120. @see @ref overview_events
  121. @since 2.9.1
  122. */
  123. class wxFileSystemWatcherEvent : public wxEvent
  124. {
  125. public:
  126. wxFileSystemWatcherEvent(int changeType = 0,
  127. int watchid = wxID_ANY);
  128. wxFileSystemWatcherEvent(int changeType,
  129. wxFSWWarningType warningType,
  130. const wxString& errorMsg,
  131. int watchid = wxID_ANY);
  132. wxFileSystemWatcherEvent(int changeType,
  133. const wxFileName& path,
  134. const wxFileName& newPath,
  135. int watchid = wxID_ANY);
  136. /**
  137. Returns the path at which the event occurred.
  138. */
  139. const wxFileName& GetPath() const;
  140. /**
  141. Returns the new path of the renamed file/directory if this is a rename
  142. event.
  143. Otherwise it returns the same path as GetPath().
  144. */
  145. const wxFileName& GetNewPath() const;
  146. /**
  147. Returns the type of file system change that occurred. See wxFSWFlags for
  148. the list of possible file system change types.
  149. */
  150. int GetChangeType() const;
  151. /**
  152. Returns @c true if this error is an error event
  153. Error event is an event generated when a warning or error condition
  154. arises.
  155. */
  156. bool IsError() const;
  157. /**
  158. Return a description of the warning or error if this is an error event.
  159. This string may be empty if the exact reason for the error or the
  160. warning is not known.
  161. */
  162. wxString GetErrorDescription() const;
  163. /**
  164. Return the type of the warning if this event is a warning one.
  165. If this is not a warning event, i.e. if GetChangeType() doesn't include
  166. ::wxFSW_EVENT_WARNING, returns ::wxFSW_WARNING_NONE.
  167. @since 3.0
  168. */
  169. wxFSWWarningType GetWarningType() const;
  170. /**
  171. Returns a wxString describing an event, useful for logging, debugging
  172. or testing.
  173. */
  174. wxString ToString() const;
  175. };
  176. wxEventType wxEVT_FSWATCHER;
  177. /**
  178. These are the possible types of file system change events.
  179. Not all of these events are reported on all platforms currently.
  180. @since 2.9.1
  181. */
  182. enum wxFSWFlags
  183. {
  184. /// File or directory was created.
  185. wxFSW_EVENT_CREATE = 0x01,
  186. /// File or directory was deleted.
  187. wxFSW_EVENT_DELETE = 0x02,
  188. /**
  189. File or directory was renamed.
  190. Notice that under MSW this event is sometimes -- although not always --
  191. followed by a ::wxFSW_EVENT_MODIFY for the new file.
  192. Under OS X this event is currently not detected and instead separate
  193. ::wxFSW_EVENT_CREATE and ::wxFSW_EVENT_DELETE events are.
  194. */
  195. wxFSW_EVENT_RENAME = 0x04,
  196. /**
  197. File or directory was modified.
  198. Depending on the program doing the file modification, multiple such
  199. events can be reported for a single logical file update.
  200. Under OS X this event is currently not detected.
  201. */
  202. wxFSW_EVENT_MODIFY = 0x08,
  203. /**
  204. File or directory was accessed.
  205. This event is currently only detected under Linux.
  206. */
  207. wxFSW_EVENT_ACCESS = 0x10,
  208. /**
  209. The item's metadata was changed, e.g.\ its permissions or timestamps.
  210. This event is currently only detected under Linux.
  211. @since 2.9.5
  212. */
  213. wxFSW_EVENT_ATTRIB = 0x20,
  214. /**
  215. The file system containing a watched item was unmounted.
  216. wxFSW_EVENT_UNMOUNT cannot be set; unmount events are produced automatically. This flag
  217. is therefore not included in wxFSW_EVENT_ALL.
  218. This event is currently only detected under Linux.
  219. @since 2.9.5
  220. */
  221. wxFSW_EVENT_UNMOUNT = 0x2000,
  222. /**
  223. A warning condition arose.
  224. This is something that probably needs to be shown to the user in an
  225. interactive program as it can indicate a relatively serious problem,
  226. e.g. some events could have been missed because of an overflow. But
  227. more events will still be coming in the future, unlike for the error
  228. condition below.
  229. */
  230. wxFSW_EVENT_WARNING = 0x40,
  231. /**
  232. An error condition arose.
  233. Errors are fatal, i.e. no more events will be reported after an error
  234. and the program can stop watching the directories currently being
  235. monitored.
  236. */
  237. wxFSW_EVENT_ERROR = 0x80,
  238. wxFSW_EVENT_ALL = wxFSW_EVENT_CREATE | wxFSW_EVENT_DELETE |
  239. wxFSW_EVENT_RENAME | wxFSW_EVENT_MODIFY |
  240. wxFSW_EVENT_ACCESS | wxFSW_EVENT_ATTRIB |
  241. wxFSW_EVENT_WARNING | wxFSW_EVENT_ERROR
  242. };
  243. /**
  244. Possible warning types for the warning events generated by
  245. wxFileSystemWatcher.
  246. @since 3.0
  247. */
  248. enum wxFSWWarningType
  249. {
  250. /**
  251. This is not a warning at all.
  252. */
  253. wxFSW_WARNING_NONE,
  254. /**
  255. A generic warning.
  256. Further information may be provided in the user-readable message
  257. available from wxFileSystemWatcherEvent::GetErrorDescription()
  258. */
  259. wxFSW_WARNING_GENERAL,
  260. /**
  261. An overflow event.
  262. This warning indicates that some file system changes were not signaled
  263. by any events, usually because there were too many of them and the
  264. internally used queue has overflown. If such event is received it is
  265. recommended to completely rescan the files or directories being
  266. monitored.
  267. */
  268. wxFSW_WARNING_OVERFLOW
  269. };