/[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 68 - (show annotations) (download)
Sat Mar 9 01:28:31 2013 UTC (6 years, 5 months ago) by william
File size: 10837 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22