/[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 253 - (show 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 #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 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 public main()
56 {
57 InitializeComponent();
58 ListViewSorter Sorter = new ListViewSorter();
59 //lstPrograms.ListViewItemSorter = Sorter;
60
61 string log_path = Application.StartupPath;
62 string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
63 FileInfo log_file = new FileInfo(string.Format(@"{0}\{1}", log_path, log_filename));
64 TruncateLogFile(log_file);
65 #if DEBUG
66 LogLevel gLevel = gLog.LogLevel;
67 #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 gLog.SetLogLevel(gLevel);
74 #else
75 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 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
78 gLevel &= ~LogLevel.kLogLevel_Debug;
79 #else
80 gLevel |= LogLevel.kLogLevel_Debug;
81 #endif
82 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
83 gLog.SetLogLevel(gLevel);
84 #endif
85
86
87 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
88 }
89 #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
130 private void ReportProgress(int progress)
131 {
132 if (this.InvokeRequired)
133 {
134 try { this.Invoke((Action)(delegate { ReportProgress(progress); })); }
135 catch { }
136 return;
137 }
138 if (progress_status != null && !this.IsDisposed)
139 progress_status.Value = progress;
140 }
141
142 #region gLog Events
143 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
144 #endregion
145 #region XMLTV Events
146 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
150
151 #region Logging Events
152 StringBuilder log_flusher = new StringBuilder();
153 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
154 void OnLogFlush(string logmessage)
155 {
156 if (this.IsDisposed) { return; }
157 UpdateStatus(logmessage);
158 UpdateLogOutput(logmessage);
159 Application.DoEvents();
160 }
161 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 void UpdateStatus(string logmessage)
168 {
169 try
170 {
171 if (this.InvokeRequired)
172 {
173 try { this.Invoke((Action)(delegate { UpdateStatus(logmessage); })); }
174 catch { }
175 return;
176 }
177 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
178 }
179 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
180 }
181 void UpdateLogOutput(string logmessage)
182 {
183 try
184 {
185 if (this.InvokeRequired)
186 {
187 try { this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); }
188 catch { }
189 return;
190 }
191 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 }
195 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
196 }
197 #endregion
198
199 #region Binary Data Read/Write support
200 private void DeserializeDataFromFile(object filename)
201 {
202 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
203 XMLTV.Load(filename.ToString(), new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
204 }
205 private void DeserializeDataToFile(object filename)
206 {
207 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
208 XMLTV.Save(filename.ToString());
209 }
210 #endregion
211
212
213 #region CreateControls
214 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 //for (int i = 0; i < dataGrid.RowCount; i++)
223 //{
224 // dataGrid.AutoResizeRow(i, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
225 //}
226 }
227 void CreateControls()
228 {
229 try
230 {
231 if (this.InvokeRequired)
232 {
233 try { this.Invoke((Action)(delegate { CreateControls(); })); }
234 catch { }
235 return;
236 }
237 Type data_type;
238 object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
239 CreatControlsFromType(data_type);
240 dataGrid.DataSource = datasource;
241 AutoResizeDataGridView();
242 flow_datagrid_sort.Enabled = true;
243 btnSort.PerformClick();
244 }
245 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
246 }
247
248
249 void CreatControlsFromType(Type data_type)
250 {
251 #if USE_DYNAMIC_SORTING_FILTERING_CONTROLS
252 _CreatControlsFromType(data_type);
253 #else
254 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 #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 gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
275 }
276 [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
277 void CreateSourceDefinitionControls(Type data_type)
278 {
279 // no controls needed
280 gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
281 }
282 [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
283 void CreateChannelDefinitionControls(Type data_type)
284 {
285 gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
286 }
287 [Conditional("USE_DYNAMIC_SORTING_FILTERING_CONTROLS")]
288 void CreateProgramDefinitionControls(Type data_type)
289 {
290 gLog.Verbose.Warn.WriteLine("No controls have been defined for: {0}", data_type.Name);
291 }
292 #endregion
293
294 #region Program Entry Display
295 //private int CurrentRowIndex = -1;
296 void DisplaySelectedProgramEntry(DataGridViewRow row)
297 {
298 //int RowIndex = row.Index; // this is the index in the list
299
300 //if (CurrentRowIndex == -1) { AlreadyDisplayedProgramEntryForIndex = false; CurrentRowIndex = RowIndex; }
301 //if (CurrentRowIndex == RowIndex && AlreadyDisplayedProgramEntryForIndex) {
302 //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 {
322 gLog.Warn.WriteLine("Unable to display program entry for rowindex: {0}", row.Index);
323
324 return;
325 }
326 try
327 {
328 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 }
334 catch (Exception ex)
335 {
336 gLog.Error.WriteLine(ex.ToString());
337 }
338 //}
339 }
340 #endregion
341 #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
361 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 DisplaySelectedProgramEntry(row);
368 }
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 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 ToggleDescedning();
397 }
398 #endregion
399 #region Filtering events
400 private void FilterByColumn(RadioButton button)
401 {
402 Type data_type;
403 string column = (button.Tag as string);
404 object data = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
405 XMLTV.CreateFilterFromDataSource(ref data, column, txtFilterText.Text);
406 dataGrid.DataSource = data;
407 dataGrid_SelectionChanged(dataGrid, new EventArgs());
408 }
409 private RadioButton GetSelectedFilteringControl()
410 {
411 bool control_selected = false;
412 RadioButton button = new RadioButton();
413 foreach (var c in grpFilter_flow.Controls)
414 {
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 gLog.Verbose.Warn.WriteLine("Unkown filter value used. Was not: channelname, title, subtitle, description, start, or stop.");
422 //throw new InvalidOperationException("No filtering control is currently selected.");
423 return button;
424 }
425 else { return button; }
426 }
427 private void UpdateFilterText(RadioButton radio, DataGridView dgv)
428 {
429 if (radio.Checked)
430 {
431 int cellindex = GetColumnIndexForRadioButton(radio);
432 string t = radio.Tag as string;
433 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 private void btnFilter_Click(object sender, EventArgs e)
449 {
450 RadioButton button = GetSelectedFilteringControl();
451 FilterByColumn(button);
452 }
453 private void radio_filter_channelnumber_CheckedChanged(object sender, EventArgs e)
454 {
455 RadioButton radio = (sender as RadioButton);
456 UpdateFilterText(radio, dataGrid);
457 }
458 private void radio_filter_title_CheckedChanged(object sender, EventArgs e)
459 {
460 RadioButton radio = (sender as RadioButton);
461 UpdateFilterText(radio, dataGrid);
462 }
463 private void radio_filter_subtitle_CheckedChanged(object sender, EventArgs e)
464 {
465 RadioButton radio = (sender as RadioButton);
466 UpdateFilterText(radio, dataGrid);
467 }
468 private void radio_filter_description_CheckedChanged(object sender, EventArgs e)
469 {
470 RadioButton radio = (sender as RadioButton);
471 UpdateFilterText(radio, dataGrid);
472 }
473 private void radio_filter_channelname_CheckedChanged(object sender, EventArgs e)
474 {
475 RadioButton radio = (sender as RadioButton);
476 UpdateFilterText(radio, dataGrid);
477 }
478 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 }
488 private void radio_filter_rating_CheckedChanged(object sender, EventArgs e)
489 {
490 RadioButton radio = (sender as RadioButton);
491 UpdateFilterText(radio, dataGrid);
492 }
493 #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 dataGrid_SelectionChanged(dataGrid, new EventArgs());
505 }
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
531 private void dataGrid_Click(object sender, EventArgs e)
532 {
533 dataGrid_SelectionChanged(dataGrid, new EventArgs());
534 }
535
536
537 }
538 }
539

  ViewVC Help
Powered by ViewVC 1.1.22