/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // Original file Copyright Crytek GMBH or its affiliates, used under license. #ifndef CRYINCLUDE_EDITOR_UTIL_FILECHANGEMONITOR_H #define CRYINCLUDE_EDITOR_UTIL_FILECHANGEMONITOR_H #pragma once #include #include #include #include #include #include class CFileChangeMonitorListener; struct SFileChangeInfo { enum EChangeType { //! error or unknown change type eChangeType_Unknown, //! the file was created eChangeType_Created, //! the file was deleted eChangeType_Deleted, //! the file was modified (size changed,write) eChangeType_Modified, //! this is the old name of a renamed file eChangeType_RenamedOldName, //! this is the new name of a renamed file eChangeType_RenamedNewName }; SFileChangeInfo() : changeType(eChangeType_Unknown) { } bool operator==(const SFileChangeInfo& rhs) const { return changeType == rhs.changeType && filename == rhs.filename; } QString filename; EChangeType changeType; }; // Monitors directory for any changed files class CFileChangeMonitor : public QObject { public: friend class CEditorFileMonitor; typedef AZStd::set TListeners; protected: explicit CFileChangeMonitor(QObject* parent = nullptr); ~CFileChangeMonitor(); void Initialize(); static void DeleteInstance(); static CFileChangeMonitor* s_pFileMonitorInstance; public: static CFileChangeMonitor* Instance(); bool MonitorItem(const QString& sItem); void StopMonitor(); void SetEnabled(bool bEnable); bool IsEnabled(); //! get next modified file, this file will be delete from list after calling this function, //! call it until HaveModifiedFiles return true or this function returns false void Subscribe(CFileChangeMonitorListener* pListener); void Unsubscribe(CFileChangeMonitorListener* pListener); bool IsDirectory(const char* pFilename); bool IsFile(const char* pFilename); bool IsLoggingChanges() const { return ed_logFileChanges != 0; } void AddIgnoreFileMask(const char* pMask); void RemoveIgnoreFileMask(const char* pMask, int aAfterDelayMsec = 1000); private: void OnDirectoryChange(const QString &path); void OnFileChange(const QString &path); void NotifyListeners(const QString &path, SFileChangeInfo::EChangeType changeType); int ed_logFileChanges; QScopedPointer m_watcher; TListeners m_listeners; QQueue m_changes; QStringList m_ignoreMasks; QHash m_entries; }; // Used as base class (aka interface) to subscribe for file change events class CFileChangeMonitorListener { public: CFileChangeMonitorListener() : m_pMonitor(NULL) { } virtual ~CFileChangeMonitorListener() { if (m_pMonitor) { m_pMonitor->Unsubscribe(this); } } virtual void OnFileMonitorChange(const SFileChangeInfo& rChange) = 0; void SetMonitor(CFileChangeMonitor* pMonitor) { m_pMonitor = pMonitor; } private: CFileChangeMonitor* m_pMonitor; }; #endif // CRYINCLUDE_EDITOR_UTIL_FILECHANGEMONITOR_H