upnpsoap: factor out some duplicated code.

This commit is contained in:
Justin Maggard 2014-06-27 12:59:50 -07:00
parent c226e615ad
commit 95bbedb196

View File

@ -681,6 +681,23 @@ add_res(char *size, char *duration, char *bitrate, char *sampleFrequency,
runtime_vars.port, detailID, ext); runtime_vars.port, detailID, ext);
} }
static int
get_child_count(const char *object)
{
int ret;
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where PARENT_ID = '%s';", object);
return (ret > 0) ? ret : 0;
}
static int
object_exists(const char *object)
{
int ret;
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where OBJECT_ID = '%q'",
strcmp(object, "*") == 0 ? "0" : object);
return (ret > 0);
}
#define COLUMNS "o.REF_ID, o.DETAIL_ID, o.CLASS," \ #define COLUMNS "o.REF_ID, o.DETAIL_ID, o.CLASS," \
" d.SIZE, d.TITLE, d.DURATION, d.BITRATE, d.SAMPLERATE, d.ARTIST," \ " d.SIZE, d.TITLE, d.DURATION, d.BITRATE, d.SAMPLERATE, d.ARTIST," \
" d.ALBUM, d.GENRE, d.COMMENT, d.CHANNELS, d.TRACK, d.DATE, d.RESOLUTION," \ " d.ALBUM, d.GENRE, d.COMMENT, d.CHANNELS, d.TRACK, d.DATE, d.RESOLUTION," \
@ -1037,10 +1054,7 @@ callback(void *args, int argc, char **argv, char **azColName)
ret = strcatf(str, "searchable=\"1\" "); ret = strcatf(str, "searchable=\"1\" ");
} }
if( passed_args->filter & FILTER_CHILDCOUNT ) { if( passed_args->filter & FILTER_CHILDCOUNT ) {
int children; ret = strcatf(str, "childCount=\"%d\"", get_child_count(id));
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where PARENT_ID = '%s';", id);
children = (ret > 0) ? ret : 0;
ret = strcatf(str, "childCount=\"%d\"", children);
} }
/* If the client calls for BrowseMetadata on root, we have to include our "upnp:searchClass"'s, unless they're filtered out */ /* If the client calls for BrowseMetadata on root, we have to include our "upnp:searchClass"'s, unless they're filtered out */
if( passed_args->requested == 1 && strcmp(id, "0") == 0 && (passed_args->filter & FILTER_UPNP_SEARCHCLASS) ) { if( passed_args->requested == 1 && strcmp(id, "0") == 0 && (passed_args->filter & FILTER_UPNP_SEARCHCLASS) ) {
@ -1130,7 +1144,7 @@ BrowseContentDirectory(struct upnphttp * h, const char * action)
char *sql, *ptr; char *sql, *ptr;
struct Response args; struct Response args;
struct string_s str; struct string_s str;
int totalMatches; int totalMatches = 0;
int ret; int ret;
char *ObjectID, *Filter, *BrowseFlag, *SortCriteria; char *ObjectID, *Filter, *BrowseFlag, *SortCriteria;
char *orderBy = NULL; char *orderBy = NULL;
@ -1233,8 +1247,8 @@ BrowseContentDirectory(struct upnphttp * h, const char * action)
} }
else else
{ {
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where PARENT_ID = '%q'", ObjectID); if (!totalMatches)
totalMatches = (ret > 0) ? ret : 0; totalMatches = get_child_count(ObjectID);
ret = 0; ret = 0;
if( SortCriteria ) if( SortCriteria )
{ {
@ -1243,7 +1257,7 @@ BrowseContentDirectory(struct upnphttp * h, const char * action)
#endif #endif
orderBy = parse_sort_criteria(SortCriteria, &ret); orderBy = parse_sort_criteria(SortCriteria, &ret);
} }
else else if (!orderBy)
{ {
if( strncmp(ObjectID, MUSIC_PLIST_ID, strlen(MUSIC_PLIST_ID)) == 0 ) if( strncmp(ObjectID, MUSIC_PLIST_ID, strlen(MUSIC_PLIST_ID)) == 0 )
{ {
@ -1293,8 +1307,7 @@ BrowseContentDirectory(struct upnphttp * h, const char * action)
/* Does the object even exist? */ /* Does the object even exist? */
if( !totalMatches ) if( !totalMatches )
{ {
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where OBJECT_ID = '%q'", ObjectID); if( !object_exists(ObjectID) )
if( ret <= 0 )
{ {
SoapError(h, 701, "No such object error"); SoapError(h, 701, "No such object error");
goto browse_error; goto browse_error;
@ -1687,9 +1700,7 @@ SearchContentDirectory(struct upnphttp * h, const char * action)
/* Does the object even exist? */ /* Does the object even exist? */
if( !totalMatches ) if( !totalMatches )
{ {
ret = sql_get_int_field(db, "SELECT count(*) from OBJECTS where OBJECT_ID = '%q'", if( !object_exists(ContainerID) )
!strcmp(ContainerID, "*")?"0":ContainerID);
if( ret <= 0 )
{ {
SoapError(h, 710, "No such container"); SoapError(h, 710, "No such container");
goto search_error; goto search_error;