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, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush)); |
34 |
} |
35 |
|
36 |
StringBuilder log_flusher = new StringBuilder(); |
37 |
|
38 |
void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) |
39 |
{ |
40 |
OnLogFlush(e.Buffer); |
41 |
} |
42 |
//bool txtLog_EnterMouse = false; |
43 |
|
44 |
private void txtLog_MouseLeave(object sender, EventArgs e) |
45 |
{ |
46 |
//txtLog_EnterMouse = false; |
47 |
} |
48 |
private void txtLog_MouseEnter(object sender, EventArgs e) |
49 |
{ |
50 |
//txtLog_EnterMouse = true; |
51 |
} |
52 |
void OnLogFlush(string logmessage) |
53 |
{ |
54 |
if (this.IsDisposed) { return; } |
55 |
UpdateStatus(logmessage); |
56 |
UpdateLogOutput(logmessage); |
57 |
Application.DoEvents(); |
58 |
} |
59 |
|
60 |
void UpdateStatus(string logmessage) |
61 |
{ |
62 |
txtStatus.Text = logmessage.Replace(System.Environment.NewLine, ""); |
63 |
} |
64 |
void UpdateLogOutput(string logmessage) |
65 |
{ |
66 |
if (txtLog.InvokeRequired) |
67 |
{ |
68 |
try |
69 |
{ |
70 |
txtLog.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); |
71 |
} |
72 |
catch { } |
73 |
return; |
74 |
} |
75 |
txtLog.AppendText(logmessage); |
76 |
txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end |
77 |
txtLog.ScrollToCaret(); //Now scroll it automatically |
78 |
} |
79 |
|
80 |
//List<IXMLTVChannel> Channels; |
81 |
List<IXMLTVProgram> Programs; |
82 |
|
83 |
private void main_Load(object sender, EventArgs e) |
84 |
{ |
85 |
|
86 |
} |
87 |
private void main_Shown(object sender, EventArgs e) |
88 |
{ |
89 |
|
90 |
} |
91 |
|
92 |
|
93 |
void LoadXMLTVShcedule(string schedule_xml) |
94 |
{ |
95 |
//XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent)); |
96 |
XMLTV.CreateInstance(schedule_xml); |
97 |
XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated); |
98 |
} |
99 |
|
100 |
void XMLTV_OnInstanceCreated(object sender, EventArgs e) |
101 |
{ |
102 |
var instance = XMLTV.GetInstance(); |
103 |
if (instance != null) |
104 |
{ |
105 |
var program_count = instance.Programs.Values.Count; |
106 |
var program_list = instance.Programs.Values.ToList().OrderBy(s => s.Start); |
107 |
//Programs = program_list(0, program_count).ToList(); |
108 |
Programs = new List<IXMLTVProgram>(program_list.ToArray()); |
109 |
} |
110 |
CreateControls(); |
111 |
} |
112 |
|
113 |
|
114 |
//void xmltv_cancelevent(object sender, CancelEventArgs e) |
115 |
//{ |
116 |
// if (form_closing) |
117 |
// { |
118 |
// e.Cancel = true; |
119 |
// } |
120 |
//} |
121 |
|
122 |
void CreateControls() |
123 |
{ |
124 |
if (this.InvokeRequired) |
125 |
{ |
126 |
this.Invoke((Action)(delegate { CreateControls(); })); |
127 |
return; |
128 |
} |
129 |
foreach (var program in Programs) |
130 |
{ |
131 |
ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign)); |
132 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Title)); |
133 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.SubTitle)); |
134 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Description)); |
135 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Start.ToString("yyyy/MM/dd hh:mm tt"))); |
136 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, program.Stop.ToString("yyyy/MM/dd hh:mm tt"))); |
137 |
lstPrograms.Items.Add(li); |
138 |
} |
139 |
} |
140 |
|
141 |
private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e) |
142 |
{ |
143 |
try |
144 |
{ |
145 |
//LoadXMLTVShcedule("20130307_continuum_schedule.xml"); |
146 |
var result = xmltv_file_chooser.ShowDialog(); |
147 |
if (result != DialogResult.OK) return; |
148 |
ClearLocalLog(); |
149 |
LoadXMLTVShcedule(xmltv_file_chooser.FileName); |
150 |
//CreateControls(); |
151 |
} |
152 |
catch (Exception ex) |
153 |
{ |
154 |
gLog.Error.WriteLine(ex.ToString()); |
155 |
} |
156 |
} |
157 |
|
158 |
private void mnuItemOpenSavedData_Click(object sender, EventArgs e) |
159 |
{ |
160 |
try |
161 |
{ |
162 |
var result = xmltv_program_data_loader.ShowDialog(); |
163 |
if (result != DialogResult.OK) return; |
164 |
string filename = xmltv_program_data_loader.FileName; |
165 |
IXMLTVRuntimeInstance xmltv = null; |
166 |
bool status = false; |
167 |
xmltv = XMLTV.DeSerialize(filename, out status); |
168 |
XMLTV.CreateFromInstance(xmltv, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated)); |
169 |
if (!status) |
170 |
{ |
171 |
MessageBox.Show("Failed to load data - check log", "Failed to load data", MessageBoxButtons.OK, MessageBoxIcon.Error); |
172 |
return; |
173 |
} |
174 |
MessageBox.Show("Successfully loaded data", "Successfully loaded data", MessageBoxButtons.OK, MessageBoxIcon.Information); |
175 |
} |
176 |
catch (Exception ex) |
177 |
{ |
178 |
gLog.Error.WriteLine(ex.ToString()); |
179 |
} |
180 |
} |
181 |
|
182 |
private void mnuItemSaveData_Click(object sender, EventArgs e) |
183 |
{ |
184 |
try |
185 |
{ |
186 |
var result = xmltv_program_data_saver.ShowDialog(); |
187 |
if (result != DialogResult.OK) return; |
188 |
string filename = xmltv_program_data_saver.FileName; |
189 |
if (!XMLTV.Serialize(filename)) |
190 |
{ |
191 |
MessageBox.Show("Failed to save data - check log", "Failed to save data", MessageBoxButtons.OK, MessageBoxIcon.Error); |
192 |
return; |
193 |
} |
194 |
MessageBox.Show("Successfully saved data", "Successfully saved data", MessageBoxButtons.OK, MessageBoxIcon.Information); |
195 |
} |
196 |
catch (Exception ex) |
197 |
{ |
198 |
gLog.Error.WriteLine(ex.ToString()); |
199 |
} |
200 |
} |
201 |
|
202 |
private void main_FormClosing(object sender, FormClosingEventArgs e) |
203 |
{ |
204 |
try |
205 |
{ |
206 |
XMLTV.DestroyInstance(); |
207 |
} |
208 |
catch { } |
209 |
} |
210 |
|
211 |
private void mnuItemClearLocalLog_Click(object sender, EventArgs e) |
212 |
{ |
213 |
ClearLocalLog(); |
214 |
} |
215 |
private void ClearLocalLog() |
216 |
{ |
217 |
var log_top_entry = txtLog.Lines.FirstOrDefault(); |
218 |
txtLog.Clear(); |
219 |
if (!string.IsNullOrEmpty(log_top_entry)) |
220 |
{ |
221 |
txtLog.AppendText(log_top_entry); |
222 |
} |
223 |
} |
224 |
|
225 |
private void mnuItemExit_Click(object sender, EventArgs e) |
226 |
{ |
227 |
this.Close(); |
228 |
} |
229 |
|
230 |
} |
231 |
} |
232 |
|