BsWin32FolderMonitor.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. namespace BansheeEngine
  4. {
  5. /** @cond INTERNAL */
  6. /** @addtogroup Platform
  7. * @{
  8. */
  9. /** Types of notifications we would like to receive when we start a FolderMonitor on a certain folder. */
  10. enum class FolderChange
  11. {
  12. FileName = 0x0001, /**< Called when filename changes. */
  13. DirName = 0x0002, /**< Called when directory name changes. */
  14. Attributes = 0x0004, /**< Called when attributes changes. */
  15. Size = 0x0008, /**< Called when file size changes. */
  16. LastWrite = 0x0010, /**< Called when file is written to. */
  17. LastAccess = 0x0020, /**< Called when file is accessed. */
  18. Creation = 0x0040, /**< Called when file is created. */
  19. Security = 0x0080 /**< Called when file security descriptor changes. */
  20. };
  21. /**
  22. * Allows monitoring a file system folder for changes. Depending on the flags set this monitor can notify you when file
  23. * is changed/moved/renamed and similar.
  24. */
  25. class BS_CORE_EXPORT FolderMonitor
  26. {
  27. struct Pimpl;
  28. class FileNotifyInfo;
  29. struct FolderWatchInfo;
  30. public:
  31. FolderMonitor();
  32. ~FolderMonitor();
  33. /**
  34. * Starts monitoring a folder at the specified path.
  35. *
  36. * @param[in] folderPath Absolute path to the folder you want to monitor.
  37. * @param[in] subdirectories If true, provided folder and all of its subdirectories will be monitored for
  38. * changes. Otherwise only the provided folder will be monitored.
  39. * @param[in] changeFilter A set of flags you may OR together. Different notification events will trigger
  40. * depending on which flags you set.
  41. */
  42. void startMonitor(const Path& folderPath, bool subdirectories, FolderChange changeFilter);
  43. /** Stops monitoring the folder at the specified path. */
  44. void stopMonitor(const Path& folderPath);
  45. /** Stops monitoring all folders that are currently being monitored. */
  46. void stopMonitorAll();
  47. /**
  48. * Callbacks will only get fired after update is called().
  49. *
  50. * @note Internal method.
  51. */
  52. void _update();
  53. /** Triggers when a file in the monitored folder is modified. Provides absolute path to the file. */
  54. Event<void(const Path&)> onModified;
  55. /** Triggers when a file/folder is added in the monitored folder. Provides absolute path to the file/folder. */
  56. Event<void(const Path&)> onAdded;
  57. /** Triggers when a file/folder is removed from the monitored folder. Provides absolute path to the file/folder. */
  58. Event<void(const Path&)> onRemoved;
  59. /** Triggers when a file/folder is renamed in the monitored folder. Provides absolute path with old and new names. */
  60. Event<void(const Path&, const Path&)> onRenamed;
  61. private:
  62. /** Worker method that monitors the IO ports for any modification notifications. */
  63. void workerThreadMain();
  64. /** Called by the worker thread whenever a modification notification is received. */
  65. void handleNotifications(FileNotifyInfo& notifyInfo, FolderWatchInfo& watchInfo);
  66. Pimpl* mPimpl;
  67. };
  68. /** @} */
  69. /** @endcond */
  70. }