BsWin32FolderMonitor.h 3.0 KB

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