File indexing completed on 2024-10-06 04:31:38
0001 /** 0002 * SPDX-FileCopyrightText: 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 #include "sftpplugin-win.h" 0007 0008 #include <QDebug> 0009 #include <QDesktopServices> 0010 #include <QDir> 0011 #include <QMessageBox> 0012 #include <QProcess> 0013 #include <QRegularExpression> 0014 0015 #include <KLocalizedString> 0016 #include <KPluginFactory> 0017 0018 #include "plugin_sftp_debug.h" 0019 0020 K_PLUGIN_CLASS_WITH_JSON(SftpPlugin, "kdeconnect_sftp.json") 0021 0022 SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args) 0023 : KdeConnectPlugin(parent, args) 0024 , deviceId(device()->id()) 0025 { 0026 } 0027 0028 bool SftpPlugin::startBrowsing() 0029 { 0030 NetworkPacket np(PACKET_TYPE_SFTP_REQUEST, {{QStringLiteral("startBrowsing"), true}}); 0031 sendPacket(np); 0032 return false; 0033 } 0034 0035 void SftpPlugin::receivePacket(const NetworkPacket &np) 0036 { 0037 QStringList receivedFieldsList = np.body().keys(); 0038 QSet<QString> receivedFields(receivedFieldsList.begin(), receivedFieldsList.end()); 0039 if (!(expectedFields - receivedFields).isEmpty()) { 0040 qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid packet received."; 0041 for (QString missingField : (expectedFields - receivedFields)) { 0042 qCWarning(KDECONNECT_PLUGIN_SFTP) << "Field" << missingField << "missing from packet."; 0043 } 0044 return; 0045 } 0046 if (np.has(QStringLiteral("errorMessage"))) { 0047 qCWarning(KDECONNECT_PLUGIN_SFTP) << np.get<QString>(QStringLiteral("errorMessage")); 0048 return; 0049 } 0050 0051 QString path; 0052 if (np.has(QStringLiteral("multiPaths"))) { 0053 QStringList paths = np.get<QStringList>(QStringLiteral("multiPaths")); 0054 if (paths.size() == 1) { 0055 path = paths[0]; 0056 } else { 0057 path = QStringLiteral("/"); 0058 } 0059 } else { 0060 path = np.get<QString>(QStringLiteral("path")); 0061 } 0062 if (!path.endsWith(QChar::fromLatin1('/'))) { 0063 path += QChar::fromLatin1('/'); 0064 } 0065 0066 QString url_string = QStringLiteral("sftp://%1:%2@%3:%4%5") 0067 .arg(np.get<QString>(QStringLiteral("user")), 0068 np.get<QString>(QStringLiteral("password")), 0069 np.get<QString>(QStringLiteral("ip")), 0070 np.get<QString>(QStringLiteral("port")), 0071 path); 0072 static QRegularExpression uriRegex(QStringLiteral("^sftp://kdeconnect:\\w+@\\d+.\\d+.\\d+.\\d+:17[3-6][0-9]/$")); 0073 if (!uriRegex.match(url_string).hasMatch()) { 0074 qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid URL invoked. If the problem persists, contact the developers."; 0075 } 0076 0077 if (!QDesktopServices::openUrl(QUrl(url_string))) { 0078 QMessageBox::critical(nullptr, 0079 i18n("KDE Connect"), 0080 i18n("Cannot handle SFTP protocol. Apologies for the inconvenience"), 0081 QMessageBox::Abort, 0082 QMessageBox::Abort); 0083 } 0084 } 0085 0086 #include "moc_sftpplugin-win.cpp" 0087 #include "sftpplugin-win.moc"