public OpResult CommandList(int port)
        {
            OpResult opResult = new OpResult(OpStatusCode.Ok);

            if (port != 0)
            {
                opResult.AppendFormat("=== Ports: ==========");
                opResult.AppendFormat("TCP/IP Socket port: {0}", port);
                opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10));
            }
            opResult.AppendFormat("=== Connection Commands: ==========");
            opResult.AppendFormat("help - Shows this page");
            opResult.AppendFormat("exit - Closes the socket connection");
            foreach (KeyValuePair <string, IBaseCommand> cmd in m_commands)
            {
                opResult.AppendFormat("{0} {1}", cmd.Key, (cmd.Value == null) ? "" : cmd.Value.ShowSyntax());
            }
            return(opResult);
        }
 /// <summary>
 /// Executes a command with the given parameter string and returns a string return
 /// </summary>
 /// <param name="command">command name string</param>
 /// <param name="param">parameter string</param>
 /// <param name="result">string</param>
 /// <returns></returns>
 public OpResult Execute(String command, string param)
 {
     command = command.ToLower();
     if (m_commands.ContainsKey(command))
     {
         try
         {
             return(m_commands[command].Execute(param));
         }
         catch (Exception ex)
         {
             OpResult opResult = new OpResult();
             opResult.StatusCode = OpStatusCode.Exception;
             opResult.StatusText = ex.Message;
             opResult.AppendFormat(ex.Message);
             return(opResult);
         }
     }
     else
     {
         return(new OpResult(OpStatusCode.BadRequest));
     }
 }
 public OpResult Execute(String command, string param, ServerSettings settings, RemotedWindowsMediaPlayer remotePlayer)
 {
     command = command.ToLower();
     if (m_commands.ContainsKey(command))
     {
         try
         {
             if (command.Equals("music-list-stats"))
             {
                 return(ExecuteLibraryStats(settings));
             }
             else if (m_commands[command] is MusicCmd)
             {
                 //Make sure cache is not being modified before executing any of the music-* commands
                 if (cacheLock.TryEnterReadLock(10))
                 {
                     try
                     {
                         return(((ICommand)m_commands[command]).Execute(param));
                     }
                     finally
                     {
                         cacheLock.ExitReadLock();
                     }
                 }
                 else
                 {
                     return(getSettings(settings, true));
                 }
             }
             else if (m_commands[command] is IExperienceCommand || m_commands[command] is IWmpCommand)
             {
                 //Only allow one thread at a time to access remoted player
                 waitHandle.WaitOne();
                 try
                 {
                     if (m_commands[command] is IWmpCommand)
                     {
                         return(((IWmpCommand)m_commands[command]).Execute(remotePlayer, param));
                     }
                     else
                     {
                         return(((IExperienceCommand)m_commands[command]).ExecuteMediaExperience(param));
                     }
                 }
                 finally
                 {
                     waitHandle.Set();
                 }
             }
             else
             {
                 return(((ICommand)m_commands[command]).Execute(param));
             }
         }
         catch (Exception ex)
         {
             OpResult opResult = new OpResult();
             opResult.StatusCode = OpStatusCode.Exception;
             opResult.StatusText = ex.Message;
             opResult.AppendFormat(ex.Message);
             return(opResult);
         }
     }
     else
     {
         return(new OpResult(OpStatusCode.BadRequest));
     }
 }
        public OpResult CommandListHTML(int port)
        {
            OpResult opResult = new OpResult(OpStatusCode.Ok);

            string page_start =
                "<html><head><script LANGUAGE='JavaScript'>\r\n" +
                "function toggle (o){ var all=o.childNodes;  if (all[0].childNodes[0].innerText == '+') open(o,true);  else open(o,false);}\r\n" +
                "function open (o, b) { var all=o.childNodes;  if (b) {all[0].childNodes[0].innerText='-';all[1].style.display='inline';}  else {all[0].childNodes[0].innerText='+';all[1].style.display='none';}}\r\n" +
                "function toggleAll (b){ var all=document.childNodes[0].childNodes[1].childNodes; for (var i=0; i<all.length; i++) {if(all[i].id=='section') open(all[i],b)};}\r\n" +
                "</script></head>\r\n" +
                "<body><a id='top'>Jump to</a>: <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a>, <a href='#bottom'>Bottom</a><hr><font size=+3><a id='commands'>Command List</a>:&nbsp;&nbsp;</font>[<a onclick='toggleAll(true);' >Open All</a>] | [<a onclick='toggleAll(false);' >Collapse All</a>]<hr>\r\n";
            string page_end =
                "</pre></span></div>\r\n" +
                "<br><hr><b><a id='examples'>Note: URLs must be correctly encoded</a></b><hr><br>\r\n" +
                "<b>Note - The following custom examples require that:</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 - Custom formats artist_browse and artist_list are defined in the &quot;music.template&quot; file<br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 - The &quot;music.template&quot; file has been copied to the ehome directory (usually C:\\Windows\\ehome)<br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 - Windows Media Center has been restarted after #1 and #2<br>\r\n" +
                "<br><b>Working track browser using custom formats: (can be slow... but this works as an album browser)</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Display complete artist list linked to albums: <a href='music-list-details%20template:artist_list'>http://hostname:40510/music-list-details%20template:artist_list</a><br>\r\n" +
                "<br><b>Examples using artist filter: (warning can be very slow with large libraries)</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All artists: <a href='music-list-artists'>http://hostname:40510/music-list-artists</a><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All albums: <a href='music-list-albums'>http://hostname:40510/music-list-albums</a><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All albums by artists starting with the letter &quot;A&quot;: <a href='music-list-albums%20artist:a'>http://hostname:40510/music-list-albums%20artist:a</a><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Play the tenth and thirteenth song in your collection: <a href='music-play%20indexes:10,13'>http://hostname:40510/music-play%20indexes:10,13</a><br>\r\n" +
                "<br><b>Examples using custom formats and artist match: (can be slow...)</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Display pretty albums and tracks by the first artist starting with &quot;Jack&quot;: <a href='music-list-details%20template:artist_browse%20artist:jack'>http://hostname:40510/music-list-details%20template:artist_browse%20artist:jack</a><br>\r\n" +
                "<br><b>More help:</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Help on the music commands: <a href='music-list-artists%20-help'>http://hostname:40510/music-list-artists%20-help</a><br>\r\n" +
                "<br><hr>\r\n" +
                "<a id='bottom'>Generated by</a>: Windows Media Center TCP/IP Controller (<a href='http://github.com/GeekGoneOld/MceController'>WMCController Home</a>)\r\n" +
                "<hr>Jump to: <a href='#top'>Top</a>, <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a><br>\r\n" +
                "<script LANGUAGE='JavaScript'>toggleAll(false);</script></body></html>\r\n";

            string header_start = "</pre></span></div><br><div id='section' onclick='toggle(this)' style='border:solid 1px black;'><font size=+1 style='font:15pt courier;'><span>-</span>";
            string header_end   = "</font><span style='display:'><pre>";

            opResult.AppendFormat("{0}", page_start);

            if (port != 0)
            {
                opResult.AppendFormat("{0} Ports: {1}", header_start, header_end);
                opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10));
            }
            opResult.AppendFormat("{0} Connection Commands: {1}", header_start, header_end);
            opResult.AppendFormat("<a href='/help'>help</a> - Shows this page");
            opResult.AppendFormat("<a href='/help'>exit</a> - Closes the socket connection");
            foreach (KeyValuePair <string, IBaseCommand> cmd in m_commands)
            {
                if (cmd.Key.StartsWith("==="))
                {
                    opResult.AppendFormat(cmd.Key.Replace("==========", header_end).Replace("===", header_start));
                }
                else
                {
                    opResult.AppendFormat("<a href='/{0}'>{1}</a> {2}",
                                          cmd.Key, cmd.Key, (cmd.Value == null) ? "" : cmd.Value.ShowSyntax().Replace("<", "&lt;").Replace(">", "&gt;"));
                }
            }
            opResult.AppendFormat("{0}", page_end);
            return(opResult);
        }
 /// <summary>
 /// Executes a command with the given parameter string and returns a string return
 /// </summary>
 /// <param name="command">command name string</param>
 /// <param name="param">parameter string</param>
 /// <param name="playlist">now playing playlist, may be null</param>
 /// <param name="result">string</param>
 /// <returns></returns>
 public OpResult Execute(String command, string param, NowPlayingList playlist, MediaItem currentItem)
 {
     command = command.ToLower();
     if (m_commands.ContainsKey(command))
     {
         try
         {
             if (command.Equals("music-cache-status"))
             {
                 OpResult opResult = new OpResult();
                 CacheStatus status = new CacheStatus(this.isCacheBuilding);
                 opResult.StatusCode = OpStatusCode.Json;
                 opResult.ContentText = JsonConvert.SerializeObject(status, Newtonsoft.Json.Formatting.Indented);
                 return opResult;
             }
             else if (m_commands[command] is MusicICommand)
             {
                 //Make sure cache is not being modified before executing any of the music-* commands
                 lock (cacheLock)
                 {
                     return ((MusicICommand)m_commands[command]).Execute(param, playlist, currentItem);
                 }
             }
             else
             {
                 return m_commands[command].Execute(param);
             }
         }
         catch (Exception ex)
         {
             OpResult opResult = new OpResult();
             opResult.StatusCode = OpStatusCode.Exception;
             opResult.StatusText = ex.Message;
             opResult.AppendFormat(ex.Message);
             return opResult;
         }
     }
     else
     {
         return new OpResult(OpStatusCode.BadRequest);
     }
 }
        public OpResult CommandListHTML(int port)
        {
            OpResult opResult = new OpResult(OpStatusCode.Ok);

            string page_start =
                "<html><head><script LANGUAGE='JavaScript'>\r\n" +
                "function toggle (o){ var all=o.childNodes;  if (all[0].childNodes[0].innerText == '+') open(o,true);  else open(o,false);}\r\n" +
                "function open (o, b) { var all=o.childNodes;  if (b) {all[0].childNodes[0].innerText='-';all[1].style.display='inline';}  else {all[0].childNodes[0].innerText='+';all[1].style.display='none';}}\r\n" +
                "function toggleAll (b){ var all=document.childNodes[0].childNodes[1].childNodes; for (var i=0; i<all.length; i++) {if(all[i].id=='section') open(all[i],b)};}\r\n" +
                "</script></head>\r\n" +
                "<body><a id='top'>Jump to</a>: <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a>, <a href='#bottom'>Bottom</a><hr><font size=+3><a id='commands'>Command List</a>:&nbsp;&nbsp;</font>[<a onclick='toggleAll(true);' >Open All</a>] | [<a onclick='toggleAll(false);' >Collapse All</a>]<hr>\r\n";
            string page_end =
                "</pre></span></div>\r\n" +
                "<br><hr><b><a id='examples'>Note: URLs must be correctly encoded</a></b><hr><br>\r\n" +
                "<b>Note - The following custom examples require that:</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 - Custom formats artist_browse and artist_list are defined in the &quot;music.template&quot; file<br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 - The &quot;music.template&quot; file has been copied to the ehome directory (usually C:\\Windows\\ehome)<br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 - Vista Media Center has been restarted after #1 and #2<br>\r\n" +
                "<br><b>Working track browser using custom formats: (can be slow... but this works as an album browser)</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Display complete artist list linked to albums: <a href='music-list-details%20template:artist_list'>http://hostname:40510/music-list-details%20template:artist_list</a><br>\r\n" +
                "<br><b>Examples using artist filter: (warning can be very slow with large libraries)</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All artists: <a href='music-list-artists'>http://hostname:40510/music-list-artists</a><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All albums: <a href='music-list-albums'>http://hostname:40510/music-list-albums</a><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;All albums by artists starting with the letter &quot;A&quot;: <a href='music-list-albums%20artist:a'>http://hostname:40510/music-list-albums%20artist:a</a><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Play the tenth and thirteenth song in your collection: <a href='music-play%20indexes:10,13'>http://hostname:40510/music-play%20indexes:10,13</a><br>\r\n" +
                "<br><b>Examples using custom formats and artist match: (can be slow...)</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Display pretty albums and tracks by the first artist starting with &quot;Jack&quot;: <a href='music-list-details%20template:artist_browse%20artist:jack'>http://hostname:40510/music-list-details%20template:artist_browse%20artist:jack</a><br>\r\n" +
                "<br><b>More help:</b><br>\r\n" +
                "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Help on the music commands: <a href='music-list-artists%20-help'>http://hostname:40510/music-list-artists%20-help</a><br>\r\n" +
                "<br><hr>\r\n" +
                "<a id='bottom'>Generated by</a>: Vista Media Center TCP/IP Controller (<a href='http://www.codeplex.com/VmcController'>vmcController Home</a>)\r\n" +
                "<hr>Jump to: <a href='#top'>Top</a>, <a href='#commands'>Command List</a>, <a href='#examples'>Notes and Examples</a><br>\r\n" +
                "<script LANGUAGE='JavaScript'>toggleAll(false);</script></body></html>\r\n";

            string header_start = "</pre></span></div><br><div id='section' onclick='toggle(this)' style='border:solid 1px black;'><font size=+1 style='font:15pt courier;'><span>+</span>";
            string header_end = "</font><span style='display:'><pre>";

            opResult.AppendFormat("{0}", page_start);

            if (port != 0)
            {
                opResult.AppendFormat("{0} Ports: {1}", header_start, header_end);
                opResult.AppendFormat("TCP/IP Socket port: {0}", port);
                opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10));
            }
            opResult.AppendFormat("{0} Connection Commands: {1}", header_start, header_end);
            opResult.AppendFormat("<a href='/help'>help</a> - Shows this page");
            opResult.AppendFormat("<a href='/help'>exit</a> - Closes the socket connection");
            foreach (KeyValuePair<string, ICommand> cmd in m_commands)
            {
                if (cmd.Key.StartsWith("==="))
                    opResult.AppendFormat(cmd.Key.Replace("==========", header_end).Replace("===", header_start));
                else
                    opResult.AppendFormat("<a href='/{0}'>{1}</a> {2}",
                        cmd.Key, cmd.Key, (cmd.Value == null) ? "" : cmd.Value.ShowSyntax().Replace("<", "&lt;").Replace(">", "&gt;"));
            }
            opResult.AppendFormat("{0}", page_end);
            return opResult;
        }
 public OpResult CommandList(int port)
 {
     OpResult opResult = new OpResult(OpStatusCode.Ok);
     if (port != 0)
     {
         opResult.AppendFormat("=== Ports: ==========");
         opResult.AppendFormat("TCP/IP Socket port: {0}", port);
         opResult.AppendFormat("HTTP Server port: {0} (http://your_server:{1}/)", (port + 10), (port + 10));
     }
     opResult.AppendFormat("=== Connection Commands: ==========");
     opResult.AppendFormat("help - Shows this page");
     opResult.AppendFormat("exit - Closes the socket connection");
     foreach (KeyValuePair<string, ICommand> cmd in m_commands)
     {
         opResult.AppendFormat("{0} {1}", cmd.Key, (cmd.Value == null) ? "" : cmd.Value.ShowSyntax());
     }
     return opResult;
 }
 /// <summary>
 /// Executes a command with the given parameter string and returns a string return
 /// </summary>
 /// <param name="command">command name string</param>
 /// <param name="param">parameter string</param>
 /// <param name="result">string</param>
 /// <returns></returns>
 public OpResult Execute(String command, string param)
 {
     command = command.ToLower();
     if (m_commands.ContainsKey(command))
     {
         try
         {
             return m_commands[command].Execute(param);
         }
         catch (Exception ex)
         {
             OpResult opResult = new OpResult();
             opResult.StatusCode = OpStatusCode.Exception;
             opResult.StatusText = ex.Message;
             opResult.AppendFormat(ex.Message);
             return opResult;
         }
     }
     else
     {
         return new OpResult(OpStatusCode.BadRequest);
     }
 }