Debian Codegraph @

src/filesystem/dbasefilewatcher.cpp

raw
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dbasefilewatcher.h"
6 #include "private/dbasefilewatcher_p.h"
7
8 #include <QEvent>
9 #include <QDebug>
10
11 DCORE_BEGIN_NAMESPACE
12
13 QList<DBaseFileWatcher*> DBaseFileWatcherPrivate::watcherList;
14 DBaseFileWatcherPrivate::DBaseFileWatcherPrivate(DBaseFileWatcher *qq)
15 : DObjectPrivate(qq)
16 {
17
18 }
19
20 /*!
21 @~english
22 @class Dtk::Core::DBaseFileWatcher
23 @ingroup dtkcore
24
25 @brief The DBaseFileWatcher class provides an interface for monitoring files and directories for modifications.
26 */
27
28 DBaseFileWatcher::~DBaseFileWatcher()
29 {
30 stopWatcher();
31 DBaseFileWatcherPrivate::watcherList.removeOne(this);
32 }
33
34 QUrl DBaseFileWatcher::fileUrl() const
35 {
36 Q_D(const DBaseFileWatcher);
37
38 return d->url;
39 }
40
41 /*!
42 @~english
43 @brief Let file watcher start watching file changes.
44 @sa stopWatcher(), restartWatcher()
45 */
46 bool DBaseFileWatcher::startWatcher()
47 {
48 Q_D(DBaseFileWatcher);
49
50 if (d->started)
51 return true;
52
53 if (d->start()) {
54 d->started = true;
55
56 return true;
57 }
58
59 return false;
60 }
61
62 /*!
63 @~english
64 @brief Stop watching file changes.
65 @sa startWatcher(), restartWatcher()
66 */
67 bool DBaseFileWatcher::stopWatcher()
68 {
69 Q_D(DBaseFileWatcher);
70
71 if (!d->started)
72 return false;
73
74 if (d->stop()) {
75 d->started = false;
76
77 return true;
78 }
79
80 return false;
81 }
82
83 /*!
84 @~english
85 @brief Stop file watcher and then restart it to watching file changes.
86 @sa startWatcher(), stopWatcher()
87 */
88 bool DBaseFileWatcher::restartWatcher()
89 {
90 bool ok = stopWatcher();
91 return ok && startWatcher();
92 }
93
94 /*!
95 @~english
96 @brief Set enable file watcher for \a subfileUrl or not
97 @param[in] subfileUrl The given url
98 @param[in] enabled Enable file change watching or not.
99 */
100 void DBaseFileWatcher::setEnabledSubfileWatcher(const QUrl &subfileUrl, bool enabled)
101 {
102 Q_UNUSED(subfileUrl)
103 Q_UNUSED(enabled)
104 }
105
106 /*!
107 @~english
108 @brief Emit a signal about \a targetUrl got a \a signal with \a arg1
109 Example usage:
110
111 @code
112 DBaseFileWatcher::ghostSignal(QUrl("bookmark:///"), &DBaseFileWatcher::fileDeleted, QUrl("bookmark:///bookmarkFile1"));
113 @endcode
114
115 @return 成功发送返回 true,否则返回 false.
116 */
117 bool DBaseFileWatcher::ghostSignal(const QUrl &targetUrl, DBaseFileWatcher::SignalType1 signal, const QUrl &arg1)
118 {
119 if (!signal)
120 return false;
121
122 bool ok = false;
123
124 for (DBaseFileWatcher *watcher : DBaseFileWatcherPrivate::watcherList) {
125 if (watcher->fileUrl() == targetUrl) {
126 ok = true;
127 (watcher->*signal)(arg1);
128 }
129 }
130
131 return ok;
132 }
133
134 /*!
135 @~english
136 @brief Emit a signal about \a targetUrl got a \a signal with \a arg1 and \a arg2
137 Example usage:
138 @code
139 DBaseFileWatcher::ghostSignal(QUrl("bookmark:///"), &DBaseFileWatcher::fileMoved, QUrl("bookmark:///bookmarkFile1"), QUrl("bookmark:///NewNameFile1"));
140 @endcode
141 */
142 bool DBaseFileWatcher::ghostSignal(const QUrl &targetUrl, DBaseFileWatcher::SignalType2 signal, const QUrl &arg1, const QUrl &arg2)
143 {
144 if (!signal)
145 return false;
146
147 bool ok = false;
148
149 for (DBaseFileWatcher *watcher : DBaseFileWatcherPrivate::watcherList) {
150 if (watcher->fileUrl() == targetUrl) {
151 ok = true;
152 (watcher->*signal)(arg1, arg2);
153 }
154 }
155
156 return ok;
157 }
158
159 DBaseFileWatcher::DBaseFileWatcher(DBaseFileWatcherPrivate &dd,
160 const QUrl &url, QObject *parent)
161 : QObject(parent)
162 , DObject(dd)
163 {
164 Q_ASSERT(url.isValid());
165
166 d_func()->url = url;
167 DBaseFileWatcherPrivate::watcherList << this;
168 }
169
170 DCORE_END_NAMESPACE
171
172 //#include "moc_dbasefilewatcher.cpp"