vdr-plugin-streamdev/server/menuHTTP.c

566 lines
16 KiB
C
Raw Normal View History

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
2008-03-28 16:13:11 +01:00
#include <vdr/channels.h>
#include "server/menuHTTP.h"
//**************************** cRecordingIterator **************
cRecordingsIterator::cRecordingsIterator(eStreamType StreamType): RecordingsLock(&Recordings)
{
streamType = StreamType;
first = NextSuitable(Recordings.First());
current = NULL;
}
const cRecording* cRecordingsIterator::NextSuitable(const cRecording *Recording)
{
while (Recording)
{
bool isPes = Recording->IsPesRecording();
if (!isPes || (isPes && streamType == stPES))
break;
Recording = Recordings.Next(Recording);
}
return Recording;
}
bool cRecordingsIterator::Next()
{
if (first)
{
current = first;
first = NULL;
}
else
current = NextSuitable(Recordings.Next(current));
return current;
}
const cString cRecordingsIterator::ItemRessource() const
{
struct stat st;
if (stat(current->FileName(), &st) == 0)
return cString::sprintf("%lu:%llu.rec", (unsigned long) st.st_dev, (unsigned long long) st.st_ino);
return "";
}
2008-03-28 16:13:11 +01:00
//**************************** cChannelIterator **************
2013-02-03 11:02:25 +01:00
cChannelIterator::cChannelIterator(const cChannel *First)
{
first = First;
current = NULL;
}
2008-03-28 16:13:11 +01:00
2013-02-03 11:02:25 +01:00
bool cChannelIterator::Next()
2008-03-28 16:13:11 +01:00
{
2013-02-03 11:02:25 +01:00
if (first)
{
current = first;
first = NULL;
}
else
current = NextChannel(current);
2008-03-28 16:13:11 +01:00
return current;
}
2013-02-03 11:02:25 +01:00
const cString cChannelIterator::ItemId() const
{
if (current)
{
if (current->GroupSep())
{
int index = 0;
for (int curr = Channels.GetNextGroup(-1); curr >= 0; curr = Channels.GetNextGroup(curr))
{
if (Channels.Get(curr) == current)
return itoa(index);
index++;
}
}
else
{
return itoa(current->Number());
}
}
return cString("-1");
}
const cChannel* cChannelIterator::GetGroup(const char* GroupId)
{
int group = -1;
if (GroupId)
{
int Index = atoi(GroupId);
group = Channels.GetNextGroup(-1);
while (Index-- && group >= 0)
group = Channels.GetNextGroup(group);
}
return group >= 0 ? Channels.Get(group) : NULL;
}
2008-03-28 16:13:11 +01:00
//**************************** cListAll **************
cListAll::cListAll(): cChannelIterator(Channels.First())
{}
const cChannel* cListAll::NextChannel(const cChannel *Channel)
{
if (Channel)
2010-12-02 09:57:17 +01:00
Channel = SkipFakeGroups(Channels.Next(Channel));
2008-03-28 16:13:11 +01:00
return Channel;
}
//**************************** cListChannels **************
cListChannels::cListChannels(): cChannelIterator(Channels.Get(Channels.GetNextNormal(-1)))
{}
const cChannel* cListChannels::NextChannel(const cChannel *Channel)
{
if (Channel)
Channel = Channels.Get(Channels.GetNextNormal(Channel->Index()));
return Channel;
}
// ********************* cListGroups ****************
cListGroups::cListGroups(): cChannelIterator(Channels.Get(Channels.GetNextGroup(-1)))
{}
const cChannel* cListGroups::NextChannel(const cChannel *Channel)
{
if (Channel)
Channel = Channels.Get(Channels.GetNextGroup(Channel->Index()));
return Channel;
}
//
// ********************* cListGroup ****************
2013-02-03 11:02:25 +01:00
cListGroup::cListGroup(const char *GroupId): cChannelIterator(GetNextChannelInGroup(GetGroup(GroupId)))
2008-03-28 16:13:11 +01:00
{}
2010-12-02 09:57:17 +01:00
const cChannel* cListGroup::GetNextChannelInGroup(const cChannel *Channel)
2008-03-28 16:13:11 +01:00
{
if (Channel)
2010-12-02 09:57:17 +01:00
Channel = SkipFakeGroups(Channels.Next(Channel));
return Channel && !Channel->GroupSep() ? Channel : NULL;
}
const cChannel* cListGroup::NextChannel(const cChannel *Channel)
{
return GetNextChannelInGroup(Channel);
2008-03-28 16:13:11 +01:00
}
//
// ********************* cListTree ****************
2013-02-03 11:02:25 +01:00
cListTree::cListTree(const char *SelectedGroupId): cChannelIterator(Channels.Get(Channels.GetNextGroup(-1)))
2008-03-28 16:13:11 +01:00
{
2013-02-03 11:02:25 +01:00
selectedGroup = GetGroup(SelectedGroupId);
2008-03-28 16:13:11 +01:00
currentGroup = Channels.Get(Channels.GetNextGroup(-1));
}
const cChannel* cListTree::NextChannel(const cChannel *Channel)
{
if (currentGroup == selectedGroup)
{
if (Channel)
2010-12-02 09:57:17 +01:00
Channel = SkipFakeGroups(Channels.Next(Channel));
2008-03-28 16:13:11 +01:00
if (Channel && Channel->GroupSep())
currentGroup = Channel;
}
else
{
if (Channel)
Channel = Channels.Get(Channels.GetNextGroup(Channel->Index()));
currentGroup = Channel;
}
return Channel;
}
2013-02-03 11:02:25 +01:00
// ******************** cMenuList ******************
cMenuList::cMenuList(cItemIterator *Iterator) : iterator(Iterator)
2008-03-28 16:13:11 +01:00
{}
2013-02-03 11:02:25 +01:00
cMenuList::~cMenuList()
2008-03-28 16:13:11 +01:00
{
delete iterator;
}
2013-02-03 11:02:25 +01:00
// ******************** cHtmlMenuList ******************
const char* cHtmlMenuList::menu =
"[<a href=\"/\">Home</a> (<a href=\"all.html\" tvid=\"RED\">no script</a>)] "
"[<a href=\"tree.html\" tvid=\"GREEN\">Tree View</a>] "
2013-02-02 23:28:55 +01:00
"[<a href=\"groups.html\" tvid=\"YELLOW\">Groups</a> (<a href=\"groups.m3u\">Playlist</a> | <a href=\"groups.rss\">RSS</a>)] "
"[<a href=\"channels.html\" tvid=\"BLUE\">Channels</a> (<a href=\"channels.m3u\">Playlist</a> | <a href=\"channels.rss\">RSS</a>)] "
"[<a href=\"recordings.html\">Recordings</a> (<a href=\"recordings.m3u\">Playlist</a> | <a href=\"recordings.rss\">RSS</a>)] ";
2008-03-28 16:13:11 +01:00
2013-02-03 11:02:25 +01:00
const char* cHtmlMenuList::css =
2008-03-28 16:13:11 +01:00
"<style type=\"text/css\">\n"
"<!--\n"
"a:link, a:visited, a:hover, a:active, a:focus { color:#333399; }\n"
"body { font:100% Verdana, Arial, Helvetica, sans-serif; background-color:#9999FF; margin:1em; }\n"
".menu { position:fixed; top:0px; left:1em; right:1em; height:3em; text-align:center; background-color:white; border:inset 2px #9999ff; }\n"
"h2 { font-size:150%; margin:0em; padding:0em 1.5em; }\n"
".contents { margin-top:5em; background-color:white; }\n"
".group { background:url(data:image/gif;base64,R0lGODdhAQAeAIQeAJub/5yc/6Cf/6Oj/6am/6qq/66u/7Gx/7S0/7i4/7u8/7+//8LD/8bG/8nK/83N/9HQ/9TU/9fX/9va/97e/+Lh/+Xl/+no/+3t//Dw//Pz//b3//v7//7+/////////ywAAAAAAQAeAAAFGCAQCANRGAeSKAvTOA8USRNVWReWaRvXhQA7) repeat-x; border:inset 2px #9999ff; }\n"
".items { border-top:dashed 1px; margin-top:0px; margin-bottom:0px; padding:0.7em 5em; }\n"
".apid { padding-left:28px; margin:0.5em; background:url(data:image/gif;base64,R0lGODlhGwASAKEBAAAAAP///////////yH5BAEKAAEALAAAAAAbABIAAAJAjI+pywj5WgPxVAmpNRcHqnGb94FPhE7m+okts7JvusSmSys2iLv6TstldjMfhhUkcXi+zjFUVFo6TiVVij0UAAA7) no-repeat; }\n"
".dpid { padding-left:28px; margin:0.5em; background:url(data:image/gif;base64,R0lGODlhGwASAKEBAAAAAP///////////yH5BAEKAAEALAAAAAAbABIAAAJFjI+py+0BopwAUoqivRvr83UaZ4RWMnVoBbLZaJbuqcCLGcv0+t5Vvgu2hLrh6pfDzVSpnlGEbAZhnIutZaVmH9yuV1EAADs=) no-repeat; }\n"
"button { width:2em; margin:0.2em 0.5em; vertical-align:top; }\n"
"-->\n"
"</style>";
2013-02-03 11:02:25 +01:00
const char* cHtmlMenuList::js =
2008-03-28 16:13:11 +01:00
"<script language=\"JavaScript\">\n"
"<!--\n"
"function eventTarget(evt) {\n"
" if (!evt) evt = window.event;\n"
" if (evt.target) return evt.target;\n"
" else if (evt.srcElement) return evt.srcElement;\n"
" else return null;\n"
"}\n"
// toggle visibility of a group
"function clickHandler(evt) {\n"
" var button = eventTarget(evt);\n"
" if (button) {\n"
" var group = document.getElementById('c' + button.id);\n"
" if (group) {\n"
" button.removeChild(button.firstChild);\n"
" if (group.style.display == 'block') {\n"
" button.appendChild(document.createTextNode(\"+\"));\n"
" group.style.display = 'none';\n"
" } else {\n"
" button.appendChild(document.createTextNode(\"-\"));\n"
" group.style.display = 'block';\n"
" }\n"
" }\n"
" }\n"
"}\n"
// insert a click button infront of each h2 and an id to the corresponding list
"function init() {\n"
" var titles = document.getElementsByTagName('h2');\n"
" for (var i = 0; i < titles.length; i++) {\n"
" var button = document.createElement('button');\n"
" button.id = 'g' + i;\n"
" button.onclick = clickHandler;\n"
" button.appendChild(document.createTextNode('+'));\n"
" titles[i].insertBefore(button, titles[i].firstChild);\n"
" var group = titles[i].nextSibling;\n"
" while (group) {\n"
" if (group.className && group.className == 'items') {\n"
" group.id = 'cg' + i;\n"
" break;\n"
" }\n"
" group = group.nextSibling;\n"
" }\n"
" }\n"
"}\n"
"window.onload = init;\n"
// hide lists before the browser renders it
"if (document.styleSheets[0].insertRule)\n"
" document.styleSheets[0].insertRule('.items { display:none }', 0);\n"
"else if (document.styleSheets[0].addRule)\n"
" document.styleSheets[0].addRule('.items', 'display:none');\n"
"//-->\n"
"</script>";
2013-02-03 11:02:25 +01:00
std::string cHtmlMenuList::StreamTypeMenu()
2008-03-28 16:13:11 +01:00
{
std::string typeMenu;
typeMenu += (streamType == stTS ? (std::string) "[TS] " :
(std::string) "[<a href=\"/TS/" + self + "\">TS</a>] ");
#ifdef STREAMDEV_PS
2008-03-28 16:13:11 +01:00
typeMenu += (streamType == stPS ? (std::string) "[PS] " :
(std::string) "[<a href=\"/PS/" + self + "\">PS</a>] ");
#endif
2008-03-28 16:13:11 +01:00
typeMenu += (streamType == stPES ? (std::string) "[PES] " :
(std::string) "[<a href=\"/PES/" + self + "\">PES</a>] ");
typeMenu += (streamType == stES ? (std::string) "[ES] " :
(std::string) "[<a href=\"/ES/" + self + "\">ES</a>] ");
2010-12-02 09:57:17 +01:00
typeMenu += (streamType == stEXT ? (std::string) "[EXT] " :
(std::string) "[<a href=\"/EXT/" + self + "\">EXT</a>] ");
2008-03-28 16:13:11 +01:00
return typeMenu;
}
2013-02-03 11:02:25 +01:00
cHtmlMenuList::cHtmlMenuList(cItemIterator *Iterator, eStreamType StreamType, const char *Self, const char *Rss, const char *GroupTarget): cMenuList(Iterator)
2008-03-28 16:13:11 +01:00
{
streamType = StreamType;
self = strdup(Self);
2013-02-02 23:28:55 +01:00
rss = strdup(Rss);
2008-03-28 16:13:11 +01:00
groupTarget = (GroupTarget && *GroupTarget) ? strdup(GroupTarget) : NULL;
htmlState = hsRoot;
2013-02-03 11:02:25 +01:00
onItem = true;
2008-03-28 16:13:11 +01:00
}
2013-02-03 11:02:25 +01:00
cHtmlMenuList::~cHtmlMenuList()
2008-03-28 16:13:11 +01:00
{
free((void *) self);
2013-02-02 23:28:55 +01:00
free((void *) rss);
2008-03-28 16:13:11 +01:00
free((void *) groupTarget);
}
2013-02-03 11:02:25 +01:00
bool cHtmlMenuList::HasNext()
2008-03-28 16:13:11 +01:00
{
return htmlState != hsPageBottom;
}
2013-02-03 11:02:25 +01:00
std::string cHtmlMenuList::Next()
2008-03-28 16:13:11 +01:00
{
switch (htmlState)
{
case hsRoot:
htmlState = hsHtmlHead;
break;
case hsHtmlHead:
htmlState = hsCss;
break;
case hsCss:
htmlState = *self ? hsPageTop : hsJs;
break;
case hsJs:
htmlState = hsPageTop;
break;
case hsPageTop:
2013-02-03 11:02:25 +01:00
onItem = NextItem();
htmlState = onItem ? (IsGroup() ? hsGroupTop : hsPlainTop) : hsPageBottom;
2008-03-28 16:13:11 +01:00
break;
case hsPlainTop:
htmlState = hsPlainItem;
break;
case hsPlainItem:
2013-02-03 11:02:25 +01:00
onItem = NextItem();
htmlState = onItem && !IsGroup() ? hsPlainItem : hsPlainBottom;
2008-03-28 16:13:11 +01:00
break;
case hsPlainBottom:
2013-02-03 11:02:25 +01:00
htmlState = onItem ? hsGroupTop : hsPageBottom;
2008-03-28 16:13:11 +01:00
break;
case hsGroupTop:
2013-02-03 11:02:25 +01:00
onItem = NextItem();
htmlState = onItem && !IsGroup() ? hsItemsTop : hsGroupBottom;
2008-03-28 16:13:11 +01:00
break;
case hsItemsTop:
htmlState = hsItem;
break;
case hsItem:
2013-02-03 11:02:25 +01:00
onItem = NextItem();
htmlState = onItem && !IsGroup() ? hsItem : hsItemsBottom;
2008-03-28 16:13:11 +01:00
break;
case hsItemsBottom:
htmlState = hsGroupBottom;
break;
case hsGroupBottom:
2013-02-03 11:02:25 +01:00
htmlState = onItem ? hsGroupTop : hsPageBottom;
2008-03-28 16:13:11 +01:00
break;
case hsPageBottom:
default:
2013-02-03 11:02:25 +01:00
esyslog("streamdev-server cHtmlMenuList: invalid call to Next()");
2008-03-28 16:13:11 +01:00
break;
}
switch (htmlState)
{
// NOTE: JavaScript requirements:
// Group title is identified by <h2> tag
// Channel list must be a sibling of <h2> with class "items"
case hsHtmlHead: return "<html><head>" + HtmlHead();
case hsCss: return css;
case hsJs: return js;
case hsPageTop: return "</head><body>" + PageTop() + "<div class=\"contents\">";
case hsGroupTop: return "<div class=\"group\"><h2>" + GroupTitle() + "</h2>";
case hsItemsTop:
case hsPlainTop: return "<ol class=\"items\">";
case hsItem:
case hsPlainItem: return ItemText();
case hsItemsBottom:
case hsPlainBottom: return "</ol>";
case hsGroupBottom: return "</div>";
case hsPageBottom: return "</div>" + PageBottom() + "</body></html>";
default: return "";
}
}
2013-02-03 11:02:25 +01:00
std::string cHtmlMenuList::HtmlHead()
2008-03-28 16:13:11 +01:00
{
2013-02-02 23:28:55 +01:00
return (std::string) "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"" + rss + "\"/>";
2008-03-28 16:13:11 +01:00
}
2013-02-03 11:02:25 +01:00
std::string cHtmlMenuList::PageTop()
2008-03-28 16:13:11 +01:00
{
return (std::string) "<div class=\"menu\"><div>" + menu + "</div><div>" + StreamTypeMenu() + "</div></div>";
}
2013-02-03 11:02:25 +01:00
std::string cHtmlMenuList::PageBottom()
2008-03-28 16:13:11 +01:00
{
return (std::string) "";
}
2013-02-03 11:02:25 +01:00
std::string cHtmlMenuList::GroupTitle()
2008-03-28 16:13:11 +01:00
{
if (groupTarget)
{
2013-02-03 11:02:25 +01:00
return (std::string) "<a href=\"" + groupTarget + "?group=" + (const char*) ItemId() + "\">" +
ItemTitle() + "</a>";
2008-03-28 16:13:11 +01:00
}
else
{
2013-02-03 11:02:25 +01:00
return (std::string) ItemTitle();
2008-03-28 16:13:11 +01:00
}
}
2013-02-03 11:02:25 +01:00
std::string cHtmlMenuList::ItemText()
2008-03-28 16:13:11 +01:00
{
std::string line;
std::string suffix;
switch (streamType) {
case stTS: suffix = (std::string) ".ts"; break;
#ifdef STREAMDEV_PS
case stPS: suffix = (std::string) ".vob"; break;
#endif
// for Network Media Tank
case stPES: suffix = (std::string) ".vdr"; break;
default: suffix = "";
}
2013-02-03 11:02:25 +01:00
line += (std::string) "<li value=\"" + (const char*) ItemId() + "\">";
line += (std::string) "<a href=\"" + (const char*) ItemRessource() + suffix + "\"";
// for Network Media Tank
line += (std::string) " vod ";
2013-02-03 11:02:25 +01:00
if (strlen(ItemId()) < 4)
line += (std::string) " tvid=\"" + (const char*) ItemId() + "\"";
2008-03-28 16:13:11 +01:00
2013-02-03 11:02:25 +01:00
line += (std::string) ">" + ItemTitle() + "</a>";
2008-03-28 16:13:11 +01:00
2013-02-03 11:02:25 +01:00
// TS always streams all PIDs
if (streamType != stTS)
2008-03-28 16:13:11 +01:00
{
int index = 1;
2013-02-03 11:02:25 +01:00
const char* lang;
std::string pids;
for (int i = 0; (lang = Alang(i)) != NULL; ++i, ++index) {
pids += (std::string) " <a href=\"" + (const char*) ItemRessource() +
"+" + (const char*)itoa(index) + suffix + "\" class=\"apid\" vod>" + (const char*) lang + "</a>";
}
for (int i = 0; (lang = Dlang(i)) != NULL; ++i, ++index) {
pids += (std::string) " <a href=\"" + (const char*) ItemRessource() +
"+" + (const char*)itoa(index) + suffix + "\" class=\"dpid\" vod>" + (const char*) lang + "</a>";
}
// always show audio PIDs for stES to select audio only
if (index > 2 || streamType == stES)
line += pids;
2008-03-28 16:13:11 +01:00
}
line += "</li>";
return line;
}
2013-02-03 11:02:25 +01:00
// ******************** cM3uMenuList ******************
cM3uMenuList::cM3uMenuList(cItemIterator *Iterator, const char* Base)
: cMenuList(Iterator),
m_IConv(cCharSetConv::SystemCharacterTable(), "UTF-8")
2008-03-28 16:13:11 +01:00
{
base = strdup(Base);
m3uState = msFirst;
}
2013-02-03 11:02:25 +01:00
cM3uMenuList::~cM3uMenuList()
2008-03-28 16:13:11 +01:00
{
free(base);
}
2013-02-03 11:02:25 +01:00
bool cM3uMenuList::HasNext()
2008-03-28 16:13:11 +01:00
{
return m3uState != msLast;
}
2013-02-03 11:02:25 +01:00
std::string cM3uMenuList::Next()
2008-03-28 16:13:11 +01:00
{
if (m3uState == msFirst)
{
m3uState = msContinue;
return "#EXTM3U";
}
2013-02-03 11:02:25 +01:00
if (!NextItem())
2008-03-28 16:13:11 +01:00
{
m3uState = msLast;
return "";
}
2013-02-03 11:02:25 +01:00
std::string name = (std::string) m_IConv.Convert(ItemTitle());
2008-03-28 16:13:11 +01:00
2013-02-03 11:02:25 +01:00
if (IsGroup())
2008-03-28 16:13:11 +01:00
{
return (std::string) "#EXTINF:-1," + name + "\r\n" +
2013-02-03 11:02:25 +01:00
base + "group.m3u?group=" + (const char*) ItemId();
2008-03-28 16:13:11 +01:00
}
else
{
return (std::string) "#EXTINF:-1," +
2013-02-03 11:02:25 +01:00
(const char*) ItemId() + " " + name + "\r\n" +
base + (const char*) ItemRessource();
2008-03-28 16:13:11 +01:00
}
}
2013-02-03 11:02:25 +01:00
// ******************** cRssMenuList ******************
cRssMenuList::cRssMenuList(cItemIterator *Iterator, const char *Base, const char *Html)
: cMenuList(Iterator),
2013-02-02 23:28:55 +01:00
m_IConv(cCharSetConv::SystemCharacterTable(), "UTF-8")
{
base = strdup(Base);
html = strdup(Html);
rssState = msFirst;
}
2013-02-03 11:02:25 +01:00
cRssMenuList::~cRssMenuList()
2013-02-02 23:28:55 +01:00
{
free(base);
free(html);
}
2013-02-03 11:02:25 +01:00
bool cRssMenuList::HasNext()
2013-02-02 23:28:55 +01:00
{
return rssState != msLast;
}
2013-02-03 11:02:25 +01:00
std::string cRssMenuList::Next()
2013-02-02 23:28:55 +01:00
{
std::string type_ext;
if (rssState == msFirst)
{
rssState = msContinue;
return (std::string) "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<rss version=\"2.0\">\n\t<channel>\n"
"\t\t<title>VDR</title>\n"
"\t\t<link>" + base + html + "</link>\n"
"\t\t<description>VDR channel list</description>\n"
;
}
2013-02-03 11:02:25 +01:00
if (!NextItem())
2013-02-02 23:28:55 +01:00
{
rssState = msLast;
return "\t</channel>\n</rss>\n";
}
2013-02-03 11:02:25 +01:00
std::string name = (std::string) m_IConv.Convert(ItemTitle());
2013-02-02 23:28:55 +01:00
2013-02-03 11:02:25 +01:00
if (IsGroup())
2013-02-02 23:28:55 +01:00
{
return (std::string) "\t\t<item>\n\t\t\t<title>" +
name + "</title>\n\t\t\t<link>" +
2013-02-03 11:02:25 +01:00
base + "group.rss?group=" + (const char*) ItemId() + "</link>\n\t\t</item>\n";
2013-02-02 23:28:55 +01:00
}
else
{
return (std::string) "\t\t<item>\n\t\t\t<title>" +
2013-02-03 11:02:25 +01:00
(const char*) ItemId() + " " + name + "</title>\n\t\t\t<link>" +
base + (const char*) ItemRessource() + "</link>\n\t\t\t<enclosure url=\"" +
base + (const char*) ItemRessource() + "\" type=\"video/mpeg\" />\n\t\t</item>\n";
2013-02-02 23:28:55 +01:00
}
}