1
/* This file is part of Clementine.
2
Copyright 2011, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "groovesharkurlhandler.h"
22
#include "groovesharkservice.h"
23
#include "core/logging.h"
26
GroovesharkUrlHandler::GroovesharkUrlHandler(GroovesharkService* service, QObject* parent)
29
timer_mark_stream_key_(new QTimer(this)) {
30
// We have to warn Grooveshark when user has listened for more than 30
31
// seconds of a song, and when it ends. I guess this is used by Grooveshark
32
// for statistics and user history.
33
// To do this, we have TrackAboutToEnd method, and timer_mark_stream_key_ timer.
34
// It is not perfect, as we may call Grooveshark MarkStreamKeyOver30Secs even
35
// if user hasn't actually listen to 30 seconds (e.g. stream set to pause
36
// state) but this is not a big deal and it should be accurate enough anyway.
37
timer_mark_stream_key_->setInterval(30000);
38
timer_mark_stream_key_->setSingleShot(true);
39
connect(timer_mark_stream_key_, SIGNAL(timeout()), SLOT(MarkStreamKeyOver30Secs()));
42
UrlHandler::LoadResult GroovesharkUrlHandler::StartLoading(const QUrl& url) {
43
qint64 length_nanosec = 0;
45
QStringList ids = url.toString().remove("grooveshark://").split("/");
47
qLog(Error) << "Invalid grooveshark URL: " << url.toString();
48
qLog(Error) << "Should be grooveshark://artist_id/album_id/song_id";
50
last_artist_id_ = ids[0];
51
last_album_id_ = ids[1];
52
last_song_id_ = ids[2];
54
streaming_url = service_->GetStreamingUrlFromSongId(last_song_id_, last_artist_id_,
55
&last_server_id_, &last_stream_key_, &length_nanosec);
56
qLog(Debug) << "Grooveshark Streaming URL: " << streaming_url;
58
timer_mark_stream_key_->start();
61
return LoadResult(url, LoadResult::TrackAvailable, streaming_url, length_nanosec);
64
void GroovesharkUrlHandler::TrackAboutToEnd() {
65
if (timer_mark_stream_key_->isActive()) {
66
timer_mark_stream_key_->stop();
69
service_->MarkSongComplete(last_song_id_, last_stream_key_, last_server_id_);
72
void GroovesharkUrlHandler::TrackSkipped() {
73
timer_mark_stream_key_->stop();
76
void GroovesharkUrlHandler::MarkStreamKeyOver30Secs() {
77
service_->MarkStreamKeyOver30Secs(last_stream_key_, last_server_id_);