MediaPortal.cs
changeset 5 29ccfbf98e54
parent 4 8372aa8d6292
child 7 bf908f6c7758
     1.1 --- a/MediaPortal.cs	Fri May 15 22:41:33 2015 +0200
     1.2 +++ b/MediaPortal.cs	Sat May 16 01:31:32 2015 +0200
     1.3 @@ -5,6 +5,7 @@
     1.4  using System.Text;
     1.5  using System.Threading.Tasks;
     1.6  using System.Xml;
     1.7 +using System.IO;
     1.8  
     1.9  namespace SatChanGen
    1.10  {
    1.11 @@ -144,9 +145,18 @@
    1.12  
    1.13  		}
    1.14  
    1.15 +        //
    1.16 +        //
    1.17 +        //
    1.18  		public static void Export(List<Channel> aChannels, List<string> aTunerCards,  string aFileName, bool aAddMapping)
    1.19  		{
    1.20 +            //Create a dictionary for our groups
    1.21 +            Dictionary<string, List<Channel>> groups = new Dictionary<string, List<Channel>>();
    1.22 +
    1.23  			XmlDocument xmlDoc = new XmlDocument();
    1.24 +            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
    1.25 +            xmlDoc.AppendChild(xmlDeclaration);
    1.26 +
    1.27  			XmlNode rootElement = xmlDoc.CreateElement("tvserver");			
    1.28  			AddAttribute(rootElement, "version", "1.0");
    1.29  			xmlDoc.AppendChild(rootElement);
    1.30 @@ -155,38 +165,49 @@
    1.31  			rootElement.AppendChild(nodechannels);
    1.32  
    1.33  			int channelId = 0;
    1.34 -      int channelMapId = 0;
    1.35 +            int channelMapId = 0;
    1.36  			foreach (Channel channel in aChannels)
    1.37  			{
    1.38 +                //Create group if needed
    1.39 +                if (!groups.ContainsKey(channel.Category)) 
    1.40 +                {
    1.41 +                    //KeyValuePair<string, List<Channel>> entry=new KeyValuePair<string, List<Channel>>();
    1.42 +                    groups.Add(channel.Category, new List<Channel>());
    1.43 +                }
    1.44 +
    1.45 +                //Add that channel to its group
    1.46 +                groups[channel.Category].Add(channel);
    1.47 +
    1.48 +                //Create and populate channel element
    1.49  				channelId++;
    1.50  				XmlNode nodechannel = xmlDoc.CreateElement("channel");
    1.51  				AddAttribute(nodechannel, "GrabEpg", "True");
    1.52  				AddAttribute(nodechannel, "IdChannel", channelId);
    1.53  				AddAttribute(nodechannel, "IsRadio", "False");
    1.54  				AddAttribute(nodechannel, "IsTv", "True");
    1.55 -        AddAttribute(nodechannel, "LastGrabTime", "2000-1-1 0:0:0");
    1.56 +                AddAttribute(nodechannel, "LastGrabTime", "2000-1-1 0:0:0");
    1.57  				//AddAttribute(nodechannel, "SortOrder", channel.SortOrder);
    1.58 -        //sAddAttribute(nodechannel, "TimesWatched", "0");
    1.59 -        AddAttribute(nodechannel, "TotalTimeWatched", "2000-1-1 0:0:0");
    1.60 +                //AddAttribute(nodechannel, "TimesWatched", "0");
    1.61 +                AddAttribute(nodechannel, "TotalTimeWatched", "2000-1-1 0:0:0");
    1.62  				AddAttribute(nodechannel, "VisibleInGuide", "True");
    1.63  				AddAttribute(nodechannel, "DisplayName", channel.Name);
    1.64  				AddAttribute(nodechannel, "ChannelNumber", channelId);
    1.65  
    1.66 -        if (aAddMapping)
    1.67 -        {
    1.68 -          //I don't think we can get mapping to work without having corresponding server and card definition elements
    1.69 -          XmlNode nodeMaps = xmlDoc.CreateElement("mappings");
    1.70 -          foreach (string tuner in aTunerCards)
    1.71 -          {
    1.72 -            channelMapId++;
    1.73 -            XmlNode nodeMap = xmlDoc.CreateElement("map");
    1.74 -            AddAttribute(nodeMap, "IdCard", tuner);
    1.75 -            AddAttribute(nodeMap, "IdChannel", channelId);
    1.76 -            AddAttribute(nodeMap, "IdChannelMap", channelMapId);
    1.77 -            nodeMaps.AppendChild(nodeMap);
    1.78 -          }
    1.79 -          nodechannel.AppendChild(nodeMaps);
    1.80 -        }
    1.81 +                if (aAddMapping)
    1.82 +                {
    1.83 +                    //I don't think we can get mapping to work without having corresponding server and card definition elements
    1.84 +                    XmlNode nodeMaps = xmlDoc.CreateElement("mappings");
    1.85 +                    foreach (string tuner in aTunerCards)
    1.86 +                    {
    1.87 +                        channelMapId++;
    1.88 +                        XmlNode nodeMap = xmlDoc.CreateElement("map");
    1.89 +                        AddAttribute(nodeMap, "IdCard", tuner);
    1.90 +                        AddAttribute(nodeMap, "IdChannel", channelId);
    1.91 +                        AddAttribute(nodeMap, "IdChannelMap", channelMapId);
    1.92 +                        nodeMaps.AppendChild(nodeMap);
    1.93 +                    }
    1.94 +                    nodechannel.AppendChild(nodeMaps);
    1.95 +                }
    1.96  
    1.97  				XmlNode nodeTuningDetails = xmlDoc.CreateElement("TuningDetails");
    1.98  				XmlNode nodeTune = xmlDoc.CreateElement("tune");
    1.99 @@ -229,9 +250,43 @@
   1.100  				nodechannels.AppendChild(nodechannel);				
   1.101  			}
   1.102  
   1.103 +            //Create groups element
   1.104 +            XmlNode nodeChannelGroups = xmlDoc.CreateElement("channelgroups");
   1.105 +            rootElement.AppendChild(nodeChannelGroups);
   1.106  
   1.107 +            int groupSortOrder = 0;
   1.108 +            foreach (KeyValuePair<string, List<Channel>> group in groups)
   1.109 +            {
   1.110 +                //Create group element
   1.111 +                XmlNode nodeChannelGroup = xmlDoc.CreateElement("channelgroup");
   1.112 +                AddAttribute(nodeChannelGroup, "GroupName", group.Key);
   1.113 +                AddAttribute(nodeChannelGroup, "SortOrder", groupSortOrder.ToString());
   1.114 +                XmlNode nodeGroupMap = xmlDoc.CreateElement("mappings");
   1.115 +                nodeChannelGroup.AppendChild(nodeGroupMap);
   1.116 +                nodeChannelGroups.AppendChild(nodeChannelGroup);
   1.117  
   1.118 -			xmlDoc.Save(aFileName);
   1.119 +                //Sort by name
   1.120 +                List<Channel> sortedChannels = group.Value.OrderBy(o => o.Name).ToList();
   1.121 +
   1.122 +                //Add each channel to its group
   1.123 +                int channelSortOrder = 0;
   1.124 +                foreach (Channel channel in sortedChannels)
   1.125 +                {
   1.126 +                    XmlNode nodeMap = xmlDoc.CreateElement("map");
   1.127 +                    AddAttribute(nodeMap, "ChannelName", channel.Name);
   1.128 +                    AddAttribute(nodeMap, "SortOrder", channelSortOrder.ToString());
   1.129 +                    nodeGroupMap.AppendChild(nodeMap);
   1.130 +                    channelSortOrder++;
   1.131 +                }
   1.132 +
   1.133 +                groupSortOrder++;
   1.134 +            }
   1.135 +
   1.136 +            using (TextWriter sw = new StreamWriter(aFileName, false, Encoding.UTF8)) //Set encoding
   1.137 +            {
   1.138 +                xmlDoc.Save(sw);
   1.139 +            }
   1.140 +			//xmlDoc.Save(aFileName);
   1.141  		}
   1.142  
   1.143  		private static void AddAttribute(XmlNode node, string tagName, string tagValue)