aboutsummaryrefslogtreecommitdiffstats
path: root/src/playlist/DespotifyPlaylistPlugin.cxx
blob: 3466d5685224b4e81649b3d360b18b3c67d9485f (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * 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 "playlist_plugin.h"
#include "PlaylistRegistry.hxx"
#include "conf.h"
#include "uri.h"
#include "tag.h"
#include "song.h"
#include "input_stream.h"

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

#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;
};

static void
add_song(struct despotify_playlist *ctx, struct ds_track *track)
{
	const char *dsp_scheme = despotify_playlist_plugin.schemes[0];
	struct song *song;
	char uri[128];
	char *ds_uri;

	/* Create a spt://... URI for MPD */
	g_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_remote_new(uri);
	song->tag = mpd_despotify_tag_from_track(track);

	ctx->list = g_slist_prepend(ctx->list, song);
}

static bool
parse_track(struct despotify_playlist *ctx,
		struct ds_link *link)
{
	struct ds_track *track;

	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;

	for (track = playlist->tracks; track; track = track->next)
		add_song(ctx, track);

	return true;
}

static bool
despotify_playlist_init(G_GNUC_UNUSED const struct config_param *param)
{
	return true;
}

static void
despotify_playlist_finish(void)
{
}


static struct playlist_provider *
despotify_playlist_open_uri(const char *url, G_GNUC_UNUSED GMutex *mutex,
			    G_GNUC_UNUSED GCond *cond)
{
	struct despotify_playlist *ctx;
	struct despotify_session *session;
	struct ds_link *link;
	bool parse_result;

	session = mpd_despotify_get_session();
	if (!session)
		goto clean_none;

	/* Get link without spt:// */
	link = despotify_link_from_uri(url + strlen(despotify_playlist_plugin.schemes[0]) + 3);
	if (!link) {
		g_debug("Can't find %s\n", 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);

	switch (link->type)
	{
	case LINK_TYPE_TRACK:
		parse_result = parse_track(ctx, link);
		break;
	case LINK_TYPE_PLAYLIST:
		parse_result = parse_playlist(ctx, link);
		break;
	default:
		parse_result = false;
		break;
	}
	despotify_free_link(link);
	if (!parse_result)
		goto clean_playlist;

	ctx->list = g_slist_reverse(ctx->list);

	return &ctx->base;

clean_playlist:
	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
};

const struct playlist_plugin despotify_playlist_plugin = {
	"despotify",

	despotify_playlist_init,
	despotify_playlist_finish,

	despotify_playlist_open_uri,
	nullptr,
	despotify_playlist_close,
	despotify_playlist_read,

	despotify_schemes,
	nullptr,
	nullptr,
};