squawk/qomemo/qxmpp_omemo_manager.cpp

67 lines
1.8 KiB
C++
Raw Normal View History

/*
* Created by victoria on 2021-05-12.
*/
#include "qxmpp_omemo_manager.h"
#include <QDomElement>
#include <QXmppClient.h>
#include <QXmppPubSubIq.h>
#include <iostream>
QXmpp::Omemo::Manager::Manager() : deviceService(new QXmpp::Omemo::DeviceService(this)) {
connect(this, &Manager::deviceListReceived, deviceService.get(), &DeviceService::onDeviceListReceived);
}
bool QXmpp::Omemo::Manager::handleStanza(const QDomElement &stanza) {
QString str{};
QTextStream info(&str);
stanza.save(info, 4);
std::cout << str.toStdString();
if (stanza.tagName() == "iq") {
if (stanza.attribute("type") == "result") {
auto pubsub = stanza.firstChildElement("pubsub");
if (!pubsub.isNull()) {
auto items = pubsub.firstChildElement("items");
if (items.attribute("node") == "eu.siacs.conversations.axolotl.devicelist") {
auto item = items.firstChildElement("item");
if (!item.isNull()) {
auto list = item.firstChildElement("list");
if (!list.isNull()) {
DeviceList deviceList{};
deviceList.fromXml(list);
emit deviceListReceived(stanza.attribute("from"), deviceList);
return true;
}
}
}
}
}
}
return false;
}
void QXmpp::Omemo::Manager::setClient(QXmppClient *client) {
QXmppClientExtension::setClient(client);
if (!client)
return;
QObject::connect(client, &QXmppClient::connected, this, &Manager::fetchOwnDevices);
}
void QXmpp::Omemo::Manager::fetchOwnDevices() {
QXmppPubSubIq iq{};
iq.setFrom(client()->configuration().jid());
iq.setTo(client()->configuration().jidBare());
iq.setType(QXmppIq::Get);
iq.setQueryNode("eu.siacs.conversations.axolotl.devicelist");
client()->sendPacket(iq);
}