commit 5d03ee59e8cadbe65b23a3c81ee448d0be5754c1
Author: Milan Crha
Date:   Thu Jul 30 14:53:32 2009 +0200

    Bug #584030 - [Mail-To-Task] improve duplicate handling and such

diff --git a/calendar/gui/Makefile.am b/calendar/gui/Makefile.am
index 6b515f4..6c4f34b 100644
--- a/calendar/gui/Makefile.am
+++ b/calendar/gui/Makefile.am
@@ -29,6 +29,7 @@ component_LTLIBRARIES = libevolution-calendar.la
 ecalendarincludedir = $(privincludedir)/calendar/gui
 
 ecalendarinclude_HEADERS =	\
+	cal-editor-utils.h	\
 	e-cal-config.h		\
 	e-cal-popup.h		\
 	itip-utils.h
@@ -69,8 +70,8 @@ etspec_DATA =				\
 
 libevolution_calendar_la_SOURCES =		\
 	$(IDL_GENERATED)			\
-	e-attachment-handler-calendar.c		\
-	e-attachment-handler-calendar.h		\
+	cal-editor-utils.c			\
+	cal-editor-utils.h			\
 	cal-search-bar.c			\
 	cal-search-bar.h			\
 	calendar-config.c			\
@@ -92,6 +93,8 @@ libevolution_calendar_la_SOURCES =		\
 	control-factory.h			\
 	e-alarm-list.c				\
 	e-alarm-list.h				\
+	e-attachment-handler-calendar.c		\
+	e-attachment-handler-calendar.h		\
 	e-cal-component-preview.c		\
 	e-cal-component-preview.h		\
 	e-cal-component-memo-preview.c		\
diff --git a/calendar/gui/cal-editor-utils.c b/calendar/gui/cal-editor-utils.c
new file mode 100644
index 0000000..ece9abe
--- /dev/null
+++ b/calendar/gui/cal-editor-utils.c
@@ -0,0 +1,118 @@
+/*
+ * This program 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; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <e-util/e-dialog-utils.h>
+
+#include "cal-editor-utils.h"
+
+#include "e-comp-editor-registry.h"
+#include "dialogs/event-editor.h"
+#include "dialogs/task-editor.h"
+#include "dialogs/memo-editor.h"
+
+extern ECompEditorRegistry *comp_editor_registry;
+
+/**
+ * open_component_editor:
+ * @client: Already opened #ECal, where to store the component
+ * @comp: #ECalComponent component to be stored
+ * @is_new: Whether the @comp is a new component or an existing
+ * @error: #GError for possible error reporting
+ *
+ * Opens component editor for the event stored in the comp component.
+ * If such component exists in the client already (with the same UID),
+ * then there's opened already stored event, instead of the comp.
+ *
+ * It blocks until finished and should be called in the main thread.
+ **/
+void
+open_component_editor (ECal *client, ECalComponent *comp, gboolean is_new, GError **error)
+{
+	ECalComponentId *id;
+	CompEditorFlags flags = 0;
+	CompEditor *editor = NULL;
+
+	g_return_if_fail (client != NULL);
+	g_return_if_fail (comp != NULL);
+
+	id = e_cal_component_get_id (comp);
+	g_return_if_fail (id != NULL);
+	g_return_if_fail (id->uid != NULL);
+
+	if (is_new) {
+		flags |= COMP_EDITOR_NEW_ITEM;
+	} else {
+		editor = e_comp_editor_registry_find (comp_editor_registry, id->uid);
+	}
+
+	if (!editor) {
+		if (itip_organizer_is_user (comp, client))
+			flags |= COMP_EDITOR_USER_ORG;
+
+		switch (e_cal_component_get_vtype (comp)) {
+		case E_CAL_COMPONENT_EVENT:
+			if (e_cal_component_has_attendees (comp))
+				flags |= COMP_EDITOR_MEETING;
+
+			editor = event_editor_new (client, flags);
+
+			if (flags & COMP_EDITOR_MEETING)
+				event_editor_show_meeting (EVENT_EDITOR (editor));
+			break;
+		case E_CAL_COMPONENT_TODO:
+			if (e_cal_component_has_attendees (comp))
+				flags |= COMP_EDITOR_IS_ASSIGNED;
+
+			editor = task_editor_new (client, flags);
+
+			if (flags & COMP_EDITOR_IS_ASSIGNED)
+				task_editor_show_assignment (TASK_EDITOR (editor));
+			break;
+		case E_CAL_COMPONENT_JOURNAL:
+			if (e_cal_component_has_organizer (comp))
+				flags |= COMP_EDITOR_IS_SHARED;
+
+			editor = memo_editor_new (client, flags);
+			break;
+		default:
+			if (error)
+				*error = g_error_new (E_CALENDAR_ERROR, E_CALENDAR_STATUS_INVALID_OBJECT, "%s", _("Invalid object"));
+			break;
+		}
+
+		if (editor) {
+			comp_editor_edit_comp (editor, comp);
+
+			/* request save for new events */
+			comp_editor_set_changed (editor, is_new);
+
+			e_comp_editor_registry_add (comp_editor_registry, editor, TRUE);
+		}
+	}
+
+	if (editor)
+		gtk_window_present (GTK_WINDOW (editor));
+
+	e_cal_component_free_id (id);
+}
diff --git a/calendar/gui/cal-editor-utils.h b/calendar/gui/cal-editor-utils.h
new file mode 100644
index 0000000..de37367
--- /dev/null
+++ b/calendar/gui/cal-editor-utils.h
@@ -0,0 +1,33 @@
+/*
+ * This program 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; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ *
+ * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
+ *
+ */
+
+#ifndef CAL_EDITOR_UTILS_HEADER
+#define CAL_EDITOR_UTILS_HEADER
+
+#include <glib.h>
+#include <libecal/e-cal.h>
+#include <libecal/e-cal-component.h>
+
+G_BEGIN_DECLS
+
+void open_component_editor (ECal *client, ECalComponent *comp, gboolean is_new, GError **error);
+
+G_END_DECLS
+
+#endif
diff --git a/plugins/mail-to-task/mail-to-task.c b/plugins/mail-to-task/mail-to-task.c
index e7fa727..1a0ab7c 100644
--- a/plugins/mail-to-task/mail-to-task.c
+++ b/plugins/mail-to-task/mail-to-task.c
@@ -52,6 +52,7 @@
 #include "e-util/e-dialog-utils.h"
 #include <gtkhtml/gtkhtml.h>
 #include <calendar/common/authentication.h>
+#include <calendar/gui/cal-editor-utils.h>
 
 static gchar *
 clean_name(const guchar *s)
@@ -321,6 +322,261 @@ report_error_idle (const gchar *format, const gchar *param)
 	g_idle_add ((GSourceFunc)do_report_error, err);
 }
 
+struct _manage_comp
+{
+	ECal *client;
+	ECalComponent *comp;
+	icalcomponent *stored_comp; /* the one in client already */
+};
+
+static void
+free_manage_comp_struct (struct _manage_comp *mc)
+{
+	g_return_if_fail (mc != NULL);
+
+	g_object_unref (mc->comp);
+	g_object_unref (mc->client);
+	if (mc->stored_comp)
+		icalcomponent_free (mc->stored_comp);
+	g_free (mc);
+}
+
+static gint
+do_ask (const gchar *text, gboolean is_create_edit_add)
+{
+	gint res;
+	GtkWidget *dialog = gtk_message_dialog_new (NULL,
+		GTK_DIALOG_MODAL, 
+		GTK_MESSAGE_QUESTION,
+		is_create_edit_add ? GTK_BUTTONS_NONE : GTK_BUTTONS_YES_NO,
+		"%s", text);
+
+	if (is_create_edit_add) {
+		gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+			GTK_STOCK_EDIT, GTK_RESPONSE_YES,
+			GTK_STOCK_NEW, GTK_RESPONSE_NO,
+			NULL);
+	}
+
+	res = gtk_dialog_run (GTK_DIALOG (dialog));
+
+	gtk_widget_destroy (dialog);
+
+	return res;
+}
+
+static const gchar *
+get_question_edit_old (ECalSourceType source_type)
+{
+	const gchar *ask = NULL;
+
+	switch (source_type) {
+	case E_CAL_SOURCE_TYPE_EVENT:
+		ask = _("Selected calendar contains event '%s' already. Would you like to edit the old event?");
+		break;
+	case E_CAL_SOURCE_TYPE_TODO:
+		ask = _("Selected task list contains task '%s' already. Would you like to edit the old task?");
+		break;
+	case E_CAL_SOURCE_TYPE_JOURNAL:
+		ask = _("Selected memo list contains memo '%s' already. Would you like to edit the old memo?");
+		break;
+	default:
+		g_assert_not_reached ();
+		break;
+	}
+
+	return ask;
+}
+
+static const gchar *
+get_question_create_new (ECalSourceType source_type)
+{
+	const gchar *ask = NULL;
+
+	switch (source_type) {
+	case E_CAL_SOURCE_TYPE_EVENT:
+		ask = _("Selected calendar contains some events for the given mails already. Would you like to create new events anyway?");
+		break;
+	case E_CAL_SOURCE_TYPE_TODO:
+		ask = _("Selected task list contains some tasks for the given mails already. Would you like to create new tasks anyway?");
+		break;
+	case E_CAL_SOURCE_TYPE_JOURNAL:
+		ask = _("Selected memo list contains some memos for the given mails already. Would you like to create new memos anyway?");
+		break;
+	default:
+		g_assert_not_reached ();
+		break;
+	}
+
+	return ask;
+}
+
+static const gchar *
+get_question_create_new_n (ECalSourceType source_type, gint count)
+{
+	const gchar *ask = NULL;
+
+	switch (source_type) {
+	case E_CAL_SOURCE_TYPE_EVENT:
+		ask = ngettext (
+			"Selected calendar contains an event for the given mail already. Would you like to create new event anyway?",
+			"Selected calendar contains events for the given mails already. Would you like to create new events anyway?",
+			count);
+		break;
+	case E_CAL_SOURCE_TYPE_TODO:
+		ask = ngettext (
+			"Selected task list contains a task for the given mail already. Would you like to create new task anyway?",
+			"Selected task list contains tasks for the given mails already. Would you like to create new tasks anyway?",
+			count);
+		break;
+	case E_CAL_SOURCE_TYPE_JOURNAL:
+		ask = ngettext (
+			"Selected memo list contains a memo for the given mail already. Would you like to create new memo anyway?",
+			"Selected memo list contains memos for the given mails already. Would you like to create new memos anyway?",
+			count);
+		break;
+	default:
+		g_assert_not_reached ();
+		break;
+	}
+
+	return ask;
+}
+
+static gboolean
+do_manage_comp_idle (GSList *manage_comp_datas)
+{
+	GError *error = NULL;
+	guint with_old = 0;
+	gboolean need_editor = FALSE;
+	ECalSourceType source_type = E_CAL_SOURCE_TYPE_LAST;
+	GSList *l;
+
+	g_return_val_if_fail (manage_comp_datas != NULL, FALSE);
+
+	if (manage_comp_datas->data) {
+		struct _manage_comp *mc = manage_comp_datas->data;
+
+		if (mc->comp && (e_cal_component_has_attendees (mc->comp) || e_cal_component_has_organizer (mc->comp)))
+			need_editor = TRUE;
+
+		source_type = e_cal_get_source_type (mc->client);
+	}
+
+	if (source_type == E_CAL_SOURCE_TYPE_LAST) {
+		g_slist_foreach (manage_comp_datas, (GFunc) free_manage_comp_struct, NULL);
+		g_slist_free (manage_comp_datas);
+
+		g_warning ("mail-to-task: Incorrect call of %s, no data given", G_STRFUNC);
+		return FALSE;
+	}
+
+	for (l = manage_comp_datas; l; l = l->next) {
+		struct _manage_comp *mc = l->data;
+
+		if (mc && mc->stored_comp)
+			with_old++;
+	}
+
+	if (need_editor) {
+		for (l = manage_comp_datas; l && !error; l = l->next) {
+			ECalComponent *edit_comp = NULL;
+			struct _manage_comp *mc = l->data;
+
+			if (!mc)
+				continue;
+
+			if (mc->stored_comp) {
+				const gchar *ask = get_question_edit_old (source_type);
+
+				if (ask) {
+					char *msg = g_strdup_printf (ask, icalcomponent_get_summary (mc->stored_comp) ? icalcomponent_get_summary (mc->stored_comp) : _("[No Summary]"));
+					gint chosen;
+
+					chosen = do_ask (msg, TRUE);
+
+					if (chosen == GTK_RESPONSE_YES) {
+						edit_comp = e_cal_component_new ();
+						if (!e_cal_component_set_icalcomponent (edit_comp, icalcomponent_new_clone (mc->stored_comp))) {
+							g_object_unref (edit_comp);
+							edit_comp = NULL;
+
+							error = g_error_new (E_CALENDAR_ERROR, E_CALENDAR_STATUS_INVALID_OBJECT, "%s", _("Invalid object returned from a server"));
+						}
+					} else if (chosen == GTK_RESPONSE_NO) {
+						/* user wants to create a new event, thus generate a new UID */
+						gchar *new_uid = e_cal_component_gen_uid ();
+
+						edit_comp = mc->comp;
+						e_cal_component_set_uid (edit_comp, new_uid);
+						e_cal_component_set_recurid (edit_comp, NULL);
+
+						g_free (new_uid);
+					}
+
+					g_free (msg);
+				}
+			} else {
+				edit_comp = mc->comp;
+			}
+
+			if (edit_comp) {
+				open_component_editor (mc->client, edit_comp, edit_comp == mc->comp, &error);
+				if (edit_comp != mc->comp)
+					g_object_unref (edit_comp);
+			}
+		}
+	} else {
+		gboolean can = TRUE;
+
+		if (with_old > 0) {
+			const gchar *ask = NULL;
+
+			can = FALSE;
+			
+			if (with_old == g_slist_length (manage_comp_datas)) {
+				ask = get_question_create_new_n (source_type, with_old);
+			} else {
+				ask = get_question_create_new (source_type);
+			}
+
+			if (ask)
+				can = do_ask (ask, FALSE) == GTK_RESPONSE_YES;
+		}
+
+		if (can) {
+			for (l = manage_comp_datas; l && !error; l = l->next) {
+				struct _manage_comp *mc = l->data;
+
+				if (!mc)
+					continue;
+
+				if (mc->stored_comp) {
+					gchar *new_uid = e_cal_component_gen_uid ();
+
+					e_cal_component_set_uid (mc->comp, new_uid);
+					e_cal_component_set_recurid (mc->comp, NULL);
+
+					g_free (new_uid);
+				}
+
+				e_cal_create_object (mc->client, e_cal_component_get_icalcomponent (mc->comp), NULL, &error);
+			}
+		}
+	}
+
+	if (error) {
+		e_notice (NULL, GTK_MESSAGE_ERROR, _("An error occurred during processing: %s"), error->message);
+		g_error_free (error);
+	}
+
+	g_slist_foreach (manage_comp_datas, (GFunc) free_manage_comp_struct, NULL);
+	g_slist_free (manage_comp_datas);
+
+	return FALSE;
+}
+
 typedef struct {
 	ECal *client;
 	CamelFolder *folder;
@@ -361,6 +617,7 @@ do_mail_to_event (AsyncData *data)
 			}
 		}
 	} else {
+		GSList *mcs = NULL;
 		gint i;
 		ECalSourceType source_type = e_cal_get_source_type (client);
 		ECalComponentDateTime dt, dt2;
@@ -382,6 +639,7 @@ do_mail_to_event (AsyncData *data)
 			ECalComponentText text;
 			icalproperty *icalprop;
 			icalcomponent *icalcomp;
+			struct _manage_comp *mc;
 
 			/* retrieve the message from the CamelFolder */
 			message = camel_folder_get_message (folder, g_ptr_array_index (uids, i), NULL);
@@ -444,23 +702,31 @@ do_mail_to_event (AsyncData *data)
 			/* set attachment files */
 			set_attachments (client, comp, message);
 
+			/* no need to increment a sequence number, this is a new component */
+			e_cal_component_abort_sequence (comp);
+
 			icalcomp = e_cal_component_get_icalcomponent (comp);
 
 			icalprop = icalproperty_new_x ("1");
 			icalproperty_set_x_name (icalprop, "X-EVOLUTION-MOVE-CALENDAR");
 			icalcomponent_add_property (icalcomp, icalprop);
 
-			/* save the task to the selected source */
-			if (!e_cal_create_object (client, icalcomp, NULL, &err)) {
-				report_error_idle (_("Could not create object. %s"), err ? err->message : _("Unknown error"));
+			mc = g_new0 (struct _manage_comp, 1);
+			mc->client = g_object_ref (client);
+			mc->comp = g_object_ref (comp);
 
-				if (err)
-					g_error_free (err);
-				err = NULL;
-			}
+			if (!e_cal_get_object (client, icalcomponent_get_uid (icalcomp), NULL, &(mc->stored_comp), NULL))
+				mc->stored_comp = NULL;
+
+			mcs = g_slist_append (mcs, mc);
 
 			g_object_unref (comp);
 		}
+
+		if (mcs) {
+			/* process this in the main thread, as we may ask user too */
+			g_idle_add ((GSourceFunc)do_manage_comp_idle, mcs);
+		}
 	}
 
 	/* free memory */
@@ -588,6 +854,8 @@ mail_to_event (ECalSourceType source_type, gboolean with_attendees, GPtrArray *u
 		/* ask the user which tasks list to save to */
 		dialog = e_source_selector_dialog_new (NULL, source_list);
 
+		e_source_selector_dialog_select_default_source (E_SOURCE_SELECTOR_DIALOG (dialog));
+
 		if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
 			source = e_source_selector_dialog_peek_primary_selection (E_SOURCE_SELECTOR_DIALOG (dialog));
 
@@ -678,13 +946,15 @@ org_gnome_mail_to_meeting_menu (EPlugin *ep, EMMenuTargetSelect *t)
 void
 org_gnome_mail_to_task (gpointer ep, EMPopupTargetSelect *t)
 {
-	mail_to_event (E_CAL_SOURCE_TYPE_TODO, TRUE, t->uids, t->folder, (EMFolderView *) t->target.widget);
+	/* do not create assigned tasks */
+	mail_to_event (E_CAL_SOURCE_TYPE_TODO, FALSE, t->uids, t->folder, (EMFolderView *) t->target.widget);
 }
 
 void
 org_gnome_mail_to_task_menu (EPlugin *ep, EMMenuTargetSelect *t)
 {
-	mail_to_event (E_CAL_SOURCE_TYPE_TODO, TRUE, t->uids, t->folder, (EMFolderView *) t->target.widget);
+	/* do not create assigned tasks */
+	mail_to_event (E_CAL_SOURCE_TYPE_TODO, FALSE, t->uids, t->folder, (EMFolderView *) t->target.widget);
 }
 
 void
diff --git a/plugins/mail-to-task/org-gnome-mail-to-task.eplug.xml b/plugins/mail-to-task/org-gnome-mail-to-task.eplug.xml
index 65ae2f4..e8494fb 100644
--- a/plugins/mail-to-task/org-gnome-mail-to-task.eplug.xml
+++ b/plugins/mail-to-task/org-gnome-mail-to-task.eplug.xml
@@ -15,7 +15,7 @@
 	  type="item"
 	  path="70.mail_to_event1"
 	  icon="appointment-new"
-	  _label="Convert to an _Event"
+	  _label="Create an _Event"
 	  enable="many"
 	  visible="many"
 	  activate="org_gnome_mail_to_event"/>
@@ -23,15 +23,15 @@
 	  type="item"
 	  path="70.mail_to_event2"
 	  icon="stock_new-meeting"
-	  _label="Convert to a _Meeting"
-	  enable="many"
+	  _label="Create a _Meeting"
+	  enable="one"
 	  visible="many"
 	  activate="org_gnome_mail_to_meeting"/>
 	<item
 	  type="item"
 	  path="70.mail_to_event3"
 	  icon="stock_todo"
-	  _label="Convert to a _Task"
+	  _label="Create a _Task"
 	  enable="many"
 	  visible="many"
 	  activate="org_gnome_mail_to_task"/>
@@ -39,7 +39,7 @@
 	  type="item"
 	  path="70.mail_to_event4"
 	  icon="stock_insert-note"
-	  _label="Convert to a Mem_o"
+	  _label="Create a Mem_o"
 	  enable="many"
 	  visible="many"
 	  activate="org_gnome_mail_to_memo"/>
@@ -59,7 +59,7 @@
 	  type="item"
 	  verb="ConvertMeeting"
 	  path="/commands/ConvertMeeting"
-	  enable="many"
+	  enable="one"
 	  activate="org_gnome_mail_to_meeting_menu"/>
 	<item
 	  type="item"
diff --git a/plugins/mail-to-task/org-gnome-mail-to-task.xml b/plugins/mail-to-task/org-gnome-mail-to-task.xml
index 98accf4..c3d72b6 100644
--- a/plugins/mail-to-task/org-gnome-mail-to-task.xml
+++ b/plugins/mail-to-task/org-gnome-mail-to-task.xml
@@ -1,16 +1,16 @@
 <Root>
   <commands>
-    <cmd name="ConvertEvent" _label="Convert to an _Event"
-      _tip="Convert the selected message to a new event"
+    <cmd name="ConvertEvent" _label="Create an _Event"
+      _tip="Create a new event from the selected message"
       pixtype="stock" pixname="appointment-new"/>
-    <cmd name="ConvertMeeting" _label="Convert to a _Meeting"
-      _tip="Convert the selected message to a new meeting"
+    <cmd name="ConvertMeeting" _label="Create a _Meeting"
+      _tip="Create a new meeting from the selected message"
       pixtype="stock" pixname="stock_new-meeting"/>
-    <cmd name="ConvertTask" _label="Convert to a _Task"
-      _tip="Convert the selected message to a new task"
+    <cmd name="ConvertTask" _label="Create a _Task"
+      _tip="Create a new task from the selected message"
       pixtype="stock" pixname="stock_todo"/>
-    <cmd name="ConvertMemo" _label="Convert to a Mem_o"
-      _tip="Convert the selected message to a new memo"
+    <cmd name="ConvertMemo" _label="Create a Mem_o"
+      _tip="Create a new memo from the selected message"
       pixtype="stock" pixname="stock_insert-note"/>
   </commands>
 
