Click here to Skip to main content
15,896,111 members
Articles / Programming Languages / C#

The Favalias Application

Rate me:
Please Sign up or sign in to vote.
4.92/5 (27 votes)
30 Sep 20037 min read 70.8K   2.6K   52  
Favalias application enables you to manage your favorites web sites in an XML file and to launch your favorites application using aliases. You can also make your own addins (in any .NET language) to call your own code.
// Jean-Christophe Magnon
// jcmag@yahoo.com
#region Copyright � 2003 The Favalias Group
/*
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not claim
 * that you wrote the original software. If you use this software in a product,
 * an acknowledgment in the product documentation is required, as shown here:
 *
 * Portions Copyright � 2003 The Favalias Group (http://sourceforge.net/projects/favalias).
 *
 * 2. Altered source versions must be plainly marked as such, and must not be 
 * misrepresented as being the original software.
 * 
 * 3. This notice may not be removed or altered from any source distribution.
*/
#endregion

#region Namespaces
using Favalias.Forms;
using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using Toub.Threading;
#endregion

namespace Favalias.Controllers
{
	/// <summary>
	/// Load favicons with WebRequest.
	/// </summary>
	public class FaviconsController
	{
		#region Fields
		
		private FormProgress _formProgress;
		private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
		private int _waitingRequests;
		private Hashtable _favicons;
		private FormFavalias _parent;
		private int _timeout;
		
		#endregion

		#region Initialization

		public FaviconsController(FormFavalias parent)
		{
			string timeout = System.Configuration.ConfigurationSettings.AppSettings["timeoutForLoadingFavicons"];
			_timeout = 15000; // 15 seconds by default
			if(timeout.Length != 0)
			{
				Regex myRegex = new Regex(@"\d{1,6}");
				if(myRegex.IsMatch(timeout))
					_timeout = Convert.ToInt32(timeout);
			}				
			this._parent = parent;
			deserializeFavicons();
		}

		#endregion

		#region Properties
		
		public Hashtable Favicons
		{
			get
			{
				return _favicons;
			}
		}

		#endregion

		#region Public Methods
		public void LoadFavicons()
		{
			try
			{
				_favicons = new Hashtable();
				_formProgress = new FormProgress();
				_formProgress.Show();
				XmlDocument doc = new XmlDocument();
				doc.Load(FavaliasController.userPref.favoritesFile);
				_waitingRequests = doc.SelectNodes("//favorite").Count;
				_formProgress.progressBar1.Maximum = _waitingRequests;

				XmlNodeList categories = doc.SelectNodes("favorites/category");	
				foreach(XmlNode node in categories)
				{
					recurseCategory(node);
					recurseFavorites(node);
				}
			}
			catch(Exception exc)
			{
				Debug.WriteLine(exc);
				if(_log.IsErrorEnabled) _log.Error(MethodBase.GetCurrentMethod().Name, exc);
			}
		}

		#endregion

		#region Private Methods
		/// <summary>
		/// Deserialize the Hashtable containing the _favicons (if _favicons.dat exists)
		/// </summary>
		private void deserializeFavicons()
		{
			try
			{
				_favicons = new Hashtable();
				BinaryFormatter formatter = new BinaryFormatter();
				string path = FavaliasController.appDataFolder + @"\_favicons.dat";
				if(File.Exists(path))
				{
					using(FileStream _formProgress = new FileStream(path, FileMode.Open))
					{
						_favicons = (Hashtable)formatter.Deserialize(_formProgress);
					}
				}
			}
			catch(Exception exc)
			{
				Debug.WriteLine(exc);
				if(_log.IsErrorEnabled) _log.Error(MethodBase.GetCurrentMethod().Name, exc);
			}
		}

		/// <summary>
		/// Serialize the Hashtable containing the _favicons in _favicons.dat
		/// </summary>
		private void serializeFavicons()
		{
			try
			{
				BinaryFormatter formatter = new BinaryFormatter();
				using(FileStream _formProgress = new FileStream(FavaliasController.appDataFolder + @"\_favicons.dat", FileMode.Create))
				{
					formatter.Serialize(_formProgress, _favicons);
				}
			}
			catch(Exception exc)
			{
				Debug.WriteLine(exc);
				if(_log.IsErrorEnabled) _log.Error(MethodBase.GetCurrentMethod().Name, exc);
			}
		}


		private void recurseCategory(XmlNode node)
		{
			XmlNodeList categories = node.SelectNodes("category");	
			foreach(XmlNode xnode in categories)
			{
				recurseCategory(xnode);
				recurseFavorites(xnode);
			}
		}

		private void recurseFavorites(XmlNode node)
		{
			XmlNodeList favorites = node.SelectNodes("favorite");	
			foreach(XmlNode xnode in favorites)
			{
				Uri completeUri = new Uri(xnode.InnerText);
				RequestState rs = new RequestState();
				rs.faviconName = xnode.Attributes["name"].Value;
				rs.faviconUri = "http://" + completeUri.Host + "/favicon.ico";

				PriorityThreadPool.QueueUserWorkItem(new WaitCallback(async_with_threadpool_step1), rs, 1);
			}
		}

		private void async_with_threadpool_step1(object state)
		{
			RequestState rs = state as RequestState;
			if(rs != null)
			{
				try
				{
					Debug.WriteLine("http://" + new Uri(rs.faviconUri).Host);
					WebRequest request = WebRequest.Create("http://" + new Uri(rs.faviconUri).Host);
					request.Timeout = _timeout;
					using(WebResponse resp = request.GetResponse())
					{
						//Debug.WriteLine("ContentType : " + ((HttpWebResponse)resp).ContentType);
						string contentType = ((HttpWebResponse)resp).ContentType;
						int deb = contentType.IndexOf("charset", 0);
						string encod = "ISO-8859-1";
						if(deb > 0)
							encod = contentType.Substring(deb + 8, contentType.Length - (deb + 8));
						//Debug.WriteLine("encod : " + encod);
						
						StreamReader reader = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.GetEncoding(encod));
						string webPage = reader.ReadToEnd();
						string pattern = @"<link\s+rel\s*=""(shortcut)?\s*icon""\s+href\s*=\s*""([\w:/\.]+)"">";
						Regex regExp = new Regex(pattern, RegexOptions.IgnoreCase);
						for(Match m = regExp.Match(webPage); m.Success; m = m.NextMatch())
						{
							//Debug.WriteLine("Match : " + m.Value);
							string url = m.Groups[2].Value;
							//Debug.WriteLine("group (" + rs.faviconName + ") : " + url);
							if(url.StartsWith("\\") || url.StartsWith("/"))
								rs.faviconUri = "http://" + (new Uri(rs.faviconUri)).Host + url;
							else
								rs.faviconUri = url;
						}

					}
					Debug.WriteLine(MethodBase.GetCurrentMethod().Name + " - " + rs.faviconName + " ; rs.faviconUri : " + rs.faviconUri);
					if(_log.IsInfoEnabled) _log.Info(MethodBase.GetCurrentMethod().Name + " - " + rs.faviconName + " ; rs.faviconUri : " + rs.faviconUri);
					async_with_threadpool_step2(rs);

				}
				catch(Exception exc)
				{
					if(_formProgress != null)
					{
						_formProgress.PerformStep(rs.faviconName + " : " + exc.Message);
					}
					if(_log.IsInfoEnabled) _log.Info(MethodBase.GetCurrentMethod().Name + " - " + rs.faviconName, exc);
					Debug.WriteLine(rs.faviconName + " : " + exc.ToString());
					decreaseWaitingRequests();
				}
			}
		}
		
		private void async_with_threadpool_step2(object state)
		{
			RequestState rs = state as RequestState;
			if(rs != null)
			{
				try
				{
					WebRequest requestIcon = WebRequest.Create(rs.faviconUri);
					requestIcon.Timeout = _timeout;
					using(WebResponse response = requestIcon.GetResponse())
					{
						Debug.WriteLine(rs.faviconName + " - content type : " + response.ContentType);
						using(Stream myStream = response.GetResponseStream())
						{
//							byte[] bytes = new byte[response.ContentLength];
//							myStream.Read(bytes, 0, bytes.Length);
//							MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);							
							Image img = Image.FromStream(myStream);
							saveFavicon(rs.faviconName, img);
						}
					}
					if(_formProgress != null)
					{
						_formProgress.Invoke(new FormProgress.ProgressInvoker(_formProgress.PerformStep), new object[]{rs.faviconName + " : OK"});
					}
					if(_log.IsInfoEnabled) _log.Info(MethodBase.GetCurrentMethod().Name + " - " + rs.faviconName + " : OK");
				}
				catch(Exception exc)
				{
					if(_formProgress != null)
					{
						_formProgress.Invoke(new FormProgress.ProgressInvoker(_formProgress.PerformStep), new object[]{rs.faviconName + " : (" + exc.GetType().Name + ") -- " + exc.Message});
					}
					if(_log.IsInfoEnabled) _log.Info(MethodBase.GetCurrentMethod().Name + " - " + rs.faviconName, exc);
					Debug.WriteLine(rs.faviconName + " : " + exc.ToString());
				}
			}
			decreaseWaitingRequests();
		}

		private void decreaseWaitingRequests()
		{
			Interlocked.Decrement(ref _waitingRequests);
			if (_waitingRequests == 0)
			{
				if (_formProgress != null)
				{
					_formProgress.Invoke(new MethodInvoker(_formProgress.EndProgress));
				}
				serializeFavicons();
				_parent.loadFavorites();
			}
		}

		private void saveFavicon(string name, Image img)
		{
			if (img.Height > 1 && img.Width > 1)
			{
				Bitmap b = (img.Height > 16 && img.Width > 16) ? new Bitmap(img, 16, 16) : new Bitmap(img);
				lock(_favicons.SyncRoot)
				{
					_favicons.Add(name, b);
				}
				if(_log.IsInfoEnabled) _log.Info("adding in _favicons : " + name + " ; " + b.GetHashCode());
				Debug.WriteLine("adding in _favicons : " + name + " ; " + b.GetHashCode());
			}
		}
		#endregion

		
		/// <summary>
		/// Used to pass data across asynchronous calls
		/// </summary>
		internal class RequestState
		{
			public string faviconUri;
			public string faviconName;
		}
	}
}

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
Web Developer
France France
I am an MCSD.NET and MCT. I give a lot of Microsoft Trainings (www.bdcworld.com) in France.

Comments and Discussions