2013-06-06 04:57:06 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2013-06-06 04:57:06 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2013-06-06 04:57:06 +01:00
|
|
|
*/
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
#ifndef NETWORKPACKET_H
|
|
|
|
#define NETWORKPACKET_H
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
#include "networkpackettypes.h"
|
2013-08-28 22:47:39 +01:00
|
|
|
|
2013-09-09 17:30:44 +01:00
|
|
|
#include <QIODevice>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QObject>
|
2013-09-24 13:13:02 +01:00
|
|
|
#include <QSharedPointer>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QString>
|
2014-09-21 21:22:06 +01:00
|
|
|
#include <QUrl>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QVariant>
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2014-06-14 13:31:31 +01:00
|
|
|
#include "kdeconnectcore_export.h"
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-09-24 13:10:25 +01:00
|
|
|
class FileTransferJob;
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
class KDECONNECTCORE_EXPORT NetworkPacket
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
2015-06-24 19:04:33 +01:00
|
|
|
Q_GADGET
|
2023-08-12 12:36:21 +01:00
|
|
|
Q_PROPERTY(QString id READ id MEMBER m_id)
|
|
|
|
Q_PROPERTY(QString type READ type MEMBER m_type)
|
|
|
|
Q_PROPERTY(QVariantMap body READ body MEMBER m_body)
|
|
|
|
Q_PROPERTY(QVariantMap payloadTransferInfo READ payloadTransferInfo MEMBER m_payloadTransferInfo)
|
|
|
|
Q_PROPERTY(qint64 payloadSize READ payloadSize MEMBER m_payloadSize)
|
2013-06-06 04:57:06 +01:00
|
|
|
|
|
|
|
public:
|
2017-09-03 20:39:44 +01:00
|
|
|
const static int s_protocolVersion;
|
2013-09-02 12:26:26 +01:00
|
|
|
|
2023-06-27 12:10:59 +01:00
|
|
|
explicit NetworkPacket(const QString &type = QString(), const QVariantMap &body = {});
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket(const NetworkPacket &other) = default; // Copy constructor, required for QMetaType and queued signals
|
|
|
|
NetworkPacket &operator=(const NetworkPacket &other) = default;
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2013-07-04 00:09:49 +01:00
|
|
|
QByteArray serialize() const;
|
2022-09-10 22:23:52 +01:00
|
|
|
static bool unserialize(const QByteArray &json, NetworkPacket *out);
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2023-08-12 12:36:21 +01:00
|
|
|
inline QString id() const
|
2022-09-10 22:23:52 +01:00
|
|
|
{
|
|
|
|
return m_id;
|
|
|
|
}
|
2023-08-12 12:36:21 +01:00
|
|
|
inline QString type() const
|
2022-09-10 22:23:52 +01:00
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
2023-08-12 12:36:21 +01:00
|
|
|
QVariantMap body() const
|
2022-09-10 22:23:52 +01:00
|
|
|
{
|
|
|
|
return m_body;
|
2013-07-04 00:09:49 +01:00
|
|
|
}
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Get and set info from body. Note that id and type can not be accessed through these.
|
|
|
|
template<typename T>
|
|
|
|
T get(const QString &key, const T &defaultValue = {}) const
|
|
|
|
{
|
|
|
|
return m_body.value(key, defaultValue).template value<T>(); // Important note: Awesome template syntax is awesome
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
void set(const QString &key, const T &value)
|
|
|
|
{
|
|
|
|
m_body[key] = QVariant(value);
|
|
|
|
}
|
|
|
|
bool has(const QString &key) const
|
|
|
|
{
|
|
|
|
return m_body.contains(key);
|
|
|
|
}
|
2013-09-09 17:30:44 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QSharedPointer<QIODevice> payload() const
|
|
|
|
{
|
|
|
|
return m_payload;
|
|
|
|
}
|
|
|
|
void setPayload(const QSharedPointer<QIODevice> &device, qint64 payloadSize)
|
|
|
|
{
|
|
|
|
m_payload = device;
|
|
|
|
m_payloadSize = payloadSize;
|
|
|
|
Q_ASSERT(m_payloadSize >= -1);
|
|
|
|
}
|
|
|
|
bool hasPayload() const
|
|
|
|
{
|
|
|
|
return (m_payloadSize != 0);
|
|
|
|
}
|
|
|
|
qint64 payloadSize() const
|
|
|
|
{
|
|
|
|
return m_payloadSize;
|
|
|
|
} //-1 means it is an endless stream
|
|
|
|
FileTransferJob *createPayloadTransferJob(const QUrl &destination) const;
|
|
|
|
|
|
|
|
// To be called by a particular DeviceLink
|
|
|
|
QVariantMap payloadTransferInfo() const
|
|
|
|
{
|
|
|
|
return m_payloadTransferInfo;
|
|
|
|
}
|
|
|
|
void setPayloadTransferInfo(const QVariantMap &map)
|
|
|
|
{
|
|
|
|
m_payloadTransferInfo = map;
|
|
|
|
}
|
|
|
|
bool hasPayloadTransferInfo() const
|
|
|
|
{
|
|
|
|
return !m_payloadTransferInfo.isEmpty();
|
|
|
|
}
|
2013-09-09 17:30:44 +01:00
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
private:
|
2017-09-03 20:39:44 +01:00
|
|
|
QString m_id;
|
|
|
|
QString m_type;
|
|
|
|
QVariantMap m_body;
|
2019-07-21 17:10:39 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QSharedPointer<QIODevice> m_payload;
|
|
|
|
qint64 m_payloadSize;
|
|
|
|
QVariantMap m_payloadTransferInfo;
|
2013-09-09 17:30:44 +01:00
|
|
|
};
|
2013-07-04 00:09:49 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
KDECONNECTCORE_EXPORT QDebug operator<<(QDebug s, const NetworkPacket &pkg);
|
2018-10-29 16:41:13 +00:00
|
|
|
Q_DECLARE_METATYPE(NetworkPacket)
|
2016-06-03 14:50:46 +01:00
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
#endif // NETWORKPACKET_H
|