-
Notifications
You must be signed in to change notification settings - Fork 6
/
musiccontroller.cpp
399 lines (342 loc) · 10.6 KB
/
musiccontroller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* libwatchfish - library with common functionality for SailfishOS smartwatch connector programs.
* Copyright (C) 2015 Javier S. Pedro <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtCore/QDir>
#include <QtCore/QCryptographicHash>
#include <QDBusMessage>
#include <QDBusReply>
#include "musiccontroller.h"
#include "musiccontroller_p.h"
namespace watchfish
{
Q_LOGGING_CATEGORY(musicControllerCat, "watchfish-MusicController")
MusicControllerPrivate::MusicControllerPrivate(MusicController *q)
: manager(new MprisManager(this)), q_ptr(q)
{
connect(manager, &MprisManager::currentServiceChanged,
this, &MusicControllerPrivate::handleCurrentServiceChanged);
connect(manager, &MprisManager::playbackStatusChanged,
this, &MusicControllerPrivate::handlePlaybackStatusChanged);
connect(manager, &MprisManager::metadataChanged,
this, &MusicControllerPrivate::handleMetadataChanged);
connect(manager, &MprisManager::shuffleChanged,
q, &MusicController::shuffleChanged);
connect(manager, &MprisManager::loopStatusChanged,
q, &MusicController::repeatChanged);
}
MusicControllerPrivate::~MusicControllerPrivate()
{
if (_pulseBus != NULL) {
qDebug() << "Disconnecting from PulseAudio P2P DBus";
QDBusConnection::disconnectFromBus("org.PulseAudio1");
delete(_pulseBus);
}
}
QString MusicControllerPrivate::stripAlbumArtComponent(const QString& component)
{
static QRegExp rsb("\\[.*\\]");
static QRegExp rfb("{.*}");
static QRegExp rrb("\\(.*\\)");
static QRegExp stripB("^[()_{}[]!@#$^&*+=|\\\\/\"'?<>~`\\s\\t]*");
static QRegExp stripE("[()_{}[]!@#$^&*+=|\\\\/\"'?<>~`\\s\\t]*$");
QString s(component);
if (s.isEmpty()) {
return QString(" ");
}
s = s.replace(rsb, "");
s = s.replace(rfb, "");
s = s.replace(rrb, "");
s = s.replace(stripB, "");
s = s.replace(stripE, "");
s = s.replace(" ", " ");
s = s.replace("\t", " ");
return s.toLower();
}
QString MusicControllerPrivate::findAlbumArt(const QString &artist, const QString &album)
{
QDir dir(QDir::homePath() + "/.cache/media-art/");
QByteArray first_hash = QCryptographicHash::hash(stripAlbumArtComponent(artist).toUtf8(),
QCryptographicHash::Md5).toHex();
QByteArray second_hash = QCryptographicHash::hash(stripAlbumArtComponent(album).toUtf8(),
QCryptographicHash::Md5).toHex();
QString file = QString("album-%1-%2.jpeg").arg(first_hash.constData(), second_hash.constData());
qCDebug(musicControllerCat()) << "checking for albumart in" << file;
if (dir.exists(file)) {
return dir.absoluteFilePath(file);
}
// Now try with an empty artist name
first_hash = QCryptographicHash::hash(QString(" ").toUtf8(), QCryptographicHash::Md5).toHex();
file = QString("album-%1-%2.jpeg").arg(first_hash.constData(), second_hash.constData());
qCDebug(musicControllerCat()) << "checking for albumart in" << file;
if (dir.exists(file)) {
return dir.absoluteFilePath(file);
}
return QString();
}
void MusicControllerPrivate::updateStatus()
{
Q_Q(MusicController);
QString service = manager->currentService();
MusicController::Status newStatus;
if (service.isEmpty()) {
newStatus = MusicController::StatusNoPlayer;
} else {
switch (manager->playbackStatus()) {
case Mpris::Playing:
newStatus = MusicController::StatusPlaying;
break;
case Mpris::Paused:
newStatus = MusicController::StatusPaused;
break;
default:
newStatus = MusicController::StatusStopped;
break;
}
}
if (newStatus != curStatus) {
curStatus = newStatus;
emit q->statusChanged();
}
}
void MusicControllerPrivate::updateAlbumArt()
{
Q_Q(MusicController);
QString newAlbumArt = findAlbumArt(curArtist, curAlbum);
if (newAlbumArt != curAlbumArt) {
curAlbumArt = newAlbumArt;
emit q->albumArtChanged();
}
}
void MusicControllerPrivate::updateMetadata()
{
Q_Q(MusicController);
QVariantMap metadata = manager->metadata();
bool checkAlbumArt = false;
qCDebug(musicControllerCat()) << metadata;
QString newArtist = metadata.value("xesam:artist").toString(),
newAlbum = metadata.value("xesam:album").toString(),
newTitle = metadata.value("xesam:title").toString();
if (newArtist != curArtist) {
curArtist = newArtist;
checkAlbumArt = true;
emit q->artistChanged();
}
if (newAlbum != curAlbum) {
curAlbum = newAlbum;
checkAlbumArt = true;
emit q->albumChanged();
}
if (newTitle != curTitle) {
curTitle = newTitle;
emit q->titleChanged();
}
if (checkAlbumArt) {
updateAlbumArt();
}
int newDuration = metadata.value("mpris:length").toULongLong() / 1000UL;
if (newDuration != curDuration) {
curDuration = newDuration;
emit q->durationChanged();
}
emit q->metadataChanged();
}
void MusicControllerPrivate::handleCurrentServiceChanged()
{
Q_Q(MusicController);
qCDebug(musicControllerCat()) << manager->currentService();
updateStatus();
emit q->serviceChanged();
}
void MusicControllerPrivate::handlePlaybackStatusChanged()
{
qCDebug(musicControllerCat()) << manager->playbackStatus();
updateStatus();
}
void MusicControllerPrivate::handleMetadataChanged()
{
updateMetadata();
}
MusicController::MusicController(QObject *parent)
: QObject(parent), d_ptr(new MusicControllerPrivate(this))
{
}
MusicController::~MusicController()
{
delete d_ptr;
}
void MusicControllerPrivate::connectPulseBus() {
if (_pulseBus) {
if (!_pulseBus->isConnected())
delete(_pulseBus);
else
return;
}
QDBusMessage call = QDBusMessage::createMethodCall("org.PulseAudio1", "/org/pulseaudio/server_lookup1", "org.freedesktop.DBus.Properties", "Get" );
call << "org.PulseAudio.ServerLookup1" << "Address";
QDBusReply<QDBusVariant> lookupReply = QDBusConnection::sessionBus().call(call);
if (lookupReply.isValid()) {
qDebug() << "PulseAudio Bus address: " << lookupReply.value().variant().toString();
_pulseBus = new QDBusConnection(QDBusConnection::connectToPeer(lookupReply.value().variant().toString(), "org.PulseAudio1"));
if (_maxVolume == 0) {
// Query max volume
call = QDBusMessage::createMethodCall("com.Meego.MainVolume2", "/com/meego/mainvolume2",
"org.freedesktop.DBus.Properties", "Get");
call << "com.Meego.MainVolume2" << "StepCount";
QDBusReply<QDBusVariant> volumeMaxReply = _pulseBus->call(call);
if (volumeMaxReply.isValid()) {
_maxVolume = volumeMaxReply.value().variant().toUInt();
qDebug() << "Max volume: " << _maxVolume;
}
else {
qWarning() << "Could not read volume max, cannot adjust volume: " << volumeMaxReply.error().message();
}
}
}
else
qDebug() << "Cannot connect to PulseAudio bus";
}
MusicController::Status MusicController::status() const
{
Q_D(const MusicController);
return d->curStatus;
}
QString MusicController::service() const
{
Q_D(const MusicController);
return d->manager->currentService();
}
QVariantMap MusicController::metadata() const
{
Q_D(const MusicController);
return d->manager->metadata();
}
QString MusicController::title() const
{
Q_D(const MusicController);
return d->curTitle;
}
QString MusicController::album() const
{
Q_D(const MusicController);
return d->curAlbum;
}
QString MusicController::artist() const
{
Q_D(const MusicController);
return d->curArtist;
}
QString MusicController::albumArt() const
{
Q_D(const MusicController);
return d->curAlbumArt;
}
int MusicController::duration() const
{
Q_D(const MusicController);
return d->curDuration;
}
MusicController::RepeatStatus MusicController::repeat() const
{
Q_D(const MusicController);
switch (d->manager->loopStatus()) {
case Mpris::None:
default:
return RepeatNone;
case Mpris::Track:
return RepeatTrack;
case Mpris::Playlist:
return RepeatPlaylist;
}
}
bool MusicController::shuffle() const
{
Q_D(const MusicController);
return d->manager->shuffle();
}
int MusicController::volume() const
{
uint volume = -1;
QDBusMessage call = QDBusMessage::createMethodCall("com.Meego.MainVolume2", "/com/meego/mainvolume2",
"org.freedesktop.DBus.Properties", "Get");
call << "com.Meego.MainVolume2" << "CurrentStep";
QDBusReply<QDBusVariant> volumeReply = d_ptr->_pulseBus->call(call);
if (volumeReply.isValid()) {
// Decide the new value for volume, taking limits into account
volume = volumeReply.value().variant().toUInt();
}
return volume;
}
void MusicController::play()
{
Q_D(MusicController);
d->manager->play();
}
void MusicController::pause()
{
Q_D(MusicController);
d->manager->pause();
}
void MusicController::playPause()
{
Q_D(MusicController);
d->manager->playPause();
}
void MusicController::next()
{
Q_D(MusicController);
d->manager->next();
}
void MusicController::previous()
{
Q_D(MusicController);
d->manager->previous();
}
void MusicController::setVolume(const uint newVolume)
{
qDebug() << "Setting volume: " << newVolume;
d_ptr->connectPulseBus();
QDBusMessage call = QDBusMessage::createMethodCall("com.Meego.MainVolume2", "/com/meego/mainvolume2",
"org.freedesktop.DBus.Properties", "Set");
call << "com.Meego.MainVolume2" << "CurrentStep" << QVariant::fromValue(QDBusVariant(newVolume));
QDBusError err = d_ptr->_pulseBus->call(call);
if (err.isValid()) {
qWarning() << err.message();
}
}
void MusicController::volumeUp()
{
d_ptr->connectPulseBus();
uint curVolume = this->volume();
uint newVolume = curVolume + 1;
if (newVolume >= d_ptr->_maxVolume) {
qDebug() << "Cannot increase volume beyond maximum " << d_ptr->_maxVolume;
return;
}
setVolume(newVolume);
}
void MusicController::volumeDown()
{
d_ptr->connectPulseBus();
uint curVolume = this->volume();
if (curVolume == 0) {
qDebug() << "Cannot decrease volume beyond 0";
return;
}
uint newVolume = curVolume - 1;
setVolume(newVolume);
}
}