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>
This commit is contained in:
Eike Ziller
2020-06-11 15:41:26 +02:00
parent 0be309bcf1
commit 7cedde2a0a
2 changed files with 76 additions and 2 deletions

View File

@@ -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)

View File

@@ -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,