From e1c749e33d22066ac847a18a0f7a2d6b819ba196 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Mon, 9 Sep 2013 18:30:44 +0200 Subject: [PATCH] Added a payload field to networkpackage, protocol version increased to 4 It will be used to attach files to packages when devicelinks support it --- kded/networkpackage.cpp | 43 +++++++++++++++++++++++++---------------- kded/networkpackage.h | 17 +++++++++++++--- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/kded/networkpackage.cpp b/kded/networkpackage.cpp index 9443c73d8..a485b85cf 100644 --- a/kded/networkpackage.cpp +++ b/kded/networkpackage.cpp @@ -22,6 +22,7 @@ #include #include + #include #include #include @@ -33,13 +34,29 @@ #include const QCA::EncryptionAlgorithm NetworkPackage::EncryptionAlgorithm = QCA::EME_PKCS1v15; -const int NetworkPackage::ProtocolVersion = 3; +const int NetworkPackage::ProtocolVersion = 4; NetworkPackage::NetworkPackage(const QString& type) { mId = QString::number(QDateTime::currentMSecsSinceEpoch()); mType = type; mBody = QVariantMap(); + mPayload = 0; +} + + +void NetworkPackage::createIdentityPackage(NetworkPackage* np) +{ + KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc"); + QString id = config->group("myself").readEntry("id",""); + np->mId = QString::number(QDateTime::currentMSecsSinceEpoch()); + np->mType = PACKAGE_TYPE_IDENTITY; + np->mPayload = 0; + np->set("deviceId", id); + np->set("deviceName", QHostInfo::localHostName()); + np->set("protocolVersion", NetworkPackage::ProtocolVersion); + + //qDebug() << "createIdentityPackage" << np->serialize(); } QByteArray NetworkPackage::serialize() const @@ -51,6 +68,10 @@ QByteArray NetworkPackage::serialize() const //variant["body"] = mBody; QVariantMap variant = QJson::QObjectHelper::qobject2qvariant(this); + if (hasPayload()) { + variant["payloadTransferInfo"] = mPayloadTransferInfo; + } + //QVariant -> json bool ok; QJson::Serializer serializer; @@ -81,6 +102,10 @@ bool NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np) //QVariant -> Object QJson::QObjectHelper::qvariant2qobject(variant,np); + if (variant.contains("payloadTransferInfo")) { + np->mPayloadTransferInfo = variant["payloadTransferInfo"].toMap(); + } + return true; } @@ -129,19 +154,3 @@ bool NetworkPackage::decrypt (QCA::PrivateKey& key, NetworkPackage* out) const } - - -void NetworkPackage::createIdentityPackage(NetworkPackage* np) -{ - KSharedConfigPtr config = KSharedConfig::openConfig("kdeconnectrc"); - QString id = config->group("myself").readEntry("id",""); - np->mId = QString::number(QDateTime::currentMSecsSinceEpoch()); - np->mType = PACKAGE_TYPE_IDENTITY; - np->set("deviceId", id); - np->set("deviceName", QHostInfo::localHostName()); - np->set("protocolVersion", NetworkPackage::ProtocolVersion); - - //qDebug() << "createIdentityPackage" << np->serialize(); -} - - diff --git a/kded/networkpackage.h b/kded/networkpackage.h index 6cbdafa9d..55614a314 100644 --- a/kded/networkpackage.h +++ b/kded/networkpackage.h @@ -24,9 +24,11 @@ #include "networkpackagetypes.h" #include +#include #include #include #include +#include #include #include @@ -66,7 +68,16 @@ public: template void set(const QString& key, const T& value) { mBody[key] = QVariant(value); } bool has(const QString& key) const { return mBody.contains(key); } + QIODevice* payload() const { return mPayload; } + void setPayload(QIODevice* device) { mPayload = device;} + bool hasPayload() const { return (mPayload != 0); } + + //To be called by a particular DeviceLink + QVariantMap payloadTransferInfo() const { return mPayloadTransferInfo; } + void setPayloadTransferInfo(const QVariantMap& map) { mPayloadTransferInfo = map; } + private: + void setId(const QString& id) { mId = id; } void setType(const QString& t) { mType = t; } void setBody(const QVariantMap& b) { mBody = b; } @@ -75,9 +86,9 @@ private: QString mType; QVariantMap mBody; + QIODevice* mPayload; + QVariantMap mPayloadTransferInfo; + }; -//Set specialization need this awesome-to-the-max syntax: -//template<> inline void NetworkPackage::set(const QString& key, const QStringList& value) { mBody[key] = QVariant(value); } - #endif // NETWORKPACKAGE_H