Example #1
0
        private void Loop()
        {
            DateTime next = DateTime.Now;

            while (true)
            {
                if (next.CompareTo(DateTime.Now) <= 0)
                {
                    bool b = this.UpdateItemCollection();
                    next = DateTime.Now.Add(new TimeSpan(0, 0, (b) ? this.FeedLoadInterval : Math.Min(this.FeedLoadInterval, 600)));
                }
                mCurrentItemIndex++;
                if (mCurrentItemIndex >= mItemCollection.Count)
                {
                    mCurrentItemIndex = 0;
                }
                if (mItemCollection.Count > 0)
                {
                    RSSItem ri = mItemCollection[mCurrentItemIndex];
                    this.UpdateUI(ri);
                }
                else
                {
                    this.UpdateUI(this.Default);
                }
                Thread.Sleep(this.UpdateInterval * 1000);
            }
        }
Example #2
0
        /// <summary>
        /// Creates an RSS item from an XML node with the
        /// corresponding child nodes (title,description etc.)
        /// </summary>
        /// <param name="xmlNode">The node to extract the details from</param>
        /// <returns>An RssItem object with details taken from the item node.</returns>
        private RSSItem getRssItem(XmlNode xmlNode)
        {
            RSSItem rssItem = new RSSItem();

            for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
            {
                switch (xmlNode.ChildNodes[i].Name.ToLower())
                {
                case "title":
                {
                    rssItem.Title = xmlNode.ChildNodes[i].InnerText;
                    break;
                }

                case "description":
                {
                    rssItem.Description = xmlNode.ChildNodes[i].InnerText;
                    break;
                }

                case "link":
                {
                    rssItem.Link = xmlNode.ChildNodes[i].InnerText;
                    break;
                }

                case "author":
                {
                    rssItem.Author = xmlNode.ChildNodes[i].InnerText;
                    break;
                }

                case "comments":
                {
                    rssItem.Comments = xmlNode.ChildNodes[i].InnerText;
                    break;
                }

                case "pubdate":
                {
                    rssItem.Pubdate = xmlNode.ChildNodes[i].InnerText;
                    break;
                }

                case "guid":
                {
                    rssItem.Guid = xmlNode.ChildNodes[i].InnerText;
                    break;
                }
                }
            }

            return(rssItem);
        }
Example #3
0
        private void UpdateUI(RSSItem item)
        {
            ToolStrip parent = this.Parent as ToolStrip;

            if (parent != null && parent.InvokeRequired)
            {
                parent.Invoke(new UpdateUIDelegate(DirectUpdateUI), new object[] { item });
            }
            else
            {
                this.DirectUpdateUI(item);
            }
        }
Example #4
0
        private void DirectUpdateUI(RSSItem item)
        {
            string txt = item.Title;

            if (string.IsNullOrEmpty(item.Pubdate) == false)
            {
                DateTime dt = DateTime.Parse(item.Pubdate);
                if (dt != null)
                {
                    txt += " (" + dt.ToShortDateString() + ")";
                }
            }
            this.Text        = txt;
            this.Tag         = item.Link;
            this.LinkVisited = false;
        }
Example #5
0
 private void UpdateUI(RSSItem item)
 {
     ToolStrip parent = this.Parent as ToolStrip;
       if (parent != null && parent.InvokeRequired)
       {
     parent.Invoke(new UpdateUIDelegate(DirectUpdateUI), new object[] { item });
       }
       else
       {
     this.DirectUpdateUI(item);
       }
 }
Example #6
0
 private void DirectUpdateUI(RSSItem item)
 {
     string txt = item.Title;
       if (string.IsNullOrEmpty(item.Pubdate) == false)
       {
     DateTime dt = DateTime.Parse(item.Pubdate);
     if (dt != null)
     {
       txt += " (" + dt.ToShortDateString() + ")";
     }
       }
       this.Text = txt;
       this.Tag = item.Link;
       this.LinkVisited = false;
 }
Example #7
0
 public void Add(RSSItem rssItem)
 {
     List.Add(rssItem);
 }
Example #8
0
        /// <summary>
        /// Creates an RSS item from an XML node with the 
        /// corresponding child nodes (title,description etc.)
        /// </summary>
        /// <param name="xmlNode">The node to extract the details from</param>
        /// <returns>An RssItem object with details taken from the item node.</returns>
        private RSSItem getRssItem(XmlNode xmlNode)
        {
            RSSItem rssItem = new RSSItem();

            for (int i=0;i < xmlNode.ChildNodes.Count;i++)
            {
                switch ( xmlNode.ChildNodes[i].Name.ToLower() )
                {
                    case "title":
                    {
                        rssItem.Title = xmlNode.ChildNodes[i].InnerText;
                        break;
                    }
                    case "description":
                    {
                        rssItem.Description = xmlNode.ChildNodes[i].InnerText;
                        break;
                    }
                    case "link":
                    {
                        rssItem.Link = xmlNode.ChildNodes[i].InnerText;
                        break;
                    }
                    case "author":
                    {
                        rssItem.Author = xmlNode.ChildNodes[i].InnerText;
                        break;
                    }
                    case "comments":
                    {
                        rssItem.Comments = xmlNode.ChildNodes[i].InnerText;
                        break;
                    }
                    case "pubdate":
                    {
                        rssItem.Pubdate = xmlNode.ChildNodes[i].InnerText;
                        break;
                    }
                    case "guid":
                    {
                        rssItem.Guid = xmlNode.ChildNodes[i].InnerText;
                        break;
                    }
                }
            }

            return rssItem;
        }
Example #9
0
        /// <summary>
        /// Retrieves an RSS feed using the given Url, parses it and
        /// creates and new <see cref="RssFeed">RssFeed</see> object with the information.
        /// If an error occurs in the XML loading of the document, or parsing of
        /// the RSS feed, the error is trapped and stored inside the RssFeed's
        /// ErrorMessage property.
        /// </summary>
        /// <param name="Url">The url to retrieve the RSS feed from, this can
        /// be in the format of http:// and also file://.. (ftp?)</param>
        /// <returns>An <see cref="RssFeed">RssFeed</see> object with information
        /// retrieved from the feed.</returns>
        public RSSFeed Retrieve(string Url)
        {
            RSSFeed rssFeed = new RSSFeed();

            rssFeed.URL   = Url;
            rssFeed.Items = new RSSItemCollection();

            XmlTextReader xmlTextReader = new XmlTextReader(Url);
            XmlDocument   xmlDoc        = new XmlDocument();

            try
            {
                xmlDoc.Load(xmlTextReader);

                // Fire the load event
                if (this.FeedLoaded != null)
                {
                    this.FeedLoaded(this, new EventArgs());
                }

                XmlNode rssXmlNode = null;

                // Loop child nodes till we find the rss one
                for (int i = 0; i < xmlDoc.ChildNodes.Count; i++)
                {
                    System.Diagnostics.Debug.Write("Child: " + xmlDoc.ChildNodes[i].Name);
                    System.Diagnostics.Debug.WriteLine(" has " + xmlDoc.ChildNodes[i].ChildNodes.Count + " children");

                    if (xmlDoc.ChildNodes[i].Name == this.rootNodeName && xmlDoc.ChildNodes[i].ChildNodes.Count > 0)
                    {
                        rssXmlNode = xmlDoc.ChildNodes[i];

                        // Fire the found event
                        if (this.RssNodeFound != null)
                        {
                            this.RssNodeFound(this, new EventArgs());
                        }

                        break;
                    }
                }

                if (rssXmlNode != null)
                {
                    XmlNode channelXmlNode = null;

                    // Loop through the rss node till we find the channel
                    for (int i = 0; i < rssXmlNode.ChildNodes.Count; i++)
                    {
                        System.Diagnostics.Debug.WriteLine("Rss child: " + rssXmlNode.ChildNodes[i].Name);
                        if (rssXmlNode.ChildNodes[i].Name == this.channelNodeName && rssXmlNode.ChildNodes[i].ChildNodes.Count > 0)
                        {
                            channelXmlNode = rssXmlNode.ChildNodes[i];

                            // Fire the found event
                            if (this.ChannelNodeFound != null)
                            {
                                this.ChannelNodeFound(this, new EventArgs());
                            }

                            break;
                        }
                    }

                    // Found the channel node
                    if (channelXmlNode != null)
                    {
                        // Loop through its children, copying details to the
                        // RssFeed struct, and parsing the items
                        for (int i = 0; i < channelXmlNode.ChildNodes.Count; i++)
                        {
                            System.Diagnostics.Debug.WriteLine(channelXmlNode.ChildNodes[i].Name);
                            switch (channelXmlNode.ChildNodes[i].Name.ToLower())
                            {
                            case "title":
                            {
                                rssFeed.Title = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "description":
                            {
                                rssFeed.Description = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "language":
                            {
                                rssFeed.Language = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "copyright":
                            {
                                rssFeed.Copyright = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "webmaster":
                            {
                                rssFeed.Webmaster = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "pubDate":
                            {
                                rssFeed.PubDate = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "lastBuildDate":
                            {
                                rssFeed.LastBuildDate = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "category":
                            {
                                rssFeed.Category = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "generator":
                            {
                                rssFeed.Generator = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "ttl":
                            {
                                rssFeed.Ttl = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "rating":
                            {
                                rssFeed.Rating = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "skipHours":
                            {
                                rssFeed.Skiphours = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "skipDays":
                            {
                                rssFeed.Skipdays = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "managingEditor":
                            {
                                rssFeed.ManagingEditor = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "item":
                            {
                                RSSItem t = this.getRssItem(channelXmlNode.ChildNodes[i]);
                                t.Feed = rssFeed;
                                rssFeed.Items.Add(t);

                                // Fire the found event
                                if (this.ItemAdded != null)
                                {
                                    this.ItemAdded(this, new EventArgs());
                                }

                                break;
                            }
                            }
                        }

                        // If rdf mode is set, then the channel node only contains
                        // information about the channel, it doesn't hold the item
                        // nodes. The item nodes are children of the root node in
                        // an RDF document, so we use this instead.
                        if (this.RdfMode)
                        {
                            for (int i = 0; i < rssXmlNode.ChildNodes.Count; i++)
                            {
                                switch (rssXmlNode.ChildNodes[i].Name)
                                {
                                case "item":
                                {
                                    RSSItem t = this.getRssItem(rssXmlNode.ChildNodes[i]);
                                    t.Feed = rssFeed;
                                    rssFeed.Items.Add(t);

                                    // Fire the found event
                                    if (this.ItemAdded != null)
                                    {
                                        this.ItemAdded(this, new EventArgs());
                                    }

                                    break;
                                }
                                }
                            }
                        }
                    }
                    else
                    {
                        rssFeed.ErrorMessage = "Unable to find rss <seehannel> node";

                        // Fire the error event
                        if (this.Error != null)
                        {
                            RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
                            args.Message = rssFeed.ErrorMessage;
                            this.Error(this, args);
                        }
                    }
                }
                else
                {
                    rssFeed.ErrorMessage = "Unable to find root <rss> node";

                    // Fire the error event
                    if (this.Error != null)
                    {
                        RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
                        args.Message = rssFeed.ErrorMessage;
                        this.Error(this, args);
                    }
                }
            }
            catch (XmlException err)
            {
                //
                rssFeed.ErrorMessage = "Xml error: " + err.Message;

                // Fire the error event
                if (this.Error != null)
                {
                    RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
                    args.Message = rssFeed.ErrorMessage;
                    this.Error(this, args);
                }
                return(rssFeed);
            }
            catch (WebException we)
            {
                rssFeed.ErrorMessage = "Web error: " + we.Message;
            }

            return(rssFeed);
        }
Example #10
0
 public void Add(RSSItem rssItem)
 {
     List.Add(rssItem);
 }