From 5244f7909e04b23fbd5706dc6bcadafba21f7600 Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 16 Nov 2014 16:40:06 +0100 Subject: initial notification support --- notificationconn.cc | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 notificationconn.cc (limited to 'notificationconn.cc') diff --git a/notificationconn.cc b/notificationconn.cc new file mode 100644 index 0000000..fde3303 --- /dev/null +++ b/notificationconn.cc @@ -0,0 +1,139 @@ +#include +#include +#include + +#include "sappeer.h" +#include "endianhelpers.h" +#include "notificationconn.h" + +NotificationConn::Notification::Notification() + : type(NotificationPopup), sequenceNumber(0), urgent(false), + applicationId(0), category(0), + count(0), sender(0), + notificationId(-1) +{ +} + +NotificationConn::NotificationConn(SAPConnection *conn, QObject *parent) + : QObject(parent), _conn(conn), _socket(conn->getSocket(104)) +{ + connect(_conn, SIGNAL(disconnected()), SLOT(deleteLater())); + Q_ASSERT(_socket); + connect(_socket, SIGNAL(connected()), SLOT(handleConnected())); + connect(_socket, SIGNAL(messageReceived()), SLOT(handleMessageReceived())); +} + +QByteArray NotificationConn::packNotification(const Notification &n) +{ + QByteArray data; + + append(data, 0x10 | n.type); + append(data, n.sequenceNumber | (n.urgent ? 0x4000 : 0)); + + append(data, n.applicationId); + append(data, n.category); + + int attributeCount = 0; + + // Let's count attributes first + if (!n.packageName.isEmpty()) attributeCount++; + if (n.count >= 0) attributeCount++; + if (!n.title.isEmpty()) attributeCount++; + if (n.time.isValid()) attributeCount++; + if (n.sender >= 0) attributeCount++; + if (!n.body.isEmpty()) attributeCount++; + if (!n.thumbnail.isEmpty()) attributeCount++; + if (!n.applicationName.isEmpty()) attributeCount++; + //TODO if (n.openInHost) attributeCount++; + //if (n.openInWatch) attributeCount++; + if (n.notificationId != -1) attributeCount++; + + append(data, attributeCount); + + if (n.time.isValid()) { + append(data, (NotificationTime << 24) | sizeof(qint64)); + append(data, n.time.toMSecsSinceEpoch()); + } + if (!n.thumbnail.isEmpty()) { + append(data, (NotificationThumbnail << 24) | n.thumbnail.size()); + data.append(n.thumbnail); + } + if (!n.applicationName.isEmpty()) { + QByteArray str = n.applicationName.toUtf8(); + append(data, (NotificationApplicationName << 24) | str.length()); + data.append(str); + } + if (!n.packageName.isEmpty()) { + QByteArray str = n.packageName.toUtf8(); + append(data, (NotificationPackageName << 24) | str.length()); + data.append(str); + } + if (!n.title.isEmpty()) { + QByteArray str = n.title.toUtf8(); + append(data, (NotificationTitle << 24) | str.length()); + data.append(str); + } + if (!n.body.isEmpty()) { + QByteArray str = n.body.toUtf8(); + append(data, (NotificationBody << 24) | str.length()); + data.append(str); + } + if (n.count >= 0) { + append(data, (NotificationCount << 24) | sizeof(quint16)); + append(data, n.count); + } + if (n.sender >= 0) { + append(data, (NotificationSender << 24) | sizeof(quint16)); + append(data, n.sender); + } + if (n.notificationId != -1) { + append(data, (NotificationId << 24) | sizeof(quint32)); + append(data, n.notificationId); + } + + return data; +} + +void NotificationConn::handleConnected() +{ + qDebug() << "Manager socket now connected!"; + + QTimer::singleShot(2000, this, SLOT(performTest())); + +} + +void NotificationConn::handleMessageReceived() +{ + QByteArray data = _socket->receive(); + data.remove(0, 1); // Remove first null byte + + qDebug() << "Got notif msg" << data.toHex(); + + // TODO Seems that we receive the notification ID that we should act upon. +} + +void NotificationConn::performTest() +{ + qDebug() << "Performing notif test"; + + Notification n; + n.sequenceNumber = 1; + n.type = NotificationPopup; + n.time = QDateTime::currentDateTime(); + n.title = "A title"; + n.packageName = "com.example.package"; + n.applicationName = "Example package"; + n.body = "A long body"; + n.sender = 0; + n.count = 13; + n.category = 0; + n.applicationId = 0xc2d7; + n.notificationId = 1; + + QByteArray packet = packNotification(n); + packet.prepend('\0'); + + qDebug() << packet.toHex(); + + _socket->send(packet); +} -- cgit v1.2.3