1 |
william |
22 |
using System; |
2 |
|
|
using System.Collections.Generic; |
3 |
|
|
using System.Linq; |
4 |
|
|
using System.Text; |
5 |
|
|
using libxmltv.Interfaces; |
6 |
william |
46 |
using System.ComponentModel; |
7 |
william |
50 |
using System.IO; |
8 |
william |
52 |
using System.Reflection; |
9 |
|
|
using System.Globalization; |
10 |
william |
118 |
using System.Windows.Forms; |
11 |
william |
128 |
using Enterprise.Logging; |
12 |
william |
22 |
|
13 |
|
|
namespace libxmltv.Core |
14 |
|
|
{ |
15 |
|
|
/// <summary> |
16 |
|
|
/// Main class: Creates the XMLTV Loader |
17 |
|
|
/// </summary> |
18 |
|
|
public static class XMLTV |
19 |
|
|
{ |
20 |
william |
56 |
static XMLTV() |
21 |
|
|
{ |
22 |
|
|
xmltv_logger.Initialize(); |
23 |
|
|
if (instance == null) |
24 |
|
|
{ |
25 |
|
|
instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(); |
26 |
|
|
} |
27 |
|
|
} |
28 |
william |
23 |
|
29 |
william |
118 |
#region public members |
30 |
|
|
#region save data |
31 |
|
|
public static void Save(string file) |
32 |
|
|
{ |
33 |
|
|
if (!Serialize(file)) |
34 |
|
|
{ |
35 |
|
|
MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); |
36 |
|
|
return; |
37 |
|
|
} |
38 |
|
|
MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information); |
39 |
|
|
} |
40 |
|
|
public static void Save(Stream stream) |
41 |
|
|
{ |
42 |
|
|
if (!Serialize(stream)) |
43 |
|
|
{ |
44 |
|
|
MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); |
45 |
|
|
return; |
46 |
|
|
} |
47 |
|
|
MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information); |
48 |
|
|
} |
49 |
|
|
#endregion |
50 |
|
|
#region load data |
51 |
|
|
public static void Load(string file) { Load(file, null); } |
52 |
|
|
public static void Load(string file, EventHandler<EventArgs> handler) |
53 |
|
|
{ |
54 |
|
|
if (!DeSerialize(file, handler)) |
55 |
|
|
{ |
56 |
|
|
MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); |
57 |
|
|
return; |
58 |
|
|
} |
59 |
|
|
MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information); |
60 |
|
|
OnCreatedInstance(); |
61 |
|
|
|
62 |
|
|
} |
63 |
|
|
public static void Load(Stream stream) { Load(stream, null); } |
64 |
|
|
public static void Load(Stream stream, EventHandler<EventArgs> handler) |
65 |
|
|
{ |
66 |
|
|
if (!DeSerialize(stream, handler)) |
67 |
|
|
{ |
68 |
|
|
MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); |
69 |
|
|
return; |
70 |
|
|
} |
71 |
|
|
MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information); |
72 |
|
|
OnCreatedInstance(); |
73 |
|
|
} |
74 |
|
|
#endregion |
75 |
|
|
#region Create/Destroy |
76 |
|
|
public static void Create(params object[] args) { CreateInstance(args); } |
77 |
|
|
public static void Destroy() { DestroyInstance(); } |
78 |
|
|
#endregion |
79 |
|
|
|
80 |
|
|
#region Get Data |
81 |
william |
128 |
/// <summary> |
82 |
|
|
/// Gets the source of the XMLTV Data : <see cref="libxmltv.Interfaces.IXMLTVSource"/> |
83 |
|
|
/// </summary> |
84 |
|
|
/// <returns>returns an instance of the xmltv data source : <see cref="libxmltv.Interfaces.IXMLTVSource"/></returns> |
85 |
william |
118 |
public static IXMLTVSource GetSource() |
86 |
|
|
{ |
87 |
|
|
var gInstance = GetInstance(); |
88 |
|
|
var list = gInstance.Source; |
89 |
|
|
return list; |
90 |
|
|
} |
91 |
william |
128 |
/// <summary> |
92 |
|
|
/// Gets the collection of XMLTV Channels : List of <see cref="libxmltv.Interfaces.IXMLTVChannel"/> |
93 |
|
|
/// </summary> |
94 |
|
|
/// <returns>returns an instance of the xmltv channels : as a list of: <see cref="libxmltv.Interfaces.IXMLTVChannel"/></returns> |
95 |
william |
118 |
public static List<IXMLTVChannel> GetChannels() |
96 |
|
|
{ |
97 |
|
|
var gInstance = GetInstance(); |
98 |
|
|
var list = gInstance.Channels; |
99 |
|
|
return list; |
100 |
|
|
} |
101 |
william |
128 |
/// <summary> |
102 |
|
|
/// Gets the collection of XMLTV Programs : List of <see cref="libxmltv.Interfaces.IXMLTVProgram"/> |
103 |
|
|
/// </summary> |
104 |
|
|
/// <returns>returns an instance of the xmltv channels : as a list of: <see cref="libxmltv.Interfaces.IXMLTVProgram"/></returns> |
105 |
william |
118 |
public static List<IXMLTVProgram> GetPrograms() |
106 |
|
|
{ |
107 |
|
|
var gInstance = GetInstance(); |
108 |
|
|
var list = gInstance.Programs; |
109 |
|
|
return list; |
110 |
|
|
} |
111 |
william |
128 |
/// <summary> |
112 |
|
|
/// Creates a binding source for a bindable control |
113 |
|
|
/// </summary> |
114 |
|
|
/// <param name="data">The object data to create a binding source from</param> |
115 |
|
|
/// <param name="type">Represents the underlying type of the created binding source data</param> |
116 |
|
|
/// <returns>Returns an object representing the created binding source</returns> |
117 |
|
|
public static object CreateBindingSourceFromData(object data, out Type type) |
118 |
william |
120 |
{ |
119 |
|
|
if (data == null) { throw new ArgumentNullException("data", "cannot be null"); } |
120 |
william |
126 |
//BindingSource source = new BindingSource(); |
121 |
william |
120 |
|
122 |
|
|
IDataSourceBindable binder = (data as IDataSourceBindable); |
123 |
william |
125 |
if (binder == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", data.GetType().Name, typeof(IDataSourceBindable).Name)); } |
124 |
william |
128 |
object bindable = binder.CreateBindableDataSource(out type); |
125 |
william |
126 |
//source.DataSource = bindable; |
126 |
|
|
//return source; |
127 |
|
|
return bindable; |
128 |
william |
120 |
} |
129 |
|
|
|
130 |
william |
128 |
private static object ConvertSourceType(Type source_type, object source) |
131 |
|
|
{ |
132 |
|
|
//try |
133 |
|
|
//{ |
134 |
|
|
// IDataConverter converter = (source as IDataConverter); |
135 |
|
|
// if (converter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataConverter).Name)); } |
136 |
|
|
// object t = converter.ConvertObjectData(source); |
137 |
|
|
// if (t != null) { source = t; } |
138 |
|
|
// return t; |
139 |
|
|
//} |
140 |
|
|
//catch (Exception ex) { gLog.Verbose.Error.WriteLine(ex.ToString()); return null; } |
141 |
|
|
return source; |
142 |
|
|
} |
143 |
|
|
|
144 |
william |
126 |
public static void CreateFilterFromDataSource(ref object source, params string[] args) |
145 |
william |
125 |
{ |
146 |
|
|
if (source == null) { throw new ArgumentNullException("source", "cannot be null"); } |
147 |
william |
130 |
object t = ConvertSourceType(source.GetType(), source); |
148 |
|
|
if (t == null) { throw new ArgumentException("source_type", string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, source.GetType().Name)); } |
149 |
william |
125 |
if (args == null) { throw new ArgumentNullException("args", "cannot be null"); } |
150 |
william |
128 |
source = t; |
151 |
william |
125 |
IDataSourceFilterable filter = (source as IDataSourceFilterable); |
152 |
|
|
if (filter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceFilterable).Name)); } |
153 |
william |
130 |
filter.Filter(ref source, args); |
154 |
william |
125 |
} |
155 |
william |
131 |
public static void CreateSorterFromDataSource(ref object source, bool descending, params string[] args) |
156 |
william |
125 |
{ |
157 |
|
|
if (source == null) { throw new ArgumentNullException("source", "cannot be null"); } |
158 |
william |
128 |
object t = ConvertSourceType(source.GetType(), source); |
159 |
|
|
if (t == null) { throw new ArgumentException("source_type", string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, source.GetType().Name)); } |
160 |
william |
125 |
if (args == null) { throw new ArgumentNullException("args", "cannot be null"); } |
161 |
william |
128 |
source = t; |
162 |
william |
125 |
IDataSourceSortable sorter = (source as IDataSourceSortable); |
163 |
|
|
if (sorter == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(IDataSourceSortable).Name)); } |
164 |
william |
131 |
sorter.Sort(ref source, descending, args); |
165 |
william |
125 |
} |
166 |
william |
118 |
#endregion |
167 |
|
|
#endregion |
168 |
|
|
|
169 |
|
|
|
170 |
william |
50 |
static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance; |
171 |
william |
117 |
internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); } |
172 |
|
|
internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); } |
173 |
william |
50 |
|
174 |
william |
118 |
internal static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler) |
175 |
|
|
{ |
176 |
|
|
instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance, handler); |
177 |
|
|
//OnCreatedInstance(); |
178 |
william |
56 |
} |
179 |
william |
50 |
|
180 |
william |
118 |
internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } } |
181 |
|
|
|
182 |
william |
50 |
#region IXMLTVSerializer<IXMLTVRuntimeInstance> members |
183 |
william |
117 |
internal static bool Serialize(string file) { return instance.Serialize(file); } |
184 |
|
|
internal static bool Serialize(Stream stream) { return instance.Serialize(stream); } |
185 |
william |
118 |
internal static bool DeSerialize(string file, EventHandler<EventArgs> handler) |
186 |
william |
117 |
{ |
187 |
|
|
bool status; |
188 |
william |
118 |
var gInstance = instance.DeSerialize(file, out status); |
189 |
|
|
CreateFromInstance(gInstance, handler); |
190 |
william |
117 |
return status; |
191 |
|
|
} |
192 |
william |
118 |
internal static bool DeSerialize(Stream stream, EventHandler<EventArgs> handler) |
193 |
william |
117 |
{ |
194 |
|
|
bool status; |
195 |
|
|
var gInstance = instance.DeSerialize(stream, out status); |
196 |
william |
118 |
CreateFromInstance(gInstance, handler); |
197 |
william |
117 |
return status; |
198 |
|
|
} |
199 |
william |
50 |
#endregion |
200 |
william |
117 |
internal static void DestroyInstance() { instance.DestroyInstance(); } |
201 |
william |
54 |
|
202 |
william |
117 |
internal static EventHandler<EventArgs> OnInstanceCreated |
203 |
william |
54 |
{ |
204 |
|
|
get { return instance.OnInstanceCreated; } |
205 |
|
|
set { instance.OnInstanceCreated = value; } |
206 |
|
|
} |
207 |
william |
117 |
|
208 |
william |
118 |
|
209 |
william |
22 |
} |
210 |
william |
50 |
|
211 |
william |
56 |
internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new() |
212 |
william |
50 |
{ |
213 |
william |
56 |
public XMLTV() |
214 |
|
|
{ |
215 |
|
|
instance = new CLASS(); |
216 |
|
|
} |
217 |
|
|
public XMLTV(object raw_instance, EventHandler<EventArgs> handler) |
218 |
|
|
{ |
219 |
|
|
instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS)); |
220 |
|
|
if (instance != null) |
221 |
|
|
{ |
222 |
|
|
IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>); |
223 |
|
|
if (loader != null) |
224 |
|
|
{ |
225 |
|
|
SetOnInstanceCreated(handler); |
226 |
|
|
instance = loader.LoadFromInstance(instance); |
227 |
|
|
} |
228 |
|
|
} |
229 |
|
|
} |
230 |
william |
52 |
public XMLTV(params object[] args) |
231 |
william |
50 |
{ |
232 |
william |
53 |
|
233 |
|
|
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; |
234 |
william |
52 |
CultureInfo culture = CultureInfo.CurrentCulture; |
235 |
|
|
try |
236 |
william |
50 |
{ |
237 |
william |
52 |
object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture); |
238 |
|
|
instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS)); |
239 |
|
|
} |
240 |
|
|
catch (Exception ex) |
241 |
|
|
{ |
242 |
william |
55 |
xmltv_logger.Error.WriteLine(ex.ToString()); |
243 |
william |
52 |
|
244 |
|
|
StringBuilder parameter_builder = new StringBuilder(); |
245 |
|
|
foreach (object arg in args) |
246 |
william |
50 |
{ |
247 |
william |
52 |
Type type = arg.GetType(); |
248 |
|
|
parameter_builder.AppendFormat("({0}), ", type.FullName); |
249 |
william |
50 |
} |
250 |
william |
52 |
|
251 |
|
|
string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' }); |
252 |
|
|
throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex); |
253 |
william |
50 |
} |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
private CLASS instance; |
257 |
|
|
#region IXMLTV<T> members |
258 |
william |
51 |
public IXMLTVSerializer<CLASS> CreateSerializer() |
259 |
|
|
{ |
260 |
|
|
// we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect |
261 |
|
|
ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>); |
262 |
|
|
if (class_serializer != null) |
263 |
|
|
{ |
264 |
|
|
return class_serializer.Serializer; |
265 |
|
|
} |
266 |
|
|
else |
267 |
|
|
{ |
268 |
|
|
return new XMLTVSerializer<CLASS>(instance); |
269 |
|
|
} |
270 |
|
|
} |
271 |
william |
50 |
public INTERFACE GetInstance() |
272 |
|
|
{ |
273 |
|
|
return (INTERFACE)instance; |
274 |
|
|
} |
275 |
william |
117 |
public void SetInstance(INTERFACE gInstance) |
276 |
|
|
{ |
277 |
|
|
this.instance = gInstance as CLASS; |
278 |
|
|
} |
279 |
william |
50 |
#endregion |
280 |
|
|
|
281 |
|
|
#region IXMLTVSerializer<INTERFACE> members |
282 |
|
|
public bool Serialize(string file) { return CreateSerializer().Serialize(file); } |
283 |
|
|
public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); } |
284 |
|
|
public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); } |
285 |
|
|
public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); } |
286 |
|
|
#endregion |
287 |
william |
54 |
public void DestroyInstance() |
288 |
|
|
{ |
289 |
|
|
IDestroyInstance destoyer = (instance as IDestroyInstance); |
290 |
|
|
if (destoyer != null) |
291 |
|
|
{ |
292 |
|
|
destoyer.DestroyInstance(); |
293 |
|
|
} |
294 |
|
|
else |
295 |
|
|
{ |
296 |
william |
55 |
xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name); |
297 |
william |
54 |
} |
298 |
|
|
} |
299 |
|
|
private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance) |
300 |
|
|
{ |
301 |
|
|
ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent); |
302 |
|
|
if (setter != null) { setter.SetOnInstanceCreated(event_instance); } |
303 |
|
|
} |
304 |
|
|
private EventHandler<EventArgs> GetOnInstanceCreated() |
305 |
|
|
{ |
306 |
|
|
IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent); |
307 |
|
|
if (getter != null) { return getter.GetOnInstanceCreated(); } |
308 |
|
|
return null; |
309 |
|
|
} |
310 |
|
|
public EventHandler<EventArgs> OnInstanceCreated |
311 |
|
|
{ |
312 |
|
|
get { return GetOnInstanceCreated(); } |
313 |
|
|
set { SetOnInstanceCreated(value); } |
314 |
|
|
} |
315 |
william |
50 |
} |
316 |
|
|
|
317 |
william |
22 |
} |
318 |
william |
46 |
|
319 |
william |
49 |
|