Commit 09b00fa4 authored by Max Kellermann's avatar Max Kellermann

db/upnp/Object: use strictly-typed enums

At the same time, rename the enum types and the class attributes, and add an "UNKNOWN" type/class. The latter avoids the "-1" hack.
parent 74842fd6
...@@ -721,18 +721,27 @@ UpnpDatabase::VisitServer(ContentDirectoryService* server, ...@@ -721,18 +721,27 @@ UpnpDatabase::VisitServer(ContentDirectoryService* server,
return SearchSongs(server, objid.c_str(), selection, return SearchSongs(server, objid.c_str(), selection,
visit_song, error); visit_song, error);
if (tdirent.m_type == UPnPDirObject::item) { if (tdirent.type == UPnPDirObject::Type::ITEM) {
// Target is a song. Not too sure we ever get there actually, maybe // Target is a song. Not too sure we ever get there actually, maybe
// this is always catched by the special uri test above. // this is always catched by the special uri test above.
if (visit_song && switch (tdirent.item_class) {
tdirent.m_iclass == UPnPDirObject::audioItem_musicTrack) { case UPnPDirObject::ItemClass::MUSIC:
return visitSong(tdirent, "", selection, visit_song, if (visit_song)
error); return visitSong(tdirent, "", selection, visit_song,
} else if (visit_playlist && error);
tdirent.m_iclass == UPnPDirObject::audioItem_playlist) { break;
// Note: I've yet to see a playlist item (playlists
// seem to be usually handled as containers, so I'll case UPnPDirObject::ItemClass::PLAYLIST:
// decide what to do when I see one... if (visit_playlist) {
/* Note: I've yet to see a playlist
item (playlists seem to be usually
handled as containers, so I'll decide
what to do when I see one... */
}
break;
case UPnPDirObject::ItemClass::UNKNOWN:
break;
} }
return true; return true;
...@@ -757,28 +766,40 @@ UpnpDatabase::VisitServer(ContentDirectoryService* server, ...@@ -757,28 +766,40 @@ UpnpDatabase::VisitServer(ContentDirectoryService* server,
if (visit_song || visit_playlist) { if (visit_song || visit_playlist) {
for (const auto &dirent : dirbuf.m_items) { for (const auto &dirent : dirbuf.m_items) {
if (visit_song && switch (dirent.item_class) {
dirent.m_iclass == UPnPDirObject::audioItem_musicTrack) { case UPnPDirObject::ItemClass::MUSIC:
/* We identify songs by giving them a if (visit_song) {
special path. The Id is enough to /* We identify songs by giving
fetch them from the server them a special path. The Id
anyway. */ is enough to fetch them
from the server anyway. */
std::string p;
if (!selection.recursive) std::string p;
p = selection.uri + "/" + if (!selection.recursive)
titleToPathElt(dirent.m_title); p = selection.uri + "/" +
titleToPathElt(dirent.m_title);
if (!visitSong(dirent, p.c_str(),
selection, visit_song, error)) if (!visitSong(dirent, p.c_str(),
return false; selection, visit_song, error))
} else if (visit_playlist && return false;
dirent.m_iclass == UPnPDirObject::audioItem_playlist) { }
/* Note: I've yet to see a playlist
item (playlists seem to be usually break;
handled as containers, so I'll
decide what to do when I see case UPnPDirObject::ItemClass::PLAYLIST:
one... */ if (visit_playlist) {
/* Note: I've yet to see a
playlist item (playlists
seem to be usually handled
as containers, so I'll
decide what to do when I
see one... */
}
break;
case UPnPDirObject::ItemClass::UNKNOWN:
break;
} }
} }
} }
......
...@@ -56,9 +56,9 @@ public: ...@@ -56,9 +56,9 @@ public:
:m_dir(dir) :m_dir(dir)
{ {
m_okitems["object.item.audioItem.musicTrack"] = m_okitems["object.item.audioItem.musicTrack"] =
UPnPDirObject::audioItem_musicTrack; UPnPDirObject::ItemClass::MUSIC;
m_okitems["object.item.playlistItem"] = m_okitems["object.item.playlistItem"] =
UPnPDirObject::audioItem_playlist; UPnPDirObject::ItemClass::PLAYLIST;
} }
UPnPDirContent& m_dir; UPnPDirContent& m_dir;
...@@ -74,7 +74,7 @@ protected: ...@@ -74,7 +74,7 @@ protected:
case 'c': case 'c':
if (!strcmp(name, "container")) { if (!strcmp(name, "container")) {
m_tobj.clear(); m_tobj.clear();
m_tobj.m_type = UPnPDirObject::container; m_tobj.type = UPnPDirObject::Type::CONTAINER;
m_tobj.m_id = m_path.back().attributes["id"]; m_tobj.m_id = m_path.back().attributes["id"];
m_tobj.m_pid = m_path.back().attributes["parentID"]; m_tobj.m_pid = m_path.back().attributes["parentID"];
} }
...@@ -82,7 +82,7 @@ protected: ...@@ -82,7 +82,7 @@ protected:
case 'i': case 'i':
if (!strcmp(name, "item")) { if (!strcmp(name, "item")) {
m_tobj.clear(); m_tobj.clear();
m_tobj.m_type = UPnPDirObject::item; m_tobj.type = UPnPDirObject::Type::ITEM;
m_tobj.m_id = m_path.back().attributes["id"]; m_tobj.m_id = m_path.back().attributes["id"];
m_tobj.m_pid = m_path.back().attributes["parentID"]; m_tobj.m_pid = m_path.back().attributes["parentID"];
} }
...@@ -96,14 +96,14 @@ protected: ...@@ -96,14 +96,14 @@ protected:
bool ok = !m_tobj.m_id.empty() && !m_tobj.m_pid.empty() && bool ok = !m_tobj.m_id.empty() && !m_tobj.m_pid.empty() &&
!m_tobj.m_title.empty(); !m_tobj.m_title.empty();
if (ok && m_tobj.m_type == UPnPDirObject::item) { if (ok && m_tobj.type == UPnPDirObject::Type::ITEM) {
auto it = m_okitems.find(m_tobj.m_props["upnp:class"]); auto it = m_okitems.find(m_tobj.m_props["upnp:class"]);
if (it == m_okitems.end()) { if (it == m_okitems.end()) {
PLOGINF("checkobjok: found object of unknown class: [%s]\n", PLOGINF("checkobjok: found object of unknown class: [%s]\n",
m_tobj.m_props["upnp:class"].c_str()); m_tobj.m_props["upnp:class"].c_str());
ok = false; ok = false;
} else { } else {
m_tobj.m_iclass = it->second; m_tobj.item_class = it->second;
} }
} }
......
...@@ -30,7 +30,12 @@ ...@@ -30,7 +30,12 @@
*/ */
class UPnPDirObject { class UPnPDirObject {
public: public:
enum ObjType {item, container}; enum class Type {
UNKNOWN,
ITEM,
CONTAINER,
};
// There are actually several kinds of containers: // There are actually several kinds of containers:
// object.container.storageFolder, object.container.person, // object.container.storageFolder, object.container.person,
// object.container.playlistContainer etc., but they all seem to // object.container.playlistContainer etc., but they all seem to
...@@ -38,13 +43,17 @@ public: ...@@ -38,13 +43,17 @@ public:
// items are special to us, and so should playlists, but I've not // items are special to us, and so should playlists, but I've not
// seen one of the latter yet (servers seem to use containers for // seen one of the latter yet (servers seem to use containers for
// playlists). // playlists).
enum ItemClass {audioItem_musicTrack, audioItem_playlist}; enum class ItemClass {
UNKNOWN,
MUSIC,
PLAYLIST,
};
std::string m_id; // ObjectId std::string m_id; // ObjectId
std::string m_pid; // Parent ObjectId std::string m_pid; // Parent ObjectId
std::string m_title; // dc:title. Directory name for a container. std::string m_title; // dc:title. Directory name for a container.
ObjType m_type; // item or container Type type;
ItemClass m_iclass; ItemClass item_class;
// Properties as gathered from the XML document (url, artist, etc.) // Properties as gathered from the XML document (url, artist, etc.)
// The map keys are the XML tag or attribute names. // The map keys are the XML tag or attribute names.
std::map<std::string, std::string> m_props; std::map<std::string, std::string> m_props;
...@@ -68,8 +77,8 @@ public: ...@@ -68,8 +77,8 @@ public:
m_id.clear(); m_id.clear();
m_pid.clear(); m_pid.clear();
m_title.clear(); m_title.clear();
m_type = (ObjType)-1; type = Type::UNKNOWN;
m_iclass = (ItemClass)-1; item_class = ItemClass::UNKNOWN;
m_props.clear(); m_props.clear();
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment