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 / TemplateRDFControl.cs < prev    next >
Text File  |  2003-06-28  |  9KB  |  345 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.             this.Height = 108;
  72.             this.LastHeight = 108;
  73.  
  74.             RetrieveNews();
  75.         }
  76.  
  77.         public override void CheckThemeElements()
  78.         {
  79.             for (int nCounter = 0; nCounter < 6; nCounter++)
  80.             {
  81.                 NewsLabels[nCounter].LinkColor = ComponentTextColor;
  82.                 NewsLabels[nCounter].VisitedLinkColor = ComponentTextColor;
  83.             }
  84.         }
  85.  
  86.         public override void SuspendComponent()
  87.         {
  88.             bSuspend = true;
  89.         }
  90.  
  91.         public override void ResumeComponent()
  92.         {
  93.             bSuspend = false;
  94.         }
  95.  
  96.         #region Component Designer generated code
  97.         /// <summary>
  98.         /// Required method for Designer support - do not modify 
  99.         /// the contents of this method with the code editor.
  100.         /// </summary>
  101.         private void InitializeComponent()
  102.         {
  103.             this.components = new System.ComponentModel.Container();
  104.             this.timer1 = new System.Windows.Forms.Timer(this.components);
  105.             // 
  106.             // timer1
  107.             // 
  108.             this.timer1.Enabled = true;
  109.             this.timer1.Interval = 300000;
  110.             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  111.             // 
  112.             // TechCriticControl
  113.             // 
  114.             this.Name = "TechCriticControl";
  115.             this.Size = new System.Drawing.Size(156, 150);
  116.  
  117.         }
  118.         #endregion
  119.  
  120.         private void RetrieveNews()
  121.         {
  122.             if (bSuspend == true) {return;}
  123.  
  124.             WebClient newClient = new WebClient();
  125.  
  126.             try
  127.             {
  128.                 newClient.DownloadFile("***NEWSFEEDLOCATION***",Path.GetTempPath() + "***TEMPFILENAME***");
  129.  
  130.                 if (this.Closed == false)
  131.                 {
  132.                     if (this.Height != 108)
  133.                     {    
  134.                         this.Height = 108;
  135.                         ComponentResize();
  136.                     }
  137.                 }
  138.                 else
  139.                 {
  140.                     if (this.LastHeight != 108)
  141.                     {    
  142.                         this.LastHeight = 108;
  143.                     }
  144.                 }
  145.             }
  146.             catch (System.Net.WebException) 
  147.             {
  148.                 NewsLabels[0].Text = "Download Error";
  149.                 
  150.                 for (int nCounter = 1; nCounter < 6; nCounter++)
  151.                 {
  152.                     NewsLabels[nCounter].Text = "";
  153.                 }
  154.  
  155.                 if (this.Closed == false)
  156.                 {
  157.                     this.Height = 35;
  158.                     ComponentResize();
  159.                 }
  160.                 else
  161.                 {
  162.                     this.LastHeight = 35;
  163.                 }
  164.  
  165.                 return;
  166.             }
  167.  
  168.             RDFNewsFeed newFeed = new RDFNewsFeed();
  169.             try
  170.             {
  171.                 NewsFeedSerializer.Deserialize(ref newFeed,Path.GetTempPath() + "***TEMPFILENAME***");
  172.             }
  173.             catch (Exception)
  174.             {
  175.                 NewsLabels[0].Text = "Read Error";
  176.  
  177.                 for (int nCounter = 1; nCounter < 6; nCounter++)
  178.                 {
  179.                     NewsLabels[nCounter].Text = "";
  180.                 }
  181.  
  182.                 return;
  183.             }
  184.  
  185.             for (int nCounter = 0; nCounter < 6; nCounter++)
  186.             {
  187.                 Object[] oArgs = {(Object)newFeed.Items[nCounter].Title,(Object)newFeed.Items[nCounter].URL,(Object)nCounter};
  188.                 SetNewsTextDelegate oDel = new SetNewsTextDelegate(this.SetNewsText);
  189.                 this.Invoke(oDel,oArgs);
  190.             }
  191.         }
  192.  
  193.         delegate void SetNewsTextDelegate(String strText, String strLink, int nIndex);
  194.         private void SetNewsText(String strText,String strLink,int nIndex)
  195.         {
  196.             NewsLabels[nIndex].Text = strText;
  197.             NewsLabels[nIndex].Tag = (Object)strLink;
  198.             NewsTips[nIndex].ShowAlways = true;
  199.             NewsTips[nIndex].SetToolTip(NewsLabels[nIndex],strText);
  200.         }
  201.  
  202.         private void NewsLabelClick(object sender, System.EventArgs e)
  203.         {
  204.             Process.Start(this.BrowserLocation,((LinkLabel)sender).Tag.ToString());
  205.         }
  206.  
  207.         private void timer1_Tick(object sender, System.EventArgs e)
  208.         {
  209.             new Thread(new ThreadStart(RetrieveNews)).Start();
  210.         }
  211.     }
  212.  
  213.     /// <summary>
  214.     /// The NewsFeed class holds all the information from the feed
  215.     /// </summary>
  216.     public class RDFNewsFeed
  217.     {
  218.         /// <summary>
  219.         /// Create an instance of NewsFeed
  220.         /// </summary>
  221.         public RDFNewsFeed() {}
  222.  
  223.         /// <summary>
  224.         /// The Item group that holds the information
  225.         /// </summary>
  226.         public ItemClass[] Items;
  227.  
  228.         /// <summary>
  229.         /// Clear the feed so it can be read in again
  230.         /// </summary>
  231.         public void Clear()
  232.         {
  233.             try
  234.             {
  235.                 for (int nCounter = 0; nCounter < Items.Length; nCounter++)
  236.                 {
  237.                     GC.ReRegisterForFinalize((Object)Items[nCounter]);
  238.                     Items[nCounter] = null;
  239.                 }
  240.             }
  241.             catch (System.NullReferenceException) {;}
  242.  
  243.             Items = null;
  244.         }
  245.  
  246.         /// <summary>
  247.         /// Add an item to the collection
  248.         /// </summary>
  249.         /// <param name="Title">Title of the Article</param>
  250.         /// <param name="URL">URL of the Article</param>
  251.         public void AddItem(String Title, String URL)
  252.         {
  253.             ItemClass newItem = new ItemClass();
  254.             ArrayList alItems = new ArrayList();
  255.  
  256.             newItem.Title = Title;
  257.             newItem.URL = URL;
  258.  
  259.             try
  260.             {
  261.                 for (int nCounter = 0; nCounter < Items.Length; nCounter++)
  262.                 {
  263.                     alItems.Add((Object)Items[nCounter]);
  264.                 }
  265.             }
  266.             catch (System.NullReferenceException) {;}
  267.  
  268.             alItems.Add((Object)newItem);
  269.  
  270.             Items = new ItemClass[alItems.Count];
  271.  
  272.             for (int nCounter = 0; nCounter < alItems.Count; nCounter++)
  273.             {
  274.                 Items[nCounter] = (ItemClass)alItems[nCounter];
  275.             }
  276.         }
  277.     }
  278.  
  279.     /// <summary>
  280.     /// The ItemClass class holds the information for a single story
  281.     /// </summary>
  282.     public class ItemClass
  283.     {
  284.         /// <summary>
  285.         /// Construct an instance of the ItemClass class
  286.         /// </summary>
  287.         public ItemClass() {}
  288.  
  289.         public String Title;
  290.         public String URL;
  291.     }
  292.  
  293.     /// <summary>
  294.     /// The FlexBetaSerializer handles all File I/O for the component
  295.     /// </summary>
  296.     public class NewsFeedSerializer
  297.     {
  298.         /// <summary>
  299.         /// Deserializes the news feed XML file from the disk
  300.         /// </summary>
  301.         /// <param name="thisNews">A reference to the FlexBetaNews object to deserialize</param>
  302.         public static void Deserialize(ref RDFNewsFeed thisNews,String TempFilePath)
  303.         {
  304.             String strNextURL = "";
  305.             String strNextTitle = "";
  306.  
  307.             thisNews.Clear();
  308.  
  309.             System.Xml.XmlTextReader newReader = new XmlTextReader(TempFilePath);
  310.             while (newReader.Read() != false)
  311.             {
  312.                 if ((newReader.Name == "item") & (newReader.NodeType != XmlNodeType.EndElement))
  313.                 {
  314.                     strNextURL = "";
  315.                     strNextTitle = "";
  316.  
  317.                     while (newReader.Read() != false)
  318.                     {
  319.                         if ((newReader.Name == "title") & (newReader.NodeType != XmlNodeType.EndElement))
  320.                         {
  321.                             newReader.Read();
  322.                             strNextTitle = newReader.Value;
  323.                         }
  324.                         if ((newReader.Name == "link") & (newReader.NodeType != XmlNodeType.EndElement))
  325.                         {
  326.                             newReader.Read();
  327.                             strNextURL = newReader.Value;
  328.                         }
  329.  
  330.                         if (strNextURL != "")
  331.                         {
  332.                             if (strNextTitle != "")
  333.                             {
  334.                                 thisNews.AddItem(strNextTitle,strNextURL);
  335.                                 break;
  336.                             }
  337.                         }
  338.                     }
  339.                 }
  340.             }
  341.             newReader.Close();
  342.         }
  343.     }
  344. }
  345.