/[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 116 - (hide annotations) (download)
Sun Mar 10 16:09:35 2013 UTC (7 years, 4 months ago) by william
File size: 14146 byte(s)
+ also serialize to file on a seperate thread ... although it does not block the main form as badly as deserializtion does

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

  ViewVC Help
Powered by ViewVC 1.1.22