Debian Codegraph @

src/filesystem/dfilewatchermanager.cpp

raw
1 // SPDX-FileCopyrightText: 2017 - 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dfilewatchermanager.h"
6 #include "dfilewatcher.h"
7 #include "base/private/dobject_p.h"
8
9 #include <QMap>
10 #include <QUrl>
11
12 DCORE_BEGIN_NAMESPACE
13
14 class DFileWatcherManagerPrivate : public DObjectPrivate
15 {
16 public:
17 DFileWatcherManagerPrivate(DFileWatcherManager *qq);
18
19 QMap<QString, DFileWatcher *> watchersMap;
20
21 D_DECLARE_PUBLIC(DFileWatcherManager)
22 };
23
24 DFileWatcherManagerPrivate::DFileWatcherManagerPrivate(DFileWatcherManager *qq)
25 : DObjectPrivate(qq)
26 {
27 }
28
29 /*!
30 @~english
31 \class Dtk::Core::DFileWatcherManager
32 \inmodule dtkcore
33 \brief The DFileWatcherManager class can help you manage file watchers and get signal when file got changed.
34 */
35
36 DFileWatcherManager::DFileWatcherManager(QObject *parent)
37 : QObject(parent)
38 , DObject(*new DFileWatcherManagerPrivate(this))
39 {
40 }
41
42 DFileWatcherManager::~DFileWatcherManager() {}
43
44 /*!
45 @~english
46 \brief Add file watcher for \a filePath to the file watcher manager.
47 \return The file watcher which got created and added into the file watcher manager.
48 */
49 DFileWatcher *DFileWatcherManager::add(const QString &filePath)
50 {
51 Q_D(DFileWatcherManager);
52
53 DFileWatcher *watcher = d->watchersMap.value(filePath);
54
55 if (watcher) {
56 return watcher;
57 }
58
59 watcher = new DFileWatcher(filePath, this);
60
61 connect(watcher, &DFileWatcher::fileAttributeChanged, this, [this](const QUrl &url) {
62 Q_EMIT fileAttributeChanged(url.toLocalFile());
63 });
64 connect(watcher, &DFileWatcher::fileClosed, this, [this](const QUrl &url) { Q_EMIT fileClosed(url.toLocalFile()); });
65 connect(watcher, &DFileWatcher::fileDeleted, this, [this](const QUrl &url) { Q_EMIT fileDeleted(url.toLocalFile()); });
66 connect(watcher, &DFileWatcher::fileModified, this, [this](const QUrl &url) { Q_EMIT fileModified(url.toLocalFile()); });
67 connect(watcher, &DFileWatcher::fileMoved, this, [this](const QUrl &fromUrl, const QUrl &toUrl) {
68 Q_EMIT fileMoved(fromUrl.toLocalFile(), toUrl.toLocalFile());
69 });
70 connect(watcher, &DFileWatcher::subfileCreated, this, [this](const QUrl &url) { Q_EMIT subfileCreated(url.toLocalFile()); });
71
72 d->watchersMap[filePath] = watcher;
73 watcher->startWatcher();
74
75 return watcher;
76 }
77
78 /*!
79 @~english
80 \brief Remove file watcher for \a filePath from the file watcher manager.
81 */
82 void DFileWatcherManager::remove(const QString &filePath)
83 {
84 Q_D(DFileWatcherManager);
85
86 DFileWatcher *watcher = d->watchersMap.take(filePath);
87
88 if (watcher) {
89 watcher->deleteLater();
90 }
91 }
92
93 /*!
94 @~english
95 @brief Remove all file watcher
96 */
97 void DFileWatcherManager::removeAll()
98 {
99 Q_D(DFileWatcherManager);
100 for (auto it : d->watchersMap) {
101 it->deleteLater();
102 }
103 d->watchersMap.clear();
104 }
105
106 /*!
107 @~english
108 @brief Show all file watcher
109 */
110 QStringList DFileWatcherManager::watchedFiles() const
111 {
112 Q_D(const DFileWatcherManager);
113 return d->watchersMap.keys();
114 }
115
116 DCORE_END_NAMESPACE