aboutsummaryrefslogtreecommitdiffstats
path: root/src/playlist/DespotifyPlaylistPlugin.cxx
blob: 5759a10c0b373ef65f2ccca6fc1daf9b39d97bbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2011-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 "DespotifyPlaylistPlugin.hxx"
#include "DespotifyUtils.hxx"
#include "MemoryPlaylistProvider.hxx"
#include "Tag.hxx"
#include "Song.hxx"

extern "C" {
#include <despotify.h>
}

#include <glib.h>

#include <string.h>
#include <stdlib.h>

static void
add_song(std::forward_list<SongPointer> &songs, struct ds_track *track)
{
	const char *dsp_scheme = despotify_playlist_plugin.schemes[0];
	Song *song;
	char uri[128];
	char *ds_uri;

	/* Create a spt://... URI for MPD */
	snprintf(uri, sizeof(uri), "%s://", dsp_scheme);
	ds_uri = uri + strlen(dsp_scheme) + 3;

	if (despotify_track_to_uri(track, ds_uri) != ds_uri) {
		/* Should never really fail, but let's be sure */
		g_debug("Can't add track %s\n", track->title);
		return;
	}

	song = Song::NewRemote(uri);
	song->tag = mpd_despotify_tag_from_track(track);

	songs.emplace_front(song);
}

static bool
parse_track(struct despotify_session *session,
	    std::forward_list<SongPointer> &songs,
	    struct ds_link *link)
{
	struct ds_track *track = despotify_link_get_track(session, link);
	if (track == nullptr)
		return false;

	add_song(songs, track);
	return true;
}

static bool
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;
}

static struct playlist_provider *
despotify_playlist_open_uri(const char *url,
			    gcc_unused Mutex &mutex, gcc_unused Cond &cond)
{
	despotify_session *session = mpd_despotify_get_session();
	if (session == nullptr)
		return nullptr;

	/* Get link without spt:// */
	ds_link *link =
		despotify_link_from_uri(url + strlen(despotify_playlist_plugin.schemes[0]) + 3);
	if (link == nullptr) {
		g_debug("Can't find %s\n", url);
		return nullptr;
	}

	std::forward_list<SongPointer> songs;

	bool parse_result;
	switch (link->type) {
	case LINK_TYPE_TRACK:
		parse_result = parse_track(session, songs, link);
		break;
	case LINK_TYPE_PLAYLIST:
		parse_result = parse_playlist(session, songs, link);
		break;
	default:
		parse_result = false;
		break;
	}

	despotify_free_link(link);
	if (!parse_result)
		return nullptr;

	songs.reverse();
	return new MemoryPlaylistProvider(std::move(songs));
}

static const char *const despotify_schemes[] = {
	"spt",
	nullptr
};

const struct playlist_plugin despotify_playlist_plugin = {
	"despotify",

	nullptr,
	nullptr,
	despotify_playlist_open_uri,
	nullptr,
	nullptr,
	nullptr,

	despotify_schemes,
	nullptr,
	nullptr,
};