ContentDirectoryService.cxx 3.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 21
#include "config.h"

22 23
#include "ContentDirectoryService.hxx"
#include "Device.hxx"
24 25 26 27 28
#include "UniqueIxml.hxx"
#ifdef USING_PUPNP
#	include "ixmlwrap.hxx"
#endif
#include "Action.hxx"
29
#include "util/IterableSplitString.hxx"
30 31 32 33 34
#include "util/RuntimeError.hxx"
#include "util/UriRelative.hxx"
#include "util/UriUtil.hxx"

#include <algorithm>
35

36 37
#include <upnptools.h>

38
ContentDirectoryService::ContentDirectoryService(const UPnPDevice &device,
39
						 const UPnPService &service) noexcept
40
	:m_actionURL(uri_apply_base(service.controlURL, device.URLBase)),
41 42 43 44 45 46 47
	 m_serviceType(service.serviceType),
	 m_deviceId(device.UDN),
	 m_friendlyName(device.friendlyName),
	 m_manufacturer(device.manufacturer),
	 m_modelName(device.modelName),
	 m_rdreqcnt(200)
{
48
	if (m_modelName == "MediaTomb") {
49 50 51 52 53 54
		// 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;
	}
}

55 56
/* this destructor exists here just so it won't get inlined */
ContentDirectoryService::~ContentDirectoryService() noexcept = default;
57

58
std::forward_list<std::string>
59
ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl) const
60
{
61
#ifdef USING_PUPNP
62 63 64
	UniqueIxmlDocument request(UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
						  0,
						  nullptr, nullptr));
65 66
	if (!request)
		throw std::runtime_error("UpnpMakeAction() failed");
67

68
	IXML_Document *_response;
69 70
	auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
				   m_serviceType.c_str(),
71
				   nullptr /*devUDN*/, request.get(), &_response);
72 73 74
	if (code != UPNP_E_SUCCESS)
		throw FormatRuntimeError("UpnpSendAction() failed: %s",
					 UpnpGetErrorMessage(code));
75

76 77 78 79
	UniqueIxmlDocument response(_response);

	const char *s = ixmlwrap::getFirstElementValue(response.get(),
						       "SearchCaps");
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#else
	std::vector<std::pair<std::string, std::string>> responseData;
	int errcode;
	std::string errdesc;
	auto code = UpnpSendAction(hdl, "", m_actionURL, m_serviceType,
				   "GetSearchCapabilities", {}, responseData, &errcode,
				   errdesc);
	if (code != UPNP_E_SUCCESS)
		throw FormatRuntimeError("UpnpSendAction() failed: %s",
					 UpnpGetErrorMessage(code));
	const char *s{nullptr};
	for (auto &entry : responseData) {
		if (entry.first == "SearchCaps") {
			s = entry.second.c_str();
		}
	}
#endif
97
	if (s == nullptr || *s == 0)
Rosen Penev's avatar
Rosen Penev committed
98
		return {};
99

100 101 102 103
	std::forward_list<std::string> result;
	for (const auto &i : IterableSplitString(s, ','))
		result.emplace_front(i);
	return result;
104
}