diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index e35fd1e2674..f1ada61a810 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -31,9 +31,9 @@ #include "qtcprocess.h" #include -#include -#include #include +#include +#include #include #include #include @@ -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 &task) diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h index 4a5e3fa8bb2..dc57d1add15 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,