File indexing completed on 2024-05-19 04:49:36

0001 /*
0002  * Copyright 2012  Alex Merry <alex.merry@kdemail.net>
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  *
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #include "DBusAbstractAdaptor.h"
0027 
0028 #include <QDebug>
0029 #include <QMetaClassInfo>
0030 #include <QDBusMessage>
0031 
0032 #include "core/support/Debug.h"
0033 
0034 DBusAbstractAdaptor::DBusAbstractAdaptor( QObject *parent )
0035     : QDBusAbstractAdaptor( parent )
0036     , m_connection( QDBusConnection::sessionBus() )
0037 {
0038 }
0039 
0040 QDBusConnection DBusAbstractAdaptor::connection() const
0041 {
0042     return m_connection;
0043 }
0044 
0045 void DBusAbstractAdaptor::setConnection( const QDBusConnection &conn )
0046 {
0047     m_connection = conn;
0048 }
0049 
0050 QString DBusAbstractAdaptor::dBusPath() const
0051 {
0052     return m_path;
0053 }
0054 
0055 void DBusAbstractAdaptor::setDBusPath( const QString &path )
0056 {
0057     m_path = path;
0058 }
0059 
0060 void DBusAbstractAdaptor::signalPropertyChange( const QString &property, const QVariant &value )
0061 {
0062     if ( m_updatedProperties.isEmpty() && m_invalidatedProperties.isEmpty() ) {
0063         QMetaObject::invokeMethod( this, "_m_emitPropertiesChanged", Qt::QueuedConnection );
0064         debug() << "MPRIS2: Queueing up a PropertiesChanged signal";
0065     }
0066 
0067     m_updatedProperties[property] = value;
0068 }
0069 
0070 void DBusAbstractAdaptor::signalPropertyChange( const QString &property )
0071 {
0072     if ( !m_invalidatedProperties.contains( property ) ) {
0073         if ( m_updatedProperties.isEmpty() && m_invalidatedProperties.isEmpty() ) {
0074             QMetaObject::invokeMethod( this, "_m_emitPropertiesChanged", Qt::QueuedConnection );
0075             debug() << "MPRIS2: Queueing up a PropertiesChanged signal";
0076         }
0077 
0078         m_invalidatedProperties << property;
0079     }
0080 }
0081 
0082 void DBusAbstractAdaptor::_m_emitPropertiesChanged()
0083 {
0084     Q_ASSERT( !m_path.isEmpty() );
0085 
0086     if( m_updatedProperties.isEmpty() && m_invalidatedProperties.isEmpty() ) {
0087         debug() << "MPRIS2: Nothing to do";
0088         return;
0089     }
0090 
0091     int ifaceIndex = metaObject()->indexOfClassInfo( "D-Bus Interface" );
0092     if ( ifaceIndex < 0 ) {
0093         warning() << "MPRIS2: No D-Bus interface given (missing Q_CLASSINFO)";
0094     } else {
0095         QDBusMessage signal = QDBusMessage::createSignal( m_path,
0096              QStringLiteral("org.freedesktop.DBus.Properties"),
0097              QStringLiteral("PropertiesChanged") );
0098         signal << metaObject()->classInfo( ifaceIndex ).value();
0099         signal << m_updatedProperties;
0100         signal << m_invalidatedProperties;
0101         m_connection.send( signal );
0102     }
0103 
0104     m_updatedProperties.clear();
0105     m_invalidatedProperties.clear();
0106 }
0107