using MediaBrowser.Dlna.Common;
using MediaBrowser.Model.Dlna;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security;
using System.Text;
namespace MediaBrowser.Dlna.Server
{
public class DescriptionXmlBuilder
{
private readonly DeviceProfile _profile;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly string _serverUdn;
private readonly string _serverAddress;
public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress)
{
if (string.IsNullOrWhiteSpace(serverUdn))
{
throw new ArgumentNullException("serverUdn");
}
_profile = profile;
_serverUdn = serverUdn;
_serverAddress = serverAddress;
}
public string GetXml()
{
var builder = new StringBuilder();
builder.Append("");
builder.Append("");
builder.Append("");
builder.Append("1");
builder.Append("0");
builder.Append("");
AppendDeviceInfo(builder);
builder.Append("");
return builder.ToString();
}
private void AppendDeviceInfo(StringBuilder builder)
{
builder.Append("");
AppendDeviceProperties(builder);
AppendIconList(builder);
AppendServiceList(builder);
builder.Append("");
}
private void AppendDeviceProperties(StringBuilder builder)
{
builder.Append("uuid:" + SecurityElement.Escape(_serverUdn) + "");
builder.Append("" + SecurityElement.Escape(_profile.XDlnaCap ?? string.Empty) + "");
builder.Append("M-DMS-1.50");
builder.Append("" + SecurityElement.Escape(_profile.XDlnaDoc ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(_profile.FriendlyName ?? string.Empty) + "");
builder.Append("urn:schemas-upnp-org:device:MediaServer:1");
builder.Append("" + SecurityElement.Escape(_profile.Manufacturer ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(_profile.ModelName ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(_profile.ModelDescription ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(_profile.ModelNumber ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(_profile.ModelUrl ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(_profile.SerialNumber ?? string.Empty) + "");
//builder.Append("" + SecurityElement.Escape(_serverAddress) + "");
if (!string.IsNullOrWhiteSpace(_profile.SonyAggregationFlags))
{
builder.Append("" + SecurityElement.Escape(_profile.SonyAggregationFlags) + "");
}
}
private void AppendIconList(StringBuilder builder)
{
builder.Append("");
foreach (var icon in GetIcons())
{
builder.Append("");
builder.Append("" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "");
builder.Append("" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "");
builder.Append("" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(icon.Url ?? string.Empty) + "");
builder.Append("");
}
builder.Append("");
}
private void AppendServiceList(StringBuilder builder)
{
builder.Append("");
foreach (var service in GetServices())
{
builder.Append("");
builder.Append("" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(service.ScpdUrl ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(service.ControlUrl ?? string.Empty) + "");
builder.Append("" + SecurityElement.Escape(service.EventSubUrl ?? string.Empty) + "");
builder.Append("");
}
builder.Append("");
}
private IEnumerable GetIcons()
{
var list = new List();
list.Add(new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 240,
Height = 240,
Url = "/dlna/icons/logo240.png"
});
list.Add(new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 240,
Height = 240,
Url = "/dlna/icons/logo240.jpg"
});
list.Add(new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 120,
Height = 120,
Url = "/dlna/icons/logo120.png"
});
list.Add(new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 120,
Height = 120,
Url = "/dlna/icons/logo120.jpg"
});
list.Add(new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 48,
Height = 48,
Url = "/dlna/icons/logo48.png"
});
list.Add(new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 48,
Height = 48,
Url = "/dlna/icons/logo48.jpg"
});
return list;
}
private IEnumerable GetServices()
{
var list = new List();
list.Add(new DeviceService
{
ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
ScpdUrl = "/dlna/contentdirectory/contentdirectory.xml",
ControlUrl = "/dlna/contentdirectory/" + _serverUdn + "/control",
EventSubUrl = "/dlna/contentdirectory/" + _serverUdn + "/events"
});
list.Add(new DeviceService
{
ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
ScpdUrl = "/dlna/connectionmanager/connectionmanager.xml",
ControlUrl = "/dlna/connectionmanager/" + _serverUdn + "/control",
EventSubUrl = "/dlna/connectionmanager/" + _serverUdn + "/events"
});
list.Add(new DeviceService
{
ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
ScpdUrl = "/dlna/mediareceiverregistrar/mediareceiverregistrar.xml",
ControlUrl = "/dlna/mediareceiverregistrar/" + _serverUdn + "/control",
EventSubUrl = "/dlna/mediareceiverregistrar/" + _serverUdn + "/events"
});
return list;
}
public override string ToString()
{
return GetXml();
}
}
}