/[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 56 by william, Fri Mar 8 12:05:33 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 24  namespace libxmltv.Core Line 26  namespace libxmltv.Core
26              }              }
27          }          }
28    
29          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;          #region public members
30          public static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }          #region save data
31          public static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }          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          public static void CreateFromInstance(object raw_instance, EventHandler<EventArgs> handler)          }
63          {          public static void Load(Stream stream) { Load(stream, null); }
64              instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(raw_instance,handler);          public static void Load(Stream stream, EventHandler<EventArgs> handler)
65              if (OnInstanceCreated != null)          {
66                if (!DeSerialize(stream, handler))
67              {              {
68                  OnInstanceCreated.Invoke(null, new EventArgs());                  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 IXMLTVSerializer<IXMLTVRuntimeInstance> members          #region Get Data
81          public static bool Serialize(string file) { return instance.Serialize(file); }          /// <summary>
82          public static bool Serialize(Stream stream) { return instance.Serialize(stream); }          /// Gets the source of the XMLTV Data : <see cref="libxmltv.Interfaces.IXMLTVSource"/>
83          public static IXMLTVRuntimeInstance DeSerialize(string file, out bool status) { return instance.DeSerialize(file, out status); }          /// </summary>
84          public static IXMLTVRuntimeInstance DeSerialize(Stream stream, out bool status) { return instance.DeSerialize(stream, out status); }          /// <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          #endregion
168    
         public static void DestroyInstance() { instance.DestroyInstance(); }          
169    
170          public static EventHandler<EventArgs> OnInstanceCreated          static IXMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance> instance;
171            internal static IXMLTVRuntimeInstance GetInstance() { return instance.GetInstance(); }
172            internal static void CreateInstance(params object[] args) { instance = new XMLTV<IXMLTVRuntimeInstance, XMLTVRuntimeInstance>(args); }
173    
174            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
183            internal static bool Serialize(string file) { return instance.Serialize(file); }
184            internal static bool Serialize(Stream stream) { return instance.Serialize(stream); }
185            internal static bool DeSerialize(string file, EventHandler<EventArgs> handler)
186            {
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
200            internal static void DestroyInstance() { instance.DestroyInstance(); }
201    
202            internal static EventHandler<EventArgs> OnInstanceCreated
203          {          {
204              get { return instance.OnInstanceCreated; }              get { return instance.OnInstanceCreated; }
205              set { instance.OnInstanceCreated = value; }              set { instance.OnInstanceCreated = value; }
206          }          }
207    
208          
209      }      }
210    
211      internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()      internal class XMLTV<INTERFACE, CLASS> : IDestroyInstance, IXMLTV<INTERFACE, CLASS> where CLASS : class,INTERFACE,new()
# Line 117  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

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

  ViewVC Help
Powered by ViewVC 1.1.22