libupnpp 0.16.0
A C++ wrapper for the Portable UPnP reference library
cdircontent.hxx
1/* Copyright (C) 2006-2024 J.F.Dockes
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18#ifndef _UPNPDIRCONTENT_H_X_INCLUDED_
19#define _UPNPDIRCONTENT_H_X_INCLUDED_
20
21#include <map>
22#include <memory>
23#include <sstream>
24#include <string>
25#include <utility>
26#include <vector>
27
28#include "libupnpp/upnpavutils.hxx"
29
30namespace UPnPClient {
31
37class UPNPP_API UPnPResource {
38public:
40 std::string m_uri;
41
43 std::map<std::string, std::string> m_props;
44
47 const auto it = m_props.find("protocolInfo");
48 if (it == m_props.end()) {
49 return false;
50 }
51 return UPnPP::parseProtoInfEntry(it->second, e);
52 }
53};
54
60class UPNPP_API UPnPDirObject {
61public:
62
64 // Types
65
66 enum ObjType {objtnone = -1, item, container};
67 // There are actually several kinds of containers: object.container.storageFolder,
68 // object.container.person, object.container.playlistContainer etc., but they all seem to behave
69 // the same as far as we're concerned. Otoh, musicTrack items are special to us, and so should
70 // playlists, but I've not seen one of the latter yet (servers seem to use containers for
71 // playlists).
72 enum ItemClass {ITC_audioItem = 0, ITC_playlist = 1,
73 ITC_unknown = 2,
74 ITC_videoItem = 3,
75 ITC_audioItem_musicTrack = ITC_audioItem, // hist compat
76 ITC_audioItem_playlist = ITC_playlist // hist compat
77 };
78
80 // Data fields
81
83 std::string m_id;
85 std::string m_pid;
87 std::string m_title;
89 ObjType m_type;
91 ItemClass m_iclass;
92
102 std::multimap<std::string, std::string> m_props;
103
106 std::vector<UPnPResource> m_resources;
107
108
110 // Methods
111
118 bool getprop(const std::string& name, std::string& value) const;
119
125 const std::string getprop(const std::string& name) const;
126
128 const std::string& getupropref(const std::string& name) const;
129
133 bool getRoledArtists(std::vector<std::pair<std::string, std::string>>& out) const;
135 std::string getAlbumArtist() const;
138 std::string getArtists() const;
140 std::string getAlbumArtistElseArtists() const;
141
150 bool getrprop(unsigned int ridx, const std::string& nm, std::string& val) const {
151 if (ridx >= m_resources.size())
152 return false;
153 const auto it = m_resources[ridx].m_props.find(nm);
154 if (it == m_resources[ridx].m_props.end())
155 return false;
156 val = it->second;
157 return true;
158 }
159
163 std::string f2s(const std::string& nm, bool isresfield) {
164 std::string val;
165 if (isresfield) {
166 getrprop(0, nm, val);
167 } else {
168 getprop(nm, val);
169 }
170 return val;
171 }
172
177 int getDurationSeconds(unsigned ridx = 0) const {
178 std::string sdur;
179 if (!getrprop(ridx, "duration", sdur)) {
180 //?? Avoid returning 0, who knows...
181 return 1;
182 }
183 return UPnPP::upnpdurationtos(sdur);
184 }
185
191 std::string getdidl() const;
192
193 void clear() {
194 m_id.clear();
195 m_pid.clear();
196 m_title.clear();
197 m_type = objtnone;
198 m_iclass = ITC_unknown;
199 m_props.clear();
200 m_resources.clear();
201 m_didlfrag.clear();
202 }
203
204 std::string dump() const {
205 std::ostringstream os;
206 os << "UPnPDirObject: " << (m_type == item ? "item" : "container") << " id [" << m_id <<
207 "] pid [" << m_pid << "] title [" << m_title << "]" << '\n';
208 os << "Properties: " << '\n';
209 for (const auto& m_prop : m_props) {
210 os << "[" << m_prop.first << "]->[" << m_prop.second << "] " << '\n';
211 }
212 os << "Resources:" << '\n';
213 for (const auto& m_resource : m_resources) {
214 os << " Uri [" << m_resource.m_uri << "]" << '\n';
215 os << " Resource attributes:" << '\n';
216 for (const auto& m_prop : m_resource.m_props) {
217 os << " [" << m_prop.first << "]->[" << m_prop.second << "] " << '\n';
218 }
219 }
220 os << '\n';
221 return os.str();
222 }
223
224private:
225 friend class UPnPDirParser;
226 // DIDL text for element, sans header
227 std::string m_didlfrag;
228 static std::string nullstr;
229};
230
239class UPNPP_API UPnPDirContent {
240public:
241 std::vector<UPnPDirObject> m_containers;
242 std::vector<UPnPDirObject> m_items;
243
244 void clear() {
245 m_containers.clear();
246 m_items.clear();
247 }
248
261 bool parse(const std::string& didltext);
262};
263
264} // namespace
265
266#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */
Image of a MediaServer Directory Service container (directory), possibly containing items and subordi...
Definition cdircontent.hxx:239
UPnP Media Server directory entry, converted from XML data.
Definition cdircontent.hxx:60
std::vector< UPnPResource > m_resources
Resources: there may be several, for example for different audio formats of the same track,...
Definition cdircontent.hxx:106
std::string m_id
Object Id.
Definition cdircontent.hxx:83
std::string m_title
Value of dc:title.
Definition cdircontent.hxx:87
bool getrprop(unsigned int ridx, const std::string &nm, std::string &val) const
Get named resource attribute.
Definition cdircontent.hxx:150
std::string f2s(const std::string &nm, bool isresfield)
Simplified interface to retrieving values: we don't distinguish between non-existing and empty,...
Definition cdircontent.hxx:163
std::string m_pid
Parent Object Id.
Definition cdircontent.hxx:85
int getDurationSeconds(unsigned ridx=0) const
Return resource duration in seconds.
Definition cdircontent.hxx:177
ItemClass m_iclass
Item type details.
Definition cdircontent.hxx:91
std::multimap< std::string, std::string > m_props
Storage for the properties.
Definition cdircontent.hxx:102
ObjType m_type
Item or container.
Definition cdircontent.hxx:89
UPnP resource.
Definition cdircontent.hxx:37
std::map< std::string, std::string > m_props
Resource attributes.
Definition cdircontent.hxx:43
std::string m_uri
URI Value.
Definition cdircontent.hxx:40
bool protoInfo(UPnPP::ProtocolinfoEntry &e) const
Get the protocolinfo attribute in cooked form.
Definition cdircontent.hxx:46
UPnP Description phase: interpreting the device description which we downloaded from the URL obtained...
Definition avlastchg.cxx:28
Decoded protocolinfo entry data.
Definition upnpavutils.hxx:39