/***********************************************************************
 * 
 *  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.
 * 
 ***********************************************************************/

using System;
using Gtk;
using Glade;

using Novell.CASA;
using Novell.CASA.MiCasa.Common;
using Novell.CASA.MiCasa.Communication;

#if W32
using Microsoft.Win32;
#endif 
namespace Novell.CASA.GUI
{
	/// <summary>
	/// Summary description for CommonGUI.
	/// </summary>
	public class CommonGUI
	{
		
		[Glade.Widget] 
		Gtk.Label			label86,
			label88;

		[Glade.Widget] 
		Gtk.Entry	 		entryMasterPassword3, 
							entryMasterPassword4;

		[Glade.Widget] 
		Gtk.Dialog 			dialogLogin;

		Gtk.Window mainWindow = new Window("Test");

		private CasaMain mCasaInstance = null;
		private CasaTray mTrayInstance = null;
				
		public CommonGUI()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// HandleUnlock dialog
		/// </summary>
		public void HandleUnlock(CasaMain managerInstance, CasaTray trayInstance)
		{
	    	
			mCasaInstance = managerInstance;
			mTrayInstance = trayInstance;
			

			//Logger.DbgLog("GUI:CasaMain.Login() - IsMasterPasswordSet returned false");    		    
#if W32
			Glade.XML gxmlTemp = new Glade.XML ("../images/casa.glade", "dialogLogin", null);
#endif
#if LINUX
				Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogLogin", null);				
#endif

			gxmlTemp.Autoconnect (this);
			//dialogLogin.TransientFor = windowMain;        
				
			label86.Text = "Enter your Master Password to unlock your secrets";
			entryMasterPassword3.Text="";
			label88.Hide();
			entryMasterPassword4.Hide();			
			dialogLogin.SetPosition(Gtk.WindowPosition.Center);
			dialogLogin.Destroyed += new EventHandler(dialogLogin_Destroyed);
			dialogLogin.Show();	
		}

		public void on_helpbuttonAuthentication_clicked(object sender, EventArgs args)
		{
			Common.ShowHelpUrl("CASAMasterPasswordAuthentication.htm");
		}

		internal void menuDestroyMiCasa_Activated(CasaMain casaMain, CasaTray tray)
		{
			mCasaInstance = casaMain;
			mTrayInstance = tray;

			// prompt user 						
			MessageDialog md=new MessageDialog(null,Gtk.DialogFlags.Modal,
				Gtk.MessageType.Warning,
				Gtk.ButtonsType.OkCancel,
				"This will destroy all of your miCASA secrets.\r\nAre you sure?");

			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.Ok))
			{
				MiCasaRequestReply.Send(MiCasaRequestReply.VERB_REMOVE_ALL_SECRETS);
				if (mCasaInstance != null)
					mCasaInstance.objMiCasa.AggregateStore();
			}

			MessageDialog md = (MessageDialog)o;
			if (md != null)
			{
				md.Destroy();			
			}
		}

		
		private void md_Response2(object o, ResponseArgs args)
		{
			MessageDialog md = (MessageDialog)o;
			if (md != null)
			{
				md.Destroy();			
			}
		}
		
		public void okbuttonLogin_clicked(object abj, EventArgs args)
		{    
			if( 0 == miCASA.SetMasterPassword(0, entryMasterPassword3.Text) )
			{
				// unlock it
				MiCasaRequestReply.Send(MiCasaRequestReply.VERB_UNLOCK_STORE, entryMasterPassword3.Text);
				dialogLogin.Destroy();  
		
			}
			else
			{
				// prompt user 						
				MessageDialog md=new MessageDialog(
					mainWindow,Gtk.DialogFlags.Modal,
					Gtk.MessageType.Warning,
					Gtk.ButtonsType.Ok,
					"Master Password entered is incorrect");

				md.Response +=new ResponseHandler(md_Response2);
				md.SetPosition(Gtk.WindowPosition.CenterOnParent);		
				md.Modal = true;			
				md.SetIconFromFile(Common.CASAICONS);
				md.Show();
			}				
		}
            
		public void closebuttonLogin_clicked(object abj, EventArgs args)
		{
			dialogLogin.Destroy();  
		}
    
    
		public void OnDialogLoginDeleted(object obj, DeleteEventArgs args) 
		{		    			
			args.RetVal = true;        		
		}
    
		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);
		}

		private void dialogLogin_Destroyed(object sender, EventArgs e)
		{
			bool bStoreLocked = MiCASAStore.IsLocked();
			if (mCasaInstance != null)
			{	
				if (bStoreLocked)
					mCasaInstance.LockGUI();
				else
					mCasaInstance.UnlockGUI();
			}

			if (mTrayInstance != null)
			{
				if (bStoreLocked)
					mTrayInstance.UpdateTrayIcon(true);
				else
					mTrayInstance.UpdateTrayIcon(false);				
			}
		}
#if W32
		internal static string ReadRegKey(RegistryKey rk, string sSubKey, string KeyName)
		{
			// Opening the registry key
			// RegistryKey rk = Registry.Users;
			// Open a subKey as read-only
			RegistryKey sk1 = rk.OpenSubKey(sSubKey);
			// If the RegistrySubKey doesn't exist -> (null)
			if ( sk1 == null )
			{
				return null;
			}
			else
			{
				try 
				{
					// If the RegistryKey exists I get its value
					// or null is returned.
					return (string)sk1.GetValue(KeyName.ToUpper());
				}
				catch (Exception e)
				{
					//ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
					return null;
				}
			}
		}

		internal static void WriteRegKey(RegistryKey rk, string sSubKey, string KeyName, string sKeyValue)
		{
			RegistryKey sk1 = rk.OpenSubKey(sSubKey, true);			
			// If the RegistrySubKey doesn't exist -> (null)
			if ( sk1 == null )
			{
				//return null;
			}
			else
			{
				try 
				{
					// If the RegistryKey exists I get its value
					// or null is returned.
					sk1.SetValue(KeyName, sKeyValue);
					sk1.Close();
				}
				catch (Exception e)
				{
					sk1.Close();
					//ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
					//Console.Write(e.ToString());
					Logger.DbgLog("GUI:CommonGUI.WriteRegKey EXCEPTION: \n" + e.ToString());
					//return null;
				}
			}


		}

#endif
	}
}