2013-06-06 04:57:06 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef NETWORKPACKAGE_H
|
|
|
|
#define NETWORKPACKAGE_H
|
|
|
|
|
2013-07-04 18:17:22 +01:00
|
|
|
#include "networkpackagetypes.h"
|
2013-08-28 22:47:39 +01:00
|
|
|
|
2013-07-04 00:09:49 +01:00
|
|
|
#include <QObject>
|
2013-06-06 04:57:06 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QVariant>
|
2013-09-09 17:30:44 +01:00
|
|
|
#include <QIODevice>
|
2015-12-07 11:34:41 +00:00
|
|
|
//#include <QtCrypto>
|
2013-09-24 13:13:02 +01:00
|
|
|
#include <QSharedPointer>
|
2014-09-21 21:22:06 +01:00
|
|
|
#include <QUrl>
|
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;
|
|
|
|
|
2015-06-24 19:04:33 +01:00
|
|
|
class KDECONNECTCORE_EXPORT NetworkPackage
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
2015-06-24 19:04:33 +01:00
|
|
|
Q_GADGET
|
2013-09-01 21:13:03 +01:00
|
|
|
Q_PROPERTY( QString id READ id WRITE setId )
|
2013-07-04 00:09:49 +01:00
|
|
|
Q_PROPERTY( QString type READ type WRITE setType )
|
|
|
|
Q_PROPERTY( QVariantMap body READ body WRITE setBody )
|
2015-04-01 22:37:25 +01:00
|
|
|
Q_PROPERTY( QVariantMap payloadTransferInfo READ payloadTransferInfo WRITE setPayloadTransferInfo )
|
|
|
|
Q_PROPERTY( qint64 payloadSize READ payloadSize WRITE setPayloadSize )
|
2013-06-06 04:57:06 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-12-07 11:34:41 +00:00
|
|
|
//const static QCA::EncryptionAlgorithm EncryptionAlgorithm;
|
2013-09-03 01:13:13 +01:00
|
|
|
const static int ProtocolVersion;
|
2013-09-02 12:26:26 +01:00
|
|
|
|
2016-06-21 19:07:12 +01:00
|
|
|
explicit NetworkPackage(const QString& type, const QVariantMap &body = {});
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2013-09-02 12:26:26 +01:00
|
|
|
static void createIdentityPackage(NetworkPackage*);
|
|
|
|
|
2013-07-04 00:09:49 +01:00
|
|
|
QByteArray serialize() const;
|
2013-09-02 12:26:26 +01:00
|
|
|
static bool unserialize(const QByteArray& json, NetworkPackage* out);
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2015-12-07 11:34:41 +00:00
|
|
|
//void encrypt(QCA::PublicKey& key);
|
|
|
|
//bool decrypt(QCA::PrivateKey& key, NetworkPackage* out) const;
|
2015-12-11 01:12:08 +00:00
|
|
|
//bool isEncrypted() const { return mType == PACKAGE_TYPE_ENCRYPTED; }
|
2013-07-23 15:11:54 +01:00
|
|
|
|
2013-09-02 12:26:26 +01:00
|
|
|
const QString& id() const { return mId; }
|
2013-07-03 02:52:44 +01:00
|
|
|
const QString& type() const { return mType; }
|
2013-07-04 00:09:49 +01:00
|
|
|
QVariantMap& body() { return mBody; }
|
2014-01-14 21:02:38 +00:00
|
|
|
const QVariantMap& body() const { return mBody; }
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-09-02 12:26:26 +01:00
|
|
|
//Get and set info from body. Note that id and type can not be accessed through these.
|
2015-09-11 11:34:52 +01:00
|
|
|
template<typename T> T get(const QString& key, const T& defaultValue = {}) const {
|
2013-07-30 19:21:06 +01:00
|
|
|
return mBody.value(key,defaultValue).template value<T>(); //Important note: Awesome template syntax is awesome
|
2013-07-04 00:09:49 +01:00
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
template<typename T> void set(const QString& key, const T& value) { mBody[key] = QVariant(value); }
|
2013-08-10 04:21:55 +01:00
|
|
|
bool has(const QString& key) const { return mBody.contains(key); }
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2013-09-24 13:13:02 +01:00
|
|
|
QSharedPointer<QIODevice> payload() const { return mPayload; }
|
2014-11-09 20:57:09 +00:00
|
|
|
void setPayload(const QSharedPointer<QIODevice>& device, qint64 payloadSize) { mPayload = device; mPayloadSize = payloadSize; Q_ASSERT(mPayloadSize >= -1); }
|
2013-09-20 14:54:30 +01:00
|
|
|
bool hasPayload() const { return (mPayloadSize != 0); }
|
2014-11-09 20:57:09 +00:00
|
|
|
qint64 payloadSize() const { return mPayloadSize; } //-1 means it is an endless stream
|
2014-09-21 21:22:06 +01:00
|
|
|
FileTransferJob* createPayloadTransferJob(const QUrl &destination) const;
|
2013-09-09 17:30:44 +01:00
|
|
|
|
|
|
|
//To be called by a particular DeviceLink
|
|
|
|
QVariantMap payloadTransferInfo() const { return mPayloadTransferInfo; }
|
|
|
|
void setPayloadTransferInfo(const QVariantMap& map) { mPayloadTransferInfo = map; }
|
2013-09-09 21:51:34 +01:00
|
|
|
bool hasPayloadTransferInfo() const { return !mPayloadTransferInfo.isEmpty(); }
|
2013-09-09 17:30:44 +01:00
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
private:
|
2013-09-09 17:30:44 +01:00
|
|
|
|
2013-09-02 12:26:26 +01:00
|
|
|
void setId(const QString& id) { mId = id; }
|
2013-07-04 00:09:49 +01:00
|
|
|
void setType(const QString& t) { mType = t; }
|
|
|
|
void setBody(const QVariantMap& b) { mBody = b; }
|
2014-11-09 20:57:09 +00:00
|
|
|
void setPayloadSize(qint64 s) { mPayloadSize = s; }
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-09-01 21:13:03 +01:00
|
|
|
QString mId;
|
2013-06-06 04:57:06 +01:00
|
|
|
QString mType;
|
2013-09-02 12:26:26 +01:00
|
|
|
QVariantMap mBody;
|
2014-08-01 11:29:34 +01:00
|
|
|
|
2013-09-24 13:13:02 +01:00
|
|
|
QSharedPointer<QIODevice> mPayload;
|
2014-11-09 20:57:09 +00:00
|
|
|
qint64 mPayloadSize;
|
2013-09-09 17:30:44 +01:00
|
|
|
QVariantMap mPayloadTransferInfo;
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-09-09 17:30:44 +01:00
|
|
|
};
|
2013-07-04 00:09:49 +01:00
|
|
|
|
2016-06-03 14:50:46 +01:00
|
|
|
KDECONNECTCORE_EXPORT QDebug operator<<(QDebug s, const NetworkPackage& pkg);
|
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
#endif // NETWORKPACKAGE_H
|