File indexing completed on 2024-04-14 04:46:11
0001 /* 0002 SPDX-FileCopyrightText: 2017 Jean-Baptiste Mardelle 0003 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 */ 0005 0006 #pragma once 0007 0008 #include "definitions.h" 0009 #include <KDirWatch> 0010 #include <QTimer> 0011 #include <unordered_map> 0012 #include <unordered_set> 0013 0014 /** @class FileWatcher 0015 @brief This class is responsible for watching all files used in the project 0016 and triggers a reload notification when a file changes. 0017 */ 0018 class FileWatcher : public QObject 0019 { 0020 Q_OBJECT 0021 0022 public: 0023 // Constructor 0024 explicit FileWatcher(QObject *parent = nullptr); 0025 /** @brief Add a file to the queue for watched items */ 0026 void addFile(const QString &binId, const QString &url); 0027 /** @brief Remove a binId from the list of watched items */ 0028 void removeFile(const QString &binId); 0029 /** @returns True if this url is already watched */ 0030 bool contains(const QString &path) const; 0031 /** @brief Reset all watched files */ 0032 void clear(); 0033 0034 Q_SIGNALS: 0035 /** @brief This signal is triggered whenever the file corresponding to a bin clip has been modified and should be reloaded. Note that this signal is sent no 0036 * more than every 1500 ms. We also make sure that at least 1000ms has passed since the last modification of the file. */ 0037 void binClipModified(const QString &binId); 0038 /** @brief Same signal than binClipModified, but triggers immediately. Can be useful to refresh UI without actually reloading the file (yet)*/ 0039 void binClipWaiting(const QString &binId); 0040 void binClipMissing(const QString &binId); 0041 0042 private Q_SLOTS: 0043 void slotUrlModified(const QString &path); 0044 void slotUrlMissing(const QString &path); 0045 void slotUrlAdded(const QString &path); 0046 void slotProcessModifiedUrls(); 0047 void slotProcessQueue(); 0048 0049 private: 0050 /// This is a handle to the watcher singleton, not owned by this class. 0051 std::unique_ptr<KDirWatch> m_fileWatcher; 0052 /// A list with urls as keys, and the corresponding clip ids as value 0053 std::unordered_map<QString, std::unordered_set<QString>> m_occurences; 0054 /// keys are binId, keys are stored paths 0055 std::unordered_map<QString, QString> m_binClipPaths; 0056 0057 /// List of files for which we received an update since the last send 0058 std::unordered_set<QString> m_modifiedUrls; 0059 0060 /// When loading a project or adding many clips, adding many files to the watcher causes a freeze, so queue them 0061 std::unordered_map<QString, QString> m_pendingUrls; 0062 0063 QTimer m_modifiedTimer; 0064 QTimer m_queueTimer; 0065 /// Add a file to the list of watched items 0066 void doAddFile(const QString &binId, const QString &url); 0067 };