From c8245740c3a8699cfe7d7e5aa0d427b931018ad5 Mon Sep 17 00:00:00 2001
From: Yusuke Yanbe <y.yanbe@gmail.com>
Date: Mon, 4 Jul 2016 08:20:46 +0900
Subject: [PATCH] Add support for grouping videos by directory for Panasonic
 TVs

---
 upnpsoap.c | 24 +++++++++++++++++++-----
 upnpsoap.h |  5 ++++-
 2 files changed, 23 insertions(+), 6 deletions(-)

--- a/upnpsoap.c
+++ b/upnpsoap.c
@@ -1052,6 +1052,15 @@ callback(void *args, int argc, char **ar
 						add_res(size, duration, bitrate, sampleFrequency, nrAudioChannels,
 						        resolution, dlna_buf, mime, detailID, ext, passed_args);
 					}
+
+					DPRINTF(E_DEBUG, L_HTTP, "Title: %s -  %d Returned / %d childCount\n",
+								title, passed_args->returned, passed_args->childCount);
+
+					ret = strcatf(str, "&lt;pxn:groupTopFlag&gt;%d&lt;/pxn:groupTopFlag&gt;"
+							   "&lt;pxn:groupID&gt;%s&lt;/pxn:groupID&gt;"
+							   "&lt;pxn:groupTitle&gt;%s&lt;/pxn:groupTitle&gt;"
+							   "&lt;pxn:groupMemberNum&gt;%d&lt;/pxn:groupMemberNum&gt;",
+							   passed_args->returned == passed_args->childCount, parent, passed_args->parentTitle, passed_args->childCount);
 					break;
 				case ESamsungSeriesCDE:
 				case ELGDevice:
@@ -1314,8 +1323,11 @@ BrowseContentDirectory(struct upnphttp *
 		if (!where[0])
 			sqlite3_snprintf(sizeof(where), where, "PARENT_ID = '%q'", ObjectID);
 
-		if (!totalMatches)
+		if (!totalMatches) {
 			totalMatches = get_child_count(ObjectID, magic);
+			args.childCount = totalMatches;
+			args.parentTitle = sql_get_text_field(db, "SELECT d.TITLE FROM OBJECTS o LEFT JOIN DETAILS d ON (d.ID = o.DETAIL_ID) WHERE OBJECT_ID='%q'", ObjectID);
+		}
 		ret = 0;
 		if (SortCriteria && !orderBy)
 		{
@@ -1354,14 +1366,16 @@ BrowseContentDirectory(struct upnphttp *
 			SoapError(h, 709, "Unsupported or invalid sort criteria");
 			goto browse_error;
 		}
-
 		sql = sqlite3_mprintf("SELECT %s, %s, %s, " COLUMNS
 		                      "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
-				      " where %s %s limit %d, %d;",
+				      " where %s order by d.date limit %d, %d;",
 				      objectid_sql, parentid_sql, refid_sql,
-				      where, THISORNUL(orderBy), StartingIndex, RequestedCount);
+				      where, StartingIndex, RequestedCount);
 		DPRINTF(E_DEBUG, L_HTTP, "Browse SQL: %s\n", sql);
 		ret = sqlite3_exec(db, sql, callback, (void *) &args, &zErrMsg);
+		if (args.parentTitle) {
+			sqlite3_free(args.parentTitle);
+		}
 	}
 	if( (ret != SQLITE_OK) && (zErrMsg != NULL) )
 	{
@@ -1795,7 +1809,7 @@ SearchContentDirectory(struct upnphttp *
 	                      (*ContainerID == '*') ? NULL :
 	                      sqlite3_mprintf("UNION ALL " SELECT_COLUMNS
 	                                      "from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)"
-	                                      " where OBJECT_ID = '%q' and (%s) ", ContainerID, where),
+	                                      " where OBJECT_ID = '%q' and (%s) ORDER BY d.date", ContainerID, where),
 	                      orderBy, StartingIndex, RequestedCount);
 	DPRINTF(E_DEBUG, L_HTTP, "Search SQL: %s\n", sql);
 	ret = sqlite3_exec(db, sql, callback, (void *) &args, &zErrMsg);
--- a/upnpsoap.h
+++ b/upnpsoap.h
@@ -27,7 +27,8 @@
 #define CONTENT_DIRECTORY_SCHEMAS \
 	" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"" \
 	" xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\"" \
-	" xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\""
+	" xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\"" \
+	" xmlns:pxn=\"urn:schemas-panasonic-com:pxn\""
 #define DLNA_NAMESPACE \
 	" xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\""
 #define PV_NAMESPACE \
@@ -39,6 +40,8 @@ struct Response
 	int start;
 	int returned;
 	int requested;
+	int childCount;
+	char *parentTitle;
 	int iface;
 	uint32_t filter;
 	uint32_t flags;
