/***********************************************************************
 * 
 *  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.IO;
	using System.Diagnostics;
	using Gtk;
	using Glade;
	using Novell.CASA;
	using Novell.CASA.MiCasa.Common;
	using Novell.CASA.MiCasa.Communication;

#if W32
	using Microsoft.Win32;
#endif

	public class CasaMain
	{	
		public MiCasa 			objMiCasa 		= null; 
		public Firefox			objFirefox 		= null;
		public Mozilla 			objMozilla 		= null;
		public KdeWallet 		objKdeWallet 	= null;
		public GnomeKeyring		objGnomeKeyring = null;
	 
		public static Glade.XML 	gxmlMain			= null;
		public static CasaTray 	mCasaTray 		= null;
		
		public static bool  		m_bShowDebug 	= false;
		public static bool  		m_bNotifyUser 	= true;
		public static bool  		m_bGuiLocked 	= false;
		
		int loginPromptCount = 3;	

		#region Glade Widgets
	
		[Glade.Widget] 
		Gtk.Window 		windowMain;

		[Glade.Widget] 
		Gtk.TreeView 	tvLog;
	
		[Glade.Widget]
		Gtk.Notebook 	notebookStores;
	
		[Glade.Widget]
		Gtk.Dialog 		dialogPersistentStorage,
						dialogPreferences, 
						dialogAbout, 
						dialogLogin, 
						dialogLoginContinue, 
        					dialogDesktopPassword,
						dialogConfirmRefresh,
						dialogSingleInstance,
						dialogLoginReprompt,
						dialogShortPassword,
						dialogResetMP,
						dialogStillRunning,
						dialogFirefoxMP;
						
		[Glade.Widget] 
		Gtk.Entry		entryMasterPassword1,
						entryMasterPassword2, 
						entryMasterPassword3, 
						entryMasterPassword4,
        					entryDesktopPassword1,
        					entryDesktopPassword2,
						entryOldMP,
						entryNewMP1,
						entryNewMP2,
						entryFirefoxMP;
						
						
		[Glade.Widget] 
		Gtk.CheckButton	checkbuttonFirefox,
						checkbuttonMozilla, 
						checkbuttonGnomeKeyring, 
						checkbuttonKdeWallet,
						checkbuttonCloseMessage;
						
		[Glade.Widget] 
		Gtk.Label		label88,
						labelLoginContinue1, 
						labelLoginContinue2,
        					labelDesktopPasswordMessage;
						
		[Glade.Widget] 
		Gtk.Button		okbuttonPersistentStorage;
			
		[Glade.Widget] 
		Gtk.MenuItem 	mmiNew,
						mmiNewKey,
						mmiView, 
						mmiLink, 
						mmiCopy, 
						mmiDelete,
						mmiRefresh,
						mmiLockSecrets,
						mmiUnlockSecrets,
						mmiDestroySecrets,
						mmiEdit,
						mmiOptions,
						mmiDebug,
						mmiApplicationSSO,
						mmiKonquerer,
						mmiKopete,
						mmiNetworkManager,
						mmiGaim;



		[Glade.Widget]
		Gtk.DrawingArea		drawingarea1;
		
		#endregion
		
		
		/// <summary>
		/// ********************************************************************
		/// public static void Main(string[] args)
		///
		/// MAIN		
		/// The main entry point for the CASA application.		
		/// ********************************************************************
		/// </summary>
		[STAThread]
		public static void Main(string[] args)
		{
			Logger.DbgLog("GUI:CasaMain.Main() - BEGIN");

			Common.ReadPlatform();		
			Application.Init();  
		
			try 
			{
				if (Common.IsArgSet(args, Common.ARG_DEBUG))
					m_bShowDebug = true;
					
				// ping micasad
				Ping ping = new Ping();
				ping.clientmessage = "Hello micasad";
				MiCasaRequestReply.Send(MiCasaRequestReply.VERB_PING_MICASAD,ping);
			
				new CasaMain(args);															
				Application.Run();
			
			}
			catch (Exception e)
			{		
				//Console.WriteLine("CasaMain: EXCEPTION \n"+e.ToString());
				
				string message = "miCASA services are not available";				
				if (m_bShowDebug)
					message = e.ToString();
				MessageDialog md=new MessageDialog(null,Gtk.DialogFlags.Modal,
				Gtk.MessageType.Error,
				Gtk.ButtonsType.Close,
				message);
				md.Response+=new ResponseHandler(md_ResponseCloseWindow);
				md.SetPosition(Gtk.WindowPosition.CenterAlways);
				md.Show();				
				Application.Run();			  
			}
				      
			Logger.DbgLog("GUI:CasaMain.Main() - END");
		}

	
		/// <summary>
		/// *******************************************************************
		/// public CasaMain(string[] args)
		///
		/// MAIN CONSTRUCTOR
		/// CasaMain constructor funtion.		
		/// *******************************************************************
		/// </summary>
		public CasaMain(string[] args) 
		{
			Logger.DbgLog("GUI:CasaMain.CasaMain() - BEGIN");
				
			if( false == Common.CheckForSingleInstance() )
			{	      		
				MasterPasswordAuthentication();
				if (Common.IsTrayAvailable()) // && Common.IsArgSet(args, Common.ARG_SHOW_TRAY_ICON))
				{
					try 
					{
						if (mCasaTray == null)
							mCasaTray = new CasaTray(this);
					}
					catch (Exception e)
					{
						Logger.DbgLog("CasaMain:EXCEPTION"+e.ToString());
					}
				}						
			}
			else
			{			
				{
					Glade.XML gxmlTemp = new Glade.XML(Common.GladeFile, "dialogSingleInstance", null);
					gxmlTemp.Autoconnect(this);					
				}				
			}		
		
			Logger.DbgLog("GUI:CasaMain.CasaMain() - END");          
		}
    
    
		/// <summary>
		/// ********************************************************************
		/// public void InitializeGUI()
		/// 
		/// INITIALIZE MAIN GUI
		/// Main GUI initializing routine. This routine builds the Main 
		/// window of CASA.
		/// ********************************************************************
		/// </summary>
		public void InitializeGUI()
		{
			Logger.DbgLog("GUI:CasaMain.InitializeGUI() - BEGIN");
    	    	    	
			gxmlMain = new Glade.XML(Common.GladeFile, "windowMain", null);			
			gxmlMain.Autoconnect(this);
			windowMain.DeleteEvent += new DeleteEventHandler(OnWindowMainDeleted);
			
			// DEBUG 
			if (!m_bShowDebug)
				mmiDebug.Hide();

			/// PLATFORM SPECIFIC GUI CHANGES
			
			/// POLICY INIT
			StorePolicyInterface.Init();
        		        		
			Logger.DbgLog("GUI:CasaMain.InitializeGUI() - miCASA policy = " + Common.IS_MICASA);
			Logger.DbgLog("GUI:CasaMain.InitializeGUI() - Firefox policy = " + Common.IS_FIREFOX);
			Logger.DbgLog("GUI:CasaMain.InitializeGUI() - Mozilla policy = " + Common.IS_MOZILLA);
			Logger.DbgLog("GUI:CasaMain.InitializeGUI() - KdeWallet policy = " + Common.IS_KDEWALLET);
			Logger.DbgLog("GUI:CasaMain.InitializeGUI() - GNOME Keyring policy = " + Common.IS_GNOMEKEYRING);
        
			/// STOREDATA INIT
			StoreDataInterface.Init();
						        
			if( Common.IS_MICASA )
			{
				Logger.DbgLog("GUI:CasaMain.new MiCasa().");    	
				objMiCasa = new MiCasa();
			}
			else
				(notebookStores.GetNthPage(Common.STORE_MICASA)).Visible = Common.IS_MICASA;
			             
			if( Common.IS_FIREFOX )
			{
				Logger.DbgLog("GUI:CasaMain.new Firefox().");
				objFirefox = new Firefox();				
			}
			else
				(notebookStores.GetNthPage(Common.STORE_FIREFOX)).Visible = Common.IS_FIREFOX;
			
			if( Common.IS_MOZILLA )
			{
				Logger.DbgLog("GUI:CasaMain.new Mozilla().");
				objMozilla = new Mozilla();
			}
			else
				(notebookStores.GetNthPage(Common.STORE_MOZILLA)).Visible = Common.IS_MOZILLA;	      
			      
			if( Common.IS_KDEWALLET )
			{
				Logger.DbgLog("GUI:CasaMain.new KdeWallet().");        	
				objKdeWallet = new KdeWallet();
			}
			else
				(notebookStores.GetNthPage(Common.STORE_KDEWALLET)).Visible = Common.IS_KDEWALLET;	      
        		
			if( Common.IS_GNOMEKEYRING )
			{
				Logger.DbgLog("GUI:CasaMain.new GnomeKeyring().");        	
				objGnomeKeyring = new GnomeKeyring();
			}
			else
				(notebookStores.GetNthPage(Common.STORE_GNOMEKEYRING)).Visible = Common.IS_GNOMEKEYRING;	      
        		
			notebookStores.CurrentPage = Common.STORE_MICASA;

			if (MiCASAStore.IsLocked())
				LockGUI();
			else
				UnlockGUI();
		

			Gdk.Color color = new Gdk.Color(90, 77, 189);
			drawingarea1.ModifyBg(Gtk.StateType.Normal, color);

			windowMain.Show();
              
			Logger.DbgLog("GUI:CasaMain.InitializeGUI() - END");
		}
    
        
        
		/// <summary>
		/// ********************************************************************
		/// public void MasterPasswordAuthentication()
		///
		/// INITIAL MASTER PASSWORD AUTHENTICATION
    		/// This routine implements the MasterPassword authentication.
    		/// ********************************************************************
		/// </summary>
		public void MasterPasswordAuthentication()
		{  
			Logger.DbgLog("GUI:CasaMain.Login() - BEGIN");
                	    	    	
			if( true == IsMasterPasswordSet() )
			{    		
				Logger.DbgLog("GUI:CasaMain.Login() - IsMasterPasswordSet returned true");
    	        		
				if( false == miCASA.IsSecretPersistent(1,"") )
				{    		    
					Logger.DbgLog("GUI:CasaMain.Login() - IsMasterPasswordSet returned false");    		    
					Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogLogin", null);
					gxmlTemp.Autoconnect (this);
					dialogLogin.TransientFor = windowMain;        
					entryMasterPassword3.Text="";
					label88.Hide();
					entryMasterPassword4.Hide();
				}
				else
				{        		
					Logger.DbgLog("GUI:CasaMain.Login() - IsMasterPasswordSet returned true");
					InitializeGUI();
				}
			} 
			else
			{        	
				Logger.DbgLog("GUI:CasaMain.Login() - IsMasterPasswordSet returned false");
                                
                // did the daemon get restarted before the user created a master password?
                // if so, let's ask the user for there desktop password and set it if there's a cache file
                if (true == DoPersistentFilesExist())
                {
                    Glade.XML gxmlTemp = new Glade.XML(Common.GladeFile, "dialogDesktopPassword", null);
                    gxmlTemp.Autoconnect(this);
                }
                else
                {
                    Glade.XML gxmlTemp = new Glade.XML(Common.GladeFile, "dialogLogin", null);
                    gxmlTemp.Autoconnect(this);
                    entryMasterPassword3.Text = "";
                    entryMasterPassword4.Text = "";
                }
			} 	          	        
    
			Logger.DbgLog("GUI:CasaMain.Login() - END");
		}

        public void on_entryDesktopPassword1_activate(object obj, EventArgs args)
        {
            on_buttonPasswordOk_clicked(obj, args);
        }

        public void on_entryDesktopPassword2_activate(object obj, EventArgs args)
        {
            on_buttonPasswordOk_clicked(obj, args);
        }

        public void on_buttonPasswordOk_clicked(object obj, EventArgs args)
        {
            object o = obj;
            if (entryDesktopPassword1 != null && entryDesktopPassword2.Text != null)
            {
                if (entryDesktopPassword1.Text.Length < 1)
                {                    
                    labelDesktopPasswordMessage.Text = "Please enter your desktop password";
                    return;
                }

                if (entryDesktopPassword1.Text.Equals(entryDesktopPassword2.Text))
                {
                    // set the desktop password in micasa
                    try
                    {
                        Novell.CASA.miCASA.SetCredential(0,
                        "Desktop",
                        null,
                        Novell.CASA.miCASA.USERNAME_TYPE_CN_F,
                        GetLocalUsername(),
                        entryDesktopPassword1.Text);
                    }
                    catch (Exception e)
                    {
                        Logger.DbgLog(e.ToString());
                    }

                    if (dialogDesktopPassword != null)
                        dialogDesktopPassword.Destroy();

                    MasterPasswordAuthentication();
                }
                else
                {
                    labelDesktopPasswordMessage.Text = "Passwords do not match";
                    entryDesktopPassword1.Text = "";
                    entryDesktopPassword2.Text = "";                    
                }
            }
        }

        public void on_helpbuttonDesktopPassword_clicked(object obj, EventArgs args)
        {
            Common.ShowHelpUrl("CASADesktopPassword.htm");
        }

        public void on_buttonPasswordClose_clicked(object obj, EventArgs args)
        {
            if (dialogDesktopPassword != null)
                dialogDesktopPassword.Destroy();

            closebuttonLogin_clicked(obj, args);
        }
    
		public void okbuttonLogin_clicked(object abj, EventArgs args)
		{    
			Logger.DbgLog("GUI:CasaMain.okbuttonLogin_clicked() - BEGIN");
                
			if( true == entryMasterPassword4.Visible )
			{
				if( entryMasterPassword3.Text.Length < 8 )
				{
					Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogShortPassword", null);
					gxmlTemp.Autoconnect (this);			
				}
				else if(entryMasterPassword3.Text != "" && (entryMasterPassword3.Text == entryMasterPassword4.Text) && entryMasterPassword3.Text.Length >= 8)
				{ 
					miCASA.SetMasterPassword(0, entryMasterPassword3.Text);
					MiCasaRequestReply.Send(MiCasaRequestReply.VERB_UNLOCK_STORE, entryMasterPassword3.Text);
					dialogLogin.Destroy();        						
					InitializeGUI();		
				}
				else
				{
					entryMasterPassword3.Text="";
					entryMasterPassword4.Text="";
					entryMasterPassword3.HasFocus=true;
				}
			}
			else
			{
				if( 0 == miCASA.SetMasterPassword(0, entryMasterPassword3.Text) )
				{
					MiCasaRequestReply.Send(MiCasaRequestReply.VERB_UNLOCK_STORE, entryMasterPassword3.Text);
					dialogLogin.Destroy();        						
					InitializeGUI();				
				}
				else
				{				
					Logger.DbgLog("GUI:CasaMain.okbuttonLogin_clicked() - MasterPassword verification failed.");
					//dialogLogin.Destroy();				
					//LoginContinue("<b>Master Password verfication failed</b>", "The store will not be persistent");				
					loginPromptCount--;
					dialogLogin.Hide();
							
					if( loginPromptCount > 0 ) 
					{
						dialogLogin.Show();
						entryMasterPassword3.Text="";
					}
					else
					{					
						Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogLoginReprompt", null);
						gxmlTemp.Autoconnect (this);					
					}									
				}
			}
				
			Logger.DbgLog("GUI:CasaMain.okbuttonLogin_clicked() - END");          	   	
		}

		public void on_helpbuttonAuthentication_clicked(object obj, EventArgs args)
		{
			Common.ShowHelpUrl("CASAMasterPasswordAuthentication.htm");	
		}
    
		internal void on_buttonRetryRepropmt_clicked(object obj, EventArgs args)
		{
			dialogLoginReprompt.Destroy();
			dialogLogin.Show();
			entryMasterPassword3.Text="";
			loginPromptCount = 3;
		}
	
		internal void on_buttonRetryShortPassword_clicked(object obj, EventArgs args)
		{
			dialogShortPassword.Destroy();
			entryMasterPassword3.Text = "";
			entryMasterPassword4.Text = "";
			entryMasterPassword3.HasFocus = true;
		}
	
		internal void on_buttonCloseReprompt_clicked(object obj, EventArgs args)
		{
			dialogLoginReprompt.Destroy();
			dialogLogin.Destroy();
			HandleQuit();		
		}
    
		public void closebuttonLogin_clicked(object abj, EventArgs args)
		{
            if (dialogLogin != null)
			    dialogLogin.Destroy(); 

			// close tray too
			if (mCasaTray != null)
			{	
				mCasaTray.Destroy();
				mCasaTray = null;
			}
			HandleQuit();    	
		}    
    
		public void OnDialogLoginDeleted(object obj, DeleteEventArgs args) 
		{
			Logger.DbgLog("GUI:CasaMain.OnDialogLoginDeleted() - BEGIN");
    	
			// close tray too
			if (mCasaTray != null)
			{	
				mCasaTray.Destroy();
				mCasaTray = null;
			}
			HandleQuit();    	
			args.RetVal = true;
        
			Logger.DbgLog("GUI:CasaMain.OnDialogLoginDeleted() - END");
		}
    
		public void on_entryMasterPassword3_activate(object obj, EventArgs args)
		{
			if( true == entryMasterPassword4.Visible )
				entryMasterPassword4.HasFocus = true;
			else if( "" != entryMasterPassword3.Text )
				okbuttonLogin_clicked(obj, args);
		}
    
		public void on_entryMasterPassword4_activate(object obj, EventArgs args)
		{
			okbuttonLogin_clicked(obj, args);
		}
    
		public bool IsMasterPasswordSet()
		{
			Logger.DbgLog("GUI:CasaMain.IsMasterPasswordSet() - BEGIN");
    	
			string MICASA_PASSCODE_BY_MASTER_PASSWD_FILE = "/.miCASAPCByMPasswd";
			string fileName = GetUserHomeDir() + MICASA_PASSCODE_BY_MASTER_PASSWD_FILE;
           	
			Logger.DbgLog("GUI:CasaMain.IsMasterPasswordSet() - END");
			return (File.Exists(fileName));
		}

        public bool DoPersistentFilesExist()
        {
            Logger.DbgLog("GUI:CasaMain.DoesPersistentFilesExist() - BEGIN");

            // is the desktop password set already?
            try
            {
                BasicCredential bc = Novell.CASA.miCASA.GetCredential(0,
                                     "Desktop",
                                     null,
                                     Novell.CASA.miCASA.USERNAME_TYPE_CN_F);
                return false;
            }
            catch
            {
                // check for existence of persistent files
                string MICASA_PASSCODE_BY_DESKTOP_FILE = "/.miCASAPCByDesktop";
                string MICASA_KEY_FILE = "/.miCASAKey";
                string MICASA_PERSISTENCE_FILE = "/.miCASA";
                string MICASA_VALIDATION_FILE = "/.miCASAValidate";
                string sHomeDir = GetUserHomeDir();

                Logger.DbgLog("GUI:CasaMain.DoesPersistentFilesExist() - END");
                return (File.Exists(sHomeDir + MICASA_PERSISTENCE_FILE)
                        && File.Exists(sHomeDir + MICASA_KEY_FILE)
                        && File.Exists(sHomeDir + MICASA_PASSCODE_BY_DESKTOP_FILE)
                        && File.Exists(sHomeDir + MICASA_VALIDATION_FILE));
            }
        }
           
		private string GetUserHomeDir()
		{
			if (Common.IS_LINUX)	
				return Environment.GetEnvironmentVariable("HOME");
			else
				return Environment.GetEnvironmentVariable("USERPROFILE");		
		}

        private string GetLocalUsername()
        {
            return Environment.GetEnvironmentVariable("USERNAME");                    
        }


    
		/// <summary>
		/// ********************************************************************
		/// public void LoginContinue(string LabelMain, string LabelTips)
		///
		/// LOGIN WARNING DIALOG
		/// Error dialog prompt for MasterPassword authentication
		/// ********************************************************************
		/// </summary>
		public void LoginContinue(string LabelMain, string LabelTips)
		{
			Logger.DbgLog("GUI:CasaMain.LoginContinue() - BEGIN");
    	
			Glade.XML gxmlTemp = new Glade.XML(Common.GladeFile, "dialogLoginContinue", null);
			gxmlTemp.Autoconnect(this);
			dialogLoginContinue.TransientFor = windowMain;    	    	
			labelLoginContinue1.LabelProp = LabelMain;
			labelLoginContinue2.LabelProp = LabelTips;
    	
			Logger.DbgLog("GUI:CasaMain.LoginContinue() - END");    	
		}
    
		public void on_buttonLoginContinue_clicked(object abj, EventArgs args)
		{
			dialogLoginContinue.Destroy();
			InitializeGUI();
		}
    
    
       
		/// <summary>
		/// ********************************************************************
		/// internal void on_notebookStores_switch_page(object obj, SwitchPageArgs args)
		///
		/// NOTEBOOK SWITCH PAGE HANDLER
		/// To perform only on the click of the respective store tab/page.
		/// ********************************************************************
		/// </summary>
		internal void on_notebookStores_switch_page(object obj, SwitchPageArgs args)
		{
			switch(args.PageNum)
			{    		    		
				case Common.STORE_MICASA: 				 
					break;
    										
				case Common.STORE_FIREFOX:
					if( false == objFirefox.IS_STORE_AGGREGATED )
					{
						if( StoreDataInterface.IsMasterPasswordSet(Common.STORE_FIREFOX) )
						{
							Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogFirefoxMP", null);
							gxmlTemp.Autoconnect (this);
							dialogFirefoxMP.SetPosition(Gtk.WindowPosition.CenterOnParent);
							dialogFirefoxMP.TransientFor = windowMain;
							dialogFirefoxMP.DeleteEvent += new DeleteEventHandler(on_dialogFirefoxMP_deleted);
						}
						else
						{
							objFirefox.AggregateStore();
							objFirefox.IS_STORE_AGGREGATED = true;
						}
					}
					break;
    				
				case Common.STORE_MOZILLA:	
					break;
    		
				case Common.STORE_KDEWALLET:
					if( false == objKdeWallet.IS_STORE_AGGREGATED )
					{
						objKdeWallet.AggregateStore();
						objKdeWallet.IS_STORE_AGGREGATED = true;
					}
					break;
    				
				case Common.STORE_GNOMEKEYRING: 
					if( false == objGnomeKeyring.IS_STORE_AGGREGATED )
					{
						objGnomeKeyring.AggregateStore();
						objGnomeKeyring.IS_STORE_AGGREGATED = true;			
					}
					break;
			}
		}
		
		public void on_entryFirefoxMP_activate(object obj, EventArgs args)
		{
			on_buttonFirefoxMPok_clicked(obj, args);
		}
		
		public void on_buttonFirefoxMPok_clicked(object obj, EventArgs args)
		{
			if( "" != entryFirefoxMP.Text )
				if( StoreDataInterface.VerifyMasterPassword(entryFirefoxMP.Text, Common.STORE_FIREFOX) )
				{
					dialogFirefoxMP.Destroy();
					objFirefox.AggregateStore();
					objFirefox.IS_STORE_AGGREGATED = true;
				}
				else
				{
					entryFirefoxMP.Text = "";
					entryFirefoxMP.HasFocus = true;
				}
		}
		
		public void on_buttonFirefoxMPclose_clicked(object obj, EventArgs args)
		{
			dialogFirefoxMP.Destroy();
		}
		
		public void on_buttonFirefoxMPhelp_clicked(object obj, EventArgs args)
		{
		
		}
		
		public void on_dialogFirefoxMP_deleted(object obj, DeleteEventArgs args) 
		{
			dialogFirefoxMP.Destroy();
		}
		/// <summary>
		/// ********************************************************************
		/// internal void FileMenuActivated(object obj, EventArgs args)
		///
		/// FILE MENU ACTIVATION HANDLER
		/// To highlight/de-highlight menuitems in the file menu.
		/// ********************************************************************
		/// </summary>
		internal void FileMenuActivated(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.FileMenuActivated() - BEGIN");
    	
			if (MiCASAStore.IsLocked())
			{				
				LockGUI();
				Logger.DbgLog("GUI:CasaMain.FileMenuActivated() Store is locked - END");	
				return;
			}
			else
			{				
				if (m_bGuiLocked)
					UnlockGUI();				
			}
				
			switch(notebookStores.CurrentPage)			
			{    		    		
				case Common.STORE_MICASA:

					mmiNew.Sensitive = true;
					
					if( 0 != objMiCasa.tvSecretIDMiCasa.Selection.CountSelectedRows() )
					{
						mmiNew.Sensitive = mmiNewKey.Sensitive = true;
					}
					else
					{						
						mmiNewKey.Sensitive = false;
					}
					break;
    										
				case Common.STORE_FIREFOX:	
					if( 0 != objFirefox.tvSecretIDFirefox.Selection.CountSelectedRows() )
					{
						mmiNew.Sensitive = mmiNewKey.Sensitive = false;
					}
					else
					{						
						mmiNew.Sensitive = mmiNewKey.Sensitive = false;						
					}
					break;
    				
				case Common.STORE_MOZILLA:	
					break;
    		
				case Common.STORE_KDEWALLET:

					mmiNew.Sensitive = true;

					if( 0 != objKdeWallet.tvSecretIDKdeWallet.Selection.CountSelectedRows() )
					{
						mmiNew.Sensitive = mmiNewKey.Sensitive = true;
					}
					else
					{
						mmiNewKey.Sensitive = false;
					}
					break;
    				
				case Common.STORE_GNOMEKEYRING: 
					
					mmiNew.Sensitive = true;

					if( 0 != objGnomeKeyring.tvSecretIDGnomeKeyring.Selection.CountSelectedRows() )
					{
						mmiNew.Sensitive = mmiNewKey.Sensitive = true;						
					}
					else
					{
						mmiNewKey.Sensitive = false;
					}
					break;
			}
    			
			Logger.DbgLog("GUI:CasaMain.FileMenuActivated() - END");	
		}
    
    
    
    		/// <summary>
		/// ********************************************************************
		/// internal void EditMenuActivated(object obj, EventArgs args)
		///
		/// EDIT MENU ACTIVATION HANDLER
		/// To highlight/de-highlight menuitems in the edit menu.
		/// ********************************************************************
		/// </summary>
		internal void EditMenuActivated(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.EditMenuActivated() - BEGIN");
    	
			switch(notebookStores.CurrentPage)
			{    		
				case Common.STORE_MICASA: 
					if( 0 != objMiCasa.tvSecretIDMiCasa.Selection.CountSelectedRows() )
					{
						mmiView.Sensitive = mmiLink.Sensitive = mmiDelete.Sensitive = true;
						mmiCopy.Sensitive = false; 
					}
					else
					{
						mmiView.Sensitive = mmiLink.Sensitive = mmiCopy.Sensitive = mmiDelete.Sensitive = false;
					}
					break;
    										
				case Common.STORE_FIREFOX:	
					if( 0 != objFirefox.tvSecretIDFirefox.Selection.CountSelectedRows() )
					{
						mmiView.Sensitive = mmiDelete.Sensitive = true;
						mmiLink.Sensitive = mmiCopy.Sensitive = false; 
					}
					else
					{
						mmiView.Sensitive = mmiLink.Sensitive = mmiCopy.Sensitive = mmiDelete.Sensitive = false;
					}
					break;
    				
				case Common.STORE_MOZILLA:	
					break;
    		
				case Common.STORE_KDEWALLET:
					if( 0 != objKdeWallet.tvSecretIDKdeWallet.Selection.CountSelectedRows() )
					{
						mmiView.Sensitive = mmiDelete.Sensitive = true;
						mmiLink.Sensitive = mmiCopy.Sensitive = false; 
					}
					else
					{
						mmiView.Sensitive = mmiLink.Sensitive = mmiCopy.Sensitive = mmiDelete.Sensitive = false;
					}
					break;
    				
				case Common.STORE_GNOMEKEYRING: 
					if( 0 != objGnomeKeyring.tvSecretIDGnomeKeyring.Selection.CountSelectedRows() )
					{
						mmiView.Sensitive = mmiDelete.Sensitive = true;
						mmiLink.Sensitive = mmiCopy.Sensitive = false; 
					}
					else
					{
						mmiView.Sensitive = mmiLink.Sensitive = mmiCopy.Sensitive = mmiDelete.Sensitive = false;
					}
					break;
			}
    	
			Logger.DbgLog("GUI:CasaMain.EditMenuActivated() - END");
		}
    
    
    
    		/// <summary>
		/// ********************************************************************
		/// internal void OptionsMenuActivated(object obj, EventArgs args)
		///
		/// OPTIONS MENU ACTIVATION HANDLER
		/// To highlight/de-highlight menuitems in the options menu.
		/// ********************************************************************
		/// </summary>
    		internal void OptionsMenuActivated(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.OptionsMenuActivated() - BEGIN");
				
			if( Common.IS_WINDOWS )
			{
				//mmiApplicationSSO.Sensitive = false;
				mmiKonquerer.Sensitive = false;
				mmiKopete.Sensitive = false;
				mmiNetworkManager.Sensitive = false;
				mmiGaim.Sensitive = false;				
				mmiKopete.Visible = mmiGaim.Visible = false; 
			}
			else
			{
				if( Common.IS_KDEWALLET )
					mmiKonquerer.Sensitive = mmiKopete.Sensitive = true;				
				else
					mmiKonquerer.Sensitive = mmiKopete.Sensitive = false;
					
				if( Common.IS_GNOMEKEYRING )
					mmiNetworkManager.Sensitive = mmiGaim.Sensitive = true;					 
				else
					mmiNetworkManager.Sensitive = mmiGaim.Sensitive = false;
				mmiKopete.Visible = mmiGaim.Visible = false;
			}
		
			Logger.DbgLog("GUI:CasaMain.OptionsMenuActivated() - END");
		}
    
    
    
		/// <summary>
		/// ********************************************************************
		/// public void RefreshAllStores(object obj, EventArgs args)
		///
		/// REFRESH ALL STORES
		/// Re-aggregate all stores based on the aggregation policy.
		/// ********************************************************************
		/// </summary>
		public void RefreshAllStores(object obj, EventArgs args)
		{
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogConfirmRefresh", null);
			gxmlTemp.Autoconnect (this);
			dialogConfirmRefresh.TransientFor = windowMain;  	        
		}
    
		public void on_buttonRefreshYes_clicked(object abj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.on_buttonRefreshYes_clicked() - BEGIN");
    	
			RefreshStores();    	
			dialogConfirmRefresh.Destroy();
    	
			Logger.DbgLog("GUI:CasaMain.on_buttonRefreshYes_clicked() - END");
		}
    
		public void on_buttonRefreshNo_clicked(object abj, EventArgs args)
		{
			dialogConfirmRefresh.Destroy();
		}

		public void on_helpbuttonRefresh_clicked(object obj, EventArgs Args)
		{
			Common.ShowHelpUrl("RefreshStore.htm");
		}
        
    	internal void RefreshStores()
		{
			if( Common.IS_MICASA )
				objMiCasa.AggregateStore();
    	
			if( Common.IS_FIREFOX )
			{
				if( false == objFirefox.IS_STORE_AGGREGATED && notebookStores.CurrentPage == Common.STORE_FIREFOX )
				{
					if( StoreDataInterface.IsMasterPasswordSet(Common.STORE_FIREFOX) )
					{
						Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogFirefoxMP", null);
						gxmlTemp.Autoconnect (this);
						dialogFirefoxMP.SetPosition(Gtk.WindowPosition.CenterOnParent);
						dialogFirefoxMP.TransientFor = windowMain;
						dialogFirefoxMP.DeleteEvent += new DeleteEventHandler(on_dialogFirefoxMP_deleted);
					}
				}
				else
					objFirefox.AggregateStore();
				//if( true == objFirefox.IS_STORE_AGGREGATED )
				//	objFirefox.AggregateStore();
			}
			
			if( Common.IS_MOZILLA )
				objMozilla.AggregateStore();
		
			if( Common.IS_KDEWALLET )
				objKdeWallet.AggregateStore();			
		
			if( Common.IS_GNOMEKEYRING )
				objGnomeKeyring.AggregateStore();			
		}
		

		internal void ClearViewOnStores()
		{
			if( Common.IS_MICASA )
				objMiCasa.ClearViewOnStore();
    	
			if( Common.IS_FIREFOX )
			{
				objFirefox.ClearViewOnStore();
			}
			
			if( Common.IS_MOZILLA )
				objMozilla.ClearViewOnStore();
		
			if( Common.IS_KDEWALLET )
				objKdeWallet.ClearViewOnStore();			
		
			if( Common.IS_GNOMEKEYRING )
				objGnomeKeyring.ClearViewOnStore();			
		}
		
		/// <summary>		
		/// ********************************************************************
		/// public void OnNewSecretActivated(object obj, EventArgs args)
		///
		/// MAIN-MENU: NEW SECRET
		/// Add a new secret.
		/// ********************************************************************
		/// </summary>	
		public void OnNewSecretActivated(object obj, EventArgs args)
		{   
			Logger.DbgLog("GUI:CasaMain.NewSecret() - BEGIN");
    	 	
			switch(notebookStores.CurrentPage)
			{    		
				case Common.STORE_MICASA:
					objMiCasa.OnNewSecretActivated(obj, args);    				
					break;
    										
				case Common.STORE_FIREFOX:
					break;
    		
				case Common.STORE_MOZILLA:
					break;
    		
				case Common.STORE_KDEWALLET:
					objKdeWallet.OnNewSecretActivated(obj, args);
					break;
    		
				case Common.STORE_GNOMEKEYRING: 
					objGnomeKeyring.OnNewSecretActivated(obj, args);
					break;
			}  	
    	
			Logger.DbgLog("GUI:CasaMain.NewSecret() - END");          	        
		}
    
    
    
		/// <summary>		
		/// ********************************************************************
		/// public void OnNewKeyActivated(object obj, EventArgs args)s)
		///
		/// MAIN-MENU: NEW KEY
		/// Add new key-value pairs to an existing secret.
		/// ********************************************************************
		/// </summary>	
		public void OnNewKeyActivated(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.NewKeyValue() - BEGIN");
    	
			switch(notebookStores.CurrentPage)
			{
				case Common.STORE_MICASA:
					objMiCasa.OnNewKeyActivated(obj, args);    				
					break;
    										
				case Common.STORE_FIREFOX:
					break;
    		
				case Common.STORE_MOZILLA:
					break;
    		
				case Common.STORE_KDEWALLET:
					objKdeWallet.OnNewKeyActivated(obj, args);
					break;
    		
				case Common.STORE_GNOMEKEYRING:
					objGnomeKeyring.OnNewKeyActivated(obj, args);
					break;
			} 
    	
			Logger.DbgLog("GUI:CasaMain.NewKeyValue() - END");
		}
	   
	   

		/// <summary>		
		/// ********************************************************************
		/// public void OnLockMiCASASecrets(object sender, EventArgs args)
		/// public void OnUnLockMiCASASecrets(object sender, EventArgs args)
		///
		/// MAIN-MENU: LOCK/UNLOCK STORE, DESTROY SECRETS
		/// Handlers for locking/unlocking the store, and destroying secrets.
		/// ********************************************************************
		/// </summary>	
		public void OnLockMiCASASecrets(object sender, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.OnLockMiCASASecrets() - START");
			MiCasaRequestReply.Send(MiCasaRequestReply.VERB_LOCK_STORE);
			LockGUI();
			if (mCasaTray != null)
				mCasaTray.UpdateTrayIcon(true);			
			Logger.DbgLog("GUI:CasaMain.OnLockMiCASASecrets() - END");
		}

		public void OnUnLockMiCASASecrets(object sender, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.OnUnLockMiCASASecrets() - START");
			CommonGUI cg = new CommonGUI();
			cg.HandleUnlock(this, mCasaTray);
			Logger.DbgLog("GUI:CasaMain.OnUnLockMiCASASecrets() - END");
		}

		internal void LockGUI()
		{
			ClearViewOnStores();
			notebookStores.Sensitive = false;
			mmiLockSecrets.Sensitive = false;
			mmiUnlockSecrets.Sensitive = true;
			mmiDestroySecrets.Sensitive = false;						
			mmiNew.Sensitive = false;
			mmiRefresh.Sensitive = false;
			mmiDebug.Sensitive = false;
			mmiOptions.Sensitive = false;
			mmiEdit.Sensitive = false;
			m_bGuiLocked = true;						
		}

		internal void UnlockGUI()
		{
			notebookStores.Sensitive = true;
			mmiLockSecrets.Sensitive = true;
			mmiUnlockSecrets.Sensitive = false;
			mmiDestroySecrets.Sensitive = true;
			mmiNew.Sensitive = true;
			mmiRefresh.Sensitive = true;
			mmiDebug.Sensitive = true;
			mmiOptions.Sensitive = true;
			mmiEdit.Sensitive = true;
			m_bGuiLocked = false;
			
			//refresh secrets
			RefreshStores();			
		}
		
		public void OnDestroyMiCASASecrets(object sender, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.OnDestroyMiCASASecrets() - START");	
			CommonGUI temp = new CommonGUI();
			temp.menuDestroyMiCasa_Activated(this, mCasaTray);		
			Logger.DbgLog("GUI:CasaMain.OnDestroyMiCASASecrets() - END");
		}

    

		/// <summary>		
		/// ********************************************************************
		/// public void LinkKeyValue(object obj, EventArgs args)
		///
		/// MAIN-MENU: VIEW
		/// View the key-value pairs of the selected secret. Calls store
		/// specific handlers.
		/// ********************************************************************
		/// </summary>	
		public void ViewKeyValue(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.ViewKeyValue() - BEGIN");
    	
			switch(notebookStores.CurrentPage)
			{
				case Common.STORE_MICASA:
					objMiCasa.ViewKeyValues();    				
					break;
    										
				case Common.STORE_FIREFOX:
					objFirefox.ViewKeyValues();
					break;
    		
				case Common.STORE_MOZILLA:
					break;
    		
				case Common.STORE_KDEWALLET:
					objKdeWallet.ViewKeyValues();
					break;
    		
				case Common.STORE_GNOMEKEYRING:
					objGnomeKeyring.ViewKeyValues();
					break;
			}
    	
			Logger.DbgLog("GUI:CasaMain.ViewKeyValue() - END");      	        
		}
    
    
    
		/// <summary>		
		/// ********************************************************************
		/// public void LinkKeyValue(object obj, EventArgs args)
		///
		/// MAIN-MENU: LINK
		/// Link selected miCASA key-value pairs.
		/// ********************************************************************
		/// </summary>	
		public void LinkKeyValue(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.LinkKeyValue() - BEGIN");
    	
			switch(notebookStores.CurrentPage)
			{
				case Common.STORE_MICASA:
					objMiCasa.ViewKeyValues();    				
					break;
    										
				case Common.STORE_FIREFOX:
					break;
    		
				case Common.STORE_MOZILLA:
					break;
    		
				case Common.STORE_KDEWALLET:
					break;
    		
				case Common.STORE_GNOMEKEYRING:
					break;
			}      
        
			Logger.DbgLog("GUI:CasaMain.LinkKeyValue() - END");  	          	          	          	        
		}
    
    
    
		/// <summary>		
		/// ********************************************************************
		/// public void CopyKeyValue(object obj, EventArgs args)
		///
		/// MAIN-MENU: COPY
		/// Not at impletemented.
		/// ********************************************************************
		/// </summary>	
		public void CopyKeyValue(object obj, EventArgs args)
		{			  	          	          	          	        
		}
    
    
    
		/// <summary>		
		/// ********************************************************************
		/// public void DeleteSecret(object obj, EventArgs args)
		///
		/// MAIN-MENU: DELETE SECRET
		/// Call store specific delete-secret handler.
		/// ********************************************************************
		/// </summary>	
		public void DeleteSecret(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.DeleteSecret() - BEGIN");
    	
			switch(notebookStores.CurrentPage)
			{
				case Common.STORE_MICASA:
					objMiCasa.OnDeleteActivated(obj, args);    				
					break;
    										
				case Common.STORE_FIREFOX:
					objFirefox.OnDeleteActivated(obj, args);   
					break;
    		
				case Common.STORE_MOZILLA:
					break;
    		
				case Common.STORE_KDEWALLET:
					objKdeWallet.OnDeleteActivated(obj, args);
					break;
    		
				case Common.STORE_GNOMEKEYRING:
					objGnomeKeyring.OnDeleteActivated(obj, args);
					break;			
			}   
        
			Logger.DbgLog("GUI:CasaMain.DeleteSecret() - END");          	          	          	          	        
		}
		
		
		
    		/// <summary>		
		/// ********************************************************************
		/// KONQUERER, KOPETE 
		/// NETWORK-MANAGER, GAIM
		///
		/// APPLICATION SSO
		/// Call respective application sso handlers.
		/// ********************************************************************
		/// </summary>	
		public void on_konquerer_activate(object obj, EventArgs args)
		{ 
			if(null != objKdeWallet)
				objKdeWallet.SSOKonquerer(obj, args);
		}
		
		public void on_kopete_activate(object obj, EventArgs args)
		{ 
			if(null != objKdeWallet)
				objKdeWallet.SSOKopete(obj, args);
		}
		
		public void on_networkmanager_activate(object obj, EventArgs args)
		{ 
			if(null != objGnomeKeyring)
				objGnomeKeyring.SSONetworkManager(obj, args);
		}
		
		public void on_gaim_activate(object obj, EventArgs args)
		{ 
			if(null != objGnomeKeyring)
				objGnomeKeyring.SSOGaim(obj, args);
		}
		
		
		
		/// <summary>		
		/// ********************************************************************
		/// public void ResetMasterPassword(object obj, EventArgs args)
		///
		/// CHANGE MASTER PASSWORD
		/// Change the previously set Master Password.
		/// ********************************************************************
		/// </summary>
		public void ResetMasterPassword(object obj, EventArgs args)
		{    	    	
			Logger.DbgLog("GUI:CasaMain.ResetMasterPassword() - BEGIN");
    	
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogResetMP", null);
			gxmlTemp.Autoconnect (this);
			dialogResetMP.TransientFor = windowMain;
			entryOldMP.Text="";
			entryNewMP1.Text="";
			entryNewMP2.Text="";		
			
			Logger.DbgLog("GUI:CasaMain.ResetMasterPassword() - END");          	          	          	          	        
		}    
    
		public void okbuttonResetMasterPWD_clicked(object abj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.okbuttonResetMasterPWD_clicked() - BEGIN");
    	
			if (entryOldMP.Text == "")
			{
				DisplayError("You must enter you current Master Password");
				return;
			}

			// check Current MP
			if (entryOldMP.Text != "")
			{
				int rcode = miCASA.SetMasterPassword(1, entryOldMP.Text);
				if (rcode != 0)
				{
					DisplayError("Current Master Password is not correct");
					return;
				}
			}

			if (entryNewMP1.Text.Length < 8)
			{
				Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogShortPassword", null);
				gxmlTemp.Autoconnect (this);
				return;
			}

			if (entryNewMP1.Text != entryNewMP2.Text)
			{
				DisplayError("New Master Passwords do not match");
				return;
			}
		
			if (miCASA.ChangeMasterPassword(entryOldMP.Text, entryNewMP1.Text))
				dialogResetMP.Destroy();
			else
				DisplayError("Change failed");
		    	
			Logger.DbgLog("GUI:CasaMain.okbuttonResetMasterPWD_clicked() - END");
		}
    
		public void cancelbuttonResetMasterPWD_clicked(object abj, EventArgs args)
		{
			dialogResetMP.Destroy();
		}	

		private void DisplayError(String sErrorMessage)
		{
			MessageDialog md = new MessageDialog(windowMain,
				Gtk.DialogFlags.Modal,
				Gtk.MessageType.Warning,
				Gtk.ButtonsType.Close,
				sErrorMessage);

			md.Response +=new ResponseHandler(md_Response);
			//md_ResponseCloseWindow
			md.Show();

		}
		
		public void on_helpbuttonResetMP_clicked(object obj, EventArgs args)
		{
			Common.ShowHelpUrl("CASAMasterPasswordAuthentication.htm");
		}
          
    
		/// <summary>		
		/// ********************************************************************
		/// public void PersistentStorage(object obj, EventArgs args)
		///
		/// PERSISTENT STORAGE
		/// As of now, We are not using this routine.
		/// ********************************************************************
		/// </summary>
		public void PersistentStorage(object obj, EventArgs args)
		{    	    	
			Logger.DbgLog("GUI:CasaMain.PersistentStorage() - BEGIN");
    	
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogPersistentStorage", null);
			gxmlTemp.Autoconnect (this);
			dialogPersistentStorage.TransientFor = windowMain;
			entryMasterPassword1.Text="";
			entryMasterPassword2.Text="";
			
			if(IsMasterPasswordSet() == true)
			{        
				entryMasterPassword1.Sensitive=false;
				entryMasterPassword2.Sensitive=false;
				okbuttonPersistentStorage.Sensitive=false;
			}
        
			Logger.DbgLog("GUI:CasaMain.PersistentStorage() - END");          	          	          	          	        
		}    
    
		public void okbuttonPersistentStorage_clicked(object abj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.okbuttonPersistentStorage_clicked() - BEGIN");
    	
			if(("" != entryMasterPassword1.Text && "" != entryMasterPassword2.Text) && (entryMasterPassword1.Text == entryMasterPassword2.Text))
			{    		
				//StorePolicyInterface.SetMasterPassword(entryMasterPassword1.Text);
				miCASA.SetMasterPassword(0, entryMasterPassword1.Text);
				dialogPersistentStorage.Destroy();
			}
      	
			Logger.DbgLog("GUI:CasaMain.okbuttonPersistentStorage_clicked() - END");
		}
    
		public void cancelbuttonPersistentStorage_clicked(object abj, EventArgs args)
		{
			dialogPersistentStorage.Destroy();
		}
    
    
    
		/// <summary>		
		/// ********************************************************************
		/// public void Preferences(object obj, EventArgs args)
		///
		/// PREFERENCES
		/// Handle Preferences dialog to set the store aggregation policy.
		/// (Main Menu: Options > Preferences).
		/// ********************************************************************
		/// </summary>
		public void Preferences(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.Preferences() - BEGIN");
    	
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogPreferences", null);
			gxmlTemp.Autoconnect (this);
			dialogPreferences.SetPosition(Gtk.WindowPosition.CenterOnParent);
			dialogPreferences.TransientFor = windowMain;		
			checkbuttonFirefox.Active = Common.IS_FIREFOX;
			checkbuttonMozilla.Active = Common.IS_MOZILLA;			
			checkbuttonGnomeKeyring.Active = Common.IS_GNOMEKEYRING;
			checkbuttonKdeWallet.Active = Common.IS_KDEWALLET;						
			checkbuttonFirefox.Sensitive = Common.IS_FIREFOX_AVAILABLE;
			checkbuttonKdeWallet.Sensitive = Common.IS_KDEWALLET_AVAILABLE;
			checkbuttonGnomeKeyring.Sensitive = Common.IS_GNOMEKEYRING_AVAILABLE;						
			
			if (Common.IS_WINDOWS)
			{
				checkbuttonGnomeKeyring.Sensitive = false;
				checkbuttonKdeWallet.Sensitive = false;
			}
        
			Logger.DbgLog("GUI:CasaMain.Preferences() - END");  	          	        
		}
    
		public void okbuttonPreferences_clicked(object abj, EventArgs args)
		{    	
			Logger.DbgLog("GUI:CasaMain.okbuttonPreferences_clicked() - BEGIN");
        
			string[] storeID = new string[]{""};
        
			Common.IS_FIREFOX 		= checkbuttonFirefox.Active;	
			Common.IS_MOZILLA	  	= checkbuttonMozilla.Active;
			Common.IS_GNOMEKEYRING 	= checkbuttonGnomeKeyring.Active;
			Common.IS_KDEWALLET	  	= checkbuttonKdeWallet.Active;
			dialogPreferences.Destroy();
			
			/// FIREFOX
			if(Common.IS_FIREFOX)
			{
				if( null == objFirefox )
					objFirefox = new Firefox();
				//objFirefox.AggregateStore();
				notebookStores.GetNthPage(Common.STORE_FIREFOX).Visible = true;
				StorePolicyInterface.SetAggregationPolicy(Common.STORE_FIREFOX, true, storeID, 1);
			}
			else
			{
				//objFirefox = null; 
				notebookStores.GetNthPage(Common.STORE_FIREFOX).Visible = false;
				StorePolicyInterface.SetAggregationPolicy(Common.STORE_FIREFOX, false, storeID, 1);
			}
			
			/// MOZILLA
			if(Common.IS_MOZILLA)
			{
				if( null == objMozilla )
					objMozilla = new Mozilla();			
				//objMozilla.AggregateStore();
				notebookStores.GetNthPage(Common.STORE_MOZILLA).Visible = true;
				StorePolicyInterface.SetAggregationPolicy(Common.STORE_MOZILLA, true, storeID, 1);
			}
			else
			{
				//objMozilla = null;
				notebookStores.GetNthPage(Common.STORE_MOZILLA).Visible = false;
				StorePolicyInterface.SetAggregationPolicy(Common.STORE_MOZILLA, false, storeID, 1);
			}
			
			/// KDEWALLET
			if(Common.IS_KDEWALLET)
			{
				if( null == objKdeWallet )
					objKdeWallet = new KdeWallet();			
				//objKdeWallet.AggregateStore();
				notebookStores.GetNthPage(Common.STORE_KDEWALLET).Visible = true;
				StorePolicyInterface.SetAggregationPolicy(Common.STORE_KDEWALLET, true, storeID, 1);
			}
			else
			{
				//objKdeWallet = null;
				notebookStores.GetNthPage(Common.STORE_KDEWALLET).Visible = false;
				StorePolicyInterface.SetAggregationPolicy(Common.STORE_KDEWALLET, false, storeID, 1);
			}
			
			/// GNOMEKEYRING
			if(Common.IS_GNOMEKEYRING)
			{
				if( null == objGnomeKeyring )
					objGnomeKeyring = new GnomeKeyring();			
				//objGnomeKeyring.AggregateStore();
				notebookStores.GetNthPage(Common.STORE_GNOMEKEYRING).Visible = true;
				StorePolicyInterface.SetAggregationPolicy(Common.STORE_GNOMEKEYRING, true, storeID, 1);
			}
			else
			{
				//objGnomeKeyring = null;
				notebookStores.GetNthPage(Common.STORE_GNOMEKEYRING).Visible = false;
				StorePolicyInterface.SetAggregationPolicy(Common.STORE_GNOMEKEYRING, false, storeID, 1);
			}
			StorePolicyInterface.SaveAggregationPolicy();
			
			Logger.DbgLog("GUI:CasaMain.okbuttonPreferences_clicked() - END");          	   	
		}
    
		public void cancelbuttonPreferences_clicked(object abj, EventArgs args)
		{
			dialogPreferences.Destroy();
		}
    
		public void on_helpbuttonPreferences_clicked(object obj, EventArgs args)
		{
			Common.ShowHelpUrl("Preferences.htm");
		}




		/*
		private bool IsTraySetForStartup()
		{
#if W32
			string sStartup = CommonGUI.ReadRegKey(
				Registry.CurrentUser,
				"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
				"CASA Tray");
			
			if (sStartup == null || sStartup.Length < 1)
				return false;
			else 
				return true;
#endif

#if LINUX
		// TODO check startup script for the user.
		return true;
#endif
		}
		*/

		private static void md_ResponseCloseWindow(object o, ResponseArgs args)
		{		
			MessageDialog md = (MessageDialog)o;
			if (md != null)
			{
				md.Destroy();
				Application.Quit();
			}
		}
	
		internal void PromptUserForStartup()
		{
			// prompt user 						
			MessageDialog md=new MessageDialog(null,Gtk.DialogFlags.Modal,
			Gtk.MessageType.Question,
			Gtk.ButtonsType.YesNo,
			"Show the Tray Icon at startup?");

			md.SetPosition(Gtk.WindowPosition.Center);
			md.Response +=new ResponseHandler(md_Response);
			md.Modal = true;
			md.SetIconFromFile(Common.CASAICONS);
			md.Show();
		}

		private void md_Response(object o, ResponseArgs args)
		{
			if (args.ResponseId.Equals(Gtk.ResponseType.Yes))
			{
#if W32
				// get our program path
				String sPath = "\"" +Environment.GetEnvironmentVariable("ProgramFiles")+
				"\\Novell\\CASA\\bin\\CASAManager.exe\" -tray";
			
				CommonGUI.WriteRegKey(Registry.CurrentUser,
				"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
				"CASA Tray",
				sPath);			
#endif
			
				// TODO:  Set startup script for the user
			}

			MessageDialog md = (MessageDialog)o;
			if (md != null)
			{
				md.Destroy();			
			}
		}
    
    
    
		/// <summary>		
		/// ********************************************************************
		/// public void on_create_sample_secrets1_activate(object obj, EventArgs arg)
		/// public void on_remove_test_secrets1_activate(object obj, EventArgs args)
		///
		/// DEBUG LOGGING
		/// Create and delete miCASA test secrets.
		/// ********************************************************************
		/// </summary>
		public void on_create_sample_secrets1_activate(object obj, EventArgs arg)
		{
			MiCasaRequestReply.Send(MiCasaRequestReply.VERB_CREATE_TEST_SECRETS, null, null, null, null);
			//StoreDataInterface.RefreshAllStores();				    	
			objMiCasa.AggregateStore();
		}

		public void on_remove_test_secrets1_activate(object obj, EventArgs args)
		{
			MiCasaRequestReply.Send(MiCasaRequestReply.VERB_REMOVE_TEST_SECRETS, null, null, null, null);
			//StoreDataInterface.RefreshAllStores();				    	
			objMiCasa.AggregateStore();
		}
    
    
    
		/// <summary>		
		/// ********************************************************************
		/// public void About(object obj, EventArgs args)
		///
		/// ABOUT
		/// Display the About dialog box.
		/// ********************************************************************
		/// </summary>
		public void About(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.About() - BEGIN");
    	
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogAbout", null);
			gxmlTemp.Autoconnect (this);
			dialogAbout.TransientFor = windowMain;        	
			Logger.DbgLog("GUI:CasaMain.About() - END");
		}
    
		public void closebuttonAbout_clicked(object obj, EventArgs args)
		{
			dialogAbout.Destroy();
		}
    
		public void on_buttonNovell_clicked(object obj, EventArgs args)
		{
			Common.ShowUrl("http://www.novell.com");
		}
	
	
	
		/// <summary>		
		/// ********************************************************************
		/// 
		///
		/// SINGLE INSTANCE OF CASAMANAGER
		/// Check for single instance of CASAManager.exe.
		/// ********************************************************************
		/// </summary>
		public void on_dialogSingleInstance_delete_event(object obj, DeleteEventArgs args)
		{
			dialogSingleInstance.Destroy();	
			HandleQuit();					
		}
	
		public void on_buttonSIClose_clicked(object obj, EventArgs args)
		{
			dialogSingleInstance.Destroy();	
			HandleQuit();					
		}
    

		/// <summary>		
		/// ********************************************************************
		/// public void StillRunning(object obj, EventArgs args)
		///
		/// GO TO TRAY
		/// Inform that CASAManager will continue to run in the tray.
		/// ********************************************************************
		/// </summary>
		public void StillRunning(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.StillRunning() - BEGIN");
    	
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogStillRunning", null);
			gxmlTemp.Autoconnect (this);
			dialogStillRunning.TransientFor = windowMain;        	
			Logger.DbgLog("GUI:CasaMain.StillRunning() - END");
		}
    
		public void btnStillRunning_clicked(object obj, EventArgs args)
		{
			if (checkbuttonCloseMessage.Active)
			{								
				m_bNotifyUser = false;
			}

			dialogStillRunning.Destroy();
		}
    
		
		/// <summary>		
		/// ********************************************************************
		/// public void on_view_log_file1_activate(object obj, EventArgs args)
		///
		/// LOGGING
		/// Logging window operations.
		/// ********************************************************************
		/// </summary>
		public void on_view_log_file1_activate(object obj, EventArgs args)
		{
		}

		public void on_enable_logging1_activate(object obj, EventArgs args)
		{
			Gtk.CheckMenuItem miLog = (Gtk.CheckMenuItem)obj;
			if (miLog.Active)
				OpenLogWindow();
			else
				CloseLogWindow();
		}

		internal void OpenLogWindow()
		{
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogLog", null);
			gxmlTemp.Autoconnect (this);		
				
			Gtk.TreeViewColumn col1 = new Gtk.TreeViewColumn("", new Gtk.CellRendererText(), "text", 0);
			tvLog.AppendColumn(col1);
			Logger.ActivateLogWindow(tvLog);
		}	

		internal void CloseLogWindow()
		{
			Logger.StopLogWindow();
			dialogLogin.Dispose();
			dialogLogin.Destroy();
		}
				
		
		/// <summary>
		/// ********************************************************************
		/// public void OnMenuHelpActivate(object sender, EventArgs args)
		///
		/// HELP
		/// Opens help contents for CASAManager.
		/// ********************************************************************
		/// </summary>
		public void OnMenuHelpActivate(object sender, EventArgs args)
		{
			Common.ShowHelpUrl("Contents.htm");
		}
				
		
		/// <summary>		
		/// ******************************************************************** 
		/// internal void Focus()
		///
		/// MAIN WINDOW FOCUS
		/// Handling Hocus-Focus of Main window.
		/// ********************************************************************
		/// </summary>
		internal void Focus()
		{
			if (windowMain != null)
				windowMain.Present();
		}

		internal void Hide()
		{
			if (windowMain != null)
				windowMain.Visible = false;
		}

		internal void Show()
		{
			if (windowMain == null)
				MasterPasswordAuthentication();
			else
			{			
				RefreshStores();
				windowMain.Visible = true;
			}
		}

		internal void Dispose()
		{	
			if (windowMain != null)
				windowMain.Destroy();
		}
		
		
		/// <summary>		
		/// ********************************************************************
		/// private void HandleQuit()
		///
		/// QUIT CASAMANAGER
		/// Quit CASAManager.exe, also handle the closing the trayicon.
		/// ********************************************************************
		/// </summary>
		private void HandleQuit()
		{
			if (mCasaTray != null)
			{
				if (m_bNotifyUser)
				{
					// StillRunning(null, null);
					// prompt user
					MessageDialog md=new MessageDialog(null,Gtk.DialogFlags.Modal,
					Gtk.MessageType.Info,
					Gtk.ButtonsType.Close,
					"CASA Manager will continue to run in the task bar.");

					md.SetPosition(Gtk.WindowPosition.Center);
					md.Response +=new ResponseHandler(md_Response);
					md.Modal = true;
					md.SetIconFromFile(Common.CASAICONS);
					md.Show();				
					m_bNotifyUser = false;					
				}
				mCasaTray.CasaManagerQuit();
				return;
			}
			else
				Application.Quit();
		}
		
		public void QuitApplication(object obj, EventArgs args)
		{
			Logger.DbgLog("GUI:CasaMain.QuitApplication() - BEGIN");
    	
			HandleQuit();    	
    	
			Logger.DbgLog("GUI:CasaMain.QuitApplication() - END");    	        
		}
    
        	public void OnWindowMainDeleted(object obj, DeleteEventArgs args) 
		{
			Logger.DbgLog("GUI:CasaMain.OnWindowMainDeleted() - BEGIN");
    				
			HandleQuit();			
			args.RetVal = true;
        
			Logger.DbgLog("GUI:CasaMain.OnWindowMainDeleted() - END");
		}		
		
	}
}

/// ********************************************************************
///								END OF FILE
/// ********************************************************************