playlist/{asx,rss,soundcloud,xspf}: merge code to MemoryPlaylistProvider

This commit is contained in:
Max Kellermann 2013-01-29 18:56:35 +01:00
parent e4455962c6
commit a8b75dc4df
9 changed files with 164 additions and 257 deletions

View File

@ -911,6 +911,8 @@ endif
libplaylist_plugins_a_SOURCES = \ libplaylist_plugins_a_SOURCES = \
src/PlaylistPlugin.hxx \ src/PlaylistPlugin.hxx \
src/playlist/MemoryPlaylistProvider.cxx \
src/playlist/MemoryPlaylistProvider.hxx \
src/playlist/ExtM3uPlaylistPlugin.cxx \ src/playlist/ExtM3uPlaylistPlugin.cxx \
src/playlist/ExtM3uPlaylistPlugin.hxx \ src/playlist/ExtM3uPlaylistPlugin.hxx \
src/playlist/M3uPlaylistPlugin.cxx \ src/playlist/M3uPlaylistPlugin.cxx \

View File

@ -19,7 +19,7 @@
#include "config.h" #include "config.h"
#include "AsxPlaylistPlugin.hxx" #include "AsxPlaylistPlugin.hxx"
#include "PlaylistPlugin.hxx" #include "MemoryPlaylistProvider.hxx"
#include "input_stream.h" #include "input_stream.h"
#include "song.h" #include "song.h"
#include "tag.h" #include "tag.h"
@ -213,17 +213,10 @@ asx_parser_destroy(gpointer data)
* *
*/ */
struct AsxPlaylist {
struct playlist_provider base;
GSList *songs;
};
static struct playlist_provider * static struct playlist_provider *
asx_open_stream(struct input_stream *is) asx_open_stream(struct input_stream *is)
{ {
AsxParser parser; AsxParser parser;
AsxPlaylist *playlist;
GMarkupParseContext *context; GMarkupParseContext *context;
char buffer[1024]; char buffer[1024];
size_t nbytes; size_t nbytes;
@ -270,38 +263,13 @@ asx_open_stream(struct input_stream *is)
/* create a #AsxPlaylist object from the parsed song list */ /* create a #AsxPlaylist object from the parsed song list */
playlist = g_new(AsxPlaylist, 1); MemoryPlaylistProvider *playlist =
playlist_provider_init(&playlist->base, &asx_playlist_plugin); new MemoryPlaylistProvider(g_slist_reverse(parser.songs));
playlist->songs = g_slist_reverse(parser.songs);
parser.songs = NULL; parser.songs = NULL;
g_markup_parse_context_free(context); g_markup_parse_context_free(context);
return &playlist->base; return playlist;
}
static void
asx_close(struct playlist_provider *_playlist)
{
AsxPlaylist *playlist = (AsxPlaylist *)_playlist;
g_slist_foreach(playlist->songs, song_free_callback, NULL);
g_slist_free(playlist->songs);
g_free(playlist);
}
static struct song *
asx_read(struct playlist_provider *_playlist)
{
AsxPlaylist *playlist = (AsxPlaylist *)_playlist;
if (playlist->songs == NULL)
return NULL;
struct song *song = (struct song *)playlist->songs->data;
playlist->songs = g_slist_remove(playlist->songs, song);
return song;
} }
static const char *const asx_suffixes[] = { static const char *const asx_suffixes[] = {
@ -321,8 +289,8 @@ const struct playlist_plugin asx_playlist_plugin = {
nullptr, nullptr,
nullptr, nullptr,
asx_open_stream, asx_open_stream,
asx_close, nullptr,
asx_read, nullptr,
nullptr, nullptr,
asx_suffixes, asx_suffixes,

View File

@ -20,13 +20,9 @@
#include "config.h" #include "config.h"
#include "DespotifyPlaylistPlugin.hxx" #include "DespotifyPlaylistPlugin.hxx"
#include "DespotifyUtils.hxx" #include "DespotifyUtils.hxx"
#include "PlaylistPlugin.hxx" #include "MemoryPlaylistProvider.hxx"
#include "PlaylistRegistry.hxx"
#include "conf.h"
#include "uri.h"
#include "tag.h" #include "tag.h"
#include "song.h" #include "song.h"
#include "input_stream.h"
extern "C" { extern "C" {
#include <despotify.h> #include <despotify.h>
@ -34,13 +30,10 @@ extern "C" {
#include <glib.h> #include <glib.h>
#include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
struct despotify_playlist { struct despotify_playlist {
struct playlist_provider base;
struct despotify_session *session; struct despotify_session *session;
GSList *list; GSList *list;
}; };
@ -116,7 +109,6 @@ static struct playlist_provider *
despotify_playlist_open_uri(const char *url, despotify_playlist_open_uri(const char *url,
gcc_unused Mutex &mutex, gcc_unused Cond &cond) gcc_unused Mutex &mutex, gcc_unused Cond &cond)
{ {
struct despotify_playlist *ctx;
struct despotify_session *session; struct despotify_session *session;
struct ds_link *link; struct ds_link *link;
bool parse_result; bool parse_result;
@ -132,19 +124,17 @@ despotify_playlist_open_uri(const char *url,
goto clean_none; goto clean_none;
} }
ctx = g_new(struct despotify_playlist, 1); struct despotify_playlist ctx;
ctx.list = nullptr;
ctx->list = nullptr; ctx.session = session;
ctx->session = session;
playlist_provider_init(&ctx->base, &despotify_playlist_plugin);
switch (link->type) switch (link->type)
{ {
case LINK_TYPE_TRACK: case LINK_TYPE_TRACK:
parse_result = parse_track(ctx, link); parse_result = parse_track(&ctx, link);
break; break;
case LINK_TYPE_PLAYLIST: case LINK_TYPE_PLAYLIST:
parse_result = parse_playlist(ctx, link); parse_result = parse_playlist(&ctx, link);
break; break;
default: default:
parse_result = false; parse_result = false;
@ -154,53 +144,15 @@ despotify_playlist_open_uri(const char *url,
if (!parse_result) if (!parse_result)
goto clean_playlist; goto clean_playlist;
ctx->list = g_slist_reverse(ctx->list); return new MemoryPlaylistProvider(g_slist_reverse(ctx.list));
return &ctx->base;
clean_playlist: clean_playlist:
g_slist_free(ctx->list); g_slist_free(ctx.list);
clean_none: clean_none:
return nullptr; return nullptr;
} }
static void
track_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
{
struct song *song = (struct song *)data;
song_free(song);
}
static void
despotify_playlist_close(struct playlist_provider *_playlist)
{
struct despotify_playlist *ctx = (struct despotify_playlist *)_playlist;
g_slist_foreach(ctx->list, track_free_callback, nullptr);
g_slist_free(ctx->list);
g_free(ctx);
}
static struct song *
despotify_playlist_read(struct playlist_provider *_playlist)
{
struct despotify_playlist *ctx = (struct despotify_playlist *)_playlist;
if (!ctx->list)
return nullptr;
/* Remove the current track */
song *out = (song *)ctx->list->data;
ctx->list = g_slist_remove(ctx->list, out);
return out;
}
static const char *const despotify_schemes[] = { static const char *const despotify_schemes[] = {
"spt", "spt",
nullptr nullptr
@ -214,8 +166,8 @@ const struct playlist_plugin despotify_playlist_plugin = {
despotify_playlist_open_uri, despotify_playlist_open_uri,
nullptr, nullptr,
despotify_playlist_close, nullptr,
despotify_playlist_read, nullptr,
despotify_schemes, despotify_schemes,
nullptr, nullptr,

View File

@ -0,0 +1,84 @@
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "MemoryPlaylistProvider.hxx"
#include "song.h"
static void
memory_playlist_close(struct playlist_provider *_playlist)
{
MemoryPlaylistProvider *playlist = (MemoryPlaylistProvider *)_playlist;
delete playlist;
}
static struct song *
memory_playlist_read(struct playlist_provider *_playlist)
{
MemoryPlaylistProvider *playlist = (MemoryPlaylistProvider *)_playlist;
return playlist->Read();
}
static constexpr struct playlist_plugin memory_playlist_plugin = {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
memory_playlist_close,
memory_playlist_read,
nullptr,
nullptr,
nullptr,
};
MemoryPlaylistProvider::MemoryPlaylistProvider(GSList *_songs)
:songs(_songs) {
playlist_provider_init(this, &memory_playlist_plugin);
}
static void
song_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
{
struct song *song = (struct song *)data;
song_free(song);
}
MemoryPlaylistProvider::~MemoryPlaylistProvider()
{
g_slist_foreach(songs, song_free_callback, NULL);
g_slist_free(songs);
}
inline song *
MemoryPlaylistProvider::Read()
{
if (songs == nullptr)
return nullptr;
song *result = (song *)songs->data;
songs = g_slist_remove(songs, result);
return result;
}

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_MEMORY_PLAYLIST_PROVIDER_HXX
#define MPD_MEMORY_PLAYLIST_PROVIDER_HXX
#include "PlaylistPlugin.hxx"
#include <glib.h>
struct song;
class MemoryPlaylistProvider : public playlist_provider {
GSList *songs;
public:
MemoryPlaylistProvider(GSList *_songs);
~MemoryPlaylistProvider();
song *Read();
};
#endif

View File

@ -19,7 +19,7 @@
#include "config.h" #include "config.h"
#include "PlsPlaylistPlugin.hxx" #include "PlsPlaylistPlugin.hxx"
#include "PlaylistPlugin.hxx" #include "MemoryPlaylistProvider.hxx"
#include "input_stream.h" #include "input_stream.h"
#include "uri.h" #include "uri.h"
#include "song.h" #include "song.h"
@ -28,8 +28,6 @@
#include <glib.h> #include <glib.h>
struct PlsPlaylist { struct PlsPlaylist {
struct playlist_provider base;
GSList *songs; GSList *songs;
}; };
@ -112,7 +110,6 @@ pls_open_stream(struct input_stream *is)
char buffer[1024]; char buffer[1024];
bool success; bool success;
GKeyFile *keyfile; GKeyFile *keyfile;
PlsPlaylist *playlist;
GString *kf_data = g_string_new(""); GString *kf_data = g_string_new("");
do { do {
@ -153,49 +150,13 @@ pls_open_stream(struct input_stream *is)
return NULL; return NULL;
} }
playlist = g_new(PlsPlaylist, 1); PlsPlaylist playlist;
playlist_provider_init(&playlist->base, &pls_playlist_plugin); playlist.songs = nullptr;
playlist->songs = NULL;
pls_parser(keyfile, playlist);
pls_parser(keyfile, &playlist);
g_key_file_free(keyfile); g_key_file_free(keyfile);
return &playlist->base;
}
return new MemoryPlaylistProvider(g_slist_reverse(playlist.songs));
static void
song_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
{
struct song *song = (struct song *)data;
song_free(song);
}
static void
pls_close(struct playlist_provider *_playlist)
{
PlsPlaylist *playlist = (PlsPlaylist *)_playlist;
g_slist_foreach(playlist->songs, song_free_callback, NULL);
g_slist_free(playlist->songs);
g_free(playlist);
}
static struct song *
pls_read(struct playlist_provider *_playlist)
{
PlsPlaylist *playlist = (PlsPlaylist *)_playlist;
if (playlist->songs == NULL)
return NULL;
struct song *song = (struct song *)playlist->songs->data;
playlist->songs = g_slist_remove(playlist->songs, song);
return song;
} }
static const char *const pls_suffixes[] = { static const char *const pls_suffixes[] = {
@ -215,8 +176,8 @@ const struct playlist_plugin pls_playlist_plugin = {
nullptr, nullptr,
nullptr, nullptr,
pls_open_stream, pls_open_stream,
pls_close, nullptr,
pls_read, nullptr,
nullptr, nullptr,
pls_suffixes, pls_suffixes,

View File

@ -19,7 +19,7 @@
#include "config.h" #include "config.h"
#include "RssPlaylistPlugin.hxx" #include "RssPlaylistPlugin.hxx"
#include "PlaylistPlugin.hxx" #include "MemoryPlaylistProvider.hxx"
#include "input_stream.h" #include "input_stream.h"
#include "song.h" #include "song.h"
#include "tag.h" #include "tag.h"
@ -209,17 +209,10 @@ rss_parser_destroy(gpointer data)
* *
*/ */
struct RssPlaylist {
struct playlist_provider base;
GSList *songs;
};
static struct playlist_provider * static struct playlist_provider *
rss_open_stream(struct input_stream *is) rss_open_stream(struct input_stream *is)
{ {
RssParser parser; RssParser parser;
RssPlaylist *playlist;
GMarkupParseContext *context; GMarkupParseContext *context;
char buffer[1024]; char buffer[1024];
size_t nbytes; size_t nbytes;
@ -266,38 +259,13 @@ rss_open_stream(struct input_stream *is)
/* create a #rss_playlist object from the parsed song list */ /* create a #rss_playlist object from the parsed song list */
playlist = g_new(RssPlaylist, 1); MemoryPlaylistProvider *playlist =
playlist_provider_init(&playlist->base, &rss_playlist_plugin); new MemoryPlaylistProvider(g_slist_reverse(parser.songs));
playlist->songs = g_slist_reverse(parser.songs);
parser.songs = NULL; parser.songs = NULL;
g_markup_parse_context_free(context); g_markup_parse_context_free(context);
return &playlist->base; return playlist;
}
static void
rss_close(struct playlist_provider *_playlist)
{
RssPlaylist *playlist = (RssPlaylist *)_playlist;
g_slist_foreach(playlist->songs, song_free_callback, NULL);
g_slist_free(playlist->songs);
g_free(playlist);
}
static struct song *
rss_read(struct playlist_provider *_playlist)
{
RssPlaylist *playlist = (RssPlaylist *)_playlist;
if (playlist->songs == NULL)
return NULL;
struct song *song = (struct song *)playlist->songs->data;
playlist->songs = g_slist_remove(playlist->songs, song);
return song;
} }
static const char *const rss_suffixes[] = { static const char *const rss_suffixes[] = {
@ -318,8 +286,8 @@ const struct playlist_plugin rss_playlist_plugin = {
nullptr, nullptr,
nullptr, nullptr,
rss_open_stream, rss_open_stream,
rss_close, nullptr,
rss_read, nullptr,
nullptr, nullptr,
rss_suffixes, rss_suffixes,

View File

@ -19,7 +19,7 @@
#include "config.h" #include "config.h"
#include "SoundCloudPlaylistPlugin.hxx" #include "SoundCloudPlaylistPlugin.hxx"
#include "PlaylistPlugin.hxx" #include "MemoryPlaylistProvider.hxx"
#include "conf.h" #include "conf.h"
#include "input_stream.h" #include "input_stream.h"
#include "song.h" #include "song.h"
@ -30,12 +30,6 @@
#include <string.h> #include <string.h>
struct soundcloud_playlist {
struct playlist_provider base;
GSList *songs;
};
static struct { static struct {
char *apikey; char *apikey;
} soundcloud_config; } soundcloud_config;
@ -326,8 +320,6 @@ soundcloud_parse_json(const char *url, yajl_handle hand,
static struct playlist_provider * static struct playlist_provider *
soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond) soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond)
{ {
struct soundcloud_playlist *playlist = NULL;
char *s, *p; char *s, *p;
char *scheme, *arg, *rest; char *scheme, *arg, *rest;
s = g_strdup(uri); s = g_strdup(uri);
@ -399,34 +391,7 @@ soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond)
if (ret == -1) if (ret == -1)
return NULL; return NULL;
playlist = g_new(struct soundcloud_playlist, 1); return new MemoryPlaylistProvider(g_slist_reverse(data.songs));
playlist_provider_init(&playlist->base, &soundcloud_playlist_plugin);
playlist->songs = g_slist_reverse(data.songs);
return &playlist->base;
}
static void
soundcloud_close(struct playlist_provider *_playlist)
{
struct soundcloud_playlist *playlist = (struct soundcloud_playlist *)_playlist;
g_free(playlist);
}
static struct song *
soundcloud_read(struct playlist_provider *_playlist)
{
struct soundcloud_playlist *playlist = (struct soundcloud_playlist *)_playlist;
if (playlist->songs == NULL)
return NULL;
struct song* s;
s = (struct song *)playlist->songs->data;
playlist->songs = g_slist_remove(playlist->songs, s);
return s;
} }
static const char *const soundcloud_schemes[] = { static const char *const soundcloud_schemes[] = {
@ -441,8 +406,8 @@ const struct playlist_plugin soundcloud_playlist_plugin = {
soundcloud_finish, soundcloud_finish,
soundcloud_open_uri, soundcloud_open_uri,
nullptr, nullptr,
soundcloud_close, nullptr,
soundcloud_read, nullptr,
soundcloud_schemes, soundcloud_schemes,
nullptr, nullptr,

View File

@ -19,7 +19,7 @@
#include "config.h" #include "config.h"
#include "XspfPlaylistPlugin.hxx" #include "XspfPlaylistPlugin.hxx"
#include "PlaylistPlugin.hxx" #include "MemoryPlaylistProvider.hxx"
#include "input_stream.h" #include "input_stream.h"
#include "uri.h" #include "uri.h"
#include "song.h" #include "song.h"
@ -231,17 +231,10 @@ xspf_parser_destroy(gpointer data)
* *
*/ */
struct XspfPlaylist {
struct playlist_provider base;
GSList *songs;
};
static struct playlist_provider * static struct playlist_provider *
xspf_open_stream(struct input_stream *is) xspf_open_stream(struct input_stream *is)
{ {
XspfParser parser; XspfParser parser;
XspfPlaylist *playlist;
GMarkupParseContext *context; GMarkupParseContext *context;
char buffer[1024]; char buffer[1024];
size_t nbytes; size_t nbytes;
@ -288,38 +281,13 @@ xspf_open_stream(struct input_stream *is)
/* create a #xspf_playlist object from the parsed song list */ /* create a #xspf_playlist object from the parsed song list */
playlist = g_new(XspfPlaylist, 1); MemoryPlaylistProvider *playlist =
playlist_provider_init(&playlist->base, &xspf_playlist_plugin); new MemoryPlaylistProvider(g_slist_reverse(parser.songs));
playlist->songs = g_slist_reverse(parser.songs);
parser.songs = NULL; parser.songs = NULL;
g_markup_parse_context_free(context); g_markup_parse_context_free(context);
return &playlist->base; return playlist;
}
static void
xspf_close(struct playlist_provider *_playlist)
{
XspfPlaylist *playlist = (XspfPlaylist *)_playlist;
g_slist_foreach(playlist->songs, song_free_callback, NULL);
g_slist_free(playlist->songs);
g_free(playlist);
}
static struct song *
xspf_read(struct playlist_provider *_playlist)
{
XspfPlaylist *playlist = (XspfPlaylist *)_playlist;
if (playlist->songs == NULL)
return NULL;
struct song *song = (struct song *)playlist->songs->data;
playlist->songs = g_slist_remove(playlist->songs, song);
return song;
} }
static const char *const xspf_suffixes[] = { static const char *const xspf_suffixes[] = {
@ -339,8 +307,8 @@ const struct playlist_plugin xspf_playlist_plugin = {
nullptr, nullptr,
nullptr, nullptr,
xspf_open_stream, xspf_open_stream,
xspf_close, nullptr,
xspf_read, nullptr,
nullptr, nullptr,
xspf_suffixes, xspf_suffixes,