/[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 101 - (hide annotations) (download)
Sun Mar 10 10:36:47 2013 UTC (7 years, 4 months ago) by william
File size: 13517 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22