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

  ViewVC Help
Powered by ViewVC 1.1.22