1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using libxmltv.Interfaces; |
6 |
using System.Xml.Linq; |
7 |
using System.Globalization; |
8 |
using System.Windows.Forms; |
9 |
|
10 |
namespace libxmltv.Core |
11 |
{ |
12 |
internal class XMLTVProgramCollection : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgramCollection, IDisposable |
13 |
{ |
14 |
private Dictionary<int, IXMLTVProgram> entries = new Dictionary<int, IXMLTVProgram>(); |
15 |
//internal static void CreateInstance(XMLTVRuntimeInstance xmltv) |
16 |
//{ |
17 |
// using (XMLTVProgramCollection g = new XMLTVProgramCollection(xmltv)) { g.instance.Programs = g.Collection; } |
18 |
//} |
19 |
//private XMLTVRuntimeInstance instance; |
20 |
protected XMLTVProgramCollection(XMLTVRuntimeInstance instance) : base(instance) |
21 |
{ |
22 |
xmltv_logger.Debug.WriteLine("Creating Instance of XMLTVProgramCollection"); |
23 |
Create(); |
24 |
xmltv_logger.Debug.WriteLine("Created Instance of XMLTVProgramCollection"); |
25 |
xmltv_logger.Info.WriteLine(this.ToString()); |
26 |
} |
27 |
|
28 |
#region IXMLTVProgramCollection members |
29 |
public Dictionary<int, IXMLTVProgram> Collection |
30 |
{ |
31 |
get { return entries; } |
32 |
} |
33 |
#endregion |
34 |
|
35 |
private DateTime ParseDate(string timeStamp) |
36 |
{ |
37 |
DateTime dt = new DateTime(); |
38 |
try |
39 |
{ |
40 |
dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture); |
41 |
} |
42 |
catch (Exception ex) { throw ex; } |
43 |
return dt; |
44 |
} |
45 |
private void Create() |
46 |
{ |
47 |
var doc = XDocument.Parse(this.GetInstance().XmlDoc); |
48 |
double program_index = 0; |
49 |
double program_count = doc.Descendants(XMLTVConstants.PROGRAM_ELEMENT).Count(); |
50 |
double progress = 0; |
51 |
foreach (var c in doc.Descendants(XMLTVConstants.PROGRAM_ELEMENT)) |
52 |
{ |
53 |
if (this.GetInstance().IsAborting) |
54 |
{ |
55 |
xmltv_logger.Verbose.Debug.WriteLine("Detected Instance abort event..."); |
56 |
break; |
57 |
} |
58 |
Program program = new Program(); |
59 |
|
60 |
program.Id = (int)program_index; |
61 |
xmltv_logger.Verbose.Debug.WriteLine("program_Id: {0}", program.Id); |
62 |
if (c.HasAttributes) |
63 |
{ |
64 |
var start = c.Attribute(XMLTVConstants.Programs.ProgramStart); |
65 |
program.Start = start == null ? new DateTime() : ParseDate(start.Value); |
66 |
if (!program.Start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); } |
67 |
|
68 |
var stop = c.Attribute(XMLTVConstants.Programs.ProgramStop); |
69 |
program.Stop = stop == null ? new DateTime() : ParseDate(stop.Value); |
70 |
if (!program.Stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); } |
71 |
|
72 |
var channelid = c.Attribute(XMLTVConstants.Programs.ProgramChannelId); |
73 |
if (!string.IsNullOrEmpty(program.Description)){ xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid);} |
74 |
IXMLTVChannel channel = new Channel(); |
75 |
string _channelid = channelid == null ? string.Empty : channelid.Value; |
76 |
if (this.GetInstance().Channels.ContainsKey(_channelid)) { program.Channel = this.GetInstance().Channels[_channelid]; } |
77 |
else { program.Channel = new Channel(); } |
78 |
if (!string.IsNullOrEmpty(_channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channel: {0}", program.Channel.ToString()); } |
79 |
} |
80 |
try |
81 |
{ |
82 |
var title = c.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault(); |
83 |
program.Title = title == null ? string.Empty : title.Value; |
84 |
} |
85 |
catch (Exception) { program.Title = string.Empty; } |
86 |
if (!string.IsNullOrEmpty(program.Title)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", program.Title == string.Empty ? "empty" : program.Title); } |
87 |
try |
88 |
{ |
89 |
var subtitle = c.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault(); |
90 |
program.SubTitle = subtitle == null ? string.Empty : subtitle.Value; |
91 |
} |
92 |
catch (Exception) { program.SubTitle = string.Empty; } |
93 |
if (!string.IsNullOrEmpty(program.SubTitle)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", program.SubTitle == string.Empty ? "empty" : program.SubTitle); } |
94 |
try |
95 |
{ |
96 |
var description = c.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault(); |
97 |
program.Description = description == null ? string.Empty : description.Value; ; |
98 |
} |
99 |
catch (Exception) { program.Description = string.Empty; } |
100 |
if (!string.IsNullOrEmpty(program.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", program.Description == string.Empty ? "empty" : program.Description); } |
101 |
|
102 |
entries.Add(program.Id, program); |
103 |
|
104 |
program_index++; |
105 |
progress = 100.0 * (program_index / program_count); |
106 |
xmltv_logger.ReportProgress(this, new Enterprise.Logging.ReportProgressEventArgs((int)progress)); |
107 |
Application.DoEvents(); |
108 |
} |
109 |
//instance.Programs = Collection; |
110 |
} |
111 |
public override string ToString() |
112 |
{ |
113 |
return string.Format("Total Programs Parsed: {0}", Collection == null ? 0 : Collection.Count); |
114 |
} |
115 |
|
116 |
public void Dispose() |
117 |
{ |
118 |
//throw new NotImplementedException(); |
119 |
} |
120 |
} |
121 |
[Serializable] |
122 |
internal class Program : IXMLTVProgram |
123 |
{ |
124 |
public Program() |
125 |
{ |
126 |
Id = 0; |
127 |
Start = new DateTime(); |
128 |
Stop = new DateTime(); |
129 |
Channel = new Channel(); |
130 |
Title = string.Empty; |
131 |
SubTitle = string.Empty; |
132 |
Description = string.Empty; |
133 |
} |
134 |
#region IXMLTVProgram members |
135 |
public int Id { get; set; } |
136 |
public DateTime Start { get; set; } |
137 |
public DateTime Stop { get; set; } |
138 |
public IXMLTVChannel Channel { get; set; } |
139 |
public string Title { get; set; } |
140 |
public string SubTitle { get; set; } |
141 |
public string Description { get; set; } |
142 |
#endregion |
143 |
public override string ToString() |
144 |
{ |
145 |
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")); |
146 |
} |
147 |
} |
148 |
} |