diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2020-01-21 15:51:31 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2020-02-05 20:21:45 +0100 |
commit | 74a2467edd1bf0800cbaf1878984c6bccd42570d (patch) | |
tree | 10d141287c8e9b32a37c5241432be5a800e8a847 /src/corelib/io | |
parent | 7dd6d32657cb079f8b3e1ff6b1dc27248ec0a2d4 (diff) |
Add QFile::moveToTrash, which moves a file to the trash
Due to the nature of QFile just operating on a file path, this also
works for paths that are actually directories.
The test covers files from different locations on which this
operation should typically succeed, but tries to handle the case
where trashing files will fail because of the file system
structure.
On Windows 7, running the test will open a confirmation dialog as
the implementation of IFileOperation doesn't respect the various
flags. This might depend on the specific Windows 7 patch level,
and the option to always use SHFileOperation on that platform needs
to be evaluated further.
[ChangeLog][QtCore][QFile] Introduce QFile::moveToTrash to allow
applications to move files to the trash.
Change-Id: I45019040c25b30f7db293b6933c63aca2f319514
Fixes: QTBUG-47703
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
Diffstat (limited to 'src/corelib/io')
-rw-r--r-- | src/corelib/io/qfile.cpp | 57 | ||||
-rw-r--r-- | src/corelib/io/qfile.h | 3 |
2 files changed, 60 insertions, 0 deletions
diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index 5320ae2986..a516c6cd27 100644 --- a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -552,6 +552,63 @@ QFile::remove(const QString &fileName) } /*! + Moves the file specified by fileName() to the trash. Returns \c true if successful, + and sets the fileName() to the path at which the file can be found within the trash; + otherwise returns \c false. + + \note On systems where the system API doesn't report the location of the file in the + trash, fileName() will be set to the null string once the file has been moved. On + systems that don't have a trash can, this function always returns false. +*/ +bool +QFile::moveToTrash() +{ + Q_D(QFile); + if (d->fileName.isEmpty() && + !static_cast<QFSFileEngine *>(d->engine())->isUnnamedFile()) { + qWarning("QFile::remove: Empty or null file name"); + return false; + } + unsetError(); + close(); + if (error() == QFile::NoError) { + QFileSystemEntry fileEntry(d->fileName); + QFileSystemEntry trashEntry; + QSystemError error; + if (QFileSystemEngine::moveFileToTrash(fileEntry, trashEntry, error)) { + setFileName(trashEntry.filePath()); + unsetError(); + return true; + } + d->setError(QFile::RenameError, error.toString()); + } + return false; +} + +/*! + \overload + + Moves the file specified by fileName() to the trash. Returns \c true if successful, + and sets \a pathInTrash (if provided) to the path at which the file can be found within + the trash; otherwise returns \c false. + + \note On systems where the system API doesn't report the path of the file in the + trash, \a pathInTrash will be set to the null string once the file has been moved. + On systems that don't have a trash can, this function always returns false. +*/ +bool +QFile::moveToTrash(const QString &fileName, QString *pathInTrash) +{ + QFile file(fileName); + if (file.moveToTrash()) { + if (pathInTrash) + *pathInTrash = file.fileName(); + return true; + } + return false; +} + +/*! Renames the file currently specified by fileName() to \a newName. Returns \c true if successful; otherwise returns \c false. diff --git a/src/corelib/io/qfile.h b/src/corelib/io/qfile.h index 2099b2852f..917fec4e1a 100644 --- a/src/corelib/io/qfile.h +++ b/src/corelib/io/qfile.h @@ -125,6 +125,9 @@ public: bool remove(); static bool remove(const QString &fileName); + bool moveToTrash(); + static bool moveToTrash(const QString &fileName, QString *pathInTrash = nullptr); + bool rename(const QString &newName); static bool rename(const QString &oldName, const QString &newName); |