File indexing completed on 2024-04-14 15:49:45

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2017 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <DBusTypes.hxx>
0022 
0023 #include <QtEndian>
0024 
0025 //--------------------------------------------------------------------------------
0026 
0027 const QDBusArgument &operator>>(const QDBusArgument &argument, KDbusImageStruct &pixmap)
0028 {
0029   pixmap = QPixmap();
0030 
0031   if ( argument.currentType() == QDBusArgument::StructureType )
0032   {
0033     qint32 w = 0, h = 0;
0034     QByteArray data;
0035 
0036     argument.beginStructure();
0037     argument >> w;
0038     argument >> h;
0039     argument >> data;
0040     argument.endStructure();
0041 
0042     // convert data to QPixmap
0043     if ( w && h && !data.isEmpty() )
0044     {
0045       if ( QSysInfo::ByteOrder == QSysInfo::LittleEndian )
0046       {
0047         quint32 *uintBuf = reinterpret_cast<quint32 *>(data.data());
0048         for (uint i = 0; i < data.size() / sizeof(quint32); ++i)
0049         {
0050           *uintBuf = qToBigEndian(*uintBuf);
0051           ++uintBuf;
0052         }
0053       }
0054       pixmap = QPixmap::fromImage(QImage(reinterpret_cast<const uchar *>(data.constData()), w, h, QImage::Format_ARGB32));
0055     }
0056   }
0057 
0058   return argument;
0059 }
0060 
0061 //--------------------------------------------------------------------------------
0062 
0063 const QDBusArgument &operator>>(const QDBusArgument &argument, KDbusImageVector &icon)
0064 {
0065   icon = QIcon();
0066 
0067   if ( argument.currentType() == QDBusArgument::ArrayType )
0068   {
0069     argument.beginArray();
0070 
0071     while ( !argument.atEnd() )
0072     {
0073       KDbusImageStruct pixmap;
0074       argument >> pixmap;
0075       if ( !pixmap.isNull() )
0076         icon.addPixmap(pixmap);
0077     }
0078 
0079     argument.endArray();
0080   }
0081   return argument;
0082 }
0083 
0084 //--------------------------------------------------------------------------------
0085 
0086 const QDBusArgument &operator>>(const QDBusArgument &argument, KDbusToolTipStruct &tip)
0087 {
0088   tip = KDbusToolTipStruct();
0089 
0090   if ( argument.currentType() == QDBusArgument::StructureType )
0091   {
0092     argument.beginStructure();
0093     argument >> tip.icon;
0094     argument >> tip.image;
0095     argument >> tip.title;
0096     argument >> tip.subTitle;
0097     argument.endStructure();
0098   }
0099 
0100   return argument;
0101 }
0102 
0103 //--------------------------------------------------------------------------------
0104 // add "dummy" operators we never need since we'll never send icons, but qDBusRegisterMetaType needs them
0105 // But we must correctly create the structure information
0106 
0107 QDBusArgument &operator<<(QDBusArgument &argument, const KDbusImageStruct &pixmap)
0108 {
0109   argument.beginStructure();
0110   argument << qint32(pixmap.width());
0111   argument << qint32(pixmap.height());
0112   argument << QByteArray();
0113   argument.endStructure();
0114   return argument;
0115 }
0116 
0117 //--------------------------------------------------------------------------------
0118 
0119 QDBusArgument &operator<<(QDBusArgument &argument, const KDbusImageVector &)
0120 {
0121   argument.beginArray(qMetaTypeId<KDbusImageStruct>());
0122   argument.endArray();
0123   return argument;
0124 }
0125 
0126 //--------------------------------------------------------------------------------
0127 
0128 QDBusArgument &operator<<(QDBusArgument &argument, const KDbusToolTipStruct &tip)
0129 {
0130   argument.beginStructure();
0131   argument << tip.icon;
0132   argument << tip.image;
0133   argument << tip.title;
0134   argument << tip.subTitle;
0135   argument.endStructure();
0136   return argument;
0137 }
0138 
0139 //--------------------------------------------------------------------------------