using System; using System.Collections.Generic; using System.Linq; using System.Text; using libxmltv.Interfaces; using System.Xml.Linq; using System.Globalization; using System.Windows.Forms; namespace libxmltv.Core { internal class XMLTVProgramCollection : IDisposable { private Dictionary entries = new Dictionary(); internal static void CreateInstance(XMLTVRuntimeInstance xmltv) { using (XMLTVProgramCollection g = new XMLTVProgramCollection(xmltv)) { g.instance.Programs = g.Collection; } } private XMLTVRuntimeInstance instance; protected XMLTVProgramCollection(XMLTVRuntimeInstance xmltv) { XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgramCollection"); //IXMLTV_PARSER _xmltv; //if (!Internals.VerifyInstance(xmltv, out _xmltv)) { return; } //XMLTV_PARSER = _xmltv; instance = xmltv; Create(); } #region IXMLTVSource //private IXMLTV_PARSER XMLTV_PARSER { get; set; } public Dictionary Collection { get { return entries; } } #endregion // private DateTime ParseDate(string timeStamp) { DateTime dt = new DateTime(); try { dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture); } catch (Exception ex) { throw ex; } return dt; } private void Create() { var doc = instance.XmlDoc; int index = 0; foreach (var c in doc.Descendants(XMLTVConstants.PROGRAM_ELEMENT)) { Program program = new Program(); program.Id = index; XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("program_Id: {0}", program.Id); if (c.HasAttributes) { var start = c.Attribute(XMLTVConstants.Programs.ProgramStart).Value; program.Start = ParseDate(start); XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); var stop = c.Attribute(XMLTVConstants.Programs.ProgramStop).Value; program.Stop = ParseDate(stop); XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); var channelid = c.Attribute(XMLTVConstants.Programs.ProgramChannelId).Value; XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); IXMLTVChannel channel = new Channel(); try { channel = instance.Channels[channelid]; } catch (KeyNotFoundException) { XMLTV_LOGGER.Log.Verbose.Error.WriteLine(string.Format("Unable to find Channel by id: '{0}' for this program.", channelid)); } program.Channel = channel; XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channel: {0}", program.Channel.ToString()); } try { var title = c.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault().Value; program.Title = title; } catch (Exception) { program.Title = string.Empty; } XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_title: {0}", program.Title == string.Empty ? "empty" : program.Title); try { var subtitle = c.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault().Value; program.SubTitle = subtitle; } catch (Exception) { program.SubTitle = string.Empty; } XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", program.SubTitle == string.Empty ? "empty" : program.SubTitle); try { var description = c.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault().Value; program.Description = description; } catch (Exception) { program.Description = string.Empty; } XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_description: {0}", program.Description == string.Empty ? "empty" : program.Description); entries.Add(program.Id, program); Application.DoEvents(); index++; } //instance.Programs = Collection; } public override string ToString() { return string.Format("Program Count: {0}", Collection == null ? 0 : Collection.Count); } public void Dispose() { //throw new NotImplementedException(); } } [Serializable] internal class Program : IXMLTVProgram { public Program() { Id = 0; Start = new DateTime(); Stop = new DateTime(); Channel = new Channel(); Title = string.Empty; SubTitle = string.Empty; Description = string.Empty; } #region IXMLTVProgram members public int Id { get; set; } public DateTime Start { get; set; } public DateTime Stop { get; set; } public IXMLTVChannel Channel { get; set; } public string Title { get; set; } public string SubTitle { get; set; } public string Description { get; set; } #endregion public override string ToString() { return string.Format("{0}: {1} - {2} ({3}) ['{4}' <==> '{5}']", Id, Title, SubTitle, Channel.ToString(), Start.ToString("yyyy/MM/dd hh:mm tt"), Stop.ToString("yyyy/MM/dd hh:mm tt")); } } }