1 /*
2  * Copyright 2012  Alex Merry <alex.merry@kdemail.net>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "DBusAbstractAdaptor.h"
27 
28 #include <QDebug>
29 #include <QMetaClassInfo>
30 #include <QDBusMessage>
31 
32 #include "core/support/Debug.h"
33 
DBusAbstractAdaptor(QObject * parent)34 DBusAbstractAdaptor::DBusAbstractAdaptor( QObject *parent )
35     : QDBusAbstractAdaptor( parent )
36     , m_connection( QDBusConnection::sessionBus() )
37 {
38 }
39 
connection() const40 QDBusConnection DBusAbstractAdaptor::connection() const
41 {
42     return m_connection;
43 }
44 
setConnection(const QDBusConnection & conn)45 void DBusAbstractAdaptor::setConnection( const QDBusConnection &conn )
46 {
47     m_connection = conn;
48 }
49 
dBusPath() const50 QString DBusAbstractAdaptor::dBusPath() const
51 {
52     return m_path;
53 }
54 
setDBusPath(const QString & path)55 void DBusAbstractAdaptor::setDBusPath( const QString &path )
56 {
57     m_path = path;
58 }
59 
signalPropertyChange(const QString & property,const QVariant & value)60 void DBusAbstractAdaptor::signalPropertyChange( const QString &property, const QVariant &value )
61 {
62     if ( m_updatedProperties.isEmpty() && m_invalidatedProperties.isEmpty() ) {
63         QMetaObject::invokeMethod( this, "_m_emitPropertiesChanged", Qt::QueuedConnection );
64         debug() << "MPRIS2: Queueing up a PropertiesChanged signal";
65     }
66 
67     m_updatedProperties[property] = value;
68 }
69 
signalPropertyChange(const QString & property)70 void DBusAbstractAdaptor::signalPropertyChange( const QString &property )
71 {
72     if ( !m_invalidatedProperties.contains( property ) ) {
73         if ( m_updatedProperties.isEmpty() && m_invalidatedProperties.isEmpty() ) {
74             QMetaObject::invokeMethod( this, "_m_emitPropertiesChanged", Qt::QueuedConnection );
75             debug() << "MPRIS2: Queueing up a PropertiesChanged signal";
76         }
77 
78         m_invalidatedProperties << property;
79     }
80 }
81 
_m_emitPropertiesChanged()82 void DBusAbstractAdaptor::_m_emitPropertiesChanged()
83 {
84     Q_ASSERT( !m_path.isEmpty() );
85 
86     if( m_updatedProperties.isEmpty() && m_invalidatedProperties.isEmpty() ) {
87         debug() << "MPRIS2: Nothing to do";
88         return;
89     }
90 
91     int ifaceIndex = metaObject()->indexOfClassInfo( "D-Bus Interface" );
92     if ( ifaceIndex < 0 ) {
93         warning() << "MPRIS2: No D-Bus interface given (missing Q_CLASSINFO)";
94     } else {
95         QDBusMessage signal = QDBusMessage::createSignal( m_path,
96              QStringLiteral("org.freedesktop.DBus.Properties"),
97              QStringLiteral("PropertiesChanged") );
98         signal << metaObject()->classInfo( ifaceIndex ).value();
99         signal << m_updatedProperties;
100         signal << m_invalidatedProperties;
101         m_connection.send( signal );
102     }
103 
104     m_updatedProperties.clear();
105     m_invalidatedProperties.clear();
106 }
107 
108