Utils: Fix FileUtils::copyRecursively with user interaction

CopyAskingForOverwrite got passed around as value, thus it got copied.
The messagebox results were stored into the fields of such copies, and
therefore had not the expected values across the recursion.

This change wraps the copy helper function into a lambda and the lambda
is passed around instead of the object. It may still be a bit much
decoupling architecture for a single use-case, but it works now (for
me).

Fixes: QTCREATORBUG-31174
Change-Id: I5444d18d7bf4ef59ab879e31e5233eadf1c935e4
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Alessandro Portale
2024-07-02 17:28:05 +02:00
parent 29bfece543
commit 1bf641795f
4 changed files with 53 additions and 51 deletions

View File

@@ -305,10 +305,9 @@ FileUtils::CopyAskingForOverwrite::CopyAskingForOverwrite(QWidget *dialogParent,
, m_postOperation(postOperation) , m_postOperation(postOperation)
{} {}
bool FileUtils::CopyAskingForOverwrite::operator()(const FilePath &src, FileUtils::CopyHelper FileUtils::CopyAskingForOverwrite::operator()()
const FilePath &dest,
QString *error)
{ {
CopyHelper helperFunction = [this](const FilePath &src, const FilePath &dest, QString *error) {
bool copyFile = true; bool copyFile = true;
if (dest.exists()) { if (dest.exists()) {
if (m_skipAll) if (m_skipAll)
@@ -318,8 +317,8 @@ bool FileUtils::CopyAskingForOverwrite::operator()(const FilePath &src,
m_parent, m_parent,
Tr::tr("Overwrite File?"), Tr::tr("Overwrite File?"),
Tr::tr("Overwrite existing file \"%1\"?").arg(dest.toUserOutput()), Tr::tr("Overwrite existing file \"%1\"?").arg(dest.toUserOutput()),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No
| QMessageBox::Cancel); | QMessageBox::NoToAll | QMessageBox::Cancel);
if (res == QMessageBox::Cancel) { if (res == QMessageBox::Cancel) {
return false; return false;
} else if (res == QMessageBox::No) { } else if (res == QMessageBox::No) {
@@ -348,6 +347,8 @@ bool FileUtils::CopyAskingForOverwrite::operator()(const FilePath &src,
} }
m_files.append(dest.absoluteFilePath()); m_files.append(dest.absoluteFilePath());
return true; return true;
};
return helperFunction;
} }
FilePaths FileUtils::CopyAskingForOverwrite::files() const FilePaths FileUtils::CopyAskingForOverwrite::files() const
@@ -694,11 +695,10 @@ FilePathInfo FileUtils::filePathInfoFromTriple(const QString &infos, int modeBas
return {size, flags, dt}; return {size, flags, dt};
} }
bool FileUtils::copyRecursively( bool FileUtils::copyRecursively(const FilePath &srcFilePath,
const FilePath &srcFilePath,
const FilePath &tgtFilePath, const FilePath &tgtFilePath,
QString *error, QString *error,
std::function<bool(const FilePath &, const FilePath &, QString *)> copyHelper) CopyHelper copyHelper)
{ {
if (srcFilePath.isDir()) { if (srcFilePath.isDir()) {
if (!tgtFilePath.ensureWritableDir()) { if (!tgtFilePath.ensureWritableDir()) {

View File

@@ -43,13 +43,14 @@ struct QTCREATOR_UTILS_EXPORT RunResult
class QTCREATOR_UTILS_EXPORT FileUtils class QTCREATOR_UTILS_EXPORT FileUtils
{ {
public: public:
using CopyHelper = std::function<bool(const FilePath &, const FilePath &, QString *)>;
#ifdef QT_GUI_LIB #ifdef QT_GUI_LIB
class QTCREATOR_UTILS_EXPORT CopyAskingForOverwrite class QTCREATOR_UTILS_EXPORT CopyAskingForOverwrite
{ {
public: public:
CopyAskingForOverwrite(QWidget *dialogParent, CopyAskingForOverwrite(QWidget *dialogParent,
const std::function<void(FilePath)> &postOperation = {}); const std::function<void(FilePath)> &postOperation = {});
bool operator()(const FilePath &src, const FilePath &dest, QString *error); CopyHelper operator()();
FilePaths files() const; FilePaths files() const;
private: private:
@@ -65,7 +66,7 @@ public:
const FilePath &srcFilePath, const FilePath &srcFilePath,
const FilePath &tgtFilePath, const FilePath &tgtFilePath,
QString *error, QString *error,
std::function<bool(const FilePath &, const FilePath &, QString *)> helper); CopyHelper helper);
static bool copyIfDifferent(const FilePath &srcFilePath, static bool copyIfDifferent(const FilePath &srcFilePath,
const FilePath &tgtFilePath); const FilePath &tgtFilePath);

View File

@@ -281,17 +281,17 @@ void CreateAndroidManifestWizard::createAndroidTemplateFiles()
FileUtils::copyRecursively(version->prefix() / "src/android/java/AndroidManifest.xml", FileUtils::copyRecursively(version->prefix() / "src/android/java/AndroidManifest.xml",
m_directory / "AndroidManifest.xml", m_directory / "AndroidManifest.xml",
nullptr, nullptr,
copy); copy());
} else { } else {
FileUtils::copyRecursively(version->prefix() / "src/android/templates", FileUtils::copyRecursively(version->prefix() / "src/android/templates",
m_directory, m_directory,
nullptr, nullptr,
copy); copy());
if (m_copyGradle) { if (m_copyGradle) {
FilePath gradlePath = version->prefix() / "src/3rdparty/gradle"; FilePath gradlePath = version->prefix() / "src/3rdparty/gradle";
QTC_ASSERT(gradlePath.exists(), return); QTC_ASSERT(gradlePath.exists(), return);
FileUtils::copyRecursively(gradlePath, m_directory, nullptr, copy); FileUtils::copyRecursively(gradlePath, m_directory, nullptr, copy());
} }
} }

View File

@@ -425,11 +425,12 @@ bool executePluginInstallWizard(const FilePath &archive)
return copyPluginFile(data.sourcePath, installPath); return copyPluginFile(data.sourcePath, installPath);
} else { } else {
QString error; QString error;
FileUtils::CopyAskingForOverwrite copy(ICore::dialogParent(),
postCopyOperation());
if (!FileUtils::copyRecursively(data.extractedPath, if (!FileUtils::copyRecursively(data.extractedPath,
installPath, installPath,
&error, &error,
FileUtils::CopyAskingForOverwrite(ICore::dialogParent(), copy())) {
postCopyOperation()))) {
QMessageBox::warning(ICore::dialogParent(), QMessageBox::warning(ICore::dialogParent(),
Tr::tr("Failed to Copy Plugin Files"), Tr::tr("Failed to Copy Plugin Files"),
error); error);