/[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 86 - (hide annotations) (download)
Sat Mar 9 13:51:58 2013 UTC (6 years, 8 months ago) by william
File size: 12297 byte(s)
+ use a Properties dictionary to hold the Program metadata

1 william 71 //#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     using System;
4 william 2 using System.Collections.Generic;
5     using System.ComponentModel;
6     using System.Data;
7 william 29 using System.Linq;
8 william 2 using System.Drawing;
9     using System.Text;
10     using System.Windows.Forms;
11 william 11 using libxmltv.Core;
12 william 13 using Enterprise.Logging;
13 william 29 using libxmltv.Interfaces;
14 william 31 using System.IO;
15     using System.Runtime.Serialization.Formatters.Binary;
16 william 40 using System.Threading;
17 william 79 using System.Diagnostics;
18 william 2
19     namespace xmltv_parser
20     {
21 william 50
22 william 13 public partial class main : Form
23 william 2 {
24 william 13 public main()
25 william 2 {
26     InitializeComponent();
27 william 62 ListViewSorter Sorter = new ListViewSorter();
28     lstPrograms.ListViewItemSorter = Sorter;
29 william 13 string log_path = Application.StartupPath;
30     string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
31 william 59 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
32 william 67 #if DEBUG
33     LogLevel gLevel = gLog.LogLevel;
34 william 69 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
35     gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
36     #else
37     gLevel |= LogLevel.kLogLevel_VerboseDebug;
38     #endif
39     gLevel |= LogLevel.kLogLevel_Debug;
40 william 67 gLog.SetLogLevel(gLevel);
41     #else
42 william 68 LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
43     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
44 william 69 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
45     gLevel &= ~LogLevel.kLogLevel_Debug;
46     #else
47     gLevel |= LogLevel.kLogLevel_Debug;
48     #endif
49 william 70 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
50 william 67 gLog.SetLogLevel(gLevel);
51     #endif
52    
53    
54 william 59 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
55 william 2 }
56 william 11
57 william 59 private void ReportProgress(int progress)
58     {
59 william 84 if (this.InvokeRequired)
60 william 59 {
61 william 84 try
62 william 59 {
63 william 84 this.Invoke((Action)(delegate { ReportProgress(progress); }));
64 william 59 }
65 william 84 catch { }
66     return;
67 william 59 }
68 william 84 if (this.IsDisposed) { return; }
69     progress_status.Value = progress;
70 william 59 }
71     private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
72    
73 william 40 StringBuilder log_flusher = new StringBuilder();
74 william 61
75 william 41 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
76 william 40 {
77 william 54 OnLogFlush(e.Buffer);
78 william 40 }
79 william 47 //bool txtLog_EnterMouse = false;
80 william 40
81 william 47 private void txtLog_MouseLeave(object sender, EventArgs e)
82     {
83     //txtLog_EnterMouse = false;
84     }
85     private void txtLog_MouseEnter(object sender, EventArgs e)
86     {
87     //txtLog_EnterMouse = true;
88     }
89 william 40 void OnLogFlush(string logmessage)
90     {
91 william 47 if (this.IsDisposed) { return; }
92 william 54 UpdateStatus(logmessage);
93     UpdateLogOutput(logmessage);
94     Application.DoEvents();
95     }
96    
97     void UpdateStatus(string logmessage)
98 william 61 {
99 william 72 if (this.InvokeRequired)
100     {
101     try
102     {
103     this.Invoke((Action)(delegate { UpdateStatus(logmessage); }));
104     }
105     catch { }
106     return;
107     }
108 william 42 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
109 william 54 }
110     void UpdateLogOutput(string logmessage)
111     {
112 william 72 if (this.InvokeRequired)
113 william 54 {
114 william 56 try
115     {
116 william 72 this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
117 william 56 }
118     catch { }
119 william 54 return;
120     }
121 william 47 txtLog.AppendText(logmessage);
122     txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
123     txtLog.ScrollToCaret(); //Now scroll it automatically
124 william 40 }
125    
126 william 29 //List<IXMLTVChannel> Channels;
127     List<IXMLTVProgram> Programs;
128    
129 william 16 private void main_Load(object sender, EventArgs e)
130 william 11 {
131    
132     }
133 william 16 private void main_Shown(object sender, EventArgs e)
134 william 30 {
135 william 52
136 william 29 }
137    
138    
139     void LoadXMLTVShcedule(string schedule_xml)
140 william 11 {
141 william 54 //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
142 william 72 XMLTV.CreateInstance(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
143     //XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
144 william 11 }
145 william 29
146 william 54 void XMLTV_OnInstanceCreated(object sender, EventArgs e)
147 william 46 {
148 william 59 ReportProgress(0);
149 william 54 var instance = XMLTV.GetInstance();
150     if (instance != null)
151 william 46 {
152 william 73 var program_count = instance.Programs.Count;
153 william 86 var program_list = instance.Programs.ToList().OrderBy(s => s.GetProperty(XMLTVConstants.Programs.ProgramStart).ToString());
154 william 54 //Programs = program_list(0, program_count).ToList();
155     Programs = new List<IXMLTVProgram>(program_list.ToArray());
156 william 46 }
157 william 54 CreateControls();
158 william 46 }
159    
160 william 54
161     //void xmltv_cancelevent(object sender, CancelEventArgs e)
162     //{
163     // if (form_closing)
164     // {
165     // e.Cancel = true;
166     // }
167     //}
168    
169 william 29 void CreateControls()
170     {
171 william 54 if (this.InvokeRequired)
172     {
173     this.Invoke((Action)(delegate { CreateControls(); }));
174     return;
175     }
176 william 60 List<ListViewItem> items = new List<ListViewItem>();
177 william 29 foreach (var program in Programs)
178     {
179 william 86 IXMLTVChannel Channel = null;
180     var instance = XMLTV.GetInstance();
181     var id = program.GetProperty(XMLTVConstants.Programs.ProgramChannelId).ToString();
182     Channel = instance.Channels.Find(m => m.Id == id);
183     ListViewItem li = new ListViewItem(string.Format("{0} {1}", Channel.Number, Channel.CallSign));
184 william 60 li.Tag = program;
185 william 86 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.GetProperty(XMLTVConstants.Programs.ProgramTitle).ToString()));
186     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.GetProperty(XMLTVConstants.Programs.ProgramSubTitle).ToString()));
187     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.GetProperty(XMLTVConstants.Programs.ProgramDescription).ToString()));
188     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, ((DateTime)program.GetProperty(XMLTVConstants.Programs.ProgramStart)).ToString("yyyy/MM/dd hh:mm tt")));
189     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, ((DateTime)program.GetProperty(XMLTVConstants.Programs.ProgramStop)).ToString("yyyy/MM/dd hh:mm tt")));
190 william 60 //lstPrograms.Items.Add(li);
191     items.Add(li);
192 william 29 }
193 william 60
194     lstPrograms.Items.AddRange(items.ToArray());
195 william 29 }
196 william 30
197     private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
198 william 52 {
199     try
200     {
201 william 59 lstPrograms.Items.Clear();
202 william 52 //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
203     var result = xmltv_file_chooser.ShowDialog();
204     if (result != DialogResult.OK) return;
205     ClearLocalLog();
206     LoadXMLTVShcedule(xmltv_file_chooser.FileName);
207 william 54 //CreateControls();
208 william 52 }
209     catch (Exception ex)
210     {
211 william 55 gLog.Error.WriteLine(ex.ToString());
212 william 52 }
213 william 30 }
214    
215     private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
216     {
217 william 52 try
218 william 31 {
219 william 52 var result = xmltv_program_data_loader.ShowDialog();
220     if (result != DialogResult.OK) return;
221     string filename = xmltv_program_data_loader.FileName;
222     IXMLTVRuntimeInstance xmltv = null;
223     bool status = false;
224     xmltv = XMLTV.DeSerialize(filename, out status);
225 william 56 XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
226 william 52 if (!status)
227     {
228     MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
229     return;
230     }
231     MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
232 william 31 }
233 william 52 catch (Exception ex)
234     {
235 william 55 gLog.Error.WriteLine(ex.ToString());
236 william 52 }
237 william 30 }
238    
239     private void mnuItemSaveData_Click(object sender, EventArgs e)
240     {
241 william 52 try
242 william 31 {
243 william 52 var result = xmltv_program_data_saver.ShowDialog();
244     if (result != DialogResult.OK) return;
245     string filename = xmltv_program_data_saver.FileName;
246     if (!XMLTV.Serialize(filename))
247     {
248     MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
249     return;
250     }
251     MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
252 william 31 }
253 william 52 catch (Exception ex)
254     {
255 william 55 gLog.Error.WriteLine(ex.ToString());
256 william 52 }
257 william 30 }
258 william 44
259     private void main_FormClosing(object sender, FormClosingEventArgs e)
260     {
261 william 54 try
262     {
263     XMLTV.DestroyInstance();
264     }
265     catch { }
266 william 44 }
267 william 47
268     private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
269     {
270     ClearLocalLog();
271     }
272     private void ClearLocalLog()
273     {
274     var log_top_entry = txtLog.Lines.FirstOrDefault();
275     txtLog.Clear();
276     if (!string.IsNullOrEmpty(log_top_entry))
277     {
278     txtLog.AppendText(log_top_entry);
279     }
280     }
281    
282 william 56 private void mnuItemExit_Click(object sender, EventArgs e)
283     {
284     this.Close();
285     }
286    
287 william 60 private void lstPrograms_SelectedIndexChanged(object sender, EventArgs e)
288     {
289     IXMLTVProgram program = null;
290     var item = lstPrograms.SelectedItems[0];
291     int selected_index = lstPrograms.Items.IndexOf(item);
292     program = (item.Tag as IXMLTVProgram);
293     gLog.Debug.WriteLine("Selected ListViewItem at index: {0}", selected_index);
294     if (program == null) { return; }
295     txtStatus.Text = program.ToString();
296     //gLog.Debug.WriteLine(program.ToString());
297 william 61
298 william 60 }
299    
300 william 61 private void lstPrograms_ColumnClick(object sender, ColumnClickEventArgs e)
301     {
302 william 62 if (!(lstPrograms.ListViewItemSorter is ListViewSorter)) return;
303     ListViewSorter Sorter = (ListViewSorter)lstPrograms.ListViewItemSorter;
304     Sorter.LastColumn = Sorter.CurrentColumn;
305     Sorter.CurrentColumn = e.Column;
306     if (Sorter.LastColumn == e.Column)
307     {
308     if (lstPrograms.Sorting == SortOrder.Ascending) { lstPrograms.Sorting = SortOrder.Descending; }
309     else { lstPrograms.Sorting = SortOrder.Ascending; }
310     }
311     else { lstPrograms.Sorting = SortOrder.Descending; }
312 william 61 lstPrograms.Sort();
313     }
314 william 2 }
315     }
316 william 29

  ViewVC Help
Powered by ViewVC 1.1.22