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
|
|
|
#include "networkpacket.h"
|
2014-09-21 23:59:34 +01:00
|
|
|
#include "core_debug.h"
|
2013-08-31 12:04:00 +01:00
|
|
|
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QDataStream>
|
|
|
|
#include <QDateTime>
|
2014-09-21 23:59:34 +01:00
|
|
|
#include <QDebug>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QMetaObject>
|
|
|
|
#include <QMetaProperty>
|
2013-06-06 04:57:06 +01:00
|
|
|
|
2014-10-10 19:26:50 +01:00
|
|
|
#include "dbushelper.h"
|
2013-09-24 13:10:25 +01:00
|
|
|
#include "filetransferjob.h"
|
2015-03-02 04:16:07 +00:00
|
|
|
#include "kdeconnectconfig.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "pluginloader.h"
|
2013-09-24 13:10:25 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QDebug operator<<(QDebug s, const NetworkPacket &pkg)
|
2016-06-03 14:50:46 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
s.nospace() << "NetworkPacket(" << pkg.type() << ':' << pkg.body();
|
2016-06-03 14:50:46 +01:00
|
|
|
if (pkg.hasPayload()) {
|
|
|
|
s.nospace() << ":withpayload";
|
|
|
|
}
|
|
|
|
s.nospace() << ')';
|
|
|
|
return s.space();
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
const int NetworkPacket::s_protocolVersion = 7;
|
2017-09-03 20:39:44 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket::NetworkPacket(const QString &type, const QVariantMap &body)
|
2017-09-03 20:39:44 +01:00
|
|
|
: m_id(QString::number(QDateTime::currentMSecsSinceEpoch()))
|
|
|
|
, m_type(type)
|
|
|
|
, m_body(body)
|
|
|
|
, m_payload()
|
|
|
|
, m_payloadSize(0)
|
2013-07-04 00:09:49 +01:00
|
|
|
{
|
2013-09-09 17:30:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
QByteArray NetworkPacket::serialize() const
|
2013-06-06 04:57:06 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// Object -> QVariant
|
2020-01-04 12:34:48 +00:00
|
|
|
QVariantMap variant;
|
|
|
|
variant.insert(QStringLiteral("id"), m_id);
|
|
|
|
variant.insert(QStringLiteral("type"), m_type);
|
|
|
|
variant.insert(QStringLiteral("body"), m_body);
|
2013-07-04 00:09:49 +01:00
|
|
|
|
2020-01-04 12:34:48 +00:00
|
|
|
if (hasPayload()) {
|
|
|
|
variant.insert(QStringLiteral("payloadSize"), m_payloadSize);
|
|
|
|
variant.insert(QStringLiteral("payloadTransferInfo"), m_payloadTransferInfo);
|
2013-09-09 17:30:44 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// QVariant -> json
|
2014-09-12 23:49:56 +01:00
|
|
|
auto jsonDocument = QJsonDocument::fromVariant(variant);
|
|
|
|
QByteArray json = jsonDocument.toJson(QJsonDocument::Compact);
|
|
|
|
if (json.isEmpty()) {
|
2014-09-21 22:54:27 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Serialization error:";
|
2013-07-24 22:51:06 +01:00
|
|
|
} else {
|
2015-12-11 01:12:08 +00:00
|
|
|
/*if (!isEncrypted()) {
|
2018-03-04 19:48:51 +00:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "Serialized packet:" << json;
|
2015-12-11 01:12:08 +00:00
|
|
|
}*/
|
2013-07-24 22:51:06 +01:00
|
|
|
json.append('\n');
|
|
|
|
}
|
2013-07-04 00:09:49 +01:00
|
|
|
|
|
|
|
return json;
|
2013-06-06 04:57:06 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
template<class T>
|
|
|
|
void qvariant2qobject(const QVariantMap &variant, T *object)
|
2014-09-12 23:49:56 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
for (QVariantMap::const_iterator iter = variant.begin(); iter != variant.end(); ++iter) {
|
2021-11-24 09:44:31 +00:00
|
|
|
const int propertyIndex = T::staticMetaObject.indexOfProperty(iter.key().toLatin1().data());
|
2015-04-01 22:38:32 +01:00
|
|
|
if (propertyIndex < 0) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "missing property" << object << iter.key();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-24 19:04:33 +01:00
|
|
|
QMetaProperty property = T::staticMetaObject.property(propertyIndex);
|
|
|
|
bool ret = property.writeOnGadget(object, *iter);
|
2015-04-01 22:38:32 +01:00
|
|
|
if (!ret) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "couldn't set" << object << "->" << property.name() << '=' << *iter;
|
2014-09-12 23:49:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool NetworkPacket::unserialize(const QByteArray &a, NetworkPacket *np)
|
2013-06-17 11:23:08 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// Json -> QVariant
|
2014-09-12 23:49:56 +01:00
|
|
|
QJsonParseError parseError;
|
|
|
|
auto parser = QJsonDocument::fromJson(a, &parseError);
|
|
|
|
if (parser.isNull()) {
|
2014-09-21 22:54:27 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Unserialization error:" << parseError.errorString();
|
2013-09-03 15:01:28 +01:00
|
|
|
return false;
|
2013-07-04 02:34:35 +01:00
|
|
|
}
|
2013-07-04 00:09:49 +01:00
|
|
|
|
2014-09-12 23:49:56 +01:00
|
|
|
auto variant = parser.toVariant().toMap();
|
|
|
|
qvariant2qobject(variant, np);
|
2013-09-09 17:28:52 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
np->m_payloadTransferInfo = variant[QStringLiteral("payloadTransferInfo")].toMap(); // Will return an empty qvariantmap if was not present, which is ok
|
2013-09-09 17:30:44 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Ids containing characters that are not allowed as dbus paths would make app crash
|
|
|
|
if (np->m_body.contains(QStringLiteral("deviceId"))) {
|
2016-11-26 14:38:08 +00:00
|
|
|
QString deviceId = np->get<QString>(QStringLiteral("deviceId"));
|
2019-08-14 16:36:19 +01:00
|
|
|
DBusHelper::filterNonExportableCharacters(deviceId);
|
2016-11-26 14:38:08 +00:00
|
|
|
np->set(QStringLiteral("deviceId"), deviceId);
|
2014-10-10 19:26:50 +01:00
|
|
|
}
|
2014-08-01 11:29:34 +01:00
|
|
|
|
2013-09-03 15:01:28 +01:00
|
|
|
return true;
|
2013-06-17 11:23:08 +01:00
|
|
|
}
|
2013-09-02 02:17:23 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
FileTransferJob *NetworkPacket::createPayloadTransferJob(const QUrl &destination) const
|
2013-09-24 13:10:25 +01:00
|
|
|
{
|
2019-06-02 15:02:21 +01:00
|
|
|
return new FileTransferJob(this, destination);
|
2013-09-24 13:10:25 +01:00
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_networkpacket.cpp"
|