From ecb57cfc32ebbc457fd5e58ae0efc4dcb9d4d3f8 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Tue, 27 Dec 2011 22:30:05 +0100 Subject: Pass notification of failure of watches onto the caller. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is particularly useful for situations where the user might really want to be notified about a failure, for instance, in a backup application. Empty paths are not treated as an error in calling, as the user code cannot really do anything sensible to handle this error, but empty paths should not be used. Change-Id: Iddb44fd39f4e3fac5c3f9f60fb7999e1833280a8 Reviewed-by: João Abecasis Reviewed-by: Denis Dzyubenko Reviewed-by: Bradley T. Hughes --- src/corelib/io/qfilesystemwatcher.cpp | 99 +++++++++++++++++++++++++---------- src/corelib/io/qfilesystemwatcher.h | 8 +-- 2 files changed, 76 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/corelib/io/qfilesystemwatcher.cpp b/src/corelib/io/qfilesystemwatcher.cpp index efa9029d56..29abf96af1 100644 --- a/src/corelib/io/qfilesystemwatcher.cpp +++ b/src/corelib/io/qfilesystemwatcher.cpp @@ -425,20 +425,28 @@ QFileSystemWatcher::~QFileSystemWatcher() otherwise the fileChanged() signal is emitted when \a path is modified, renamed or removed. - \note There is a system dependent limit to the number of files and - directories that can be monitored simultaneously. If this limit - has been reached, \a path will not be added to the file system - watcher, and a warning message will be printed to \e{stderr}. + If the watch was successful, true is returned. + + Reasons for a watch failure are generally system-dependent, but + may include the resource not existing, access failures, or the + total watch count limit, if the platform has one. + + \note There may be a system dependent limit to the number of + files and directories that can be monitored simultaneously. + If this limit is been reached, \a path will not be monitored, + and false is returned. \sa addPaths(), removePath() */ -void QFileSystemWatcher::addPath(const QString &path) +bool QFileSystemWatcher::addPath(const QString &path) { if (path.isEmpty()) { qWarning("QFileSystemWatcher::addPath: path is empty"); - return; + return true; } - addPaths(QStringList(path)); + + QStringList paths = addPaths(QStringList(path)); + return paths.isEmpty(); } /*! @@ -451,23 +459,37 @@ void QFileSystemWatcher::addPath(const QString &path) otherwise the fileChanged() signal is emitted when the path is modified, renamed, or removed. - \note There is a system dependent limit to the number of files and - directories that can be monitored simultaneously. If this limit - has been reached, the excess \a paths will not be added to the - file system watcher, and a warning message will be printed to - \e{stderr} for each path that could not be added. + The return value is a list of paths that could not be watched. + + Reasons for a watch failure are generally system-dependent, but + may include the resource not existing, access failures, or the + total watch count limit, if the platform has one. + + \note There may be a system dependent limit to the number of + files and directories that can be monitored simultaneously. + If this limit has been reached, the excess \a paths will not + be monitored, and they will be added to the returned QStringList. \sa addPath(), removePaths() */ -void QFileSystemWatcher::addPaths(const QStringList &paths) +QStringList QFileSystemWatcher::addPaths(const QStringList &paths) { Q_D(QFileSystemWatcher); - if (paths.isEmpty()) { + + QStringList p = paths; + QMutableListIterator it(p); + + while (it.hasNext()) { + const QString &path = it.next(); + if (path.isEmpty()) + it.remove(); + } + + if (p.isEmpty()) { qWarning("QFileSystemWatcher::addPaths: list is empty"); - return; + return QStringList(); } - QStringList p = paths; QFileSystemWatcherEngine *engine = 0; if(!objectName().startsWith(QLatin1String("_qt_autotest_force_engine_"))) { @@ -495,42 +517,65 @@ void QFileSystemWatcher::addPaths(const QStringList &paths) if(engine) p = engine->addPaths(p, &d->files, &d->directories); - if (!p.isEmpty()) - qWarning("QFileSystemWatcher: failed to add paths: %s", - qPrintable(p.join(QLatin1String(", ")))); + return p; } /*! Removes the specified \a path from the file system watcher. + If the watch is successfully removed, true is returned. + + Reasons for watch removal failing are generally system-dependent, + but may be due to the path having already been deleted, for example. + \sa removePaths(), addPath() */ -void QFileSystemWatcher::removePath(const QString &path) +bool QFileSystemWatcher::removePath(const QString &path) { if (path.isEmpty()) { qWarning("QFileSystemWatcher::removePath: path is empty"); - return; + return true; } - removePaths(QStringList(path)); + + QStringList paths = removePaths(QStringList(path)); + return paths.isEmpty(); } /*! Removes the specified \a paths from the file system watcher. + The return value is a list of paths which were not able to be + unwatched successfully. + + Reasons for watch removal failing are generally system-dependent, + but may be due to the path having already been deleted, for example. + \sa removePath(), addPaths() */ -void QFileSystemWatcher::removePaths(const QStringList &paths) +QStringList QFileSystemWatcher::removePaths(const QStringList &paths) { - if (paths.isEmpty()) { - qWarning("QFileSystemWatcher::removePaths: list is empty"); - return; - } Q_D(QFileSystemWatcher); + QStringList p = paths; + QMutableListIterator it(p); + + while (it.hasNext()) { + const QString &path = it.next(); + if (path.isEmpty()) + it.remove(); + } + + if (p.isEmpty()) { + qWarning("QFileSystemWatcher::removePaths: list is empty"); + return QStringList(); + } + if (d->native) p = d->native->removePaths(p, &d->files, &d->directories); if (d->poller) p = d->poller->removePaths(p, &d->files, &d->directories); + + return p; } /*! diff --git a/src/corelib/io/qfilesystemwatcher.h b/src/corelib/io/qfilesystemwatcher.h index 763c8de0d6..a6954850f0 100644 --- a/src/corelib/io/qfilesystemwatcher.h +++ b/src/corelib/io/qfilesystemwatcher.h @@ -64,10 +64,10 @@ public: QFileSystemWatcher(const QStringList &paths, QObject *parent = 0); ~QFileSystemWatcher(); - void addPath(const QString &file); - void addPaths(const QStringList &files); - void removePath(const QString &file); - void removePaths(const QStringList &files); + bool addPath(const QString &file); + QStringList addPaths(const QStringList &files); + bool removePath(const QString &file); + QStringList removePaths(const QStringList &files); QStringList files() const; QStringList directories() const; -- cgit v1.2.3