FileChangeMonitor.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_EDITOR_UTIL_FILECHANGEMONITOR_H
  9. #define CRYINCLUDE_EDITOR_UTIL_FILECHANGEMONITOR_H
  10. #pragma once
  11. #if !defined(Q_MOC_RUN)
  12. #include <AzCore/std/containers/set.h>
  13. #include <QFileInfoList>
  14. #include <QFileSystemWatcher>
  15. #include <QObject>
  16. #include <QQueue>
  17. #include <QScopedPointer>
  18. #endif
  19. class CFileChangeMonitorListener;
  20. struct SFileChangeInfo
  21. {
  22. enum EChangeType
  23. {
  24. //! error or unknown change type
  25. eChangeType_Unknown,
  26. //! the file was created
  27. eChangeType_Created,
  28. //! the file was deleted
  29. eChangeType_Deleted,
  30. //! the file was modified (size changed,write)
  31. eChangeType_Modified,
  32. //! this is the old name of a renamed file
  33. eChangeType_RenamedOldName,
  34. //! this is the new name of a renamed file
  35. eChangeType_RenamedNewName
  36. };
  37. SFileChangeInfo()
  38. : changeType(eChangeType_Unknown)
  39. {
  40. }
  41. bool operator==(const SFileChangeInfo& rhs) const
  42. {
  43. return changeType == rhs.changeType && filename == rhs.filename;
  44. }
  45. QString filename;
  46. EChangeType changeType;
  47. };
  48. // Monitors directory for any changed files
  49. class CFileChangeMonitor : public QObject
  50. {
  51. public:
  52. friend class CEditorFileMonitor;
  53. typedef AZStd::set<CFileChangeMonitorListener*> TListeners;
  54. protected:
  55. explicit CFileChangeMonitor(QObject* parent = nullptr);
  56. ~CFileChangeMonitor();
  57. void Initialize();
  58. static void DeleteInstance();
  59. static CFileChangeMonitor* s_pFileMonitorInstance;
  60. public:
  61. static CFileChangeMonitor* Instance();
  62. bool MonitorItem(const QString& sItem);
  63. void StopMonitor();
  64. void SetEnabled(bool bEnable);
  65. bool IsEnabled();
  66. //! get next modified file, this file will be delete from list after calling this function,
  67. //! call it until HaveModifiedFiles return true or this function returns false
  68. void Subscribe(CFileChangeMonitorListener* pListener);
  69. void Unsubscribe(CFileChangeMonitorListener* pListener);
  70. bool IsDirectory(const char* pFilename);
  71. bool IsFile(const char* pFilename);
  72. bool IsLoggingChanges() const
  73. {
  74. return ed_logFileChanges != 0;
  75. }
  76. void AddIgnoreFileMask(const char* pMask);
  77. void RemoveIgnoreFileMask(const char* pMask, int aAfterDelayMsec = 1000);
  78. private:
  79. void OnDirectoryChange(const QString &path);
  80. void OnFileChange(const QString &path);
  81. void NotifyListeners(const QString &path, SFileChangeInfo::EChangeType changeType);
  82. int ed_logFileChanges;
  83. QScopedPointer<QFileSystemWatcher> m_watcher;
  84. TListeners m_listeners;
  85. QQueue<SFileChangeInfo> m_changes;
  86. QStringList m_ignoreMasks;
  87. QHash<QString, QFileInfoList> m_entries;
  88. };
  89. // Used as base class (aka interface) to subscribe for file change events
  90. class CFileChangeMonitorListener
  91. {
  92. public:
  93. CFileChangeMonitorListener()
  94. : m_pMonitor(nullptr)
  95. {
  96. }
  97. virtual ~CFileChangeMonitorListener()
  98. {
  99. if (m_pMonitor)
  100. {
  101. m_pMonitor->Unsubscribe(this);
  102. }
  103. }
  104. virtual void OnFileMonitorChange(const SFileChangeInfo& rChange) = 0;
  105. void SetMonitor(CFileChangeMonitor* pMonitor)
  106. {
  107. m_pMonitor = pMonitor;
  108. }
  109. private:
  110. CFileChangeMonitor* m_pMonitor;
  111. };
  112. #endif // CRYINCLUDE_EDITOR_UTIL_FILECHANGEMONITOR_H