~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/nowplaying/nowplayingengine.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright 2007 Alex Merry <alex.merry@kdemail.net>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License version 2 as
 
6
 *   published by the Free Software Foundation
 
7
 *
 
8
 *   This program is distributed in the hope that it will be useful,
 
9
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *   GNU General Public License for more details
 
12
 *
 
13
 *   You should have received a copy of the GNU Library General Public
 
14
 *   License along with this program; if not, write to the
 
15
 *   Free Software Foundation, Inc.,
 
16
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "nowplayingengine.h"
 
20
 
 
21
#include <config-nowplaying.h>
 
22
 
 
23
#include <QStringList>
 
24
 
 
25
#include <KDebug>
 
26
#include <KLocale>
 
27
 
 
28
#include "playerinterface/player.h"
 
29
#include "playerinterface/playerfactory.h"
 
30
#include "playerinterface/dbuswatcher.h"
 
31
#include "playerinterface/pollingwatcher.h"
 
32
#include "playerinterface/mpris/mpris.h"
 
33
#include "playerinterface/juk.h"
 
34
#ifdef XMMS_FOUND
 
35
#include "playerinterface/xmms.h"
 
36
#endif // XMMS_FOUND
 
37
 
 
38
#include "playercontrol.h"
 
39
#include "playercontainer.h"
 
40
 
 
41
NowPlayingEngine::NowPlayingEngine(QObject* parent,
 
42
                                   const QVariantList& args)
 
43
    : Plasma::DataEngine(parent),
 
44
      dbusWatcher(new DBusWatcher(this)),
 
45
      pollingWatcher(0)
 
46
{
 
47
    Q_UNUSED(args)
 
48
 
 
49
    setData("players", QStringList());
 
50
 
 
51
    connect(dbusWatcher, SIGNAL(newPlayer(Player::Ptr)),
 
52
            this,        SLOT(addPlayer(Player::Ptr)));
 
53
    connect(dbusWatcher, SIGNAL(playerDisappeared(Player::Ptr)),
 
54
            this,        SLOT(removePlayer(Player::Ptr)));
 
55
 
 
56
    dbusWatcher->addFactory(new MprisFactory(dbusWatcher));
 
57
    dbusWatcher->addFactory(new JukFactory(dbusWatcher));
 
58
 
 
59
#ifdef XMMS_FOUND
 
60
    pollingWatcher = new PollingWatcher(this);
 
61
    connect(pollingWatcher, SIGNAL(newPlayer(Player::Ptr)),
 
62
            this,        SLOT(addPlayer(Player::Ptr)));
 
63
    connect(pollingWatcher, SIGNAL(playerDisappeared(Player::Ptr)),
 
64
            this,        SLOT(removePlayer(Player::Ptr)));
 
65
    pollingWatcher->addFactory(new XmmsFactory(pollingWatcher));
 
66
#endif
 
67
}
 
68
 
 
69
Plasma::Service* NowPlayingEngine::serviceForSource(const QString& source)
 
70
{
 
71
    PlayerContainer* container = qobject_cast<PlayerContainer*>(containerForSource(source));
 
72
    if (container) {
 
73
        return container->service(this);
 
74
    } else {
 
75
        return DataEngine::serviceForSource(source);
 
76
    }
 
77
}
 
78
 
 
79
 
 
80
bool NowPlayingEngine::sourceRequestEvent(const QString& source)
 
81
{
 
82
    kDebug() << "Source" << source << "was requested";
 
83
    if (source == "help") {
 
84
        setData(source, "Use 'players' to get a list of players.\n"
 
85
                        "Use 'properties' to get a list of all properties that may be returned."
 
86
                        );
 
87
        return true;
 
88
    } else if (source == "properties") {
 
89
        setData(source, "State",           "QString - playing|paused|stopped");
 
90
        setData(source, "Artist",          "QString - the artist metadata for the\n"
 
91
                                           "          current track, if available");
 
92
        setData(source, "Album",           "QString - the album metadata for the\n"
 
93
                                           "          current track, if available");
 
94
        setData(source, "Title",           "QString - the title metadata for the\n"
 
95
                                           "          current track, if available");
 
96
        setData(source, "Track number",    "int     - the album/collection track number\n"
 
97
                                           "          (eg: on a CD) if known, 0 otherwise");
 
98
        setData(source, "Comment",         "QString - the comment metadata for the\n"
 
99
                                           "          current track, if available");
 
100
        setData(source, "Genre",           "QString - the comment metadata for the\n"
 
101
                                           "          current track, if available");
 
102
        setData(source, "Length",          "int     - the length of the current track\n"
 
103
                                           "          in seconds, 0 if unknown");
 
104
        setData(source, "Position",        "int     - the position of the current track\n"
 
105
                                           "          in seconds, 0 if unknown");
 
106
        setData(source, "Volume",          "float   - the volume, given as a float\n"
 
107
                                           "          between 0 and 1, or -1 if unknown");
 
108
        setData(source, "Artwork",         "QPixmap - the album artwork, if available");
 
109
        setData(source, "Lyrics",          "QString - song lyrics, if available");
 
110
        return true;
 
111
    }
 
112
 
 
113
    return false;
 
114
}
 
115
 
 
116
void NowPlayingEngine::addPlayer(Player::Ptr player)
 
117
{
 
118
    kDebug() << "Adding player" << player->name();
 
119
    Plasma::DataContainer *container = containerForSource("players");
 
120
    QStringList players;
 
121
    if (container) {
 
122
        players = container->data()["players"].toStringList();
 
123
    }
 
124
 
 
125
    players << player->name();
 
126
    setData("players", players);
 
127
 
 
128
    addSource(new PlayerContainer(player, this));
 
129
}
 
130
 
 
131
void NowPlayingEngine::removePlayer(Player::Ptr player)
 
132
{
 
133
    kDebug() << "Player" << player->name() << "disappeared";
 
134
    Plasma::DataContainer *container = containerForSource("players");
 
135
    if (container) {
 
136
        QStringList players = container->data()["players"].toStringList();
 
137
        players.removeAll(player->name());
 
138
        setData("players", players);
 
139
    }
 
140
 
 
141
    removeSource(player->name());
 
142
}
 
143
 
 
144
#include "nowplayingengine.moc"