/[xmltv_parser]/trunk/xmltv_parser/main.cs
ViewVC logotype

Annotation of /trunk/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations) (download)
Fri Mar 8 14:24:17 2013 UTC (6 years, 6 months ago) by william
File size: 10499 byte(s)

1 william 2 using System;
2     using System.Collections.Generic;
3     using System.ComponentModel;
4     using System.Data;
5 william 29 using System.Linq;
6 william 2 using System.Drawing;
7     using System.Text;
8     using System.Windows.Forms;
9 william 11 using libxmltv.Core;
10 william 13 using Enterprise.Logging;
11 william 29 using libxmltv.Interfaces;
12 william 31 using System.IO;
13     using System.Runtime.Serialization.Formatters.Binary;
14 william 40 using System.Threading;
15 william 2
16     namespace xmltv_parser
17     {
18 william 50
19 william 13 public partial class main : Form
20 william 2 {
21 william 54 //bool form_closing = false;
22 william 50 //private IXMLTVRuntimeInstance xmltv;
23 william 42 private bool IsUnix
24     {
25     get { return System.Environment.OSVersion.Platform == PlatformID.Unix; }
26     }
27    
28 william 13 public main()
29 william 2 {
30     InitializeComponent();
31 william 62 ListViewSorter Sorter = new ListViewSorter();
32     lstPrograms.ListViewItemSorter = Sorter;
33    
34 william 13 string log_path = Application.StartupPath;
35     string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
36 william 59 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 william 2 }
40 william 11
41 william 59 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 william 40 StringBuilder log_flusher = new StringBuilder();
57 william 61
58 william 41 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
59 william 40 {
60 william 54 OnLogFlush(e.Buffer);
61 william 40 }
62 william 47 //bool txtLog_EnterMouse = false;
63 william 40
64 william 47 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 william 40 void OnLogFlush(string logmessage)
73     {
74 william 47 if (this.IsDisposed) { return; }
75 william 54 UpdateStatus(logmessage);
76     UpdateLogOutput(logmessage);
77     Application.DoEvents();
78     }
79    
80     void UpdateStatus(string logmessage)
81 william 61 {
82 william 42 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
83 william 54 }
84     void UpdateLogOutput(string logmessage)
85     {
86     if (txtLog.InvokeRequired)
87     {
88 william 56 try
89     {
90     txtLog.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
91     }
92     catch { }
93 william 54 return;
94     }
95 william 47 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 william 40 }
99    
100 william 29 //List<IXMLTVChannel> Channels;
101     List<IXMLTVProgram> Programs;
102    
103 william 16 private void main_Load(object sender, EventArgs e)
104 william 11 {
105    
106     }
107 william 16 private void main_Shown(object sender, EventArgs e)
108 william 30 {
109 william 52
110 william 29 }
111    
112    
113     void LoadXMLTVShcedule(string schedule_xml)
114 william 11 {
115 william 54 //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
116     XMLTV.CreateInstance(schedule_xml);
117 william 61 XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
118 william 11 }
119 william 29
120 william 54 void XMLTV_OnInstanceCreated(object sender, EventArgs e)
121 william 46 {
122 william 59 ReportProgress(0);
123 william 54 var instance = XMLTV.GetInstance();
124     if (instance != null)
125 william 46 {
126 william 54 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 william 46 }
131 william 54 CreateControls();
132 william 46 }
133    
134 william 54
135     //void xmltv_cancelevent(object sender, CancelEventArgs e)
136     //{
137     // if (form_closing)
138     // {
139     // e.Cancel = true;
140     // }
141     //}
142    
143 william 29 void CreateControls()
144     {
145 william 54 if (this.InvokeRequired)
146     {
147     this.Invoke((Action)(delegate { CreateControls(); }));
148     return;
149     }
150 william 60 List<ListViewItem> items = new List<ListViewItem>();
151 william 29 foreach (var program in Programs)
152     {
153     ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
154 william 60 li.Tag = program;
155 william 29 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 william 60 //lstPrograms.Items.Add(li);
161     items.Add(li);
162 william 29 }
163 william 60
164     lstPrograms.Items.AddRange(items.ToArray());
165 william 29 }
166 william 30
167     private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
168 william 52 {
169     try
170     {
171 william 59 lstPrograms.Items.Clear();
172 william 52 //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 william 54 //CreateControls();
178 william 52 }
179     catch (Exception ex)
180     {
181 william 55 gLog.Error.WriteLine(ex.ToString());
182 william 52 }
183 william 30 }
184    
185     private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
186     {
187 william 52 try
188 william 31 {
189 william 52 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 william 56 XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
196 william 52 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 william 31 }
203 william 52 catch (Exception ex)
204     {
205 william 55 gLog.Error.WriteLine(ex.ToString());
206 william 52 }
207 william 30 }
208    
209     private void mnuItemSaveData_Click(object sender, EventArgs e)
210     {
211 william 52 try
212 william 31 {
213 william 52 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 william 31 }
223 william 52 catch (Exception ex)
224     {
225 william 55 gLog.Error.WriteLine(ex.ToString());
226 william 52 }
227 william 30 }
228 william 44
229     private void main_FormClosing(object sender, FormClosingEventArgs e)
230     {
231 william 54 try
232     {
233     XMLTV.DestroyInstance();
234     }
235     catch { }
236 william 44 }
237 william 47
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 william 56 private void mnuItemExit_Click(object sender, EventArgs e)
253     {
254     this.Close();
255     }
256    
257 william 60 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 william 61
268 william 60 }
269    
270 william 61 private void lstPrograms_ColumnClick(object sender, ColumnClickEventArgs e)
271     {
272 william 62 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 william 61 lstPrograms.Sort();
283     }
284 william 2 }
285     }
286 william 29

  ViewVC Help
Powered by ViewVC 1.1.22