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

  ViewVC Help
Powered by ViewVC 1.1.22