From 74a2467edd1bf0800cbaf1878984c6bccd42570d Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 21 Jan 2020 15:51:31 +0100 Subject: 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 --- src/corelib/io/qfile.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'src/corelib/io/qfile.cpp') 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 @@ -551,6 +551,63 @@ QFile::remove(const QString &fileName) return QFile(fileName).remove(); } +/*! + 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(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. -- cgit v1.2.3