squawk/qomemo/variant/conversations.cpp

79 lines
2.0 KiB
C++
Raw Normal View History

2021-05-12 21:32:13 +00:00
/*
* Created by victoria on 2021-05-13.
*/
#include "conversations.h"
#include "qomemo/device.h"
#include "shared/qxmppfactories.h"
#include <QXmppIq.h>
using namespace QXmpp::Omemo;
using namespace QXmpp::Factories;
QXmppElement Variant::Conversations::deviceToXml(const Device &device) {
2021-05-13 14:54:37 +00:00
auto result = createElement("device");
result.setAttribute("id", QString::number(device.id));
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return result;
2021-05-12 21:32:13 +00:00
}
Device Variant::Conversations::deviceFromXml(const QXmppElement &xml) {
2021-05-13 14:54:37 +00:00
Device result{};
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
if (!elementMatches(xml, "device"))
return result;
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
result.id = xml.attribute("id").toInt();
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return result;
2021-05-12 21:32:13 +00:00
}
QXmppElement
Variant::Conversations::deviceListToXml(const DeviceList &deviceList) {
2021-05-13 14:54:37 +00:00
auto element = createElement("list", "eu.siacs.conversations.axolotl");
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
for (const auto &device : deviceList.devices) {
element.appendChild(deviceToXml(device));
}
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return element;
2021-05-12 21:32:13 +00:00
}
DeviceList Variant::Conversations::deviceListFromXml(const QXmppElement &xml) {
2021-05-13 14:54:37 +00:00
DeviceList result{};
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
if (!elementMatches(xml, "list", "eu.siacs.conversations.axolotl"))
return result;
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto deviceElement = xml.firstChildElement("device");
while (!deviceElement.isNull()) {
result.devices.push_back(deviceFromXml(deviceElement));
deviceElement = deviceElement.nextSiblingElement("device");
}
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return result;
2021-05-12 21:32:13 +00:00
}
QXmppIq Variant::Conversations::deviceListSetIq(const DeviceList &deviceList) {
2021-05-13 14:54:37 +00:00
QXmppIq iq{};
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
iq.setType(QXmppIq::Set);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto item = createElement("item");
item.appendChild(deviceListToXml(deviceList));
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto publish = createElement("publish");
publish.setAttribute("node", "eu.siacs.conversations.axolotl.devicelist");
publish.appendChild(item);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
auto pubSub = createElement("pubsub", "http://jabber.org/protocol/pubsub");
pubSub.appendChild(publish);
pubSub.appendChild(createOpenPublishOptions());
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
iq.extensions().push_back(pubSub);
2021-05-12 21:32:13 +00:00
2021-05-13 14:54:37 +00:00
return iq;
2021-05-12 21:32:13 +00:00
}