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

public class KdeWallet : Store
{
	
	Gtk.TreeStore 		tsSecretIDKdeWallet,
						tsNativeInfoKdeWallet,
						tsKeyValue;
						
	CellRendererText    cellEditable;
	
	ArrayList 			arrDeletedKeys = null;
	
	public bool IS_STORE_AGGREGATED = false;
	
	string[] stringKonquererTemplates = new string[]{ "New Website",
													  "Novell Website   (novell.com)",
													  "Novell Innerweb   (innerweb.novell.com)",
													  "Novell Bugzilla   (bugzilla.novell.com)",
													  "Novell Forge   (forge.novell.com)",
													  "Bandit Project   (bandit-project.org)",
													  "Groupwise Webaccess   (gmail.novell.com)",
													  "Gmail   (gmail.com)",
													  "Yahoo! Mail   (mail.yahoo.com)",
													  "MSN Hotmail   (hotmail.com)"};

	#region Glade Widgets
	 
  	[Glade.Widget]
  	public Gtk.Window 	windowMain;
	
	public Gtk.TreeView tvSecretIDKdeWallet;
  						
  	[Glade.Widget]
  	Gtk.TreeView 		tvKeyValue, 
  						tvNativeInfoKdeWallet;
  					  	  	
	[Glade.Widget]
	Gtk.Dialog 			dialogNewSecret,
						dialogManageSecret,
						dialogConfirmDelete,
						dialogLogin,
						dialogKonquerer,
						dialogKopete,
						dialogSpecialCharacter;
	  						
  	[Glade.Widget] 
  	Gtk.Menu 			menuRightClick;
  	
  	[Glade.Widget] 
  	Gtk.Entry 			entrySecretID,
  						entryKey,
  						entryValue,
						entryMasterPassword3,
						entryMasterPassword4,
						entryDeleteSecretID,
						entryKonquererURL,
						entryKonquererUsrKey,
						entryKonquererUsrValue,
						entryKonquererPwdKey,
						entryKonquererPwdValue;
  	
  	[Glade.Widget] 
  	Gtk.CheckButton		cbuttonShowPassword;
	
	[Glade.Widget] 
  	Gtk.Label			label86,
						label88;
  	
  		
  	[Glade.Widget] 
  	Gtk.MenuItem 		cmiNewKey,
  						cmiDelete,
  						cmiView,
  						cmiLink,
  						cmiCopy;
  	
  	[Glade.Widget] 
  	Gtk.Combo			comboKonquererTemplates;
  	#endregion
  	
  	
  	///#######################################################################
	/// CONSTRUCTOR 
		
	/// <summary>		
	///  
	/// </summary>	  	  	
  	public KdeWallet()
  	{
  		Logger.DbgLog("GUI:KdeWallet.KdeWallet() - BEGIN");
  		
		/// SecretID TreeStore	
		tvSecretIDKdeWallet = (Gtk.TreeView)CasaMain.gxmlMain.GetWidget("tvSecretIDKdeWallet");
		tsSecretIDKdeWallet = new TreeStore(typeof(string), typeof(string[]), typeof(string[]), typeof(string), typeof(string[]), typeof(string[]));		
		tvSecretIDKdeWallet.AppendColumn("Secret ID",new CellRendererText(),"text",0);
		tvSecretIDKdeWallet.Model = tsSecretIDKdeWallet;		
		tvSecretIDKdeWallet.RowActivated 		+= new RowActivatedHandler(OntvSecretIDKdeWalletRowActivated);
		tvSecretIDKdeWallet.ButtonReleaseEvent 	+= new ButtonReleaseEventHandler(OnRightClicked);
		tvSecretIDKdeWallet.CursorChanged 		+= new EventHandler(OnCursorChanged);
		/// NativeInfo TreeStore			
		tvNativeInfoKdeWallet = (Gtk.TreeView)CasaMain.gxmlMain.GetWidget("tvNativeInfoKdeWallet");
		tsNativeInfoKdeWallet = new TreeStore(typeof(string), typeof(string));		
		tvNativeInfoKdeWallet.AppendColumn("NativeKey",new CellRendererText(),"text",0);
		tvNativeInfoKdeWallet.AppendColumn("NativeValue",new CellRendererText(),"text",1);
		tvNativeInfoKdeWallet.Model = tsNativeInfoKdeWallet;
		tvNativeInfoKdeWallet.ModifyBase(StateType.Normal,new Gdk.Color(0xff,0xff,0xe6));
		/// Aggregate the store
		//AggregateStore();
		
		Logger.DbgLog("GUI:KdeWallet.KdeWallet() - END");  	       	     
  	} 
  	
  	
  	
  	///#######################################################################
	/// AGGREGATE STORE 
		
	/// <summary>		
	///  
	/// </summary>
  	public override void AggregateStore()
  	{
		Logger.DbgLog("GUI:KdeWallet.AggregateStore() - BEGIN");
		
		try
		{
			tsSecretIDKdeWallet.Clear();  	
			tsNativeInfoKdeWallet.Clear();
  			StoreDataInterface.AggregateStore(Common.STORE_KDEWALLET);
  			StoreDataInterface.ReadStore(Common.STORE_KDEWALLET,ref tsSecretIDKdeWallet);
  		}
  		catch(Exception exp)
  		{
  			Logger.DbgLog("GUI:KdeWallet.AggregateStore() - EXCEPTION" + exp.ToString());
  		}
  		
  		Logger.DbgLog("GUI:KdeWallet.AggregateStore() - END");
  	}


	public override void ClearViewOnStore()
	{
		tsSecretIDKdeWallet.Clear();  	
		tsNativeInfoKdeWallet.Clear();
	}
  	  	
  	
  	///#######################################################################
	/// DISPLAY NATIVE INFO 
		
	/// <summary>		
	///  
	/// </summary>	
  	private void OnCursorChanged(object obj, EventArgs args)
  	{
  		Logger.DbgLog("GUI:KdeWallet.OnCursorChanged() - BEGIN");
  		
  		TreeModel 	model;
		TreeIter 	iter;
		string 	 	selected 		= null;  		
		string[] 	NativeKeys 		= null,
				 	NativeValues	= null;
		
		if( tvSecretIDKdeWallet.Selection.GetSelected (out model, out iter) )
			selected = (string) model.GetValue(iter, 0);

		if( (null != selected) && (selected.Length > 0) )
		{			
			tsNativeInfoKdeWallet.Clear();
			/// Populate NativeInfo
			tsNativeInfoKdeWallet.AppendValues("Wallet Name", "= "+model.GetValue(iter,3));			
			NativeKeys   = (string[]) model.GetValue(iter, 4);
			NativeValues = (string[]) model.GetValue(iter, 5);			
			for( int i=0; i< NativeKeys.Length; i++ )
				if( (null != NativeValues[i]) && ("" != NativeValues[i]) )
					tsNativeInfoKdeWallet.AppendValues(NativeKeys[i], "= "+NativeValues[i]);		
			tvNativeInfoKdeWallet.ShowAll();
		} 
  		
  		Logger.DbgLog("GUI:KdeWallet.OnCursorChanged() - END");
  	}
  	
  	
  	///#######################################################################
	/// RIGHT-CLICK CONTEXT MENU 
		
	/// <summary>		
	///  
	/// </summary>	  	  	  	
	public void OnRightClicked(object obj, ButtonReleaseEventArgs args)
	{		
		Logger.DbgLog("GUI:KdeWallet.OnRightClicked() - BEGIN");
		
		if( 3 == args.Event.Button )
		{
			try
			{
				Logger.DbgLog("GUI:GnomeKeyring.OnRightClicked() - Context menu opened.");
				Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "menuRightClick", null);
        			gxmlTemp.Autoconnect (this);
				menuRightClick.Popup(null, null, null, IntPtr.Zero, 3, Gtk.Global.CurrentEventTime);
				
				if( 0 != tvSecretIDKdeWallet.Selection.CountSelectedRows() )
					cmiLink.Sensitive = cmiCopy.Sensitive = false;
				else
					cmiNewKey.Sensitive = cmiView.Sensitive = cmiLink.Sensitive = cmiCopy.Sensitive = cmiDelete.Sensitive = false;
			}	
			catch(Exception exp)
			{
				Logger.DbgLog("GUI:GnomeKeyring.OnRightClicked() - EXCEPTION" + exp.ToString());		
			}		
		}
		
		Logger.DbgLog("GUI:KdeWallet.OnRightClicked() - END");	
	}
	
  	
  	///#######################################################################
	/// VIEW KEY-VALUES 
		
	/// <summary>		
	///  
	/// </summary>	  	
  	public override void ViewKeyValues()
	{
		Logger.DbgLog("GUI:KdeWallet.ViewKeyValues() - BEGIN");
		
		TreeModel 	model;
		TreeIter 	iter;
		string 		selected = null;
		string[] 	keys 	 = null,
				values   = null;				
		
		try
		{
		if( null == arrDeletedKeys )
			arrDeletedKeys = new ArrayList();
		else
			arrDeletedKeys.Clear();

		if( tvSecretIDKdeWallet.Selection.GetSelected (out model, out iter) )
		{
			selected = (string)   model.GetValue(iter, 0);
			keys 	 = (string[]) model.GetValue(iter, 1);
			values   = (string[]) model.GetValue(iter, 2);
				
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogManageSecret", null);
	    		gxmlTemp.Autoconnect (this);
			dialogManageSecret.TransientFor = (Gtk.Window)CasaMain.gxmlMain.GetWidget("windowMain");
			dialogManageSecret.Title = "KDE Wallet - Manage Secret";
			
			cellEditable = new CellRendererText();
			cellEditable.Editable = true;
			cellEditable.Edited += new EditedHandler(OnKeyValueEdited);		
			///			   KEY:0          VALUE:1         VALUE-DUP:2  	  DIRTY-BIT:3   LINK:4 			
			tsKeyValue = new TreeStore(typeof(string),typeof(string), typeof(string), typeof(bool), typeof(string));
			tvKeyValue.AppendColumn("Key",new CellRendererText(),"text",0);
			tvKeyValue.AppendColumn("Value",cellEditable,"text",2);
			tvKeyValue.AppendColumn("Linked", new CellRendererText(), "text", 4);

			entrySecretID.Text = selected;
		
			for( int i=0; i< keys.Length; i++ )
			{
				if( (null != keys[i]) && (null != values[i]) )
					tsKeyValue.AppendValues(keys[i], values[i], "********", false, "No");					
			}
			tvKeyValue.Model = tsKeyValue;
			entryKey.HasFocus = true;
		}
		}
		catch(Exception exp)
		{
			Logger.DbgLog("GUI:KdeWallet.ViewKeyValues() - EXCEPTION" + exp.ToString());
		}
		
		Logger.DbgLog("GUI:KdeWallet.ViewKeyValues() - END");
	}
	
	
	/// <summary>
	/// EDIT KEY-VALUE 
	/// </summary>
	public void OnKeyValueEdited(object obj, EditedArgs args)	
	{
		Logger.DbgLog("GUI:KdeWallet.OnKeyValueEdited() - BEGIN");
		
		TreeModel model;
		TreeIter  iter;	
		object    val;
		string    KeyName  = null,
				  KeyValue = null;
		string[]  Keys	   = null,
				  Values   = null;  
		
		try
		{		
			tvKeyValue.Selection.GetSelected (out model, out iter);		
			val = tsKeyValue.GetValue(iter,0); 
			KeyName = val.ToString();
			if( true == cbuttonShowPassword.Active )
				val = tsKeyValue.GetValue(iter,1);
			else
			 	val = tsKeyValue.GetValue(iter,2);
			KeyValue = val.ToString();
							
			tvSecretIDKdeWallet.Selection.GetSelected (out model, out iter);		
			
			if( false == entrySecretID.Editable )
			{
				if( ("" != args.NewText) && (Common.MAX_LEN >= args.NewText.Length) && (KeyValue != args.NewText) )
				{
				if(  Common.STATUS_SUCCESS == StoreDataInterface.UpdateStore(Common.STORE_KDEWALLET, Common.OPERATION_MODIFY_KEY, KeyName, args.NewText, ref model, ref iter) )
				{
					Logger.DbgLog("GUI:KdeWallet.OnKeyValueEdited() - StoreDataInterface.UpdateStore() succeeded");
					tvKeyValue.Selection.GetSelected (out model, out iter);				
					tsKeyValue.SetValue(iter, 1, args.NewText);					
					tsKeyValue.SetValue(iter, 2, "********");
					
					tvSecretIDKdeWallet.Selection.GetSelected (out model, out iter);
					Keys   = (string[]) model.GetValue(iter, 1);
					Values = (string[]) model.GetValue(iter, 2);		
					for( int i=0; i< Keys.Length; i++ )
					{
						if( Keys[i] == KeyName )
						{
							Values[i] = args.NewText;
							tsSecretIDKdeWallet.SetValue(iter, 2, Values);				
							break;
						}
					}
					//AggregateStore();
				}
				else
					Logger.DbgLog("GUI:KdeWallet.OnKeyValueEdited() - ERROR: STATUS_STORE_UPDATEFAILED");
				}				
			}
			else if( (Common.MAX_LEN >= args.NewText.Length) )
			{
				tvKeyValue.Selection.GetSelected (out model, out iter);				
				tsKeyValue.SetValue(iter, 1, args.NewText);
				tsKeyValue.SetValue(iter, 2, "********");				
			}				
		}
		catch(Exception exp)
		{
			Logger.DbgLog("GUI:KdeWallet.OnKeyValueEdited() - EXCEPTION:" + exp.ToString());
		}
		
		Logger.DbgLog("GUI:KdeWallet.OnKeyValueEdited() - END");
		
	}
	
	
	/// <summary>
	/// ADD BUTTON CLICKED 
	/// </summary>
	public void on_buttonNewAdd_clicked(object obj, EventArgs args)
	{
		Logger.DbgLog("GUI:KdeWallet.on_buttonNewAdd_clicked() - BEGIN");
				
		if( ("" != entryKey.Text) && ("" != entryValue.Text) )
		{
			TreeIter  iterKey;			
			ArrayList arrKeys   = null,
				      arrValues = null;
			object    val = null;
			arrKeys   = new ArrayList();
			arrValues = new ArrayList();			
			
			if(tsKeyValue.GetIterFirst(out iterKey))
			{
				do
				{									
					val = tsKeyValue.GetValue(iterKey,0);
					arrKeys.Add(val.ToString());
					val = tsKeyValue.GetValue(iterKey,1);
					arrValues.Add(val.ToString());
				}
				while( tsKeyValue.IterNext(ref iterKey) );
			}		
			if( -1 == arrKeys.IndexOf(entryKey.Text) )
			if( true == Common.ValidateString(entryKey.Text) )
			{
				iterKey = tsKeyValue.AppendValues(entryKey.Text, entryValue.Text, "********", true, "No");
				entryKey.Text = entryValue.Text = "";				
			}
			else
			{
				Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogSpecialCharacter", null);
				gxmlTemp.Autoconnect (this);
				//dialogSpecialCharacter.TransientFor = (Gtk.Window)CasaMain.gxmlMain.GetWidget("dialogNewSecret");			
			}	
			//tvKeyValue.Selection.SelectIter(iterKey);
			entryKey.HasFocus = true;
		}			
		
		Logger.DbgLog("GUI:KdeWallet.on_buttonNewAdd_clicked() - END");	
	}
	
	public void on_buttonSCClose_clicked(object obj, EventArgs args)
	{
		dialogSpecialCharacter.Destroy();
	}
	
	/// <summary>
	/// REMOVE BUTTON CLICKED 
	/// </summary>
	public void on_buttonNewRemove_clicked(object obj, EventArgs args)
	{
		Logger.DbgLog("GUI:KdeWallet.on_buttonNewRemove_clicked() - BEGIN");
		
		TreeModel modelKey;
		TreeIter  iterKey; 
		
		if(tvKeyValue.Selection.GetSelected (out modelKey, out iterKey))
			if( false == (bool)tsKeyValue.GetValue(iterKey,3) )
				arrDeletedKeys.Add(tsKeyValue.GetValue(iterKey,0));		


			
		if( 0 != tvKeyValue.Selection.CountSelectedRows() )
		{
			TreeModel model;
			TreeIter  iter;
			
			tvKeyValue.Selection.GetSelected (out model, out iter);
			tsKeyValue.Remove(ref iter);
			tvKeyValue.ColumnsAutosize();
		}
		
		Logger.DbgLog("GUI:KdeWallet.on_buttonNewRemove_clicked() - END");
	}
	
	/// <summary>
	/// SHOW PASSWORD CHECK BUTTON CLICKED 
	/// </summary>
	public void on_cbuttonShowPassword_toggled(object obj, EventArgs args)
	{
		TreeViewColumn tvCol;
		
		if( tvKeyValue.Model.IterNChildren() > 0 )
		if( true == cbuttonShowPassword.Active)
		{
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogLogin", null);
			gxmlTemp.Autoconnect (this);
			dialogLogin.TransientFor = dialogManageSecret;        
				
			label86.Text = "Enter your Master Password to view passwords";
			entryMasterPassword3.Text="";
			entryMasterPassword3.HasFocus = true;
			label88.Hide();
			entryMasterPassword4.Hide();
			dialogLogin.Show();						
		}
		else
		{
			tvKeyValue.RemoveColumn(tvKeyValue.GetColumn(1));
			tvCol = new TreeViewColumn("Value", cellEditable, "text", 2);
			tvKeyValue.InsertColumn(tvCol, 1);
		}	
	
	}
	
	public void okbuttonLogin_clicked(object abj, EventArgs args)
	{    
		TreeViewColumn tvCol;
		
		if( tvKeyValue.Model.IterNChildren() > 0 )

		if( 0 == miCASA.SetMasterPassword(0, entryMasterPassword3.Text) )
		{			
			tvKeyValue.RemoveColumn(tvKeyValue.GetColumn(1));
			tvCol = new TreeViewColumn("Value", cellEditable, "text", 1);
			tvKeyValue.InsertColumn(tvCol, 1);			
			dialogLogin.Destroy();  		
		}
		else
		{
			// prompt user 						
			MessageDialog md=new MessageDialog(dialogLogin,Gtk.DialogFlags.Modal,
			Gtk.MessageType.Warning,
			Gtk.ButtonsType.Ok,
			"Master Password incorrect");

			md.Response +=new ResponseHandler(md_Response2);
			md.SetPosition(Gtk.WindowPosition.CenterOnParent);		
			md.Modal = true;
			md.Show();
		}				
	}
	
	public void closebuttonLogin_clicked(object abj, EventArgs args)
	{
		cbuttonShowPassword.Active = false;
		dialogLogin.Destroy();  
	}
    
    
	public void OnDialogLoginDeleted(object obj, DeleteEventArgs args) 
	{		    			
		cbuttonShowPassword.Active = false;
		dialogLogin.Destroy();
		args.RetVal = true;        		
	}
	
	public void on_entryMasterPassword3_activate(object obj, EventArgs args)
	{
		if( "" != entryMasterPassword3.Text )
			okbuttonLogin_clicked(obj, args);
	}
    
	public void on_entryMasterPassword4_activate(object obj, EventArgs args)
	{
		okbuttonLogin_clicked(obj, args);
	}
	
	private void md_Response2(object o, ResponseArgs args)
	{
		MessageDialog md = (MessageDialog)o;
		if (md != null)
		{
			md.Destroy();			
			entryMasterPassword3.Text="";
			entryMasterPassword3.HasFocus = true;
		}
	}
	
	
	/// <summary>
	/// MANAGE SECRET-ID DIALOG OK-BUTTON CLICKED 
	/// </summary>
	public void on_buttonManageOk_clicked(object obj, EventArgs args)
	{
	  TreeModel modelSecret;
		TreeIter  iterSecret, 
			  iterKey;
		string    NewKey    	 = null,
			  NewValue  	 = null;				  
		string[]  strDeletedKeys = null;			  
		bool   	  dirtyBit  	 = false;		
		//ArrayList arrKeys   	 = null,
		//	  arrValues 	 = null;
		
		try
		{
			if( (0 == tvKeyValue.Model.IterNChildren()) && tvSecretIDKdeWallet.Selection.GetSelected (out modelSecret, out iterSecret) )
			{
				if( Common.STATUS_SUCCESS == StoreDataInterface.UpdateStore(Common.STORE_KDEWALLET, Common.OPERATION_DELETE_SECRET, "", "", ref modelSecret, ref iterSecret) )
				{				
					tsSecretIDKdeWallet.Remove(ref iterSecret);
					tvSecretIDKdeWallet.ColumnsAutosize();
					tsNativeInfoKdeWallet.Clear();					
					Logger.DbgLog("GUI:KdeWallet.on_buttonManageOk_clicked() - DELETE_SECRET_SUCCEEDED");					
				}
				else
					Logger.DbgLog("GUI:KdeWallet.on_buttonManageOk_clicked() - DELETE_SECRET_FAILED");					
				AggregateStore();
			}
			else 
			{
			if( (null != arrDeletedKeys) && (arrDeletedKeys.Count > 0) )
			{					
				tvSecretIDKdeWallet.Selection.GetSelected (out modelSecret, out iterSecret);
				strDeletedKeys = (string[])arrDeletedKeys.ToArray(typeof(string));
				for( int i=0; i < strDeletedKeys.Length; i++)
				{
					if( Common.STATUS_SUCCESS == StoreDataInterface.UpdateStore(Common.STORE_KDEWALLET, Common.OPERATION_DELETE_KEY, strDeletedKeys[i], null, ref modelSecret, ref iterSecret) )
						Logger.DbgLog("GUI:KdeWallet.on_buttonManageOk_clicked() - DELETE_KEY_SUCCEEDED.");
					else
						Logger.DbgLog("GUI:KdeWallet.on_buttonManageOk_clicked() - DELETE_KEY_FAILED.");	
				}
				arrDeletedKeys.Clear();
							
			}			
			
			if( tsKeyValue.GetIterFirst(out iterKey) && tvSecretIDKdeWallet.Selection.GetSelected (out modelSecret, out iterSecret) )
			{					
				do
				{									
					NewKey   = (string) tsKeyValue.GetValue(iterKey,0);				
					NewValue = (string) tsKeyValue.GetValue(iterKey,1);
					dirtyBit = (bool)   tsKeyValue.GetValue(iterKey,3);
					
					if( true == dirtyBit )
					{	
						if( Common.STATUS_SUCCESS == StoreDataInterface.UpdateStore(Common.STORE_KDEWALLET, Common.OPERATION_ADD_KEY, NewKey, NewValue, ref modelSecret, ref iterSecret) )
							Logger.DbgLog("GUI:KdeWallet.on_buttonManageOk_clicked() - ADD_KEY_VALUE_SUCCEEDED.");
						else
							Logger.DbgLog("GUI:KdeWallet.on_buttonManageOk_clicked() - ADD_KEY_VALUE_FAILED.");		
					}
				}
				while( tsKeyValue.IterNext(ref iterKey) );									
				
			}
				AggregateStore();
			}						
		}
		catch(Exception exp)
		{
			Logger.DbgLog("GUI:KdeWallet.on_buttonManageOk_clicked() - EXCEPTION:" + exp.ToString());		
		}
		
		tsKeyValue.Dispose();
		dialogManageSecret.Destroy();
	}												
	
	/// <summary>
	/// MANAGE SECRET-ID DIALOG CANCEL-BUTTON CLICKED 
	/// </summary>													
	public void on_buttonManageCancel_clicked(object obj, EventArgs args)
	{
		tsKeyValue.Dispose();
		dialogManageSecret.Destroy();
	}
	
	/// <summary>
	/// SECRET-ID DOUBLE CLICKED 
	/// </summary>
	private void OntvSecretIDKdeWalletRowActivated( object obj, RowActivatedArgs args ) 
	{ 
		Logger.DbgLog("GUI:KdeWallet.OntvSecretIDKdeWalletRowActivated() - SecretID double clicked.");					
		ViewKeyValues();			
	}
	
			
	/// <summary>
	/// VIEW KEY-VALUES CALLED VIA MAIN-MENU/CONTEXT-MENU 
	/// </summary>
	public void OnViewActivated(object obj, EventArgs args)
	{
		Logger.DbgLog("GUI:KdeWallet.OnViewActivated() - ViewKeyValues() called.");
		
		ViewKeyValues();		
	}
	
	 
	///#######################################################################
	/// ADD NEW SECRET
	
    	/// <summary>
	/// 
	/// </summary>
	public void OnNewSecretActivated(object obj, EventArgs args)
	{
		Logger.DbgLog("GUI:KdeWallet.OnNewSecretActivated() - BEGIN");
		
		Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogNewSecret", null);
        	gxmlTemp.Autoconnect (this);
        	dialogNewSecret.TransientFor = (Gtk.Window)CasaMain.gxmlMain.GetWidget("windowMain");
        	dialogNewSecret.Title = "KDE Wallet - New Secret";
		
        	cellEditable = new CellRendererText();
		cellEditable.Editable = true;
		cellEditable.Edited += new EditedHandler(OnKeyValueEdited);		
        	///			   KEY:0          VALUE:1         VALUE-DUP:2  	  DIRTY-BIT:3   LINK:4 			
		tsKeyValue = new TreeStore(typeof(string),typeof(string), typeof(string), typeof(bool), typeof(string));
		tvKeyValue.AppendColumn("Key",new CellRendererText(),"text",0);
		tvKeyValue.AppendColumn("Value",cellEditable,"text",2);
		tvKeyValue.AppendColumn("Linked",new CellRendererText(),"text",4);
		tvKeyValue.Model = tsKeyValue;		
		tsKeyValue.Clear();
		entrySecretID.HasFocus = true;
		entrySecretID.Text = "";
		
		Logger.DbgLog("GUI:KdeWallet.OnNewSecretActivated() - END");
	}
	
	
	///#######################################################################
    	/// ADD NEW KEY-VALUES TO EXISTING SECRET
    
    	/// <summary>
	/// 
	/// </summary>
	public void OnNewKeyActivated(object obj, EventArgs args)
	{
		Logger.DbgLog("GUI:KdeWallet.OnNewKeyActivated() - BEGIN");
		
		ViewKeyValues();
		
        	Logger.DbgLog("GUI:KdeWallet.OnNewKeyActivated() - END");	
	}
	
	public void on_buttonNewOk_clicked(object obj, EventArgs args)
	{
		Logger.DbgLog("GUI:KdeWallet.on_buttonNewOk_clicked() - BEGIN");
		
		TreeModel modelSecret;
		TreeIter  iterSecret,
				  iterKey;
		string 	  NewKey       = null,
				  NewValue     = null; 
		string[]  Keys	   	   = null,
				  Values   	   = null,
				  NativeKeys   = null,
				  NativeValues = null;
		object 	  val		   = null;		
		ArrayList arrKeys	   = null,
				  arrValues    = null;
					
		if ( true == entrySecretID.Editable && false == Common.ValidateString(entrySecretID.Text) )
		{
			/*// prompt user 						
			MessageDialog md=new MessageDialog(this.windowMain,Gtk.DialogFlags.Modal,
				Gtk.MessageType.Warning,
				Gtk.ButtonsType.Ok,
				"Secret ID may not contain \"*\"");

			md.Response += new ResponseHandler(md_Response);
			md.SetPosition(Gtk.WindowPosition.CenterOnParent);		
			md.Modal = true;
			md.Show();*/
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogSpecialCharacter", null);
			gxmlTemp.Autoconnect (this);
			entrySecretID.HasFocus = true;
			return;
		}

		if( (true == entrySecretID.Editable) && ("" != entrySecretID.Text) && (tvKeyValue.Model.IterNChildren() > 0) )
		{
			Logger.DbgLog("GUI:KdeWallet.on_buttonNewOk_clicked() - Adding New Secrets and KeyValues.");
			
			arrKeys 	= new ArrayList();
			arrValues 	= new ArrayList();
			
			try
			{
				if(tsKeyValue.GetIterFirst(out iterKey))
				{
					do
					{
						val = tsKeyValue.GetValue(iterKey,0);
						NewKey = val.ToString();				
						val = tsKeyValue.GetValue(iterKey,1);
						NewValue = val.ToString();
						
						if( -1 == (arrKeys.IndexOf(NewKey)) )
						{	
							arrKeys.Add(NewKey);
							arrValues.Add(NewValue);
						}
					} 
					while( tsKeyValue.IterNext(ref iterKey) );
					
					Keys = (string[])arrKeys.ToArray(typeof(string));
					Values = (string[])arrValues.ToArray(typeof(string));
					NativeKeys = new string[Common.MAX_NATIVE_ELEMENTS];
					NativeValues = new string[Common.MAX_NATIVE_ELEMENTS];					
					NativeKeys[Common.INDEX_NATIVEINFO_FOLDERNAME] = Common.NATIVEINFO_FOLDERNAME; 
					NativeKeys[Common.INDEX_NATIVEINFO_TYPEID] = Common.NATIVEINFO_TYPEID;
					NativeKeys[Common.INDEX_NATIVEINFO_SYNC] = Common.NATIVEINFO_SYNC;
					NativeKeys[Common.INDEX_NATIVEINFO_SYNCTYPE] = Common.NATIVEINFO_SYNCTYPE;
					NativeKeys[Common.INDEX_NATIVEINFO_MODIFIEDTIME] = Common.NATIVEINFO_MODIFIEDTIME;				
					NativeValues[Common.INDEX_NATIVEINFO_FOLDERNAME] = "Passwords";
					NativeValues[Common.INDEX_NATIVEINFO_TYPEID] = "Maps";
					NativeValues[Common.INDEX_NATIVEINFO_SYNC] = null;
					NativeValues[Common.INDEX_NATIVEINFO_SYNCTYPE] = null;
					NativeValues[Common.INDEX_NATIVEINFO_MODIFIEDTIME] = null;					
					
					iterSecret = tsSecretIDKdeWallet.AppendValues(entrySecretID.Text, Keys, Values, "kdewallet", NativeKeys, NativeValues);
					modelSecret = tvSecretIDKdeWallet.Model;					
					
					if( Common.STATUS_SUCCESS == StoreDataInterface.UpdateStore(Common.STORE_KDEWALLET, Common.OPERATION_ADD_SECRET, "", "", ref modelSecret, ref iterSecret) )
					{
						AggregateStore();
						Logger.DbgLog("GUI:KdeWallet.on_buttonNewOk_clicked() - ADD_NEW_SECRET_SUCCEEDED.");
					}
	    			else
	    				Logger.DbgLog("GUI:KdeWallet.on_buttonNewOk_clicked() - ERROR: ADD_NEW_SECRET_FAILED");	
	    		}
    		}
			catch(Exception exp)
			{
				Logger.DbgLog("GUI:KdeWallet.on_buttonNewOk_clicked() - EXCEPTION:" + exp.ToString());	
			}
			tsKeyValue.Dispose();
			dialogNewSecret.Destroy();		
		}
	
		
		Logger.DbgLog("GUI:KdeWallet.on_buttonNewOk_clicked() - END");
	}
	
	private void md_Response(object o, ResponseArgs args)
	{
		MessageDialog md = (MessageDialog)o;
		if (md != null)
		{
			md.Destroy();			
		}
	}	
		
	public void on_buttonNewCancel_clicked(object obj, EventArgs args)
	{		
		dialogNewSecret.Destroy();
	}

	public void on_helpbuttonNewSecret_clicked(object obj, EventArgs args)
	{
		Common.ShowHelpUrl("AddNewSecrets.htm");
	}
	///#######################################################################
    	/// LINK 
    
    	/// <summary>
	/// LINK Key-Values
	/// </summary>
	public void OnLinkActivated(object obj, EventArgs args)
	{
		
	}
	
	
	///#######################################################################
    	/// COPY
    
    	/// <summary>
	/// COPY Key-Values
	/// </summary>	
	public void OnCopyActivated(object obj, EventArgs args)
	{
		
	}  
	
	
	///#######################################################################
    	/// DELETE SECRET
    
    /// <summary>
	/// DELETE Secret
	/// </summary>
	public void OnDeleteActivated(object obj, EventArgs args)
	{
		Logger.DbgLog("GUI:KdeWallet.OnDeleteActivated() - BEGIN");		
		if( 0 != tvSecretIDKdeWallet.Selection.CountSelectedRows() )
		{
			Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogConfirmDelete", null);
			gxmlTemp.Autoconnect (this);
			dialogConfirmDelete.TransientFor = (Gtk.Window)CasaMain.gxmlMain.GetWidget("windowMain");                
			dialogConfirmDelete.Title = "KDE Wallet - Delete Secret";
			
			TreeModel model;
			TreeIter  iter;
			string    selected = null;			
			if( tvSecretIDKdeWallet.Selection.GetSelected (out model, out iter) )
			{
				selected = (string) model.GetValue (iter, 0);
				if( (null != selected) && (selected.Length > 0) )
					entryDeleteSecretID.Text = selected;
			}
		}        
		Logger.DbgLog("GUI:KdeWallet.OnDeleteActivated() - END");
	}
	
	public void on_buttonYes_clicked(object obj, EventArgs args)
	{
		Logger.DbgLog("GUI:KdeWallet.on_buttonYes_clicked() - BEGIN");
				
		TreeModel model;
		TreeIter  iter;
		
		try
		{
			if( tvSecretIDKdeWallet.Selection.GetSelected (out model, out iter) )
			{
				if( Common.STATUS_SUCCESS == StoreDataInterface.UpdateStore(Common.STORE_KDEWALLET, Common.OPERATION_DELETE_SECRET, "", "", ref model, ref iter) )
				{				
					tsSecretIDKdeWallet.Remove(ref iter);
					tvSecretIDKdeWallet.ColumnsAutosize();
					tsNativeInfoKdeWallet.Clear();
					dialogConfirmDelete.Destroy();
					Logger.DbgLog("GUI:KdeWallet.on_buttonYes_clicked() - DELETE_SECRET_SUCCEEDED");
					
				}
				else
					Logger.DbgLog("GUI:KdeWallet.on_buttonYes_clicked() - DELETE_SECRET_FAILED");
			}
		}
		catch(Exception exp)
		{
			Logger.DbgLog("GUI:KdeWallet.on_buttonYes_clicked() - EXCEPTION:" + exp.ToString());
		}
		
 		Logger.DbgLog("GUI:KdeWallet.on_buttonYes_clicked() - END");	
	}
	
	public void on_buttonNo_clicked(object obj, EventArgs args)
	{		
		dialogConfirmDelete.Destroy();
	}
	
	public void on_buttonhelpEditSecret_clicked(object obj, EventArgs args)
	{
		Common.ShowHelpUrl("EditingSecrets.html");
	}	
	public void on_helpbuttonAuthentication_clicked(object obj, EventArgs args)
	{
		Common.ShowHelpUrl("CASAMasterPasswordAuthentication.htm");
	}
	
	///#######################################################################
    /// Application SSO    
    /// <summary>
	/// 
	/// </summary>
	
	/// Konquerer
	public void SSOKonquerer(object obj, EventArgs args)
	{
		Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogKonquerer", null);
		gxmlTemp.Autoconnect (this);
		dialogKonquerer.TransientFor = (Gtk.Window)CasaMain.gxmlMain.GetWidget("windowMain");                
		comboKonquererTemplates.PopdownStrings = stringKonquererTemplates;
		comboKonquererTemplates.DisableActivate();		
	}
	public void on_okKonquerer_clicked(object abj, EventArgs args)
	{		
		TreeModel modelSecret;
		TreeIter  iterSecret;				  
		string[]  Keys	   	   = null,
				  Values   	   = null,
				  NativeKeys   = null,
				  NativeValues = null;		
		ArrayList arrKeys	   = null,
				  arrValues    = null;
		
		try
		{
			if( "" != entryKonquererURL.Text && "" != entryKonquererUsrKey.Text && "" != entryKonquererUsrValue.Text && "" != entryKonquererPwdKey.Text && "" != entryKonquererPwdValue.Text )
			{
				arrKeys 	= new ArrayList();
				arrValues = new ArrayList();
				
				arrKeys.Add(entryKonquererUsrKey.Text);
				arrKeys.Add(entryKonquererPwdKey.Text);
				arrValues.Add(entryKonquererUsrValue.Text);
				arrValues.Add(entryKonquererPwdValue.Text);
			
				Keys = (string[])arrKeys.ToArray(typeof(string));
				Values = (string[])arrValues.ToArray(typeof(string));
		
				NativeKeys = new string[Common.MAX_NATIVE_ELEMENTS];
				NativeValues = new string[Common.MAX_NATIVE_ELEMENTS];					
				NativeKeys[Common.INDEX_NATIVEINFO_FOLDERNAME] = Common.NATIVEINFO_FOLDERNAME; 
				NativeKeys[Common.INDEX_NATIVEINFO_TYPEID] = Common.NATIVEINFO_TYPEID;
				NativeKeys[Common.INDEX_NATIVEINFO_SYNC] = Common.NATIVEINFO_SYNC;
				NativeKeys[Common.INDEX_NATIVEINFO_SYNCTYPE] = Common.NATIVEINFO_SYNCTYPE;
				NativeKeys[Common.INDEX_NATIVEINFO_MODIFIEDTIME] = Common.NATIVEINFO_MODIFIEDTIME;				
				NativeValues[Common.INDEX_NATIVEINFO_FOLDERNAME] = "Form Data";
				NativeValues[Common.INDEX_NATIVEINFO_TYPEID] = "Maps";
				NativeValues[Common.INDEX_NATIVEINFO_SYNC] = null;
				NativeValues[Common.INDEX_NATIVEINFO_SYNCTYPE] = null;
				NativeValues[Common.INDEX_NATIVEINFO_MODIFIEDTIME] = null;					
					
				iterSecret = tsSecretIDKdeWallet.AppendValues(entryKonquererURL.Text, Keys, Values, "kdewallet", NativeKeys, NativeValues);
				modelSecret = tvSecretIDKdeWallet.Model;					
						
				if( Common.STATUS_SUCCESS == StoreDataInterface.UpdateStore(Common.STORE_KDEWALLET, Common.OPERATION_ADD_SECRET, "", "", ref modelSecret, ref iterSecret) )
				{
					AggregateStore();
					Logger.DbgLog("GUI:KdeWallet.on_okKonquerer_clicked() - ADD_NEW_SECRET_SUCCEEDED.");
				}
		    		else
		    			Logger.DbgLog("GUI:KdeWallet.on_okKonquerer_clicked() - ERROR: ADD_NEW_SECRET_FAILED");	
				
				dialogKonquerer.Destroy();
			}			
		}
		catch(Exception exp)
		{
			Logger.DbgLog("GUI:KdeWallet.on_okKonquerer_clicked() - EXCEPTION:" + exp.ToString());	
		}
		
	}
	
	public void on_entryKonquererTemplates_changed(object obj, EventArgs args)
	{
		/// new
		if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[0] )
		{
			entryKonquererURL.Text = "";
			entryKonquererUsrKey.Text = "";
			entryKonquererPwdKey.Text = "";
			entryKonquererURL.HasFocus = true;
		}
		/// novell
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[1] )
		{
			entryKonquererURL.Text = "http://www.novell.com/ICSLogin/#";
			entryKonquererUsrKey.Text = "username";
			entryKonquererPwdKey.Text = "password";
			entryKonquererUsrValue.HasFocus = true;
		}
		/// innerweb 
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[2] )
		{
			entryKonquererURL.Text = "https://innerweb.novell.com/ICSLogin/#";
			entryKonquererUsrKey.Text = "username";			
			entryKonquererPwdKey.Text = "password";
			entryKonquererUsrValue.HasFocus = true;
		}
		/// bugzilla
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[3] )
		{
			entryKonquererURL.Text = "https://bugzilla.novell.com/ICSLogin/#";
			entryKonquererUsrKey.Text = "username";
			entryKonquererPwdKey.Text = "password";
			entryKonquererUsrValue.HasFocus = true;
		}
		/// forge
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[4] )
		{
			entryKonquererURL.Text = "http://forge.novell.com/modules/news/#";
			entryKonquererUsrKey.Text = "username";
			entryKonquererPwdKey.Text = "password";
			entryKonquererUsrValue.HasFocus = true;
		}
		/// bandit
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[5] )
		{
			entryKonquererURL.Text = "https://www.bandit-project.org/ICSLogin/#login"; 
			entryKonquererUsrKey.Text = "username";
			entryKonquererPwdKey.Text = "password";
			entryKonquererUsrValue.HasFocus = true;
		}
		/// gw webaccess
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[6] )
		{
			entryKonquererURL.Text = "https://gmail.novell.com/gw/webacc#loginForm";
			entryKonquererUsrKey.Text = "User.id";
			entryKonquererPwdKey.Text = "User.password";
			entryKonquererUsrValue.HasFocus = true;
		}
		/// gmail
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[7] )
		{
			entryKonquererURL.Text = "https://www.google.com/accounts/ServiceLogin#";
			entryKonquererUsrKey.Text = "Email";
			entryKonquererPwdKey.Text = "Passwd";
			entryKonquererUsrValue.HasFocus = true;
		}
		/// yahoo mail
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[8] )
		{
			entryKonquererURL.Text = "http://mail.yahoo.com/#login_form";
			entryKonquererUsrKey.Text = "login";
			entryKonquererPwdKey.Text = "passwd";
			entryKonquererUsrValue.HasFocus = true;
		}
		/// hotmail
		else if( comboKonquererTemplates.Entry.Text == stringKonquererTemplates[9] )
		{
			entryKonquererURL.Text = "http://login.passport.net/uilogin.srf#f1";
			entryKonquererUsrKey.Text = "login";
			entryKonquererPwdKey.Text = "passwd";
			entryKonquererUsrValue.HasFocus = true;
		}
	}
	
	public void on_cancelKonquerer_clicked(object obj, EventArgs args)
	{	
		dialogKonquerer.Destroy();
	}
	
	public void on_helpKonquerer_clicked(object obj, EventArgs args)
	{		
	}
	
	/// Kopete
	public void SSOKopete(object obj, EventArgs args)
	{
		Glade.XML gxmlTemp = new Glade.XML (Common.GladeFile, "dialogKopete", null);
		gxmlTemp.Autoconnect (this);
		dialogKopete.TransientFor = (Gtk.Window)CasaMain.gxmlMain.GetWidget("windowMain");                
		
	}
	public void on_okKopete_clicked(object obj, EventArgs args)
	{		
		dialogKopete.Destroy();
	}
	public void on_cancelKopete_clicked(object obj, EventArgs args)
	{	
		dialogKopete.Destroy();
	}
	public void on_helpKopete_clicked(object obj, EventArgs args)
	{		
	}
}
}
///##################################################################
///							END OF FILE
///##################################################################