/[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 59 - (hide annotations) (download)
Fri Mar 8 13:40:32 2013 UTC (6 years, 5 months ago) by william
File size: 8922 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 13 string log_path = Application.StartupPath;
32     string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
33 william 59 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 william 2 }
37 william 11
38 william 59 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 william 40 StringBuilder log_flusher = new StringBuilder();
54 william 54
55 william 41 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
56 william 40 {
57 william 54 OnLogFlush(e.Buffer);
58 william 40 }
59 william 47 //bool txtLog_EnterMouse = false;
60 william 40
61 william 47 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 william 40 void OnLogFlush(string logmessage)
70     {
71 william 47 if (this.IsDisposed) { return; }
72 william 54 UpdateStatus(logmessage);
73     UpdateLogOutput(logmessage);
74     Application.DoEvents();
75     }
76    
77     void UpdateStatus(string logmessage)
78     {
79 william 42 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
80 william 54 }
81     void UpdateLogOutput(string logmessage)
82     {
83     if (txtLog.InvokeRequired)
84     {
85 william 56 try
86     {
87     txtLog.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
88     }
89     catch { }
90 william 54 return;
91     }
92 william 47 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 william 40 }
96    
97 william 29 //List<IXMLTVChannel> Channels;
98     List<IXMLTVProgram> Programs;
99    
100 william 16 private void main_Load(object sender, EventArgs e)
101 william 11 {
102    
103     }
104 william 16 private void main_Shown(object sender, EventArgs e)
105 william 30 {
106 william 52
107 william 29 }
108    
109    
110     void LoadXMLTVShcedule(string schedule_xml)
111 william 11 {
112 william 54 //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
113     XMLTV.CreateInstance(schedule_xml);
114     XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);
115 william 11 }
116 william 29
117 william 54 void XMLTV_OnInstanceCreated(object sender, EventArgs e)
118 william 46 {
119 william 59 ReportProgress(0);
120 william 54 var instance = XMLTV.GetInstance();
121     if (instance != null)
122 william 46 {
123 william 54 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 william 46 }
128 william 54 CreateControls();
129 william 46 }
130    
131 william 54
132     //void xmltv_cancelevent(object sender, CancelEventArgs e)
133     //{
134     // if (form_closing)
135     // {
136     // e.Cancel = true;
137     // }
138     //}
139    
140 william 29 void CreateControls()
141     {
142 william 54 if (this.InvokeRequired)
143     {
144     this.Invoke((Action)(delegate { CreateControls(); }));
145     return;
146     }
147 william 29 foreach (var program in Programs)
148     {
149     ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
150     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Title));
151     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.SubTitle));
152     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Description));
153     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Start.ToString("yyyy/MM/dd hh:mm tt")));
154     li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Stop.ToString("yyyy/MM/dd hh:mm tt")));
155     lstPrograms.Items.Add(li);
156     }
157     }
158 william 30
159     private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
160 william 52 {
161     try
162     {
163 william 59 lstPrograms.Items.Clear();
164 william 52 //LoadXMLTVShcedule("20130307_continuum_schedule.xml");
165     var result = xmltv_file_chooser.ShowDialog();
166     if (result != DialogResult.OK) return;
167     ClearLocalLog();
168     LoadXMLTVShcedule(xmltv_file_chooser.FileName);
169 william 54 //CreateControls();
170 william 52 }
171     catch (Exception ex)
172     {
173 william 55 gLog.Error.WriteLine(ex.ToString());
174 william 52 }
175 william 30 }
176    
177     private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
178     {
179 william 52 try
180 william 31 {
181 william 52 var result = xmltv_program_data_loader.ShowDialog();
182     if (result != DialogResult.OK) return;
183     string filename = xmltv_program_data_loader.FileName;
184     IXMLTVRuntimeInstance xmltv = null;
185     bool status = false;
186     xmltv = XMLTV.DeSerialize(filename, out status);
187 william 56 XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
188 william 52 if (!status)
189     {
190     MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error);
191     return;
192     }
193     MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information);
194 william 31 }
195 william 52 catch (Exception ex)
196     {
197 william 55 gLog.Error.WriteLine(ex.ToString());
198 william 52 }
199 william 30 }
200    
201     private void mnuItemSaveData_Click(object sender, EventArgs e)
202     {
203 william 52 try
204 william 31 {
205 william 52 var result = xmltv_program_data_saver.ShowDialog();
206     if (result != DialogResult.OK) return;
207     string filename = xmltv_program_data_saver.FileName;
208     if (!XMLTV.Serialize(filename))
209     {
210     MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error);
211     return;
212     }
213     MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information);
214 william 31 }
215 william 52 catch (Exception ex)
216     {
217 william 55 gLog.Error.WriteLine(ex.ToString());
218 william 52 }
219 william 30 }
220 william 44
221     private void main_FormClosing(object sender, FormClosingEventArgs e)
222     {
223 william 54 try
224     {
225     XMLTV.DestroyInstance();
226     }
227     catch { }
228 william 44 }
229 william 47
230     private void mnuItemClearLocalLog_Click(object sender, EventArgs e)
231     {
232     ClearLocalLog();
233     }
234     private void ClearLocalLog()
235     {
236     var log_top_entry = txtLog.Lines.FirstOrDefault();
237     txtLog.Clear();
238     if (!string.IsNullOrEmpty(log_top_entry))
239     {
240     txtLog.AppendText(log_top_entry);
241     }
242     }
243    
244 william 56 private void mnuItemExit_Click(object sender, EventArgs e)
245     {
246     this.Close();
247     }
248    
249 william 2 }
250     }
251 william 29

  ViewVC Help
Powered by ViewVC 1.1.22