/[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 84 - (show annotations) (download)
Sat Mar 9 12:45:23 2013 UTC (6 years, 9 months ago) by william
File size: 11713 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 (this.IsDisposed) { return; }
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<CancelEventArgs>(xmltv_cancelevent));
142 XMLTV.CreateInstance(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
143 //XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
144 }
145
146 void XMLTV_OnInstanceCreated(object sender, EventArgs e)
147 {
148 ReportProgress(0);
149 var instance = XMLTV.GetInstance();
150 if (instance != null)
151 {
152 var program_count = instance.Programs.Count;
153 var program_list = instance.Programs.ToList().OrderBy(s => s.Start);
154 //Programs = program_list(0, program_count).ToList();
155 Programs = new List<IXMLTVProgram>(program_list.ToArray());
156 }
157 CreateControls();
158 }
159
160
161 //void xmltv_cancelevent(object sender, CancelEventArgs e)
162 //{
163 // if (form_closing)
164 // {
165 // e.Cancel = true;
166 // }
167 //}
168
169 void CreateControls()
170 {
171 if (this.InvokeRequired)
172 {
173 this.Invoke((Action)(delegate { CreateControls(); }));
174 return;
175 }
176 List<ListViewItem> items = new List<ListViewItem>();
177 foreach (var program in Programs)
178 {
179 ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
180 li.Tag = program;
181 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Title));
182 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.SubTitle));
183 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Description));
184 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Start.ToString("yyyy/MM/dd hh:mm tt")));
185 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Stop.ToString("yyyy/MM/dd hh:mm tt")));
186 //lstPrograms.Items.Add(li);
187 items.Add(li);
188 }
189
190 lstPrograms.Items.AddRange(items.ToArray());
191 }
192
193 private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
194 {
195 try
196 {
197 lstPrograms.Items.Clear();
198 //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
199 var result = xmltv_file_chooser.ShowDialog();
200 if (result != DialogResult.OK) return;
201 ClearLocalLog();
202 LoadXMLTVShcedule(xmltv_file_chooser.FileName);
203 //CreateControls();
204 }
205 catch (Exception ex)
206 {
207 gLog.Error.WriteLine(ex.ToString());
208 }
209 }
210
211 private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
212 {
213 try
214 {
215 var result = xmltv_program_data_loader.ShowDialog();
216 if (result != DialogResult.OK) return;
217 string filename = xmltv_program_data_loader.FileName;
218 IXMLTVRuntimeInstance xmltv = null;
219 bool status = false;
220 xmltv = XMLTV.DeSerialize(filename, out status);
221 XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
222 if (!status)
223 {
224 MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
225 return;
226 }
227 MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
228 }
229 catch (Exception ex)
230 {
231 gLog.Error.WriteLine(ex.ToString());
232 }
233 }
234
235 private void mnuItemSaveData_Click(object sender, EventArgs e)
236 {
237 try
238 {
239 var result = xmltv_program_data_saver.ShowDialog();
240 if (result != DialogResult.OK) return;
241 string filename = xmltv_program_data_saver.FileName;
242 if (!XMLTV.Serialize(filename))
243 {
244 MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
245 return;
246 }
247 MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
248 }
249 catch (Exception ex)
250 {
251 gLog.Error.WriteLine(ex.ToString());
252 }
253 }
254
255 private void main_FormClosing(object sender, FormClosingEventArgs e)
256 {
257 try
258 {
259 XMLTV.DestroyInstance();
260 }
261 catch { }
262 }
263
264 private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
265 {
266 ClearLocalLog();
267 }
268 private void ClearLocalLog()
269 {
270 var log_top_entry = txtLog.Lines.FirstOrDefault();
271 txtLog.Clear();
272 if (!string.IsNullOrEmpty(log_top_entry))
273 {
274 txtLog.AppendText(log_top_entry);
275 }
276 }
277
278 private void mnuItemExit_Click(object sender, EventArgs e)
279 {
280 this.Close();
281 }
282
283 private void lstPrograms_SelectedIndexChanged(object sender, EventArgs e)
284 {
285 IXMLTVProgram program = null;
286 var item = lstPrograms.SelectedItems[0];
287 int selected_index = lstPrograms.Items.IndexOf(item);
288 program = (item.Tag as IXMLTVProgram);
289 gLog.Debug.WriteLine("Selected ListViewItem at index: {0}", selected_index);
290 if (program == null) { return; }
291 txtStatus.Text = program.ToString();
292 //gLog.Debug.WriteLine(program.ToString());
293
294 }
295
296 private void lstPrograms_ColumnClick(object sender, ColumnClickEventArgs e)
297 {
298 if (!(lstPrograms.ListViewItemSorter is ListViewSorter)) return;
299 ListViewSorter Sorter = (ListViewSorter)lstPrograms.ListViewItemSorter;
300 Sorter.LastColumn = Sorter.CurrentColumn;
301 Sorter.CurrentColumn = e.Column;
302 if (Sorter.LastColumn == e.Column)
303 {
304 if (lstPrograms.Sorting == SortOrder.Ascending) { lstPrograms.Sorting = SortOrder.Descending; }
305 else { lstPrograms.Sorting = SortOrder.Ascending; }
306 }
307 else { lstPrograms.Sorting = SortOrder.Descending; }
308 lstPrograms.Sort();
309 }
310 }
311 }
312

  ViewVC Help
Powered by ViewVC 1.1.22