Default script for Web-navigation pages

Post Reply
Eugene
Posts: 2940
Joined: Tue Nov 17, 2009 8:05 pm

Default script for Web-navigation pages

Post by Eugene »

Version 1.07.1 (http://www.wildmediaserver.com/forum/vi ... f=19#p4269) adds the ability to use a script to create Web-navigation pages (Settings - Devices - your device - Settings - Additional - "Web-navigation" - Script)

Default script (required version 1.72)

Code: Select all

const
  csPageHeader = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'#13#10 +  
                 '<html xmlns="http://www.w3.org/1999/xhtml">'#13#10 + 
                 '<head>'#13#10 + 
                 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'#13#10 + 
                 '<link href="%spresentation/css/styles1.css" rel="stylesheet" type="text/css"/>'#13#10 + 
                 '<title>Wild Media Server (UPnP, DLNA, HTTP) - %s</title>'#13#10 + 
                 '</head>'#13#10 +
                 '<body>'#13#10 + 
                 '<img class="imagelogo" src="%spresentation/images/wms48x48.png" align="left"/>'#13#10 + 
                 '<br/><h1>%s</h1><br/>'#13#10;
  csPageFooter = '<p class="copyright">'#13#10 + 
                 '<a class="copyright" title="Wild Media Server (UPnP, DLNA, HTTP)" href="http://www.wildmediaserver.com">'#13#10 + 
                 ''#13#10 + 
                 'Wild Media Server (UPnP, DLNA, HTTP)</a>'#13#10 + 
                 '<br/><a class="copyright" title="Copyright © 2009-2015 Evgeny Lachinov" href="http://www.wildmediaserver.com">'#13#10 + 
                 ''#13#10 + 
                 'Copyright © 2009-2015 Evgeny Lachinov</a>'#13#10 + 
                 '</p>'#13#10 + 
                 '</body>'#13#10 + 
                 '</html>'#13#10;                 

  csRuntimeMessages = 'WmsRuntimeMessages';

  csUrlTemplate   = '%sMediaServer/Folders/%s?start=%d;count=%d';  
                 
procedure WriteString(const aStr: string);
begin
  if aStr <> '' then  
    WebPageText := WebPageText + aStr
end;                                  

procedure WriteLine(const aText: string);
begin
  WriteString(aText + #13#10)
end;        

procedure WriteImage(const aImage, aImageClass, aHint: string; aWidth, aHeight: Integer);
var
  sWidthHeight: string;
begin
  if (aWidth > 0) and (aHeight > 0) then
    sWidthHeight := Format(' width=%d height=%d', [aWidth, aHeight])
  else
    sWidthHeight := '';
  WriteString(Format('<img class="%s" src="%s" alt="%s" hspace=2%s/>',
              [aImageClass, aImage, WmsHtmlEncode(aHint), sWidthHeight]));
end;  

procedure WriteLink(const aTitle, aTitleClass, aLink, aImage, aImageClass: string; 
                aImageWidth, aImageHeight: Integer; aImageOnly: Boolean = False);
begin
  if (aImage <> '') and not aImageOnly then
    WriteImage(aImage, aImageClass, aTitle, aImageWidth, aImageHeight);
  WriteLine(Format('<a class="' + aTitleClass + '" title="%s" href="%s">', [WmsHtmlEncode(aTitle), aLink]) + #13#10);
  if aImageOnly then
    WriteImage(aImage, aImageClass, aTitle, aImageWidth, aImageHeight)
  else
    WriteString(WmsHtmlEncode(aTitle));
  WriteLine('</a>');
end;

function GetCountClass(aCurrent: Boolean): string;
begin
  if aCurrent then  
    Result := 'a'    
  else  
    Result := ''
end;

function GetCountValue(aIndex: Integer): Integer;
begin
  case aIndex of  
    0: Result := 5;    
    1: Result := 10;    
    2: Result := 15;    
    3: Result := 20;   
    4: Result := 50;    
  else  
    Result := 0    
  end
end;

function GetCountLabel(aIndex: Integer): string;
var
  iCount: Integer;
begin
  iCount := GetCountValue(aIndex);  
  if iCount = 0 then  
    Result := WmsLocalizationString(csRuntimeMessages, 'rsWebRowsPerPageAll', 'All')    
  else  
    Result := WmsLocalizationString(csRuntimeMessages, 'rsWebRowsPerPage' + IntToStr(iCount), IntToStr(iCount))    
end;

procedure WriteHeader;
var
  i: Integer;
  FolderItem: TWmsScriptMediaItem;
  FolderList: TList;
  sWebLink: string;
begin
  WriteString(Format(csPageHeader, [ServerRootFolder, ServerName, ServerRootFolder, ServerName]));
  if CurrentMediaItem <> nil then begin
    FolderList := TList.Create;
    try
      FolderItem := CurrentMediaItem;
      while FolderItem <> nil do begin
        FolderList.Add(FolderItem);
        FolderItem := FolderItem.ItemParent;
      end;
      WriteLine('<div>');
      for i := FolderList.Count - 1 downto 0 do begin
        FolderItem := TWmsScriptMediaItem(FolderList[i]);
        sWebLink := FolderItem.WebLink(FolderItem);
        sWebLink := sWebLink + Format('?start=0;count=%d', [RequestItemCount]);
        WriteLink(FolderItem.Properties[mpiTitle], 'history',
               sWebLink, ServerRootFolder + 'presentation/images/bulletgreen8.png', 'imagehistory', 0, 0);
      end;
      WriteLine('</div>');
      WriteLine('<br/><div>' + WmsLocalizationString(csRuntimeMessages, 'rsWebRowsOnPage', 'Lines per page: '));
      for i := 0 to 5 do
        WriteLink(GetCountLabel(i), 'itemcount' + GetCountClass(GetCountValue(i) = RequestItemCount),
                  Format(csUrlTemplate, [ServerRootFolder, CurrentMediaItem.ItemID, 0, GetCountValue(i)]), '', '', 0, 0);
      WriteLine('</div>')
    finally
      FolderList.Free
    end
  end
end;

procedure WriteBody;
var
  i, iEnd, iImageWidth, iImageHeight: Integer;
  Item: TWmsScriptMediaItem;
  sImageLink, sWebLink: string;
begin
  if CurrentMediaItemList <> nil then begin
    if RequestItemCount = 0 then
      iEnd := CurrentMediaItemList.Count
    else
      iEnd := Min(CurrentMediaItemList.Count, RequestItemStart + RequestItemCount);
    if iEnd > RequestItemStart then begin
      WriteLine('<ul>');
      if RequestItemStart = 0 then begin
        Item := nil; 
        for i := 0 to CurrentMediaItemList.Count - 1 do begin
          Item := CurrentMediaItemList[i];
          if Item.IsFolder or (Item.MediaType = mtImage) or Item.IsServiceItem or 
             (Item.ItemOrigin.MediaType = mtImage) or Item.ItemOrigin.IsServiceItem then
            Item := nil
          else
            Break
        end;
        if Item <> nil then begin
          WriteLine('<li>');
          sWebLink := Item.ItemOrigin.WebLink(Item.ItemParent);
          sImageLink := ServerRootFolder + 'presentation/images/note24.png';
          WriteLink(WmsLocalizationString(csRuntimeMessages, 'rsWebPlayAll', 'Play All'), 'medialist', sWebLink, sImageLink, 'imagelist', 0, 0);
          WriteLine('</li>');
        end
      end;
      for i := RequestItemStart to iEnd - 1 do begin
        Item := CurrentMediaItemList[i];
        WriteLine('<li>');
        sWebLink := Item.WebLink(Item);
        if Item.IsFolder then
          sWebLink := sWebLink + Format('?start=0;count=%d', [RequestItemCount]);
        if WmsThumbnailsSupported(Item.MediaType) and cfgWebThumbnails and Item.ThumbnailExists then begin
          sImageLink := ServerRootFolder + 'MediaServer/WebThumbnails/' + Item.ThumbnailID;
          iImageWidth  := cfgWebThumbnailWidth;
          iImageHeight := cfgWebThumbnailHeight;
        end else begin
          sImageLink := ServerRootFolder + 'presentation/images/' + Item.WebImageName;
          iImageWidth := 0; iImageHeight := 0;
        end;
        WriteLink(Item.Properties[mpiTitle], 'medialist', sWebLink,
                  sImageLink, 'imagelist', iImageWidth, iImageHeight);
        WriteLine('</li>')
      end;
      WriteString('</ul>');
    end
  end
end;

function GetNavImageName(aIndex: Integer; aEnabled: Boolean): string;
begin
  case aIndex of   
    0: Result := 'first24';
    1: Result := 'previous24';
    2: Result := 'next24';
    3: Result := 'last24'    
  end;                             
  if aEnabled then
    Result := Result + '_h.png'
  else 
    Result := Result + '_d.png'    
end;

function GetNavLabel(aIndex: Integer): string;
begin
  case aIndex of   
    0: Result := WmsLocalizationString(csRuntimeMessages, 'rsWebNavFirstPage', 'First Page');
    1: Result := WmsLocalizationString(csRuntimeMessages, 'rsWebNavPrevPage',  'Previous Page');
    2: Result := WmsLocalizationString(csRuntimeMessages, 'rsWebNavNextPage', 'Next Page');
    3: Result := WmsLocalizationString(csRuntimeMessages, 'rsWebNavLastPage', 'Last Page');    
  end;  
end;

procedure WriteFooter;
var
  i, iPageNo, iPageCount: Integer;

  procedure WriteNavLink(aIndex: Integer);
  var
    bEnabled: Boolean;
    iStart: Integer;
    sImage: string;
  begin
    iStart := RequestItemStart;
    case aIndex of
      0: iStart := 0;
      1: if (iPageNo > 1) and (RequestItemCount > 0) then
           iStart := (iPageNo - 2) * RequestItemCount;
      2: if (iPageNo < iPageCount) and (RequestItemCount > 0) then
           iStart := iPageNo * RequestItemCount;
      3: iStart := (iPageCount - 1) * RequestItemCount
    end;
    if aIndex in [0..1] then
      bEnabled := iPageNo > 1
    else
      bEnabled := iPageNo < iPageCount;
    sImage := ServerRootFolder + 'presentation/images/' + GetNavImageName(aIndex, bEnabled);
    if bEnabled and (CurrentMediaItem <> nil) then
      WriteLink(GetNavLabel(aIndex), 'navigate', Format(csUrlTemplate, [ServerRootFolder, CurrentMediaItem.ItemID, iStart, RequestItemCount]),
                sImage, 'imagenavigate', 0, 0, True)
    else
      WriteImage(sImage, 'imagenavigate', GetNavLabel(aIndex), 0, 0)
  end;

begin
  if (CurrentMediaItemList <> nil) and (CurrentMediaItem <> nil) then begin
    if RequestItemCount > 0 then begin
      iPageNo := (RequestItemStart div RequestItemCount) + 1;
      iPageCount := ((CurrentMediaItemList.Count - 1) div RequestItemCount) + 1;
    end else begin
      iPageNo := 1;
      iPageCount := 1;
    end;
    WriteLine('<div>');
    for i := 0 to 1 do
      WriteNavLink(i);
    WriteLine(Format(WmsLocalizationString(csRuntimeMessages, 'rsWebPageNo', 'Page %d of %d'), [iPageNo, iPageCount]));
    for i := 2 to 3 do
      WriteNavLink(i);
    WriteLine('</div>')
  end;

  WriteString(csPageFooter)
end;

begin
  WebPageText := '';
  WriteHeader;  
  WriteBody;
  WriteFooter;     
  WebPageText := WmsUtf8Encode(WebPageText)
end.
ds1001
Posts: 9
Joined: Sat Sep 10, 2011 1:28 am

Re: Default script for Web-navigation pages

Post by ds1001 »

Does this feature work for a Samsung TV? Do i have to buy and get the license before it will work or should it be working in the demo?
Windows 7 SP1 (x64), MB Asus P5Q-E, Onboard ADI AD2000B audio, 4G ram
NVIDIA GeForce 9800 GTX,Core2 Quad Q9400, Linksys E1550 Router - WiFi n
Samsung LN46C630KLF TV
Eugene
Posts: 2940
Joined: Tue Nov 17, 2009 8:05 pm

Re: Default script for Web-navigation pages

Post by Eugene »

Web-navigation is free of charge
ds1001
Posts: 9
Joined: Sat Sep 10, 2011 1:28 am

Re: Default script for Web-navigation pages

Post by ds1001 »

How does it work. I installed the default script but i don't see anything on my TV suggesting web navigation. What should i be looking for on my TV?
Windows 7 SP1 (x64), MB Asus P5Q-E, Onboard ADI AD2000B audio, 4G ram
NVIDIA GeForce 9800 GTX,Core2 Quad Q9400, Linksys E1550 Router - WiFi n
Samsung LN46C630KLF TV
Eugene
Posts: 2940
Joined: Tue Nov 17, 2009 8:05 pm

Re: Default script for Web-navigation pages

Post by Eugene »

The TV have Internet Browser ?
ds1001
Posts: 9
Joined: Sat Sep 10, 2011 1:28 am

Re: Default script for Web-navigation pages

Post by ds1001 »

That explains it. My Samsung doesn't have a browser. I was being too hopeful.
Windows 7 SP1 (x64), MB Asus P5Q-E, Onboard ADI AD2000B audio, 4G ram
NVIDIA GeForce 9800 GTX,Core2 Quad Q9400, Linksys E1550 Router - WiFi n
Samsung LN46C630KLF TV
Post Reply