/* * This work is licensed under the Creative Commons Attribution 2.5 License. * To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, * San Francisco, California, 94105, USA. * * Original developer: David Betz * */ using System; using System.Collections.Generic; using System.Reflection; using System.Data; using System.Configuration; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Net; using FeedBuilder; using DataFeedFramework.Web.Controls.Support; [assembly: System.Web.UI.WebResource("DataFeedFramework.Web.Controls.RssLive.png", "image/png")] namespace DataFeedFramework.Web.Controls { [ToolboxData("<{0}:InfoBlock runat=\"server\">")] public class InfoBlock : CompositeControl { private Repeater repeater; private InfoBlockData informationBlockData; private String title; private Boolean registerFeedWithBrowser = false; private InfoBlockData InformationBlockData { get { if (informationBlockData == null) { informationBlockData = new InfoBlockData(); } return informationBlockData; } set { informationBlockData = value; } } public Int32 BlockHeight { get { return this.InformationBlockData.BlockHeight; } set { this.InformationBlockData.BlockHeight = value; } } public Int32 BlockWidth { get { return this.InformationBlockData.BlockWidth; } set { this.InformationBlockData.BlockWidth = value; } } public Boolean ShowBorder { get { return this.InformationBlockData.ShowBorder; } set { this.InformationBlockData.ShowBorder = value; } } public BlockSize BlockSize { get { return this.InformationBlockData.BlockSize; } set { this.InformationBlockData.BlockSize = value; } } public Boolean RegisterFeedWithBrowser { get { return registerFeedWithBrowser; } set { registerFeedWithBrowser = value; } } public String Title { get { return title; } private set { title = value; } } public String DataSource { get { if (this.InformationBlockData.DataSource.Length < 1) { throw new ApplicationException("DataSource cannot be blank for InformationBlock"); } return this.InformationBlockData.DataSource; } set { this.InformationBlockData.DataSource = value; } } String BlockCssClass { get { switch (this.BlockSize) { case BlockSize.Regular: return "InformationBlock"; case BlockSize.Wide: return "InformationWideBlock"; case BlockSize.Tall: return "InformationHighBlock"; default: return "InformationBlock"; } } } public InfoBlock() { } protected override void CreateChildControls() { HtmlGenericControl div = new HtmlGenericControl("div"); String inlineStyle = ""; if (this.BlockWidth > 0) { inlineStyle = "width: " + this.BlockWidth.ToString() + "px !important;"; } if (this.BlockHeight > 0) { inlineStyle = "height: " + this.BlockHeight.ToString() + "px !important;"; } if (inlineStyle.Length > 0) { div.Attributes.Add("style", inlineStyle); } if (ShowBorder) { div.Attributes.Add("class", this.BlockCssClass); } else { div.Attributes.Add("class", this.BlockCssClass + " InformationBlockNoBorder"); } HtmlGenericControl footerDiv = new HtmlGenericControl("div"); footerDiv.Attributes.Add("class", "BlockFooter"); Object bindingSource = null; Object feedLink = null; Boolean localResource = false; String dffLocalPrefixList = ConfigurationFacade.ApplicationSettings("DataFeedFrameworkLocalPrefixList"); String[] dffLocalPrefixes = dffLocalPrefixList.Split(",".ToCharArray()); foreach (String prefix in dffLocalPrefixes) { if (this.DataSource.Contains(prefix)) { localResource = true; } } if (localResource) { feedLink = new Literal(); } else { feedLink = new HyperLink(); } try { AbstractReader reader = FeedReaderBuilder.Build(this.DataSource); bindingSource = reader.GetEntryList(); if (String.IsNullOrEmpty(this.Title)) { this.Title = reader.FeedTitle; } if (!localResource) { ((HyperLink)feedLink).NavigateUrl = reader.FeedLink; } if (this.RegisterFeedWithBrowser) { if (this.Page.Header == null) { throw new InvalidOperationException("To register a feed with a browser, the header of the page must have runat=\"server\" set."); } HtmlGenericControl browserLink = new HtmlGenericControl("link"); browserLink.Attributes.Add("rel", "alternate"); browserLink.Attributes.Add("type", "application/rss+xml"); browserLink.Attributes.Add("href", reader.FeedLink); browserLink.Attributes.Add("title", reader.FeedTitle); this.Page.Header.Controls.Add(browserLink); } HyperLink link = new HyperLink(); link.NavigateUrl = this.DataSource; Image rssButton = new Image(); String url = Page.ClientScript.GetWebResourceUrl(typeof(DataFeedFramework.Web.Controls.InfoBlock), "DataFeedFramework.Web.Controls.RssLive.png"); rssButton.ImageUrl = url; rssButton.AlternateText = "RSS"; link.Controls.Add(rssButton); footerDiv.Controls.Add(link); } catch (WebException ex) { Literal error = new Literal(); if (ex.Message == "Invalid URI: The URI scheme is not valid") { error.Text = "Incompatible feed detected."; } else { error.Text = "" + ex.Message + ""; } div.Controls.Add(error); } HtmlGenericControl h3 = new HtmlGenericControl("h3"); Literal h3Text = new Literal(); h3Text.Text = this.Title; if (feedLink != null && feedLink is HyperLink) { ((HyperLink)feedLink).Controls.Add(h3Text); h3.Controls.Add((HyperLink)feedLink); } else { h3.Controls.Add(h3Text); } div.Controls.Add(h3); try { repeater = new Repeater(); repeater.DataSource = bindingSource; repeater.DataMember = "text"; repeater.ItemTemplate = new RssListTemplate(ListItemType.Item); repeater.HeaderTemplate = new RssListTemplate(ListItemType.Header); repeater.FooterTemplate = new RssListTemplate(ListItemType.Footer); div.Controls.Add(repeater); repeater.DataBind(); } catch (Exception ex) { Literal error = new Literal(); error.Text = "" + ex.Message + ""; div.Controls.Add(error); } div.Controls.Add(footerDiv); this.Controls.Add(div); base.CreateChildControls(); } } }