[OpenDocString] kdeconnect-kde (cpp)
networkpacket.cpp
QDebug operator<<(QDebug s, const NetworkPacket &pkg)
{
    s.nospace() << "NetworkPacket(" << pkg.type() << ':' << pkg.body();
    if (pkg.hasPayload()) {
        s.nospace() << ":withpayload";
    }
    s.nospace() << ')';
    return s.space();
}
This implements an stream insertion operator to output information about the given network package and its payload.
NetworkPacket::NetworkPacket(const QString &type, const QVariantMap &body)
    : m_id(QString::number(QDateTime::currentMSecsSinceEpoch()))
    , m_type(type)
    , m_body(body)
    , m_payload()
    , m_payloadSize(0)
{
}
Constructs a network package object of a given type and body.
void NetworkPacket::createIdentityPacket(NetworkPacket *np)
{
    np->m_id = QString::number(QDateTime::currentMSecsSinceEpoch());
    np->m_type = PACKET_TYPE_IDENTITY;
    np->m_payload = QSharedPointer();
    np->m_payloadSize = 0;
    np->set(QStringLiteral("deviceId"), KdeConnectConfig::instance().deviceId());
    np->set(QStringLiteral("deviceName"), KdeConnectConfig::instance().name());
    np->set(QStringLiteral("deviceType"), KdeConnectConfig::instance().deviceType());
    np->set(QStringLiteral("protocolVersion"), NetworkPacket::s_protocolVersion);
    np->set(QStringLiteral("incomingCapabilities"), PluginLoader::instance()->incomingCapabilities());
    np->set(QStringLiteral("outgoingCapabilities"), PluginLoader::instance()->outgoingCapabilities());

    // qCDebug(KDECONNECT_CORE) << "createIdentityPacket" << np->serialize();
}
Populate entries of a NetworkPacket object from entires in current configuration.
QByteArray NetworkPacket::serialize() const
{
    // Object -> QVariant
    QVariantMap variant;
    variant.insert(QStringLiteral("id"), m_id);
    variant.insert(QStringLiteral("type"), m_type);
    variant.insert(QStringLiteral("body"), m_body);

    if (hasPayload()) {
        variant.insert(QStringLiteral("payloadSize"), m_payloadSize);
        variant.insert(QStringLiteral("payloadTransferInfo"), m_payloadTransferInfo);
    }

    // QVariant -> json
    auto jsonDocument = QJsonDocument::fromVariant(variant);
    QByteArray json = jsonDocument.toJson(QJsonDocument::Compact);
    if (json.isEmpty()) {
        qCDebug(KDECONNECT_CORE) << "Serialization error:";
    } else {
        /*if (!isEncrypted()) {
            //qCDebug(KDECONNECT_CORE) << "Serialized packet:" << json;
        }*/
        json.append('\n');
    }

    return json;
}
This implements a json representation of a network package and its payload.
bool NetworkPacket::unserialize(const QByteArray &a, NetworkPacket *np)
{
    // Json -> QVariant
    QJsonParseError parseError;
    auto parser = QJsonDocument::fromJson(a, &parseError);
    if (parser.isNull()) {
        qCDebug(KDECONNECT_CORE) << "Unserialization error:" << parseError.errorString();
        return false;
    }

    auto variant = parser.toVariant().toMap();
    qvariant2qobject(variant, np);

    np->m_payloadTransferInfo = variant[QStringLiteral("payloadTransferInfo")].toMap(); // Will return an empty qvariantmap if was not present, which is ok

    // Ids containing characters that are not allowed as dbus paths would make app crash
    if (np->m_body.contains(QStringLiteral("deviceId"))) {
        QString deviceId = np->get(QStringLiteral("deviceId"));
        DBusHelper::filterNonExportableCharacters(deviceId);
        np->set(QStringLiteral("deviceId"), deviceId);
    }

    return true;
}
This creates a network packet from a json representation by first parsing the json, creating a QVariant of its internal state and turning it into a QObject of the type NetworkPacket. Finally, it filters characters which are not allowed in DBus paths. If everything goes well, it returns true.
FileTransferJob *NetworkPacket::createPayloadTransferJob(const QUrl &destination) const
{
    return new FileTransferJob(this, destination);
}
To handle the transfer of files between devices, a FileTransferJob object is created and the payload data of the network package is transferred to the destination url. By returning a pointer to a FileTransferJob object, this method allows the caller to manage the transfer job, such as starting or canceling the transfer, and receiving updates on the transfer progress.