BsFolderMonitor.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 bs
  6. {
  7. /** @addtogroup Platform-Internal
  8. * @{
  9. */
  10. /** Types of notifications we would like to receive when we start a FolderMonitor on a certain folder. */
  11. enum class FolderChangeBit
  12. {
  13. FileName = 1 << 0, /**< Called when file is created, moved or removed. */
  14. DirName = 1 << 1, /**< Called when directory is created, moved or removed. */
  15. FileWrite = 1 << 2, /**< Called when file is written to. */
  16. };
  17. typedef Flags<FolderChangeBit> FolderChangeBits;
  18. BS_FLAGS_OPERATORS(FolderChangeBit)
  19. /**
  20. * Allows monitoring a file system folder for changes. Depending on the flags set this monitor can notify you when file
  21. * is changed/moved/renamed and similar.
  22. */
  23. class BS_CORE_EXPORT FolderMonitor
  24. {
  25. class FileNotifyInfo;
  26. public:
  27. struct Pimpl;
  28. struct FolderWatchInfo;
  29. FolderMonitor();
  30. ~FolderMonitor();
  31. /**
  32. * Starts monitoring a folder at the specified path.
  33. *
  34. * @param[in] folderPath Absolute path to the folder you want to monitor.
  35. * @param[in] subdirectories If true, provided folder and all of its subdirectories will be monitored for
  36. * changes. Otherwise only the provided folder will be monitored.
  37. * @param[in] changeFilter A set of flags you may OR together. Different notification events will trigger
  38. * depending on which flags you set.
  39. */
  40. void startMonitor(const Path& folderPath, bool subdirectories, FolderChangeBits changeFilter);
  41. /** Stops monitoring the folder at the specified path. */
  42. void stopMonitor(const Path& folderPath);
  43. /** Stops monitoring all folders that are currently being monitored. */
  44. void stopMonitorAll();
  45. /** Triggers callbacks depending on events that ocurred. Expected to be called once per frame. */
  46. void _update();
  47. /** Triggers when a file in the monitored folder is modified. Provides absolute path to the file. */
  48. Event<void(const Path&)> onModified;
  49. /** Triggers when a file/folder is added in the monitored folder. Provides absolute path to the file/folder. */
  50. Event<void(const Path&)> onAdded;
  51. /** Triggers when a file/folder is removed from the monitored folder. Provides absolute path to the file/folder. */
  52. Event<void(const Path&)> onRemoved;
  53. /** Triggers when a file/folder is renamed in the monitored folder. Provides absolute path with old and new names. */
  54. Event<void(const Path&, const Path&)> onRenamed;
  55. /**
  56. * @name Internal
  57. * @{
  58. */
  59. /** Returns private data, for use by internal helper classes and methods. */
  60. Pimpl* _getPrivateData() const { return m; }
  61. /** @} */
  62. private:
  63. /** Worker method that monitors the IO ports for any modification notifications. */
  64. void workerThreadMain();
  65. /** Called by the worker thread whenever a modification notification is received. */
  66. void handleNotifications(FileNotifyInfo& notifyInfo, FolderWatchInfo& watchInfo);
  67. Pimpl* m;
  68. };
  69. /** @} */
  70. }