FileChangeMonitor.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "EditorDefs.h"
  9. #include "FileChangeMonitor.h"
  10. // Qt
  11. #include <QDateTime>
  12. #include <QTimer>
  13. CFileChangeMonitor* CFileChangeMonitor::s_pFileMonitorInstance = nullptr;
  14. //////////////////////////////////////////////////////////////////////////
  15. CFileChangeMonitor::CFileChangeMonitor(QObject* parent)
  16. : QObject(parent)
  17. {
  18. ed_logFileChanges = 0;
  19. }
  20. //////////////////////////////////////////////////////////////////////////
  21. CFileChangeMonitor::~CFileChangeMonitor()
  22. {
  23. for (TListeners::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it)
  24. {
  25. CFileChangeMonitorListener* pListener = *it;
  26. if (pListener)
  27. {
  28. pListener->SetMonitor(nullptr);
  29. }
  30. }
  31. // Send to thread a kill event.
  32. StopMonitor();
  33. }
  34. CFileChangeMonitor* CFileChangeMonitor::Instance()
  35. {
  36. if (!s_pFileMonitorInstance)
  37. {
  38. s_pFileMonitorInstance = new CFileChangeMonitor();
  39. s_pFileMonitorInstance->Initialize();
  40. }
  41. return s_pFileMonitorInstance;
  42. }
  43. void CFileChangeMonitor::DeleteInstance()
  44. {
  45. SAFE_DELETE(s_pFileMonitorInstance);
  46. }
  47. void CFileChangeMonitor::Initialize()
  48. {
  49. REGISTER_CVAR(ed_logFileChanges, 0, VF_NULL, "If its 1, then enable the logging of file monitor file changes");
  50. m_watcher.reset(new QFileSystemWatcher);
  51. connect(m_watcher.data(), &QFileSystemWatcher::fileChanged,
  52. this, &CFileChangeMonitor::OnFileChange);
  53. connect(m_watcher.data(), &QFileSystemWatcher::directoryChanged,
  54. this, &CFileChangeMonitor::OnDirectoryChange);
  55. AddIgnoreFileMask("*$tmp*");
  56. }
  57. bool CFileChangeMonitor::IsDirectory(const char* sFileName)
  58. {
  59. QFileInfo finfo(sFileName);
  60. return finfo.isDir();
  61. }
  62. //////////////////////////////////////////////////////////////////////////
  63. bool CFileChangeMonitor::IsFile(const char* sFileName)
  64. {
  65. QFileInfo finfo(sFileName);
  66. return finfo.isFile();
  67. }
  68. void CFileChangeMonitor::AddIgnoreFileMask(const char* pMask)
  69. {
  70. Log("Adding '%s' to ignore masks for changed files.", pMask);
  71. m_ignoreMasks.append(QString::fromLatin1(pMask));
  72. }
  73. void CFileChangeMonitor::RemoveIgnoreFileMask(const char* pMask, int aAfterDelayMsec)
  74. {
  75. QTimer::singleShot(aAfterDelayMsec, [=]() {
  76. m_ignoreMasks.removeAll(QString::fromLatin1(pMask));
  77. });
  78. }
  79. //////////////////////////////////////////////////////////////////////////
  80. bool CFileChangeMonitor::MonitorItem(const QString& sItem)
  81. {
  82. QFileInfo finfo(sItem);
  83. if (finfo.isDir())
  84. {
  85. QDir dir(sItem);
  86. m_entries.insert(sItem, std::move(dir.entryInfoList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot)));
  87. }
  88. return m_watcher->addPath(sItem);
  89. }
  90. void CFileChangeMonitor::StopMonitor()
  91. {
  92. if (m_watcher)
  93. {
  94. disconnect(m_watcher.data(), &QFileSystemWatcher::fileChanged,
  95. this, &CFileChangeMonitor::OnFileChange);
  96. disconnect(m_watcher.data(), &QFileSystemWatcher::directoryChanged,
  97. this, &CFileChangeMonitor::OnDirectoryChange);
  98. }
  99. }
  100. void CFileChangeMonitor::SetEnabled(bool bEnable)
  101. {
  102. m_watcher->blockSignals(!bEnable);
  103. }
  104. bool CFileChangeMonitor::IsEnabled()
  105. {
  106. return !m_watcher->signalsBlocked();
  107. }
  108. void CFileChangeMonitor::Subscribe(CFileChangeMonitorListener* pListener)
  109. {
  110. assert(pListener);
  111. pListener->SetMonitor(this);
  112. m_listeners.insert(pListener);
  113. }
  114. void CFileChangeMonitor::Unsubscribe(CFileChangeMonitorListener* pListener)
  115. {
  116. assert(pListener);
  117. m_listeners.erase(pListener);
  118. pListener->SetMonitor(nullptr);
  119. }
  120. void CFileChangeMonitor::OnDirectoryChange(const QString &path)
  121. {
  122. QDir dir(path);
  123. auto entries = dir.entryInfoList(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
  124. const auto prev = m_entries.value(path);
  125. for (const auto &fi : prev)
  126. {
  127. int eindex = entries.indexOf(fi);
  128. if (eindex >= 0)
  129. {
  130. if (fi.lastModified() != entries.at(eindex).lastModified())
  131. {
  132. NotifyListeners(fi.canonicalFilePath(), SFileChangeInfo::eChangeType_Modified);
  133. }
  134. }
  135. else
  136. {
  137. NotifyListeners(fi.canonicalFilePath(), SFileChangeInfo::eChangeType_Deleted);
  138. }
  139. }
  140. for (const auto &fi : entries)
  141. {
  142. if (!prev.contains(fi))
  143. {
  144. NotifyListeners(fi.canonicalFilePath(), SFileChangeInfo::eChangeType_Created);
  145. }
  146. }
  147. m_entries.insert(path, std::move(entries));
  148. NotifyListeners(path, SFileChangeInfo::eChangeType_Modified);
  149. }
  150. void CFileChangeMonitor::OnFileChange(const QString &path)
  151. {
  152. QFileInfo finfo(path);
  153. NotifyListeners(path, finfo.exists() ? SFileChangeInfo::eChangeType_Modified : SFileChangeInfo::eChangeType_Deleted);
  154. }
  155. void CFileChangeMonitor::NotifyListeners(const QString &path, SFileChangeInfo::EChangeType changeType)
  156. {
  157. for (const auto &glob : m_ignoreMasks)
  158. {
  159. QRegExp exp(glob, Qt::CaseInsensitive, QRegExp::Wildcard);
  160. if (path.contains(exp))
  161. {
  162. return; // mask matches, ignore event
  163. }
  164. }
  165. SFileChangeInfo change;
  166. change.filename = path;
  167. change.changeType = changeType;
  168. for (auto it = m_listeners.begin(); it != m_listeners.end(); ++it)
  169. {
  170. CFileChangeMonitorListener* pListener = *it;
  171. if (pListener)
  172. {
  173. pListener->OnFileMonitorChange(change);
  174. }
  175. }
  176. }