00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "nowplayingengine.h"
00020
00021 #include <config-nowplaying.h>
00022
00023 #include <QStringList>
00024
00025 #include <KDebug>
00026 #include <KLocale>
00027
00028 #include "player.h"
00029 #include "playerfactory.h"
00030 #include "dbuswatcher.h"
00031 #include "pollingwatcher.h"
00032 #include "mpris.h"
00033 #include "juk.h"
00034 #include "amarok.h"
00035 #ifdef XMMS_FOUND
00036 #include "xmms.h"
00037 #endif // XMMS_FOUND
00038
00039 NowPlayingEngine::NowPlayingEngine(QObject* parent,
00040 const QVariantList& args)
00041 : Plasma::DataEngine(parent),
00042 dbusWatcher(new DBusWatcher(this)),
00043 pollingWatcher(new PollingWatcher(this))
00044 {
00045 Q_UNUSED(args)
00046
00047 connect(dbusWatcher, SIGNAL(newPlayer(Player::Ptr)),
00048 this, SLOT(addPlayer(Player::Ptr)));
00049 connect(dbusWatcher, SIGNAL(playerDisappeared(Player::Ptr)),
00050 this, SLOT(removePlayer(Player::Ptr)));
00051 connect(pollingWatcher, SIGNAL(newPlayer(Player::Ptr)),
00052 this, SLOT(addPlayer(Player::Ptr)));
00053 connect(pollingWatcher, SIGNAL(playerDisappeared(Player::Ptr)),
00054 this, SLOT(removePlayer(Player::Ptr)));
00055
00056 dbusWatcher->addFactory(new MprisFactory(dbusWatcher));
00057 dbusWatcher->addFactory(new JukFactory(dbusWatcher));
00058 dbusWatcher->addFactory(new AmarokFactory(dbusWatcher));
00059 #ifdef XMMS_FOUND
00060 pollingWatcher->addFactory(new XmmsFactory(pollingWatcher));
00061 #endif
00062 }
00063
00064 bool NowPlayingEngine::sourceRequestEvent(const QString &source)
00065 {
00066 kDebug() << "Source" << source << "was requested";
00067 QString lowerSource = source.toLower();
00068 if (lowerSource == "help") {
00069 setData(source, "Use 'players' to get a list of players.\n"
00070 "Use 'properties' to get a list of all properties that may be returned."
00071 );
00072 return true;
00073 } else if (lowerSource == "properties") {
00074 setData(source, "State", "QString - playing|paused|stopped");
00075 setData(source, "Artist", "QString - the artist metadata for the\n"
00076 " current track, if available");
00077 setData(source, "Album", "QString - the album metadata for the\n"
00078 " current track, if available");
00079 setData(source, "Title", "QString - the title metadata for the\n"
00080 " current track, if available");
00081 setData(source, "Track number", "int - the album/collection track number\n"
00082 " (eg: on a CD) if known, 0 otherwise");
00083 setData(source, "Comment", "QString - the comment metadata for the\n"
00084 " current track, if available");
00085 setData(source, "Genre", "QString - the comment metadata for the\n"
00086 " current track, if available");
00087 setData(source, "Length", "int - the length of the current track\n"
00088 " in seconds, 0 if unknown");
00089 setData(source, "Position", "int - the position of the current track\n"
00090 " in seconds, 0 if unknown");
00091 setData(source, "Volume", "float - the volume, given as a float\n"
00092 " between 0 and 1, or -1 if unknown");
00093 setData(source, "Artwork", "QPixmap - the album artwork, if available");
00094 return true;
00095 } else if (lowerSource == "players") {
00096 setData(source, sources());
00097 return true;
00098 } else if (players.contains(source)) {
00099 return updateSourceEvent(source);
00100 } else {
00101 kWarning() << "Player" << source << "not found";
00102 }
00103
00104 return false;
00105 }
00106
00107 QStringList NowPlayingEngine::sources() const
00108 {
00109 return players.keys();
00110 }
00111
00112 bool NowPlayingEngine::updateSourceEvent(const QString& source)
00113 {
00114 QString lowerSource = source.toLower();
00115 if (lowerSource == "help" || lowerSource == "properties") {
00116
00117 return true;
00118 }
00119
00120 if (!players.contains(source)) {
00121 kDebug() << "Can't find source" << source;
00122 removeAllData(source);
00123 return false;
00124 }
00125 Player::Ptr player = players[source];
00126 Q_ASSERT(player);
00127
00128 if (!player->isRunning()) {
00129 kDebug() << source << "isn't running";
00130 removePlayer(player);
00131 return false;
00132 }
00133
00134 switch(player->state()) {
00135 case Player::Playing:
00136 setData(source, "State", "playing");
00137 break;
00138 case Player::Paused:
00139 setData(source, "State", "paused");
00140 break;
00141 case Player::Stopped:
00142 setData(source, "State", "stopped");
00143 break;
00144 }
00145
00146 setData(source, "Artist", player->artist());
00147 setData(source, "Album", player->album());
00148 setData(source, "Title", player->title());
00149 setData(source, "Track number", player->trackNumber());
00150 setData(source, "Comment", player->comment());
00151 setData(source, "Genre", player->genre());
00152 setData(source, "Length", player->length());
00153 setData(source, "Position", player->position());
00154 setData(source, "Volume", player->volume());
00155 setData(source, "Artwork", player->artwork());
00156
00157 return true;
00158 }
00159
00160 void NowPlayingEngine::addPlayer(Player::Ptr player)
00161 {
00162 kDebug() << "Adding player" << player->name();
00163 players.insert(player->name(), player);
00164 emit sourceAdded(player->name());
00165 }
00166
00167 void NowPlayingEngine::removePlayer(Player::Ptr player)
00168 {
00169 kDebug() << "Player" << player->name() << "disappeared";
00170 if (players.contains(player->name())) {
00171 Q_ASSERT(player == players[player->name()]);
00172
00173 players.remove(player->name());
00174 removeSource(player->name());
00175 } else {
00176 kDebug() << "We didn't know about player" << player->name();
00177 }
00178 }
00179
00180 #include "nowplayingengine.moc"