/[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 241 - (show annotations) (download)
Sun Mar 17 10:40:09 2013 UTC (7 years, 3 months ago) by william
File size: 21948 byte(s)
+ Truncate Log file with it grows over 250kb in size

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 XMLT 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}", RowIndex);
323
324 return;
325 }
326 try
327 {
328 IProgramDefinition program = list[RowIndex];
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 }
408 private RadioButton GetSelectedFilteringControl()
409 {
410 bool control_selected = false;
411 RadioButton button = new RadioButton();
412 foreach (var c in grpFilter_flow.Controls)
413 {
414 button = (c as RadioButton);
415 if (button != null) { if (button.Checked) { control_selected = true; break; } }
416 else { continue; }
417 }
418 if (!control_selected)
419 {
420 gLog.Verbose.Warn.WriteLine("Unkown filter value used. Was not: channelname, title, subtitle, description, start, or stop.");
421 //throw new InvalidOperationException("No filtering control is currently selected.");
422 return button;
423 }
424 else { return button; }
425 }
426 private void UpdateFilterText(RadioButton radio, DataGridView dgv)
427 {
428 if (radio.Checked)
429 {
430 int cellindex = GetColumnIndexForRadioButton(radio);
431 string t = radio.Tag as string;
432 DataGridViewRow row = null;
433 string data = string.Empty;
434 if (dgv.SelectedCells.Count > 0)
435 {
436 var cell = dgv.SelectedCells[0];
437 row = dgv.Rows[cell.RowIndex];
438 }
439 if (dgv.SelectedRows.Count > 0)
440 {
441 row = dgv.SelectedRows[0];
442 }
443 data = row.Cells[cellindex].Value.ToString();
444 txtFilterText.Text = data;
445 }
446 }
447 private void btnFilter_Click(object sender, EventArgs e)
448 {
449 RadioButton button = GetSelectedFilteringControl();
450 FilterByColumn(button);
451 }
452 private void radio_filter_channelnumber_CheckedChanged(object sender, EventArgs e)
453 {
454 RadioButton radio = (sender as RadioButton);
455 UpdateFilterText(radio, dataGrid);
456 }
457 private void radio_filter_title_CheckedChanged(object sender, EventArgs e)
458 {
459 RadioButton radio = (sender as RadioButton);
460 UpdateFilterText(radio, dataGrid);
461 }
462 private void radio_filter_subtitle_CheckedChanged(object sender, EventArgs e)
463 {
464 RadioButton radio = (sender as RadioButton);
465 UpdateFilterText(radio, dataGrid);
466 }
467 private void radio_filter_description_CheckedChanged(object sender, EventArgs e)
468 {
469 RadioButton radio = (sender as RadioButton);
470 UpdateFilterText(radio, dataGrid);
471 }
472 private void radio_filter_channelname_CheckedChanged(object sender, EventArgs e)
473 {
474 RadioButton radio = (sender as RadioButton);
475 UpdateFilterText(radio, dataGrid);
476 }
477 private void radio_filter_start_CheckedChanged(object sender, EventArgs e)
478 {
479 RadioButton radio = (sender as RadioButton);
480 UpdateFilterText(radio, dataGrid);
481 }
482 private void radio_filter_stop_CheckedChanged(object sender, EventArgs e)
483 {
484 RadioButton radio = (sender as RadioButton);
485 UpdateFilterText(radio, dataGrid);
486 }
487 private void radio_filter_rating_CheckedChanged(object sender, EventArgs e)
488 {
489 RadioButton radio = (sender as RadioButton);
490 UpdateFilterText(radio, dataGrid);
491 }
492 #endregion
493 #region Sorting events
494 private bool Descending = false;
495 private void ToggleDescedning() { Descending = Descending ? false : true; }
496 private void SortByColumn(RadioButton button)
497 {
498 string column = (button.Tag as string);
499 object data = dataGrid.DataSource;
500 XMLTV.CreateSorterFromDataSource(ref data, Descending, column);
501 dataGrid.DataSource = data;
502 ToggleDescedning();
503 }
504 private RadioButton GetSelectedSortControl()
505 {
506 bool control_selected = false;
507 RadioButton button = new RadioButton();
508 foreach (var c in grpSort_flow.Controls)
509 {
510 button = (c as RadioButton);
511 if (button != null) { if (button.Checked) { control_selected = true; break; } }
512 else { continue; }
513 }
514 if (!control_selected)
515 {
516 gLog.Warn.WriteLine("Unkown sort value used. Was not: channelname, title, subtitle, description, stop, or start.");
517 //throw new InvalidOperationException("No sorting control is currently selected.");
518 return button;
519 }
520 else { return button; }
521 }
522 private void btnSort_Click(object sender, EventArgs e)
523 {
524 RadioButton button = GetSelectedSortControl();
525 SortByColumn(button);
526 }
527 #endregion
528
529
530 }
531 }
532

  ViewVC Help
Powered by ViewVC 1.1.22