/***********************************************************************
 * 
 *  Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 ***********************************************************************/

namespace Novell.CASA.GUI {


	using System;
	using System.Collections;
	using Novell.CASA.CASAPolicy;
	using Novell.CASA.DataEngines;
	using Novell.CASA;


	public class StorePolicyInterface
	{
		
		private static AggregationPol aggPol;
		
		private static string 	storeNameFirefox 	  = null;
		private static string[] storeIdFirefox 	 	  = new string[Common.MAX_STORES];
		private static int 		kFireFoxStoreCounter  = 0;
		
		private static string 	storeNameMozilla 	  = null;
		private static string[] storeId_mozilla 	  = new string[Common.MAX_STORES];
		private static int 		kMozillaStoreCounter  = 0;
					
		private static string 	storeNameKwallet 	  = null;
		private static string[] storeIdKwallet 	 	  = new string[Common.MAX_STORES];
		private static int 		kWalletStoreCounter   = 0;
		
		private static string 	storeNameGkeyring 	  = null;
		private static string[] storeIdGkeyring 	  = new string[Common.MAX_STORES];
		private static int 		kGkeyringStoreCounter = 0;
 
				
	
		///#######################################################################
		/// INIT
		
		/// <summary>		
		/// Init AggregationPolicy to the respective attributes 
		/// </summary>
		public static int Init()
		{
			Logger.DbgLog("GUI:StorePolicyInterface.Init() - BEGIN");
			
			try
			{
			Common.IS_FIREFOX_AVAILABLE = AD.IsStoreAvailable(Common.STORE_FIREFOX); 				
			Common.IS_KDEWALLET_AVAILABLE = AD.IsStoreAvailable(Common.STORE_KDEWALLET);
			Common.IS_GNOMEKEYRING_AVAILABLE = AD.IsStoreAvailable(Common.STORE_GNOMEKEYRING);
			
			aggPol = (AggregationPol) ICASAPol.GetPolicy(CASAPolType.AGGREGATION_POL);			
			if( null == aggPol )
			{
				Logger.DbgLog("GUI:StorePolicyInterface.Init() - No Aggregation Policy Found.");
				return( Common.STATUS_POLICY_POLICYNOTFOUND );
			}
									
			ArrayList stores = aggPol.StoreList;						
			IEnumerator enumerator = stores.GetEnumerator();
			
			while( enumerator.MoveNext() )
			{				
				if( ((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreName.Equals(Common.STORENAME_KDEWALLET) && Common.IS_KDEWALLET_AVAILABLE)
				{					
					Common.IS_KDEWALLET = true;
					storeNameKwallet = ((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreName;
					storeIdKwallet[kWalletStoreCounter++] = ((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreId;					
					Logger.DbgLog("GUI:StorePolicyInterface.Init() - miCASA policy = " + Common.IS_MICASA);
				}
				else if(((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreName.Equals(Common.STORENAME_GNOMEKEYRING) && Common.IS_GNOMEKEYRING_AVAILABLE)
				{
					Common.IS_GNOMEKEYRING=true;
					storeNameGkeyring=((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreName;
					storeIdGkeyring[kGkeyringStoreCounter++]=((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreId;
					Logger.DbgLog("GUI:StorePolicyInterface.Init() - GNOMEKEYRING policy = " + Common.IS_GNOMEKEYRING);
				}
				else if(((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreName.Equals(Common.STORENAME_FIREFOX) && Common.IS_FIREFOX_AVAILABLE)
				{
					Common.IS_FIREFOX=true;
					storeNameFirefox=((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreName;
					storeIdFirefox[kFireFoxStoreCounter++]=((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreId;
					Logger.DbgLog("GUI:StorePolicyInterface.Init() - FIREFOX policy = " + Common.IS_FIREFOX);
				}
				else if(((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreName.Equals(Common.STORENAME_MOZILLA))
				{
					Common.IS_MOZILLA=true;
					storeNameMozilla=((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreName;
					storeId_mozilla[kMozillaStoreCounter++]=((Novell.CASA.CASAPolicy.Store)(enumerator.Current)).StoreId;
					Logger.DbgLog("GUI:StorePolicyInterface.Init() - MOZILLA policy = " + Common.IS_MOZILLA);
				}									
			}
			
			}
			catch(Exception exp)
			{
				Logger.DbgLog("GUI:StorePolicyInterface.Init() - EXCEPTION" + exp.ToString());
				//Common.ShowErrorDialog(exp);
				return( Common.STATUS_POLICY_POLICYNOTFOUND );
			}
								
			Logger.DbgLog("GUI:StorePolicyInterface.Init() - END");			
			return( Common.STATUS_SUCCESS );
		}

		
		
		///#######################################################################
		/// IS AGGREGATION POLICY ENABLED FOR STORE ?
		
		/// <summary>
		/// Reports whether the aggregation policy is set for the store.
		/// </summary>
		public static bool IsAggregationPolicyEnabledFor(int storeIDentifier)
		{
			Logger.DbgLog("GUI:StorePolicyInterface.isAggregationPolicyEnabledFor()");
						
			if( Common.STORE_MICASA == storeIDentifier )
				return( Common.IS_MICASA );
			else if( Common.STORE_KDEWALLET == storeIDentifier )
				return( Common.IS_KDEWALLET );
			else if( Common.STORE_GNOMEKEYRING == storeIDentifier )
				return( Common.IS_GNOMEKEYRING );
			else if( Common.STORE_FIREFOX == storeIDentifier )
				return( Common.IS_FIREFOX );
			else if( Common.STORE_MOZILLA == storeIDentifier )
				return( Common.IS_MOZILLA );			
			else
			{
				Logger.DbgLog("GUI:StorePolicyInterface.isAggregationPolicyEnabledFor() - STATUS_POLICY_POLICYNOTFOUND");
				//Common.ShowErrorDialog(Common.STATUS_POLICY_POLICYNOTFOUND);
				return( false );
			}								
		}



		///#######################################################################
		/// GET LIST OF STORE IDs FOR A STORE
		
		/// <summary>
		/// Get Aggregation Policy - storeId 
		/// </summary>
		public static string[] GetAggregationPolicyStoreIDFor(int storeIDentifier)
		{
			Logger.DbgLog("GUI:StorePolicyInterface.getAggregationPolicyStoreIDFor()");
			
			if( Common.STORE_KDEWALLET == storeIDentifier )
				return( storeIdKwallet );
			else if( Common.STORE_GNOMEKEYRING == storeIDentifier )
				return( storeIdGkeyring );
			else if( Common.STORE_FIREFOX == storeIDentifier )
				return( storeIdFirefox );			
			else if( Common.STORE_MOZILLA == storeIDentifier )
				return( storeId_mozilla );
			else
			{
				Logger.DbgLog("GUI:StorePolicyInterface.getAggregationPolicyStoreIDFor() - STATUS_POLICY_POLICYNOTFOUND");
				//Common.ShowErrorDialog(Common.STATUS_POLICY_POLICYNOTFOUND);
				return( null );
			}			
		}



		///#######################################################################
		/// SET AGGREGATION POLICY FOR A STORE
		
		/// <summary>
		/// Set Aggregation Policy for a perticular store. 
		/// </summary>
		public static void SetAggregationPolicy(int storeIDentifier,bool status,string[] storeId,int numberOfStoreIds)
		{			
			Logger.DbgLog("GUI:StorePolicyInterface.SetAggregationPolicy()");
			
			try
			{
								
			if( storeIDentifier == Common.STORE_KDEWALLET )
			{
				Common.IS_KDEWALLET=status;
				storeNameKwallet=Common.STORENAME_KDEWALLET;
				storeIdKwallet=storeId;
				kWalletStoreCounter=numberOfStoreIds;
			}
			else if( storeIDentifier == Common.STORE_GNOMEKEYRING )
			{
				Common.IS_GNOMEKEYRING=status;
				storeNameGkeyring=Common.STORENAME_GNOMEKEYRING;
				storeIdGkeyring=storeId;
				kGkeyringStoreCounter=numberOfStoreIds;
			}
			else if( storeIDentifier == Common.STORE_MOZILLA )
			{
				Common.IS_MOZILLA=status;
				storeNameMozilla=Common.STORENAME_MOZILLA;
				storeId_mozilla=storeId;
				kMozillaStoreCounter=numberOfStoreIds;
			}
			else if( storeIDentifier == Common.STORE_FIREFOX )
			{
				Common.IS_FIREFOX=status;
				storeNameFirefox=Common.STORENAME_FIREFOX;
				storeIdFirefox=storeId;
				kFireFoxStoreCounter=numberOfStoreIds;
			}
			}
			catch(Exception exp)
			{
				Logger.DbgLog("GUI:StorePolicyInterface.SetAggregationPolicy() - EXCEPTION" + exp.ToString());
				//Common.ShowErrorDialog(exp);
			}
		}



		///#######################################################################
		/// SAVE AGGREGATION POLICY
		
		/// <summary>
		/// Save Aggregation Policy 
		/// </summary>
		public static int SaveAggregationPolicy()
		{
			Logger.DbgLog("GUI:StorePolicyInterface.SaveAggregationPolicy()");
			
			ArrayList storeList = new ArrayList();
			Novell.CASA.CASAPolicy.Store store = null;
			
			if( true == Common.IS_KDEWALLET )
			{
				for(int i=0; i < kWalletStoreCounter; i++)
				{
					store = new Novell.CASA.CASAPolicy.Store(storeNameKwallet,storeIdKwallet[i]);
					storeList.Add(store);
				}
			}

			if( true == Common.IS_GNOMEKEYRING )
			{
				for(int i=0; i < kGkeyringStoreCounter; i++)
				{
					store = new Novell.CASA.CASAPolicy.Store(storeNameGkeyring,storeIdGkeyring[i]);
					storeList.Add(store);
				}
			}

			if( true == Common.IS_FIREFOX )
			{
				for(int i=0; i < kFireFoxStoreCounter; i++)
				{
					store = new Novell.CASA.CASAPolicy.Store(storeNameFirefox,storeIdFirefox[i]);
					storeList.Add(store);
				}
			}

			if( true == Common.IS_MOZILLA )
			{
				for(int i=0; i < kMozillaStoreCounter; i++)
				{
					store = new Novell.CASA.CASAPolicy.Store(storeNameMozilla,storeId_mozilla[i]);
					storeList.Add(store);
				}
			}
			
			
			AggregationPol aggPol = new AggregationPol(storeList);
			
			if( true == ICASAPol.SetPolicy(aggPol) )
				return( Common.STATUS_SUCCESS );
			else
			{
				Logger.DbgLog("GUI:StorePolicyInterface.SaveAggregationPolicy() - STATUS_POLICY_COULDNOTBESAVED");
				//Common.ShowErrorDialog(Common.STATUS_POLICY_COULDNOTBESAVED);
				return( Common.STATUS_POLICY_COULDNOTBESAVED );
			}
		}

		
		
		///#######################################################################
		/// SET MASTER PASSWORD
		
		/// <summary>
		/// Set Master Password for miCASA persistent store 
		/// </summary>
		public static void SetMasterPassword(string passWord)
		{
			Logger.DbgLog("GUI:StorePolicyInterface.SetMasterPassword()"); 
			
			try
			{
				Novell.CASA.miCASA.SetMasterPassword(0,passWord);
			}
			catch(Exception exp)
			{
				Logger.DbgLog("GUI:StorePolicyInterface.SetMasterPassword() - EXCEPTION" + exp.ToString());
				//Common.ShowErrorDialog(exp);
			}
		}
	}
}

///###########################################################################
///								END OF FILE
///###########################################################################