~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/internet/groovesharkurlhandler.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2011, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "groovesharkurlhandler.h"
 
19
 
 
20
#include <QTimer>
 
21
 
 
22
#include "groovesharkservice.h"
 
23
#include "core/logging.h"
 
24
 
 
25
 
 
26
GroovesharkUrlHandler::GroovesharkUrlHandler(GroovesharkService* service, QObject* parent)
 
27
  : UrlHandler(parent),
 
28
    service_(service),
 
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()));
 
40
}
 
41
 
 
42
UrlHandler::LoadResult GroovesharkUrlHandler::StartLoading(const QUrl& url) {
 
43
  qint64 length_nanosec = 0;
 
44
  QUrl streaming_url;
 
45
  QStringList ids = url.toString().remove("grooveshark://").split("/");
 
46
  if (ids.size() < 3) {
 
47
    qLog(Error) << "Invalid grooveshark URL: " << url.toString();
 
48
    qLog(Error) << "Should be grooveshark://artist_id/album_id/song_id";
 
49
  } else {
 
50
    last_artist_id_ = ids[0];
 
51
    last_album_id_  = ids[1];
 
52
    last_song_id_   = ids[2];
 
53
 
 
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;
 
57
 
 
58
    timer_mark_stream_key_->start();
 
59
  }
 
60
 
 
61
  return LoadResult(url, LoadResult::TrackAvailable, streaming_url, length_nanosec);
 
62
}
 
63
 
 
64
void GroovesharkUrlHandler::TrackAboutToEnd() {
 
65
  if (timer_mark_stream_key_->isActive()) {
 
66
    timer_mark_stream_key_->stop();
 
67
    return;
 
68
  }
 
69
  service_->MarkSongComplete(last_song_id_, last_stream_key_, last_server_id_);
 
70
}
 
71
 
 
72
void GroovesharkUrlHandler::TrackSkipped() {
 
73
  timer_mark_stream_key_->stop();
 
74
}
 
75
 
 
76
void GroovesharkUrlHandler::MarkStreamKeyOver30Secs() {
 
77
  service_->MarkStreamKeyOver30Secs(last_stream_key_, last_server_id_);
 
78
}