2
using System.Collections.Generic;
3
using System.Diagnostics;
8
using System.Text.RegularExpressions;
9
using System.Windows.Forms;
10
using XiboClient.Control;
12
using Xilium.CefGlue.WindowsForms;
16
class CefWebMedia : Media
18
private bool _disposed = false;
19
private bool _startWhenReady = false;
20
private string _filePath;
21
private RegionOptions _options;
22
private TemporaryFile _temporaryFile;
23
private CefWebBrowser _webView;
25
public CefWebMedia(RegionOptions options)
26
: base(options.width, options.height, options.top, options.left)
28
// Collect some options from the Region Options passed in
29
// and store them in member variables.
33
_filePath = ApplicationSettings.Default.LibraryPath + @"\" + _options.mediaid + ".htm";
35
// We will need a temporary file to store this HTML
36
_temporaryFile = new TemporaryFile();
38
Color backgroundColor = ColorTranslator.FromHtml(_options.backgroundColor);
40
CefBrowserSettings settings = new CefBrowserSettings();
41
settings.BackgroundColor = new CefColor(backgroundColor.A, backgroundColor.R, backgroundColor.G, backgroundColor.B);
43
// Create the web view we will use
44
_webView = new CefWebBrowser();
45
_webView.BrowserSettings = settings;
46
_webView.Dock = DockStyle.Fill;
47
_webView.BrowserCreated += _webView_BrowserCreated;
48
_webView.LoadEnd += _webView_LoadEnd;
51
// Check to see if the HTML is ready for us.
54
// Write to temporary file
55
SaveToTemporaryFile();
57
_startWhenReady = true;
64
// We need to come up with a way of setting this control to Visible = false here and still kicking
65
// off the webbrowser.
66
// I think we can do this by hacking some bits into the Cef.WinForms dll.
67
// Currently if we set this to false a browser isn't initialised by the library because it initializes it in OnHandleCreated
68
// We also need a way to protect against the web browser never being created for some reason.
69
// If it isn't then the control will never exipre (we might need to start the timer and then reset it).
71
// Start the timer and then base.RestartTimer() in _webview_LoadEnd
74
//_webView.Visible = false;
76
Controls.Add(_webView);
82
void _webView_BrowserCreated(object sender, EventArgs e)
88
// Navigate to temp file
89
_webView.Browser.GetMainFrame().LoadUrl(_temporaryFile.Path);
92
void _webView_LoadEnd(object sender, LoadEndEventArgs e)
101
public override void RenderMedia()
103
// We don't do anything in here as we want to start the timer from when the web view has loaded
106
private bool HtmlReady()
108
// Pull the RSS feed, and put it in a temporary file cache
109
// We want to check the file exists first
110
if (!File.Exists(_filePath) || _options.updateInterval == 0)
113
// It exists - therefore we want to get the last time it was updated
114
DateTime lastWriteDate = File.GetLastWriteTime(_filePath);
116
if (_options.LayoutModifiedDate.CompareTo(lastWriteDate) > 0 || DateTime.Now.CompareTo(lastWriteDate.AddHours(_options.updateInterval * 1.0 / 60.0)) > 0)
123
/// Updates the position of the background and saves to a temporary file
125
private void SaveToTemporaryFile()
127
// read the contents of the file
128
using (StreamReader reader = new StreamReader(_filePath))
130
string cachedFile = reader.ReadToEnd();
132
// Handle the background
135
if (_options.backgroundImage == null || _options.backgroundImage == "")
137
bodyStyle = "background-color:" + _options.backgroundColor + " ;";
141
bodyStyle = "background-image: url('" + _options.backgroundImage.Replace('\\', '/') + "'); background-attachment:fixed; background-color:" + _options.backgroundColor + "; background-repeat: no-repeat; background-position: " + _options.backgroundLeft + "px " + _options.backgroundTop + "px;";
144
string html = cachedFile.Replace("</head>", "<style type='text/css'>body {" + bodyStyle + " }</style></head>");
146
// We also want to parse out the duration using a regular expression
149
Match match = Regex.Match(html, "<!-- DURATION=(.*?) -->");
153
// We have a match, so override our duration.
154
Duration = Convert.ToInt32(match.Groups[1].Value);
159
Trace.WriteLine(new LogMessage("Html - SaveToTemporaryFile", "Unable to pull duration using RegEx").ToString());
162
_temporaryFile.FileContent = html;
167
/// Refresh the Local cache of the DataSetView HTML
169
private void RefreshFromXmds()
171
xmds.xmds xmds = new XiboClient.xmds.xmds();
172
xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds;
173
xmds.GetResourceCompleted += new XiboClient.xmds.GetResourceCompletedEventHandler(xmds_GetResourceCompleted);
175
xmds.GetResourceAsync(ApplicationSettings.Default.ServerKey, ApplicationSettings.Default.HardwareKey, _options.layoutId, _options.regionId, _options.mediaid, ApplicationSettings.Default.Version);
181
/// <param name="sender"></param>
182
/// <param name="e"></param>
183
private void xmds_GetResourceCompleted(object sender, XiboClient.xmds.GetResourceCompletedEventArgs e)
190
Trace.WriteLine(new LogMessage("xmds_GetResource", "Unable to get Resource: " + e.Error.Message), LogType.Error.ToString());
192
// Start the timer so that we expire
197
// Write to the library
198
using (StreamWriter sw = new StreamWriter(File.Open(_filePath, FileMode.Create, FileAccess.Write, FileShare.Read)))
204
// Write to temporary file
205
SaveToTemporaryFile();
207
// Handle Navigate in here because we will not have done it during first load
208
_webView.Browser.GetMainFrame().LoadUrl(_temporaryFile.Path);
211
catch (ObjectDisposedException)
213
Trace.WriteLine(new LogMessage("WebMedia", "Retrived the data set, stored the document but the media has already expired."), LogType.Error.ToString());
217
Trace.WriteLine(new LogMessage("WebMedia", "Unknown exception " + ex.Message), LogType.Error.ToString());
219
// This should exipre the media
226
/// Dispose of this text item
228
/// <param name="disposing"></param>
229
protected override void Dispose(bool disposing)
235
// Remove the webbrowser control
238
if (_webView != null)
243
Trace.WriteLine(new LogMessage("WebBrowser still in use.", String.Format("Dispose")));
246
// Delete the temporary file
249
if (_temporaryFile != null)
250
_temporaryFile.Dispose();
254
Debug.WriteLine("Unable to delete temporary file", "CefWebMedia - Dispose");
258
base.Dispose(disposing);