libupnpp  0.16.0
A C++ wrapper for the Portable UPnP reference library
cdircontent.hxx
1 /* Copyright (C) 2006-2016 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 
27 #include "libupnpp/upnpavutils.hxx"
28 
29 namespace UPnPClient {
30 
38 class UPnPResource {
39 public:
41  std::string m_uri;
42 
44  std::map<std::string, std::string> m_props;
45 
48  const auto it = m_props.find("protocolInfo");
49  if (it == m_props.end()) {
50  return false;
51  }
52  return UPnPP::parseProtoInfEntry(it->second, e);
53  }
54 };
55 
56 
63 public:
64  enum ObjType {item, container};
65  // There are actually several kinds of containers:
66  // object.container.storageFolder, object.container.person,
67  // object.container.playlistContainer etc., but they all seem to
68  // behave the same as far as we're concerned. Otoh, musicTrack
69  // items are special to us, and so should playlists, but I've not
70  // 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  std::string m_id;
82  std::string m_pid;
84  std::string m_title;
86  ObjType m_type;
88  ItemClass m_iclass;
89 
96  std::map<std::string, std::string> m_props;
97 
101  std::vector<UPnPResource> m_resources;
102 
109  bool getprop(const std::string& name, std::string& value) const
110  {
111  std::map<std::string, std::string>::const_iterator it =
112  m_props.find(name);
113  if (it == m_props.end())
114  return false;
115  value = it->second;
116  return true;
117  }
123  const std::string& getprop(const std::string& name) const
124  {
125  std::map<std::string, std::string>::const_iterator it =
126  m_props.find(name);
127  if (it == m_props.end())
128  return nullstr;
129  return it->second;
130  }
131 
140  bool getrprop(unsigned int ridx, const std::string& nm, std::string& val)
141  const
142  {
143  if (ridx >= m_resources.size())
144  return false;
145  std::map<std::string, std::string>::const_iterator it =
146  m_resources[ridx].m_props.find(nm);
147  if (it == m_resources[ridx].m_props.end())
148  return false;
149  val = it->second;
150  return true;
151 
152  }
153 
157  std::string f2s(const std::string& nm, bool isresfield) {
158  std::string val;
159  if (isresfield) {
160  getrprop(0, nm, val);
161  } else {
162  getprop(nm, val);
163  }
164  return val;
165  }
166 
170  int getDurationSeconds(unsigned ridx = 0) const
171  {
172  std::string sdur;
173  if (!getrprop(ridx, "duration", sdur)) {
174  //?? Avoid returning 0, who knows...
175  return 1;
176  }
177  return UPnPP::upnpdurationtos(sdur);
178  }
179 
186  std::string getdidl() const;
187 
188  void clear()
189  {
190  m_id.clear();
191  m_pid.clear();
192  m_title.clear();
193  m_type = (ObjType)-1;
194  m_iclass = (ItemClass)-1;
195  m_props.clear();
196  m_resources.clear();
197  m_didlfrag.clear();
198  }
199 
200  std::string dump() const
201  {
202  std::ostringstream os;
203  os << "UPnPDirObject: " << (m_type == item ? "item" : "container") <<
204  " id [" << m_id << "] pid [" << m_pid <<
205  "] title [" << m_title << "]" << std::endl;
206  os << "Properties: " << std::endl;
207  for (std::map<std::string,std::string>::const_iterator it =
208  m_props.begin();
209  it != m_props.end(); it++) {
210  os << "[" << it->first << "]->[" << it->second << "] "
211  << std::endl;
212  }
213  os << "Resources:" << std::endl;
214  for (std::vector<UPnPResource>::const_iterator it =
215  m_resources.begin(); it != m_resources.end(); it++) {
216  os << " Uri [" << it->m_uri << "]" << std::endl;
217  os << " Resource attributes:" << std::endl;
218  for (std::map<std::string, std::string>::const_iterator it1 =
219  it->m_props.begin();
220  it1 != it->m_props.end(); it1++) {
221  os << " [" << it1->first << "]->[" << it1->second << "] "
222  << std::endl;
223  }
224  }
225  os << std::endl;
226  return os.str();
227  }
228 
229 private:
230  friend class UPnPDirParser;
231  // didl text for element, sans header
232  std::string m_didlfrag;
233  static std::string nullstr;
234 };
235 
241 public:
242  std::vector<UPnPDirObject> m_containers;
243  std::vector<UPnPDirObject> m_items;
244 
245  void clear()
246  {
247  m_containers.clear();
248  m_items.clear();
249  }
250 
264  bool parse(const std::string& didltext);
265 };
266 
267 } // namespace
268 
269 #endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */
Definition: cdircontent.cxx:40
ItemClass m_iclass
Item type details.
Definition: cdircontent.hxx:88
const std::string & getprop(const std::string &name) const
Get named property.
Definition: cdircontent.hxx:123
std::string m_uri
URI Value.
Definition: cdircontent.hxx:41
std::string m_title
Value of dc:title.
Definition: cdircontent.hxx:84
std::vector< UPnPResource > m_resources
Resources: there may be several, for example for different audio formats of the same track...
Definition: cdircontent.hxx:101
bool protoInfo(UPnPP::ProtocolinfoEntry &e) const
Get the protocolinfo attribute in cooked form.
Definition: cdircontent.hxx:47
bool getprop(const std::string &name, std::string &value) const
Get named property.
Definition: cdircontent.hxx:109
std::map< std::string, std::string > m_props
Properties as gathered from the XML document (album, artist, etc.), The map keys are the XML tag name...
Definition: cdircontent.hxx:96
Decoded protocolinfo entry data.
Definition: upnpavutils.hxx:37
int getDurationSeconds(unsigned ridx=0) const
Return duration in seconds.
Definition: cdircontent.hxx:170
std::string m_id
Object Id.
Definition: cdircontent.hxx:80
std::map< std::string, std::string > m_props
Resource attributes.
Definition: cdircontent.hxx:44
Image of a MediaServer Directory Service container (directory), possibly containing items and subordi...
Definition: cdircontent.hxx:240
UPnP resource.
Definition: cdircontent.hxx:38
std::string m_pid
Parent Object Id.
Definition: cdircontent.hxx:82
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:157
bool getrprop(unsigned int ridx, const std::string &nm, std::string &val) const
Get named resource attribute.
Definition: cdircontent.hxx:140
UPnP Description phase: interpreting the device description which we downloaded from the URL obtained...
Definition: avlastchg.cxx:28
ObjType m_type
Item or container.
Definition: cdircontent.hxx:86