DespotifyUtils.cxx 3.97 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "DespotifyUtils.hxx"
21
#include "tag/Tag.hxx"
22
#include "tag/TagBuilder.hxx"
23 24
#include "ConfigGlobal.hxx"
#include "ConfigOption.hxx"
25 26
#include "util/Domain.hxx"
#include "Log.hxx"
27 28

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

32 33
#include <stdio.h>

34 35
const Domain despotify_domain("despotify");

36 37 38 39 40
static struct despotify_session *g_session;
static void (*registered_callbacks[8])(struct despotify_session *,
		int, void *, void *);
static void *registered_callback_data[8];

41 42 43
static void
callback(struct despotify_session* ds, int sig,
	 void *data, gcc_unused void *callback_data)
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
{
	size_t i;

	for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) {
		void (*cb)(struct despotify_session *, int, void *, void *) = registered_callbacks[i];
		void *cb_data = registered_callback_data[i];

		if (cb)
			cb(ds, sig, data, cb_data);
	}
}

bool mpd_despotify_register_callback(void (*cb)(struct despotify_session *, int, void *, void *),
		void *cb_data)
{
	size_t i;

	for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) {

		if (!registered_callbacks[i]) {
			registered_callbacks[i] = cb;
			registered_callback_data[i] = cb_data;

			return true;
		}
	}

	return false;
}

void mpd_despotify_unregister_callback(void (*cb)(struct despotify_session *, int, void *, void *))
{
	size_t i;

	for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) {

		if (registered_callbacks[i] == cb) {
81
			registered_callbacks[i] = nullptr;
82 83 84 85
		}
	}
}

86
Tag
87
mpd_despotify_tag_from_track(const ds_track &track)
88 89 90 91 92
{
	char tracknum[20];
	char comment[80];
	char date[20];

93
	if (!track.has_meta_data)
94
		return Tag();
95

96
	TagBuilder tag;
97 98
	snprintf(tracknum, sizeof(tracknum), "%d", track.tracknumber);
	snprintf(date, sizeof(date), "%d", track.year);
99
	snprintf(comment, sizeof(comment), "Bitrate %d Kbps, %sgeo restricted",
100 101 102 103
		 track.file_bitrate / 1000,
		 track.geo_restricted ? "" : "not ");
	tag.AddItem(TAG_TITLE, track.title);
	tag.AddItem(TAG_ARTIST, track.artist->name);
104
	tag.AddItem(TAG_TRACK, tracknum);
105
	tag.AddItem(TAG_ALBUM, track.album);
106 107
	tag.AddItem(TAG_DATE, date);
	tag.AddItem(TAG_COMMENT, comment);
108
	tag.SetTime(track.length / 1000);
109

110
	return tag.Commit();
111 112 113 114 115 116 117 118 119 120 121
}

struct despotify_session *mpd_despotify_get_session(void)
{
	const char *user;
	const char *passwd;
	bool high_bitrate;

	if (g_session)
		return g_session;

122 123
	user = config_get_string(CONF_DESPOTIFY_USER, nullptr);
	passwd = config_get_string(CONF_DESPOTIFY_PASSWORD, nullptr);
124 125
	high_bitrate = config_get_bool(CONF_DESPOTIFY_HIGH_BITRATE, true);

126
	if (user == nullptr || passwd == nullptr) {
127 128
		LogDebug(despotify_domain,
			 "disabling despotify because account is not configured");
129
		return nullptr;
130
	}
131

132
	if (!despotify_init()) {
133
		LogWarning(despotify_domain, "Can't initialize despotify");
134
		return nullptr;
135 136
	}

137
	g_session = despotify_init_client(callback, nullptr,
138
					  high_bitrate, true);
139
	if (!g_session) {
140 141
		LogWarning(despotify_domain,
			   "Can't initialize despotify client");
142
		return nullptr;
143 144
	}

145
	if (!despotify_authenticate(g_session, user, passwd)) {
146 147
		LogWarning(despotify_domain,
			   "Can't authenticate despotify session");
148
		despotify_exit(g_session);
149
		return nullptr;
150
	}
151 152 153

	return g_session;
}