libupnpp  0.16.0
A C++ wrapper for the Portable UPnP reference library
cdircontent.hxx
1 /* Copyright (C) 2006-2019 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 <sstream>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 #include <memory>
27 
28 #include "libupnpp/upnpavutils.hxx"
29 
30 namespace UPnPClient {
31 
39 class UPNPP_API UPnPResource {
40 public:
42  std::string m_uri;
43 
45  std::map<std::string, std::string> m_props;
46 
49  const auto it = m_props.find("protocolInfo");
50  if (it == m_props.end()) {
51  return false;
52  }
53  return UPnPP::parseProtoInfEntry(it->second, e);
54  }
55 };
56 
62 class UPNPP_API UPnPDirObject {
63 public:
64 
66  // Types
67 
68  enum ObjType {objtnone = -1, item, container};
69  // There are actually several kinds of containers:
70  // object.container.storageFolder, object.container.person,
71  // object.container.playlistContainer etc., but they all seem to
72  // behave the same as far as we're concerned. Otoh, musicTrack
73  // items are special to us, and so should playlists, but I've not
74  // seen one of the latter yet (servers seem to use containers for
75  // playlists).
76  enum ItemClass {ITC_audioItem = 0, ITC_playlist = 1,
77  ITC_unknown = 2,
78  ITC_videoItem = 3,
79  ITC_audioItem_musicTrack = ITC_audioItem, // hist compat
80  ITC_audioItem_playlist = ITC_playlist // hist compat
81  };
82 
88  struct PropertyValue {
89  PropertyValue() {}
90  PropertyValue(const std::string& v,
91  const std::map<std::string, std::string>& a)
92  : value(v) {
93  if (!a.empty()) {
94  attrs = new std::map<std::string, std::string>(a);
95  }
96  }
97  PropertyValue& operator=(PropertyValue const&) = delete;
99  : value(l.value) {
100  if (l.attrs) {
101  attrs = new std::map<std::string, std::string>(*l.attrs);
102  }
103  }
104  ~PropertyValue() {
105  delete attrs;
106  }
107  std::string value;
108  // Not using shared_ptr here because we are optimizing size in
109  // the common case of absent attributes.
110  std::map <std::string, std::string> *attrs{nullptr};
111  };
112  typedef std::map<std::string, std::vector<PropertyValue>> PropertyMap;
113 
115  // Data fields
116 
118  std::string m_id;
120  std::string m_pid;
122  std::string m_title;
124  ObjType m_type;
126  ItemClass m_iclass;
127 
136  std::map<std::string, std::string> m_props;
137 
143  std::shared_ptr<PropertyMap> m_allprops{std::shared_ptr<PropertyMap>()};
144 
148  std::vector<UPnPResource> m_resources;
149 
150 
152  // Methods
153 
160  bool getprop(const std::string& name, std::string& value) const {
161  const auto it = m_props.find(name);
162  if (it == m_props.end())
163  return false;
164  value = it->second;
165  return true;
166  }
167 
173  const std::string& getprop(const std::string& name) const {
174  const auto it = m_props.find(name);
175  return (it == m_props.end()) ? nullstr : it->second;
176  }
177 
186  bool getrprop(unsigned int ridx, const std::string& nm, std::string& val)
187  const {
188  if (ridx >= m_resources.size())
189  return false;
190  const auto it = m_resources[ridx].m_props.find(nm);
191  if (it == m_resources[ridx].m_props.end())
192  return false;
193  val = it->second;
194  return true;
195  }
196 
200  std::string f2s(const std::string& nm, bool isresfield) {
201  std::string val;
202  if (isresfield) {
203  getrprop(0, nm, val);
204  } else {
205  getprop(nm, val);
206  }
207  return val;
208  }
209 
214  int getDurationSeconds(unsigned ridx = 0) const {
215  std::string sdur;
216  if (!getrprop(ridx, "duration", sdur)) {
217  //?? Avoid returning 0, who knows...
218  return 1;
219  }
220  return UPnPP::upnpdurationtos(sdur);
221  }
222 
229  std::string getdidl() const;
230 
231  void clear(bool detailed=false) {
232  m_id.clear();
233  m_pid.clear();
234  m_title.clear();
235  m_type = objtnone;
236  m_iclass = ITC_unknown;
237  m_props.clear();
238  if (detailed) {
239  m_allprops = std::shared_ptr<PropertyMap>(new PropertyMap);
240  } else {
241  m_allprops = std::shared_ptr<PropertyMap>();
242  }
243  m_resources.clear();
244  m_didlfrag.clear();
245  }
246 
247  std::string dump() const {
248  std::ostringstream os;
249  os << "UPnPDirObject: " << (m_type == item ? "item" : "container") <<
250  " id [" << m_id << "] pid [" << m_pid <<
251  "] title [" << m_title << "]" << std::endl;
252  os << "Properties: " << std::endl;
253  for (std::map<std::string,std::string>::const_iterator it =
254  m_props.begin();
255  it != m_props.end(); it++) {
256  os << "[" << it->first << "]->[" << it->second << "] "
257  << std::endl;
258  }
259  os << "Resources:" << std::endl;
260  for (std::vector<UPnPResource>::const_iterator it =
261  m_resources.begin(); it != m_resources.end(); it++) {
262  os << " Uri [" << it->m_uri << "]" << std::endl;
263  os << " Resource attributes:" << std::endl;
264  for (std::map<std::string, std::string>::const_iterator it1 =
265  it->m_props.begin();
266  it1 != it->m_props.end(); it1++) {
267  os << " [" << it1->first << "]->[" << it1->second << "] "
268  << std::endl;
269  }
270  }
271  os << std::endl;
272  return os.str();
273  }
274 
275 private:
276  friend class UPnPDirParser;
277  // DIDL text for element, sans header
278  std::string m_didlfrag;
279  static std::string nullstr;
280 };
281 
286 class UPNPP_API UPnPDirContent {
287 public:
288  std::vector<UPnPDirObject> m_containers;
289  std::vector<UPnPDirObject> m_items;
290 
291  void clear()
292  {
293  m_containers.clear();
294  m_items.clear();
295  }
296 
312  bool parse(const std::string& didltext, bool detailed = false);
313 };
314 
315 } // namespace
316 
317 #endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */
Definition: cdircontent.cxx:41
ItemClass m_iclass
Item type details.
Definition: cdircontent.hxx:126
const std::string & getprop(const std::string &name) const
Get named property.
Definition: cdircontent.hxx:173
std::string m_uri
URI Value.
Definition: cdircontent.hxx:42
std::string m_title
Value of dc:title.
Definition: cdircontent.hxx:122
std::vector< UPnPResource > m_resources
Resources: there may be several, for example for different audio formats of the same track...
Definition: cdircontent.hxx:148
bool protoInfo(UPnPP::ProtocolinfoEntry &e) const
Get the protocolinfo attribute in cooked form.
Definition: cdircontent.hxx:48
bool getprop(const std::string &name, std::string &value) const
Get named property.
Definition: cdircontent.hxx:160
std::map< std::string, std::string > m_props
Basic/compat storage for the properties, with multiple values concatenated.
Definition: cdircontent.hxx:136
Decoded protocolinfo entry data.
Definition: upnpavutils.hxx:39
int getDurationSeconds(unsigned ridx=0) const
Return resource duration in seconds.
Definition: cdircontent.hxx:214
std::string m_id
Object Id.
Definition: cdircontent.hxx:118
std::map< std::string, std::string > m_props
Resource attributes.
Definition: cdircontent.hxx:45
Image of a MediaServer Directory Service container (directory), possibly containing items and subordi...
Definition: cdircontent.hxx:286
A PropertyValue object describes one instance of a property (the name of which is the key in the pare...
Definition: cdircontent.hxx:88
UPnP resource.
Definition: cdircontent.hxx:39
std::string m_pid
Parent Object Id.
Definition: cdircontent.hxx:120
UPnP Media Server directory entry, converted from XML data.
Definition: cdircontent.hxx:62
std::string f2s(const std::string &nm, bool isresfield)
Simplified interface to retrieving values: we don&#39;t distinguish between non-existing and empty...
Definition: cdircontent.hxx:200
bool getrprop(unsigned int ridx, const std::string &nm, std::string &val) const
Get named resource attribute.
Definition: cdircontent.hxx:186
UPnP Description phase: interpreting the device description which we downloaded from the URL obtained...
Definition: avlastchg.cxx:27
ObjType m_type
Item or container.
Definition: cdircontent.hxx:124