Click here to Skip to main content
15,896,269 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

using Favalias.SystemHotkeyNS;
using Favalias.Forms;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Favalias.Controllers
{
	/// <summary>
	/// Summary description for FavaliasController.
	/// </summary>
	public class FavaliasController
	{
		#region Constants
		// AssemblyInfo.cs and FormAbout.cs use it :
		internal const string Version = "0.8";

		// user application data folder (for Favalias) :
		internal readonly static string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Favalias";
		
		// main application folder :
		internal readonly static string appFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
		
		// addins folder :
		internal readonly static string addinsFolder = appFolder + @"\addins\";
		
		#endregion

		#region Fields
		internal static UserPreferences userPref;
		private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
		private static SystemHotkey _hotkey;
		internal static FormFavalias _master;		
		#endregion

		#region Initialization
		private FavaliasController()
		{}

		public static void Init()
		{
			// the order is important :
			initializeAppFolder();
			deserializePreferences();
			AliasesController.Init();
			CommandController.Init();
			
			// system hotkey :
			_hotkey = new SystemHotkey();
			_hotkey.Pressed += new System.EventHandler(systemHotkey1_Pressed);
			_hotkey.SetShortcut(userPref.shortcutMod, userPref.shortcutKey);
			
		}
		#endregion

		#region Preferences

		public static void SavePreferences(bool serializeSize)
		{
			serializePreferences(serializeSize);
		}

		private static void serializePreferences(bool serializeSize)
		{
			if(_master.WindowState != FormWindowState.Normal)
				_master.WindowState = FormWindowState.Normal;
			userPref.formLocation = _master.Location;
			userPref.isTransparent = !_master._displayFullBright;
			if(serializeSize)
				userPref.formSize = _master.Size;

			try
			{
				XmlSerializer serializer = new XmlSerializer(typeof(UserPreferences));
				using(FileStream fs = new FileStream(appDataFolder + @"\preferences.xml", FileMode.Create))
				{
					serializer.Serialize(fs, userPref);
				}
			}
			catch(Exception exc)
			{
				if(_log.IsErrorEnabled) _log.Error(MethodBase.GetCurrentMethod().Name, exc);
				Debug.WriteLine(exc);
			}
		}

		private static void deserializePreferences()
		{
			userPref = new UserPreferences();
			// default preferences :
			userPref.shortcutKey = Keys.S;
			userPref.shortcutMod = Keys.LWin;
			userPref.favoritesFile = appDataFolder + @"\Favorites.xml";	
			userPref.aliasesFile = appDataFolder + @"\Aliases.xml";
			userPref.browser = @"C:\Program Files\Internet Explorer\iexplore.exe";
			userPref.opacityDegree = 0.4F;
			userPref.autoHide = false;
			userPref.formSize = new Size(248, 75);
			userPref.isTransparent = true;
			WebProxy defaultProxy = WebProxy.GetDefaultProxy();
			if(defaultProxy.Address != null)
			{
				userPref.proxyIP = defaultProxy.Address.Host;
				userPref.proxyPort = defaultProxy.Address.Port;
			}
			try
			{
				XmlSerializer serializer = new XmlSerializer(typeof(Favalias.UserPreferences));
				string path = appDataFolder + @"\preferences.xml";
				if(File.Exists(path))
				{
					using(FileStream fs = new FileStream(path, FileMode.Open))
					{
						UserPreferences pref = (UserPreferences)serializer.Deserialize(fs);
						if(pref.formLocation != Point.Empty)
							userPref.formLocation = pref.formLocation;
						if(pref.favoritesFile != null && pref.favoritesFile.Length != 0)
							userPref.favoritesFile = pref.favoritesFile;
						if(pref.aliasesFile != null && pref.aliasesFile.Length != 0)
							userPref.aliasesFile = pref.aliasesFile;
						if(pref.browser != null && pref.browser.Length != 0)
							userPref.browser = pref.browser;
						if(pref.shortcutKey != Keys.None)
							userPref.shortcutKey = pref.shortcutKey;
						if(pref.shortcutMod != Keys.None)
							userPref.shortcutMod = pref.shortcutMod;
						if(pref.proxyIP != null && pref.proxyIP.Length != 0)
							userPref.proxyIP = pref.proxyIP;
						userPref.proxyPort = pref.proxyPort;
						userPref.autoHide = pref.autoHide;
						userPref.opacityDegree = pref.opacityDegree;
						userPref.isTransparent = pref.isTransparent;
						if(!pref.formSize.IsEmpty)
							userPref.formSize = pref.formSize;
						if(pref.skin != null && pref.skin.Length != 0)
							userPref.skin = pref.skin;
					}
				}
			}
			catch(Exception exc)
			{
				if(_log.IsErrorEnabled) _log.Error(MethodBase.GetCurrentMethod().Name, exc);
				Debug.WriteLine(exc);
			}
		}
		#endregion

		#region Methods
		/// <summary>
		/// Create the application folder (in application data) if it doesn't exist :
		/// </summary>
		private static void initializeAppFolder()
		{
			try
			{
				if (!Directory.Exists(FavaliasController.appDataFolder))
				{
					Directory.CreateDirectory(FavaliasController.appDataFolder);
				}
			}
			catch(Exception exc)
			{
				if(_log.IsErrorEnabled) _log.Error(MethodBase.GetCurrentMethod().Name, exc);
				MessageBox.Show("Couldn't create : " + FavaliasController.appDataFolder + ". You won't be able to save your preferences.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				Debug.WriteLine(exc);
			}
		}

		internal static bool changeShortcutKey(Keys mod, Keys key)
		{
			return _hotkey.SetShortcut(mod, key);
		}

		private static void systemHotkey1_Pressed(object sender, EventArgs e)
		{
			Debug.WriteLine("wake up !");
			_master.standup();
		}

		internal static void shutdown(bool serializeSize)
		{
			serializePreferences(serializeSize);
			CommandController.SaveCommandHistory();
			_hotkey.Dispose();
		}
		#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 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