File indexing completed on 2024-03-24 04:59:43
0001 /** 0002 * SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #ifndef NETWORKPACKET_H 0008 #define NETWORKPACKET_H 0009 0010 #include "networkpackettypes.h" 0011 0012 #include <QIODevice> 0013 #include <QObject> 0014 #include <QSharedPointer> 0015 #include <QString> 0016 #include <QUrl> 0017 #include <QVariant> 0018 0019 #include "kdeconnectcore_export.h" 0020 0021 class FileTransferJob; 0022 0023 class KDECONNECTCORE_EXPORT NetworkPacket 0024 { 0025 Q_GADGET 0026 Q_PROPERTY(QString id READ id MEMBER m_id) 0027 Q_PROPERTY(QString type READ type MEMBER m_type) 0028 Q_PROPERTY(QVariantMap body READ body MEMBER m_body) 0029 Q_PROPERTY(QVariantMap payloadTransferInfo READ payloadTransferInfo MEMBER m_payloadTransferInfo) 0030 Q_PROPERTY(qint64 payloadSize READ payloadSize MEMBER m_payloadSize) 0031 0032 public: 0033 const static int s_protocolVersion; 0034 0035 explicit NetworkPacket(const QString &type = QString(), const QVariantMap &body = {}); 0036 NetworkPacket(const NetworkPacket &other) = default; // Copy constructor, required for QMetaType and queued signals 0037 NetworkPacket &operator=(const NetworkPacket &other) = default; 0038 0039 QByteArray serialize() const; 0040 static bool unserialize(const QByteArray &json, NetworkPacket *out); 0041 0042 inline QString id() const 0043 { 0044 return m_id; 0045 } 0046 inline QString type() const 0047 { 0048 return m_type; 0049 } 0050 QVariantMap body() const 0051 { 0052 return m_body; 0053 } 0054 0055 // Get and set info from body. Note that id and type can not be accessed through these. 0056 template<typename T> 0057 T get(const QString &key, const T &defaultValue = {}) const 0058 { 0059 return m_body.value(key, defaultValue).template value<T>(); // Important note: Awesome template syntax is awesome 0060 } 0061 template<typename T> 0062 void set(const QString &key, const T &value) 0063 { 0064 m_body[key] = QVariant(value); 0065 } 0066 bool has(const QString &key) const 0067 { 0068 return m_body.contains(key); 0069 } 0070 0071 QSharedPointer<QIODevice> payload() const 0072 { 0073 return m_payload; 0074 } 0075 void setPayload(const QSharedPointer<QIODevice> &device, qint64 payloadSize) 0076 { 0077 m_payload = device; 0078 m_payloadSize = payloadSize; 0079 Q_ASSERT(m_payloadSize >= -1); 0080 } 0081 bool hasPayload() const 0082 { 0083 return (m_payloadSize != 0); 0084 } 0085 qint64 payloadSize() const 0086 { 0087 return m_payloadSize; 0088 } //-1 means it is an endless stream 0089 FileTransferJob *createPayloadTransferJob(const QUrl &destination) const; 0090 0091 // To be called by a particular DeviceLink 0092 QVariantMap payloadTransferInfo() const 0093 { 0094 return m_payloadTransferInfo; 0095 } 0096 void setPayloadTransferInfo(const QVariantMap &map) 0097 { 0098 m_payloadTransferInfo = map; 0099 } 0100 bool hasPayloadTransferInfo() const 0101 { 0102 return !m_payloadTransferInfo.isEmpty(); 0103 } 0104 0105 private: 0106 QString m_id; 0107 QString m_type; 0108 QVariantMap m_body; 0109 0110 QSharedPointer<QIODevice> m_payload; 0111 qint64 m_payloadSize; 0112 QVariantMap m_payloadTransferInfo; 0113 }; 0114 0115 KDECONNECTCORE_EXPORT QDebug operator<<(QDebug s, const NetworkPacket &pkg); 0116 Q_DECLARE_METATYPE(NetworkPacket) 0117 0118 #endif // NETWORKPACKET_H