summaryrefslogtreecommitdiff
path: root/src/mpris2/mediaplayer2player.cpp
blob: 9a87982cc45d719404f23ccac49ea879ca14b065 (plain)
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
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2018 Ricardo Villalba <rvm@users.sourceforge.net>

    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 2 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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


/***********************************************************************
 * Copyright 2012  Eike Hein <hein@kde.org>
 * Copyright 2012  Bernd Buschinski <b.buschinski@googlemail.com>
 *
 * 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 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * 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 "mediaplayer2player.h"
#include "mpris2.h"
#include "basegui.h"
#include "core.h"
#include "playlist.h"

#include <QCryptographicHash>

static QByteArray makeTrackId(const QString& source) {
    return QByteArray("/org/smplayer/") + "smplayer" + "/tid_" +
        QCryptographicHash::hash(source.toLocal8Bit(), QCryptographicHash::Sha1)
            .toHex();
}

MediaPlayer2Player::MediaPlayer2Player(BaseGui * gui, QObject* parent)
    : QDBusAbstractAdaptor(parent),
      m_core(gui->getCore()),
      m_playlist(gui->getPlaylist())
{
	//connect(m_core, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
	//connect(m_core, SIGNAL(seekableChanged(bool)), this, SLOT(seekableChanged(bool)));
	connect(m_core, SIGNAL(mediaPlaying(QString,QString)), this, SLOT(currentSourceChanged()));
	connect(m_core, SIGNAL(stateChanged(Core::State)), this, SLOT(stateUpdated()));
	connect(m_core, SIGNAL(mediaInfoChanged()), this, SLOT(emitMetadataChange()));
	connect(m_core, SIGNAL(volumeChanged(int)), this, SLOT(volumeChanged()));
}

MediaPlayer2Player::~MediaPlayer2Player() {
}

bool MediaPlayer2Player::CanGoNext() const {
	return true;
}

void MediaPlayer2Player::Next() const {
	qDebug("MediaPlayer2Player::Next");
	m_playlist->playNext();
}

bool MediaPlayer2Player::CanGoPrevious() const {
	return true;
}

void MediaPlayer2Player::Previous() const {
	qDebug("MediaPlayer2Player::Previous");
    m_playlist->playPrev();
}

bool MediaPlayer2Player::CanPause() const {
	return true;
}

void MediaPlayer2Player::Pause() const {
	qDebug("MediaPlayer2Player::Pause");
	m_core->pause();
}

void MediaPlayer2Player::PlayPause() const {
	qDebug("MediaPlayer2Player::PlayPause");
	m_core->play_or_pause();
}

void MediaPlayer2Player::Stop() const {
	qDebug("MediaPlayer2Player::Stop");
	m_core->stop();
}

bool MediaPlayer2Player::CanPlay() const {
	return true;
}

void MediaPlayer2Player::Play() const {
	qDebug("MediaPlayer2Player::Play");
	m_core->play();
}

void MediaPlayer2Player::SetPosition(const QDBusObjectPath& TrackId, qlonglong Position) const {
	double secs = Position / 1000000;
	qDebug() << "MediaPlayer2Player::SetPosition: TrackId:" << TrackId.path() << "Position:" << Position << "(secs:" << secs << ")";
	if (TrackId.path().toLocal8Bit() == makeTrackId(m_core->mdat.filename)) {
		m_core->goToSec(secs);
	}
}

void MediaPlayer2Player::OpenUri(QString uri) const {
	qDebug() << "MediaPlayer2Player::OpenUri:" << uri;
	m_core->open(uri);
}

QString MediaPlayer2Player::PlaybackStatus() const {
	return m_core->stateToString();
}

QString MediaPlayer2Player::LoopStatus() const {
	return "None";
}

void MediaPlayer2Player::setLoopStatus(const QString& loopStatus) const {
	Q_UNUSED(loopStatus)
}

double MediaPlayer2Player::Rate() const {
	return m_core->mset.speed;
}

void MediaPlayer2Player::setRate(double rate) const {
	qDebug() << "MediaPlayer2Player::setRate:" << rate;
	m_core->setSpeed(rate);
}

bool MediaPlayer2Player::Shuffle() const {
	return false;
}

void MediaPlayer2Player::setShuffle(bool shuffle) const {
	Q_UNUSED(shuffle)
}

QVariantMap MediaPlayer2Player::Metadata() const {
	QVariantMap metaData;

	if (!m_core->mdat.initialized) {
		return metaData;
	}

	metaData["mpris:trackid"] = QVariant::fromValue<QDBusObjectPath>(QDBusObjectPath(makeTrackId(m_core->mdat.filename).constData()));
	metaData["mpris:length"] = m_core->mdat.duration * 1000000;

	if (m_core->mdat.type == TYPE_STREAM)
		metaData["xesam:url"] = m_core->mdat.stream_url;
	else
		metaData["xesam:url"] = m_core->mdat.filename;

	if (!m_core->mdat.clip_album.isEmpty())
		metaData["xesam:album"] = m_core->mdat.clip_album;
	if (!m_core->mdat.clip_name.isEmpty())
		metaData["xesam:title"] = m_core->mdat.clip_name;
	else if (!m_core->mdat.filename.isEmpty()) {
		QFileInfo fileInfo(m_core->mdat.filename);
		metaData["xesam:title"] = fileInfo.fileName();
	}
	if (!m_core->mdat.clip_artist.isEmpty())
		metaData["xesam:artist"] = m_core->mdat.clip_artist;
	if (!m_core->mdat.clip_genre.isEmpty())
		metaData["xesam:genre"] = m_core->mdat.clip_genre;

	return metaData;
}

double MediaPlayer2Player::Volume() const {
	return static_cast<double>(m_core->mset.volume / 100.0);
}

void MediaPlayer2Player::setVolume(double volume) const {
	qDebug() << "MediaPlayer2Player::setVolume:" << volume;
	m_core->setVolume(static_cast<int>(volume*100));
}

qlonglong MediaPlayer2Player::Position() const {
	return static_cast<qlonglong>(m_core->mset.current_sec * 1000000);
}

double MediaPlayer2Player::MinimumRate() const {
	return 0.01;
}

double MediaPlayer2Player::MaximumRate() const {
	return 100.0;
}

bool MediaPlayer2Player::CanSeek() const {
	return true;
}

void MediaPlayer2Player::Seek(qlonglong Offset) const {
	int secs = Offset/1000000;
	qDebug() << "MediaPlayer2Player::Seek:" << Offset << "(secs" << secs << ")";
	m_core->seek(secs);
}

bool MediaPlayer2Player::CanControl() const {
	return true;
}

void MediaPlayer2Player::tick(qint64 newPos) {
	/*
	if (newPos - oldPos > tickInterval + 250 || newPos < oldPos) {
		emit Seeked(newPos * 1000);
	}
	*/

	oldPos = newPos;
}

void MediaPlayer2Player::emitMetadataChange() const {
	QVariantMap properties;
	properties["Metadata"] = Metadata();
	Mpris2::signalPropertiesChange(this, properties);
}

void MediaPlayer2Player::currentSourceChanged() const {
	QVariantMap properties;
	properties["Metadata"] = Metadata();
	properties["CanSeek"] = CanSeek();
	Mpris2::signalPropertiesChange(this, properties);
}

void MediaPlayer2Player::stateUpdated() const {
	QVariantMap properties;
	properties["PlaybackStatus"] = PlaybackStatus();
	properties["CanPause"] = CanPause();
	Mpris2::signalPropertiesChange(this, properties);
}

void MediaPlayer2Player::totalTimeChanged() const {
	QVariantMap properties;
	properties["Metadata"] = Metadata();
	Mpris2::signalPropertiesChange(this, properties);
}

void MediaPlayer2Player::seekableChanged(bool seekable) const {
	QVariantMap properties;
	properties["CanSeek"] = seekable;
	Mpris2::signalPropertiesChange(this, properties);
}

void MediaPlayer2Player::volumeChanged() const {
	QVariantMap properties;
	properties["Volume"] = Volume();
	Mpris2::signalPropertiesChange(this, properties);
}

#include "moc_mediaplayer2player.cpp"