/[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 253 - (hide annotations) (download)
Wed Apr 17 21:31:13 2013 UTC (6 years, 7 months ago) by william
File size: 22179 byte(s)
rename: #region XMLT Events to: #region XMLTV Events

1 william 149 #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 241 const long MAX_LOG_FILESIZE_IN_BYTES = 250000; //250kb max
27     void TruncateLogFile(FileInfo log_file)
28     {
29     if (!log_file.Exists)
30     {
31     gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
32     }
33     else
34     {
35     if (log_file.Length > MAX_LOG_FILESIZE_IN_BYTES)
36     {
37     string match = string.Format("{0}.*", log_file.Name);
38     var files = Directory.GetFiles(log_file.Directory.FullName, match).ToList();
39     files.RemoveAt(0);
40     files.TrimExcess();
41     int count = files.Count;
42     if (log_file.Exists)
43     {
44     File.Copy(log_file.FullName, string.Format("{0}.{1}", log_file.FullName, count));
45     }
46     gLog.CreateLog(log_file.FullName, true, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
47     }
48     else
49     {
50     gLog.CreateLog(log_file.FullName, false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
51     }
52     }
53     }
54    
55 william 13 public main()
56 william 2 {
57     InitializeComponent();
58 william 62 ListViewSorter Sorter = new ListViewSorter();
59 william 120 //lstPrograms.ListViewItemSorter = Sorter;
60 william 241
61 william 13 string log_path = Application.StartupPath;
62     string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
63 william 241 FileInfo log_file = new FileInfo(string.Format(@"{0}\{1}", log_path, log_filename));
64     TruncateLogFile(log_file);
65 william 67 #if DEBUG
66     LogLevel gLevel = gLog.LogLevel;
67 william 69 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
68     gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
69     #else
70     gLevel |= LogLevel.kLogLevel_VerboseDebug;
71     #endif
72     gLevel |= LogLevel.kLogLevel_Debug;
73 william 67 gLog.SetLogLevel(gLevel);
74     #else
75 william 68 LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
76     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
77 william 69 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
78     gLevel &= ~LogLevel.kLogLevel_Debug;
79     #else
80     gLevel |= LogLevel.kLogLevel_Debug;
81     #endif
82 william 70 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
83 william 67 gLog.SetLogLevel(gLevel);
84     #endif
85    
86    
87 william 59 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
88 william 2 }
89 william 127 #region main form events
90     private void mnuItemExit_Click(object sender, EventArgs e) { this.Close(); }
91     private void main_Load(object sender, EventArgs e) { }
92     private void main_Shown(object sender, EventArgs e) { }
93     private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
94     {
95     try
96     {
97     var result = xmltv_file_chooser.ShowDialog();
98     if (result != DialogResult.OK) return;
99     ClearLocalLog();
100     LoadXMLTVShcedule(xmltv_file_chooser.FileName);
101     }
102     catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
103     }
104     private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
105     {
106     try
107     {
108     var result = xmltv_program_data_loader.ShowDialog();
109     if (result != DialogResult.OK) return;
110     string filename = xmltv_program_data_loader.FileName;
111     Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataFromFile)); worker.Start(filename);
112     }
113     catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
114     }
115     private void mnuItemSaveData_Click(object sender, EventArgs e)
116     {
117     try
118     {
119     var result = xmltv_program_data_saver.ShowDialog();
120     if (result != DialogResult.OK) return;
121     string filename = xmltv_program_data_saver.FileName;
122     Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataToFile)); worker.Start(filename);
123     }
124     catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
125     }
126     private void main_FormClosing(object sender, FormClosingEventArgs e) { try { XMLTV.Destroy(); } catch { } }
127     private void mnuItemClearLocalLog_Click(object sender, EventArgs e) { ClearLocalLog(); }
128     #endregion
129 william 11
130 william 59 private void ReportProgress(int progress)
131     {
132 william 84 if (this.InvokeRequired)
133 william 59 {
134 william 127 try { this.Invoke((Action)(delegate { ReportProgress(progress); })); }
135 william 84 catch { }
136     return;
137 william 108 }
138 william 114 if (progress_status != null && !this.IsDisposed)
139 william 108 progress_status.Value = progress;
140 william 59 }
141 william 127
142     #region gLog Events
143 william 59 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
144 william 127 #endregion
145 william 253 #region XMLTV Events
146 william 127 void XMLTV_OnInstanceCreated(object sender, EventArgs e) { ReportProgress(0); CreateControls(); }
147     void LoadXMLTVShcedule(string schedule_xml) { XMLTV.Create(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated)); }
148     #endregion
149 william 59
150 william 127
151     #region Logging Events
152 william 40 StringBuilder log_flusher = new StringBuilder();
153 william 127 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
154 william 40 void OnLogFlush(string logmessage)
155     {
156 william 47 if (this.IsDisposed) { return; }
157 william 54 UpdateStatus(logmessage);
158     UpdateLogOutput(logmessage);
159     Application.DoEvents();
160     }
161 william 127 private void ClearLocalLog()
162     {
163     var log_top_entry = txtLog.Lines.FirstOrDefault();
164     txtLog.Clear();
165     if (!string.IsNullOrEmpty(log_top_entry)) { txtLog.AppendText(log_top_entry); }
166     }
167 william 54 void UpdateStatus(string logmessage)
168 william 61 {
169 william 127 try
170 william 72 {
171 william 127 if (this.InvokeRequired)
172 william 72 {
173 william 127 try { this.Invoke((Action)(delegate { UpdateStatus(logmessage); })); }
174     catch { }
175     return;
176 william 72 }
177 william 127 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
178 william 72 }
179 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
180 william 54 }
181     void UpdateLogOutput(string logmessage)
182     {
183 william 127 try
184 william 54 {
185 william 127 if (this.InvokeRequired)
186 william 56 {
187 william 127 try { this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); }
188     catch { }
189     return;
190 william 56 }
191 william 127 txtLog.AppendText(logmessage);
192     txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
193     txtLog.ScrollToCaret(); //Now scroll it automatically
194 william 54 }
195 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
196 william 40 }
197 william 127 #endregion
198 william 40
199 william 127 #region Binary Data Read/Write support
200 william 115 private void DeserializeDataFromFile(object filename)
201     {
202 william 118 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
203     XMLTV.Load(filename.ToString(), new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
204 william 115 }
205 william 116 private void DeserializeDataToFile(object filename)
206     {
207     if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
208 william 118 XMLTV.Save(filename.ToString());
209 william 116 }
210 william 127 #endregion
211 william 44
212 william 127
213 william 146 #region CreateControls
214 william 145 void AutoResizeDataGridView()
215     {
216     //dataGrid.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
217    
218     for (int i = 0; i < dataGrid.ColumnCount; i++)
219     {
220     dataGrid.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
221     }
222 william 146 //for (int i = 0; i < dataGrid.RowCount; i++)
223     //{
224     // dataGrid.AutoResizeRow(i, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
225     //}
226 william 145 }
227 william 127 void CreateControls()
228 william 44 {
229 william 54 try
230     {
231 william 127 if (this.InvokeRequired)
232     {
233     try { this.Invoke((Action)(delegate { CreateControls(); })); }
234     catch { }
235     return;
236     }
237 william 129 Type data_type;
238     object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
239 william 145 CreatControlsFromType(data_type);
240 william 127 dataGrid.DataSource = datasource;
241 william 145 AutoResizeDataGridView();
242 william 130 flow_datagrid_sort.Enabled = true;
243 william 143 btnSort.PerformClick();
244 william 54 }
245 william 127 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
246 william 44 }
247 william 129
248 william 135
249 william 129 void CreatControlsFromType(Type data_type)
250     {
251     #if USE_DYNAMIC_SORTING_FILTERING_CONTROLS
252     _CreatControlsFromType(data_type);
253     #else
254 william 149 gLog.Verbose.Warn.WriteLine("Dynamic creation of sorting and filtering controls has not been enabled.");
255     gLog.Verbose.Warn.WriteLine("To enable: #define USE_DYNAMIC_SORTING_FILTERING_CONTROLS");
256 william 129 #endif
257     }
258     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
259     void _CreatControlsFromType(Type data_type)
260     {
261     string strType = data_type.Name;
262     switch (strType)
263     {
264     case "IChannelDefintionList": CreateChannelDefinitionControls(data_type); break;
265     case "IProgramDefinitionList": CreateProgramDefinitionControls(data_type); break;
266     case "IXMLTVSource": CreateSourceDefinitionControls(data_type); break;
267     default: CreateDefaultControls(data_type); break;
268     }
269     }
270     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
271     void CreateDefaultControls(Type data_type)
272     {
273     // no default controls
274 william 149 gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
275 william 129 }
276     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
277     void CreateSourceDefinitionControls(Type data_type)
278     {
279     // no controls needed
280 william 149 gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
281 william 129 }
282     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
283     void CreateChannelDefinitionControls(Type data_type)
284     {
285 william 149 gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
286 william 129 }
287     [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
288     void CreateProgramDefinitionControls(Type data_type)
289     {
290 william 149 gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
291 william 129 }
292 william 135 #endregion
293 william 130
294 william 146 #region Program Entry Display
295 william 249 //private int CurrentRowIndex = -1;
296 william 146 void DisplaySelectedProgramEntry(DataGridViewRow row)
297     {
298 william 249 //int RowIndex = row.Index; // this is the index in the list
299 william 146
300     //if (CurrentRowIndex == -1) { AlreadyDisplayedProgramEntryForIndex = false; CurrentRowIndex = RowIndex; }
301     //if (CurrentRowIndex == RowIndex && AlreadyDisplayedProgramEntryForIndex) {
302 william 249 //bool DoDisplay = false;
303     //if (CurrentRowIndex == -1)
304     //{
305     // DoDisplay = true;
306     //}
307     ////if (CurrentRowIndex == RowIndex)
308     ////{
309     //// DoDisplay = false;
310     ////}
311     ////else
312     ////{
313     //// DoDisplay = true;
314     ////}
315    
316     //if (DoDisplay)
317     //{
318     //CurrentRowIndex = RowIndex;
319     List<IProgramDefinition> list = (dataGrid.DataSource as List<IProgramDefinition>);
320     if (list == null)
321 william 146 {
322 william 249 gLog.Warn.WriteLine("Unable to display program entry for rowindex: {0}", row.Index);
323    
324     return;
325 william 146 }
326 william 249 try
327 william 146 {
328 william 249 IProgramDefinition program = list[row.Index];
329     ProgramEntryControl c = new ProgramEntryControl(program);
330     c.Dock = DockStyle.Fill;
331     split_bottom.Panel2.Controls.Clear();
332     split_bottom.Panel2.Controls.Add(c);
333 william 146 }
334 william 249 catch (Exception ex)
335 william 146 {
336 william 249 gLog.Error.WriteLine(ex.ToString());
337 william 146 }
338 william 249 //}
339 william 146 }
340     #endregion
341 william 143 #region Datasource support / events
342     private void dataGrid_SelectionChanged(object sender, EventArgs e)
343     {
344     try
345     {
346     DataGridView dgv = (DataGridView)sender;
347     //var row = dgv.SelectedRows[0]; // the row that was selected
348     //var cell = dgv.SelectedCells[0];
349     DataGridViewRow row = null;
350     string data = string.Empty;
351     if (dgv.SelectedCells.Count > 0)
352     {
353     var cell = dgv.SelectedCells[0];
354     row = dgv.Rows[cell.RowIndex];
355     }
356     if (dgv.SelectedRows.Count > 0)
357     {
358     row = dgv.SelectedRows[0];
359     }
360 william 130
361 william 143 if (row != null)
362     {
363     RadioButton button = GetSelectedFilteringControl();
364     int index = GetColumnIndexForRadioButton(button);
365     data = row.Cells[index].Value.ToString();
366     txtFilterText.Text = data;
367 william 146 DisplaySelectedProgramEntry(row);
368 william 143 }
369     }
370     catch (Exception ex)
371     {
372     gLog.Verbose.Error.WriteLine(ex.ToString());
373     }
374     }
375     private int GetColumnIndexForRadioButton(RadioButton radio) { return GetColumnIndex(radio.Tag as string); }
376     private int GetColumnIndex(string columnname)
377     {
378     int index =0;
379     Type t = typeof(IProgramDefinition);
380    
381     var props = t.GetProperties();
382     foreach (var prop in props)
383     {
384     if (prop.Name.ToLower() == columnname.ToLower()) { break; }
385     index++;
386     }
387     return index;
388     }
389     #endregion
390     #region Reset event
391 william 130 private void btnResetSort_Click(object sender, EventArgs e)
392     {
393     Type data_type;
394     object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
395     dataGrid.DataSource = datasource;
396 william 131 ToggleDescedning();
397 william 130 }
398 william 127 #endregion
399 william 143 #region Filtering events
400 william 137 private void FilterByColumn(RadioButton button)
401 william 132 {
402 william 133 Type data_type;
403 william 137 string column = (button.Tag as string);
404 william 133 object data = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
405 william 137 XMLTV.CreateFilterFromDataSource(ref data, column, txtFilterText.Text);
406     dataGrid.DataSource = data;
407 william 248 dataGrid_SelectionChanged(dataGrid, new EventArgs());
408 william 137 }
409 william 139 private RadioButton GetSelectedFilteringControl()
410 william 135 {
411 william 139 bool control_selected = false;
412     RadioButton button = new RadioButton();
413 william 143 foreach (var c in grpFilter_flow.Controls)
414 william 139 {
415     button = (c as RadioButton);
416     if (button != null) { if (button.Checked) { control_selected = true; break; } }
417     else { continue; }
418     }
419     if (!control_selected)
420     {
421 william 149 gLog.Verbose.Warn.WriteLine("Unkown filter value used. Was not: channelname, title, subtitle, description, start, or stop.");
422 william 143 //throw new InvalidOperationException("No filtering control is currently selected.");
423     return button;
424 william 139 }
425     else { return button; }
426 william 137 }
427 william 143 private void UpdateFilterText(RadioButton radio, DataGridView dgv)
428 william 137 {
429 william 135 if (radio.Checked)
430     {
431 william 137 int cellindex = GetColumnIndexForRadioButton(radio);
432     string t = radio.Tag as string;
433 william 135 DataGridViewRow row = null;
434     string data = string.Empty;
435     if (dgv.SelectedCells.Count > 0)
436     {
437     var cell = dgv.SelectedCells[0];
438     row = dgv.Rows[cell.RowIndex];
439     }
440     if (dgv.SelectedRows.Count > 0)
441     {
442     row = dgv.SelectedRows[0];
443     }
444     data = row.Cells[cellindex].Value.ToString();
445     txtFilterText.Text = data;
446     }
447     }
448 william 143 private void btnFilter_Click(object sender, EventArgs e)
449     {
450     RadioButton button = GetSelectedFilteringControl();
451     FilterByColumn(button);
452     }
453 william 137 private void radio_filter_channelnumber_CheckedChanged(object sender, EventArgs e)
454 william 135 {
455     RadioButton radio = (sender as RadioButton);
456 william 137 UpdateFilterText(radio, dataGrid);
457 william 135 }
458     private void radio_filter_title_CheckedChanged(object sender, EventArgs e)
459     {
460     RadioButton radio = (sender as RadioButton);
461 william 137 UpdateFilterText(radio, dataGrid);
462 william 135 }
463     private void radio_filter_subtitle_CheckedChanged(object sender, EventArgs e)
464     {
465     RadioButton radio = (sender as RadioButton);
466 william 137 UpdateFilterText(radio, dataGrid);
467 william 135 }
468     private void radio_filter_description_CheckedChanged(object sender, EventArgs e)
469     {
470     RadioButton radio = (sender as RadioButton);
471 william 137 UpdateFilterText(radio, dataGrid);
472 william 135 }
473 william 137 private void radio_filter_channelname_CheckedChanged(object sender, EventArgs e)
474     {
475     RadioButton radio = (sender as RadioButton);
476     UpdateFilterText(radio, dataGrid);
477     }
478 william 142 private void radio_filter_start_CheckedChanged(object sender, EventArgs e)
479     {
480     RadioButton radio = (sender as RadioButton);
481     UpdateFilterText(radio, dataGrid);
482     }
483     private void radio_filter_stop_CheckedChanged(object sender, EventArgs e)
484     {
485     RadioButton radio = (sender as RadioButton);
486     UpdateFilterText(radio, dataGrid);
487 william 143 }
488 william 227 private void radio_filter_rating_CheckedChanged(object sender, EventArgs e)
489     {
490     RadioButton radio = (sender as RadioButton);
491     UpdateFilterText(radio, dataGrid);
492     }
493 william 143 #endregion
494     #region Sorting events
495     private bool Descending = false;
496     private void ToggleDescedning() { Descending = Descending ? false : true; }
497     private void SortByColumn(RadioButton button)
498     {
499     string column = (button.Tag as string);
500     object data = dataGrid.DataSource;
501     XMLTV.CreateSorterFromDataSource(ref data, Descending, column);
502     dataGrid.DataSource = data;
503     ToggleDescedning();
504 william 248 dataGrid_SelectionChanged(dataGrid, new EventArgs());
505 william 143 }
506     private RadioButton GetSelectedSortControl()
507     {
508     bool control_selected = false;
509     RadioButton button = new RadioButton();
510     foreach (var c in grpSort_flow.Controls)
511     {
512     button = (c as RadioButton);
513     if (button != null) { if (button.Checked) { control_selected = true; break; } }
514     else { continue; }
515     }
516     if (!control_selected)
517     {
518     gLog.Warn.WriteLine("Unkown sort value used. Was not: channelname, title, subtitle, description, stop, or start.");
519     //throw new InvalidOperationException("No sorting control is currently selected.");
520     return button;
521     }
522     else { return button; }
523     }
524     private void btnSort_Click(object sender, EventArgs e)
525     {
526     RadioButton button = GetSelectedSortControl();
527     SortByColumn(button);
528     }
529     #endregion
530 william 227
531 william 248 private void dataGrid_Click(object sender, EventArgs e)
532     {
533     dataGrid_SelectionChanged(dataGrid, new EventArgs());
534     }
535    
536 william 227
537 william 2 }
538     }
539 william 29

  ViewVC Help
Powered by ViewVC 1.1.22