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

  ViewVC Help
Powered by ViewVC 1.1.22