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

  ViewVC Help
Powered by ViewVC 1.1.22