/[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 128 - (show annotations) (download)
Wed Mar 13 13:45:05 2013 UTC (6 years, 10 months ago) by william
File size: 8275 byte(s)
+ current wip

1 //#define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
2 //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
3 using System;
4 using System.Collections.Generic;
5 using System.ComponentModel;
6 using System.Data;
7 using System.Linq;
8 using System.Drawing;
9 using System.Text;
10 using System.Windows.Forms;
11 using libxmltv.Core;
12 using Enterprise.Logging;
13 using libxmltv.Interfaces;
14 using System.IO;
15 using System.Runtime.Serialization.Formatters.Binary;
16 using System.Threading;
17 using System.Diagnostics;
18 using System.Xml.Linq;
19
20 namespace xmltv_parser
21 {
22
23 public partial class main : Form
24 {
25 public main()
26 {
27 InitializeComponent();
28 ListViewSorter Sorter = new ListViewSorter();
29 //lstPrograms.ListViewItemSorter = Sorter;
30 string log_path = Application.StartupPath;
31 string log_filename = string.Format("{0}.log", typeof(main).Assembly.GetName().Name);
32 gLog.CreateLog(string.Format(@"{0}\{1}", log_path, log_filename), false, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
33 #if DEBUG
34 LogLevel gLevel = gLog.LogLevel;
35 #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
36 gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
37 #else
38 gLevel |= LogLevel.kLogLevel_VerboseDebug;
39 #endif
40 gLevel |= LogLevel.kLogLevel_Debug;
41 gLog.SetLogLevel(gLevel);
42 #else
43 LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
44 // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
45 #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
46 gLevel &= ~LogLevel.kLogLevel_Debug;
47 #else
48 gLevel |= LogLevel.kLogLevel_Debug;
49 #endif
50 gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
51 gLog.SetLogLevel(gLevel);
52 #endif
53
54
55 gLog.ReportProgressEvent += new EventHandler<ReportProgressEventArgs>(gLog_ReportProgress);
56 }
57 #region main form events
58 private void mnuItemExit_Click(object sender, EventArgs e) { this.Close(); }
59 private void main_Load(object sender, EventArgs e) { }
60 private void main_Shown(object sender, EventArgs e) { }
61 private void mnuItemOpenXMLTVFile_Click(object sender, EventArgs e)
62 {
63 try
64 {
65 var result = xmltv_file_chooser.ShowDialog();
66 if (result != DialogResult.OK) return;
67 ClearLocalLog();
68 LoadXMLTVShcedule(xmltv_file_chooser.FileName);
69 }
70 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
71 }
72 private void mnuItemOpenSavedData_Click(object sender, EventArgs e)
73 {
74 try
75 {
76 var result = xmltv_program_data_loader.ShowDialog();
77 if (result != DialogResult.OK) return;
78 string filename = xmltv_program_data_loader.FileName;
79 Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataFromFile)); worker.Start(filename);
80 }
81 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
82 }
83 private void mnuItemSaveData_Click(object sender, EventArgs e)
84 {
85 try
86 {
87 var result = xmltv_program_data_saver.ShowDialog();
88 if (result != DialogResult.OK) return;
89 string filename = xmltv_program_data_saver.FileName;
90 Thread worker = new Thread(new ParameterizedThreadStart(DeserializeDataToFile)); worker.Start(filename);
91 }
92 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
93 }
94 private void main_FormClosing(object sender, FormClosingEventArgs e) { try { XMLTV.Destroy(); } catch { } }
95 private void mnuItemClearLocalLog_Click(object sender, EventArgs e) { ClearLocalLog(); }
96 #endregion
97
98 private void ReportProgress(int progress)
99 {
100 if (this.InvokeRequired)
101 {
102 try { this.Invoke((Action)(delegate { ReportProgress(progress); })); }
103 catch { }
104 return;
105 }
106 if (progress_status != null && !this.IsDisposed)
107 progress_status.Value = progress;
108 }
109
110 #region gLog Events
111 private void gLog_ReportProgress(object sender, ReportProgressEventArgs e) { ReportProgress(e.Progress); }
112 #endregion
113 #region XMLT Events
114 void XMLTV_OnInstanceCreated(object sender, EventArgs e) { ReportProgress(0); CreateControls(); }
115 void LoadXMLTVShcedule(string schedule_xml) { XMLTV.Create(schedule_xml, new EventHandler<EventArgs>(XMLTV_OnInstanceCreated)); }
116 #endregion
117
118
119 #region Logging Events
120 StringBuilder log_flusher = new StringBuilder();
121 void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
122 void OnLogFlush(string logmessage)
123 {
124 if (this.IsDisposed) { return; }
125 UpdateStatus(logmessage);
126 UpdateLogOutput(logmessage);
127 Application.DoEvents();
128 }
129 private void ClearLocalLog()
130 {
131 var log_top_entry = txtLog.Lines.FirstOrDefault();
132 txtLog.Clear();
133 if (!string.IsNullOrEmpty(log_top_entry)) { txtLog.AppendText(log_top_entry); }
134 }
135 void UpdateStatus(string logmessage)
136 {
137 try
138 {
139 if (this.InvokeRequired)
140 {
141 try { this.Invoke((Action)(delegate { UpdateStatus(logmessage); })); }
142 catch { }
143 return;
144 }
145 txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
146 }
147 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
148 }
149 void UpdateLogOutput(string logmessage)
150 {
151 try
152 {
153 if (this.InvokeRequired)
154 {
155 try { this.Invoke((Action)(delegate { UpdateLogOutput(logmessage); })); }
156 catch { }
157 return;
158 }
159 txtLog.AppendText(logmessage);
160 txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
161 txtLog.ScrollToCaret(); //Now scroll it automatically
162 }
163 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
164 }
165 #endregion
166
167 #region Binary Data Read/Write support
168 private void DeserializeDataFromFile(object filename)
169 {
170 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
171 XMLTV.Load(filename.ToString(), new EventHandler<EventArgs>(XMLTV_OnInstanceCreated));
172 }
173 private void DeserializeDataToFile(object filename)
174 {
175 if (filename == null) { throw new ArgumentNullException("filename", "cannot be null"); }
176 XMLTV.Save(filename.ToString());
177 }
178 #endregion
179
180
181 #region t
182 void CreateControls()
183 {
184 try
185 {
186 if (this.InvokeRequired)
187 {
188 try { this.Invoke((Action)(delegate { CreateControls(); })); }
189 catch { }
190 return;
191 }
192 Type t;
193 object datasource = XMLTV.CreateBindingSourceFromData(XMLTV.GetPrograms(), out t);
194 //XMLTV.CreateSorterFromDataSource(ref datasource, "title");
195 dataGrid.DataSource = datasource;
196 }
197 catch (Exception ex) { gLog.Error.WriteLine(ex.ToString()); }
198 }
199 #endregion
200
201 }
202 }
203

  ViewVC Help
Powered by ViewVC 1.1.22