| 1 |
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dfilesystemwatcher.h" |
| 6 |
#include "private/dfilesystemwatcher_dummy_p.h" |
| 7 |
|
| 8 |
DCORE_BEGIN_NAMESPACE |
| 9 |
|
| 10 |
DFileSystemWatcherPrivate::DFileSystemWatcherPrivate(int fd, DFileSystemWatcher *qq) |
| 11 |
: DObjectPrivate(qq) |
| 12 |
{ |
| 13 |
|
| 14 |
} |
| 15 |
|
| 16 |
DFileSystemWatcherPrivate::~DFileSystemWatcherPrivate() |
| 17 |
{ |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
/*! |
| 22 |
@~english |
| 23 |
@class Dtk::Core::DFileSystemWatcher |
| 24 |
\inmodule dtkcore |
| 25 |
\brief The DFileSystemWatcher class provides an interface for monitoring files and directories for modifications. |
| 26 |
|
| 27 |
DFileSystemWatcher monitors the file system for changes to files |
| 28 |
and directories by watching a list of specified paths. |
| 29 |
|
| 30 |
Call addPath() to watch a particular file or directory. Multiple |
| 31 |
paths can be added using the addPaths() function. Existing paths can |
| 32 |
be removed by using the removePath() and removePaths() functions. |
| 33 |
|
| 34 |
DFileSystemWatcher examines each path added to it. Files that have |
| 35 |
been added to the DFileSystemWatcher can be accessed using the |
| 36 |
files() function, and directories using the directories() function. |
| 37 |
|
| 38 |
The fileChanged() signal is emitted when a file has been modified, |
| 39 |
renamed or removed from disk. Similarly, the directoryChanged() |
| 40 |
signal is emitted when a directory or its contents is modified or |
| 41 |
removed. Note that DFileSystemWatcher stops monitoring files once |
| 42 |
they have been renamed or removed from disk, and directories once |
| 43 |
they have been removed from disk. |
| 44 |
|
| 45 |
@note On systems running a Linux kernel without inotify support, |
| 46 |
file systems that contain watched paths cannot be unmounted. |
| 47 |
|
| 48 |
@note Windows CE does not support directory monitoring by |
| 49 |
default as this depends on the file system driver installed. |
| 50 |
|
| 51 |
@note The act of monitoring files and directories for |
| 52 |
modifications consumes system resources. This implies there is a |
| 53 |
limit to the number of files and directories your process can |
| 54 |
monitor simultaneously. On all BSD variants, for |
| 55 |
example, an open file descriptor is required for each monitored |
| 56 |
file. Some system limits the number of open file descriptors to 256 |
| 57 |
by default. This means that addPath() and addPaths() will fail if |
| 58 |
your process tries to add more than 256 files or directories to |
| 59 |
the file system monitor. Also note that your process may have |
| 60 |
other file descriptors open in addition to the ones for files |
| 61 |
being monitored, and these other open descriptors also count in |
| 62 |
the total. OS X uses a different backend and does not |
| 63 |
suffer from this issue. |
| 64 |
*/ |
| 65 |
|
| 66 |
|
| 67 |
/*! |
| 68 |
@~english |
| 69 |
Constructs a new file system watcher object with the given \a parent. |
| 70 |
*/ |
| 71 |
DFileSystemWatcher::DFileSystemWatcher(QObject *parent) |
| 72 |
: QObject(parent) |
| 73 |
, DObject() |
| 74 |
{ |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
/*! |
| 79 |
@~english |
| 80 |
Constructs a new file system watcher object with the given \a parent |
| 81 |
which monitors the specified \a paths list. |
| 82 |
*/ |
| 83 |
DFileSystemWatcher::DFileSystemWatcher(const QStringList &paths, QObject *parent) |
| 84 |
: DFileSystemWatcher(parent) |
| 85 |
{ |
| 86 |
addPaths(paths); |
| 87 |
} |
| 88 |
|
| 89 |
/*! |
| 90 |
@~english |
| 91 |
Destroys the file system watcher. |
| 92 |
*/ |
| 93 |
DFileSystemWatcher::~DFileSystemWatcher() |
| 94 |
{ } |
| 95 |
|
| 96 |
/*! |
| 97 |
@~english |
| 98 |
Adds \a path to the file system watcher if \a path exists. The |
| 99 |
path is not added if it does not exist, or if it is already being |
| 100 |
monitored by the file system watcher. |
| 101 |
|
| 102 |
If \a path specifies a directory, the directoryChanged() signal |
| 103 |
will be emitted when \a path is modified or removed from disk; |
| 104 |
otherwise the fileChanged() signal is emitted when \a path is |
| 105 |
modified, renamed or removed. |
| 106 |
|
| 107 |
If the watch was successful, true is returned. |
| 108 |
|
| 109 |
Reasons for a watch failure are generally system-dependent, but |
| 110 |
may include the resource not existing, access failures, or the |
| 111 |
total watch count limit, if the platform has one. |
| 112 |
|
| 113 |
@note There may be a system dependent limit to the number of |
| 114 |
files and directories that can be monitored simultaneously. |
| 115 |
If this limit is been reached, \a path will not be monitored, |
| 116 |
and false is returned. |
| 117 |
|
| 118 |
@sa addPaths(), removePath() |
| 119 |
*/ |
| 120 |
bool DFileSystemWatcher::addPath(const QString &path) |
| 121 |
{ |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
/*! |
| 126 |
@~english |
| 127 |
Adds each path in \a paths to the file system watcher. Paths are |
| 128 |
not added if they not exist, or if they are already being |
| 129 |
monitored by the file system watcher. |
| 130 |
|
| 131 |
If a path specifies a directory, the directoryChanged() signal |
| 132 |
will be emitted when the path is modified or removed from disk; |
| 133 |
otherwise the fileChanged() signal is emitted when the path is |
| 134 |
modified, renamed, or removed. |
| 135 |
|
| 136 |
The return value is a list of paths that could not be watched. |
| 137 |
|
| 138 |
Reasons for a watch failure are generally system-dependent, but |
| 139 |
may include the resource not existing, access failures, or the |
| 140 |
total watch count limit, if the platform has one. |
| 141 |
|
| 142 |
@note There may be a system dependent limit to the number of |
| 143 |
files and directories that can be monitored simultaneously. |
| 144 |
If this limit has been reached, the excess \a paths will not |
| 145 |
be monitored, and they will be added to the returned QStringList. |
| 146 |
|
| 147 |
@sa addPath(), removePaths() |
| 148 |
*/ |
| 149 |
QStringList DFileSystemWatcher::addPaths(const QStringList &paths) |
| 150 |
{ |
| 151 |
return QStringList(); |
| 152 |
} |
| 153 |
|
| 154 |
/*! |
| 155 |
@~english |
| 156 |
Removes the specified \a path from the file system watcher. |
| 157 |
|
| 158 |
If the watch is successfully removed, true is returned. |
| 159 |
|
| 160 |
Reasons for watch removal failing are generally system-dependent, |
| 161 |
but may be due to the path having already been deleted, for example. |
| 162 |
|
| 163 |
@sa removePaths(), addPath() |
| 164 |
*/ |
| 165 |
bool DFileSystemWatcher::removePath(const QString &path) |
| 166 |
{ |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
/*! |
| 171 |
@~english |
| 172 |
Removes the specified \a paths from the file system watcher. |
| 173 |
|
| 174 |
The return value is a list of paths which were not able to be |
| 175 |
unwatched successfully. |
| 176 |
|
| 177 |
Reasons for watch removal failing are generally system-dependent, |
| 178 |
but may be due to the path having already been deleted, for example. |
| 179 |
|
| 180 |
@sa removePath(), addPaths() |
| 181 |
*/ |
| 182 |
QStringList DFileSystemWatcher::removePaths(const QStringList &paths) |
| 183 |
{ |
| 184 |
return QStringList(); |
| 185 |
} |
| 186 |
|
| 187 |
/*! |
| 188 |
@~english |
| 189 |
@fn void DFileSystemWatcher::fileChanged(const QString &path) |
| 190 |
|
| 191 |
This signal is emitted when the file at the specified \a path is |
| 192 |
modified, renamed or removed from disk. |
| 193 |
|
| 194 |
@sa directoryChanged() |
| 195 |
*/ |
| 196 |
|
| 197 |
/*! |
| 198 |
@~english |
| 199 |
@fn void DFileSystemWatcher::directoryChanged(const QString &path) |
| 200 |
|
| 201 |
This signal is emitted when the directory at a specified \a path |
| 202 |
is modified (e.g., when a file is added or deleted) or removed |
| 203 |
from disk. Note that if there are several changes during a short |
| 204 |
period of time, some of the changes might not Q_EMIT this signal. |
| 205 |
However, the last change in the sequence of changes will always |
| 206 |
generate this signal. |
| 207 |
|
| 208 |
@sa fileChanged() |
| 209 |
*/ |
| 210 |
|
| 211 |
/*! |
| 212 |
@~english |
| 213 |
@fn QStringList DFileSystemWatcher::directories() const |
| 214 |
|
| 215 |
Returns a list of paths to directories that are being watched. |
| 216 |
|
| 217 |
@sa files() |
| 218 |
*/ |
| 219 |
|
| 220 |
/*! |
| 221 |
@~english |
| 222 |
@fn QStringList DFileSystemWatcher::files() const |
| 223 |
|
| 224 |
Returns a list of paths to files that are being watched. |
| 225 |
|
| 226 |
@sa directories() |
| 227 |
*/ |
| 228 |
|
| 229 |
QStringList DFileSystemWatcher::directories() const |
| 230 |
{ |
| 231 |
return QStringList(); |
| 232 |
} |
| 233 |
|
| 234 |
QStringList DFileSystemWatcher::files() const |
| 235 |
{ |
| 236 |
return QStringList(); |
| 237 |
} |
| 238 |
|
| 239 |
DCORE_END_NAMESPACE |
| 240 |
|
| 241 |
#include "moc_dfilesystemwatcher.cpp" |