Sort of working Jamendo dynamic playlist.

This commit is contained in:
John Maguire 2010-11-26 15:16:48 +00:00
parent 5387d4e933
commit eca1704384
8 changed files with 141 additions and 2 deletions

View File

@ -123,6 +123,7 @@ set(SOURCES
radio/icecastfilterwidget.cpp
radio/icecastmodel.cpp
radio/icecastservice.cpp
radio/jamendodynamicplaylist.cpp
radio/jamendoplaylistitem.cpp
radio/jamendoservice.cpp
radio/lastfmconfig.cpp
@ -292,6 +293,7 @@ set(HEADERS
radio/icecastfilterwidget.h
radio/icecastmodel.h
radio/icecastservice.h
radio/jamendodynamicplaylist.h
radio/jamendoservice.h
radio/lastfmconfig.h
radio/lastfmservice.h

View File

@ -297,7 +297,8 @@ void SongLoader::TypeFound(GstElement*, uint, GstCaps* caps, void* self) {
instance->mime_type_ = gst_structure_get_name(gst_caps_get_structure(caps, 0));
qDebug() << "Mime type is" << instance->mime_type_;
if (instance->mime_type_ == "text/plain" ||
instance->mime_type_ == "text/uri-list") {
instance->mime_type_ == "text/uri-list" ||
instance->mime_type_ == "application/xml") {
// Yeah it might be a playlist, let's get some data and have a better look
instance->state_ = WaitingForMagic;
return;

View File

@ -0,0 +1,84 @@
#include "jamendodynamicplaylist.h"
#include <QEventLoop>
#include "core/network.h"
#include "core/songloader.h"
#include "library/librarybackend.h"
#include "radio/jamendoplaylistitem.h"
const char* JamendoDynamicPlaylist::kTopTracksMonthUrl =
"http://api.jamendo.com/get2/id+name+url+stream+album_name+"
"album_url+album_id+artist_id+artist_name/track/xspf/"
"track_album+album_artist/?order=ratingmonth_desc&streamencoding=ogg2";
JamendoDynamicPlaylist::JamendoDynamicPlaylist()
: network_(new NetworkAccessManager(this)),
current_page_(0),
current_index_(0) {
}
void JamendoDynamicPlaylist::Load(const QByteArray& data) {
}
QByteArray JamendoDynamicPlaylist::Save() const {
return QByteArray();
}
PlaylistItemList JamendoDynamicPlaylist::Generate() {
return GenerateMore(20);
}
PlaylistItemList JamendoDynamicPlaylist::GenerateMore(int count) {
PlaylistItemList items;
while (items.size() < count) {
// Add items from current list.
if (current_index_ < current_items_.size()) {
PlaylistItemList more_items = current_items_.mid(current_index_, count);
items << more_items;
current_index_ += more_items.size();
} else {
// We need more songs!
Fetch();
}
}
return items;
}
void JamendoDynamicPlaylist::Fetch() {
QUrl url = QUrl(kTopTracksMonthUrl);
url.addQueryItem("pn", QString::number(current_page_++));
url.addQueryItem("n", QString::number(kPageSize));
SongLoader loader(backend_);
SongLoader::Result result = loader.Load(url);
if (result != SongLoader::WillLoadAsync) {
qWarning() << "Jamendo dynamic playlist fetch failed with:"
<< url;
return;
}
// Blocking wait for reply.
{
QEventLoop event_loop;
connect(&loader, SIGNAL(LoadFinished(bool)), &event_loop, SLOT(quit()));
event_loop.exec();
}
const SongList& songs = loader.songs();
if (songs.empty()) {
qWarning() << "No songs returned from Jamendo:"
<< url;
return;
}
current_items_.clear();
foreach (const Song& song, songs) {
current_items_ << PlaylistItemPtr(new JamendoPlaylistItem(song));
}
current_index_ = 0;
}

View File

@ -0,0 +1,35 @@
#ifndef JAMENDODYNAMICPLAYLIST_H
#define JAMENDODYNAMICPLAYLIST_H
#include "smartplaylists/generator.h"
class NetworkAccessManager;
class JamendoDynamicPlaylist : public smart_playlists::Generator {
Q_OBJECT
public:
JamendoDynamicPlaylist();
virtual QString type() const { return "Jamendo"; }
virtual void Load(const QByteArray& data);
virtual QByteArray Save() const;
virtual PlaylistItemList Generate();
virtual bool is_dynamic() const { return true; }
virtual PlaylistItemList GenerateMore(int count);
private:
void Fetch();
NetworkAccessManager* network_;
int current_page_;
PlaylistItemList current_items_;
int current_index_;
static const int kPageSize = 20;
static const char* kTopTracksMonthUrl;
};
#endif

View File

@ -34,6 +34,7 @@
#include "library/librarymodel.h"
#include "radio/radiomodel.h"
#include "radio/jamendoplaylistitem.h"
#include "smartplaylists/generator.h"
#include "ui/iconloader.h"
const char* JamendoService::kServiceName = "Jamendo";
@ -76,6 +77,7 @@ JamendoService::JamendoService(RadioModel* parent)
SLOT(UpdateTotalSongCount(int)));
library_model_ = new LibraryModel(library_backend_, this);
library_model_->set_show_smart_playlists(true);
library_sort_model_->setSourceModel(library_model_);
library_sort_model_->setSortRole(LibraryModel::Role_SortText);
@ -94,12 +96,17 @@ RadioItem* JamendoService::CreateRootItem(RadioItem* parent) {
void JamendoService::LazyPopulate(RadioItem* item) {
switch (item->type) {
case RadioItem::Type_Service:
case RadioItem::Type_Service: {
library_model_->Init();
smart_playlists::GeneratorPtr generator =
smart_playlists::Generator::Create("Jamendo");
generator->set_name(tr("Jamendo Top Tracks of the Month"));
library_model_->AddGenerator(generator);
model()->merged_model()->AddSubModel(
model()->index(root_->row, 0, model()->ItemToIndex(item->parent)),
library_sort_model_);
break;
}
default:
break;
}

View File

@ -17,6 +17,7 @@
#include "generator.h"
#include "querygenerator.h"
#include "radio/jamendodynamicplaylist.h"
#include <QSettings>
@ -35,6 +36,8 @@ Generator::Generator()
GeneratorPtr Generator::Create(const QString& type) {
if (type == "Query")
return GeneratorPtr(new QueryGenerator);
else if (type == "Jamendo")
return GeneratorPtr(new JamendoDynamicPlaylist);
qWarning() << "Invalid playlist generator type:" << type;
return GeneratorPtr();

View File

@ -49,10 +49,13 @@ public:
virtual QString type() const = 0;
// Serialises the Generator's settings
// Called on UI-thread.
virtual void Load(const QByteArray& data) = 0;
// Called on UI-thread.
virtual QByteArray Save() const = 0;
// Creates and returns a playlist
// Called from non-UI thread.
virtual PlaylistItemList Generate() = 0;
// If the generator can be used as a dynamic playlist then GenerateMore
@ -61,6 +64,7 @@ public:
// the tracks returned from this method are not in that set.
virtual bool is_dynamic() const { return false; }
virtual void set_dynamic(bool dynamic) {}
// Called from non-UI thread.
virtual PlaylistItemList GenerateMore(int count) { return PlaylistItemList(); }
signals:

View File

@ -1086,6 +1086,9 @@ msgstr ""
msgid "Invalid session key"
msgstr ""
msgid "Jamendo Top Tracks of the Month"
msgstr ""
msgid "Jump to the currently playing track"
msgstr ""