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

Annotation of /trunk/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 137 - (hide annotations) (download)
Wed Mar 13 16:59:40 2013 UTC (7 years, 4 months ago) by william
File size: 18691 byte(s)

1 william 71 //#define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
2 william 69 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
3 william 129 //#define USE_DYNAMIC_SORTING_FILTERING_CONTROLS // when defined will setup dynamic sorting and filtering controls for XMLTV Data
4 william 69 using System;
5 william 2 using System.Collections.Generic;
6     using System.ComponentModel;
7     using System.Data;
8 william 29 using System.Linq;
9 william 2 using System.Drawing;
10     using System.Text;
11     using System.Windows.Forms;
12 william 11 using libxmltv.Core;
13 william 13 using Enterprise.Logging;
14 william 29 using libxmltv.Interfaces;
15 william 31 using System.IO;
16     using System.Runtime.Serialization.Formatters.Binary;
17 william 40 using System.Threading;
18 william 79 using System.Diagnostics;
19 william 110 using System.Xml.Linq;
20 william 2
21     namespace xmltv_parser
22     {
23 william 50
24 william 13 public partial class main : Form
25 william 2 {
26 william 13 public main()
27 william 2 {
28     InitializeComponent();
29 william 62 ListViewSorter Sorter = new ListViewSorter();
30 william 120 //lstPrograms.ListViewItemSorter = Sorter;
31 william 13 string log_path = Application.StartupPath;
32     string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
33 william 59 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
34 william 67 #if DEBUG
35     LogLevel gLevel = gLog.LogLevel;
36 william 69 #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 william 67 gLog.SetLogLevel(gLevel);
43     #else
44 william 68 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 william 69 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
47     gLevel &= ~LogLevel.kLogLevel_Debug;
48     #else
49     gLevel |= LogLevel.kLogLevel_Debug;
50     #endif
51 william 70 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
52 william 67 gLog.SetLogLevel(gLevel);
53     #endif
54    
55    
56 william 59 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
57 william 2 }
58 william 127 #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 william 11
99 william 59 private void ReportProgress(int progress)
100     {
101 william 84 if (this.InvokeRequired)
102 william 59 {
103 william 127 try { this.Invoke((Action)(delegate { ReportProgress(progress); })); }
104 william 84 catch { }
105     return;
106 william 108 }
107 william 114 if (progress_status != null && !this.IsDisposed)
108 william 108 progress_status.Value = progress;
109 william 59 }
110 william 127
111     #region gLog Events
112 william 59 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
113 william 127 #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 william 59
119 william 127
120     #region Logging Events
121 william 40 StringBuilder log_flusher = new StringBuilder();
122 william 127 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
123 william 40 void OnLogFlush(string logmessage)
124     {
125 william 47 if (this.IsDisposed) { return; }
126 william 54 UpdateStatus(logmessage);
127     UpdateLogOutput(logmessage);
128     Application.DoEvents();
129     }
130 william 127 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 william 54 void UpdateStatus(string logmessage)
137 william 61 {
138 william 127 try
139 william 72 {
140 william 127 if (this.InvokeRequired)
141 william 72 {
142 william 127 try { this.Invoke((Action)(delegate { UpdateStatus(logmessage); })); }
143     catch { }
144     return;
145 william 72 }
146 william 127 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
147 william 72 }
148 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
149 william 54 }
150     void UpdateLogOutput(string logmessage)
151     {
152 william 127 try
153 william 54 {
154 william 127 if (this.InvokeRequired)
155 william 56 {
156 william 127 try { this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); }
157     catch { }
158     return;
159 william 56 }
160 william 127 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 william 54 }
164 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
165 william 40 }
166 william 127 #endregion
167 william 40
168 william 127 #region Binary Data Read/Write support
169 william 115 private void DeserializeDataFromFile(object filename)
170     {
171 william 118 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
172     XMLTV.Load(filename.ToString(), new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
173 william 115 }
174 william 116 private void DeserializeDataToFile(object filename)
175     {
176     if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
177 william 118 XMLTV.Save(filename.ToString());
178 william 116 }
179 william 127 #endregion
180 william 44
181 william 127
182     #region t
183     void CreateControls()
184 william 44 {
185 william 54 try
186     {
187 william 127 if (this.InvokeRequired)
188     {
189     try { this.Invoke((Action)(delegate { CreateControls(); })); }
190     catch { }
191     return;
192     }
193 william 129 Type data_type;
194     object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
195     CreatControlsFromType(data_type);
196 william 128 //XMLTV.CreateSorterFromDataSource(ref datasource, "title");
197 william 127 dataGrid.DataSource = datasource;
198 william 130 flow_datagrid_sort.Enabled = true;
199 william 54 }
200 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
201 william 44 }
202 william 129
203 william 135
204 william 129 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 william 135 #endregion
248 william 130
249    
250     #region Column Sorting Support
251 william 131 private bool Descending = false;
252     private void ToggleDescedning() { Descending = Descending ? false : true; }
253 william 130 private void btnResetSort_Click(object sender, EventArgs e)
254     {
255     Type data_type;
256     object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
257     dataGrid.DataSource = datasource;
258 william 131 ToggleDescedning();
259 william 130 }
260 william 137
261     private void SortByColumn(Button button)
262 william 130 {
263 william 137 string column = (button.Tag as string);
264 william 130 object data = dataGrid.DataSource;
265 william 137 XMLTV.CreateSorterFromDataSource(ref data, Descending, column);
266 william 131 dataGrid.DataSource = data;
267     ToggleDescedning();
268 william 130 }
269    
270 william 137 private void btnSortChannelName_Click(object sender, EventArgs e)
271     {
272     Button button = (sender as Button);
273     SortByColumn(button);
274     }
275     private void btnSortChannelNumber_Click(object sender, EventArgs e)
276     {
277     Button button = (sender as Button);
278     SortByColumn(button);
279     }
280 william 130 private void btnSortStart_Click(object sender, EventArgs e)
281     {
282 william 137 Button button = (sender as Button);
283     SortByColumn(button);
284 william 130 }
285     private void btnSortStop_Click(object sender, EventArgs e)
286     {
287 william 137 Button button = (sender as Button);
288     SortByColumn(button);
289 william 130 }
290     private void btnSortTitle_Click(object sender, EventArgs e)
291     {
292 william 137 Button button = (sender as Button);
293     SortByColumn(button);
294 william 130 }
295     private void btnSortSubTitle_Click(object sender, EventArgs e)
296     {
297 william 137 Button button = (sender as Button);
298     SortByColumn(button);
299 william 130 }
300     private void btnSortDescription_Click(object sender, EventArgs e)
301     {
302 william 137 Button button = (sender as Button);
303     SortByColumn(button);
304 william 130 }
305 william 127 #endregion
306 william 137 private void FilterByColumn(RadioButton button)
307 william 132 {
308 william 133 Type data_type;
309 william 137 string column = (button.Tag as string);
310 william 133 object data = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
311 william 137 XMLTV.CreateFilterFromDataSource(ref data, column, txtFilterText.Text);
312     dataGrid.DataSource = data;
313     }
314     private void btnFilter_Click(object sender, EventArgs e)
315     {
316     if (radio_filter_channelnumber.Checked)
317 william 132 {
318 william 137 FilterByColumn(radio_filter_channelnumber);
319 william 132 }
320     else if (radio_filter_title.Checked)
321     {
322 william 137 FilterByColumn(radio_filter_title);
323 william 132 }
324     else if (radio_filter_subtitle.Checked)
325     {
326 william 137 FilterByColumn(radio_filter_subtitle);
327 william 132 }
328     else if (radio_filter_description.Checked)
329     {
330 william 137 FilterByColumn(radio_filter_description);
331 william 132 }
332 william 137 else if (radio_filter_channelname.Checked)
333     {
334     FilterByColumn(radio_filter_channelname);
335     }
336 william 132 else
337     {
338     // unknown filter
339     gLog.Warn.WriteLine("Unkown filter value used. Was not: channelname, title, subtitle, or description.");
340 william 137 }
341 william 132 }
342    
343 william 135 private void dataGrid_SelectionChanged(object sender, EventArgs e)
344     {
345     try
346     {
347     DataGridView dgv = (DataGridView)sender;
348     //var row = dgv.SelectedRows[0]; // the row that was selected
349     //var cell = dgv.SelectedCells[0];
350     DataGridViewRow row = null;
351     string data = string.Empty;
352     if (dgv.SelectedCells.Count > 0)
353     {
354     var cell = dgv.SelectedCells[0];
355     row = dgv.Rows[cell.RowIndex];
356     }
357     if (dgv.SelectedRows.Count > 0)
358     {
359     row = dgv.SelectedRows[0];
360     }
361 william 137 if (radio_filter_channelnumber.Checked)
362 william 135 {
363 william 137 data = row.Cells[GetColumnIndexForRadioButton(radio_filter_channelnumber)].Value.ToString();
364 william 135 }
365     else if (radio_filter_title.Checked)
366     {
367 william 137 data = row.Cells[GetColumnIndexForRadioButton(radio_filter_title)].Value.ToString();
368 william 135 }
369     else if (radio_filter_subtitle.Checked)
370     {
371 william 137 data = row.Cells[GetColumnIndexForRadioButton(radio_filter_subtitle)].Value.ToString();
372 william 135 }
373     else if (radio_filter_description.Checked)
374     {
375 william 137 data = row.Cells[GetColumnIndexForRadioButton(radio_filter_description)].Value.ToString();
376 william 135 }
377 william 137 else if (radio_filter_channelname.Checked)
378     {
379     data = row.Cells[GetColumnIndexForRadioButton(radio_filter_channelname)].Value.ToString();
380     }
381 william 135 else
382     {
383     // unknown filter
384     gLog.Warn.WriteLine("Unkown filter value used. Was not: channelname, title, subtitle, or description.");
385     }
386    
387     txtFilterText.Text = data;
388     }
389     catch (Exception ex)
390     {
391     gLog.Verbose.Error.WriteLine(ex.ToString());
392     }
393     }
394    
395    
396 william 137 private int GetColumnIndexForRadioButton(RadioButton radio)
397 william 135 {
398 william 137 return GetColumnIndex(radio.Tag as string);
399     }
400     private int GetColumnIndex(string columnname)
401     {
402     int index =0;
403     Type t = typeof(IProgramDefinition);
404    
405     var props = t.GetProperties();
406     foreach (var prop in props)
407     {
408     if (prop.Name.ToLower() == columnname.ToLower()) { break; }
409     index++;
410     }
411     return index;
412     }
413    
414     private void UpdateFilterText(RadioButton radio, DataGridView dgv)
415     {
416 william 135 if (radio.Checked)
417     {
418 william 137 int cellindex = GetColumnIndexForRadioButton(radio);
419     string t = radio.Tag as string;
420 william 135 DataGridViewRow row = null;
421     string data = string.Empty;
422     if (dgv.SelectedCells.Count > 0)
423     {
424     var cell = dgv.SelectedCells[0];
425     row = dgv.Rows[cell.RowIndex];
426     }
427     if (dgv.SelectedRows.Count > 0)
428     {
429     row = dgv.SelectedRows[0];
430     }
431     data = row.Cells[cellindex].Value.ToString();
432     txtFilterText.Text = data;
433     }
434     }
435 william 137 private void radio_filter_channelnumber_CheckedChanged(object sender, EventArgs e)
436 william 135 {
437     RadioButton radio = (sender as RadioButton);
438 william 137 UpdateFilterText(radio, dataGrid);
439 william 135 }
440     private void radio_filter_title_CheckedChanged(object sender, EventArgs e)
441     {
442     RadioButton radio = (sender as RadioButton);
443 william 137 UpdateFilterText(radio, dataGrid);
444 william 135 }
445    
446     private void radio_filter_subtitle_CheckedChanged(object sender, EventArgs e)
447     {
448     RadioButton radio = (sender as RadioButton);
449 william 137 UpdateFilterText(radio, dataGrid);
450 william 135 }
451    
452     private void radio_filter_description_CheckedChanged(object sender, EventArgs e)
453     {
454     RadioButton radio = (sender as RadioButton);
455 william 137 UpdateFilterText(radio, dataGrid);
456 william 135 }
457 william 137
458     private void radio_filter_channelname_CheckedChanged(object sender, EventArgs e)
459     {
460     RadioButton radio = (sender as RadioButton);
461     UpdateFilterText(radio, dataGrid);
462     }
463    
464    
465    
466    
467    
468 william 2 }
469     }
470 william 29

  ViewVC Help
Powered by ViewVC 1.1.22