Use DeviceFileSystemModel in SelectRemoteFileDialog

Instead of SftpFileSystemModel.

Change-Id: Icbb3cf6147fc1266b3aa763f72c919be7b329390
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2022-03-25 14:31:28 +01:00
parent 3d2c5ee81c
commit 5d27ca72d4

View File

@@ -31,18 +31,16 @@
#include <projectexplorer/kitinformation.h> #include <projectexplorer/kitinformation.h>
#include <projectexplorer/projectexplorerconstants.h> #include <projectexplorer/projectexplorerconstants.h>
#include <ssh/sftpfilesystemmodel.h> #include <projectexplorer/devicesupport/devicefilesystemmodel.h>
#include <ssh/sshconnection.h>
#include <utils/pathchooser.h> #include <utils/pathchooser.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/temporaryfile.h> #include <utils/temporaryfile.h>
#include <QCheckBox> #include <QCheckBox>
#include <QDebug>
#include <QDir>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QFormLayout> #include <QFormLayout>
#include <QFutureWatcher>
#include <QHeaderView> #include <QHeaderView>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
@@ -53,7 +51,6 @@
using namespace Core; using namespace Core;
using namespace ProjectExplorer; using namespace ProjectExplorer;
using namespace QSsh;
using namespace Utils; using namespace Utils;
namespace Debugger { namespace Debugger {
@@ -71,26 +68,24 @@ class SelectRemoteFileDialog : public QDialog
public: public:
explicit SelectRemoteFileDialog(QWidget *parent); explicit SelectRemoteFileDialog(QWidget *parent);
~SelectRemoteFileDialog();
void attachToDevice(Kit *k); void attachToDevice(Kit *k);
FilePath localFile() const { return m_localFile; } FilePath localFile() const { return m_localFile; }
FilePath remoteFile() const { return m_remoteFile; } FilePath remoteFile() const { return m_remoteFile; }
private: private:
void handleSftpOperationFinished(SftpJobId, const QString &error);
void handleSftpOperationFailed(const QString &errorMessage);
void handleConnectionError(const QString &errorMessage);
void handleRemoteError(const QString &errorMessage);
void selectFile(); void selectFile();
void clearWatcher();
QSortFilterProxyModel m_model; QSortFilterProxyModel m_model;
SftpFileSystemModel m_fileSystemModel; DeviceFileSystemModel m_fileSystemModel;
QTreeView *m_fileSystemView; QTreeView *m_fileSystemView;
QTextBrowser *m_textBrowser; QTextBrowser *m_textBrowser;
QDialogButtonBox *m_buttonBox; QDialogButtonBox *m_buttonBox;
FilePath m_localFile; FilePath m_localFile;
FilePath m_remoteFile; FilePath m_remoteFile;
SftpJobId m_sftpJobId; QFutureWatcher<bool> *m_watcher = nullptr;
}; };
SelectRemoteFileDialog::SelectRemoteFileDialog(QWidget *parent) SelectRemoteFileDialog::SelectRemoteFileDialog(QWidget *parent)
@@ -109,7 +104,7 @@ SelectRemoteFileDialog::SelectRemoteFileDialog(QWidget *parent)
m_fileSystemView->header()->setStretchLastSection(true); m_fileSystemView->header()->setStretchLastSection(true);
m_textBrowser = new QTextBrowser(this); m_textBrowser = new QTextBrowser(this);
m_textBrowser->setEnabled(false); m_textBrowser->setReadOnly(true);
m_buttonBox = new QDialogButtonBox(this); m_buttonBox = new QDialogButtonBox(this);
m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok); m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
@@ -121,15 +116,13 @@ SelectRemoteFileDialog::SelectRemoteFileDialog(QWidget *parent)
layout->addWidget(m_textBrowser); layout->addWidget(m_textBrowser);
layout->addWidget(m_buttonBox); layout->addWidget(m_buttonBox);
connect(m_buttonBox, &QDialogButtonBox::rejected, connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
this, &QDialog::reject); connect(m_buttonBox, &QDialogButtonBox::accepted, this, &SelectRemoteFileDialog::selectFile);
connect(m_buttonBox, &QDialogButtonBox::accepted, }
this, &SelectRemoteFileDialog::selectFile);
connect(&m_fileSystemModel, &SftpFileSystemModel::sftpOperationFailed, SelectRemoteFileDialog::~SelectRemoteFileDialog()
this, &SelectRemoteFileDialog::handleSftpOperationFailed); {
connect(&m_fileSystemModel, &SftpFileSystemModel::connectionError, clearWatcher();
this, &SelectRemoteFileDialog::handleConnectionError);
} }
void SelectRemoteFileDialog::attachToDevice(Kit *k) void SelectRemoteFileDialog::attachToDevice(Kit *k)
@@ -138,37 +131,23 @@ void SelectRemoteFileDialog::attachToDevice(Kit *k)
QTC_ASSERT(k, return); QTC_ASSERT(k, return);
IDevice::ConstPtr device = DeviceKitAspect::device(k); IDevice::ConstPtr device = DeviceKitAspect::device(k);
QTC_ASSERT(device, return); QTC_ASSERT(device, return);
SshConnectionParameters sshParams = device->sshParameters(); m_fileSystemModel.setDevice(device);
// TODO: change into setDevice()
m_fileSystemModel.setSshConnection(sshParams);
} }
void SelectRemoteFileDialog::handleSftpOperationFailed(const QString &errorMessage) static void copyFile(QFutureInterface<bool> &futureInterface, const FilePath &remoteSource,
const FilePath &localDestination)
{ {
m_textBrowser->append(errorMessage); // TODO: this should be implemented transparently in FilePath::copyFile()
//reject(); // The code here should be just:
} //
// futureInterface.reportResult(remoteSource.copyFile(localDestination));
//
// The implementation below won't handle really big files, like core files.
void SelectRemoteFileDialog::handleConnectionError(const QString &errorMessage) const QByteArray data = remoteSource.fileContents();
{ if (futureInterface.isCanceled())
m_textBrowser->append(errorMessage); return;
//reject(); futureInterface.reportResult(localDestination.writeFileContents(data));
}
void SelectRemoteFileDialog::handleSftpOperationFinished(SftpJobId, const QString &error)
{
if (error.isEmpty()) {
m_textBrowser->append(tr("Download of remote file succeeded."));
accept();
} else {
m_textBrowser->append(error);
//reject();
}
}
void SelectRemoteFileDialog::handleRemoteError(const QString &errorMessage)
{
m_textBrowser->append(errorMessage);
} }
void SelectRemoteFileDialog::selectFile() void SelectRemoteFileDialog::selectFile()
@@ -180,9 +159,6 @@ void SelectRemoteFileDialog::selectFile()
m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
m_fileSystemView->setEnabled(false); m_fileSystemView->setEnabled(false);
connect(&m_fileSystemModel, &SftpFileSystemModel::sftpOperationFinished,
this, &SelectRemoteFileDialog::handleSftpOperationFinished);
{ {
Utils::TemporaryFile localFile("remotecore-XXXXXX"); Utils::TemporaryFile localFile("remotecore-XXXXXX");
localFile.open(); localFile.open();
@@ -190,8 +166,35 @@ void SelectRemoteFileDialog::selectFile()
} }
idx = idx.sibling(idx.row(), 1); idx = idx.sibling(idx.row(), 1);
m_remoteFile = FilePath::fromVariant(m_fileSystemModel.data(idx, SftpFileSystemModel::PathRole)); m_remoteFile = FilePath::fromVariant(m_fileSystemModel.data(idx, DeviceFileSystemModel::PathRole));
m_sftpJobId = m_fileSystemModel.downloadFile(idx, m_localFile.toString());
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;
} }
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////