1 /*
2 * Cantata
3 *
4 * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5 *
6 * ----
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include "mpris.h"
25 #include "mpd-interface/mpdconnection.h"
26 #include "playeradaptor.h"
27 #include "rootadaptor.h"
28 #include "config.h"
29 #include "gui/currentcover.h"
30
convertTime(qlonglong t)31 static inline qlonglong convertTime(qlonglong t)
32 {
33 return t*1000000;
34 }
35
Mpris(QObject * p)36 Mpris::Mpris(QObject *p)
37 : QObject(p)
38 , pos(-1)
39 {
40 QDBusConnection::sessionBus().registerService("org.mpris.MediaPlayer2.cantata");
41
42 new PlayerAdaptor(this);
43 new MediaPlayer2Adaptor(this);
44
45 QDBusConnection::sessionBus().registerObject("/org/mpris/MediaPlayer2", this, QDBusConnection::ExportAdaptors);
46 connect(this, SIGNAL(setRandom(bool)), MPDConnection::self(), SLOT(setRandom(bool)));
47 connect(this, SIGNAL(setRepeat(bool)), MPDConnection::self(), SLOT(setRepeat(bool)));
48 connect(this, SIGNAL(setSingle(bool)), MPDConnection::self(), SLOT(setSingle(bool)));
49 connect(this, SIGNAL(setSeekId(qint32, quint32)), MPDConnection::self(), SLOT(setSeekId(qint32, quint32)));
50 connect(this, SIGNAL(seek(qint32)), MPDConnection::self(), SLOT(seek(qint32)));
51 connect(this, SIGNAL(setVolume(int)), MPDConnection::self(), SLOT(setVolume(int)));
52
53 // connect(MPDConnection::self(), SIGNAL(currentSongUpdated(const Song &)), this, SLOT(updateCurrentSong(const Song &)));
54 // connect(MPDStatus::self(), SIGNAL(updated()), this, SLOT(updateStatus()));
55 connect(CurrentCover::self(), SIGNAL(coverFile(const QString &)), this, SLOT(updateCurrentCover(const QString &)));
56 }
57
~Mpris()58 Mpris::~Mpris()
59 {
60 QDBusConnection::sessionBus().unregisterService("org.mpris.MediaPlayer2.cantata");
61 }
62
Pause()63 void Mpris::Pause()
64 {
65 if (!status.isNull() && MPDState_Playing==status->state()) {
66 StdActions::self()->playPauseTrackAction->trigger();
67 }
68 }
69
Play()70 void Mpris::Play()
71 {
72 if (!status.isNull() && status->playlistLength()>0 && MPDState_Playing!=status->state()) {
73 StdActions::self()->playPauseTrackAction->trigger();
74 }
75 }
76
SetPosition(const QDBusObjectPath & trackId,qlonglong pos)77 void Mpris::SetPosition(const QDBusObjectPath &trackId, qlonglong pos)
78 {
79 if (trackId.path()==currentTrackId()) {
80 emit setSeekId(-1, pos/1000000);
81 }
82 }
83
PlaybackStatus() const84 QString Mpris::PlaybackStatus() const
85 {
86 if (status.isNull()) {
87 return QLatin1String("Stopped");
88 }
89
90 switch (status->state()) {
91 case MPDState_Playing: return QLatin1String("Playing");
92 case MPDState_Paused: return QLatin1String("Paused");
93 default:
94 case MPDState_Stopped: return QLatin1String("Stopped");
95 }
96 }
97
SetLoopStatus(const QString & s)98 void Mpris::SetLoopStatus(const QString &s)
99 {
100 if (status.isNull()) {
101 return;
102 }
103
104 bool repeat=(QLatin1String("None")!=s);
105 bool single=(QLatin1String("Track")==s);
106
107 if (status->repeat()!=repeat) {
108 emit setRepeat(repeat);
109 }
110 if (status->single()!=single) {
111 emit setSingle(single);
112 }
113 }
114
Position() const115 qlonglong Mpris::Position() const
116 {
117 // Cant use MPDStatus, as we dont poll for track position, but use a timer instead!
118 //return MPDStatus::self()->timeElapsed();
119 return status.isNull() ? 0 : convertTime(status->guessedElapsed());
120 }
121
updateStatus()122 void Mpris::updateStatus()
123 {
124 updateStatus(MPDStatus::self());
125 }
126
updateStatus(MPDStatus * const status)127 void Mpris::updateStatus(MPDStatus * const status)
128 {
129 QVariantMap map;
130
131 // If the current song has not yet been updated, reject this status
132 // update and wait for the next unless the play queue has recently
133 // been emptied or the connection to MPD has been lost ...
134 if (status->songId()!=currentSong.id && status->songId()!=-1) {
135 return;
136 }
137
138 if (status!=this->status) {
139 this->status = status;
140 }
141 if (status->repeat()!=lastStatus.repeat || status->single()!=lastStatus.single) {
142 map.insert("LoopStatus", LoopStatus());
143 }
144 if (status->random()!=lastStatus.random) {
145 map.insert("Shuffle", Shuffle());
146 }
147 if (status->volume()!=lastStatus.volume) {
148 map.insert("Volume", Volume());
149 }
150 if (status->state()!=lastStatus.state || status->songId()!=lastStatus.songId || status->nextSongId()!=lastStatus.nextSongId || status->playlistLength()!=lastStatus.playlistLength) {
151 map.insert("CanGoNext", CanGoNext());
152 map.insert("CanGoPrevious", CanGoPrevious());
153 }
154 if (status->state()!=lastStatus.state) {
155 map.insert("PlaybackStatus", PlaybackStatus());
156 map.insert("CanPause", CanPause());
157 }
158 if (status->playlistLength()!=lastStatus.playlistLength) {
159 map.insert("CanPlay", CanPlay());
160 }
161 if (status->songId()!=lastStatus.songId || status->timeTotal()!=lastStatus.timeTotal) {
162 map.insert("CanSeek", CanSeek());
163 }
164 if (status->timeElapsed()!=lastStatus.timeElapsed) {
165 map.insert("Position", convertTime(status->timeElapsed()));
166 }
167 if (!map.isEmpty() || status->songId()!=lastStatus.songId) {
168 if (!map.contains("Position")) {
169 map.insert("Position", convertTime(status->timeElapsed()));
170 }
171 map.insert("Metadata", Metadata());
172 signalUpdate(map);
173 }
174
175 lastStatus = status->getValues();
176 }
177
updateCurrentCover(const QString & fileName)178 void Mpris::updateCurrentCover(const QString &fileName)
179 {
180 if (fileName!=currentCover) {
181 currentCover=fileName;
182 signalUpdate("Metadata", Metadata());
183 }
184 }
185
updateCurrentSong(const Song & song)186 void Mpris::updateCurrentSong(const Song &song)
187 {
188 qint32 lastSongId = currentSong.id;
189 currentSong = song;
190
191 if (song.id!=lastSongId && (song.id==lastStatus.songId || -1==lastStatus.songId)) {
192 // The update of the current song may come a little late.
193 // So reset song ID and update status once again.
194 if (-1!=lastStatus.songId) {
195 lastStatus.songId = lastSongId;
196 }
197 updateStatus();
198 } else {
199 signalUpdate("Metadata", Metadata());
200 }
201 }
202
Metadata() const203 QVariantMap Mpris::Metadata() const {
204 QVariantMap metadataMap;
205 if ((!currentSong.title.isEmpty() && !currentSong.artist.isEmpty()) || (currentSong.isStandardStream() && !currentSong.name().isEmpty())) {
206 metadataMap.insert("mpris:trackid", currentTrackId());
207 QString artist=currentSong.artist;
208 QString album=currentSong.album;
209 QString title=currentSong.title;
210
211 if (currentSong.isStandardStream()) {
212 if (artist.isEmpty()) {
213 artist=currentSong.name();
214 } else if (album.isEmpty()) {
215 album=currentSong.name();
216 }
217 if (title.isEmpty()) {
218 title=tr("(Stream)");
219 }
220 }
221 if (currentSong.time>0) {
222 metadataMap.insert("mpris:length", convertTime(currentSong.time));
223 }
224 if (!album.isEmpty()) {
225 metadataMap.insert("xesam:album", album);
226 }
227 if (!currentSong.albumartist.isEmpty() && currentSong.albumartist!=currentSong.artist) {
228 metadataMap.insert("xesam:albumArtist", QStringList() << currentSong.albumartist);
229 }
230 if (!artist.isEmpty()) {
231 metadataMap.insert("xesam:artist", QStringList() << artist);
232 }
233 if (!title.isEmpty()) {
234 metadataMap.insert("xesam:title", title);
235 }
236 if (!currentSong.genres[0].isEmpty()) {
237 metadataMap.insert("xesam:genre", QStringList() << currentSong.genres[0]);
238 }
239 if (currentSong.track>0) {
240 metadataMap.insert("xesam:trackNumber", currentSong.track);
241 }
242 if (currentSong.disc>0) {
243 metadataMap.insert("xesam:discNumber", currentSong.disc);
244 }
245 if (currentSong.year>0) {
246 metadataMap.insert("xesam:contentCreated", QString("%04d").arg(currentSong.year));
247 }
248 if (!currentSong.file.isEmpty()) {
249 if (currentSong.isNonMPD()) {
250 metadataMap.insert("xesam:url", currentSong.file);
251 } else if (MPDConnection::self()->getDetails().dirReadable) {
252 QString mpdDir=MPDConnection::self()->getDetails().dir;
253 if (!mpdDir.isEmpty()) {
254 metadataMap.insert("xesam:url", "file://"+mpdDir+currentSong.file);
255 }
256 }
257 }
258 if (!currentCover.isEmpty()) {
259 metadataMap.insert("mpris:artUrl", "file://"+currentCover);
260 }
261 }
262
263 return metadataMap;
264 }
265
Raise()266 void Mpris::Raise()
267 {
268 emit showMainWindow();
269 }
270
signalUpdate(const QString & property,const QVariant & value)271 void Mpris::signalUpdate(const QString &property, const QVariant &value)
272 {
273 QVariantMap map;
274 map.insert(property, value);
275 signalUpdate(map);
276 }
277
signalUpdate(const QVariantMap & map)278 void Mpris::signalUpdate(const QVariantMap &map)
279 {
280 if (map.isEmpty()) {
281 return;
282 }
283 QDBusMessage signal = QDBusMessage::createSignal("/org/mpris/MediaPlayer2",
284 "org.freedesktop.DBus.Properties",
285 "PropertiesChanged");
286 QVariantList args = QVariantList()
287 << "org.mpris.MediaPlayer2.Player"
288 << map
289 << QStringList();
290 signal.setArguments(args);
291 QDBusConnection::sessionBus().send(signal);
292 }
293
currentTrackId() const294 QString Mpris::currentTrackId() const
295 {
296 return QString("/org/mpris/MediaPlayer2/Track/%1").arg(QString::number(currentSong.id));
297 }
298
299 #include "moc_mpris.cpp"
300