BsFolderMonitor.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. struct Pimpl;
  26. class FileNotifyInfo;
  27. struct FolderWatchInfo;
  28. public:
  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. /** Callbacks will only get fired after update is called. */
  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. private:
  56. /** Worker method that monitors the IO ports for any modification notifications. */
  57. void workerThreadMain();
  58. /** Called by the worker thread whenever a modification notification is received. */
  59. void handleNotifications(FileNotifyInfo& notifyInfo, FolderWatchInfo& watchInfo);
  60. Pimpl* m;
  61. };
  62. /** @} */
  63. }