Click here to Skip to main content
15,896,730 members
Articles / Programming Languages / XML

RSS Reader in Windows System Tray

Rate me:
Please Sign up or sign in to vote.
4.42/5 (21 votes)
14 Dec 20062 min read 235.2K   1.5K   37  
A simple RSS reader that is very easy to use. It runs in the system tray and gives a notification(a bubble) when a new RSS piece is found.
/////////////////////////
//infoBot.net
//author: Xiaozheng Chen
//modified:  Dec 15, 2006
//chenxiaozheng@gmail.com
////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace infoBot
{
    public interface IConfigurable
    {
        void CreateFromXML(XmlNode node);
        void SaveToXML(XmlDocument doc, XmlNode node);
        ToolStripMenuItem GenerateMenuItem();
    }
    class RssGroup : IConfigurable
    {
        public string name;
        public string head;
        public string link;
        RssManager mgr;
        ToolStripMenuItem menu;
        public List<RssPiece> rsses = new List<RssPiece>();
        public RssGroup(RssManager mgr)
        {
            this.mgr = mgr;
        }
        public RssGroup(RssManager mgr, string siteName, string rssHead, string siteLink)
        {
            this.mgr = mgr;
            name = siteName;
            head = rssHead;
            link = siteLink;
        }
        public void CreateFromXML(XmlNode node)
        {
            foreach (XmlNode chd in node.ChildNodes)
            {
                string chdname = chd.Name;
                if (chdname == "item")
                {
                    RssPiece rss = new RssPiece(this);
                    rss.CreateFromXML(chd);
                    rsses.Add(rss);
                }
                else if (chdname == "title")
                {
                    name = chd.InnerText;
                }
                else if (chdname == "rsshead")
                {
                    head = chd.InnerText;
                }
                else if (chdname == "link")
                {
                    link = chd.InnerText;
                }
            }
        }
        public void SaveToXML(XmlDocument doc, XmlNode node)
        {
            XmlNode nd = doc.CreateElement("title");
            nd.InnerText = name;
            node.AppendChild(nd);
            nd = doc.CreateElement("link");
            nd.InnerText = link;
            node.AppendChild(nd);
            nd = doc.CreateElement("rsshead");
            nd.InnerText = head;
            node.AppendChild(nd);

            foreach (RssPiece rss in rsses)
            {
                nd = doc.CreateElement("item");
                rss.SaveToXML(doc, nd);
                node.AppendChild(nd);
            }
        }
        
        public ToolStripMenuItem GenerateMenuItem()
        {
            menu = new ToolStripMenuItem(name);
            foreach (RssPiece rss in rsses)
            {
                menu.DropDownItems.Add(rss.GenerateMenuItem());
            }
           // menu.Click += new EventHandler(HandleClick);
            ToolStripTextBox box = new ToolStripTextBox();
            box.Text = "New...";
            box.Click += new EventHandler(OnClickNew);
            box.KeyDown += new KeyEventHandler(OnFinishEntering);
            box.LostFocus += new EventHandler(OnLeaveEnter);
            menu.DropDownItems.Add(box);             
            return menu;
        }


        public void OnLeaveEnter(object o, EventArgs e)
        {
            ((ToolStripTextBox)o).Text = " New...";
        }

        public void OnClickNew(object o, EventArgs e)
        {
            ((ToolStripTextBox)o).Text = "";
        }

        private string ProcessURL(string name)
        {
            int starpos = head.IndexOf("*");
            if (starpos < 0)
            {
                return (head + name);
            }
            string preRss = head.Substring(0, starpos);
            string postRss = head.Substring(starpos + 1, head.Length - starpos - 1);
            return (preRss + name + postRss);
        }


        public void OnFinishEntering(object o, EventArgs e)
        {
            if (((KeyEventArgs)e).KeyCode == Keys.Enter)
            {
                ToolStripTextBox box = (ToolStripTextBox)o;
                string rssname = box.Text.Trim();
                RssPiece rss = new RssPiece(rssname, ProcessURL(rssname), this);
                rss.SetHandlers(new RssPiece.DelEventHandler(mgr.HandleRssDeleted),
                       new RssPiece.AddEventHandler(mgr.HandleRssAdded));
                rsses.Add(rss);               
                menu.DropDownItems.Insert(menu.DropDownItems.Count - 1, 
                    rss.GenerateMenuItem());
                box.Text = "";                
            }
        }
    }
    class RssPiece : IConfigurable
    {
        public string name;
        public string link;
        public bool chkflg;
        public RssGroup group;
        public delegate void DelEventHandler(RssPiece rss);
        public delegate void AddEventHandler(RssGroup group, RssPiece rss);
        public DelEventHandler delHandler;
        public AddEventHandler addHandler;
        private ToolStripMenuItem menu = null;

        public RssPiece(RssGroup group)
        {
            this.group = group;
        }
        public void SetHandlers(DelEventHandler del, AddEventHandler add)
        {
            delHandler = del;
            addHandler = add;
        }
        public RssPiece(string rssName, string rssLink, RssGroup group)
        {
            name = rssName;
            link = rssLink;
            chkflg = false;
            this.group = group;
        }
        public void CreateFromXML(XmlNode node)
        {
            RssPiece retval = new RssPiece(null);

            foreach (XmlNode chd in node.ChildNodes)
            {
                if (chd.Name == "name")
                    name = chd.InnerText.Trim();
                else if (chd.Name == "checked")
                {
                    chkflg = chd.InnerText.Trim().Equals("1");
                }
                else if (chd.Name == "link")
                {
                    link = chd.InnerText.Trim();
                }
            }
        }
        public void SaveToXML(XmlDocument doc, XmlNode node)
        {
            XmlNode nd = doc.CreateElement("name");
            nd.InnerText = name;
            node.AppendChild(nd);
            nd = doc.CreateElement("checked");
            nd.InnerText = chkflg ? "1" : "0";
            node.AppendChild(nd);
            nd = doc.CreateElement("link");
            nd.InnerText = link;
            node.AppendChild(nd);
        }
        private void HandleClick(object o, EventArgs a)
        {
            chkflg = !chkflg;
            menu.Checked = chkflg;
            if (chkflg)
            {
                System.Diagnostics.Debug.Assert(group != null);
                addHandler(group, this);
            }
            else
            {
                delHandler(this);
            }
        }
        public ToolStripMenuItem GenerateMenuItem()
        {
            menu = new ToolStripMenuItem(name);
            menu.Click += new EventHandler(HandleClick);
            menu.Checked = chkflg;
            menu.DisplayStyle = ToolStripItemDisplayStyle.Text;
            menu.MouseDown += new MouseEventHandler(menu_MouseDown);
            //menu.DragLeave += new EventHandler(UserErase);
            menu.MouseDown += new MouseEventHandler(UserMouseDown);
            menu.MouseUp += new MouseEventHandler(UserMouseUp);
            menu.MouseLeave += new EventHandler(UserMouseLeave);
//            menu.DO
            return menu;
        }

        private bool _dragFlg = false;

        private void UserMouseLeave(object o, EventArgs a)
        {
            if (_dragFlg)
            {
                //System.Diagnostics.Debug.WriteLine(this.name + "  deleted ");
                delHandler(this);
                group.rsses.Remove(this);
                menu.Owner.Items.Remove(menu);
                _dragFlg = false;
            }
        }

        private void UserMouseDown(object o, MouseEventArgs e)
        {
            _dragFlg = true;
        }

        private void UserMouseUp(object o, MouseEventArgs e)
        {
            _dragFlg = false;
        }

      
        void menu_MouseDown(object sender, MouseEventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
China China
the world is a vampire...

Comments and Discussions