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 : 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 xmltv) |
21 |
{ |
22 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("Creating Instance of XMLTVProgramCollection"); |
23 |
//IXMLTV_PARSER _xmltv; |
24 |
//if (!Internals.VerifyInstance<IXMLTV_PARSER>(xmltv, out _xmltv)) { return; } |
25 |
//XMLTV_PARSER = _xmltv; |
26 |
instance = xmltv; |
27 |
Create(); |
28 |
} |
29 |
|
30 |
#region IXMLTVSource |
31 |
//private IXMLTV_PARSER XMLTV_PARSER { get; set; } |
32 |
public Dictionary<int, IXMLTVProgram> Collection |
33 |
{ |
34 |
get { return entries; } |
35 |
} |
36 |
#endregion |
37 |
|
38 |
// |
39 |
private DateTime ParseDate(string timeStamp) |
40 |
{ |
41 |
DateTime dt = new DateTime(); |
42 |
try |
43 |
{ |
44 |
dt = DateTime.ParseExact(timeStamp, "yyyyMMddHHmmss zzzz", System.Globalization.CultureInfo.CurrentCulture); |
45 |
} |
46 |
catch (Exception ex) { throw ex; } |
47 |
return dt; |
48 |
} |
49 |
private void Create() |
50 |
{ |
51 |
var doc = instance.XmlDoc; |
52 |
int index = 0; |
53 |
foreach (var c in doc.Descendants(XMLTVConstants.PROGRAM_ELEMENT)) |
54 |
{ |
55 |
if (instance.IsDisposing) |
56 |
{ |
57 |
break; |
58 |
} |
59 |
Program program = new Program(); |
60 |
|
61 |
program.Id = index; |
62 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("program_Id: {0}", program.Id); |
63 |
if (c.HasAttributes) |
64 |
{ |
65 |
var start = c.Attribute(XMLTVConstants.Programs.ProgramStart).Value; |
66 |
program.Start = ParseDate(start); |
67 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_start: {0}", start); |
68 |
var stop = c.Attribute(XMLTVConstants.Programs.ProgramStop).Value; |
69 |
program.Stop = ParseDate(stop); |
70 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_stop: {0}", stop); |
71 |
var channelid = c.Attribute(XMLTVConstants.Programs.ProgramChannelId).Value; |
72 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channelid: {0}", channelid); |
73 |
IXMLTVChannel channel = new Channel(); |
74 |
try |
75 |
{ |
76 |
channel = instance.Channels[channelid]; |
77 |
} |
78 |
catch (KeyNotFoundException) |
79 |
{ |
80 |
XMLTV_LOGGER.Log.Verbose.Error.WriteLine(string.Format("Unable to find Channel by id: '{0}' for this program.", channelid)); |
81 |
} |
82 |
program.Channel = channel; |
83 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_channel: {0}", program.Channel.ToString()); |
84 |
} |
85 |
try |
86 |
{ |
87 |
var title = c.Descendants(XMLTVConstants.Programs.ProgramTitle).FirstOrDefault().Value; |
88 |
program.Title = title; |
89 |
} |
90 |
catch (Exception) { program.Title = string.Empty; } |
91 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_title: {0}", program.Title == string.Empty ? "empty" : program.Title); |
92 |
try |
93 |
{ |
94 |
var subtitle = c.Descendants(XMLTVConstants.Programs.ProgramSubTitle).FirstOrDefault().Value; |
95 |
program.SubTitle = subtitle; |
96 |
} |
97 |
catch (Exception) { program.SubTitle = string.Empty; } |
98 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_subtitle: {0}", program.SubTitle == string.Empty ? "empty" : program.SubTitle); |
99 |
try |
100 |
{ |
101 |
var description = c.Descendants(XMLTVConstants.Programs.ProgramDescription).FirstOrDefault().Value; |
102 |
program.Description = description; |
103 |
} |
104 |
catch (Exception) { program.Description = string.Empty; } |
105 |
XMLTV_LOGGER.Log.Verbose.Debug.WriteLine("\tprogram_description: {0}", program.Description == string.Empty ? "empty" : program.Description); |
106 |
|
107 |
entries.Add(program.Id, program); |
108 |
Application.DoEvents(); |
109 |
index++; |
110 |
} |
111 |
//instance.Programs = Collection; |
112 |
} |
113 |
public override string ToString() |
114 |
{ |
115 |
return string.Format("Program Count: {0}", Collection == null ? 0 : Collection.Count); |
116 |
} |
117 |
|
118 |
public void Dispose() |
119 |
{ |
120 |
//throw new NotImplementedException(); |
121 |
} |
122 |
} |
123 |
[Serializable] |
124 |
internal class Program : IXMLTVProgram |
125 |
{ |
126 |
public Program() |
127 |
{ |
128 |
Id = 0; |
129 |
Start = new DateTime(); |
130 |
Stop = new DateTime(); |
131 |
Channel = new Channel(); |
132 |
Title = string.Empty; |
133 |
SubTitle = string.Empty; |
134 |
Description = string.Empty; |
135 |
} |
136 |
#region IXMLTVProgram members |
137 |
public int Id { get; set; } |
138 |
public DateTime Start { get; set; } |
139 |
public DateTime Stop { get; set; } |
140 |
public IXMLTVChannel Channel { get; set; } |
141 |
public string Title { get; set; } |
142 |
public string SubTitle { get; set; } |
143 |
public string Description { get; set; } |
144 |
#endregion |
145 |
public override string ToString() |
146 |
{ |
147 |
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")); |
148 |
} |
149 |
} |
150 |
} |