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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 52 by william, Fri Mar 8 07:01:37 2013 UTC revision 131 by william, Wed Mar 13 15:16:59 2013 UTC
# Line 7  using System.ComponentModel; Line 7  using System.ComponentModel;
7  using System.IO;  using System.IO;
8  using System.Reflection;  using System.Reflection;
9  using System.Globalization;  using System.Globalization;
10    using System.Windows.Forms;
11    using Enterprise.Logging;
12    
13  namespace libxmltv.Core  namespace libxmltv.Core
14  {  {
# Line 15  namespace libxmltv.Core Line 17  namespace libxmltv.Core
17      /// </summary>      /// </summary>
18      public static class XMLTV      public static class XMLTV
19      {      {
20          static XMLTV() { xmltv_logger.Initialize(); }          static XMLTV()
21            {
22                xmltv_logger.Initialize();
23                if (instance == null)
24                {
25                    instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>();
26                }
27            }
28    
29            #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            /// <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            public static IXMLTVSource GetSource()
86            {
87                var gInstance = GetInstance();
88                var list = gInstance.Source;
89                return list;
90            }
91            /// <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            public static List<IXMLTVChannel> GetChannels()
96            {
97                var gInstance = GetInstance();
98               var list = gInstance.Channels;
99                return list;
100            }
101            /// <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            public static List<IXMLTVProgram> GetPrograms()
106            {
107                var gInstance = GetInstance();
108                var list = gInstance.Programs;
109                return list;
110            }
111            /// <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            {
119                if (data == null) { throw new ArgumentNullException("data", "cannot be null"); }
120                //BindingSource source = new BindingSource();
121    
122                IDataSourceBindable binder = (data as IDataSourceBindable);
123                if (binder == null) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", data.GetType().Name, typeof(IDataSourceBindable).Name)); }
124                object bindable = binder.CreateBindableDataSource(out type);
125                //source.DataSource = bindable;
126                //return source;
127                return bindable;
128            }
129    
130            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            public static void CreateFilterFromDataSource(ref object source, params string[] args)
145            {
146                if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
147                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                if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
150                source = t;
151                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                filter.Filter(ref source, args);            
154            }
155            public static void CreateSorterFromDataSource(ref object source, bool descending, params string[] args)
156            {
157                if (source == null) { throw new ArgumentNullException("source", "cannot be null"); }
158                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                if (args == null) { throw new ArgumentNullException("args", "cannot be null"); }
161                source = t;
162                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                sorter.Sort(ref source, descending, args);            
165            }
166            #endregion
167            #endregion
168    
169    
170          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
171          public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }          internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
172          public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }          internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
         //public static void CreateInstance(string xml_file, EventHandler<CancelEventArgs> t) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(xml_file, t); }  
         //public static IXMLTVSerializer<IXMLTVRuntimeInstance> GetSerializer() {return new XMLTVSerializer<IXMLTVRuntimeInstance>(InternalGetInstance()); }  
173    
174          //private static IXMLTVSerializer<T> CreateSerializer<T>() where T : class { return new XMLTVSerializer<T>(InternalGetInstance() as T); }          internal static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)
175            {
176                instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance, handler);
177                //OnCreatedInstance();
178            }
179    
180            internal static void OnCreatedInstance() { if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(null, new EventArgs()); } }
181    
182          #region IXMLTVSerializer<IXMLTVRuntimeInstance> members          #region IXMLTVSerializer<IXMLTVRuntimeInstance> members
183          public static bool Serialize(string file) { return instance.Serialize(file); }          internal static bool Serialize(string file) { return instance.Serialize(file); }
184          public static bool Serialize(Stream stream) { return instance.Serialize(stream); }          internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
185          public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }          internal static bool DeSerialize(string file, EventHandler<EventArgs> handler)
186          public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }          {
187                bool status;
188                var gInstance = instance.DeSerialize(file, out status);            
189                CreateFromInstance(gInstance, handler);
190                return status;
191            }
192            internal static bool DeSerialize(Stream stream, EventHandler<EventArgs> handler)
193            {
194                bool status;
195                var gInstance = instance.DeSerialize(stream, out status);
196                CreateFromInstance(gInstance, handler);
197                return status;
198            }
199          #endregion          #endregion
200            internal static void DestroyInstance() { instance.DestroyInstance(); }
201    
202            internal static EventHandler<EventArgs> OnInstanceCreated
203            {
204                get { return instance.OnInstanceCreated; }
205                set { instance.OnInstanceCreated = value; }
206            }
207    
208          
209      }      }
210    
211      internal class XMLTV<INTERFACE, CLASS> : IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE      internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
212      {      {
213            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          public XMLTV(params object[] args)          public XMLTV(params object[] args)
231          {          {
232              ////instance = new CLASS(xml_file, t);              
233              //instance = null;              BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
   
             //Type type = typeof(CLASS);  
             //var ctors = type.GetConstructors();  
             //foreach (var ctor in ctors)  
             //{  
             //    var ctor_params = ctor.GetParameters();  
             //    if (ctor_params.Count() == 2)  
             //    {  
             //        if (ctor_params[0].ParameterType == typeof(string))  
             //        {  
             //            if (ctor_params[1].ParameterType == typeof(EventHandler<CancelEventArgs>))  
             //            {  
             //                object o = ctor.Invoke(new object[] { xml_file, t });  
             //                instance = (CLASS)Convert.ChangeType(o, typeof(CLASS));  
             //                break;  
             //            }  
             //        }  
             //    }  
             //    else { continue; }  
             //}  
             BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;  
234              CultureInfo culture = CultureInfo.CurrentCulture;              CultureInfo culture = CultureInfo.CurrentCulture;
235              try              try
236              {              {
# Line 68  namespace libxmltv.Core Line 239  namespace libxmltv.Core
239              }              }
240              catch (Exception ex)              catch (Exception ex)
241              {              {
242                  xmltv_logger.Log.Error.WriteLine(ex.ToString());                  xmltv_logger.Error.WriteLine(ex.ToString());
243    
244                  StringBuilder parameter_builder = new StringBuilder();                  StringBuilder parameter_builder = new StringBuilder();
245                  foreach (object arg in args)                  foreach (object arg in args)
# Line 101  namespace libxmltv.Core Line 272  namespace libxmltv.Core
272          {          {
273              return (INTERFACE)instance;              return (INTERFACE)instance;
274          }          }
275            public void SetInstance(INTERFACE gInstance)
276            {
277                this.instance = gInstance as CLASS;
278            }
279          #endregion          #endregion
280    
281          #region IXMLTVSerializer<INTERFACE> members          #region IXMLTVSerializer<INTERFACE> members
# Line 109  namespace libxmltv.Core Line 284  namespace libxmltv.Core
284          public INTERFACE DeSerialize(string file, out bool status) { return CreateSerializer().DeSerialize(file, out status); }          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); }          public INTERFACE DeSerialize(Stream stream, out bool status) { return CreateSerializer().DeSerialize(stream, out status); }
286          #endregion          #endregion
287            public void DestroyInstance()
288            {
289                IDestroyInstance destoyer = (instance as IDestroyInstance);
290                if (destoyer != null)
291                {
292                    destoyer.DestroyInstance();
293                }
294                else
295                {
296                    xmltv_logger.Error.WriteLine("Unable to call DestroyInstance() on type: '{0}'", instance.GetType().Name);
297                }            
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      }      }
316    
317  }  }

Legend:
Removed from v.52  
changed lines
  Added in v.131

  ViewVC Help
Powered by ViewVC 1.1.22