Changeset View
Changeset View
Standalone View
Standalone View
core/networkpacket.cpp
- This file was moved from core/networkpackage.cpp.
Show All 12 Lines | |||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | 15 | * GNU General Public License for more details. | ||
16 | * | 16 | * | ||
17 | * You should have received a copy of the GNU General Public License | 17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "networkpackage.h" | 21 | #include "networkpacket.h" | ||
22 | #include "core_debug.h" | 22 | #include "core_debug.h" | ||
23 | 23 | | |||
24 | #include <QMetaObject> | 24 | #include <QMetaObject> | ||
25 | #include <QMetaProperty> | 25 | #include <QMetaProperty> | ||
26 | #include <QByteArray> | 26 | #include <QByteArray> | ||
27 | #include <QDataStream> | 27 | #include <QDataStream> | ||
28 | #include <QDateTime> | 28 | #include <QDateTime> | ||
29 | #include <QJsonDocument> | 29 | #include <QJsonDocument> | ||
30 | #include <QDebug> | 30 | #include <QDebug> | ||
31 | 31 | | |||
32 | #include "dbushelper.h" | 32 | #include "dbushelper.h" | ||
33 | #include "filetransferjob.h" | 33 | #include "filetransferjob.h" | ||
34 | #include "pluginloader.h" | 34 | #include "pluginloader.h" | ||
35 | #include "kdeconnectconfig.h" | 35 | #include "kdeconnectconfig.h" | ||
36 | 36 | | |||
37 | QDebug operator<<(QDebug s, const NetworkPackage& pkg) | 37 | QDebug operator<<(QDebug s, const NetworkPacket& pkg) | ||
38 | { | 38 | { | ||
39 | s.nospace() << "NetworkPackage(" << pkg.type() << ':' << pkg.body(); | 39 | s.nospace() << "NetworkPacket(" << pkg.type() << ':' << pkg.body(); | ||
40 | if (pkg.hasPayload()) { | 40 | if (pkg.hasPayload()) { | ||
41 | s.nospace() << ":withpayload"; | 41 | s.nospace() << ":withpayload"; | ||
42 | } | 42 | } | ||
43 | s.nospace() << ')'; | 43 | s.nospace() << ')'; | ||
44 | return s.space(); | 44 | return s.space(); | ||
45 | } | 45 | } | ||
46 | 46 | | |||
47 | const int NetworkPackage::s_protocolVersion = 7; | 47 | const int NetworkPacket::s_protocolVersion = 7; | ||
48 | 48 | | |||
49 | NetworkPackage::NetworkPackage(const QString& type, const QVariantMap& body) | 49 | NetworkPacket::NetworkPacket(const QString& type, const QVariantMap& body) | ||
50 | : m_id(QString::number(QDateTime::currentMSecsSinceEpoch())) | 50 | : m_id(QString::number(QDateTime::currentMSecsSinceEpoch())) | ||
51 | , m_type(type) | 51 | , m_type(type) | ||
52 | , m_body(body) | 52 | , m_body(body) | ||
53 | , m_payload() | 53 | , m_payload() | ||
54 | , m_payloadSize(0) | 54 | , m_payloadSize(0) | ||
55 | { | 55 | { | ||
56 | } | 56 | } | ||
57 | 57 | | |||
58 | void NetworkPackage::createIdentityPackage(NetworkPackage* np) | 58 | void NetworkPacket::createIdentityPacket(NetworkPacket* np) | ||
59 | { | 59 | { | ||
60 | KdeConnectConfig* config = KdeConnectConfig::instance(); | 60 | KdeConnectConfig* config = KdeConnectConfig::instance(); | ||
61 | np->m_id = QString::number(QDateTime::currentMSecsSinceEpoch()); | 61 | np->m_id = QString::number(QDateTime::currentMSecsSinceEpoch()); | ||
62 | np->m_type = PACKAGE_TYPE_IDENTITY; | 62 | np->m_type = PACKET_TYPE_IDENTITY; | ||
63 | np->m_payload = QSharedPointer<QIODevice>(); | 63 | np->m_payload = QSharedPointer<QIODevice>(); | ||
64 | np->m_payloadSize = 0; | 64 | np->m_payloadSize = 0; | ||
65 | np->set(QStringLiteral("deviceId"), config->deviceId()); | 65 | np->set(QStringLiteral("deviceId"), config->deviceId()); | ||
66 | np->set(QStringLiteral("deviceName"), config->name()); | 66 | np->set(QStringLiteral("deviceName"), config->name()); | ||
67 | np->set(QStringLiteral("deviceType"), config->deviceType()); | 67 | np->set(QStringLiteral("deviceType"), config->deviceType()); | ||
68 | np->set(QStringLiteral("protocolVersion"), NetworkPackage::s_protocolVersion); | 68 | np->set(QStringLiteral("protocolVersion"), NetworkPacket::s_protocolVersion); | ||
69 | np->set(QStringLiteral("incomingCapabilities"), PluginLoader::instance()->incomingCapabilities()); | 69 | np->set(QStringLiteral("incomingCapabilities"), PluginLoader::instance()->incomingCapabilities()); | ||
70 | np->set(QStringLiteral("outgoingCapabilities"), PluginLoader::instance()->outgoingCapabilities()); | 70 | np->set(QStringLiteral("outgoingCapabilities"), PluginLoader::instance()->outgoingCapabilities()); | ||
71 | 71 | | |||
72 | //qCDebug(KDECONNECT_CORE) << "createIdentityPackage" << np->serialize(); | 72 | //qCDebug(KDECONNECT_CORE) << "createIdentityPacket" << np->serialize(); | ||
73 | } | 73 | } | ||
74 | 74 | | |||
75 | template<class T> | 75 | template<class T> | ||
76 | QVariantMap qobject2qvariant(const T* object) | 76 | QVariantMap qobject2qvariant(const T* object) | ||
77 | { | 77 | { | ||
78 | QVariantMap map; | 78 | QVariantMap map; | ||
79 | auto metaObject = T::staticMetaObject; | 79 | auto metaObject = T::staticMetaObject; | ||
80 | for(int i = metaObject.propertyOffset(); i < metaObject.propertyCount(); ++i) { | 80 | for(int i = metaObject.propertyOffset(); i < metaObject.propertyCount(); ++i) { | ||
81 | QMetaProperty prop = metaObject.property(i); | 81 | QMetaProperty prop = metaObject.property(i); | ||
82 | map.insert(QString::fromLatin1(prop.name()), prop.readOnGadget(object)); | 82 | map.insert(QString::fromLatin1(prop.name()), prop.readOnGadget(object)); | ||
83 | } | 83 | } | ||
84 | 84 | | |||
85 | return map; | 85 | return map; | ||
86 | } | 86 | } | ||
87 | 87 | | |||
88 | QByteArray NetworkPackage::serialize() const | 88 | QByteArray NetworkPacket::serialize() const | ||
89 | { | 89 | { | ||
90 | //Object -> QVariant | 90 | //Object -> QVariant | ||
91 | //QVariantMap variant; | 91 | //QVariantMap variant; | ||
92 | //variant["id"] = mId; | 92 | //variant["id"] = mId; | ||
93 | //variant["type"] = mType; | 93 | //variant["type"] = mType; | ||
94 | //variant["body"] = mBody; | 94 | //variant["body"] = mBody; | ||
95 | QVariantMap variant = qobject2qvariant(this); | 95 | QVariantMap variant = qobject2qvariant(this); | ||
96 | 96 | | |||
97 | if (hasPayload()) { | 97 | if (hasPayload()) { | ||
98 | //qCDebug(KDECONNECT_CORE) << "Serializing payloadTransferInfo"; | 98 | //qCDebug(KDECONNECT_CORE) << "Serializing payloadTransferInfo"; | ||
99 | variant[QStringLiteral("payloadSize")] = payloadSize(); | 99 | variant[QStringLiteral("payloadSize")] = payloadSize(); | ||
100 | variant[QStringLiteral("payloadTransferInfo")] = m_payloadTransferInfo; | 100 | variant[QStringLiteral("payloadTransferInfo")] = m_payloadTransferInfo; | ||
101 | } | 101 | } | ||
102 | 102 | | |||
103 | //QVariant -> json | 103 | //QVariant -> json | ||
104 | auto jsonDocument = QJsonDocument::fromVariant(variant); | 104 | auto jsonDocument = QJsonDocument::fromVariant(variant); | ||
105 | QByteArray json = jsonDocument.toJson(QJsonDocument::Compact); | 105 | QByteArray json = jsonDocument.toJson(QJsonDocument::Compact); | ||
106 | if (json.isEmpty()) { | 106 | if (json.isEmpty()) { | ||
107 | qCDebug(KDECONNECT_CORE) << "Serialization error:"; | 107 | qCDebug(KDECONNECT_CORE) << "Serialization error:"; | ||
108 | } else { | 108 | } else { | ||
109 | /*if (!isEncrypted()) { | 109 | /*if (!isEncrypted()) { | ||
110 | //qCDebug(KDECONNECT_CORE) << "Serialized package:" << json; | 110 | //qCDebug(KDECONNECT_CORE) << "Serialized packet:" << json; | ||
111 | }*/ | 111 | }*/ | ||
112 | json.append('\n'); | 112 | json.append('\n'); | ||
113 | } | 113 | } | ||
114 | 114 | | |||
115 | return json; | 115 | return json; | ||
116 | } | 116 | } | ||
117 | 117 | | |||
118 | template <class T> | 118 | template <class T> | ||
Show All 10 Lines | 122 | { | |||
129 | QMetaProperty property = T::staticMetaObject.property(propertyIndex); | 129 | QMetaProperty property = T::staticMetaObject.property(propertyIndex); | ||
130 | bool ret = property.writeOnGadget(object, *iter); | 130 | bool ret = property.writeOnGadget(object, *iter); | ||
131 | if (!ret) { | 131 | if (!ret) { | ||
132 | qCWarning(KDECONNECT_CORE) << "couldn't set" << object << "->" << property.name() << '=' << *iter; | 132 | qCWarning(KDECONNECT_CORE) << "couldn't set" << object << "->" << property.name() << '=' << *iter; | ||
133 | } | 133 | } | ||
134 | } | 134 | } | ||
135 | } | 135 | } | ||
136 | 136 | | |||
137 | bool NetworkPackage::unserialize(const QByteArray& a, NetworkPackage* np) | 137 | bool NetworkPacket::unserialize(const QByteArray& a, NetworkPacket* np) | ||
138 | { | 138 | { | ||
139 | //Json -> QVariant | 139 | //Json -> QVariant | ||
140 | QJsonParseError parseError; | 140 | QJsonParseError parseError; | ||
141 | auto parser = QJsonDocument::fromJson(a, &parseError); | 141 | auto parser = QJsonDocument::fromJson(a, &parseError); | ||
142 | if (parser.isNull()) { | 142 | if (parser.isNull()) { | ||
143 | qCDebug(KDECONNECT_CORE) << "Unserialization error:" << parseError.errorString(); | 143 | qCDebug(KDECONNECT_CORE) << "Unserialization error:" << parseError.errorString(); | ||
144 | return false; | 144 | return false; | ||
145 | } | 145 | } | ||
Show All 14 Lines | 158 | { | |||
160 | DbusHelper::filterNonExportableCharacters(deviceId); | 160 | DbusHelper::filterNonExportableCharacters(deviceId); | ||
161 | np->set(QStringLiteral("deviceId"), deviceId); | 161 | np->set(QStringLiteral("deviceId"), deviceId); | ||
162 | } | 162 | } | ||
163 | 163 | | |||
164 | return true; | 164 | return true; | ||
165 | 165 | | |||
166 | } | 166 | } | ||
167 | 167 | | |||
168 | FileTransferJob* NetworkPackage::createPayloadTransferJob(const QUrl& destination) const | 168 | FileTransferJob* NetworkPacket::createPayloadTransferJob(const QUrl& destination) const | ||
169 | { | 169 | { | ||
170 | return new FileTransferJob(payload(), payloadSize(), destination); | 170 | return new FileTransferJob(payload(), payloadSize(), destination); | ||
171 | } | 171 | } | ||
172 | 172 | |