| 1 |
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dfilewatcher.h" |
| 6 |
#include "private/dbasefilewatcher_p.h" |
| 7 |
|
| 8 |
#include "dfilesystemwatcher.h" |
| 9 |
|
| 10 |
#include <QDir> |
| 11 |
#include <QDebug> |
| 12 |
|
| 13 |
DCORE_BEGIN_NAMESPACE |
| 14 |
|
| 15 |
static QString joinFilePath(const QString &path, const QString &name) |
| 16 |
{ |
| 17 |
if (path.endsWith(QDir::separator())) |
| 18 |
return path + name; |
| 19 |
|
| 20 |
return path + QDir::separator() + name; |
| 21 |
} |
| 22 |
|
| 23 |
class DFileWatcherPrivate : DBaseFileWatcherPrivate |
| 24 |
{ |
| 25 |
public: |
| 26 |
DFileWatcherPrivate(DFileWatcher *qq) |
| 27 |
: DBaseFileWatcherPrivate(qq) {} |
| 28 |
|
| 29 |
bool start() Q_DECL_OVERRIDE; |
| 30 |
bool stop() Q_DECL_OVERRIDE; |
| 31 |
|
| 32 |
void _q_handleFileDeleted(const QString &path, const QString &parentPath); |
| 33 |
void _q_handleFileAttributeChanged(const QString &path, const QString &parentPath); |
| 34 |
void _q_handleFileMoved(const QString &from, const QString &fromParent, const QString &to, const QString &toParent); |
| 35 |
void _q_handleFileCreated(const QString &path, const QString &parentPath); |
| 36 |
void _q_handleFileModified(const QString &path, const QString &parentPath); |
| 37 |
void _q_handleFileClose(const QString &path, const QString &parentPath); |
| 38 |
|
| 39 |
static QString formatPath(const QString &path); |
| 40 |
|
| 41 |
QString path; |
| 42 |
QStringList watchFileList; |
| 43 |
|
| 44 |
static QMap<QString, int> filePathToWatcherCount; |
| 45 |
|
| 46 |
Q_DECLARE_PUBLIC(DFileWatcher) |
| 47 |
}; |
| 48 |
|
| 49 |
QMap<QString, int> DFileWatcherPrivate::filePathToWatcherCount; |
| 50 |
Q_GLOBAL_STATIC(DFileSystemWatcher, watcher_file_private) |
| 51 |
|
| 52 |
QStringList parentPathList(const QString &path) |
| 53 |
{ |
| 54 |
QStringList list; |
| 55 |
QDir dir(path); |
| 56 |
|
| 57 |
list << path; |
| 58 |
|
| 59 |
while (dir.cdUp()) { |
| 60 |
list << dir.absolutePath(); |
| 61 |
} |
| 62 |
|
| 63 |
return list; |
| 64 |
} |
| 65 |
|
| 66 |
bool DFileWatcherPrivate::start() |
| 67 |
{ |
| 68 |
Q_Q(DFileWatcher); |
| 69 |
|
| 70 |
started = true; |
| 71 |
|
| 72 |
Q_FOREACH (const QString &path, parentPathList(this->path)) { |
| 73 |
if (watchFileList.contains(path)) |
| 74 |
continue; |
| 75 |
|
| 76 |
if (filePathToWatcherCount.value(path, -1) <= 0) { |
| 77 |
if (!watcher_file_private->addPath(path)) { |
| 78 |
qWarning() << Q_FUNC_INFO << "start watch failed, file path =" << path; |
| 79 |
q->stopWatcher(); |
| 80 |
started = false; |
| 81 |
return false; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
watchFileList << path; |
| 86 |
filePathToWatcherCount[path] = filePathToWatcherCount.value(path, 0) + 1; |
| 87 |
} |
| 88 |
|
| 89 |
q->connect(watcher_file_private, &DFileSystemWatcher::fileDeleted, |
| 90 |
q, &DFileWatcher::onFileDeleted); |
| 91 |
q->connect(watcher_file_private, &DFileSystemWatcher::fileAttributeChanged, |
| 92 |
q, &DFileWatcher::onFileAttributeChanged); |
| 93 |
q->connect(watcher_file_private, &DFileSystemWatcher::fileMoved, |
| 94 |
q, &DFileWatcher::onFileMoved); |
| 95 |
q->connect(watcher_file_private, &DFileSystemWatcher::fileCreated, |
| 96 |
q, &DFileWatcher::onFileCreated); |
| 97 |
q->connect(watcher_file_private, &DFileSystemWatcher::fileModified, |
| 98 |
q, &DFileWatcher::onFileModified); |
| 99 |
q->connect(watcher_file_private, &DFileSystemWatcher::fileClosed, |
| 100 |
q, &DFileWatcher::onFileClosed); |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
bool DFileWatcherPrivate::stop() |
| 106 |
{ |
| 107 |
Q_Q(DFileWatcher); |
| 108 |
|
| 109 |
q->disconnect(watcher_file_private, 0, q, 0); |
| 110 |
|
| 111 |
bool ok = true; |
| 112 |
|
| 113 |
Q_FOREACH (const QString &path, watchFileList) { |
| 114 |
int count = filePathToWatcherCount.value(path, 0); |
| 115 |
|
| 116 |
--count; |
| 117 |
|
| 118 |
if (count <= 0) { |
| 119 |
filePathToWatcherCount.remove(path); |
| 120 |
watchFileList.removeOne(path); |
| 121 |
ok = ok && watcher_file_private->removePath(path); |
| 122 |
} else { |
| 123 |
filePathToWatcherCount[path] = count; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return ok; |
| 128 |
} |
| 129 |
|
| 130 |
void DFileWatcherPrivate::_q_handleFileDeleted(const QString &path, const QString &parentPath) |
| 131 |
{ |
| 132 |
if (path != this->path && parentPath != this->path) |
| 133 |
return; |
| 134 |
|
| 135 |
Q_Q(DFileWatcher); |
| 136 |
|
| 137 |
Q_EMIT q->fileDeleted(QUrl::fromLocalFile(path)); |
| 138 |
} |
| 139 |
|
| 140 |
void DFileWatcherPrivate::_q_handleFileAttributeChanged(const QString &path, const QString &parentPath) |
| 141 |
{ |
| 142 |
if (path != this->path && parentPath != this->path) |
| 143 |
return; |
| 144 |
|
| 145 |
Q_Q(DFileWatcher); |
| 146 |
|
| 147 |
Q_EMIT q->fileAttributeChanged(QUrl::fromLocalFile(path)); |
| 148 |
} |
| 149 |
|
| 150 |
void DFileWatcherPrivate::_q_handleFileMoved(const QString &from, const QString &fromParent, const QString &to, const QString &toParent) |
| 151 |
{ |
| 152 |
Q_Q(DFileWatcher); |
| 153 |
|
| 154 |
if ((fromParent == this->path && toParent == this->path) || from == this->path) { |
| 155 |
Q_EMIT q->fileMoved(QUrl::fromLocalFile(from), QUrl::fromLocalFile(to)); |
| 156 |
} else if (fromParent == this->path) { |
| 157 |
Q_EMIT q->fileDeleted(QUrl::fromLocalFile(from)); |
| 158 |
} else if (watchFileList.contains(from)) { |
| 159 |
Q_EMIT q->fileDeleted(url); |
| 160 |
} else if (toParent == this->path) { |
| 161 |
Q_EMIT q->subfileCreated(QUrl::fromLocalFile(to)); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
void DFileWatcherPrivate::_q_handleFileCreated(const QString &path, const QString &parentPath) |
| 166 |
{ |
| 167 |
if (path != this->path && parentPath != this->path) |
| 168 |
return; |
| 169 |
|
| 170 |
Q_Q(DFileWatcher); |
| 171 |
|
| 172 |
Q_EMIT q->subfileCreated(QUrl::fromLocalFile(path)); |
| 173 |
} |
| 174 |
|
| 175 |
void DFileWatcherPrivate::_q_handleFileModified(const QString &path, const QString &parentPath) |
| 176 |
{ |
| 177 |
if (path != this->path && parentPath != this->path) |
| 178 |
return; |
| 179 |
|
| 180 |
Q_Q(DFileWatcher); |
| 181 |
|
| 182 |
Q_EMIT q->fileModified(QUrl::fromLocalFile(path)); |
| 183 |
} |
| 184 |
|
| 185 |
void DFileWatcherPrivate::_q_handleFileClose(const QString &path, const QString &parentPath) |
| 186 |
{ |
| 187 |
if (path != this->path && parentPath != this->path) |
| 188 |
return; |
| 189 |
|
| 190 |
Q_Q(DFileWatcher); |
| 191 |
|
| 192 |
Q_EMIT q->fileClosed(QUrl::fromLocalFile(path)); |
| 193 |
} |
| 194 |
|
| 195 |
QString DFileWatcherPrivate::formatPath(const QString &path) |
| 196 |
{ |
| 197 |
QString p = QFileInfo(path).absoluteFilePath(); |
| 198 |
|
| 199 |
if (p.endsWith(QDir::separator())) |
| 200 |
p.chop(1); |
| 201 |
|
| 202 |
return p.isEmpty() ? path : p; |
| 203 |
} |
| 204 |
|
| 205 |
/*! |
| 206 |
@~english |
| 207 |
\class Dtk::Core::DFileWatcher |
| 208 |
\inmodule dtkcore |
| 209 |
|
| 210 |
\brief The DFileWatcher class provides an implementation of DBaseFileWatcher for monitoring files and directories for modifications. |
| 211 |
*/ |
| 212 |
|
| 213 |
DFileWatcher::DFileWatcher(const QString &filePath, QObject *parent) |
| 214 |
: DBaseFileWatcher(*new DFileWatcherPrivate(this), QUrl::fromLocalFile(filePath), parent) |
| 215 |
{ |
| 216 |
d_func()->path = DFileWatcherPrivate::formatPath(filePath); |
| 217 |
} |
| 218 |
|
| 219 |
void DFileWatcher::onFileDeleted(const QString &path, const QString &name) |
| 220 |
{ |
| 221 |
if (name.isEmpty()) |
| 222 |
d_func()->_q_handleFileDeleted(path, QString()); |
| 223 |
else |
| 224 |
d_func()->_q_handleFileDeleted(joinFilePath(path, name), path); |
| 225 |
} |
| 226 |
|
| 227 |
void DFileWatcher::onFileAttributeChanged(const QString &path, const QString &name) |
| 228 |
{ |
| 229 |
if (name.isEmpty()) |
| 230 |
d_func()->_q_handleFileAttributeChanged(path, QString()); |
| 231 |
else |
| 232 |
d_func()->_q_handleFileAttributeChanged(joinFilePath(path, name), path); |
| 233 |
} |
| 234 |
|
| 235 |
void DFileWatcher::onFileMoved(const QString &from, const QString &fname, const QString &to, const QString &tname) |
| 236 |
{ |
| 237 |
QString fromPath, fpPath; |
| 238 |
QString toPath, tpPath; |
| 239 |
|
| 240 |
if (fname.isEmpty()) { |
| 241 |
fromPath = from; |
| 242 |
} else { |
| 243 |
fromPath = joinFilePath(from, fname); |
| 244 |
fpPath = from; |
| 245 |
} |
| 246 |
|
| 247 |
if (tname.isEmpty()) { |
| 248 |
toPath = to; |
| 249 |
} else { |
| 250 |
toPath = joinFilePath(to, tname); |
| 251 |
tpPath = to; |
| 252 |
} |
| 253 |
|
| 254 |
d_func()->_q_handleFileMoved(fromPath, fpPath, toPath, tpPath); |
| 255 |
} |
| 256 |
|
| 257 |
void DFileWatcher::onFileCreated(const QString &path, const QString &name) |
| 258 |
{ |
| 259 |
d_func()->_q_handleFileCreated(joinFilePath(path, name), path); |
| 260 |
} |
| 261 |
|
| 262 |
void DFileWatcher::onFileModified(const QString &path, const QString &name) |
| 263 |
{ |
| 264 |
if (name.isEmpty()) |
| 265 |
d_func()->_q_handleFileModified(path, QString()); |
| 266 |
else |
| 267 |
d_func()->_q_handleFileModified(joinFilePath(path, name), path); |
| 268 |
} |
| 269 |
|
| 270 |
void DFileWatcher::onFileClosed(const QString &path, const QString &name) |
| 271 |
{ |
| 272 |
if (name.isEmpty()) |
| 273 |
d_func()->_q_handleFileClose(path, QString()); |
| 274 |
else |
| 275 |
d_func()->_q_handleFileClose(joinFilePath(path, name), path); |
| 276 |
} |
| 277 |
|
| 278 |
DCORE_END_NAMESPACE |
| 279 |
|
| 280 |
#include "moc_dfilewatcher.cpp" |