
//============================================================================================================
// Microsoft Updater Application Block for .NET
//  http://msdn.microsoft.com/library/en-us/dnbda/html/updater.asp
//	
// ConfigSectionHandler.cs
//
// Handles the configuration file for the AppStart "shim".
// 
// For more information see the Updater Application Block Implementation Overview. 
// 
//============================================================================================================
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//============================================================================================================

using System;
using System.Configuration;
using System.Diagnostics;
using System.Xml;


namespace Microsoft.ApplicationBlocks.ApplicationUpdater.AppStart
{

	/// <summary>
	/// Provides encapsulated view of AppStart configuration information.
	/// </summary>
	internal class AppStartConfiguration
	{
		#region Constructors

		public AppStartConfiguration(){}

		#endregion

		#region Declarations

		private string			_exePath			= "";
		private string			_folderName			= "";

		#endregion

		#region Properties

		public string ExePath
		{
			get{ return _exePath; }
			set{ _exePath = value; }
		}

		
		public string FolderName
		{
			get{ return _folderName; }
			set{ _folderName = value; }
		}

		#endregion


	}

	/// <summary>
	/// Implements IConfigurationSectionHandler.  Creates an AppStartConfig object which encapsulates necessary configuration settings
	/// in a strong-typed reliable class for consumption by AppStart core process.
	/// </summary>
	internal class ConfigSectionHandler : IConfigurationSectionHandler
	{
		public ConfigSectionHandler(){}

//		<ClientApplicationInfo>
//			<appFolderName>C:\temp\pag\AppUpdaterClient</appFolderName>
//			<appExeName>SampleApplicationForBitsDownload.exe</appExeName>
//		</ClientApplicationInfo>

		object IConfigurationSectionHandler.Create( object parent, object configContext, XmlNode section )
		{
			AppStartConfiguration config = null;
			XmlNode rootNode = null;
			XmlNode subNode = null;
			string temp = "";

			//  grab an instance of AppStartConfiguration
			config = new AppStartConfiguration();

			//  get the primary node "ClientApplicationInfo" so we can access others easily
			rootNode = section.SelectSingleNode("ClientApplicationInfo");

			//  access each subnode, validating values and adding them to our instance of config object			
			try
			{
				subNode = rootNode.SelectSingleNode("appFolderName");
				if ( null != subNode )
				{
					temp = subNode.InnerText;
					//  check if terminal slash, add if missing
					if ( temp.IndexOf( @"\", ( temp.Length - 2 ) ) == -1 )
					{
						temp += @"\";
					}
					config.FolderName = temp;
				}
			
				subNode = rootNode.SelectSingleNode("appExeName");
				if ( null != subNode )
				{
					config.ExePath = subNode.InnerText;
				}
			}  // TRY
			catch( Exception e )
			{
				Trace.WriteLine( "AppStart:[ConfigSectionHandler.Create]: Error during parsing of app.config file:" + Environment.NewLine + e.Message );
				throw e;
			}

			return config;

		}

	}
}
