/plugins/sftp/sftpplugin-win.cpp

https://github.com/KDE/kdeconnect-kde · C++ · 78 lines · 61 code · 12 blank · 5 comment · 7 complexity · 49dc47a2bfd815651457de9c2249c06d MD5 · raw file

  1. /**
  2. * SPDX-FileCopyrightText: 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
  5. */
  6. #include "sftpplugin-win.h"
  7. #include <QDir>
  8. #include <QDebug>
  9. #include <QProcess>
  10. #include <QMessageBox>
  11. #include <QDesktopServices>
  12. #include <QRegularExpression>
  13. #include <KLocalizedString>
  14. #include <KPluginFactory>
  15. #include "plugin_sftp_debug.h"
  16. K_PLUGIN_CLASS_WITH_JSON(SftpPlugin, "kdeconnect_sftp.json")
  17. SftpPlugin::SftpPlugin(QObject* parent, const QVariantList& args)
  18. : KdeConnectPlugin(parent, args)
  19. {
  20. deviceId = device()->id();
  21. }
  22. SftpPlugin::~SftpPlugin(){}
  23. bool SftpPlugin::startBrowsing()
  24. {
  25. NetworkPacket np(PACKET_TYPE_SFTP_REQUEST, {{QStringLiteral("startBrowsing"), true}});
  26. sendPacket(np);
  27. return false;
  28. }
  29. bool SftpPlugin::receivePacket(const NetworkPacket& np)
  30. {
  31. if (!(expectedFields - np.body().keys().toSet()).isEmpty()) {
  32. qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid packet received.";
  33. for (QString missingField: (expectedFields - np.body().keys().toSet()).toList()) {
  34. qCWarning(KDECONNECT_PLUGIN_SFTP) << "Field" << missingField << "missing from packet.";
  35. }
  36. return false;
  37. }
  38. if (np.has(QStringLiteral("errorMessage"))) {
  39. qCWarning(KDECONNECT_PLUGIN_SFTP) << np.get<QString>(QStringLiteral("errorMessage"));
  40. return false;
  41. }
  42. QString path;
  43. if (np.has(QStringLiteral("multiPaths"))) {
  44. path = QStringLiteral("/");
  45. } else {
  46. path = np.get<QString>(QStringLiteral("path"));
  47. }
  48. QString url_string = QStringLiteral("sftp://%1:%2@%3:%4%5").arg(
  49. np.get<QString>(QStringLiteral("user")),
  50. np.get<QString>(QStringLiteral("password")),
  51. np.get<QString>(QStringLiteral("ip")),
  52. np.get<QString>(QStringLiteral("port")),
  53. path
  54. );
  55. static QRegularExpression uriRegex(QStringLiteral("^sftp://kdeconnect:\\w+@\\d+.\\d+.\\d+.\\d+:17[3-6][0-9]/$"));
  56. if (!uriRegex.match(url_string).hasMatch()) {
  57. qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid URL invoked. If the problem persists, contact the developers.";
  58. }
  59. if (!QDesktopServices::openUrl(QUrl(url_string))) {
  60. QMessageBox::critical(nullptr, i18n("KDE Connect"), i18n("Cannot handle SFTP protocol. Apologies for the inconvenience"),
  61. QMessageBox::Abort,
  62. QMessageBox::Abort);
  63. }
  64. return true;
  65. }
  66. #include "sftpplugin-win.moc"