summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2020-06-11 15:41:26 +0200
committerEike Ziller <eike.ziller@qt.io>2020-06-15 07:43:59 +0000
commit7cedde2a0ae4776dfba4ea3643a2ef8e76e54dfc (patch)
tree20b87fd3399279ecb27ad13ea198171c5c65eb6b /src
parent0be309bcf1b36b06b4ca91172100d9f9d926a1a1 (diff)
downloadqt-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')
-rw-r--r--src/libs/utils/fileutils.cpp62
-rw-r--r--src/libs/utils/fileutils.h16
2 files changed, 76 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)
diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h
index 4a5e3fa8bb..dc57d1add1 100644
--- a/src/libs/utils/fileutils.h
+++ b/src/libs/utils/fileutils.h
@@ -162,6 +162,22 @@ private:
class QTCREATOR_UTILS_EXPORT FileUtils {
public:
+#ifdef QT_GUI_LIB
+ class QTCREATOR_UTILS_EXPORT CopyAskingForOverwrite
+ {
+ public:
+ CopyAskingForOverwrite(QWidget *dialogParent);
+ bool operator()(const QFileInfo &src, const QFileInfo &dest, QString *error);
+ QStringList files() const;
+
+ private:
+ QWidget *m_parent;
+ QStringList m_files;
+ bool m_overwriteAll = false;
+ bool m_skipAll = false;
+ };
+#endif // QT_GUI_LIB
+
static bool removeRecursively(const FilePath &filePath, QString *error = nullptr);
static bool copyRecursively(
const FilePath &srcFilePath, const FilePath &tgtFilePath, QString *error = nullptr,