playlist/despotify: various code simplifications
This commit is contained in:
parent
8cad20585d
commit
e66005563e
@ -33,14 +33,8 @@ extern "C" {
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct despotify_playlist {
|
|
||||||
struct despotify_session *session;
|
|
||||||
|
|
||||||
std::forward_list<SongPointer> songs;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
add_song(struct despotify_playlist *ctx, struct ds_track *track)
|
add_song(std::forward_list<SongPointer> &songs, struct ds_track *track)
|
||||||
{
|
{
|
||||||
const char *dsp_scheme = despotify_playlist_plugin.schemes[0];
|
const char *dsp_scheme = despotify_playlist_plugin.schemes[0];
|
||||||
struct song *song;
|
struct song *song;
|
||||||
@ -60,92 +54,75 @@ add_song(struct despotify_playlist *ctx, struct ds_track *track)
|
|||||||
song = song_remote_new(uri);
|
song = song_remote_new(uri);
|
||||||
song->tag = mpd_despotify_tag_from_track(track);
|
song->tag = mpd_despotify_tag_from_track(track);
|
||||||
|
|
||||||
ctx->songs.emplace_front(song);
|
songs.emplace_front(song);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
parse_track(struct despotify_playlist *ctx,
|
parse_track(struct despotify_session *session,
|
||||||
struct ds_link *link)
|
std::forward_list<SongPointer> &songs,
|
||||||
|
struct ds_link *link)
|
||||||
{
|
{
|
||||||
struct ds_track *track;
|
struct ds_track *track = despotify_link_get_track(session, link);
|
||||||
|
if (track == nullptr)
|
||||||
track = despotify_link_get_track(ctx->session, link);
|
|
||||||
if (!track)
|
|
||||||
return false;
|
|
||||||
add_song(ctx, track);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
|
||||||
parse_playlist(struct despotify_playlist *ctx,
|
|
||||||
struct ds_link *link)
|
|
||||||
{
|
|
||||||
struct ds_playlist *playlist;
|
|
||||||
struct ds_track *track;
|
|
||||||
|
|
||||||
playlist = despotify_link_get_playlist(ctx->session, link);
|
|
||||||
if (!playlist)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (track = playlist->tracks; track; track = track->next)
|
add_song(songs, track);
|
||||||
add_song(ctx, track);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
despotify_playlist_init(G_GNUC_UNUSED const struct config_param *param)
|
parse_playlist(struct despotify_session *session,
|
||||||
|
std::forward_list<SongPointer> &songs,
|
||||||
|
struct ds_link *link)
|
||||||
{
|
{
|
||||||
|
ds_playlist *playlist = despotify_link_get_playlist(session, link);
|
||||||
|
if (playlist == nullptr)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (ds_track *track = playlist->tracks; track != nullptr;
|
||||||
|
track = track->next)
|
||||||
|
add_song(songs, track);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
despotify_playlist_finish(void)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static struct playlist_provider *
|
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_session *session;
|
despotify_session *session = mpd_despotify_get_session();
|
||||||
struct ds_link *link;
|
if (session == nullptr)
|
||||||
bool parse_result;
|
|
||||||
|
|
||||||
session = mpd_despotify_get_session();
|
|
||||||
if (!session)
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
/* Get link without spt:// */
|
/* Get link without spt:// */
|
||||||
link = despotify_link_from_uri(url + strlen(despotify_playlist_plugin.schemes[0]) + 3);
|
ds_link *link =
|
||||||
if (!link) {
|
despotify_link_from_uri(url + strlen(despotify_playlist_plugin.schemes[0]) + 3);
|
||||||
|
if (link == nullptr) {
|
||||||
g_debug("Can't find %s\n", url);
|
g_debug("Can't find %s\n", url);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct despotify_playlist ctx;
|
std::forward_list<SongPointer> songs;
|
||||||
ctx.session = session;
|
|
||||||
|
|
||||||
switch (link->type)
|
bool parse_result;
|
||||||
{
|
switch (link->type) {
|
||||||
case LINK_TYPE_TRACK:
|
case LINK_TYPE_TRACK:
|
||||||
parse_result = parse_track(&ctx, link);
|
parse_result = parse_track(session, songs, link);
|
||||||
break;
|
break;
|
||||||
case LINK_TYPE_PLAYLIST:
|
case LINK_TYPE_PLAYLIST:
|
||||||
parse_result = parse_playlist(&ctx, link);
|
parse_result = parse_playlist(session, songs, link);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
parse_result = false;
|
parse_result = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
despotify_free_link(link);
|
despotify_free_link(link);
|
||||||
if (!parse_result)
|
if (!parse_result)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
ctx.songs.reverse();
|
songs.reverse();
|
||||||
return new MemoryPlaylistProvider(std::move(ctx.songs));
|
return new MemoryPlaylistProvider(std::move(songs));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *const despotify_schemes[] = {
|
static const char *const despotify_schemes[] = {
|
||||||
@ -156,9 +133,8 @@ static const char *const despotify_schemes[] = {
|
|||||||
const struct playlist_plugin despotify_playlist_plugin = {
|
const struct playlist_plugin despotify_playlist_plugin = {
|
||||||
"despotify",
|
"despotify",
|
||||||
|
|
||||||
despotify_playlist_init,
|
nullptr,
|
||||||
despotify_playlist_finish,
|
nullptr,
|
||||||
|
|
||||||
despotify_playlist_open_uri,
|
despotify_playlist_open_uri,
|
||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
|
Loading…
Reference in New Issue
Block a user