Directory.cxx 4.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2003-2014 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 "Directory.hxx"
#include "Util.hxx"
#include "Expat.hxx"

25
#include <algorithm>
26 27 28 29 30 31 32 33 34 35
#include <string>
#include <vector>

#include <string.h>

static const char *const upnptags[] = {
	"upnp:artist",
	"upnp:album",
	"upnp:genre",
	"upnp:originalTrackNumber",
36
	nullptr,
37 38
};

39 40 41 42 43 44 45 46 47 48 49 50
gcc_pure
static UPnPDirObject::ItemClass
ParseItemClass(const char *name)
{
	if (strcmp(name, "object.item.audioItem.musicTrack") == 0)
		return UPnPDirObject::ItemClass::MUSIC;
	else if (strcmp(name, "object.item.playlistItem") == 0)
		return UPnPDirObject::ItemClass::PLAYLIST;
	else
		return UPnPDirObject::ItemClass::UNKNOWN;
}

51 52 53 54 55 56 57 58 59 60
gcc_pure
static int
ParseDuration(const std::string &duration)
{
	const auto v = stringToTokens(duration, ":");
	if (v.size() != 3)
		return 0;
	return atoi(v[0].c_str())*3600 + atoi(v[1].c_str())*60 + atoi(v[2].c_str());
}

61 62 63 64 65 66 67
/**
 * Transform titles to turn '/' into '_' to make them acceptable path
 * elements. There is a very slight risk of collision in doing
 * this. Twonky returns directory names (titles) like 'Artist/Album'.
 */
gcc_pure
static std::string
68
titleToPathElt(std::string &&s)
69 70 71 72 73
{
	std::replace(s.begin(), s.end(), '/', '_');
	return s;
}

74 75 76 77
/**
 * An XML parser which builds directory contents from DIDL lite input.
 */
class UPnPDirParser final : public CommonExpatParser {
78
	std::vector<std::string> m_path;
79 80 81 82 83 84 85 86 87 88 89 90
	UPnPDirObject m_tobj;

public:
	UPnPDirParser(UPnPDirContent& dir)
		:m_dir(dir)
	{
	}
	UPnPDirContent& m_dir;

protected:
	virtual void StartElement(const XML_Char *name, const XML_Char **attrs)
	{
91
		m_path.push_back(name);
92

93 94 95 96
		switch (name[0]) {
		case 'c':
			if (!strcmp(name, "container")) {
				m_tobj.clear();
97
				m_tobj.type = UPnPDirObject::Type::CONTAINER;
98 99 100 101 102 103 104 105

				const char *id = GetAttribute(attrs, "id");
				if (id != nullptr)
					m_tobj.m_id = id;

				const char *pid = GetAttribute(attrs, "parentID");
				if (pid != nullptr)
					m_tobj.m_pid = pid;
106 107
			}
			break;
108

109 110 111
		case 'i':
			if (!strcmp(name, "item")) {
				m_tobj.clear();
112
				m_tobj.type = UPnPDirObject::Type::ITEM;
113 114 115 116 117 118 119 120

				const char *id = GetAttribute(attrs, "id");
				if (id != nullptr)
					m_tobj.m_id = id;

				const char *pid = GetAttribute(attrs, "parentID");
				if (pid != nullptr)
					m_tobj.m_pid = pid;
121 122
			}
			break;
123 124 125 126 127 128

		case 'r':
			if (!strcmp(name, "res")) {
				// <res protocolInfo="http-get:*:audio/mpeg:*" size="5171496"
				// bitrate="24576" duration="00:03:35" sampleFrequency="44100"
				// nrAudioChannels="2">
129

130 131 132 133
				const char *duration =
					GetAttribute(attrs, "duration");
				if (duration != nullptr)
					m_tobj.duration = ParseDuration(duration);
134 135
			}

136 137 138 139 140
			break;
		}
	}

	bool checkobjok() {
141
		if (m_tobj.m_id.empty() || m_tobj.m_pid.empty() ||
142
		    m_tobj.name.empty() ||
143 144
		    (m_tobj.type == UPnPDirObject::Type::ITEM &&
		     m_tobj.item_class == UPnPDirObject::ItemClass::UNKNOWN))
145
			return false;
146

147
		return true;
148 149 150 151
	}

	virtual void EndElement(const XML_Char *name)
	{
152 153
		if ((!strcmp(name, "container") || !strcmp(name, "item")) &&
		    checkobjok())
154
			m_dir.objects.push_back(std::move(m_tobj));
155 156 157 158 159 160 161 162

		m_path.pop_back();
	}

	virtual void CharacterData(const XML_Char *s, int len)
	{
		std::string str(s, len);
		trimstring(str);
163
		switch (m_path.back()[0]) {
164
		case 'd':
165 166
			if (!m_path.back().compare("dc:title")) {
				m_tobj.m_title = str;
167
				m_tobj.name = titleToPathElt(std::move(str));
168 169
			}

170 171
			break;
		case 'r':
172
			if (!m_path.back().compare("res")) {
173
				m_tobj.url = str;
174 175 176
			}
			break;
		case 'u':
177 178 179 180 181
			if (m_path.back() == "upnp:class") {
				m_tobj.item_class = ParseItemClass(str.c_str());
				break;
			}

182 183 184
			for (auto i = upnptags; *i != nullptr; ++i)
				if (!m_path.back().compare(*i))
					m_tobj.m_props[*i] += str;
185 186 187 188 189 190 191 192 193 194 195
			break;
		}
	}
};

bool
UPnPDirContent::parse(const std::string &input, Error &error)
{
	UPnPDirParser parser(*this);
	return parser.Parse(input.data(), input.length(), true, error);
}