/[xmltv_parser]/trunk/libxmltv/Core/XMLTV.cs
ViewVC logotype

Annotation of /trunk/libxmltv/Core/XMLTV.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 120 - (hide annotations) (download)
Sun Mar 10 18:19:12 2013 UTC (7 years, 6 months ago) by william
File size: 10632 byte(s)

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 22
12     namespace libxmltv.Core
13     {
14     /// <summary>
15     /// Main class: Creates the XMLTV Loader
16     /// </summary>
17     public static class XMLTV
18     {
19 william 56 static XMLTV()
20     {
21     xmltv_logger.Initialize();
22     if (instance == null)
23     {
24     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
25     }
26     }
27 william 23
28 william 118 #region public members
29     #region save data
30     public static void Save(string file)
31     {
32     if (!Serialize(file))
33     {
34     MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
35     return;
36     }
37     MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
38     }
39     public static void Save(Stream stream)
40     {
41     if (!Serialize(stream))
42     {
43     MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
44     return;
45     }
46     MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
47     }
48     #endregion
49     #region load data
50     public static void Load(string file) { Load(file, null); }
51     public static void Load(string file, EventHandler<EventArgs> handler)
52     {
53     if (!DeSerialize(file, handler))
54     {
55     MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
56     return;
57     }
58     MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
59     OnCreatedInstance();
60    
61     }
62     public static void Load(Stream stream) { Load(stream, null); }
63     public static void Load(Stream stream, EventHandler<EventArgs> handler)
64     {
65     if (!DeSerialize(stream, handler))
66     {
67     MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
68     return;
69     }
70     MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
71     OnCreatedInstance();
72     }
73     #endregion
74     #region Create/Destroy
75     public static void Create(params object[] args) { CreateInstance(args); }
76     public static void Destroy() { DestroyInstance(); }
77     #endregion
78    
79     #region Get Data
80     public static IXMLTVSource GetSource()
81     {
82     var gInstance = GetInstance();
83     var list = gInstance.Source;
84     return list;
85     }
86     public static List<IXMLTVChannel> GetChannels()
87     {
88     var gInstance = GetInstance();
89     var list = gInstance.Channels;
90     return list;
91     }
92     public static List<IXMLTVProgram> GetPrograms()
93     {
94     var gInstance = GetInstance();
95     var list = gInstance.Programs;
96     return list;
97     }
98 william 120
99     public static BindingSource CreateBindingSourceForData(object data)
100     {
101     if (data == null) { throw new ArgumentNullException("data", "cannot be null"); }
102     BindingSource source = new BindingSource();
103    
104     IDataSourceBindable binder = (data as IDataSourceBindable);
105     if (binder == null)
106     {
107     throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", data.GetType().Name, typeof(IDataSourceBindable).Name));
108     }
109    
110     object bindable = binder.CreateBindableDataSource();
111     source.DataSource = bindable;
112     return source;
113     }
114    
115 william 118 #endregion
116     #endregion
117    
118    
119 william 50 static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
120 william 117 internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
121     internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
122 william 50
123 william 118 internal static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
124     {
125     instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance, handler);
126     //OnCreatedInstance();
127 william 56 }
128 william 50
129 william 118 internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } }
130    
131 william 50 #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
132 william 117 internal static bool Serialize(string file) { return instance.Serialize(file); }
133     internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
134 william 118 internal static bool DeSerialize(string file, EventHandler<EventArgs> handler)
135 william 117 {
136     bool status;
137 william 118 var gInstance = instance.DeSerialize(file, out status);
138     CreateFromInstance(gInstance, handler);
139 william 117 return status;
140     }
141 william 118 internal static bool DeSerialize(Stream stream, EventHandler<EventArgs> handler)
142 william 117 {
143     bool status;
144     var gInstance = instance.DeSerialize(stream, out status);
145 william 118 CreateFromInstance(gInstance, handler);
146 william 117 return status;
147     }
148 william 50 #endregion
149 william 117 internal static void DestroyInstance() { instance.DestroyInstance(); }
150 william 54
151 william 117 internal static EventHandler<EventArgs> OnInstanceCreated
152 william 54 {
153     get { return instance.OnInstanceCreated; }
154     set { instance.OnInstanceCreated = value; }
155     }
156 william 117
157 william 118
158 william 22 }
159 william 50
160 william 56 internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
161 william 50 {
162 william 56 public XMLTV()
163     {
164     instance = new CLASS();
165     }
166     public XMLTV(object raw_instance, EventHandler<EventArgs> handler)
167     {
168     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
169     if (instance != null)
170     {
171     IRuntimeInstanceLoader<CLASS> loader = (instance as IRuntimeInstanceLoader<CLASS>);
172     if (loader != null)
173     {
174     SetOnInstanceCreated(handler);
175     instance = loader.LoadFromInstance(instance);
176     }
177     }
178     }
179 william 52 public XMLTV(params object[] args)
180 william 50 {
181 william 53
182     BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
183 william 52 CultureInfo culture = CultureInfo.CurrentCulture;
184     try
185 william 50 {
186 william 52 object raw_instance = Activator.CreateInstance(typeof(CLASS), flags, null, args, culture);
187     instance = (CLASS)Convert.ChangeType(raw_instance, typeof(CLASS));
188     }
189     catch (Exception ex)
190     {
191 william 55 xmltv_logger.Error.WriteLine(ex.ToString());
192 william 52
193     StringBuilder parameter_builder = new StringBuilder();
194     foreach (object arg in args)
195 william 50 {
196 william 52 Type type = arg.GetType();
197     parameter_builder.AppendFormat("({0}), ", type.FullName);
198 william 50 }
199 william 52
200     string type_parameters = parameter_builder.ToString().TrimEnd(new char[] { ',', ' ' });
201     throw new Exception(string.Format("Unable to create instance of: '{0}' with parameters: '{1}'", typeof(CLASS).Name, type_parameters), ex);
202 william 50 }
203     }
204    
205     private CLASS instance;
206     #region IXMLTV<T> members
207 william 51 public IXMLTVSerializer<CLASS> CreateSerializer()
208     {
209     // we must serialize on the CLASS type, using the INTERFACE type is syntatically incorrect
210     ISerializer<CLASS> class_serializer = (instance as ISerializer<CLASS>);
211     if (class_serializer != null)
212     {
213     return class_serializer.Serializer;
214     }
215     else
216     {
217     return new XMLTVSerializer<CLASS>(instance);
218     }
219     }
220 william 50 public INTERFACE GetInstance()
221     {
222     return (INTERFACE)instance;
223     }
224 william 117 public void SetInstance(INTERFACE gInstance)
225     {
226     this.instance = gInstance as CLASS;
227     }
228 william 50 #endregion
229    
230     #region IXMLTVSerializer<INTERFACE> members
231     public bool Serialize(string file) { return CreateSerializer().Serialize(file); }
232     public bool Serialize(Stream stream) { return CreateSerializer().Serialize(stream); }
233     public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }
234     public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
235     #endregion
236 william 54 public void DestroyInstance()
237     {
238     IDestroyInstance destoyer = (instance as IDestroyInstance);
239     if (destoyer != null)
240     {
241     destoyer.DestroyInstance();
242     }
243     else
244     {
245 william 55 xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
246 william 54 }
247     }
248     private void SetOnInstanceCreated(EventHandler<EventArgs> event_instance)
249     {
250     ISetCreatedInstanceEvent setter = (instance as ISetCreatedInstanceEvent);
251     if (setter != null) { setter.SetOnInstanceCreated(event_instance); }
252     }
253     private EventHandler<EventArgs> GetOnInstanceCreated()
254     {
255     IGetCreatedInstanceEvent getter = (instance as IGetCreatedInstanceEvent);
256     if (getter != null) { return getter.GetOnInstanceCreated(); }
257     return null;
258     }
259     public EventHandler<EventArgs> OnInstanceCreated
260     {
261     get { return GetOnInstanceCreated(); }
262     set { SetOnInstanceCreated(value); }
263     }
264 william 50 }
265    
266 william 22 }
267 william 46
268 william 49

  ViewVC Help
Powered by ViewVC 1.1.22