LoadCoreDialog: Use FileTransfer for download

Change-Id: I032eceb85af3416e2488f8ba34ef4092f4aacd8d
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Jarek Kobus
2022-05-24 07:12:02 +02:00
parent 8a1f091bf0
commit 21d3f0d51e

View File

@@ -25,23 +25,21 @@
#include "loadcoredialog.h"
#include "debuggerdialogs.h"
#include "debuggerkitinformation.h"
#include "gdb/gdbengine.h"
#include <projectexplorer/devicesupport/devicefilesystemmodel.h>
#include <projectexplorer/devicesupport/filetransfer.h>
#include <projectexplorer/devicesupport/idevice.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/kitchooser.h>
#include <utils/pathchooser.h>
#include <utils/processinterface.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/temporaryfile.h>
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QFutureWatcher>
#include <QHeaderView>
#include <QLabel>
#include <QLineEdit>
@@ -69,7 +67,6 @@ class SelectRemoteFileDialog : public QDialog
public:
explicit SelectRemoteFileDialog(QWidget *parent);
~SelectRemoteFileDialog();
void attachToDevice(Kit *k);
FilePath localFile() const { return m_localFile; }
@@ -77,7 +74,6 @@ public:
private:
void selectFile();
void clearWatcher();
QSortFilterProxyModel m_model;
DeviceFileSystemModel m_fileSystemModel;
@@ -86,7 +82,7 @@ private:
QDialogButtonBox *m_buttonBox;
FilePath m_localFile;
FilePath m_remoteFile;
QFutureWatcher<bool> *m_watcher = nullptr;
FileTransfer m_fileTransfer;
};
SelectRemoteFileDialog::SelectRemoteFileDialog(QWidget *parent)
@@ -119,11 +115,21 @@ SelectRemoteFileDialog::SelectRemoteFileDialog(QWidget *parent)
connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
connect(m_buttonBox, &QDialogButtonBox::accepted, this, &SelectRemoteFileDialog::selectFile);
}
SelectRemoteFileDialog::~SelectRemoteFileDialog()
{
clearWatcher();
connect(&m_fileTransfer, &FileTransfer::done, this, [this](const ProcessResultData &result) {
const bool success = result.m_error == QProcess::UnknownError
&& result.m_exitStatus == QProcess::NormalExit
&& result.m_exitCode == 0;
if (success) {
m_textBrowser->append(tr("Download of remote file succeeded."));
accept();
} else {
m_textBrowser->append(tr("Download of remote file failed: %1")
.arg(result.m_errorString));
m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
m_fileSystemView->setEnabled(true);
}
});
}
void SelectRemoteFileDialog::attachToDevice(Kit *k)
@@ -135,22 +141,6 @@ void SelectRemoteFileDialog::attachToDevice(Kit *k)
m_fileSystemModel.setDevice(device);
}
static void copyFile(QFutureInterface<bool> &futureInterface, const FilePath &remoteSource,
const FilePath &localDestination)
{
// TODO: this should be implemented transparently in FilePath::copyFile()
// The code here should be just:
//
// futureInterface.reportResult(remoteSource.copyFile(localDestination));
//
// The implementation below won't handle really big files, like core files.
const QByteArray data = remoteSource.fileContents();
if (futureInterface.isCanceled())
return;
futureInterface.reportResult(localDestination.writeFileContents(data));
}
void SelectRemoteFileDialog::selectFile()
{
QModelIndex idx = m_model.mapToSource(m_fileSystemView->currentIndex());
@@ -161,7 +151,7 @@ void SelectRemoteFileDialog::selectFile()
m_fileSystemView->setEnabled(false);
{
Utils::TemporaryFile localFile("remotecore-XXXXXX");
TemporaryFile localFile("remotecore-XXXXXX");
localFile.open();
m_localFile = FilePath::fromString(localFile.fileName());
}
@@ -169,33 +159,8 @@ void SelectRemoteFileDialog::selectFile()
idx = idx.sibling(idx.row(), 1);
m_remoteFile = FilePath::fromVariant(m_fileSystemModel.data(idx, DeviceFileSystemModel::PathRole));
clearWatcher();
m_watcher = new QFutureWatcher<bool>(this);
auto future = runAsync(copyFile, m_remoteFile, m_localFile);
connect(m_watcher, &QFutureWatcher<bool>::finished, this, [this] {
const bool success = m_watcher->result();
if (success) {
m_textBrowser->append(tr("Download of remote file succeeded."));
accept();
} else {
m_textBrowser->append(tr("Download of remote file failed."));
m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
m_fileSystemView->setEnabled(true);
}
});
m_watcher->setFuture(future);
}
void SelectRemoteFileDialog::clearWatcher()
{
if (!m_watcher)
return;
m_watcher->disconnect();
m_watcher->future().cancel();
m_watcher->future().waitForFinished();
delete m_watcher;
m_watcher = nullptr;
m_fileTransfer.setFilesToTransfer({{m_remoteFile, m_localFile}});
m_fileTransfer.start();
}
///////////////////////////////////////////////////////////////////////