aboutsummaryrefslogtreecommitdiff
path: root/src/playlist
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-01-29 18:56:35 +0100
committerMax Kellermann <max@duempel.org>2013-01-29 18:56:35 +0100
commita8b75dc4df3bf3ba9a1554249cf6d07527184864 (patch)
tree30749b1aa56a0b8fd1f869ad96d2869b4e639b31 /src/playlist
parente4455962c640bd157713002b1d8d74fe64cecf67 (diff)
playlist/{asx,rss,soundcloud,xspf}: merge code to MemoryPlaylistProvider
Diffstat (limited to 'src/playlist')
-rw-r--r--src/playlist/AsxPlaylistPlugin.cxx44
-rw-r--r--src/playlist/DespotifyPlaylistPlugin.cxx68
-rw-r--r--src/playlist/MemoryPlaylistProvider.cxx84
-rw-r--r--src/playlist/MemoryPlaylistProvider.hxx39
-rw-r--r--src/playlist/PlsPlaylistPlugin.cxx53
-rw-r--r--src/playlist/RssPlaylistPlugin.cxx44
-rw-r--r--src/playlist/SoundCloudPlaylistPlugin.cxx43
-rw-r--r--src/playlist/XspfPlaylistPlugin.cxx44
8 files changed, 162 insertions, 257 deletions
diff --git a/src/playlist/AsxPlaylistPlugin.cxx b/src/playlist/AsxPlaylistPlugin.cxx
index 2a787f66..db571417 100644
--- a/src/playlist/AsxPlaylistPlugin.cxx
+++ b/src/playlist/AsxPlaylistPlugin.cxx
@@ -19,7 +19,7 @@
#include "config.h"
#include "AsxPlaylistPlugin.hxx"
-#include "PlaylistPlugin.hxx"
+#include "MemoryPlaylistProvider.hxx"
#include "input_stream.h"
#include "song.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 *
asx_open_stream(struct input_stream *is)
{
AsxParser parser;
- AsxPlaylist *playlist;
GMarkupParseContext *context;
char buffer[1024];
size_t nbytes;
@@ -270,38 +263,13 @@ asx_open_stream(struct input_stream *is)
/* create a #AsxPlaylist object from the parsed song list */
- playlist = g_new(AsxPlaylist, 1);
- playlist_provider_init(&playlist->base, &asx_playlist_plugin);
- playlist->songs = g_slist_reverse(parser.songs);
+ MemoryPlaylistProvider *playlist =
+ new MemoryPlaylistProvider(g_slist_reverse(parser.songs));
parser.songs = NULL;
g_markup_parse_context_free(context);
- return &playlist->base;
-}
-
-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;
+ return playlist;
}
static const char *const asx_suffixes[] = {
@@ -321,8 +289,8 @@ const struct playlist_plugin asx_playlist_plugin = {
nullptr,
nullptr,
asx_open_stream,
- asx_close,
- asx_read,
+ nullptr,
+ nullptr,
nullptr,
asx_suffixes,
diff --git a/src/playlist/DespotifyPlaylistPlugin.cxx b/src/playlist/DespotifyPlaylistPlugin.cxx
index 00b697e3..09c1aa89 100644
--- a/src/playlist/DespotifyPlaylistPlugin.cxx
+++ b/src/playlist/DespotifyPlaylistPlugin.cxx
@@ -20,13 +20,9 @@
#include "config.h"
#include "DespotifyPlaylistPlugin.hxx"
#include "DespotifyUtils.hxx"
-#include "PlaylistPlugin.hxx"
-#include "PlaylistRegistry.hxx"
-#include "conf.h"
-#include "uri.h"
+#include "MemoryPlaylistProvider.hxx"
#include "tag.h"
#include "song.h"
-#include "input_stream.h"
extern "C" {
#include <despotify.h>
@@ -34,13 +30,10 @@ extern "C" {
#include <glib.h>
-#include <assert.h>
#include <string.h>
#include <stdlib.h>
struct despotify_playlist {
- struct playlist_provider base;
-
struct despotify_session *session;
GSList *list;
};
@@ -116,7 +109,6 @@ static struct playlist_provider *
despotify_playlist_open_uri(const char *url,
gcc_unused Mutex &mutex, gcc_unused Cond &cond)
{
- struct despotify_playlist *ctx;
struct despotify_session *session;
struct ds_link *link;
bool parse_result;
@@ -132,19 +124,17 @@ despotify_playlist_open_uri(const char *url,
goto clean_none;
}
- ctx = g_new(struct despotify_playlist, 1);
-
- ctx->list = nullptr;
- ctx->session = session;
- playlist_provider_init(&ctx->base, &despotify_playlist_plugin);
+ struct despotify_playlist ctx;
+ ctx.list = nullptr;
+ ctx.session = session;
switch (link->type)
{
case LINK_TYPE_TRACK:
- parse_result = parse_track(ctx, link);
+ parse_result = parse_track(&ctx, link);
break;
case LINK_TYPE_PLAYLIST:
- parse_result = parse_playlist(ctx, link);
+ parse_result = parse_playlist(&ctx, link);
break;
default:
parse_result = false;
@@ -154,53 +144,15 @@ despotify_playlist_open_uri(const char *url,
if (!parse_result)
goto clean_playlist;
- ctx->list = g_slist_reverse(ctx->list);
-
- return &ctx->base;
+ return new MemoryPlaylistProvider(g_slist_reverse(ctx.list));
clean_playlist:
- g_slist_free(ctx->list);
+ g_slist_free(ctx.list);
clean_none:
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[] = {
"spt",
nullptr
@@ -214,8 +166,8 @@ const struct playlist_plugin despotify_playlist_plugin = {
despotify_playlist_open_uri,
nullptr,
- despotify_playlist_close,
- despotify_playlist_read,
+ nullptr,
+ nullptr,
despotify_schemes,
nullptr,
diff --git a/src/playlist/MemoryPlaylistProvider.cxx b/src/playlist/MemoryPlaylistProvider.cxx
new file mode 100644
index 00000000..b643ec50
--- /dev/null
+++ b/src/playlist/MemoryPlaylistProvider.cxx
@@ -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;
+}
+
diff --git a/src/playlist/MemoryPlaylistProvider.hxx b/src/playlist/MemoryPlaylistProvider.hxx
new file mode 100644
index 00000000..ad0a74d8
--- /dev/null
+++ b/src/playlist/MemoryPlaylistProvider.hxx
@@ -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
diff --git a/src/playlist/PlsPlaylistPlugin.cxx b/src/playlist/PlsPlaylistPlugin.cxx
index 8cb8b4f1..cecabc88 100644
--- a/src/playlist/PlsPlaylistPlugin.cxx
+++ b/src/playlist/PlsPlaylistPlugin.cxx
@@ -19,7 +19,7 @@
#include "config.h"
#include "PlsPlaylistPlugin.hxx"
-#include "PlaylistPlugin.hxx"
+#include "MemoryPlaylistProvider.hxx"
#include "input_stream.h"
#include "uri.h"
#include "song.h"
@@ -28,8 +28,6 @@
#include <glib.h>
struct PlsPlaylist {
- struct playlist_provider base;
-
GSList *songs;
};
@@ -112,7 +110,6 @@ pls_open_stream(struct input_stream *is)
char buffer[1024];
bool success;
GKeyFile *keyfile;
- PlsPlaylist *playlist;
GString *kf_data = g_string_new("");
do {
@@ -153,49 +150,13 @@ pls_open_stream(struct input_stream *is)
return NULL;
}
- playlist = g_new(PlsPlaylist, 1);
- playlist_provider_init(&playlist->base, &pls_playlist_plugin);
- playlist->songs = NULL;
-
- pls_parser(keyfile, playlist);
+ PlsPlaylist playlist;
+ playlist.songs = nullptr;
+ pls_parser(keyfile, &playlist);
g_key_file_free(keyfile);
- return &playlist->base;
-}
-
-
-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;
+ return new MemoryPlaylistProvider(g_slist_reverse(playlist.songs));
}
static const char *const pls_suffixes[] = {
@@ -215,8 +176,8 @@ const struct playlist_plugin pls_playlist_plugin = {
nullptr,
nullptr,
pls_open_stream,
- pls_close,
- pls_read,
+ nullptr,
+ nullptr,
nullptr,
pls_suffixes,
diff --git a/src/playlist/RssPlaylistPlugin.cxx b/src/playlist/RssPlaylistPlugin.cxx
index 9b95683c..1c714a96 100644
--- a/src/playlist/RssPlaylistPlugin.cxx
+++ b/src/playlist/RssPlaylistPlugin.cxx
@@ -19,7 +19,7 @@
#include "config.h"
#include "RssPlaylistPlugin.hxx"
-#include "PlaylistPlugin.hxx"
+#include "MemoryPlaylistProvider.hxx"
#include "input_stream.h"
#include "song.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 *
rss_open_stream(struct input_stream *is)
{
RssParser parser;
- RssPlaylist *playlist;
GMarkupParseContext *context;
char buffer[1024];
size_t nbytes;
@@ -266,38 +259,13 @@ rss_open_stream(struct input_stream *is)
/* create a #rss_playlist object from the parsed song list */
- playlist = g_new(RssPlaylist, 1);
- playlist_provider_init(&playlist->base, &rss_playlist_plugin);
- playlist->songs = g_slist_reverse(parser.songs);
+ MemoryPlaylistProvider *playlist =
+ new MemoryPlaylistProvider(g_slist_reverse(parser.songs));
parser.songs = NULL;
g_markup_parse_context_free(context);
- return &playlist->base;
-}
-
-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;
+ return playlist;
}
static const char *const rss_suffixes[] = {
@@ -318,8 +286,8 @@ const struct playlist_plugin rss_playlist_plugin = {
nullptr,
nullptr,
rss_open_stream,
- rss_close,
- rss_read,
+ nullptr,
+ nullptr,
nullptr,
rss_suffixes,
diff --git a/src/playlist/SoundCloudPlaylistPlugin.cxx b/src/playlist/SoundCloudPlaylistPlugin.cxx
index eefa23dd..b236481a 100644
--- a/src/playlist/SoundCloudPlaylistPlugin.cxx
+++ b/src/playlist/SoundCloudPlaylistPlugin.cxx
@@ -19,7 +19,7 @@
#include "config.h"
#include "SoundCloudPlaylistPlugin.hxx"
-#include "PlaylistPlugin.hxx"
+#include "MemoryPlaylistProvider.hxx"
#include "conf.h"
#include "input_stream.h"
#include "song.h"
@@ -30,12 +30,6 @@
#include <string.h>
-struct soundcloud_playlist {
- struct playlist_provider base;
-
- GSList *songs;
-};
-
static struct {
char *apikey;
} soundcloud_config;
@@ -326,8 +320,6 @@ soundcloud_parse_json(const char *url, yajl_handle hand,
static struct playlist_provider *
soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond)
{
- struct soundcloud_playlist *playlist = NULL;
-
char *s, *p;
char *scheme, *arg, *rest;
s = g_strdup(uri);
@@ -399,34 +391,7 @@ soundcloud_open_uri(const char *uri, Mutex &mutex, Cond &cond)
if (ret == -1)
return NULL;
- playlist = g_new(struct soundcloud_playlist, 1);
- 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;
+ return new MemoryPlaylistProvider(g_slist_reverse(data.songs));
}
static const char *const soundcloud_schemes[] = {
@@ -441,8 +406,8 @@ const struct playlist_plugin soundcloud_playlist_plugin = {
soundcloud_finish,
soundcloud_open_uri,
nullptr,
- soundcloud_close,
- soundcloud_read,
+ nullptr,
+ nullptr,
soundcloud_schemes,
nullptr,
diff --git a/src/playlist/XspfPlaylistPlugin.cxx b/src/playlist/XspfPlaylistPlugin.cxx
index 95033d4e..38bcbdf9 100644
--- a/src/playlist/XspfPlaylistPlugin.cxx
+++ b/src/playlist/XspfPlaylistPlugin.cxx
@@ -19,7 +19,7 @@
#include "config.h"
#include "XspfPlaylistPlugin.hxx"
-#include "PlaylistPlugin.hxx"
+#include "MemoryPlaylistProvider.hxx"
#include "input_stream.h"
#include "uri.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 *
xspf_open_stream(struct input_stream *is)
{
XspfParser parser;
- XspfPlaylist *playlist;
GMarkupParseContext *context;
char buffer[1024];
size_t nbytes;
@@ -288,38 +281,13 @@ xspf_open_stream(struct input_stream *is)
/* create a #xspf_playlist object from the parsed song list */
- playlist = g_new(XspfPlaylist, 1);
- playlist_provider_init(&playlist->base, &xspf_playlist_plugin);
- playlist->songs = g_slist_reverse(parser.songs);
+ MemoryPlaylistProvider *playlist =
+ new MemoryPlaylistProvider(g_slist_reverse(parser.songs));
parser.songs = NULL;
g_markup_parse_context_free(context);
- return &playlist->base;
-}
-
-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;
+ return playlist;
}
static const char *const xspf_suffixes[] = {
@@ -339,8 +307,8 @@ const struct playlist_plugin xspf_playlist_plugin = {
nullptr,
nullptr,
xspf_open_stream,
- xspf_close,
- xspf_read,
+ nullptr,
+ nullptr,
nullptr,
xspf_suffixes,