kdeconnect-kde/kded/networkpackage.h

106 lines
3.7 KiB
C
Raw Normal View History

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
#include "networkpackagetypes.h"
2013-08-28 22:47:39 +01:00
#include <QObject>
2013-06-06 04:57:06 +01:00
#include <QString>
#include <QVariant>
#include <QStringList>
#include <QIODevice>
#include <QtCrypto>
#include <QSharedPointer>
#include <qjson/parser.h>
2013-08-28 22:47:39 +01:00
#include <KUrl>
#include "kdebugnamespace.h"
#include "default_args.h"
2013-06-06 04:57:06 +01:00
class FileTransferJob;
class NetworkPackage : public QObject
2013-06-06 04:57:06 +01:00
{
Q_OBJECT
Q_PROPERTY( QString id READ id WRITE setId )
Q_PROPERTY( QString type READ type WRITE setType )
Q_PROPERTY( QVariantMap body READ body WRITE setBody )
2013-06-06 04:57:06 +01:00
public:
2013-09-03 01:13:13 +01:00
const static QCA::EncryptionAlgorithm EncryptionAlgorithm;
const static int ProtocolVersion;
2013-08-07 12:40:39 +01:00
NetworkPackage(const QString& type);
2013-06-17 11:23:08 +01:00
static void createIdentityPackage(NetworkPackage*);
QByteArray serialize() const;
static bool unserialize(const QByteArray& json, NetworkPackage* out);
2013-06-06 04:57:06 +01:00
void encrypt(QCA::PublicKey& key);
bool decrypt(QCA::PrivateKey& key, NetworkPackage* out) const;
2013-09-16 14:21:22 +01:00
bool isEncrypted() const { return mType == PACKAGE_TYPE_ENCRYPTED; }
const QString& id() const { return mId; }
const QString& type() const { return mType; }
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
//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 = default_arg<T>::get()) const {
return mBody.value(key,defaultValue).template value<T>(); //Important note: Awesome template syntax is awesome
}
template<typename T> void set(const QString& key, const T& value) { mBody[key] = QVariant(value); }
bool has(const QString& key) const { return mBody.contains(key); }
2013-06-17 11:23:08 +01:00
QSharedPointer<QIODevice> payload() const { return mPayload; }
void setPayload(const QSharedPointer<QIODevice>& device, int payloadSize) { mPayload = device; mPayloadSize = payloadSize; Q_ASSERT(mPayloadSize >= -1); }
bool hasPayload() const { return (mPayloadSize != 0); }
int payloadSize() const { return mPayloadSize; } //-1 means it is an endless stream
FileTransferJob* createPayloadTransferJob(const KUrl& destination) const;
//To be called by a particular DeviceLink
QVariantMap payloadTransferInfo() const { return mPayloadTransferInfo; }
void setPayloadTransferInfo(const QVariantMap& map) { mPayloadTransferInfo = map; }
bool hasPayloadTransferInfo() const { return !mPayloadTransferInfo.isEmpty(); }
2013-06-06 04:57:06 +01:00
private:
void setId(const QString& id) { mId = id; }
void setType(const QString& t) { mType = t; }
void setBody(const QVariantMap& b) { mBody = b; }
void setPayloadSize(int s) { mPayloadSize = s; }
2013-06-06 04:57:06 +01:00
QString mId;
2013-06-06 04:57:06 +01:00
QString mType;
QVariantMap mBody;
2013-06-06 04:57:06 +01:00
QSharedPointer<QIODevice> mPayload;
int mPayloadSize;
QVariantMap mPayloadTransferInfo;
2013-06-06 04:57:06 +01:00
};
2013-06-06 04:57:06 +01:00
#endif // NETWORKPACKAGE_H