[OpenDocString] kdeconnect-kde (cpp)
sftpplugin-win.cpp
SftpPlugin::SftpPlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
    , deviceId(device()->id())
{
}
Constructs a KdeConnectPlugin object and assigns device id to the parent object.
SftpPlugin::~SftpPlugin()
{
}
This removes the plugin from the list of plugins.
bool SftpPlugin::startBrowsing()
{
    NetworkPacket np(PACKET_TYPE_SFTP_REQUEST, {{QStringLiteral("startBrowsing"), true}});
    sendPacket(np);
    return false;
}
This sends a startBrowsing network packet and returns true.
bool SftpPlugin::receivePacket(const NetworkPacket &np)
{
    QSet receivedFields(np.body().keys().begin(), np.body().keys().end());
    if (!(expectedFields - receivedFields).isEmpty()) {
        qCWarning(KDECONNECT_PLUGIN_SFTP) << "Invalid packet received.";
        for (QString missingField : (expectedFields - receivedFields)) {
            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;
}
This retrieves the fields from a network packet, and checks that the received fields are valid. It returns true on the first successful attempt. If the packet is invalid, it returns false. If the packet is multiPaths, the path is used to locate the file in the directory with the user and password, and if the problem is persists, the dialog is printed to the console. If the problem is not possible, the dialog is printed to the console.