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
|
#include <QtCore/QtDebug>
#include <QtCore/QDir>
#include <QtCore/QCryptographicHash>
#include <MafwRenderer.h>
#include <MafwMediaInfo.h>
#include <MafwMetadata.h>
#include "qmafwwatchlet.h"
#include "qmafwwatchletplayer.h"
using namespace sowatch;
QMafwWatchletPlayer::QMafwWatchletPlayer(QMafwWatchlet* watchlet) :
QObject(watchlet),
_active(false),
_renderer(0),
_state(MafwRenderer::Stopped),
_title(),
_album(),
_artist(),
_rendererArt(),
_mediaArt()
{
Q_ASSERT(watchlet);
}
QString QMafwWatchletPlayer::title() const
{
return _title;
}
QString QMafwWatchletPlayer::album() const
{
return _album;
}
QString QMafwWatchletPlayer::artist() const
{
return _artist;
}
QUrl QMafwWatchletPlayer::mediaArt() const
{
return _mediaArt;
}
void QMafwWatchletPlayer::activate()
{
_active = true;
reconnect();
}
void QMafwWatchletPlayer::deactivate()
{
_active = false;
reconnect();
}
void QMafwWatchletPlayer::playPause()
{
if (!_renderer) return;
if (_state == MafwRenderer::Playing) {
_renderer->pause();
} else if (_state == MafwRenderer::Paused) {
_renderer->resume();
} else {
_renderer->play();
}
}
void QMafwWatchletPlayer::play()
{
if (!_renderer) return;
_renderer->play();
}
void QMafwWatchletPlayer::pause()
{
if (!_renderer) return;
_renderer->pause();
}
void QMafwWatchletPlayer::stop()
{
if (!_renderer) return;
_renderer->stop();
}
void QMafwWatchletPlayer::next()
{
if (!_renderer) return;
_renderer->next();
}
void QMafwWatchletPlayer::previous()
{
if (!_renderer) return;
_renderer->previous();
}
void QMafwWatchletPlayer::volumeUp()
{
if (!_renderer) return;
QString prop("volume");
_renderer->mafwProperty(prop, qobject_cast<QObject*>(this), SLOT(doVolumeUp(QString,QVariant)));
}
void QMafwWatchletPlayer::volumeDown()
{
if (!_renderer) return;
QString prop("volume");
_renderer->mafwProperty(prop, qobject_cast<QObject*>(this), SLOT(doVolumeDown(QString,QVariant)));
}
void QMafwWatchletPlayer::setRenderer(MafwRenderer * renderer)
{
if (_renderer && _active) {
disconnect(_renderer, 0, this, 0);
_title.clear();
_album.clear();
_artist.clear();
handleMediaChange();
emit titleChanged();
updateMediaArt();
}
_renderer = renderer;
reconnect();
}
void QMafwWatchletPlayer::reconnect()
{
if (_renderer && _active) {
connect(_renderer, SIGNAL(mediaChanged(int, const MafwContent&)),
this, SLOT(handleMediaChange()));
connect(_renderer, SIGNAL(metadataChanged(const QString&, const QList<QVariant>&)),
this, SLOT(handleChangedMetadata(const QString&, const QList<QVariant>&)));
connect(_renderer, SIGNAL(stateChanged(MafwRenderer::State)),
this, SLOT(handleChangedState(MafwRenderer::State)));
_renderer->getStatus(this, SLOT(handleStatusResults(QString,uint,int)));
_renderer->getCurrentMediaInfo(this, SLOT(handleMediaInfo(MafwMediaInfo)));
} else if (_renderer) {
disconnect(_renderer, 0, this, 0);
}
}
QString QMafwWatchletPlayer::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 QMafwWatchletPlayer::mediaArtPath() const
{
QDir dir(QDir::homePath() + "/.cache/media-art/");
QString album = stripAlbumArtComponent(_album);
QString artist = stripAlbumArtComponent(_artist);
QByteArray first_hash = QCryptographicHash::hash(artist.toUtf8(), QCryptographicHash::Md5).toHex();
QByteArray second_hash = QCryptographicHash::hash(album.toUtf8(), QCryptographicHash::Md5).toHex();
QString file = QString("album-%1-%2.jpeg").arg(first_hash.constData()).arg(second_hash.constData());
qDebug() << "checking for albumart in" << file;
if (dir.exists(file)) {
return dir.absoluteFilePath(file);
}
artist = " ";
first_hash = QCryptographicHash::hash(artist.toUtf8(), QCryptographicHash::Md5).toHex();
file = QString("album-%1-%2.jpeg").arg(first_hash.constData()).arg(second_hash.constData());
qDebug() << "checking for albumart in" << file;
if (dir.exists(file)) {
return dir.absoluteFilePath(file);
}
return QString();
}
void QMafwWatchletPlayer::updateMediaArt()
{
QUrl newArt;
if (!_rendererArt.isEmpty()) {
newArt = QUrl::fromLocalFile(_rendererArt);
} else {
QString path = mediaArtPath();
if (!path.isEmpty()) {
newArt = QUrl::fromLocalFile(path);
}
}
if (newArt != _mediaArt) {
_mediaArt = newArt;
emit mediaArtChanged();
}
}
void QMafwWatchletPlayer::handleMediaChange()
{
// Sometimes qmafw does not send the renderer-art metadata, so we assume it's been cleared here.
_rendererArt.clear();
}
void QMafwWatchletPlayer::handleChangedMetadata(const QString &s, const QList<QVariant> &l)
{
if (s == MAFW_METADATA_KEY_TITLE) {
if (!l.isEmpty()) {
_title = l[0].toString();
} else {
_title.clear();
}
emit titleChanged();
} else if (s == MAFW_METADATA_KEY_ALBUM) {
if (!l.isEmpty()) {
_album = l[0].toString();
} else {
_album.clear();
}
emit albumChanged();
updateMediaArt();
} else if (s == MAFW_METADATA_KEY_ARTIST) {
if (!l.isEmpty()) {
_artist = l[0].toString();
} else {
_artist.clear();
}
emit artistChanged();
updateMediaArt();
} else if (s == MAFW_METADATA_KEY_RENDERER_ART_URI) {
if (!l.isEmpty()) {
_rendererArt = l[0].toString();
} else {
_rendererArt.clear();
}
updateMediaArt();
}
}
void QMafwWatchletPlayer::handleChangedState(MafwRenderer::State state)
{
_state = state;
}
void QMafwWatchletPlayer::handleMediaInfo(const MafwMediaInfo &info)
{
const QMap<QString, QList<QVariant> > & data = info.metaData();
handleChangedMetadata(MAFW_METADATA_KEY_TITLE, data[MAFW_METADATA_KEY_TITLE]);
handleChangedMetadata(MAFW_METADATA_KEY_ALBUM, data[MAFW_METADATA_KEY_ALBUM]);
handleChangedMetadata(MAFW_METADATA_KEY_ARTIST, data[MAFW_METADATA_KEY_ARTIST]);
handleChangedMetadata(MAFW_METADATA_KEY_RENDERER_ART_URI, data[MAFW_METADATA_KEY_RENDERER_ART_URI]);
}
void QMafwWatchletPlayer::handleStatusResults(const QString &playlistId, uint playbackIndex, int playbackState)
{
Q_UNUSED(playlistId);
Q_UNUSED(playbackIndex);
_state = static_cast<MafwRenderer::State>(playbackState);
}
void QMafwWatchletPlayer::doVolumeUp(const QString& name, const QVariant& value)
{
_renderer->setMafwProperty(name, value.toUInt() + 10);
}
void QMafwWatchletPlayer::doVolumeDown(const QString& name, const QVariant& value)
{
_renderer->setMafwProperty(name, value.toUInt() - 10);
}
|