add basic SFTP Plugin for Windows

This commit is contained in:
Piyush Aggarwal 2019-08-22 02:21:32 +00:00
parent f91b9e9951
commit f65737da21
4 changed files with 164 additions and 6 deletions

View file

@ -28,7 +28,7 @@ if(NOT SAILFISHOS)
if(NOT WIN32)
add_subdirectory(screensaver-inhibit)
endif()
if(NOT WIN32 AND NOT APPLE)
if(NOT APPLE)
add_subdirectory(sftp)
endif()

View file

@ -2,11 +2,17 @@
find_program(HAVE_FUSERMOUNT fusermount)
configure_file(config-sftp.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-sftp.h )
set(kdeconnect_sftp_SRCS
mounter.cpp
mountloop.cpp
sftpplugin.cpp
)
if(WIN32)
set(kdeconnect_sftp_SRCS
sftpplugin-win.cpp
)
else()
set(kdeconnect_sftp_SRCS
mounter.cpp
mountloop.cpp
sftpplugin.cpp
)
endif()
kdeconnect_add_plugin(kdeconnect_sftp JSON kdeconnect_sftp.json SOURCES ${kdeconnect_sftp_SRCS})

View file

@ -0,0 +1,94 @@
/**
* Copyright 2019 Piyush Aggarwal<piyushaggarwal002@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "sftpplugin-win.h"
#include <QDir>
#include <QDebug>
#include <QProcess>
#include <QMessageBox>
#include <QDesktopServices>
#include <QRegularExpression>
#include <KLocalizedString>
#include <KPluginFactory>
#include "sftp_debug.h"
K_PLUGIN_CLASS_WITH_JSON(SftpPlugin, "kdeconnect_sftp.json")
Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_SFTP, "kdeconnect.plugin.sftp")
SftpPlugin::SftpPlugin(QObject* parent, const QVariantList& args)
: KdeConnectPlugin(parent, args)
{
deviceId = device()->id();
}
SftpPlugin::~SftpPlugin(){}
bool SftpPlugin::startBrowsing()
{
NetworkPacket np(PACKET_TYPE_SFTP_REQUEST, {{QStringLiteral("startBrowsing"), true}});
sendPacket(np);
return false;
}
bool SftpPlugin::receivePacket(const NetworkPacket& np)
{
if (!(expectedFields - np.body().keys().toSet()).isEmpty()) {
qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid packet received.";
for (QString missingField: (expectedFields - np.body().keys().toSet()).toList()) {
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"));
}
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
);
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))) {
QMessageBox::critical(nullptr, i18n("KDE Connect"), i18n("Cannot handle SFTP protocol. Apologies for the inconvenience"),
QMessageBox::Abort,
QMessageBox::Abort);
}
return true;
}
#include "sftpplugin-win.moc"

View file

@ -0,0 +1,58 @@
/**
* Copyright 2019 Piyush Aggarwal<piyushaggarwal002@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SFTPPLUGIN_WIN_H
#define SFTPPLUGIN_WIN_H
#include <core/kdeconnectplugin.h>
#include <core/device.h>
#define PACKET_TYPE_SFTP_REQUEST QStringLiteral("kdeconnect.sftp.request")
static const QSet<QString> expectedFields = QSet<QString>() << QStringLiteral("ip")
<< QStringLiteral("port")
<< QStringLiteral("user")
<< QStringLiteral("password")
<< QStringLiteral("path");
;
class SftpPlugin
: public KdeConnectPlugin
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.device.sftp")
public:
explicit SftpPlugin(QObject* parent, const QVariantList& args);
~SftpPlugin() override;
bool receivePacket(const NetworkPacket& np) override;
void connected() override {}
QString dbusPath() const override { return QStringLiteral("/modules/kdeconnect/devices/") + deviceId + QStringLiteral("/sftp"); }
public Q_SLOTS:
Q_SCRIPTABLE bool startBrowsing();
private:
QString deviceId; //Storing it to avoid accessing device() from the destructor which could cause a crash
};
#endif