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