/[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 146 - (show annotations) (download)
Thu Mar 14 14:20:27 2013 UTC (7 years, 4 months ago) by william
File size: 20066 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
25 {
26 public main()
27 {
28 InitializeComponent();
29 ListViewSorter Sorter = new ListViewSorter();
30 //lstPrograms.ListViewItemSorter = Sorter;
31 string log_path = Application.StartupPath;
32 string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
33 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
34 #if DEBUG
35 LogLevel gLevel = gLog.LogLevel;
36 #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 gLog.SetLogLevel(gLevel);
43 #else
44 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 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
47 gLevel &= ~LogLevel.kLogLevel_Debug;
48 #else
49 gLevel |= LogLevel.kLogLevel_Debug;
50 #endif
51 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
52 gLog.SetLogLevel(gLevel);
53 #endif
54
55
56 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
57 }
58 #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
99 private void ReportProgress(int progress)
100 {
101 if (this.InvokeRequired)
102 {
103 try { this.Invoke((Action)(delegate { ReportProgress(progress); })); }
104 catch { }
105 return;
106 }
107 if (progress_status != null && !this.IsDisposed)
108 progress_status.Value = progress;
109 }
110
111 #region gLog Events
112 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
113 #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
119
120 #region Logging Events
121 StringBuilder log_flusher = new StringBuilder();
122 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
123 void OnLogFlush(string logmessage)
124 {
125 if (this.IsDisposed) { return; }
126 UpdateStatus(logmessage);
127 UpdateLogOutput(logmessage);
128 Application.DoEvents();
129 }
130 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 void UpdateStatus(string logmessage)
137 {
138 try
139 {
140 if (this.InvokeRequired)
141 {
142 try { this.Invoke((Action)(delegate { UpdateStatus(logmessage); })); }
143 catch { }
144 return;
145 }
146 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
147 }
148 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
149 }
150 void UpdateLogOutput(string logmessage)
151 {
152 try
153 {
154 if (this.InvokeRequired)
155 {
156 try { this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); }
157 catch { }
158 return;
159 }
160 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 }
164 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
165 }
166 #endregion
167
168 #region Binary Data Read/Write support
169 private void DeserializeDataFromFile(object filename)
170 {
171 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
172 XMLTV.Load(filename.ToString(), new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
173 }
174 private void DeserializeDataToFile(object filename)
175 {
176 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
177 XMLTV.Save(filename.ToString());
178 }
179 #endregion
180
181
182 #region CreateControls
183 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 //for (int i = 0; i < dataGrid.RowCount; i++)
192 //{
193 // dataGrid.AutoResizeRow(i, DataGridViewAutoSizeRowMode.AllCellsExceptHeader);
194 //}
195 }
196 void CreateControls()
197 {
198 try
199 {
200 if (this.InvokeRequired)
201 {
202 try { this.Invoke((Action)(delegate { CreateControls(); })); }
203 catch { }
204 return;
205 }
206 Type data_type;
207 object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
208 CreatControlsFromType(data_type);
209 dataGrid.DataSource = datasource;
210 AutoResizeDataGridView();
211 flow_datagrid_sort.Enabled = true;
212 btnSort.PerformClick();
213 }
214 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
215 }
216
217
218 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 #endregion
262
263 #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 }
299 catch (Exception ex)
300 {
301 gLog.Error.WriteLine(ex.ToString());
302 }
303 }
304 }
305 #endregion
306 #region Datasource support / events
307 private void dataGrid_SelectionChanged(object sender, EventArgs e)
308 {
309 try
310 {
311 DataGridView dgv = (DataGridView)sender;
312 //var row = dgv.SelectedRows[0]; // the row that was selected
313 //var cell = dgv.SelectedCells[0];
314 DataGridViewRow row = null;
315 string data = string.Empty;
316 if (dgv.SelectedCells.Count > 0)
317 {
318 var cell = dgv.SelectedCells[0];
319 row = dgv.Rows[cell.RowIndex];
320 }
321 if (dgv.SelectedRows.Count > 0)
322 {
323 row = dgv.SelectedRows[0];
324 }
325
326 if (row != null)
327 {
328 RadioButton button = GetSelectedFilteringControl();
329 int index = GetColumnIndexForRadioButton(button);
330 data = row.Cells[index].Value.ToString();
331 txtFilterText.Text = data;
332 DisplaySelectedProgramEntry(row);
333 }
334 }
335 catch (Exception ex)
336 {
337 gLog.Verbose.Error.WriteLine(ex.ToString());
338 }
339 }
340 private int GetColumnIndexForRadioButton(RadioButton radio) { return GetColumnIndex(radio.Tag as string); }
341 private int GetColumnIndex(string columnname)
342 {
343 int index =0;
344 Type t = typeof(IProgramDefinition);
345
346 var props = t.GetProperties();
347 foreach (var prop in props)
348 {
349 if (prop.Name.ToLower() == columnname.ToLower()) { break; }
350 index++;
351 }
352 return index;
353 }
354 #endregion
355 #region Reset event
356 private void btnResetSort_Click(object sender, EventArgs e)
357 {
358 Type data_type;
359 object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
360 dataGrid.DataSource = datasource;
361 ToggleDescedning();
362 }
363 #endregion
364 #region Filtering events
365 private void FilterByColumn(RadioButton button)
366 {
367 Type data_type;
368 string column = (button.Tag as string);
369 object data = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out data_type);
370 XMLTV.CreateFilterFromDataSource(ref data, column, txtFilterText.Text);
371 dataGrid.DataSource = data;
372 }
373 private RadioButton GetSelectedFilteringControl()
374 {
375 bool control_selected = false;
376 RadioButton button = new RadioButton();
377 foreach (var c in grpFilter_flow.Controls)
378 {
379 button = (c as RadioButton);
380 if (button != null) { if (button.Checked) { control_selected = true; break; } }
381 else { continue; }
382 }
383 if (!control_selected)
384 {
385 gLog.Warn.WriteLine("Unkown filter value used. Was not: channelname, title, subtitle, description, start, or stop.");
386 //throw new InvalidOperationException("No filtering control is currently selected.");
387 return button;
388 }
389 else { return button; }
390 }
391 private void UpdateFilterText(RadioButton radio, DataGridView dgv)
392 {
393 if (radio.Checked)
394 {
395 int cellindex = GetColumnIndexForRadioButton(radio);
396 string t = radio.Tag as string;
397 DataGridViewRow row = null;
398 string data = string.Empty;
399 if (dgv.SelectedCells.Count > 0)
400 {
401 var cell = dgv.SelectedCells[0];
402 row = dgv.Rows[cell.RowIndex];
403 }
404 if (dgv.SelectedRows.Count > 0)
405 {
406 row = dgv.SelectedRows[0];
407 }
408 data = row.Cells[cellindex].Value.ToString();
409 txtFilterText.Text = data;
410 }
411 }
412 private void btnFilter_Click(object sender, EventArgs e)
413 {
414 RadioButton button = GetSelectedFilteringControl();
415 FilterByColumn(button);
416 }
417 private void radio_filter_channelnumber_CheckedChanged(object sender, EventArgs e)
418 {
419 RadioButton radio = (sender as RadioButton);
420 UpdateFilterText(radio, dataGrid);
421 }
422 private void radio_filter_title_CheckedChanged(object sender, EventArgs e)
423 {
424 RadioButton radio = (sender as RadioButton);
425 UpdateFilterText(radio, dataGrid);
426 }
427 private void radio_filter_subtitle_CheckedChanged(object sender, EventArgs e)
428 {
429 RadioButton radio = (sender as RadioButton);
430 UpdateFilterText(radio, dataGrid);
431 }
432 private void radio_filter_description_CheckedChanged(object sender, EventArgs e)
433 {
434 RadioButton radio = (sender as RadioButton);
435 UpdateFilterText(radio, dataGrid);
436 }
437 private void radio_filter_channelname_CheckedChanged(object sender, EventArgs e)
438 {
439 RadioButton radio = (sender as RadioButton);
440 UpdateFilterText(radio, dataGrid);
441 }
442 private void radio_filter_start_CheckedChanged(object sender, EventArgs e)
443 {
444 RadioButton radio = (sender as RadioButton);
445 UpdateFilterText(radio, dataGrid);
446 }
447 private void radio_filter_stop_CheckedChanged(object sender, EventArgs e)
448 {
449 RadioButton radio = (sender as RadioButton);
450 UpdateFilterText(radio, dataGrid);
451 }
452 #endregion
453 #region Sorting events
454 private bool Descending = false;
455 private void ToggleDescedning() { Descending = Descending ? false : true; }
456 private void SortByColumn(RadioButton button)
457 {
458 string column = (button.Tag as string);
459 object data = dataGrid.DataSource;
460 XMLTV.CreateSorterFromDataSource(ref data, Descending, column);
461 dataGrid.DataSource = data;
462 ToggleDescedning();
463 }
464 private RadioButton GetSelectedSortControl()
465 {
466 bool control_selected = false;
467 RadioButton button = new RadioButton();
468 foreach (var c in grpSort_flow.Controls)
469 {
470 button = (c as RadioButton);
471 if (button != null) { if (button.Checked) { control_selected = true; break; } }
472 else { continue; }
473 }
474 if (!control_selected)
475 {
476 gLog.Warn.WriteLine("Unkown sort value used. Was not: channelname, title, subtitle, description, stop, or start.");
477 //throw new InvalidOperationException("No sorting control is currently selected.");
478 return button;
479 }
480 else { return button; }
481 }
482 private void btnSort_Click(object sender, EventArgs e)
483 {
484 RadioButton button = GetSelectedSortControl();
485 SortByColumn(button);
486 }
487 #endregion
488 }
489 }
490

  ViewVC Help
Powered by ViewVC 1.1.22