Click here to Skip to main content
15,885,914 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.Collections;
using System.Drawing;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Drawing.Design;

namespace RSSBlogAPI
{
	using RSSCommon.PropertyEditor;

	public enum EngineEnum
	{
		DotText,
		Wordpress,
		B2Evolution,
		CommunityServer,
		Drupal
	}
		
	[Serializable][XmlRoot("weblog")]
	public class WebLog
	{
		#region Events

		///<summary>A PropertyChanged event is raised when a property is changed on a component. A PropertyChangedEventArgs object specifies the name of the property that changed.</summary>
		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

		#region Private Fields

		private string _BlogId = string.Empty;
		private string _UserName = string.Empty;
		private string _Password = string.Empty;
		private string _ServiceUrl = string.Empty;
		private string _Server = string.Empty;
		private bool _IsValidated = false;
		private EngineEnum _Engine = EngineEnum.DotText;
		private Category [] _Categories;
		private string _OutlookFolder;
		private bool _SendPasswordHashed = false;
		private ArrayList _Posts = new ArrayList();
		private string _Title = "New Weblog";

		#endregion

		#region Constructors

		public WebLog()
			: this( string.Empty, string.Empty, string.Empty, string.Empty, false, EngineEnum.DotText)
		{
		}

		public WebLog( string blogId, string userName, string password, string serviceUrl, bool validated, EngineEnum engine )
		{
			_BlogId = blogId;
			_UserName = userName;
			_Password = password;
			_ServiceUrl = serviceUrl;
			_IsValidated = validated;
			_Engine = engine;
		}

		#endregion

		#region Public Properties
		
		[Category("Post"), Browsable(false), 
		Description(@"Pending posts in queue")]
		[XmlArray("posts"), XmlArrayItem("post", typeof(Post)) ]
		public ArrayList Posts
		{
			get { return _Posts;  }	
			set 
			{
				bool changed = !object.Equals(this._Posts, value);
				this._Posts = value;
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Posts));
			}
		}

		[Category("Outlook"), Browsable(true), 
		Description(@"Outlook pickup folder. Items posted in this folder will be posted to weblog")]
		[XmlElement("outlookFolder")]
		[Editor(typeof(UIOutlookFolderPickerEditor), typeof(UITypeEditor))]
		public string OutlookFolder
		{
			get { return _OutlookFolder;  }	
			set 
			{
				bool changed = !object.Equals(this._OutlookFolder, value);
				this._OutlookFolder = value;
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.OutlookFolder));
			}
		}

		[Category("Post"), Browsable(true), ReadOnly(true),
		Description(@"Categories retrieved from the server")]
		[XmlElement("categories")]
		public Category[] Categories
		{
			get {
 
				if( null == _Categories ) _Categories= new Category[0];
				return _Categories; 
			}	
			set 
			{
				//bool changed = !object.Equals(this._Categories, value);
				this._Categories = value;
				//if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Categories));
			}
		}

		[Category("Blog"), DefaultValue(""), Browsable(true),
		Description(@"Your blog ID. For example, if your blog URL is www.blogger.com/myblog then the id will be 'myblog'. If your blog server is B2Evolution, then the blog ID will be a number.")]
		[XmlElement("blogId")]
		public string BlogId
		{
			get { return _BlogId;  }	
			set 
			{
				bool changed = !object.Equals(this._BlogId, value);
				this._BlogId = value;
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.BlogId));
			}
		}

		[Category("Credentials"), DefaultValue(false), Browsable(true),
		Description(@"If you are having login problem, set it to true or false. Some blog servers require this to be set true and some may not require")]
		[XmlElement("sendPasswordHashed")]
		public bool SendPasswordHashed
		{
			get { return _SendPasswordHashed;  }	
			set
			{
				bool changed = !object.Equals(this._SendPasswordHashed, value);
				this._SendPasswordHashed = value;
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.SendPasswordHashed));
			}
		}

		[Category("Account"), DefaultValue("New Weblog"), Browsable(true),
		Description(@"Title of this weblog which identifies it in the list")]
		[XmlElement("title")]
		public string Title
		{
			get { return _Title;  }	
			set
			{
				bool changed = !object.Equals(this._Title, value);
				this._Title = value;
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Title));
			}
		}

		[Category("Credentials"), DefaultValue(""), Browsable(true),
		Description(@"User account name. eg. myusername")]
		[XmlElement("userName")]
		public string UserName
		{
			get { return _UserName;  }	
			set
			{
				bool changed = !object.Equals(this._UserName, value);
				this._UserName = value;
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.UserName));
			}
		}

		[Category("Credentials"), DefaultValue(""), Browsable(true), 
		Description(@"DO NOT TYPE, CLICK THE '...' BUTTON")]
		[XmlElement("password")]
		[Editor(typeof(PasswordEditor), typeof(UITypeEditor))]
		public string Password
		{
			get { return _Password;  }	
			set
			{
				bool changed = !object.Equals(this._Password, value);
				if( this.ValidPassword( value ) )
				{
					this._Password = value;					
				}
				else
				{
					this._Password = "Don't Type. Click '...'";
				}
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Password));
			}
		}

		[Category("Server"), DefaultValue(""), Browsable(true),
		Description(@"URL to the blog service webservice/page which takes input. eg. http://www.YOURSERVER.com/YOURBLOGNAME/Services/SimpleBlogService.asmx")]
		[XmlElement("serviceUrl")]
		public string ServiceUrl
		{
			get { return _ServiceUrl;  }	
			set
			{
				bool changed = !object.Equals(this._ServiceUrl, value);
				this._ServiceUrl = value;
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.ServiceUrl));
			}
		}

		[Category("Server"), DefaultValue(""), Browsable(true),
		Description(@"Server name. eg. http://www.blogger.com")]
		[XmlElement("server")]
		public string Server
		{
			get { return _Server;  }	
			set
			{
				bool changed = !object.Equals(this._Server, value);
				this._Server = value;

				if( !this._Server.StartsWith("http://") )
					this._Server = "http://" + this._Server;
				if( !this._Server.EndsWith("/") )
					this._Server += "/";

				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Server));
			}
		}

		[Category("Server"), Browsable(true),
		Description(@"The Type of engine your blog provider is using")]
		[XmlElement("engine")]
		public EngineEnum Engine
		{
			get { return _Engine;  }	
			set
			{
				bool changed = !object.Equals(this._Engine, value);
				this._Engine = value;
				if(changed) OnPropertyChanged(new PropertyChangedEventArgs(Properties.Engine));
			}
		}

		#endregion

		#region Private Methods

		///<summary>A PropertyChanged event is raised when a property is 
		///changed on a component. A PropertyChangedEventArgs object 
		///specifies the name of the property that changed.</summary>
		protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
		{	
			this.MakeUrl();
			this.CheckProperties();
			if( null != PropertyChanged )
			{
				PropertyChanged(this, e);			
			}
		}

		private bool ValidPassword( string password )
		{
			// No password
			if( null == password || 0 == password.Length ) return true;

			// Password decryption provider is still not available
			if( null == RSSCommon.PropertyEditor.PasswordEditor.PasswordProvider ) return true;
			try
			{
				// If password decryption fails, surely user has manually typed the password
				// inside the property dialog.
				string decryptedPassword = 
					RSSCommon.PropertyEditor.PasswordEditor.PasswordProvider.Decrypt( password );
				return ( null != decryptedPassword && decryptedPassword.Length > 0);
			}
			catch
			{
				return false;
			}
		}

		private void CheckProperties()
		{
			if( EngineEnum.B2Evolution == this._Engine )
			{
				if( this._BlogId[0] < '0' || this._BlogId[0] > '9' )
				{
					this.BlogId = "1";
				}
			}
		}

		private void MakeUrl()
		{
			if( EngineEnum.DotText == this._Engine )
			{
				this.ServiceUrl = this.Server + this.BlogId + 
					"/services/SimpleBlogService.asmx";
			}
			else if( EngineEnum.Wordpress == this._Engine
				|| EngineEnum.Drupal == this._Engine )
			{
				this.ServiceUrl = this.Server + "xmlrpc.php";
			}
			else if( EngineEnum.B2Evolution == this._Engine )
			{
				this.ServiceUrl = this.Server + "xmlsrv/xmlrpc.php";
			}
			else if( EngineEnum.CommunityServer == this._Engine )
			{
				this.ServiceUrl = this.Server + "blogs/blogservice.asmx";
			}
		}
		#endregion
		
		#region Property Definition

		public class Properties
		{
			public const string Title = "Title";
			public const string Posts = "Posts";
			public const string SendPasswordHashed = "SendPasswordHashed";
			public const string OutlookFolder = "OutlookFolder";
			public const string Categories = "Categories";
			public const string BlogId = "BlogId";
			public const string UserName = "UserName";
			public const string Password = "Password";
			public const string ServiceUrl = "ServiceUrl";
			public const string IsValidated = "IsValidated";
			public const string Engine = "Engine";
			public const string Server = "Server";
		}

		#endregion

		#region Public Methods

		public void EnqueuePost( Post post ) 
		{
			this.Posts.Insert( 0, post );
			OnPropertyChanged(new PropertyChangedEventArgs(Properties.Posts));
		}

		public Category [] GetCategories( string categories )
		{
			// Build categories
			string [] categoryNames = categories.Trim().Split(',');
			ArrayList postCategories = new ArrayList();
			if( null != categoryNames )
			{
				foreach( string categoryName in categoryNames )
				{
					// Check if this category matches with the weblog's category list
					foreach( Category cat in this._Categories )
						if( cat.Title.ToLower() == categoryName.Trim().ToLower() )
							postCategories.Add( cat );
				}
			}
			// If we have found any valid match for category then use one
			if( postCategories.Count > 0 )
			{
				Category [] categoryArray = new Category[ postCategories.Count ];
				postCategories.CopyTo( categoryArray, 0 );

				return categoryArray;
			}
			else
			{
				// none found, use the first one as default
				if( this._Categories.Length > 0 )
				{
					Category [] categoryArray = new Category[ 1 ];
					categoryArray[ 0 ] = this._Categories[ 0 ];

					return categoryArray;
				}
				else
				{
					Category [] dummy = new Category[ 1 ];
					dummy[0] = new Category( 0, "1", "Announcement" );

					return dummy;
				}
			}
						
		}

		public override string ToString()
		{
			return this.UserName + " at " + this.Server + " ID: " + this.BlogId;
		}


		#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