Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution/addressbook/ChangeLog,v
retrieving revision 1.2100
diff -u -p -r1.2100 ChangeLog
--- ChangeLog	2 Aug 2006 06:33:54 -0000	1.2100
+++ ChangeLog	2 Aug 2006 09:50:20 -0000
@@ -1,3 +1,11 @@
+2006-07-27  Ushveen Kaur <kushveen@novell.com>
+
+	* gui/merging/eab-contact-merging.c :  Added merge features to 
+	duplicate detected dialog. 
+	* gui/merging/eab-contact-duplicate-detected.glade : Added the merge
+	button to the dialog.
+	Fix for Bug #211082
+
 2006-07-25  Johnny Jacob  <jjohnny@novell.com>
 	
 	* gui/widgets/e-addressbook-view.c: (search_activated),
Index: gui/merging/eab-contact-merging.c
===================================================================
RCS file: /cvs/gnome/evolution/addressbook/gui/merging/eab-contact-merging.c,v
retrieving revision 1.8
diff -u -p -r1.8 eab-contact-merging.c
--- gui/merging/eab-contact-merging.c	21 Apr 2006 08:02:14 -0000	1.8
+++ gui/merging/eab-contact-merging.c	2 Aug 2006 09:50:20 -0000
@@ -15,7 +15,11 @@
 #include "eab-contact-compare.h"
 #include <glade/glade.h>
 #include <gtk/gtksignal.h>
+#include <gtk/gtktable.h>
+#include <gtk/gtk.h>
+#include <gtk/gtkcombo.h>
 #include <gtk/gtkdialog.h>
+#include <string.h>
 #include <gtk/gtkcontainer.h>
 #include "addressbook/gui/widgets/eab-contact-display.h"
 #include "e-util/e-util-private.h"
@@ -29,12 +33,18 @@ typedef struct {
 	EContactMergingOpType op;
 	EBook *book;
 	EContact *contact;
+	EContact *match;
 	GList *avoid;
 	EBookIdCallback id_cb;
 	EBookCallback   cb;
 	gpointer closure;
 } EContactMergingLookup;
 
+struct dropdown_data{
+	EContact *match;
+	EContactField field;
+};
+
 static void match_query_callback (EContact *contact, EContact *match, EABContactMatchType type, gpointer closure);
 
 #define SIMULTANEOUS_MERGING_REQUESTS 20
@@ -81,7 +91,9 @@ free_lookup (EContactMergingLookup *look
 	g_object_unref (lookup->book);
 	g_object_unref (lookup->contact);
 	g_list_free (lookup->avoid);
-
+	if(lookup->match)
+		g_object_unref (lookup->match);
+ 
 	g_free (lookup);
 }
 
@@ -130,22 +142,265 @@ cancelit (EContactMergingLookup *lookup)
 	}
 }
 
+gboolean
+dialog_map (GtkWidget *window, GdkEvent *event, GtkWidget *table)
+{
+	int h, w;
+
+	w = table->allocation.width + 30 /* Spacing around the table */;
+	h = table->allocation.height + 60 /* buttons and outer spacing */;
+	
+	if (w > 400)
+		w = 400;
+	if (h > 450)
+		h = 450;
+	gtk_widget_set_usize (window, w, h);
+}
+
+
 static void
-response (GtkWidget *dialog, int response, EContactMergingLookup *lookup)
+dropdown_changed(GtkWidget *dropdown, struct dropdown_data *data)
 {
-	gtk_widget_destroy (dialog);
+	char *str;
+	str = gtk_combo_box_get_active_text (dropdown);
+	if (g_ascii_strcasecmp(str,""))
+		e_contact_set (data->match, data->field, str);
+	else
+		e_contact_set (data->match, data->field, NULL);
+	return;
+}
+
+static int
+mergeit(EContactMergingLookup *lookup)
+{
+	GtkWidget *scrolled_window, *label, *hbox;
+	GtkDialog *dialog;
+	GtkTable *table;
+	
+	dialog = (GtkDialog *)(gtk_dialog_new_with_buttons("Merge Contact", NULL, GTK_DIALOG_NO_SEPARATOR, NULL));
+	gtk_container_border_width(GTK_CONTAINER(dialog), 5);
 
+	scrolled_window = gtk_scrolled_window_new (NULL, NULL);	
+	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
+					GTK_POLICY_AUTOMATIC,
+					GTK_POLICY_AUTOMATIC);
+	
+	table = (GtkTable *) gtk_table_new (20, 2, FALSE);
+	gtk_container_set_border_width ((GtkContainer *) table, 12);
+	gtk_table_set_row_spacings (table, 6);
+	gtk_table_set_col_spacings (table, 2);
+       
+	gtk_dialog_add_buttons ((GtkDialog *) dialog,
+				GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+				"Merge", GTK_RESPONSE_OK,
+				NULL);
+
+	EContactField field;
+	int row = -1;
+	for(field = E_CONTACT_FULL_NAME; field != (E_CONTACT_LAST_SIMPLE_STRING -1) ; field++) {
+		char *string = NULL ,*string1 = NULL;
+		struct dropdown_data *data = (struct dropdown_data*)malloc(sizeof(struct dropdown_data));
+		GtkWidget *dropdown;
+		GList *email_attr_list;
+		int num_of_email;
+
+		string = e_contact_get (lookup->contact, field);
+		string1 = e_contact_get (lookup->match, field);
+
+		email_attr_list = e_contact_get_attributes (lookup->match, E_CONTACT_EMAIL);
+		num_of_email = g_list_length(email_attr_list);
+
+		/*for all string fields*/
+		if (string && *string){
+			
+			if ((field == E_CONTACT_EMAIL_1 || field == E_CONTACT_EMAIL_2 || field == E_CONTACT_EMAIL_3 || field == E_CONTACT_EMAIL_4) && (num_of_email<4)){
+				char *str = NULL;
+				row++;
+				str = e_contact_get (lookup->contact, field);
+				switch(num_of_email)
+					{
+					    case 0:
+						    field = E_CONTACT_EMAIL_1;
+						    break;
+					    case 1:
+						    if((str && *str) && (g_ascii_strcasecmp(e_contact_get (lookup->match, E_CONTACT_EMAIL_1),str))){
+							    data->field = E_CONTACT_EMAIL_2;
+							    break;
+						    }
+						    else
+							    continue;
+					    case 2:
+						    if((str && *str) && (g_ascii_strcasecmp(str,e_contact_get (lookup->match, E_CONTACT_EMAIL_1))) && (g_ascii_strcasecmp(e_contact_get (lookup->match, E_CONTACT_EMAIL_2),str))){
+							    data->field = E_CONTACT_EMAIL_3;
+							    break;
+						    }
+						    else
+							    continue;
+					    case 3:
+						    if((str && *str) && (g_ascii_strcasecmp(e_contact_get (lookup->match, E_CONTACT_EMAIL_1),str)) && (g_ascii_strcasecmp(e_contact_get (lookup->match, E_CONTACT_EMAIL_2),str)) && (g_ascii_strcasecmp(e_contact_get (lookup->match, E_CONTACT_EMAIL_3),str)))
+							    data->field = E_CONTACT_EMAIL_4;
+						    else
+							    continue;
+					}
+				label = gtk_label_new("Email");
+				hbox = gtk_hbox_new(FALSE, 0);
+				gtk_box_pack_start (GTK_BOX(hbox), (GtkWidget*)label, FALSE, FALSE, 0);
+				gtk_table_attach_defaults (table, (GtkWidget *)hbox, 0, 1, row, row+1);
+				
+				dropdown = gtk_combo_box_new_text();	
+				gtk_combo_box_append_text (dropdown, string);
+
+				e_contact_set (lookup->match, data->field, string);
+				gtk_combo_box_append_text (dropdown, "");
+				
+				gtk_combo_box_set_active(dropdown, 0);
+
+				data->match = (EContact *)malloc(sizeof(EContact));
+				data->match = lookup->match;
+				
+				g_signal_connect(dropdown, "changed", G_CALLBACK(dropdown_changed), data);
+				
+				hbox = gtk_hbox_new(FALSE, 0);
+				gtk_box_pack_start (GTK_BOX(hbox), (GtkWidget*)dropdown, FALSE, FALSE, 0);
+				gtk_table_attach_defaults (table, (GtkWidget *)hbox, 1, 2, row, row+1);
+				gtk_widget_show((GtkWidget *)dropdown);
+				continue;
+			}
+			row++;
+			if (((field == E_CONTACT_FULL_NAME) || (field == E_CONTACT_GIVEN_NAME)) && (!g_ascii_strcasecmp(string,string1))){
+				label = gtk_label_new(e_contact_pretty_name(field));
+				hbox = gtk_hbox_new(FALSE, 0);
+				gtk_box_pack_start (GTK_BOX(hbox), (GtkWidget*)label, FALSE, FALSE, 0);
+				gtk_table_attach_defaults (table, (GtkWidget *)hbox, 0, 1, row, row+1);
+
+				label = gtk_label_new(string);
+				hbox = gtk_hbox_new(FALSE, 0);
+				gtk_box_pack_start (GTK_BOX(hbox), (GtkWidget*)label, FALSE, FALSE, 0);
+				gtk_table_attach_defaults (table, (GtkWidget*)hbox, 1, 2, row, row+1);
+				continue;
+			}
+			
+			/*for all string fields except name and email*/
+			if(!(string1 && *string1) || (g_ascii_strcasecmp(string,string1))){
+				label = gtk_label_new(e_contact_pretty_name(field));
+				hbox = gtk_hbox_new(FALSE, 0); 
+				gtk_box_pack_start (GTK_BOX(hbox), (GtkWidget*)label, FALSE, FALSE, 0);
+				gtk_table_attach_defaults (table, (GtkWidget *)hbox, 0, 1, row, row+1); 
+			
+				dropdown = gtk_combo_box_new_text();	
+				gtk_combo_box_append_text (dropdown, string);
+				e_contact_set (lookup->match, field, string);
+				if(string1 && *string1)
+					gtk_combo_box_append_text (dropdown, string1);
+				else
+					gtk_combo_box_append_text (dropdown, "");
+				
+				gtk_combo_box_set_active(dropdown, 0);
+				data->field = field;
+				data->match = (EContact *)malloc(sizeof(EContact));
+				data->match = lookup->match;
+					
+				g_signal_connect(dropdown, "changed", G_CALLBACK(dropdown_changed), data);
+				
+				hbox = gtk_hbox_new(FALSE, 0);
+				gtk_box_pack_start (GTK_BOX(hbox), (GtkWidget*)dropdown, FALSE, FALSE, 0);
+				gtk_table_attach_defaults (table, (GtkWidget *)hbox, 1, 2, row, row+1);
+				gtk_widget_show_all((GtkWidget *)dropdown);
+			}
+		}
+	}
+	
+	gtk_window_set_default_size (GTK_WINDOW (dialog), 420, 300);
+	gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), GTK_WIDGET (table));
+	gtk_box_pack_start (GTK_BOX (dialog->vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, 0); 
+
+	gtk_widget_show (scrolled_window);	
+	g_signal_connect (dialog, "map-event", G_CALLBACK (dialog_map), table);	
+	gtk_widget_show_all((GtkWidget *)table);
+	
+	gint result = gtk_dialog_run (dialog);
+	switch (result)
+	{	     
+	    case GTK_RESPONSE_OK:
+		    lookup->contact = lookup->match;
+		    e_book_async_remove_contact (lookup->book, lookup->match, NULL, lookup);
+		    e_book_async_add_contact (lookup->book, lookup->contact, final_id_cb, lookup);
+		    gtk_widget_destroy (dialog);    
+		    return 1;
+		    break;
+	    case GTK_RESPONSE_CANCEL:
+		    gtk_widget_destroy (dialog);
+		    return 0;
+		    break;  
+	}
+}
+	
+static void
+response (GtkWidget *dialog, int response, EContactMergingLookup *lookup)
+{
+	static int merge_response;
 	switch (response) {
-	case 0:
-		doit (lookup);
-		break;
-	case 1:
-		cancelit (lookup);
-		break;
-	case GTK_RESPONSE_DELETE_EVENT:
-		cancelit (lookup);
-		break;
+	    case 0:
+		    doit (lookup);
+		    gtk_widget_destroy (dialog);
+		    break;
+	    case 1:
+		    cancelit (lookup);
+		    gtk_widget_destroy (dialog);
+		    break;
+	    case 2:
+		    merge_response = mergeit (lookup);
+		    if (merge_response)
+			    gtk_widget_destroy (dialog);
+		    break;
+	    case GTK_RESPONSE_DELETE_EVENT:
+		    cancelit (lookup);
+		    gtk_widget_destroy (dialog);
+		    break;
+	}
+}
+
+static gboolean
+check_if_same(EContact *contact, EContact *match)
+{	
+	EContactField field;
+	
+	for(field = E_CONTACT_FULL_NAME; field != (E_CONTACT_LAST_SIMPLE_STRING -1) ; field++) {
+		GList *email_attr_list;
+		int num_of_email;
+		
+		email_attr_list = e_contact_get_attributes (match, E_CONTACT_EMAIL);
+		num_of_email = g_list_length (email_attr_list);
+			
+		if ((field == E_CONTACT_EMAIL_1 || field == E_CONTACT_EMAIL_2 || field == E_CONTACT_EMAIL_3 || field == E_CONTACT_EMAIL_4) && (num_of_email<4)){
+			char *str = NULL;
+			str = e_contact_get (contact, field);
+			switch(num_of_email)
+			{
+			    case 0:
+				    return FALSE;
+			    case 1:
+				    if((str && *str) && (g_ascii_strcasecmp(e_contact_get (match, E_CONTACT_EMAIL_1),str)))
+					    return FALSE;
+			    case 2:
+				    if((str && *str) && (g_ascii_strcasecmp(str,e_contact_get (match, E_CONTACT_EMAIL_1))) && (g_ascii_strcasecmp(e_contact_get (match, E_CONTACT_EMAIL_2),str)))
+					    return FALSE;
+			    case 3:
+				    if((str && *str) && (g_ascii_strcasecmp(e_contact_get (match, E_CONTACT_EMAIL_1),str)) && (g_ascii_strcasecmp(e_contact_get (match, E_CONTACT_EMAIL_2),str)) && (g_ascii_strcasecmp(e_contact_get (match, E_CONTACT_EMAIL_3),str)))
+					    return FALSE;
+			}
+		}
+		else{
+			char *string = NULL ,*string1 = NULL;	
+			string = e_contact_get (contact, field);
+			string1 = e_contact_get (match, field);
+			if ((string && *string) && (string1 && *string1) && (g_ascii_strcasecmp(string1,string)))
+				return FALSE;
+			else if ((string && *string) && !(string1 && *string1))
+				return FALSE;
+		}
 	}
+	return TRUE;
 }
 
 static void
@@ -153,19 +408,25 @@ match_query_callback (EContact *contact,
 {
 	EContactMergingLookup *lookup = closure;
 	char *gladefile;
-
+		
 	if ((gint) type <= (gint) EAB_CONTACT_MATCH_VAGUE) {
 		doit (lookup);
 	} else {
 		GladeXML *ui;
 		
-		GtkWidget *widget;
+		GtkWidget *widget, *merge_button;
 
+		lookup->match = g_object_ref (match);
 		if (lookup->op == E_CONTACT_MERGING_ADD) {
+			static gboolean flag;
+			flag = check_if_same (contact, match);
 			gladefile = g_build_filename (EVOLUTION_GLADEDIR,
 						      "eab-contact-duplicate-detected.glade",
 						      NULL);
 			ui = glade_xml_new (gladefile, NULL, NULL);
+			merge_button = GTK_BUTTON (glade_xml_get_widget (ui, "button5"));
+			if (flag)
+				gtk_widget_set_sensitive(merge_button, FALSE);
 			g_free (gladefile);
 		} else if (lookup->op == E_CONTACT_MERGING_COMMIT) {
 			gladefile = g_build_filename (EVOLUTION_GLADEDIR,
@@ -215,6 +476,7 @@ eab_merging_book_add_contact (EBook     
 	lookup->id_cb = cb;
 	lookup->closure = closure;
 	lookup->avoid = NULL;
+	lookup->match = NULL;
 
 	add_lookup (lookup);
 
@@ -237,6 +499,7 @@ eab_merging_book_commit_contact (EBook  
 	lookup->cb = cb;
 	lookup->closure = closure;
 	lookup->avoid = g_list_append (NULL, contact);
+	lookup->match = NULL;
 
 	add_lookup (lookup);
 
Index: gui/merging/eab-contact-duplicate-detected.glade
===================================================================
RCS file: /cvs/gnome/evolution/addressbook/gui/merging/eab-contact-duplicate-detected.glade,v
retrieving revision 1.5
diff -u -p -r1.5 eab-contact-duplicate-detected.glade
--- gui/merging/eab-contact-duplicate-detected.glade	17 Aug 2005 08:47:40 -0000	1.5
+++ gui/merging/eab-contact-duplicate-detected.glade	2 Aug 2006 09:50:20 -0000
@@ -56,6 +56,19 @@
 	      <property name="response_id">0</property>
 	    </widget>
 	  </child>
+
+	  <child>
+	    <widget class="GtkButton" id="button5">
+	      <property name="visible">True</property>
+	      <property name="can_default">True</property>
+	      <property name="can_focus">True</property>
+	      <property name="label">Merge</property>
+	      <property name="relief">GTK_RELIEF_NORMAL</property>
+	      <property name="focus_on_click">True</property>
+	      <property name="response_id">2</property>
+	    </widget>
+	  </child>
+
 	</widget>
 	<packing>
 	  <property name="padding">0</property>
