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