2014-04-10 17:06:54 +02:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
2014-05-01 05:24:55 +02:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
2014-05-21 02:56:24 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-04-16 07:08:12 +02:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-08-07 04:51:09 +02:00
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
2014-04-23 18:29:21 +02:00
|
|
|
|
using MediaBrowser.Dlna.Didl;
|
2014-05-21 02:56:24 +02:00
|
|
|
|
using MediaBrowser.Dlna.Server;
|
|
|
|
|
using MediaBrowser.Dlna.Service;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
using MediaBrowser.Model.Channels;
|
2014-04-16 07:08:12 +02:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
using MediaBrowser.Model.Library;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-04-16 07:08:12 +02:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
using System.Text;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
using System.Threading;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
using System.Xml;
|
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
namespace MediaBrowser.Dlna.ContentDirectory
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-05-21 02:56:24 +02:00
|
|
|
|
public class ControlHandler : BaseControlHandler
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-04-16 07:08:12 +02:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
private readonly User _user;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
|
2014-04-10 17:06:54 +02:00
|
|
|
|
private const string NS_DC = "http://purl.org/dc/elements/1.1/";
|
|
|
|
|
private const string NS_DIDL = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
|
|
|
|
|
private const string NS_DLNA = "urn:schemas-dlna-org:metadata-1-0/";
|
|
|
|
|
private const string NS_UPNP = "urn:schemas-upnp-org:metadata-1-0/upnp/";
|
|
|
|
|
|
2014-04-21 18:02:30 +02:00
|
|
|
|
private readonly int _systemUpdateId;
|
2014-04-16 07:08:12 +02:00
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
2014-04-23 18:29:21 +02:00
|
|
|
|
private readonly DidlBuilder _didlBuilder;
|
|
|
|
|
|
2014-04-24 07:08:10 +02:00
|
|
|
|
private readonly DeviceProfile _profile;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
private readonly IUserViewManager _userViewManager;
|
|
|
|
|
private readonly IChannelManager _channelManager;
|
2014-04-24 07:08:10 +02:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
public ControlHandler(ILogger logger, ILibraryManager libraryManager, DeviceProfile profile, string serverAddress, IImageProcessor imageProcessor, IUserDataManager userDataManager, User user, int systemUpdateId, IServerConfigurationManager config, IUserViewManager userViewManager, IChannelManager channelManager)
|
2014-05-21 02:56:24 +02:00
|
|
|
|
: base(config, logger)
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-04-16 07:08:12 +02:00
|
|
|
|
_libraryManager = libraryManager;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
_userDataManager = userDataManager;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
_user = user;
|
2014-04-21 18:02:30 +02:00
|
|
|
|
_systemUpdateId = systemUpdateId;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
_userViewManager = userViewManager;
|
|
|
|
|
_channelManager = channelManager;
|
2014-04-24 07:08:10 +02:00
|
|
|
|
_profile = profile;
|
2014-04-23 18:29:21 +02:00
|
|
|
|
|
2014-06-14 20:24:20 +02:00
|
|
|
|
_didlBuilder = new DidlBuilder(profile, user, imageProcessor, serverAddress);
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
protected override IEnumerable<KeyValuePair<string, string>> GetResult(string methodName, Headers methodParams)
|
2014-04-16 07:08:12 +02:00
|
|
|
|
{
|
2014-05-21 02:56:24 +02:00
|
|
|
|
var deviceId = "test";
|
2014-05-01 05:24:55 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
var user = _user;
|
2014-05-01 05:24:55 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
if (string.Equals(methodName, "GetSearchCapabilities", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
return HandleGetSearchCapabilities();
|
2014-05-01 05:24:55 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
if (string.Equals(methodName, "GetSortCapabilities", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
return HandleGetSortCapabilities();
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
if (string.Equals(methodName, "GetSystemUpdateID", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
return HandleGetSystemUpdateID();
|
2014-04-18 19:16:25 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
if (string.Equals(methodName, "Browse", StringComparison.OrdinalIgnoreCase))
|
2014-07-30 05:31:35 +02:00
|
|
|
|
return HandleBrowse(methodParams, user, deviceId).Result;
|
2014-04-16 07:08:12 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
if (string.Equals(methodName, "X_GetFeatureList", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
return HandleXGetFeatureList();
|
2014-04-16 07:08:12 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
if (string.Equals(methodName, "X_SetBookmark", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
return HandleXSetBookmark(methodParams, user);
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
if (string.Equals(methodName, "Search", StringComparison.OrdinalIgnoreCase))
|
2014-07-30 05:31:35 +02:00
|
|
|
|
return HandleSearch(methodParams, user, deviceId).Result;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
throw new ResourceNotFoundException("Unexpected control request name: " + methodName);
|
2014-04-16 07:08:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<KeyValuePair<string, string>> HandleXSetBookmark(IDictionary<string, string> sparams, User user)
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
|
|
|
|
var id = sparams["ObjectID"];
|
2014-04-16 07:08:12 +02:00
|
|
|
|
|
2014-04-19 19:43:12 +02:00
|
|
|
|
var item = GetItemFromObjectId(id, user);
|
2014-04-18 07:03:01 +02:00
|
|
|
|
|
|
|
|
|
var newbookmark = int.Parse(sparams["PosSecond"], _usCulture);
|
|
|
|
|
|
|
|
|
|
var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
|
|
|
|
|
|
|
|
|
|
userdata.PlaybackPositionTicks = TimeSpan.FromSeconds(newbookmark).Ticks;
|
|
|
|
|
|
|
|
|
|
_userDataManager.SaveUserData(user.Id, item, userdata, UserDataSaveReason.TogglePlayed,
|
|
|
|
|
CancellationToken.None);
|
2014-04-16 07:08:12 +02:00
|
|
|
|
|
2014-04-10 17:06:54 +02:00
|
|
|
|
return new Headers();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 07:08:12 +02:00
|
|
|
|
private IEnumerable<KeyValuePair<string, string>> HandleGetSearchCapabilities()
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-08-17 07:38:13 +02:00
|
|
|
|
return new Headers(true) { { "SearchCaps", "res@resolution,res@size,res@duration,dc:title,dc:creator,upnp:actor,upnp:artist,upnp:genre,upnp:album,dc:date,upnp:class,@id,@refID,@protocolInfo,upnp:author,dc:description,pv:avKeywords" } };
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 07:08:12 +02:00
|
|
|
|
private IEnumerable<KeyValuePair<string, string>> HandleGetSortCapabilities()
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-08-17 07:38:13 +02:00
|
|
|
|
return new Headers(true) { { "SortCaps", "res@duration,res@size,res@bitrate,dc:date,dc:title,dc:size,upnp:album,upnp:artist,upnp:albumArtist,upnp:episodeNumber,upnp:genre,upnp:originalTrackNumber,upnp:rating" } };
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 07:08:12 +02:00
|
|
|
|
private IEnumerable<KeyValuePair<string, string>> HandleGetSystemUpdateID()
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-05-12 18:47:22 +02:00
|
|
|
|
var headers = new Headers(true);
|
|
|
|
|
headers.Add("Id", _systemUpdateId.ToString(_usCulture));
|
2014-05-21 02:56:24 +02:00
|
|
|
|
return headers;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 07:08:12 +02:00
|
|
|
|
private IEnumerable<KeyValuePair<string, string>> HandleXGetFeatureList()
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-06-22 07:52:31 +02:00
|
|
|
|
return new Headers(true) { { "FeatureList", GetFeatureListXml() } };
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetFeatureListXml()
|
|
|
|
|
{
|
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
builder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
|
|
|
|
builder.Append("<Features xmlns=\"urn:schemas-upnp-org:av:avs\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:schemas-upnp-org:av:avs http://www.upnp.org/schemas/av/avs.xsd\">");
|
|
|
|
|
|
|
|
|
|
builder.Append("<Feature name=\"samsung.com_BASICVIEW\" version=\"1\">");
|
|
|
|
|
builder.Append("<container id=\"I\" type=\"object.item.imageItem\"/>");
|
|
|
|
|
builder.Append("<container id=\"A\" type=\"object.item.audioItem\"/>");
|
|
|
|
|
builder.Append("<container id=\"V\" type=\"object.item.videoItem\"/>");
|
|
|
|
|
builder.Append("</Feature>");
|
|
|
|
|
|
|
|
|
|
builder.Append("</Features>");
|
|
|
|
|
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
private async Task<IEnumerable<KeyValuePair<string, string>>> HandleBrowse(Headers sparams, User user, string deviceId)
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
|
|
|
|
var id = sparams["ObjectID"];
|
|
|
|
|
var flag = sparams["BrowseFlag"];
|
2014-04-20 07:21:08 +02:00
|
|
|
|
var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
|
|
|
|
|
var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));
|
2014-04-10 17:06:54 +02:00
|
|
|
|
|
|
|
|
|
var provided = 0;
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
int? requested = 0;
|
|
|
|
|
int? start = 0;
|
|
|
|
|
|
|
|
|
|
int requestedVal;
|
|
|
|
|
if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requestedVal) && requestedVal > 0)
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-07-30 05:31:35 +02:00
|
|
|
|
requested = requestedVal;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
|
|
|
|
int startVal;
|
|
|
|
|
if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out startVal) && startVal > 0)
|
2014-04-10 17:06:54 +02:00
|
|
|
|
{
|
2014-07-30 05:31:35 +02:00
|
|
|
|
start = startVal;
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//var root = GetItem(id) as IMediaFolder;
|
|
|
|
|
var result = new XmlDocument();
|
|
|
|
|
|
|
|
|
|
var didl = result.CreateElement(string.Empty, "DIDL-Lite", NS_DIDL);
|
|
|
|
|
didl.SetAttribute("xmlns:dc", NS_DC);
|
|
|
|
|
didl.SetAttribute("xmlns:dlna", NS_DLNA);
|
|
|
|
|
didl.SetAttribute("xmlns:upnp", NS_UPNP);
|
2014-04-22 19:25:54 +02:00
|
|
|
|
//didl.SetAttribute("xmlns:sec", NS_SEC);
|
2014-04-10 17:06:54 +02:00
|
|
|
|
result.AppendChild(didl);
|
|
|
|
|
|
2014-08-06 04:26:12 +02:00
|
|
|
|
var item = GetItemFromObjectId(id, user);
|
2014-04-16 07:08:12 +02:00
|
|
|
|
|
2014-08-06 04:26:12 +02:00
|
|
|
|
var totalCount = 0;
|
2014-04-21 03:36:12 +02:00
|
|
|
|
|
2014-04-16 07:08:12 +02:00
|
|
|
|
if (string.Equals(flag, "BrowseMetadata"))
|
|
|
|
|
{
|
2014-08-30 19:49:54 +02:00
|
|
|
|
|
|
|
|
|
|
2014-08-06 04:26:12 +02:00
|
|
|
|
var folder = item as Folder;
|
|
|
|
|
|
|
|
|
|
if (folder == null)
|
|
|
|
|
{
|
|
|
|
|
result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, item, deviceId, filter));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-08-30 19:49:54 +02:00
|
|
|
|
|
|
|
|
|
|
2014-08-06 04:26:12 +02:00
|
|
|
|
var childrenResult = (await GetChildrenSorted(folder, user, sortCriteria, start, requested).ConfigureAwait(false));
|
|
|
|
|
totalCount = childrenResult.TotalRecordCount;
|
|
|
|
|
|
2014-08-30 19:49:54 +02:00
|
|
|
|
result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, folder, totalCount, filter, id));
|
2014-08-06 04:26:12 +02:00
|
|
|
|
}
|
2014-04-16 07:08:12 +02:00
|
|
|
|
provided++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-08-06 04:26:12 +02:00
|
|
|
|
var folder = (Folder)item;
|
|
|
|
|
|
|
|
|
|
var childrenResult = (await GetChildrenSorted(folder, user, sortCriteria, start, requested).ConfigureAwait(false));
|
|
|
|
|
totalCount = childrenResult.TotalRecordCount;
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
provided = childrenResult.Items.Length;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
foreach (var i in childrenResult.Items)
|
2014-04-16 07:08:12 +02:00
|
|
|
|
{
|
2014-04-18 07:03:01 +02:00
|
|
|
|
if (i.IsFolder)
|
2014-04-16 07:08:12 +02:00
|
|
|
|
{
|
2014-04-18 07:03:01 +02:00
|
|
|
|
var f = (Folder)i;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
var childCount = (await GetChildrenSorted(f, user, sortCriteria, null, 0).ConfigureAwait(false))
|
|
|
|
|
.TotalRecordCount;
|
2014-04-16 07:08:12 +02:00
|
|
|
|
|
2014-04-23 18:29:21 +02:00
|
|
|
|
result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, f, childCount, filter));
|
2014-04-18 07:03:01 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-04-23 18:29:21 +02:00
|
|
|
|
result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, i, deviceId, filter));
|
2014-04-16 07:08:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var resXML = result.OuterXml;
|
|
|
|
|
|
|
|
|
|
return new List<KeyValuePair<string, string>>
|
|
|
|
|
{
|
|
|
|
|
new KeyValuePair<string,string>("Result", resXML),
|
|
|
|
|
new KeyValuePair<string,string>("NumberReturned", provided.ToString(_usCulture)),
|
2014-08-31 19:10:31 +02:00
|
|
|
|
new KeyValuePair<string,string>("TotalMatches", totalCount.ToString(_usCulture)),
|
2014-04-21 18:02:30 +02:00
|
|
|
|
new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
|
2014-04-16 07:08:12 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
private async Task<IEnumerable<KeyValuePair<string, string>>> HandleSearch(Headers sparams, User user, string deviceId)
|
2014-04-18 07:03:01 +02:00
|
|
|
|
{
|
2014-04-20 07:21:08 +02:00
|
|
|
|
var searchCriteria = new SearchCriteria(sparams.GetValueOrDefault("SearchCriteria", ""));
|
|
|
|
|
var sortCriteria = new SortCriteria(sparams.GetValueOrDefault("SortCriteria", ""));
|
|
|
|
|
var filter = new Filter(sparams.GetValueOrDefault("Filter", "*"));
|
|
|
|
|
|
|
|
|
|
// sort example: dc:title, dc:date
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
int? requested = 0;
|
|
|
|
|
int? start = 0;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
int requestedVal;
|
|
|
|
|
if (sparams.ContainsKey("RequestedCount") && int.TryParse(sparams["RequestedCount"], out requestedVal) && requestedVal > 0)
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
2014-07-30 05:31:35 +02:00
|
|
|
|
requested = requestedVal;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
|
|
|
|
int startVal;
|
|
|
|
|
if (sparams.ContainsKey("StartingIndex") && int.TryParse(sparams["StartingIndex"], out startVal) && startVal > 0)
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
2014-07-30 05:31:35 +02:00
|
|
|
|
start = startVal;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//var root = GetItem(id) as IMediaFolder;
|
|
|
|
|
var result = new XmlDocument();
|
|
|
|
|
|
|
|
|
|
var didl = result.CreateElement(string.Empty, "DIDL-Lite", NS_DIDL);
|
|
|
|
|
didl.SetAttribute("xmlns:dc", NS_DC);
|
|
|
|
|
didl.SetAttribute("xmlns:dlna", NS_DLNA);
|
|
|
|
|
didl.SetAttribute("xmlns:upnp", NS_UPNP);
|
2014-04-24 07:08:10 +02:00
|
|
|
|
|
2014-04-24 16:11:05 +02:00
|
|
|
|
foreach (var att in _profile.XmlRootAttributes)
|
2014-04-24 07:08:10 +02:00
|
|
|
|
{
|
|
|
|
|
didl.SetAttribute(att.Name, att.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-20 07:21:08 +02:00
|
|
|
|
result.AppendChild(didl);
|
|
|
|
|
|
|
|
|
|
var folder = (Folder)GetItemFromObjectId(sparams["ContainerID"], user);
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
var childrenResult = (await GetChildrenSorted(folder, user, searchCriteria, sortCriteria, start, requested).ConfigureAwait(false));
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
var totalCount = childrenResult.TotalRecordCount;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
var provided = childrenResult.Items.Length;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
foreach (var i in childrenResult.Items)
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
|
|
|
|
if (i.IsFolder)
|
|
|
|
|
{
|
|
|
|
|
var f = (Folder)i;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
var childCount = (await GetChildrenSorted(f, user, searchCriteria, sortCriteria, null, 0).ConfigureAwait(false))
|
|
|
|
|
.TotalRecordCount;
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-04-23 18:29:21 +02:00
|
|
|
|
result.DocumentElement.AppendChild(_didlBuilder.GetFolderElement(result, f, childCount, filter));
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-04-23 18:29:21 +02:00
|
|
|
|
result.DocumentElement.AppendChild(_didlBuilder.GetItemElement(result, i, deviceId, filter));
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var resXML = result.OuterXml;
|
|
|
|
|
|
|
|
|
|
return new List<KeyValuePair<string, string>>
|
|
|
|
|
{
|
|
|
|
|
new KeyValuePair<string,string>("Result", resXML),
|
|
|
|
|
new KeyValuePair<string,string>("NumberReturned", provided.ToString(_usCulture)),
|
|
|
|
|
new KeyValuePair<string,string>("TotalMatches", totalCount.ToString(_usCulture)),
|
2014-04-21 18:02:30 +02:00
|
|
|
|
new KeyValuePair<string,string>("UpdateID", _systemUpdateId.ToString(_usCulture))
|
2014-04-20 07:21:08 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
private async Task<QueryResult<BaseItem>> GetChildrenSorted(Folder folder, User user, SearchCriteria search, SortCriteria sort, int? startIndex, int? limit)
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
2014-08-17 07:38:13 +02:00
|
|
|
|
// TODO: Make a recursive version of GetChildrenSorted (although sorting isn't needed)
|
|
|
|
|
var result = folder.GetRecursiveChildren(user, true);
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
2014-08-17 07:38:13 +02:00
|
|
|
|
var items = FilterUnsupportedContent(result);
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
|
|
|
|
if (search.SearchType == SearchType.Audio)
|
|
|
|
|
{
|
|
|
|
|
items = items.OfType<Audio>();
|
|
|
|
|
}
|
|
|
|
|
else if (search.SearchType == SearchType.Video)
|
|
|
|
|
{
|
|
|
|
|
items = items.OfType<Video>();
|
|
|
|
|
}
|
|
|
|
|
else if (search.SearchType == SearchType.Image)
|
|
|
|
|
{
|
|
|
|
|
items = items.OfType<Photo>();
|
|
|
|
|
}
|
|
|
|
|
else if (search.SearchType == SearchType.Playlist)
|
|
|
|
|
{
|
2014-08-07 04:51:09 +02:00
|
|
|
|
items = items.OfType<Playlist>();
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
2014-08-17 07:38:13 +02:00
|
|
|
|
else if (search.SearchType == SearchType.MusicAlbum)
|
|
|
|
|
{
|
|
|
|
|
items = items.OfType<MusicAlbum>();
|
|
|
|
|
}
|
2014-04-20 07:21:08 +02:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
items = SortItems(items, user, sort);
|
|
|
|
|
|
|
|
|
|
return ToResult(items, startIndex, limit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<QueryResult<BaseItem>> GetChildrenSorted(Folder folder, User user, SortCriteria sort, int? startIndex, int? limit)
|
|
|
|
|
{
|
|
|
|
|
if (folder is UserRootFolder)
|
|
|
|
|
{
|
|
|
|
|
var result = await _userViewManager.GetUserViews(new UserViewQuery
|
|
|
|
|
{
|
|
|
|
|
UserId = user.Id.ToString("N")
|
|
|
|
|
|
|
|
|
|
}, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return ToResult(result, startIndex, limit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var view = folder as UserView;
|
|
|
|
|
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await GetUserViewChildren(view, user, sort).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return ToResult(result, startIndex, limit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var channel = folder as Channel;
|
|
|
|
|
|
|
|
|
|
if (channel != null)
|
|
|
|
|
{
|
2014-08-01 02:37:06 +02:00
|
|
|
|
try
|
2014-07-30 05:31:35 +02:00
|
|
|
|
{
|
2014-08-01 02:37:06 +02:00
|
|
|
|
// Don't blow up here because it could cause parent screens with other content to fail
|
|
|
|
|
return await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
|
|
|
|
|
{
|
|
|
|
|
ChannelId = channel.Id.ToString("N"),
|
|
|
|
|
Limit = limit,
|
|
|
|
|
StartIndex = startIndex,
|
|
|
|
|
UserId = user.Id.ToString("N")
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
2014-08-01 02:37:06 +02:00
|
|
|
|
}, CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Already logged at lower levels
|
|
|
|
|
}
|
2014-07-30 05:31:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var channelFolderItem = folder as ChannelFolderItem;
|
|
|
|
|
|
|
|
|
|
if (channelFolderItem != null)
|
|
|
|
|
{
|
2014-08-01 02:37:06 +02:00
|
|
|
|
try
|
2014-07-30 05:31:35 +02:00
|
|
|
|
{
|
2014-08-01 02:37:06 +02:00
|
|
|
|
// Don't blow up here because it could cause parent screens with other content to fail
|
|
|
|
|
return await _channelManager.GetChannelItemsInternal(new ChannelItemQuery
|
|
|
|
|
{
|
|
|
|
|
ChannelId = channelFolderItem.ChannelId,
|
|
|
|
|
FolderId = channelFolderItem.Id.ToString("N"),
|
|
|
|
|
Limit = limit,
|
|
|
|
|
StartIndex = startIndex,
|
|
|
|
|
UserId = user.Id.ToString("N")
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
2014-08-01 02:37:06 +02:00
|
|
|
|
}, CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// Already logged at lower levels
|
|
|
|
|
}
|
2014-07-30 05:31:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ToResult(GetPlainFolderChildrenSorted(folder, user, sort), startIndex, limit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private QueryResult<BaseItem> ToResult(IEnumerable<BaseItem> items, int? startIndex, int? limit)
|
|
|
|
|
{
|
|
|
|
|
var list = items.ToArray();
|
|
|
|
|
var totalCount = list.Length;
|
|
|
|
|
|
|
|
|
|
if (startIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
list = list.Skip(startIndex.Value).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
list = list.Take(limit.Value).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new QueryResult<BaseItem>
|
|
|
|
|
{
|
|
|
|
|
Items = list,
|
|
|
|
|
TotalRecordCount = totalCount
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<IEnumerable<BaseItem>> GetUserViewChildren(UserView folder, User user, SortCriteria sort)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(folder.ViewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
var result = await _channelManager.GetChannelsInternal(new ChannelQuery()
|
|
|
|
|
{
|
|
|
|
|
UserId = user.Id.ToString("N")
|
|
|
|
|
|
|
|
|
|
}, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return result.Items;
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(folder.ViewType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SortItems(folder.GetChildren(user, true).OfType<Series>(), user, sort);
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(folder.ViewType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2014-08-01 14:13:24 +02:00
|
|
|
|
return SortItems(folder.GetRecursiveChildren(user, true).OfType<Movie>(), user, sort);
|
2014-07-30 05:31:35 +02:00
|
|
|
|
}
|
|
|
|
|
if (string.Equals(folder.ViewType, CollectionType.Music, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SortItems(folder.GetChildren(user, true).OfType<MusicArtist>(), user, sort);
|
|
|
|
|
}
|
2014-08-14 15:24:30 +02:00
|
|
|
|
if (string.Equals(folder.ViewType, CollectionType.Folders, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SortItems(folder.GetChildren(user, true), user, sort);
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(folder.ViewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SortItems(folder.GetChildren(user, true), user, sort);
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(folder.ViewType, CollectionType.LiveTvRecordingGroups, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SortItems(folder.GetChildren(user, true), user, sort);
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(folder.ViewType, CollectionType.LiveTvChannels, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SortItems(folder.GetChildren(user, true), user, sort);
|
|
|
|
|
}
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
|
|
|
|
return GetPlainFolderChildrenSorted(folder, user, sort);
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
private IEnumerable<BaseItem> GetPlainFolderChildrenSorted(Folder folder, User user, SortCriteria sort)
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
|
|
|
|
var items = folder.GetChildren(user, true);
|
|
|
|
|
|
|
|
|
|
items = FilterUnsupportedContent(items);
|
2014-04-18 07:03:01 +02:00
|
|
|
|
|
2014-08-03 04:16:37 +02:00
|
|
|
|
if (folder.IsPreSorted)
|
2014-04-18 07:03:01 +02:00
|
|
|
|
{
|
2014-04-20 07:21:08 +02:00
|
|
|
|
return items;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-20 07:21:08 +02:00
|
|
|
|
return SortItems(items, user, sort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<BaseItem> SortItems(IEnumerable<BaseItem> items, User user, SortCriteria sort)
|
|
|
|
|
{
|
2014-07-30 05:31:35 +02:00
|
|
|
|
return _libraryManager.Sort(items, user, new[] { ItemSortBy.SortName }, sort.SortOrder);
|
2014-04-20 07:21:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<BaseItem> FilterUnsupportedContent(IEnumerable<BaseItem> items)
|
|
|
|
|
{
|
|
|
|
|
return items.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
// Unplayable
|
2014-08-07 04:51:09 +02:00
|
|
|
|
if (i.LocationType == LocationType.Virtual && !i.IsFolder)
|
2014-04-20 07:21:08 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unplayable
|
|
|
|
|
var supportsPlaceHolder = i as ISupportsPlaceHolders;
|
|
|
|
|
if (supportsPlaceHolder != null && supportsPlaceHolder.IsPlaceHolder)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i is Game || i is Book)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2014-04-18 07:03:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-19 19:43:12 +02:00
|
|
|
|
private BaseItem GetItemFromObjectId(string id, User user)
|
|
|
|
|
{
|
2014-08-30 23:45:04 +02:00
|
|
|
|
return DidlBuilder.IsIdRoot(id)
|
2014-04-19 19:43:12 +02:00
|
|
|
|
|
2014-04-20 07:21:08 +02:00
|
|
|
|
? user.RootFolder
|
2014-06-22 07:52:31 +02:00
|
|
|
|
: ParseItemId(id, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BaseItem ParseItemId(string id, User user)
|
|
|
|
|
{
|
|
|
|
|
Guid itemId;
|
|
|
|
|
|
|
|
|
|
if (Guid.TryParse(id, out itemId))
|
|
|
|
|
{
|
|
|
|
|
return _libraryManager.GetItemById(itemId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.Error("Error parsing item Id: {0}. Returning user root folder.", id);
|
|
|
|
|
|
|
|
|
|
return user.RootFolder;
|
2014-04-19 19:43:12 +02:00
|
|
|
|
}
|
2014-04-10 17:06:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|