/[xmltv_parser]/trunk/xmltv_parser/main.cs
ViewVC logotype

Contents of /trunk/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 130 - (show annotations) (download)
Wed Mar 13 14:39:08 2013 UTC (6 years, 11 months ago) by william
File size: 11856 byte(s)
+ current wip

1 //#define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
2 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
3 //#define USE_DYNAMIC_SORTING_FILTERING_CONTROLS // when defined will setup dynamic sorting and filtering controls for XMLTV Data
4 using System;
5 using System.Collections.Generic;
6 using System.ComponentModel;
7 using System.Data;
8 using System.Linq;
9 using System.Drawing;
10 using System.Text;
11 using System.Windows.Forms;
12 using libxmltv.Core;
13 using Enterprise.Logging;
14 using libxmltv.Interfaces;
15 using System.IO;
16 using System.Runtime.Serialization.Formatters.Binary;
17 using System.Threading;
18 using System.Diagnostics;
19 using System.Xml.Linq;
20
21 namespace xmltv_parser
22 {
23
24 public partial class main : Form
25 {
26 public main()
27 {
28 InitializeComponent();
29 ListViewSorter Sorter = new ListViewSorter();
30 //lstPrograms.ListViewItemSorter = Sorter;
31 string log_path = Application.StartupPath;
32 string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
33 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
34 #if DEBUG
35 LogLevel gLevel = gLog.LogLevel;
36 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
37 gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
38 #else
39 gLevel |= LogLevel.kLogLevel_VerboseDebug;
40 #endif
41 gLevel |= LogLevel.kLogLevel_Debug;
42 gLog.SetLogLevel(gLevel);
43 #else
44 LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
45 // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
46 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
47 gLevel &= ~LogLevel.kLogLevel_Debug;
48 #else
49 gLevel |= LogLevel.kLogLevel_Debug;
50 #endif
51 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
52 gLog.SetLogLevel(gLevel);
53 #endif
54
55
56 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
57 }
58 #region main form events
59 private void mnuItemExit_Click(object sender, EventArgs e) { this.Close(); }
60 private void main_Load(object sender, EventArgs e) { }
61 private void main_Shown(object sender, EventArgs e) { }
62 private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
63 {
64 try
65 {
66 var result = xmltv_file_chooser.ShowDialog();
67 if (result != DialogResult.OK) return;
68 ClearLocalLog();
69 LoadXMLTVShcedule(xmltv_file_chooser.FileName);
70 }
71 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
72 }
73 private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
74 {
75 try
76 {
77 var result = xmltv_program_data_loader.ShowDialog();
78 if (result != DialogResult.OK) return;
79 string filename = xmltv_program_data_loader.FileName;
80 Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataFromFile)); worker.Start(filename);
81 }
82 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
83 }
84 private void mnuItemSaveData_Click(object sender, EventArgs e)
85 {
86 try
87 {
88 var result = xmltv_program_data_saver.ShowDialog();
89 if (result != DialogResult.OK) return;
90 string filename = xmltv_program_data_saver.FileName;
91 Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataToFile)); worker.Start(filename);
92 }
93 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
94 }
95 private void main_FormClosing(object sender, FormClosingEventArgs e) { try { XMLTV.Destroy(); } catch { } }
96 private void mnuItemClearLocalLog_Click(object sender, EventArgs e) { ClearLocalLog(); }
97 #endregion
98
99 private void ReportProgress(int progress)
100 {
101 if (this.InvokeRequired)
102 {
103 try { this.Invoke((Action)(delegate { ReportProgress(progress); })); }
104 catch { }
105 return;
106 }
107 if (progress_status != null && !this.IsDisposed)
108 progress_status.Value = progress;
109 }
110
111 #region gLog Events
112 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
113 #endregion
114 #region XMLT Events
115 void XMLTV_OnInstanceCreated(object sender, EventArgs e) { ReportProgress(0); CreateControls(); }
116 void LoadXMLTVShcedule(string schedule_xml) { XMLTV.Create(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated)); }
117 #endregion
118
119
120 #region Logging Events
121 StringBuilder log_flusher = new StringBuilder();
122 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
123 void OnLogFlush(string logmessage)
124 {
125 if (this.IsDisposed) { return; }
126 UpdateStatus(logmessage);
127 UpdateLogOutput(logmessage);
128 Application.DoEvents();
129 }
130 private void ClearLocalLog()
131 {
132 var log_top_entry = txtLog.Lines.FirstOrDefault();
133 txtLog.Clear();
134 if (!string.IsNullOrEmpty(log_top_entry)) { txtLog.AppendText(log_top_entry); }
135 }
136 void UpdateStatus(string logmessage)
137 {
138 try
139 {
140 if (this.InvokeRequired)
141 {
142 try { this.Invoke((Action)(delegate { UpdateStatus(logmessage); })); }
143 catch { }
144 return;
145 }
146 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
147 }
148 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
149 }
150 void UpdateLogOutput(string logmessage)
151 {
152 try
153 {
154 if (this.InvokeRequired)
155 {
156 try { this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); }
157 catch { }
158 return;
159 }
160 txtLog.AppendText(logmessage);
161 txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
162 txtLog.ScrollToCaret(); //Now scroll it automatically
163 }
164 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
165 }
166 #endregion
167
168 #region Binary Data Read/Write support
169 private void DeserializeDataFromFile(object filename)
170 {
171 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
172 XMLTV.Load(filename.ToString(), new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
173 }
174 private void DeserializeDataToFile(object filename)
175 {
176 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
177 XMLTV.Save(filename.ToString());
178 }
179 #endregion
180
181
182 #region t
183 void CreateControls()
184 {
185 try
186 {
187 if (this.InvokeRequired)
188 {
189 try { this.Invoke((Action)(delegate { CreateControls(); })); }
190 catch { }
191 return;
192 }
193 Type data_type;
194 object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
195 CreatControlsFromType(data_type);
196 //XMLTV.CreateSorterFromDataSource(ref datasource, "title");
197 dataGrid.DataSource = datasource;
198 flow_datagrid_sort.Enabled = true;
199 }
200 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
201 }
202
203
204 void CreatControlsFromType(Type data_type)
205 {
206 #if USE_DYNAMIC_SORTING_FILTERING_CONTROLS
207 _CreatControlsFromType(data_type);
208 #else
209 gLog.Warn.WriteLine("Dynamic creation of sorting and filtering controls has not been enabled.");
210 gLog.Warn.WriteLine("To enable: #define USE_DYNAMIC_SORTING_FILTERING_CONTROLS");
211 #endif
212 }
213 [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
214 void _CreatControlsFromType(Type data_type)
215 {
216 string strType = data_type.Name;
217 switch (strType)
218 {
219 case "IChannelDefintionList": CreateChannelDefinitionControls(data_type); break;
220 case "IProgramDefinitionList": CreateProgramDefinitionControls(data_type); break;
221 case "IXMLTVSource": CreateSourceDefinitionControls(data_type); break;
222 default: CreateDefaultControls(data_type); break;
223 }
224 }
225 [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
226 void CreateDefaultControls(Type data_type)
227 {
228 // no default controls
229 gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
230 }
231 [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
232 void CreateSourceDefinitionControls(Type data_type)
233 {
234 // no controls needed
235 gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
236 }
237 [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
238 void CreateChannelDefinitionControls(Type data_type)
239 {
240 gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
241 }
242 [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
243 void CreateProgramDefinitionControls(Type data_type)
244 {
245 gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
246 }
247 #endregion
248
249
250 #region Column Sorting Support
251 private void btnResetSort_Click(object sender, EventArgs e)
252 {
253 Type data_type;
254 object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
255 dataGrid.DataSource = datasource;
256 }
257 private void btnSortChannelName_Click(object sender, EventArgs e)
258 {
259 object data = dataGrid.DataSource;
260 }
261
262 private void btnSortStart_Click(object sender, EventArgs e)
263 {
264 object data = dataGrid.DataSource;
265 }
266 private void btnSortStop_Click(object sender, EventArgs e)
267 {
268 object data = dataGrid.DataSource;
269 }
270 private void btnSortTitle_Click(object sender, EventArgs e)
271 {
272 object data = dataGrid.DataSource;
273 }
274 private void btnSortSubTitle_Click(object sender, EventArgs e)
275 {
276 object data = dataGrid.DataSource;
277 XMLTV.CreateSorterFromDataSource(ref data, "subtitle");
278 dataGrid.DataSource = data;
279 }
280 private void btnSortDescription_Click(object sender, EventArgs e)
281 {
282 object data = dataGrid.DataSource;
283 }
284 #endregion
285
286
287 }
288 }
289

  ViewVC Help
Powered by ViewVC 1.1.22