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