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-07-04 00:09:49 +01:00
|
|
|
#include <QObject>
|
2013-06-06 04:57:06 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QVariant>
|
2013-07-30 19:21:06 +01:00
|
|
|
#include <QStringList>
|
2013-07-04 00:09:49 +01:00
|
|
|
#include <qjson/parser.h>
|
|
|
|
#include "default_args.h"
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-07-04 00:09:49 +01:00
|
|
|
class NetworkPackage : public QObject
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
2013-07-04 00:09:49 +01:00
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY( long id READ id WRITE setId )
|
|
|
|
Q_PROPERTY( QString type READ type WRITE setType )
|
|
|
|
Q_PROPERTY( QVariantMap body READ body WRITE setBody )
|
|
|
|
Q_PROPERTY( int version READ version WRITE setVersion )
|
2013-06-06 04:57:06 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2013-07-04 00:09:49 +01:00
|
|
|
NetworkPackage(QString type);
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2013-07-04 00:09:49 +01:00
|
|
|
static void unserialize(QByteArray, NetworkPackage*);
|
|
|
|
QByteArray serialize() const;
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
static void createIdentityPackage(NetworkPackage*);
|
|
|
|
|
2013-06-27 13:33:51 +01:00
|
|
|
long 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; }
|
|
|
|
int version() const { return mVersion; }
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2013-07-04 00:09:49 +01:00
|
|
|
//Get and set info from body. Note that id, type and version can not be accessed through these.
|
2013-07-30 19:21:06 +01:00
|
|
|
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
|
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); }
|
|
|
|
|
|
|
|
bool has(const QString& key) { return mBody.contains(key); }
|
2013-06-17 11:23:08 +01:00
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
private:
|
2013-07-04 00:09:49 +01:00
|
|
|
void setId(long id) { mId = id; }
|
|
|
|
void setType(const QString& t) { mType = t; }
|
|
|
|
void setBody(const QVariantMap& b) { mBody = b; }
|
|
|
|
void setVersion(int v) { mVersion = v; }
|
2013-06-06 04:57:06 +01:00
|
|
|
|
|
|
|
long mId;
|
|
|
|
QString mType;
|
2013-07-04 00:09:49 +01:00
|
|
|
QVariantMap mBody; //json in the Android side
|
|
|
|
int mVersion;
|
2013-06-06 04:57:06 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-07-30 19:21:06 +01:00
|
|
|
//Set specialization need this awesome-to-the-max syntax:
|
|
|
|
//template<> inline void NetworkPackage::set<QStringList>(const QString& key, const QStringList& value) { mBody[key] = QVariant(value); }
|
2013-07-04 00:09:49 +01:00
|
|
|
|
2013-06-06 04:57:06 +01:00
|
|
|
#endif // NETWORKPACKAGE_H
|