2019-08-22 03:21:32 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
|
2019-08-22 03:21:32 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2019-08-22 03:21:32 +01:00
|
|
|
*/
|
|
|
|
#include "sftpplugin-win.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDesktopServices>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QProcess>
|
2019-08-22 03:21:32 +01:00
|
|
|
#include <QRegularExpression>
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_sftp_debug.h"
|
2019-08-22 03:21:32 +01:00
|
|
|
|
|
|
|
K_PLUGIN_CLASS_WITH_JSON(SftpPlugin, "kdeconnect_sftp.json")
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
|
2019-08-22 03:21:32 +01:00
|
|
|
: KdeConnectPlugin(parent, args)
|
2022-12-22 16:24:31 +00:00
|
|
|
, deviceId(device()->id())
|
2019-08-22 03:21:32 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
SftpPlugin::~SftpPlugin()
|
|
|
|
{
|
|
|
|
}
|
2019-08-22 03:21:32 +01:00
|
|
|
|
|
|
|
bool SftpPlugin::startBrowsing()
|
|
|
|
{
|
|
|
|
NetworkPacket np(PACKET_TYPE_SFTP_REQUEST, {{QStringLiteral("startBrowsing"), true}});
|
|
|
|
sendPacket(np);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool SftpPlugin::receivePacket(const NetworkPacket &np)
|
2019-08-22 03:21:32 +01:00
|
|
|
{
|
2022-10-17 22:17:49 +01:00
|
|
|
QSet<QString> receivedFields(np.body().keys().begin(), np.body().keys().end());
|
|
|
|
if (!(expectedFields - receivedFields).isEmpty()) {
|
2019-08-22 03:21:32 +01:00
|
|
|
qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid packet received.";
|
2022-10-17 22:17:49 +01:00
|
|
|
for (QString missingField : (expectedFields - receivedFields)) {
|
2019-08-22 03:21:32 +01:00
|
|
|
qCWarning(KDECONNECT_PLUGIN_SFTP) << "Field" << missingField << "missing from packet.";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (np.has(QStringLiteral("errorMessage"))) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_SFTP) << np.get<QString>(QStringLiteral("errorMessage"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path;
|
|
|
|
if (np.has(QStringLiteral("multiPaths"))) {
|
|
|
|
path = QStringLiteral("/");
|
|
|
|
} else {
|
|
|
|
path = np.get<QString>(QStringLiteral("path"));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QString url_string = QStringLiteral("sftp://%1:%2@%3:%4%5")
|
|
|
|
.arg(np.get<QString>(QStringLiteral("user")),
|
|
|
|
np.get<QString>(QStringLiteral("password")),
|
|
|
|
np.get<QString>(QStringLiteral("ip")),
|
|
|
|
np.get<QString>(QStringLiteral("port")),
|
|
|
|
path);
|
2019-08-22 03:21:32 +01:00
|
|
|
static QRegularExpression uriRegex(QStringLiteral("^sftp://kdeconnect:\\w+@\\d+.\\d+.\\d+.\\d+:17[3-6][0-9]/$"));
|
|
|
|
if (!uriRegex.match(url_string).hasMatch()) {
|
|
|
|
qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid URL invoked. If the problem persists, contact the developers.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!QDesktopServices::openUrl(QUrl(url_string))) {
|
2022-09-10 22:23:52 +01:00
|
|
|
QMessageBox::critical(nullptr,
|
|
|
|
i18n("KDE Connect"),
|
|
|
|
i18n("Cannot handle SFTP protocol. Apologies for the inconvenience"),
|
|
|
|
QMessageBox::Abort,
|
|
|
|
QMessageBox::Abort);
|
2019-08-22 03:21:32 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "sftpplugin-win.moc"
|