diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 2a85b1539..e3f3f0dfc 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -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() diff --git a/plugins/sftp/CMakeLists.txt b/plugins/sftp/CMakeLists.txt index d914db5ab..2066102ea 100644 --- a/plugins/sftp/CMakeLists.txt +++ b/plugins/sftp/CMakeLists.txt @@ -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}) diff --git a/plugins/sftp/sftpplugin-win.cpp b/plugins/sftp/sftpplugin-win.cpp new file mode 100644 index 000000000..10959b534 --- /dev/null +++ b/plugins/sftp/sftpplugin-win.cpp @@ -0,0 +1,94 @@ +/** + * Copyright 2019 Piyush Aggarwal + * + * 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 . + */ +#include "sftpplugin-win.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +#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(QStringLiteral("errorMessage")); + return false; + } + + QString path; + if (np.has(QStringLiteral("multiPaths"))) { + path = QStringLiteral("/"); + } else { + path = np.get(QStringLiteral("path")); + } + + QString url_string = QStringLiteral("sftp://%1:%2@%3:%4%5").arg( + np.get(QStringLiteral("user")), + np.get(QStringLiteral("password")), + np.get(QStringLiteral("ip")), + np.get(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" diff --git a/plugins/sftp/sftpplugin-win.h b/plugins/sftp/sftpplugin-win.h new file mode 100644 index 000000000..01f42918d --- /dev/null +++ b/plugins/sftp/sftpplugin-win.h @@ -0,0 +1,58 @@ +/** + * Copyright 2019 Piyush Aggarwal + * + * 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 . + */ + +#ifndef SFTPPLUGIN_WIN_H +#define SFTPPLUGIN_WIN_H + +#include +#include + +#define PACKET_TYPE_SFTP_REQUEST QStringLiteral("kdeconnect.sftp.request") + +static const QSet expectedFields = QSet() << 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