# HG changeset patch
# User StephaneLenclud
# Date 1431695778 -7200
# Node ID d73e99c5333caf6e4ae892de701f81b1215d4548
# Parent  f203331a8f4afcdd8f4a418a81ee7220775f0516
First MP export draft.

diff -r f203331a8f4a -r d73e99c5333c MainForm.cs
--- a/MainForm.cs	Fri May 15 13:23:52 2015 +0200
+++ b/MainForm.cs	Fri May 15 15:16:18 2015 +0200
@@ -25,6 +25,7 @@
         private void buttonGenerate_Click(object sender, EventArgs e)
         {
 	        List<Channel> channels=KingOfSat.Parse("http://en.kingofsat.net/pack-skygermany.php");
+			MediaPortal.Export(channels,"channels.xml");
         }
     }
 }
diff -r f203331a8f4a -r d73e99c5333c MediaPortal.cs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MediaPortal.cs	Fri May 15 15:16:18 2015 +0200
@@ -0,0 +1,246 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml;
+
+namespace SatChanGen
+{
+	class MediaPortal
+	{
+
+		public static string Polarisation(Channel aChannel)
+		{
+			switch (aChannel.Polarisation)
+			{
+				case "H":
+					return "1";
+				case "V":
+					return "2";
+				case "L": //Check that on KingOfSat
+					return "3";
+				case "R": //Check that on KingOfSat
+					return "4";
+
+				default:
+					{
+						Debug.Write("WARNING: Unknown Polarisation " + aChannel.Polarisation + " for " + aChannel.Name + "\n");
+						return "-1";
+					}
+			}
+
+			/*
+			"Not Set",
+            "Not Defined",
+            "Horizontal",
+            "Vertical",
+            "Circular Left",
+            "Circular Right"
+			 */
+		}
+
+		public static string Modulation(Channel aChannel)
+		{
+			//Those can be worked out from TvLibrary FormDVBSTuningDetail.Designer.cs
+
+			switch (aChannel.Modulation)
+			{
+				case "QPSK":
+					return "20";
+				case "8PSK":
+					return "27";
+				default:
+				{
+					Debug.Write("WARNING: Unknown modulation " + aChannel.Modulation + " for " + aChannel.Name + "\n");
+					return "-1";
+				}
+			}
+
+
+			/*
+			"Not Set",
+			"Not Defined",
+			"16 QAM",
+			"32 QAM",
+			"64 QAM",
+			"80 QAM",
+			"96 QAM",
+			"112 QAM",
+			"128 QAM",
+			"160 QAM",
+			"192 QAM",
+			"224 QAM",
+			"256 QAM",
+			"320 QAM",
+			"384 QAM",
+			"448 QAM",
+			"512 QAM",
+			"640 QAM",
+			"768 QAM",
+			"896 QAM",
+			"1024 QAM",
+			"QPSK",
+			"BPSK",
+			"OQPSK",
+			"8 VSB",
+			"16 VSB",
+			"Analog Amplitude",
+			"Analog Frequency",
+			"8 PSK",
+			"RF",
+			"16 APSK",
+			"32 APSK",
+			"QPSK2 (DVB-S2)",
+			"8 PSK2 (DVB-S2)",
+			"DirectTV"
+			 */
+		}
+
+
+		public static string FEC(Channel aChannel)
+		{
+			//Those can be worked out from TvLibrary FormDVBSTuningDetail.Designer.cs
+
+			switch (aChannel.FEC)
+			{
+				case "1/2":
+					return "1";
+				case "2/3":
+					return "2";
+				case "3/4":
+					return "3";
+				case "3/5":
+					return "4";
+				case "4/5":
+					return "5";
+				case "5/6":
+					return "6";
+				case "5/11":
+					return "7";
+				case "7/8":
+					return "8";
+				case "1/4":
+					return "9";
+				case "1/3":
+					return "10";
+				case "2/5":
+					return "11";
+				case "6/7":
+					return "12";
+				case "8/9":
+					return "13";
+				case "9/10":
+					return "14";
+
+				default:
+				{
+					Debug.Write("WARNING: Unknown FEC " + aChannel.FEC + " for " + aChannel.Name + "\n");
+					return "-1";
+				}
+			}
+
+
+		}
+
+		public static void Export(List<Channel> aChannels, string aFileName)
+		{
+			XmlDocument xmlDoc = new XmlDocument();
+			XmlNode rootElement = xmlDoc.CreateElement("tvserver");			
+			AddAttribute(rootElement, "version", "1.0");
+			xmlDoc.AppendChild(rootElement);
+			//Create our channel element
+			XmlNode nodechannels = xmlDoc.CreateElement("channels");			
+			rootElement.AppendChild(nodechannels);
+
+			int channelId = 0;
+			foreach (Channel channel in aChannels)
+			{
+				channelId++;
+				XmlNode nodechannel = xmlDoc.CreateElement("channel");
+				AddAttribute(nodechannel, "GrabEpg", "True");
+				AddAttribute(nodechannel, "IdChannel", channelId);
+				AddAttribute(nodechannel, "IsRadio", "False");
+				AddAttribute(nodechannel, "IsTv", "True");
+				//AddAttribute(nodechannel, "LastGrabTime", channel.LastGrabTime);
+				//AddAttribute(nodechannel, "SortOrder", channel.SortOrder);
+				//AddAttribute(nodechannel, "TimesWatched", channel.TimesWatched);
+				//AddAttribute(nodechannel, "TotalTimeWatched", channel.TotalTimeWatched);
+				AddAttribute(nodechannel, "VisibleInGuide", "True");
+				AddAttribute(nodechannel, "DisplayName", channel.Name);
+				AddAttribute(nodechannel, "ChannelNumber", channelId);
+
+
+				XmlNode nodeTuningDetails = xmlDoc.CreateElement("TuningDetails");
+				XmlNode nodeTune = xmlDoc.CreateElement("tune");
+				AddAttribute(nodeTune, "IdChannel", channelId);
+				AddAttribute(nodeTune, "IdTuning", channelId);
+				AddAttribute(nodeTune, "Bandwidth", "8"); //Weird
+				AddAttribute(nodeTune, "ChannelNumber", "10000");
+				AddAttribute(nodeTune, "ChannelType", "3"); //Bad hard coding, does it means DVB-S
+				AddAttribute(nodeTune, "CountryId", "31"); //Is that Germany?
+				AddAttribute(nodeTune, "Diseqc", "0"); //Disabled
+				AddAttribute(nodeTune, "FreeToAir", "False"); //Can we get that from KingOfSat? Does it matter?
+				AddAttribute(nodeTune, "Frequency", ((Int32)(Convert.ToDouble(channel.Frequency)*1000)).ToString());
+				AddAttribute(nodeTune, "MajorChannel", "-1");
+				AddAttribute(nodeTune, "MinorChannel", "-1");
+				AddAttribute(nodeTune, "Modulation", Modulation(channel));
+				AddAttribute(nodeTune, "Name", channel.Name);
+				AddAttribute(nodeTune, "NetworkId", channel.NetworkID);
+				AddAttribute(nodeTune, "PmtPid", channel.PMT);
+				AddAttribute(nodeTune, "Polarisation", Polarisation(channel));
+				AddAttribute(nodeTune, "Provider", channel.Provider); //Does that matter? Should really be SKY or CSAT
+				AddAttribute(nodeTune, "ServiceId", channel.SID);
+				AddAttribute(nodeTune, "SwitchingFrequency", "11700000"); //Does it matter?
+				AddAttribute(nodeTune, "Symbolrate", channel.SymbolRate);
+				AddAttribute(nodeTune, "TransportId", channel.TransponderID);
+				AddAttribute(nodeTune, "TuningSource", "0"); //Is that needed?
+				AddAttribute(nodeTune, "VideoSource", "0"); //Is that needed?
+				AddAttribute(nodeTune, "AudioSource", "0"); //Is that needed?
+				AddAttribute(nodeTune, "IsVCRSignal", "False");
+				AddAttribute(nodeTune, "SatIndex", "-1");
+				AddAttribute(nodeTune, "InnerFecRate", FEC(channel));
+				AddAttribute(nodeTune, "Band", "0"); //Needed?
+				AddAttribute(nodeTune, "Pilot", "-1");
+				AddAttribute(nodeTune, "RollOff", "-1");
+				AddAttribute(nodeTune, "Url", "");
+				AddAttribute(nodeTune, "Bitrate", "0"); //Should we bother putting it in there?
+				nodeTuningDetails.AppendChild(nodeTune);
+					
+				nodechannel.AppendChild(nodeTuningDetails);
+
+				nodechannels.AppendChild(nodechannel);				
+			}
+
+
+
+			xmlDoc.Save(aFileName);
+		}
+
+		private static void AddAttribute(XmlNode node, string tagName, string tagValue)
+		{
+			XmlAttribute attr = node.OwnerDocument.CreateAttribute(tagName);
+			attr.InnerText = tagValue;
+			node.Attributes.Append(attr);
+		}
+
+		private static void AddAttribute(XmlNode node, string tagName, int tagValue)
+		{
+			AddAttribute(node, tagName, tagValue.ToString());
+		}
+
+		// store DateTime Values as strings. Improves readability
+		private static void AddAttribute(XmlNode node, string tagName, DateTime tagValue)
+		{
+			AddAttribute(node, tagName,
+						 String.Format("{0}-{1}-{2} {3}:{4}:{5}", tagValue.Year, tagValue.Month, tagValue.Day, tagValue.Hour,
+									   tagValue.Minute, tagValue.Second));
+		}
+
+		private static void AddAttribute(XmlNode node, string tagName, bool tagValue)
+		{
+			AddAttribute(node, tagName, tagValue.ToString());
+		}
+	}
+}
diff -r f203331a8f4a -r d73e99c5333c SatChanGen.csproj
--- a/SatChanGen.csproj	Fri May 15 13:23:52 2015 +0200
+++ b/SatChanGen.csproj	Fri May 15 15:16:18 2015 +0200
@@ -48,6 +48,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Channel.cs" />
+    <Compile Include="MediaPortal.cs" />
     <Compile Include="KingOfSat.cs" />
     <Compile Include="MainForm.cs">
       <SubType>Form</SubType>