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.Reflection; |
8 |
|
9 |
namespace libxmltv.Core |
10 |
{ |
11 |
[Serializable] |
12 |
internal class XMLTVProgram : XMLTVBase<XMLTVRuntimeInstance>, IXMLTVProgram |
13 |
{ |
14 |
public XMLTVProgram() : base(null,XMLTVConstants.PROGRAM_ELEMENT) |
15 |
{ |
16 |
Id = 0; |
17 |
Start = new DateTime(); |
18 |
Stop = new DateTime(); |
19 |
Channel = new XMLTVChannel(); |
20 |
Title = string.Empty; |
21 |
SubTitle = string.Empty; |
22 |
Description = string.Empty; |
23 |
} |
24 |
public XMLTVProgram(XMLTVRuntimeInstance instance, XElement node) |
25 |
: base(instance, XMLTVConstants.PROGRAM_ELEMENT) |
26 |
{ |
27 |
xmltv_logger.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgram"); |
28 |
Create(node); |
29 |
xmltv_logger.Verbose.Debug.WriteLine("Created Instance of XMLTVProgram"); |
30 |
UpdateInstance(); |
31 |
} |
32 |
#region IXMLTVProgram members |
33 |
public int Id { get; set; } |
34 |
public DateTime Start { get; set; } |
35 |
public DateTime Stop { get; set; } |
36 |
public IXMLTVChannel Channel { get; set; } |
37 |
public string Title { get; set; } |
38 |
public string SubTitle { get; set; } |
39 |
public string Description { get; set; } |
40 |
#endregion |
41 |
public override string ToString() |
42 |
{ |
43 |
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")); |
44 |
} |
45 |
|
46 |
private void UpdateInstance() |
47 |
{ |
48 |
bool found_field = false; |
49 |
var instance_type = this.GetInstance().GetType(); |
50 |
var fields = instance_type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
51 |
foreach (var field in fields) |
52 |
{ |
53 |
if (field.FieldType == typeof(List<IXMLTVProgram>)) |
54 |
{ |
55 |
found_field = true; |
56 |
try |
57 |
{ |
58 |
|
59 |
var list = (List<IXMLTVProgram>)field.GetValue(this.GetInstance()); |
60 |
this.Id = list.Count + 1; |
61 |
list.Add(this); |
62 |
xmltv_logger.Verbose.Debug.WriteLine("Updating instance with program information: {0}", this.ToString()); |
63 |
field.SetValue(this.GetInstance(), list); |
64 |
break; |
65 |
} |
66 |
catch (Exception ex) |
67 |
{ |
68 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information."); |
69 |
xmltv_logger.Verbose.Error.WriteLine(ex.ToString()); |
70 |
} |
71 |
} |
72 |
} |
73 |
if (!found_field) |
74 |
{ |
75 |
xmltv_logger.Verbose.Error.WriteLine("Unable to update instance with program information."); |
76 |
} |
77 |
} |
78 |
|
79 |
private void Create(XElement node) |
80 |
{ |
81 |
if (node.HasAttributes) |
82 |
{ |
83 |
var start = node.Attribute(XMLTVConstants.Programs.ProgramStart); |
84 |
this.Start = start == null ? new DateTime() : ParseDate(start.Value); |
85 |
if (!this.Start.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); } |
86 |
|
87 |
var stop = node.Attribute(XMLTVConstants.Programs.ProgramStop); |
88 |
this.Stop = stop == null ? new DateTime() : ParseDate(stop.Value); |
89 |
if (!this.Stop.Equals(new DateTime())) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); } |
90 |
|
91 |
var channelid = node.Attribute(XMLTVConstants.Programs.ProgramChannelId); |
92 |
if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); } |
93 |
IXMLTVChannel channel = new XMLTVChannel(); |
94 |
string _channelid = channelid == null ? string.Empty : channelid.Value; |
95 |
this.Channel = this.GetInstance().Channels.Find(m => m.Id == _channelid); |
96 |
if (this.Channel == null) { this.Channel = new XMLTVChannel(); } |
97 |
if (!string.IsNullOrEmpty(_channelid)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_channel: {0}", this.Channel.ToString()); } |
98 |
} |
99 |
try |
100 |
{ |
101 |
var title = node.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault(); |
102 |
this.Title = title == null ? string.Empty : title.Value; |
103 |
} |
104 |
catch (Exception) { this.Title = string.Empty; } |
105 |
if (!string.IsNullOrEmpty(this.Title)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_title: {0}", this.Title == string.Empty ? "empty" : this.Title); } |
106 |
try |
107 |
{ |
108 |
var subtitle = node.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault(); |
109 |
this.SubTitle = subtitle == null ? string.Empty : subtitle.Value; |
110 |
} |
111 |
catch (Exception) { this.SubTitle = string.Empty; } |
112 |
if (!string.IsNullOrEmpty(this.SubTitle)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", this.SubTitle == string.Empty ? "empty" : this.SubTitle); } |
113 |
try |
114 |
{ |
115 |
var description = node.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault(); |
116 |
this.Description = description == null ? string.Empty : description.Value; ; |
117 |
} |
118 |
catch (Exception) { this.Description = string.Empty; } |
119 |
if (!string.IsNullOrEmpty(this.Description)) { xmltv_logger.Verbose.Debug.WriteLine("\tprogram_description: {0}", this.Description == string.Empty ? "empty" : this.Description); } |
120 |
|
121 |
//entries.Add(program.Id, program); |
122 |
//program_index++; |
123 |
//progress = 100.0 * (program_index / program_count); |
124 |
//xmltv_logger.ReportProgress(this, new Enterprise.Logging.ReportProgressEventArgs((int)progress)); |
125 |
//Application.DoEvents(); |
126 |
} |
127 |
private DateTime ParseDate(string timeStamp) |
128 |
{ |
129 |
DateTime dt = new DateTime(); |
130 |
try |
131 |
{ |
132 |
dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture); |
133 |
} |
134 |
catch (Exception ex) { throw ex; } |
135 |
return dt; |
136 |
} |
137 |
} |
138 |
} |