home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 October / PCWorld_2003-10_cd.bin / Software / Vyzkuste / windowsxxl / windowsxxl.exe / DashboardSetup-V11-Framework11.exe / Source / TemplateRSSControl.cs < prev    next >
Text File  |  2003-06-28  |  7KB  |  269 lines

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Windows.Forms;
  7. using System.Net;
  8. using System.IO;
  9. using System.Diagnostics;
  10. using System.Threading;
  11. using System.Xml;
  12. using System.Xml.Serialization;
  13. using System.Reflection;
  14. using System.Runtime.CompilerServices;
  15.  
  16. [assembly: AssemblyTitle("Newsfeed")]
  17. [assembly: AssemblyDescription("Generated newsfeed component for Codename: Dashboard")]
  18. [assembly: AssemblyConfiguration("")]
  19. [assembly: AssemblyCompany("SNP Software")]
  20. [assembly: AssemblyProduct("Codename: Dashboard")]
  21. [assembly: AssemblyCopyright("(c) 2002-2003 STOP Network Productions Ltd.")]
  22. [assembly: AssemblyTrademark("")]
  23. [assembly: AssemblyCulture("")]    
  24. [assembly: AssemblyVersion("1.1.518.942")]
  25.  
  26. namespace ***NAMESPACE***
  27. {
  28.     public class ***CLASSNAME*** : STOPNetwork.Dashboard.Components.ComponentControl
  29.     {
  30.         private LinkLabel[] NewsLabels = new LinkLabel[6];
  31.         private ToolTip[] NewsTips = new ToolTip[6];
  32.         private bool bSuspend = false;
  33.         private System.Windows.Forms.Timer timer1;
  34.         private System.ComponentModel.IContainer components;
  35.  
  36.         public ***CLASSNAME***()
  37.         {
  38.             InitializeComponent();
  39.         }
  40.  
  41.         protected override void Dispose( bool disposing )
  42.         {
  43.             if( disposing )
  44.             {
  45.                 if( components != null )
  46.                     components.Dispose();
  47.             }
  48.             base.Dispose( disposing );
  49.         }
  50.  
  51.         public override void InitializeControl()
  52.         {
  53.             for (int nCounter = 0; nCounter < 6; nCounter++)
  54.             {
  55.                 NewsLabels[nCounter] = new LinkLabel();
  56.                 NewsLabels[nCounter].Parent = this;
  57.                 NewsLabels[nCounter].Left = 5;
  58.                 NewsLabels[nCounter].Width = this.Width - 10;
  59.                 NewsLabels[nCounter].Top = (nCounter * 14) + 19;
  60.                 NewsLabels[nCounter].Height = 14;
  61.                 NewsLabels[nCounter].LinkColor = ComponentTextColor;
  62.                 NewsLabels[nCounter].VisitedLinkColor = ComponentTextColor;
  63.                 NewsLabels[nCounter].BackColor = Color.Transparent;
  64.                 NewsLabels[nCounter].Click += new System.EventHandler(NewsLabelClick);
  65.                 NewsLabels[nCounter].Show();
  66.  
  67.                 NewsTips[nCounter] = new ToolTip();
  68.                 NewsTips[nCounter].ShowAlways = true;
  69.             }
  70.  
  71.             if (this.Closed == false)
  72.             {
  73.                 this.Height = 108;
  74.                 this.LastHeight = 108;
  75.                 ComponentResize();
  76.             }
  77.             else
  78.             {
  79.                 this.LastHeight = 108;
  80.             }
  81.  
  82.             RetrieveNews();
  83.         }
  84.  
  85.         public override void CheckThemeElements()
  86.         {
  87.             for (int nCounter = 0; nCounter < 6; nCounter++)
  88.             {
  89.                 NewsLabels[nCounter].LinkColor = ComponentTextColor;
  90.                 NewsLabels[nCounter].VisitedLinkColor = ComponentTextColor;
  91.             }
  92.         }
  93.  
  94.         public override void SuspendComponent()
  95.         {
  96.             bSuspend = true;
  97.         }
  98.  
  99.         public override void ResumeComponent()
  100.         {
  101.             bSuspend = false;
  102.         }
  103.  
  104.         #region Component Designer generated code
  105.         /// <summary>
  106.         /// Required method for Designer support - do not modify 
  107.         /// the contents of this method with the code editor.
  108.         /// </summary>
  109.         private void InitializeComponent()
  110.         {
  111.             this.components = new System.ComponentModel.Container();
  112.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  113.             // 
  114.             // timer1
  115.             // 
  116.             this.timer1.Enabled = true;
  117.             this.timer1.Interval = 300000;
  118.             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  119.             // 
  120.             // TechCriticControl
  121.             // 
  122.             this.Name = "TechCriticControl";
  123.             this.Size = new System.Drawing.Size(156, 150);
  124.  
  125.         }
  126.         #endregion
  127.  
  128.         private void RetrieveNews()
  129.         {
  130.             if (bSuspend == true) {return;}
  131.  
  132.             WebClient newClient = new WebClient();
  133.  
  134.             try
  135.             {
  136.                 newClient.DownloadFile("***NEWSFEEDLOCATION***",Path.GetTempPath() + "***TEMPFILENAME***");
  137.  
  138.                 if (this.Closed == false)
  139.                 {
  140.                     if (this.Height != 108)
  141.                     {    
  142.                         this.Height = 108;
  143.                         ComponentResize();
  144.                     }
  145.                 }
  146.                 else
  147.                 {
  148.                     if (this.LastHeight != 108)
  149.                     {    
  150.                         this.LastHeight = 108;
  151.                     }
  152.                 }
  153.             }
  154.             catch (System.Net.WebException) 
  155.             {
  156.                 NewsLabels[0].Text = "Download Error";
  157.                 
  158.                 for (int nCounter = 1; nCounter < 6; nCounter++)
  159.                 {
  160.                     NewsLabels[nCounter].Text = "";
  161.                 }
  162.  
  163.                 if (this.Closed == false)
  164.                 {
  165.                     this.Height = 35;
  166.                     ComponentResize();
  167.                 }
  168.                 else
  169.                 {
  170.                     this.LastHeight = 35;
  171.                 }
  172.  
  173.                 return;
  174.             }
  175.  
  176.             RSSNewsFeed newFeed = new RSSNewsFeed();
  177.             try
  178.             {
  179.                 NewsFeedSerializer.Deserialize(ref newFeed,Path.GetTempPath() + "***TEMPFILENAME***");
  180.             }
  181.             catch (Exception)
  182.             {
  183.                 NewsLabels[0].Text = "Read Error";
  184.  
  185.                 for (int nCounter = 1; nCounter < 6; nCounter++)
  186.                 {
  187.                     NewsLabels[nCounter].Text = "";
  188.                 }
  189.  
  190.                 return;
  191.             }
  192.  
  193.             for (int nCounter = 0; nCounter < 6; nCounter++)
  194.             {
  195.                 Object[] oArgs = {(Object)newFeed.ccChannel.niItem[nCounter].strTitle,(Object)newFeed.ccChannel.niItem[nCounter].strLink,(Object)nCounter};
  196.                 SetNewsTextDelegate oDel = new SetNewsTextDelegate(this.SetNewsText);
  197.                 this.Invoke(oDel,oArgs);
  198.             }
  199.         }
  200.  
  201.         delegate void SetNewsTextDelegate(String strText, String strLink, int nIndex);
  202.         private void SetNewsText(String strText,String strLink,int nIndex)
  203.         {
  204.             NewsLabels[nIndex].Text = strText;
  205.             NewsLabels[nIndex].Tag = (Object)strLink;
  206.             NewsTips[nIndex].ShowAlways = true;
  207.             NewsTips[nIndex].SetToolTip(NewsLabels[nIndex],strText);
  208.         }
  209.  
  210.         private void NewsLabelClick(object sender, System.EventArgs e)
  211.         {
  212.             Process.Start(this.BrowserLocation,((LinkLabel)sender).Tag.ToString());
  213.         }
  214.  
  215.         private void timer1_Tick(object sender, System.EventArgs e)
  216.         {
  217.             new Thread(new ThreadStart(RetrieveNews)).Start();
  218.         }
  219.     }
  220.  
  221.     /// <summary>
  222.     /// Class to hold the data from the news feed
  223.     /// </summary>
  224.     [XmlRoot("rss")]
  225.     public class RSSNewsFeed
  226.     {
  227.         public RSSNewsFeed() {}
  228.  
  229.         [XmlAttribute("version")] public String strVersion;
  230.         [XmlElement("channel")] public ChannelClass ccChannel;
  231.     }
  232.  
  233.     /// <summary>
  234.     /// Class to hold a set of stories
  235.     /// </summary>
  236.     public class ChannelClass
  237.     {
  238.         [XmlElement("item")] public ItemClass[] niItem;
  239.     }
  240.  
  241.     /// <summary>
  242.     /// Class to hold a single story
  243.     /// </summary>
  244.     public class ItemClass
  245.     {
  246.         [XmlElement("title")] public String strTitle;
  247.         [XmlElement("link")] public String strLink;
  248.     }
  249.  
  250.     /// <summary>
  251.     /// The NewsSerializer handles all File I/O for the component
  252.     /// </summary>
  253.     public class NewsFeedSerializer
  254.     {
  255.         /// <summary>
  256.         /// Deserializes the news feed from the disk and populates the NewsFeed class
  257.         /// </summary>
  258.         /// <param name="theFeed">Reference to the news feed object to deserialize</param>
  259.         /// <param name="strPath">Path to the temporary XML file</param>
  260.         public static void Deserialize(ref RSSNewsFeed theFeed,String strPath)
  261.         {
  262.             XmlSerializer newSerial = new XmlSerializer(typeof(RSSNewsFeed));
  263.             StreamReader newReader = new StreamReader(strPath);
  264.             theFeed = (RSSNewsFeed)newSerial.Deserialize(newReader);
  265.             newReader.Close();
  266.         }
  267.     }
  268. }
  269.