File indexing completed on 2024-05-05 05:39:01

0001 /*
0002     SNI DBus Serialisers
0003     SPDX-FileCopyrightText: 2015 David Edmundson <davidedmundson@kde.org>
0004     SPDX-FileCopyrightText: 2009 Marco Martin <notmart@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "snidbus.h"
0010 
0011 #include <QSysInfo>
0012 #include <QtEndian>
0013 
0014 // mostly copied from KStatusNotiferItemDbus.cpps from knotification
0015 
0016 KDbusImageStruct::KDbusImageStruct()
0017 {
0018 }
0019 
0020 KDbusImageStruct::KDbusImageStruct(const QImage &image)
0021 {
0022     width = image.size().width();
0023     height = image.size().height();
0024     if (image.format() == QImage::Format_ARGB32) {
0025         data = QByteArray((char *)image.bits(), image.sizeInBytes());
0026     } else {
0027         QImage image32 = image.convertToFormat(QImage::Format_ARGB32);
0028         data = QByteArray((char *)image32.bits(), image32.sizeInBytes());
0029     }
0030 
0031     // swap to network byte order if we are little endian
0032     if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
0033         quint32 *uintBuf = (quint32 *)data.data();
0034         for (uint i = 0; i < data.size() / sizeof(quint32); ++i) {
0035             *uintBuf = qToBigEndian(*uintBuf);
0036             ++uintBuf;
0037         }
0038     }
0039 }
0040 
0041 // Marshall the ImageStruct data into a D-BUS argument
0042 const QDBusArgument &operator<<(QDBusArgument &argument, const KDbusImageStruct &icon)
0043 {
0044     argument.beginStructure();
0045     argument << icon.width;
0046     argument << icon.height;
0047     argument << icon.data;
0048     argument.endStructure();
0049     return argument;
0050 }
0051 
0052 // Retrieve the ImageStruct data from the D-BUS argument
0053 const QDBusArgument &operator>>(const QDBusArgument &argument, KDbusImageStruct &icon)
0054 {
0055     qint32 width;
0056     qint32 height;
0057     QByteArray data;
0058 
0059     argument.beginStructure();
0060     argument >> width;
0061     argument >> height;
0062     argument >> data;
0063     argument.endStructure();
0064 
0065     icon.width = width;
0066     icon.height = height;
0067     icon.data = data;
0068 
0069     return argument;
0070 }
0071 
0072 // Marshall the ImageVector data into a D-BUS argument
0073 const QDBusArgument &operator<<(QDBusArgument &argument, const KDbusImageVector &iconVector)
0074 {
0075     argument.beginArray(qMetaTypeId<KDbusImageStruct>());
0076     for (int i = 0; i < iconVector.size(); ++i) {
0077         argument << iconVector[i];
0078     }
0079     argument.endArray();
0080     return argument;
0081 }
0082 
0083 // Retrieve the ImageVector data from the D-BUS argument
0084 const QDBusArgument &operator>>(const QDBusArgument &argument, KDbusImageVector &iconVector)
0085 {
0086     argument.beginArray();
0087     iconVector.clear();
0088 
0089     while (!argument.atEnd()) {
0090         KDbusImageStruct element;
0091         argument >> element;
0092         iconVector.append(element);
0093     }
0094 
0095     argument.endArray();
0096 
0097     return argument;
0098 }
0099 
0100 // Marshall the ToolTipStruct data into a D-BUS argument
0101 const QDBusArgument &operator<<(QDBusArgument &argument, const KDbusToolTipStruct &toolTip)
0102 {
0103     argument.beginStructure();
0104     argument << toolTip.icon;
0105     argument << toolTip.image;
0106     argument << toolTip.title;
0107     argument << toolTip.subTitle;
0108     argument.endStructure();
0109     return argument;
0110 }
0111 
0112 // Retrieve the ToolTipStruct data from the D-BUS argument
0113 const QDBusArgument &operator>>(const QDBusArgument &argument, KDbusToolTipStruct &toolTip)
0114 {
0115     QString icon;
0116     KDbusImageVector image;
0117     QString title;
0118     QString subTitle;
0119 
0120     argument.beginStructure();
0121     argument >> icon;
0122     argument >> image;
0123     argument >> title;
0124     argument >> subTitle;
0125     argument.endStructure();
0126 
0127     toolTip.icon = icon;
0128     toolTip.image = image;
0129     toolTip.title = title;
0130     toolTip.subTitle = subTitle;
0131 
0132     return argument;
0133 }