[Commits] contactwindow.c 1.6 1.7

miras at claws-mail.org miras at claws-mail.org
Sat Nov 26 04:09:31 CET 2011


Update of /home/claws-mail/contacts/src
In directory claws-mail:/tmp/cvs-serv18420/src

Modified Files:
	contactwindow.c 
Log Message:
2011-11-26 [mir]	0.6.0cvs27

	* src/contactwindow.c
	    - Refactored implementation so that updates
	      are only committed when user requests to
	      save changes. Eliminates server round trips
	      and avoid saving incomplete contact information.

Index: contactwindow.c
===================================================================
RCS file: /home/claws-mail/contacts/src/contactwindow.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- contactwindow.c	22 Nov 2011 00:10:53 -0000	1.6
+++ contactwindow.c	26 Nov 2011 03:09:29 -0000	1.7
@@ -68,15 +68,17 @@
 
 typedef struct {
 	MainWindow* main;
-	AddressBook* abook;
-	Plugin* plugin;
-	Contact* contact;
-	GtkWidget* window;
-	GtkWidget* tabs;
-	GtkWidget* email_list;
-	GtkWidget* image;
-	ContactEntryData** data;
-	gboolean   new_row;
+	AddressBook* 		abook;
+	Plugin*				plugin;
+	Contact*			contact;
+	Contact*			old;
+	GtkWidget*			window;
+	GtkWidget*			tabs;
+	GtkWidget*			email_list;
+	GtkWidget*			image;
+	ContactEntryData**	data;
+	gboolean			new_row;
+	gboolean			image_email_dirty;
 } ContactWindow;
 
 static gint contact_entry_data_size = 0;
@@ -85,6 +87,7 @@
 static void email_list_add(GtkTreeView* view, ContactWindow* cw);
 static gchar* hash_table_get_entry(GHashTable* hash, const gchar* key);
 
+/*
 static void contact_entry_data_update(ContactWindow* cw) {
 	gint i;
 
@@ -93,7 +96,7 @@
 		d->contact = cw->contact;
 	}
 }
-
+*/
 static void contact_entry_data_free(ContactEntryData** data) {
 	gint i;
 
@@ -121,19 +124,23 @@
 	return res;
 }
 
-static gboolean contact_dirty(ContactEntryData** data) {
+static gboolean contact_dirty(ContactWindow* cw) {
 	gboolean result = FALSE;
 	gint i;
+	ContactEntryData** data = cw->data;
+
+	if (cw->image_email_dirty)
+		return TRUE;
 	
 	for (i = 0; !result && i < contact_entry_data_size; i++) {
 		Contact* contact = data[i]->contact;
 		if (contact && contact->data && data[i]->key && data[i]->entry) {
 			const gchar* new_data =
 				gtk_entry_get_text(GTK_ENTRY(data[i]->entry));
-				result = entry_dirty(contact->data, data[i]->key, new_data);
+			result = entry_dirty(contact->data, data[i]->key, new_data);
 		}
 	}
-	
+
 	return result;
 }
 
@@ -147,13 +154,16 @@
 		if (contact_is_new)
 			cw->plugin->set_contact(cw->abook, contact, &err);
 		else
-			cw->plugin->update_contact(cw->abook, contact, &err);
+			err = contact_write_to_backend(cw->plugin, cw->abook, cw->old, contact);
 		if (err) {
 			show_message(cw->window, GTK_UTIL_MESSAGE_WARNING, "%s", err);
 			g_free(err);
 			err = NULL;
+			contact_free(contact);
+			g_free(contact);
 		}
 		else {
+			cw->image_email_dirty = FALSE;
 			if (contact_is_new) {
 				if (debug_get_mode()) {
 					contact_dump(contact, stderr);
@@ -164,6 +174,10 @@
 				while (gtk_events_pending())
   					gtk_main_iteration();
 				contact_is_new = FALSE;
+				if (cw->old) {
+					contact_free(cw->old);
+					g_free(cw->old);
+				}
 			}
 			else {
 				row = gtk_tree_view_get_selection(
@@ -174,6 +188,10 @@
 							CONTACT_DATA_COLUMN, contact, -1);
 					}
 				}
+				if (cw->old) {
+					contact_free(cw->old);
+					g_free(cw->old);
+				}
 			}
 		}
 	}
@@ -194,6 +212,9 @@
 	gboolean bool;
 	gchar ch;
 	gint num;
+    GtkTreeModel* model;
+    GtkTreeIter iter;
+    GSList* new_list = NULL;
 	
 	contact = cw->contact;
 		
@@ -234,7 +255,21 @@
 			tmp = NULL;
 		}
 	}
-	
+
+	if (cw->image_email_dirty) {
+		changed = TRUE;
+	    model = gtk_tree_view_get_model(GTK_TREE_VIEW(cw->email_list));
+	 	if (gtk_tree_model_get_iter_first(model, &iter)) {
+			do {
+				Email* a = g_new0(Email, 1);
+				gtk_tree_model_get(model, &iter, WINDOW_ALIAS, &a->alias,
+					WINDOW_EMAIL, &a->email, WINDOW_REMARKS, &a->remarks, -1);
+				new_list = g_slist_prepend(new_list, a);
+			} while (gtk_tree_model_iter_next(model, &iter));
+			gslist_free(&cw->contact->emails, email_free);
+			cw->contact->emails = new_list;
+		}
+	}
 	if (changed)
 		contact_update(cw, cw->contact);
 }
@@ -245,7 +280,7 @@
 	ContactWindow* cw = (ContactWindow *) data;
 	gchar* error = NULL;
 
-	if (contact_dirty(cw->data)) {
+	if (contact_dirty(cw)) {
 		if (show_question(cw->window, _("Contact has unsaved data. Save?"))) {
 			save_contact(cw, &error);
 			if (error) {
@@ -345,8 +380,8 @@
 				email->remarks = gtk_editable_get_chars(GTK_EDITABLE(remark), 0, -1);
 				contact->emails = g_slist_append(contact->emails, email);
 				cw->new_row = TRUE;
+				cw->image_email_dirty = TRUE;
 				list_view_append_email(GTK_TREE_VIEW(cw->email_list), email);
-				contact_update(cw, contact);
 			}
 			else
 				email_free(email);
@@ -389,9 +424,9 @@
 				if (r) {
 					cw->contact->emails =
 						g_slist_remove(cw->contact->emails, remove);
-					contact_update(cw, cw->contact);
 					email_list_add(GTK_TREE_VIEW(cw->email_list), cw);
 					email_free(remove);
+					cw->image_email_dirty = TRUE;
 				}
 			}
 		}
@@ -535,7 +570,7 @@
 		}
 		
 		g_free(file);
-		contact_update(cw, cw->contact);
+		cw->image_email_dirty = TRUE;
 	}
 
 	gtk_widget_destroy(dialog);
@@ -563,8 +598,6 @@
 							gchar *new_text,
 							gpointer data) {
 	ContactWindow* win = (ContactWindow *) data;
-	Contact *old_contact = NULL, *new_contact = NULL;
-	gchar* error;
 	GtkTreeIter iter;
 	GtkTreeModel* model;
 	GtkTreeViewColumn* column;
@@ -576,6 +609,8 @@
 	gtk_tree_model_get_iter_from_string(model, &iter, path_string);
 	gtk_tree_view_get_cursor(GTK_TREE_VIEW(win->email_list), &path, &column);
 
+	gtk_tree_path_free(path);
+
 	if (cell->text && new_text && strcmp(cell->text, new_text) == 0)
 		return;
 	
@@ -585,52 +620,12 @@
 			break;
 	}
 
-	if (win->abook && win->plugin) {	
-		if (contact_is_new) {
-			new_contact = contact_copy(win->contact);
-/*			list_view_append_contact(
-					GTK_TREE_VIEW(win->main->contact_list), new_contact);
-			while (gtk_events_pending())
-  				gtk_main_iteration();*/
-			win->plugin->set_contact(win->abook, new_contact, &error);
-			if (! error) {
-				contact_is_new = FALSE;
-				list_view_append_contact(
-					GTK_TREE_VIEW(win->main->contact_list), new_contact);
-				while (gtk_events_pending())
-  					gtk_main_iteration();
-			}
-		}
-		else {
-			old_contact = win->contact;
-			new_contact = contact_copy(old_contact);
-			contact_set_attr(new_contact, atoi(path_string), col, new_text);
-/*			gtk_list_store_set(GTK_LIST_STORE(model), &iter, col, new_text, -1);*/
-			error = contact_write_to_backend(
-					win->plugin, win->abook, old_contact, new_contact);
-			if (! error)
-				gtk_list_store_set(GTK_LIST_STORE(model), &iter, col, new_text, -1);
-		}
-	}
-	else {
-		show_message(win->window, GTK_UTIL_MESSAGE_ERROR, "Unresolved error");
-	}
-	if (error) {
-		show_message(win->window, GTK_UTIL_MESSAGE_ERROR, "%s", error);
-		g_free(error);
-		contact_free(new_contact);
-		g_free(new_contact);
-	}
-	else {
-		if (old_contact) {
-			contact_free(old_contact);
-			g_free(old_contact);
-		}
-		win->contact = new_contact;
-		contact_entry_data_update(win);
-		abook_list_cursor_changed_cb(
-				GTK_TREE_VIEW(win->main->abook_list), win->main);
+	if (! contact_is_new) {
+		contact_set_attr(win->contact, atoi(path_string), col, new_text);
 	}
+	
+	win->image_email_dirty = TRUE;
+	gtk_list_store_set(GTK_LIST_STORE(model), &iter, col, new_text, -1);
 }
 
 static ContactEntryData* get_contact_entry_data(
@@ -804,7 +799,7 @@
 	gslist_free(&cw->contact->emails, email_free);
 	cw->contact->emails = NULL;
 	gtk_tree_model_foreach(tree_model, contact_reorder_emails, cw);
-	contact_update(cw, cw->contact);
+	cw->image_email_dirty = TRUE;
 }
 
 static void contact_widget(ContactWindow* cw) {
@@ -1065,7 +1060,8 @@
 
 	dialog = g_new0(ContactWindow, 1);
 	dialog->main = main;
-	dialog->contact = contact;
+	dialog->old = contact;
+	dialog->contact = contact_copy(contact);
 	dialog->new_row = FALSE;
 	
 	view = GTK_TREE_VIEW(main->abook_list);



More information about the Commits mailing list