/[xmltv_parser]/branches/linux/xmltv_parser/xmltv_parser/main.cs
ViewVC logotype

Contents of /branches/linux/xmltv_parser/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 288 - (show annotations) (download)
Thu Jul 30 15:15:51 2015 UTC (4 years, 1 month ago) by william
File size: 22266 byte(s)


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

  ViewVC Help
Powered by ViewVC 1.1.22