File indexing completed on 2024-05-12 04:19:46

0001 /*
0002 Gwenview: an image viewer
0003 Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
0004 
0005 This program is free software; you can redistribute it and/or
0006 modify it under the terms of the GNU General Public License
0007 as published by the Free Software Foundation; either version 2
0008 of the License, or (at your option) any later version.
0009 
0010 This program is distributed in the hope that it will be useful,
0011 but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 GNU General Public License for more details.
0014 
0015 You should have received a copy of the GNU General Public License
0016 along with this program; if not, write to the Free Software
0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018 */
0019 
0020 #include "dbusabstractadaptor.h"
0021 
0022 // Qt
0023 #include <QDBusConnection>
0024 #include <QDBusMessage>
0025 #include <QMetaClassInfo>
0026 
0027 namespace Gwenview
0028 {
0029 DBusAbstractAdaptor::DBusAbstractAdaptor(const QString &objectDBusPath, QObject *parent)
0030     : QDBusAbstractAdaptor(parent)
0031     , mObjectPath(objectDBusPath)
0032 {
0033     Q_ASSERT(!mObjectPath.isEmpty());
0034 }
0035 
0036 void DBusAbstractAdaptor::signalPropertyChange(const QString &propertyName, const QVariant &value)
0037 {
0038     const bool firstChange = mChangedProperties.isEmpty();
0039 
0040     mChangedProperties.insert(propertyName, value);
0041 
0042     if (firstChange) {
0043         // trigger signal emission on next event loop
0044         QMetaObject::invokeMethod(this, &DBusAbstractAdaptor::emitPropertiesChangeDBusSignal, Qt::QueuedConnection);
0045     }
0046 }
0047 
0048 void DBusAbstractAdaptor::emitPropertiesChangeDBusSignal()
0049 {
0050     if (mChangedProperties.isEmpty()) {
0051         return;
0052     }
0053 
0054     const QMetaObject *metaObject = this->metaObject();
0055     const int dBusInterfaceNameIndex = metaObject->indexOfClassInfo("D-Bus Interface");
0056     Q_ASSERT(dBusInterfaceNameIndex >= 0);
0057     const char *dBusInterfaceName = metaObject->classInfo(dBusInterfaceNameIndex).value();
0058 
0059     QDBusMessage signalMessage =
0060         QDBusMessage::createSignal(mObjectPath, QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("PropertiesChanged"));
0061     signalMessage << dBusInterfaceName << mChangedProperties << QStringList();
0062 
0063     QDBusConnection::sessionBus().send(signalMessage);
0064 
0065     mChangedProperties.clear();
0066 }
0067 
0068 }
0069 
0070 #include "moc_dbusabstractadaptor.cpp"