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

« back to all changes in this revision

Viewing changes to src/radio/jamendodynamicplaylist.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
 
#include "jamendodynamicplaylist.h"
2
 
 
3
 
#include <QEventLoop>
4
 
#include <QHttp>
5
 
#include <QHttpRequestHeader>
6
 
#include <QtDebug>
7
 
 
8
 
#include "core/network.h"
9
 
#include "library/librarybackend.h"
10
 
#include "radio/jamendoplaylistitem.h"
11
 
#include "radio/jamendoservice.h"
12
 
 
13
 
const char* JamendoDynamicPlaylist::kUrl =
14
 
    "http://api.jamendo.com/get2/id/track/plain/";
15
 
 
16
 
JamendoDynamicPlaylist::JamendoDynamicPlaylist()
17
 
  : order_by_(OrderBy_Rating),
18
 
    order_direction_(Order_Descending),
19
 
    current_page_(0),
20
 
    current_index_(0) {
21
 
}
22
 
 
23
 
JamendoDynamicPlaylist::JamendoDynamicPlaylist(const QString& name, OrderBy order_by)
24
 
  : order_by_(order_by),
25
 
    order_direction_(Order_Descending),
26
 
    current_page_(0),
27
 
    current_index_(0) {
28
 
  set_name(name);
29
 
}
30
 
 
31
 
void JamendoDynamicPlaylist::Load(const QByteArray& data) {
32
 
  QDataStream s(data);
33
 
  s >> *this;
34
 
}
35
 
 
36
 
void JamendoDynamicPlaylist::Load(OrderBy order_by, OrderDirection order_direction) {
37
 
  order_by_ = order_by;
38
 
  order_direction_ = order_direction;
39
 
}
40
 
 
41
 
QByteArray JamendoDynamicPlaylist::Save() const {
42
 
  QByteArray ret;
43
 
  QDataStream s(&ret, QIODevice::WriteOnly);
44
 
  s << *this;
45
 
 
46
 
  return ret;
47
 
}
48
 
 
49
 
PlaylistItemList JamendoDynamicPlaylist::Generate() {
50
 
  return GenerateMore(20);
51
 
}
52
 
 
53
 
PlaylistItemList JamendoDynamicPlaylist::GenerateMore(int count) {
54
 
  int tries = 0;
55
 
 
56
 
  PlaylistItemList items;
57
 
  while (items.size() < count && tries++ < kApiRetryLimit) {
58
 
    // Add items from current list.
59
 
    if (current_index_ < current_items_.size()) {
60
 
      PlaylistItemList more_items = current_items_.mid(current_index_, count);
61
 
      items << more_items;
62
 
      current_index_ += more_items.size();
63
 
    } else {
64
 
      // We need more songs!
65
 
      Fetch();
66
 
    }
67
 
  }
68
 
 
69
 
  return items;
70
 
}
71
 
 
72
 
QString JamendoDynamicPlaylist::OrderSpec(OrderBy by, OrderDirection dir) {
73
 
  QString ret;
74
 
  switch (by) {
75
 
    case OrderBy_Listened:    ret += "listened";    break;
76
 
    case OrderBy_Rating:      ret += "rating";      break;
77
 
    case OrderBy_RatingMonth: ret += "ratingmonth"; break;
78
 
    case OrderBy_RatingWeek:  ret += "ratingweek";  break;
79
 
  }
80
 
  switch (dir) {
81
 
    case Order_Ascending:     ret += "_asc";        break;
82
 
    case Order_Descending:    ret += "_desc";       break;
83
 
  }
84
 
  return ret;
85
 
}
86
 
 
87
 
void JamendoDynamicPlaylist::Fetch() {
88
 
  QUrl url(kUrl);
89
 
  url.addQueryItem("pn", QString::number(current_page_++));
90
 
  url.addQueryItem("n", QString::number(kPageSize));
91
 
  url.addQueryItem("order", OrderSpec(order_by_, order_direction_));
92
 
 
93
 
  // We have to use QHttp here because there's no way to disable Keep-Alive
94
 
  // with QNetworkManager.
95
 
  QHttpRequestHeader header("GET", url.encodedPath() + "?" + url.encodedQuery());
96
 
  header.setValue("Host", url.encodedHost());
97
 
 
98
 
  QHttp http(url.host());
99
 
  http.request(header);
100
 
 
101
 
  // Wait for the reply
102
 
  {
103
 
    QEventLoop event_loop;
104
 
    connect(&http, SIGNAL(requestFinished(int,bool)), &event_loop, SLOT(quit()));
105
 
    event_loop.exec();
106
 
  }
107
 
 
108
 
  if (http.error() != QHttp::NoError) {
109
 
    qWarning() << "HTTP error returned from Jamendo:" << http.errorString()
110
 
               << ", url:" << url.toString();
111
 
    return;
112
 
  }
113
 
 
114
 
  // The reply will contain one track ID per line
115
 
  QStringList lines = QString::fromAscii(http.readAll()).split('\n');
116
 
 
117
 
  // Get the songs from the database
118
 
  SongList songs = backend_->GetSongsByForeignId(
119
 
        lines, JamendoService::kTrackIdsTable, JamendoService::kTrackIdsColumn);
120
 
 
121
 
  if (songs.empty()) {
122
 
    qWarning() << "No songs returned from Jamendo:"
123
 
               << url.toString();
124
 
    return;
125
 
  }
126
 
 
127
 
  current_items_.clear();
128
 
  foreach (const Song& song, songs) {
129
 
    if (song.is_valid())
130
 
      current_items_ << PlaylistItemPtr(new JamendoPlaylistItem(song));
131
 
  }
132
 
  current_index_ = 0;
133
 
}
134
 
 
135
 
QDataStream& operator <<(QDataStream& s, const JamendoDynamicPlaylist& p) {
136
 
  s << quint8(p.order_by_) << quint8(p.order_direction_);
137
 
  return s;
138
 
}
139
 
 
140
 
QDataStream& operator >>(QDataStream& s, JamendoDynamicPlaylist& p) {
141
 
  quint8 order_by, order_direction;
142
 
  s >> order_by >> order_direction;
143
 
  p.order_by_ = JamendoDynamicPlaylist::OrderBy(order_by);
144
 
  p.order_direction_ = JamendoDynamicPlaylist::OrderDirection(order_direction);
145
 
  return s;
146
 
}