Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / Visual Basic

RSS Feed Aggregator and Blogging Smart Client

Rate me:
Please Sign up or sign in to vote.
4.91/5 (85 votes)
16 Aug 2005CPOL52 min read 1.1M   2.4K   397  
RSS Feed aggregator and blogging Smart Client which uses Enterprise Library, Updater Application Block, lots of XML hacks and desktop tricks. A comprehensive guide to real life hurdles of Smart Client development.
// Copyright � 2005 by Omar Al Zabir. All rights are reserved.
// 
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
// 
// website http://www.oazabir.com, email OmarAlZabir@gmail.com, msn oazabir@hotmail.com

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;


namespace RSSFeeder
{
	using RSSCommon;
	using RSSCommon.Helper;
	using Helpers;

	/// <summary>
	/// Summary description for AddNewChannel.
	/// </summary>
	public class AddNewChannel : System.Windows.Forms.Form
	{
		#region Designer

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox urlTextBox;
		private System.Windows.Forms.Button downloadButton;
		private System.Windows.Forms.GroupBox previewGroupBox;
		private System.Windows.Forms.Button addChannelButton;
		private System.Windows.Forms.Label progressLabel;
		private System.Windows.Forms.Button closeButton;
		private System.Windows.Forms.CheckBox outlookFolderAutoCheckBox;
		private System.Windows.Forms.Label customFolderLabel;
		private System.Windows.Forms.TextBox customFolderTextBox;
		private System.Windows.Forms.Button selectFolderButton;
		private System.Windows.Forms.GroupBox configurationGroupBox;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox proxyNameTextBox;
		private System.Windows.Forms.TextBox portTextBox;
		private System.Windows.Forms.TreeView channelsTree;
		private System.Windows.Forms.ImageList icons;
		private System.Windows.Forms.TextBox proxyUserNameTextBox;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox proxyPasswordTextBox;
		private System.Windows.Forms.Label label3;
		private System.ComponentModel.IContainer components;

		public AddNewChannel()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(AddNewChannel));
			this.label1 = new System.Windows.Forms.Label();
			this.urlTextBox = new System.Windows.Forms.TextBox();
			this.downloadButton = new System.Windows.Forms.Button();
			this.previewGroupBox = new System.Windows.Forms.GroupBox();
			this.channelsTree = new System.Windows.Forms.TreeView();
			this.icons = new System.Windows.Forms.ImageList(this.components);
			this.selectFolderButton = new System.Windows.Forms.Button();
			this.customFolderTextBox = new System.Windows.Forms.TextBox();
			this.customFolderLabel = new System.Windows.Forms.Label();
			this.outlookFolderAutoCheckBox = new System.Windows.Forms.CheckBox();
			this.addChannelButton = new System.Windows.Forms.Button();
			this.progressLabel = new System.Windows.Forms.Label();
			this.closeButton = new System.Windows.Forms.Button();
			this.configurationGroupBox = new System.Windows.Forms.GroupBox();
			this.proxyPasswordTextBox = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.proxyUserNameTextBox = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.portTextBox = new System.Windows.Forms.TextBox();
			this.proxyNameTextBox = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.previewGroupBox.SuspendLayout();
			this.configurationGroupBox.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.TabIndex = 0;
			this.label1.Text = "Channel URL:";
			// 
			// urlTextBox
			// 
			this.urlTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.urlTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.urlTextBox.Location = new System.Drawing.Point(104, 8);
			this.urlTextBox.Name = "urlTextBox";
			this.urlTextBox.Size = new System.Drawing.Size(432, 21);
			this.urlTextBox.TabIndex = 1;
			this.urlTextBox.Text = "";
			// 
			// downloadButton
			// 
			this.downloadButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.downloadButton.Location = new System.Drawing.Point(464, 32);
			this.downloadButton.Name = "downloadButton";
			this.downloadButton.TabIndex = 2;
			this.downloadButton.Text = "Download";
			this.downloadButton.Click += new System.EventHandler(this.downloadButton_Click);
			// 
			// previewGroupBox
			// 
			this.previewGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.previewGroupBox.Controls.Add(this.channelsTree);
			this.previewGroupBox.Controls.Add(this.selectFolderButton);
			this.previewGroupBox.Controls.Add(this.customFolderTextBox);
			this.previewGroupBox.Controls.Add(this.customFolderLabel);
			this.previewGroupBox.Controls.Add(this.outlookFolderAutoCheckBox);
			this.previewGroupBox.Enabled = false;
			this.previewGroupBox.Location = new System.Drawing.Point(16, 168);
			this.previewGroupBox.Name = "previewGroupBox";
			this.previewGroupBox.Size = new System.Drawing.Size(520, 264);
			this.previewGroupBox.TabIndex = 3;
			this.previewGroupBox.TabStop = false;
			this.previewGroupBox.Text = "Channels(s)";
			// 
			// channelsTree
			// 
			this.channelsTree.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.channelsTree.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.channelsTree.CheckBoxes = true;
			this.channelsTree.FullRowSelect = true;
			this.channelsTree.HideSelection = false;
			this.channelsTree.ImageList = this.icons;
			this.channelsTree.Location = new System.Drawing.Point(8, 17);
			this.channelsTree.Name = "channelsTree";
			this.channelsTree.Size = new System.Drawing.Size(504, 183);
			this.channelsTree.TabIndex = 10;
			// 
			// icons
			// 
			this.icons.ImageSize = new System.Drawing.Size(16, 16);
			this.icons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("icons.ImageStream")));
			this.icons.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// selectFolderButton
			// 
			this.selectFolderButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.selectFolderButton.Enabled = false;
			this.selectFolderButton.Location = new System.Drawing.Point(472, 232);
			this.selectFolderButton.Name = "selectFolderButton";
			this.selectFolderButton.Size = new System.Drawing.Size(24, 21);
			this.selectFolderButton.TabIndex = 9;
			this.selectFolderButton.Text = "...";
			this.selectFolderButton.Click += new System.EventHandler(this.selectFolderButton_Click);
			// 
			// customFolderTextBox
			// 
			this.customFolderTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.customFolderTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.customFolderTextBox.Enabled = false;
			this.customFolderTextBox.Location = new System.Drawing.Point(152, 232);
			this.customFolderTextBox.Name = "customFolderTextBox";
			this.customFolderTextBox.ReadOnly = true;
			this.customFolderTextBox.Size = new System.Drawing.Size(320, 21);
			this.customFolderTextBox.TabIndex = 8;
			this.customFolderTextBox.Text = "";
			// 
			// customFolderLabel
			// 
			this.customFolderLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.customFolderLabel.AutoSize = true;
			this.customFolderLabel.Location = new System.Drawing.Point(16, 232);
			this.customFolderLabel.Name = "customFolderLabel";
			this.customFolderLabel.Size = new System.Drawing.Size(131, 17);
			this.customFolderLabel.TabIndex = 7;
			this.customFolderLabel.Text = "Specifically Create under:";
			// 
			// outlookFolderAutoCheckBox
			// 
			this.outlookFolderAutoCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.outlookFolderAutoCheckBox.Checked = true;
			this.outlookFolderAutoCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
			this.outlookFolderAutoCheckBox.Location = new System.Drawing.Point(16, 208);
			this.outlookFolderAutoCheckBox.Name = "outlookFolderAutoCheckBox";
			this.outlookFolderAutoCheckBox.Size = new System.Drawing.Size(472, 24);
			this.outlookFolderAutoCheckBox.TabIndex = 6;
			this.outlookFolderAutoCheckBox.Text = "Automatically create under: {0}";
			this.outlookFolderAutoCheckBox.CheckedChanged += new System.EventHandler(this.outlookFolderAutoCheckBox_CheckedChanged);
			// 
			// addChannelButton
			// 
			this.addChannelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.addChannelButton.Enabled = false;
			this.addChannelButton.Location = new System.Drawing.Point(376, 440);
			this.addChannelButton.Name = "addChannelButton";
			this.addChannelButton.TabIndex = 6;
			this.addChannelButton.Text = "Add Channel";
			this.addChannelButton.Click += new System.EventHandler(this.addChannelButton_Click);
			// 
			// progressLabel
			// 
			this.progressLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.progressLabel.Location = new System.Drawing.Point(40, 56);
			this.progressLabel.Name = "progressLabel";
			this.progressLabel.Size = new System.Drawing.Size(496, 40);
			this.progressLabel.TabIndex = 4;
			this.progressLabel.Text = "Type or Copy-paste address manually or drag && drop a link or an RSS/XML icon her" +
				"e.";
			// 
			// closeButton
			// 
			this.closeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.closeButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.closeButton.Location = new System.Drawing.Point(464, 440);
			this.closeButton.Name = "closeButton";
			this.closeButton.TabIndex = 7;
			this.closeButton.Text = "Cancel";
			this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
			// 
			// configurationGroupBox
			// 
			this.configurationGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.configurationGroupBox.Controls.Add(this.proxyPasswordTextBox);
			this.configurationGroupBox.Controls.Add(this.label3);
			this.configurationGroupBox.Controls.Add(this.proxyUserNameTextBox);
			this.configurationGroupBox.Controls.Add(this.label2);
			this.configurationGroupBox.Controls.Add(this.portTextBox);
			this.configurationGroupBox.Controls.Add(this.proxyNameTextBox);
			this.configurationGroupBox.Controls.Add(this.label5);
			this.configurationGroupBox.Controls.Add(this.label4);
			this.configurationGroupBox.Location = new System.Drawing.Point(16, 88);
			this.configurationGroupBox.Name = "configurationGroupBox";
			this.configurationGroupBox.Size = new System.Drawing.Size(520, 72);
			this.configurationGroupBox.TabIndex = 8;
			this.configurationGroupBox.TabStop = false;
			this.configurationGroupBox.Text = "Configuration";
			// 
			// proxyPasswordTextBox
			// 
			this.proxyPasswordTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.proxyPasswordTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.proxyPasswordTextBox.Location = new System.Drawing.Point(376, 46);
			this.proxyPasswordTextBox.Name = "proxyPasswordTextBox";
			this.proxyPasswordTextBox.Size = new System.Drawing.Size(128, 21);
			this.proxyPasswordTextBox.TabIndex = 7;
			this.proxyPasswordTextBox.Text = "";
			this.proxyPasswordTextBox.TextChanged += new System.EventHandler(this.proxyPasswordTextBox_TextChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(298, 48);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 16);
			this.label3.TabIndex = 6;
			this.label3.Text = "Password:";
			// 
			// proxyUserNameTextBox
			// 
			this.proxyUserNameTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.proxyUserNameTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.proxyUserNameTextBox.Location = new System.Drawing.Point(96, 46);
			this.proxyUserNameTextBox.Name = "proxyUserNameTextBox";
			this.proxyUserNameTextBox.Size = new System.Drawing.Size(152, 21);
			this.proxyUserNameTextBox.TabIndex = 5;
			this.proxyUserNameTextBox.Text = "";
			this.proxyUserNameTextBox.TextChanged += new System.EventHandler(this.proxyUserNameTextBox_TextChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 4;
			this.label2.Text = "User Name:";
			// 
			// portTextBox
			// 
			this.portTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.portTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.portTextBox.Location = new System.Drawing.Point(424, 19);
			this.portTextBox.Name = "portTextBox";
			this.portTextBox.Size = new System.Drawing.Size(80, 21);
			this.portTextBox.TabIndex = 3;
			this.portTextBox.Text = "";
			this.portTextBox.TextChanged += new System.EventHandler(this.portTextBox_TextChanged);
			// 
			// proxyNameTextBox
			// 
			this.proxyNameTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.proxyNameTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.proxyNameTextBox.Location = new System.Drawing.Point(96, 19);
			this.proxyNameTextBox.Name = "proxyNameTextBox";
			this.proxyNameTextBox.Size = new System.Drawing.Size(248, 21);
			this.proxyNameTextBox.TabIndex = 2;
			this.proxyNameTextBox.Text = "";
			this.proxyNameTextBox.TextChanged += new System.EventHandler(this.proxyNameTextBox_TextChanged);
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(384, 19);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(40, 23);
			this.label5.TabIndex = 1;
			this.label5.Text = "Port:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 16);
			this.label4.Name = "label4";
			this.label4.TabIndex = 0;
			this.label4.Text = "Proxy Name/IP:";
			// 
			// AddNewChannel
			// 
			this.AllowDrop = true;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.CancelButton = this.closeButton;
			this.ClientSize = new System.Drawing.Size(552, 470);
			this.Controls.Add(this.downloadButton);
			this.Controls.Add(this.configurationGroupBox);
			this.Controls.Add(this.closeButton);
			this.Controls.Add(this.progressLabel);
			this.Controls.Add(this.urlTextBox);
			this.Controls.Add(this.previewGroupBox);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.addChannelButton);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "AddNewChannel";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Add New Channel";
			this.Load += new System.EventHandler(this.AddNewChannel_Load);
			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.AddNewChannel_DragDrop);
			this.DragEnter += new System.Windows.Forms.DragEventHandler(this.AddNewChannel_DragEnter);
			this.previewGroupBox.ResumeLayout(false);
			this.configurationGroupBox.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#endregion

		#region Private Variables

		private IList _Channels; 

		private string _ProxyName = string.Empty;
		private string _ProxyPort = string.Empty;
		private string _ProxyUserName = string.Empty;
		private string _ProxyUserPassword = string.Empty;

		#endregion

		#region Private Methods

		/// <summary>
		/// Download feed from URL and show in the preview pane
		/// </summary>
		private void DownloadFeed()
		{
			try
			{
				progressLabel.Text = "Downloading...";
				progressLabel.Visible = true;
				
				base.Cursor = Cursors.AppStarting;

				Uri uri = null;
				try
				{
					uri = new Uri( urlTextBox.Text );
				}
				catch
				{
					if( !urlTextBox.Text.ToLower().StartsWith("http://") )
					{
						urlTextBox.Text = "http://" + urlTextBox.Text;
						uri = new Uri( urlTextBox.Text );
					}
				}

				if( null != uri )
				{
					this.downloadButton.Enabled = false;
			
					// Start a background thread to download the URL
					ThreadPool.QueueUserWorkItem( new WaitCallback( this.DownloadURL ), uri );
				}
			}
			catch( Exception x )
			{
				MessageBox.Show( this, "Error occured while downloading." + Environment.NewLine
					+ x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );

				progressLabel.Text = x.Message;
			}
		}

		/// <summary>
		/// Download the specified URL in background
		/// </summary>
		/// <param name="state"></param>
		private void DownloadURL( object state )
		{
			Uri uri = state as Uri;

			try
			{
				IList channels;
				IList channelSources;

				RSSHelper.DownloadFeeds( uri, _ProxyName, int.Parse( _ProxyPort ), 
					_ProxyUserName, 
					RSSCommon.PropertyEditor.PasswordEditor.PasswordProvider.Encrypt( _ProxyUserPassword ),
					out channels, out channelSources, 
					new ProgressEventHandler( this.DownloadProgress ) );

				// If the URI returns an HTML, then we should have a source of channels
				if( channelSources.Count > 0 )
					this._Channels = channelSources;
				else	// Otherwise we will get the RSS channel(s)
					this._Channels = channels;

				this.Invoke( new MethodInvoker( this.ShowChannel ) );
				
			}
			catch( Exception x )
			{
				this.Invoke( new WaitCallback( SetError ) , new object[] { x } );
			}
			finally
			{							
				this.Cursor = Cursors.Default;
				this.downloadButton.Enabled = true;
			}
		}

		private void DownloadProgress( object sender, ProgressEventArgs e )
		{
			if( base.InvokeRequired )
			{
				this.Invoke( new ProgressEventHandler( DownloadProgress ), new object[] { sender, e } );
			}
			else
			{
				this.progressLabel.Text = e.Message;
			}
		}

		private void SetError( object exception )
		{
			if( exception is System.Xml.XmlException )
			{
				this.progressLabel.Text = "Xml Exception. The URL may not be an RSS Feed.";
				this.progressLabel.ForeColor = Color.Red;
			}
			else if( exception is System.IO.IOException )
			{
				this.progressLabel.Text = "Connection problem.";
				this.progressLabel.ForeColor = Color.Red;
			}
			else
			{
				Exception x = exception as Exception;
				this.progressLabel.Text = x.Message;
				this.progressLabel.ForeColor = Color.Red;
			}

			Exception e = exception as Exception;
			MessageBox.Show( this, this.progressLabel.Text + Environment.NewLine + e.Message, "Error",
				MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
		}
		/// <summary>
		/// Show downloaded channel feeds
		/// </summary>
		/// <param name="channel"></param>
		private void ShowChannel( )
		{
			previewGroupBox.Enabled = true;

			channelsTree.Nodes.Clear();

			foreach( object item in this._Channels )
			{
				// if the item is a RSSChannel then add the channel as root node and 
				// all the downloaded items as child node
				if( item is RssChannel )
				{
					RssChannel channel = item as RssChannel;
					TreeNode channelNode = new TreeNode( channel.Title, 0, 0 );

					channelNode.Tag = channel;
					channelNode.Checked = true;
					
					foreach( RssFeed rssItem in channel.Feeds )
					{
						TreeNode itemNode = new TreeNode( rssItem.Title, 1, 1 );
						channelNode.Nodes.Add( itemNode );
						itemNode.Checked = true;
					}

					channelNode.ExpandAll();

					channelsTree.Nodes.Add( channelNode );
				}
				else
				{
					string [] channelSource = item as string[];

					TreeNode channelNode = new TreeNode( channelSource[0], 0, 0 );
					
					// Some people just put the file name like "rss.xml". So, we need to construct a full
					// Uri from it
					string url = channelSource[1];
					try
					{
						Uri uri = new Uri( url );
						channelNode.Tag = uri.ToString();
					}
					catch
					{
						// See, I told you, some people are just too lazy
						Uri baseUri = new Uri( urlTextBox.Text );
						
						string fullUri = baseUri.ToString();
						string path = fullUri.Substring(0, fullUri.LastIndexOf('/'));
						channelNode.Tag = path + "/" + channelSource[1];
					}

					

					channelNode.Checked = true;
					channelsTree.Nodes.Add( channelNode );
				}
			}
			
			//this._Channel = channel;

			this.downloadButton.Enabled = false;
			this.addChannelButton.Enabled = true;
		}

		/// <summary>
		/// Add the downloaded channel to the channel collection
		/// </summary>
		private void AddChannel( )
		{
			if( Configuration.Instance.SyncWithOutlook )
			{
				// Ensure a proper path is selected
				if( !outlookFolderAutoCheckBox.Checked )
				{
					// user does not want auto folder creation. A custom folder name must be specified
					if( 0 == customFolderTextBox.Text.Length )
					{
						MessageBox.Show( this, "Please select a proper outlook folder as a base folder for the new channels", "Error", MessageBoxButtons.OK, 
							MessageBoxIcon.Exclamation );
						return;
					}
				}
			}

			IList channelsAdded = new ArrayList();

			foreach( TreeNode node in channelsTree.Nodes )
			{
				if( node.Checked )
				{
					string title;
					string url;
				
					if( node.Tag is RssChannel )
					{
						RssChannel rssChannel = node.Tag as RssChannel;
						title = rssChannel.Title;
						url = urlTextBox.Text;
					}
					else
					{
						title = node.Text;
						url = node.Tag as string;
					}

					Channel channel = new Channel( 
						0, // ID 
						title, // Title
						new Uri( url ), // FeedURL
						new TimeSpan( 0, Configuration.Instance.DefaultDownloadFrequency, 0), // From minutes
						DateTime.Now, // Last Updated 
						DateTime.Now.AddMinutes( Configuration.Instance.DefaultDownloadFrequency ), // Next Update
						DateTime.MinValue, // Last Update in outlook
						customFolderTextBox.Text, // Folder Path
						string.Empty, // XSL Path
						true, // Show in newspaper
						0, // Item count
						0,  // Unread Count
						( outlookFolderAutoCheckBox.Checked ? 
						Channel.FolderCreationTypeEnum.Automatic_under_Base_Folder 
						: Channel.FolderCreationTypeEnum.Specific_Folder ),  // Folder Type
						string.Empty, // View Xml Path
						string.Empty // Outlook XSL 
						);

					// Create a new channel
					if( DatabaseHelper.AddNewChannel( ref channel ) )
					{
						// For for total sync
						channelsAdded.Add( channel );
					}
				}			
			}

			// Download newly added channels
			RSSHelper.DownloadChannels( this, channelsAdded, this.OnDownloadComplete );

			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void SelectOutlookFolder()
		{
			string path = OutlookHelper.SelectFolderPath();
			
			if( null == path )
			{
				// No folder selected	
			}
			else
			{
				customFolderTextBox.Text = path;
			}

			base.Activate();
		}

		#endregion

		#region Properties
		
		public EventHandler OnDownloadComplete;

		private string _Url;

		public string Url
		{
			get { return this._Url; }
			set { this._Url = value; }			
		}

		#endregion

		#region Control Events
		
		private void AddNewChannel_Load(object sender, System.EventArgs e)
		{
			urlTextBox.Text = this._Url;

			customFolderTextBox.Text = outlookFolderAutoCheckBox.Text = string.Format( outlookFolderAutoCheckBox.Text,
				Configuration.Instance.OutlookBaseFolder );

			proxyNameTextBox.Text = Configuration.Instance.ProxyName;
			portTextBox.Text = Configuration.Instance.ProxyPort.ToString();
			proxyUserNameTextBox.Text = Configuration.Instance.ProxyUserName;
			proxyPasswordTextBox.Text = Configuration.Instance.ProxyUserPassword;
		}

		private void AddNewChannel_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			BrowserHelper.DragEnter( e );
		}

		private void AddNewChannel_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			string hyperLinkUrl = BrowserHelper.GetDroppedURL( e );
			if( null == hyperLinkUrl || 0 == hyperLinkUrl.Length )
				return;

			this.urlTextBox.Text = hyperLinkUrl;
		}

		private void proxyNameTextBox_TextChanged(object sender, System.EventArgs e)
		{
			this._ProxyName = proxyNameTextBox.Text;
		}

		private void portTextBox_TextChanged(object sender, System.EventArgs e)
		{
			this._ProxyPort = portTextBox.Text;
		}

		private void proxyUserNameTextBox_TextChanged(object sender, System.EventArgs e)
		{
			this._ProxyUserName = proxyUserNameTextBox.Text;
		}

		private void proxyPasswordTextBox_TextChanged(object sender, System.EventArgs e)
		{
			this._ProxyUserPassword = proxyPasswordTextBox.Text;
		}


		private void selectFolderButton_Click(object sender, System.EventArgs e)
		{
			this.SelectOutlookFolder();
			base.Activate();
			base.Focus();
		}

		private void outlookFolderAutoCheckBox_CheckedChanged(object sender, System.EventArgs e)
		{
			selectFolderButton.Enabled = customFolderTextBox.Enabled = !outlookFolderAutoCheckBox.Checked;
		}

		private void closeButton_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
		private void downloadButton_Click(object sender, System.EventArgs e)
		{
			this.DownloadFeed();
		}

		
		private void addChannelButton_Click(object sender, System.EventArgs e)
		{
			this.AddChannel();		
		}

		#endregion
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions