Parent: [1301b9] (diff)

Child: [9afb3a] (diff)

Download this file

cdircontent.cxx    251 lines (227 with data), 8.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Copyright (C) 2006-2016 J.F.Dockes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "libupnpp/config.h"
#include <string.h>
#include <unordered_map>
#include <string>
#include <vector>
#include <iostream>
#include "libupnpp/control/cdircontent.hxx"
#include "libupnpp/expatmm.hxx"
#include "libupnpp/log.hxx"
#include "libupnpp/upnpp_p.hxx"
using namespace std;
using namespace UPnPP;
namespace UPnPClient {
string UPnPDirObject::nullstr;
// An XML parser which builds directory contents from DIDL-lite input.
class UPnPDirParser : public inputRefXMLParser {
public:
UPnPDirParser(UPnPDirContent& dir, const string& input)
: inputRefXMLParser(input), m_dir(dir)
{
//LOGDEB("UPnPDirParser: input: " << input << endl);
m_okitems["object.item.audioItem"] = UPnPDirObject::ITC_audioItem;
m_okitems["object.item.audioItem.musicTrack"] =
UPnPDirObject::ITC_audioItem;
m_okitems["object.item.audioItem.audioBroadcast"] =
UPnPDirObject::ITC_audioItem;
m_okitems["object.item.audioItem.audioBook"] =
UPnPDirObject::ITC_audioItem;
m_okitems["object.item.playlistItem"] = UPnPDirObject::ITC_playlist;
m_okitems["object.item.videoItem"] = UPnPDirObject::ITC_videoItem;
}
UPnPDirContent& m_dir;
protected:
class StackEl {
public:
StackEl(const string& nm) : name(nm) {}
string name;
XML_Size sta;
std::unordered_map<string,string> attributes;
string data;
};
virtual void StartElement(const XML_Char *name, const XML_Char **attrs)
{
//LOGDEB("startElement: name [" << name << "]" << " bpos " <<
// XML_GetCurrentByteIndex(expat_parser) << endl);
m_path.push_back(StackEl(name));
m_path.back().sta = XML_GetCurrentByteIndex(expat_parser);
auto& mapattrs = m_path.back().attributes;
mapattrs.clear();
for (int i = 0; attrs[i] != 0; i += 2) {
mapattrs[attrs[i]] = attrs[i+1];
}
switch (name[0]) {
case 'c':
if (!strcmp(name, "container")) {
m_tobj.clear();
m_tobj.m_type = UPnPDirObject::container;
m_tobj.m_id = mapattrs["id"];
m_tobj.m_pid = mapattrs["parentID"];
}
break;
case 'i':
if (!strcmp(name, "item")) {
m_tobj.clear();
m_tobj.m_type = UPnPDirObject::item;
m_tobj.m_id = mapattrs["id"];
m_tobj.m_pid = mapattrs["parentID"];
}
break;
default:
break;
}
}
virtual bool checkobjok()
{
// We used to check id and pid not empty, but this is ok if we
// are parsing a didl fragment sent from a control point. So
// lets all ok and hope for the best.
bool ok = true; /*!m_tobj.m_id.empty() && !m_tobj.m_pid.empty() &&
!m_tobj.m_title.empty();*/
if (ok && m_tobj.m_type == UPnPDirObject::item) {
map<string, UPnPDirObject::ItemClass>::const_iterator it;
it = m_okitems.find(m_tobj.m_props["upnp:class"]);
if (it == m_okitems.end()) {
// Only log this if the record comes from an MS as e.g. naims
// send records with empty classes (and empty id/pid)
if (!m_tobj.m_id.empty()) {
LOGINF("checkobjok: found object of unknown class: [" <<
m_tobj.m_props["upnp:class"] << "]" << endl);
}
m_tobj.m_iclass = UPnPDirObject::ITC_unknown;
} else {
m_tobj.m_iclass = it->second;
}
}
if (!ok) {
LOGINF("checkobjok:skip: id ["<< m_tobj.m_id<<"] pid ["<<
m_tobj.m_pid << "] clss [" << m_tobj.m_props["upnp:class"]
<< "] tt [" << m_tobj.m_title << "]" << endl);
}
return ok;
}
virtual void EndElement(const XML_Char *name)
{
string parentname;
if (m_path.size() == 1) {
parentname = "root";
} else {
parentname = m_path[m_path.size()-2].name;
}
//LOGDEB("Closing element " << name << " inside element " <<
// parentname << " data " << m_path.back().data << endl);
if (!strcmp(name, "container")) {
if (checkobjok()) {
m_dir.m_containers.push_back(m_tobj);
}
} else if (!strcmp(name, "item")) {
if (checkobjok()) {
unsigned int len = XML_GetCurrentByteIndex(expat_parser) -
m_path.back().sta;
m_tobj.m_didlfrag = m_input.substr(m_path.back().sta, len)
+ "</item></DIDL-Lite>";
m_dir.m_items.push_back(m_tobj);
}
} else if (!parentname.compare("item") ||
!parentname.compare("container")) {
switch (name[0]) {
case 'd':
if (!strcmp(name, "dc:title")) {
m_tobj.m_title = m_path.back().data;
} else {
addprop(name, m_path.back().data);
}
break;
case 'r':
if (!strcmp(name, "res")) {
// <res protocolInfo="http-get:*:audio/mpeg:*" size="517149"
// bitrate="24576" duration="00:03:35"
// sampleFrequency="44100" nrAudioChannels="2">
UPnPResource res;
res.m_uri = m_path.back().data;
for (auto it : m_path.back().attributes) {
res.m_props[it.first] = it.second;
}
m_tobj.m_resources.push_back(res);
} else {
addprop(name, m_path.back().data);
}
break;
default:
addprop(name, m_path.back().data);
break;
}
}
m_path.pop_back();
}
virtual void CharacterData(const XML_Char *s, int len)
{
if (s == 0 || *s == 0)
return;
string str(s, len);
m_path.back().data += str;
}
private:
vector<StackEl> m_path;
UPnPDirObject m_tobj;
map<string, UPnPDirObject::ItemClass> m_okitems;
void addprop(const string& nm, const string& data) {
// e.g <upnp:artist role="AlbumArtist">Jojo</upnp:artist>
auto& mapattrs = m_path.back().attributes;
auto roleit = mapattrs.find("role");
string rolevalue;
if (roleit != mapattrs.end()) {
if (roleit->second.compare("AlbumArtist")) {
// AlbumArtist is not useful for the user
rolevalue = string(" (") + roleit->second + string(")");
}
}
auto it = m_tobj.m_props.find(nm);
if (it == m_tobj.m_props.end()) {
m_tobj.m_props[nm] = data + rolevalue;
} else {
// Only add the value if it's not already there. Actually,
// to do this properly we'd need to only build the string
// at the end when we have all the entries
string &current(it->second);
if (current.compare(data)) {
current += string(", ") + data + rolevalue;
}
}
}
};
bool UPnPDirContent::parse(const std::string& input)
{
UPnPDirParser parser(*this, input);
return parser.Parse();
}
static const string didl_header(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<DIDL-Lite xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\""
" xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
" xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\""
" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\">");
// Maybe we'll do something about building didl from scratch if this
// proves necessary.
string UPnPDirObject::getdidl() const
{
return didl_header + m_didlfrag;
}
} // namespace