ContentDirectoryService.cxx 2.71 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "ContentDirectoryService.hxx"
21
#include "UniqueIxml.hxx"
22 23
#include "Device.hxx"
#include "ixmlwrap.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "util/UriRelative.hxx"
25
#include "util/RuntimeError.hxx"
26
#include "util/IterableSplitString.hxx"
27

28 29
#include <upnptools.h>

30
ContentDirectoryService::ContentDirectoryService(const UPnPDevice &device,
31
						 const UPnPService &service) noexcept
32
	:m_actionURL(uri_apply_base(service.controlURL, device.URLBase)),
33 34 35 36 37 38 39
	 m_serviceType(service.serviceType),
	 m_deviceId(device.UDN),
	 m_friendlyName(device.friendlyName),
	 m_manufacturer(device.manufacturer),
	 m_modelName(device.modelName),
	 m_rdreqcnt(200)
{
40
	if (m_modelName == "MediaTomb") {
41 42 43 44 45 46
		// Readdir by 200 entries is good for most, but MediaTomb likes
		// them really big. Actually 1000 is better but I don't dare
		m_rdreqcnt = 500;
	}
}

Rosen Penev's avatar
Rosen Penev committed
47 48
/* this destructor exists here just so it won't get inlined */
ContentDirectoryService::~ContentDirectoryService() noexcept = default;
49

50
std::forward_list<std::string>
51
ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl) const
52
{
53 54 55
	UniqueIxmlDocument request(UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
						  0,
						  nullptr, nullptr));
56 57
	if (!request)
		throw std::runtime_error("UpnpMakeAction() failed");
58

59
	IXML_Document *_response;
60 61
	auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
				   m_serviceType.c_str(),
62
				   nullptr /*devUDN*/, request.get(), &_response);
63 64 65
	if (code != UPNP_E_SUCCESS)
		throw FormatRuntimeError("UpnpSendAction() failed: %s",
					 UpnpGetErrorMessage(code));
66

67 68 69 70 71
	UniqueIxmlDocument response(_response);

	const char *s = ixmlwrap::getFirstElementValue(response.get(),
						       "SearchCaps");
	if (s == nullptr || *s == 0)
72 73 74
		/* we could just "return {}" here, but GCC 5 doesn't
		   understand that */
		return std::forward_list<std::string>();
75

76 77 78 79
	std::forward_list<std::string> result;
	for (const auto &i : IterableSplitString(s, ','))
		result.emplace_front(i);
	return result;
80
}