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

  ViewVC Help
Powered by ViewVC 1.1.22