BsWin32FolderMonitor.h 3.2 KB

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