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

  ViewVC Help
Powered by ViewVC 1.1.22