diff options
| author | Eike Ziller <eike.ziller@qt.io> | 2020-06-11 15:41:26 +0200 |
|---|---|---|
| committer | Eike Ziller <eike.ziller@qt.io> | 2020-06-15 07:43:59 +0000 |
| commit | 7cedde2a0ae4776dfba4ea3643a2ef8e76e54dfc (patch) | |
| tree | 20b87fd3399279ecb27ad13ea198171c5c65eb6b /src/libs/utils/fileutils.cpp | |
| parent | 0be309bcf1b36b06b4ca91172100d9f9d926a1a1 (diff) | |
| download | qt-creator-7cedde2a0ae4776dfba4ea3643a2ef8e76e54dfc.tar.gz | |
Utils: Create re-usable Callable for copying and asking for overwrite
FileUtils::copyRecursively has the option to override the copy
operation, and this is e.g. used for asking the user if files already
exist, and to track what is actually copied.
Make that functionality available for re-use.
Change-Id: I16b7eddd32509b06866a1070e45ab58629f9a9be
Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/libs/utils/fileutils.cpp')
| -rw-r--r-- | src/libs/utils/fileutils.cpp | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index e35fd1e267..f1ada61a81 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -31,9 +31,9 @@ #include "qtcprocess.h" #include <QDataStream> -#include <QDir> -#include <QDebug> #include <QDateTime> +#include <QDebug> +#include <QDir> #include <QOperatingSystemVersion> #include <QRegularExpression> #include <QTimer> @@ -991,6 +991,64 @@ QTextStream &operator<<(QTextStream &s, const FilePath &fn) return s << fn.toString(); } +#ifdef QT_GUI_LIB +FileUtils::CopyAskingForOverwrite::CopyAskingForOverwrite(QWidget *dialogParent) + : m_parent(dialogParent) +{} + +bool FileUtils::CopyAskingForOverwrite::operator()(const QFileInfo &src, + const QFileInfo &dest, + QString *error) +{ + bool copyFile = true; + if (dest.exists()) { + if (m_skipAll) + copyFile = false; + else if (!m_overwriteAll) { + const int res = QMessageBox::question( + m_parent, + QCoreApplication::translate("Utils::FileUtils", "Overwrite File?"), + QCoreApplication::translate("Utils::FileUtils", "Overwrite existing file \"%1\"?") + .arg(FilePath::fromFileInfo(dest).toUserOutput()), + QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll + | QMessageBox::Cancel); + if (res == QMessageBox::Cancel) { + return false; + } else if (res == QMessageBox::No) { + copyFile = false; + } else if (res == QMessageBox::NoToAll) { + m_skipAll = true; + copyFile = false; + } else if (res == QMessageBox::YesToAll) { + m_overwriteAll = true; + } + if (copyFile) + QFile::remove(dest.filePath()); + } + } + if (copyFile) { + if (!dest.absoluteDir().exists()) + dest.absoluteDir().mkpath(dest.absolutePath()); + if (!QFile::copy(src.filePath(), dest.filePath())) { + if (error) { + *error = QCoreApplication::translate("Utils::FileUtils", + "Could not copy file \"%1\" to \"%2\".") + .arg(FilePath::fromFileInfo(src).toUserOutput(), + FilePath::fromFileInfo(dest).toUserOutput()); + } + return false; + } + } + m_files.append(dest.absoluteFilePath()); + return true; +} + +QStringList FileUtils::CopyAskingForOverwrite::files() const +{ + return m_files; +} +#endif // QT_GUI_LIB + #ifdef Q_OS_WIN template <> void withNtfsPermissions(const std::function<void()> &task) |
