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