~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to plugins/nowlistening/nlmpris2.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
Import upstream version 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        nlmpris2.cpp
 
3
 
 
4
    Kopete Now Listening To plugin
 
5
 
 
6
    Copyright (c) 2012 by Volker Härtel <cyberbeat@gmx.de>
 
7
 
 
8
    Kopete (c) 2002,2003 by the Kopete developers  <kopete-devel@kde.org>
 
9
 
 
10
        Purpose:
 
11
        This class abstracts the interface to mpris by
 
12
        implementing NLMediaPlayer
 
13
 
 
14
    *************************************************************************
 
15
    *                                                                       *
 
16
    * This program is free software; you can redistribute it and/or modify  *
 
17
    * it under the terms of the GNU General Public License as published by  *
 
18
    * the Free Software Foundation; either version 2 of the License, or     *
 
19
    * (at your option) any later version.                                   *
 
20
    *                                                                       *
 
21
    *************************************************************************
 
22
*/
 
23
 
 
24
#include <kdebug.h>
 
25
 
 
26
 
 
27
#include <QtDBus/QtDBus>
 
28
 
 
29
#include "nlmpris2.h"
 
30
 
 
31
 
 
32
NLmpris2::NLmpris2() : NLMediaPlayer()
 
33
{
 
34
        m_type = Audio;
 
35
        m_name = "MPRIS2 compatible player";
 
36
        m_client = 0;
 
37
}
 
38
 
 
39
NLmpris2::~NLmpris2()
 
40
{
 
41
        delete m_client;
 
42
}
 
43
 
 
44
void NLmpris2::update()
 
45
{
 
46
        m_playing = false;
 
47
 
 
48
        if( m_client == 0 || !m_client->isValid() ) {
 
49
                QStringList services;
 
50
                const QDBusConnection& sessionConn( QDBusConnection::sessionBus() );
 
51
 
 
52
                // Check if the connection is successful
 
53
                if( sessionConn.isConnected() )
 
54
                {
 
55
                        const QDBusConnectionInterface* bus = sessionConn.interface();
 
56
                        const QDBusReply<QStringList>& reply( bus->registeredServiceNames() );
 
57
 
 
58
                        if( reply.isValid() )
 
59
                        {
 
60
                                // Search for "org.mpris.MediaPlayer2" string
 
61
                                services = reply.value().filter( "org.mpris.MediaPlayer2" );
 
62
                        }
 
63
                }
 
64
 
 
65
                // If no service was found then return false and unlock the mutex
 
66
                if( services.isEmpty() )
 
67
                {
 
68
                        return;
 
69
                }
 
70
                // Start the d-bus interface, needed to check the application status and make calls to it
 
71
                if ( m_client != 0 ){
 
72
                        delete m_client;
 
73
                        m_client = 0;
 
74
                }
 
75
                m_client = new QDBusInterface( services.at(0), "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties" );
 
76
                QDBusInterface dbusMprisRoot ( services.at(0), "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2");
 
77
 
 
78
                // See if the application is registered.
 
79
                if( !m_client->isValid() )
 
80
                {
 
81
                        return;
 
82
                }
 
83
 
 
84
                if ( !dbusMprisRoot.isValid() )
 
85
                {
 
86
                        m_name = QString( "MPRIS2 compatible player" );
 
87
                }
 
88
                else
 
89
                {
 
90
                        m_name = dbusMprisRoot.property( "Identity" ).toString();
 
91
                }
 
92
        }
 
93
 
 
94
        // see if it's playing
 
95
        QDBusReply <QVariant> mprisStatus = m_client->call ( "Get", "org.mpris.MediaPlayer2.Player", "PlaybackStatus" );
 
96
        if (!mprisStatus.isValid())
 
97
        {
 
98
                return;
 
99
        }
 
100
        m_playing = ( mprisStatus.value().toString() == "Playing" );
 
101
 
 
102
        QDBusReply<QVariant> metaDataReply = m_client->call( "Get", "org.mpris.MediaPlayer2.Player", "Metadata" );
 
103
        if ( !metaDataReply.isValid() )
 
104
        {
 
105
                return;
 
106
        }
 
107
        if ( !metaDataReply.value().canConvert<QDBusArgument>() )
 
108
                return;
 
109
        QVariantMap metaData;
 
110
        QDBusArgument arg = metaDataReply.value().value<QDBusArgument>();
 
111
        arg >> metaData;
 
112
 
 
113
        // Fetch title
 
114
        const QString newTrack = metaData["xesam:title"].toString();
 
115
 
 
116
        if ( newTrack != m_track )
 
117
        {
 
118
                m_newTrack = true;
 
119
                m_track = newTrack;
 
120
        }
 
121
 
 
122
        // Fetch album
 
123
        m_album = metaData["xesam:album"].toString();
 
124
 
 
125
        // Fetch artist
 
126
        m_artist = metaData["xesam:artist"].toString();
 
127
 
 
128
}