/[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 147 - (hide annotations) (download)
Thu Mar 14 15:23:09 2013 UTC (7 years, 4 months ago) by william
File size: 20328 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 william 146 #region CreateControls
183 william 145 void AutoResizeDataGridView()
184     {
185     //dataGrid.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
186    
187     for (int i = 0; i < dataGrid.ColumnCount; i++)
188     {
189     dataGrid.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
190     }
191 william 146 //for (int i = 0; i < dataGrid.RowCount; i++)
192     //{
193     // dataGrid.AutoResizeRow(i, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
194     //}
195 william 145 }
196 william 127 void CreateControls()
197 william 44 {
198 william 54 try
199     {
200 william 127 if (this.InvokeRequired)
201     {
202     try { this.Invoke((Action)(delegate { CreateControls(); })); }
203     catch { }
204     return;
205     }
206 william 129 Type data_type;
207     object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
208 william 145 CreatControlsFromType(data_type);
209 william 127 dataGrid.DataSource = datasource;
210 william 145 AutoResizeDataGridView();
211 william 130 flow_datagrid_sort.Enabled = true;
212 william 143 btnSort.PerformClick();
213 william 54 }
214 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
215 william 44 }
216 william 129
217 william 135
218 william 129 void CreatControlsFromType(Type data_type)
219     {
220     #if USE_DYNAMIC_SORTING_FILTERING_CONTROLS
221     _CreatControlsFromType(data_type);
222     #else
223     gLog.Warn.WriteLine("Dynamic creation of sorting and filtering controls has not been enabled.");
224     gLog.Warn.WriteLine("To enable: #define USE_DYNAMIC_SORTING_FILTERING_CONTROLS");
225     #endif
226     }
227     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
228     void _CreatControlsFromType(Type data_type)
229     {
230     string strType = data_type.Name;
231     switch (strType)
232     {
233     case "IChannelDefintionList": CreateChannelDefinitionControls(data_type); break;
234     case "IProgramDefinitionList": CreateProgramDefinitionControls(data_type); break;
235     case "IXMLTVSource": CreateSourceDefinitionControls(data_type); break;
236     default: CreateDefaultControls(data_type); break;
237     }
238     }
239     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
240     void CreateDefaultControls(Type data_type)
241     {
242     // no default controls
243     gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
244     }
245     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
246     void CreateSourceDefinitionControls(Type data_type)
247     {
248     // no controls needed
249     gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
250     }
251     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
252     void CreateChannelDefinitionControls(Type data_type)
253     {
254     gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
255     }
256     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
257     void CreateProgramDefinitionControls(Type data_type)
258     {
259     gLog.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
260     }
261 william 135 #endregion
262 william 130
263 william 146 #region Program Entry Display
264     private int CurrentRowIndex = -1;
265     void DisplaySelectedProgramEntry(DataGridViewRow row)
266     {
267     int RowIndex = row.Index; // this is the index in the list
268    
269     //if (CurrentRowIndex == -1) { AlreadyDisplayedProgramEntryForIndex = false; CurrentRowIndex = RowIndex; }
270     //if (CurrentRowIndex == RowIndex && AlreadyDisplayedProgramEntryForIndex) {
271     bool DoDisplay = false;
272     if (CurrentRowIndex == -1)
273     {
274     DoDisplay = true;
275     }
276     if (CurrentRowIndex == RowIndex)
277     {
278     DoDisplay = false;
279     }
280     else
281     {
282     DoDisplay = true;
283     }
284    
285     if (DoDisplay)
286     {
287     CurrentRowIndex = RowIndex;
288     List<IProgramDefinition> list = (dataGrid.DataSource as List<IProgramDefinition>);
289     if (list == null)
290     {
291     gLog.Warn.WriteLine("Unable to display program entry for rowindex: {0}", RowIndex);
292    
293     return;
294     }
295     try
296     {
297     IProgramDefinition program = list[RowIndex];
298 william 147 ProgramEntryControl c = new ProgramEntryControl(program);
299     c.Dock = DockStyle.Fill;
300     split_bottom.Panel2.Controls.Clear();
301     split_bottom.Panel2.Controls.Add(c);
302 william 146 }
303     catch (Exception ex)
304     {
305     gLog.Error.WriteLine(ex.ToString());
306     }
307     }
308     }
309     #endregion
310 william 143 #region Datasource support / events
311     private void dataGrid_SelectionChanged(object sender, EventArgs e)
312     {
313     try
314     {
315     DataGridView dgv = (DataGridView)sender;
316     //var row = dgv.SelectedRows[0]; // the row that was selected
317     //var cell = dgv.SelectedCells[0];
318     DataGridViewRow row = null;
319     string data = string.Empty;
320     if (dgv.SelectedCells.Count > 0)
321     {
322     var cell = dgv.SelectedCells[0];
323     row = dgv.Rows[cell.RowIndex];
324     }
325     if (dgv.SelectedRows.Count > 0)
326     {
327     row = dgv.SelectedRows[0];
328     }
329 william 130
330 william 143 if (row != null)
331     {
332     RadioButton button = GetSelectedFilteringControl();
333     int index = GetColumnIndexForRadioButton(button);
334     data = row.Cells[index].Value.ToString();
335     txtFilterText.Text = data;
336 william 146 DisplaySelectedProgramEntry(row);
337 william 143 }
338     }
339     catch (Exception ex)
340     {
341     gLog.Verbose.Error.WriteLine(ex.ToString());
342     }
343     }
344     private int GetColumnIndexForRadioButton(RadioButton radio) { return GetColumnIndex(radio.Tag as string); }
345     private int GetColumnIndex(string columnname)
346     {
347     int index =0;
348     Type t = typeof(IProgramDefinition);
349    
350     var props = t.GetProperties();
351     foreach (var prop in props)
352     {
353     if (prop.Name.ToLower() == columnname.ToLower()) { break; }
354     index++;
355     }
356     return index;
357     }
358     #endregion
359     #region Reset event
360 william 130 private void btnResetSort_Click(object sender, EventArgs e)
361     {
362     Type data_type;
363     object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
364     dataGrid.DataSource = datasource;
365 william 131 ToggleDescedning();
366 william 130 }
367 william 127 #endregion
368 william 143 #region Filtering events
369 william 137 private void FilterByColumn(RadioButton button)
370 william 132 {
371 william 133 Type data_type;
372 william 137 string column = (button.Tag as string);
373 william 133 object data = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
374 william 137 XMLTV.CreateFilterFromDataSource(ref data, column, txtFilterText.Text);
375     dataGrid.DataSource = data;
376     }
377 william 139 private RadioButton GetSelectedFilteringControl()
378 william 135 {
379 william 139 bool control_selected = false;
380     RadioButton button = new RadioButton();
381 william 143 foreach (var c in grpFilter_flow.Controls)
382 william 139 {
383     button = (c as RadioButton);
384     if (button != null) { if (button.Checked) { control_selected = true; break; } }
385     else { continue; }
386     }
387     if (!control_selected)
388     {
389 william 143 gLog.Warn.WriteLine("Unkown filter value used. Was not: channelname, title, subtitle, description, start, or stop.");
390     //throw new InvalidOperationException("No filtering control is currently selected.");
391     return button;
392 william 139 }
393     else { return button; }
394 william 137 }
395 william 143 private void UpdateFilterText(RadioButton radio, DataGridView dgv)
396 william 137 {
397 william 135 if (radio.Checked)
398     {
399 william 137 int cellindex = GetColumnIndexForRadioButton(radio);
400     string t = radio.Tag as string;
401 william 135 DataGridViewRow row = null;
402     string data = string.Empty;
403     if (dgv.SelectedCells.Count > 0)
404     {
405     var cell = dgv.SelectedCells[0];
406     row = dgv.Rows[cell.RowIndex];
407     }
408     if (dgv.SelectedRows.Count > 0)
409     {
410     row = dgv.SelectedRows[0];
411     }
412     data = row.Cells[cellindex].Value.ToString();
413     txtFilterText.Text = data;
414     }
415     }
416 william 143 private void btnFilter_Click(object sender, EventArgs e)
417     {
418     RadioButton button = GetSelectedFilteringControl();
419     FilterByColumn(button);
420     }
421 william 137 private void radio_filter_channelnumber_CheckedChanged(object sender, EventArgs e)
422 william 135 {
423     RadioButton radio = (sender as RadioButton);
424 william 137 UpdateFilterText(radio, dataGrid);
425 william 135 }
426     private void radio_filter_title_CheckedChanged(object sender, EventArgs e)
427     {
428     RadioButton radio = (sender as RadioButton);
429 william 137 UpdateFilterText(radio, dataGrid);
430 william 135 }
431     private void radio_filter_subtitle_CheckedChanged(object sender, EventArgs e)
432     {
433     RadioButton radio = (sender as RadioButton);
434 william 137 UpdateFilterText(radio, dataGrid);
435 william 135 }
436     private void radio_filter_description_CheckedChanged(object sender, EventArgs e)
437     {
438     RadioButton radio = (sender as RadioButton);
439 william 137 UpdateFilterText(radio, dataGrid);
440 william 135 }
441 william 137 private void radio_filter_channelname_CheckedChanged(object sender, EventArgs e)
442     {
443     RadioButton radio = (sender as RadioButton);
444     UpdateFilterText(radio, dataGrid);
445     }
446 william 142 private void radio_filter_start_CheckedChanged(object sender, EventArgs e)
447     {
448     RadioButton radio = (sender as RadioButton);
449     UpdateFilterText(radio, dataGrid);
450     }
451     private void radio_filter_stop_CheckedChanged(object sender, EventArgs e)
452     {
453     RadioButton radio = (sender as RadioButton);
454     UpdateFilterText(radio, dataGrid);
455 william 143 }
456     #endregion
457     #region Sorting events
458     private bool Descending = false;
459     private void ToggleDescedning() { Descending = Descending ? false : true; }
460     private void SortByColumn(RadioButton button)
461     {
462     string column = (button.Tag as string);
463     object data = dataGrid.DataSource;
464     XMLTV.CreateSorterFromDataSource(ref data, Descending, column);
465     dataGrid.DataSource = data;
466     ToggleDescedning();
467     }
468     private RadioButton GetSelectedSortControl()
469     {
470     bool control_selected = false;
471     RadioButton button = new RadioButton();
472     foreach (var c in grpSort_flow.Controls)
473     {
474     button = (c as RadioButton);
475     if (button != null) { if (button.Checked) { control_selected = true; break; } }
476     else { continue; }
477     }
478     if (!control_selected)
479     {
480     gLog.Warn.WriteLine("Unkown sort value used. Was not: channelname, title, subtitle, description, stop, or start.");
481     //throw new InvalidOperationException("No sorting control is currently selected.");
482     return button;
483     }
484     else { return button; }
485     }
486     private void btnSort_Click(object sender, EventArgs e)
487     {
488     RadioButton button = GetSelectedSortControl();
489     SortByColumn(button);
490     }
491     #endregion
492 william 2 }
493     }
494 william 29

  ViewVC Help
Powered by ViewVC 1.1.22