[Evolution-hackers] Save calendar as comma separated file



Hi there Rodrigo,

Inspired by the GNOME bounty which you can find here:

	http://www.gnome.org/bounties/Tasks.html#127557

I created support for CSV-files while saving calendars.

	http://bugzilla.gnome.org/show_bug.cgi?id=127557

I don't think I will need to explain how things works. It's very easy
and done in your save-calendar plugin.

I also attached the patch.


Perhaps I will work some more on it, cleanup some stuff. But right now I
am going to eat my dinner :-) (maybe tomorrow).


-- 
Philip Van Hoof, Software Developer @ Cronos
home: me at freax dot org
gnome: pvanhoof at gnome dot org
work: philip dot vanhoof at cronos dot be
junk: philip dot vanhoof at gmail dot com
http://www.freax.be, http://www.freax.eu.org
Index: org-gnome-save-calendar.eplug.in
===================================================================
RCS file: /cvs/gnome/evolution/plugins/save-calendar/org-gnome-save-calendar.eplug.in,v
retrieving revision 1.4
diff -u -r1.4 org-gnome-save-calendar.eplug.in
--- org-gnome-save-calendar.eplug.in	21 Oct 2004 01:32:26 -0000	1.4
+++ org-gnome-save-calendar.eplug.in	18 Nov 2004 21:16:28 -0000
@@ -9,6 +9,12 @@
 			<menu id="org.gnome.evolution.calendar.source.popup" target="source">
 				<item type="item" path="19.save_calendar" label="Save to _Disk" icon="stock_save" activate="org_gnome_save_calendar"/>
 			</menu>
+			<menu id="org.gnome.evolution.tasks.source.popup" target="source">
+				<item type="item" path="19.save_tasks_csv" label="Save _csv to Disk" icon="stock_save" activate="org_gnome_save_tasks_csv"/>
+			</menu>
+			<menu id="org.gnome.evolution.calendar.source.popup" target="source">
+				<item type="item" path="19.save_calendar_csv" label="Save _csv to Disk" icon="stock_save" activate="org_gnome_save_calendar_csv"/>
+			</menu>
 		</hook>
 	</e-plugin>
-</e-plugin-list>
\ No newline at end of file
+</e-plugin-list>
Index: save-calendar.c
===================================================================
RCS file: /cvs/gnome/evolution/plugins/save-calendar/save-calendar.c,v
retrieving revision 1.3
diff -u -r1.3 save-calendar.c
--- save-calendar.c	21 Oct 2004 11:21:42 -0000	1.3
+++ save-calendar.c	18 Nov 2004 21:16:28 -0000
@@ -40,9 +40,13 @@
 #include <libedataserverui/e-source-selector.h>
 #include <libecal/e-cal.h>
 #include <calendar/gui/e-cal-popup.h>
+#include <libgnomevfs/gnome-vfs.h>
+#include <string.h>
 
 void org_gnome_save_calendar (EPlugin *ep, ECalPopupTargetSource *target);
 void org_gnome_save_tasks (EPlugin *ep, ECalPopupTargetSource *target);
+void org_gnome_save_calendar_csv (EPlugin *ep, ECalPopupTargetSource *target);
+void org_gnome_save_tasks_csv (EPlugin *ep, ECalPopupTargetSource *target);
 
 static void
 display_error_message (GtkWidget *parent, GError *error)
@@ -55,6 +59,178 @@
 	gtk_widget_destroy (dialog);
 }
 
+
+static GString *
+gslist_to_csv (GSList *list_in)
+{
+	GSList *list = list_in;
+	GString *str = g_string_new ("");
+	while (list) {
+		str = g_string_append (str, (const char*)list->data);
+		list = g_slist_next (list);
+		if (list) str = g_string_append (str, ", ");
+	}
+	return str;
+}
+
+static gchar *
+icaltimetype_to_gchar (icaltimetype *t)
+{
+	if (t) return g_strdup_printf (_("%2d/%2d/%4d %2d:%2d:%2d"), t->month, t->day, t->year, t->hour, t->minute, t->second);
+	else return "";
+}
+
+static void
+do_save_calendar_csv (EPlugin *ep, ECalPopupTargetSource *target, ECalSourceType type)
+{
+	ESource *primary_source;
+	char *dest_uri;
+	ECal *source_client;
+	GtkWidget *dialog;
+	GError *error = NULL;
+	GList *objects=NULL, *csv_lines=NULL;
+	GnomeVFSResult result;
+	GnomeVFSHandle *handle;
+	GnomeVFSURI *uri;
+	
+	primary_source = e_source_selector_peek_primary_selection (target->selector);
+
+	/* ask the user for destination file */
+#ifdef USE_GTKFILECHOOSER
+	dialog = gtk_file_chooser_dialog_new (_("Select destination file"),
+					      NULL,
+					      GTK_FILE_CHOOSER_ACTION_SAVE,
+					      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+					      GTK_STOCK_SAVE, GTK_RESPONSE_OK,
+					      NULL);
+	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+#else
+	dialog = gtk_file_selection_new (_("Select destination file"));
+#endif
+
+	if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK) {
+		gtk_widget_destroy (dialog);
+		return;
+	}
+
+#ifdef USE_GTKFILECHOOSER
+	dest_uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
+#else
+	dest_uri = g_strdup (gtk_file_selection_get_filename (GTK_FILE_SELECTION (dialog)));
+#endif
+	gtk_widget_destroy (dialog);
+	if (!dest_uri)
+		return;
+
+	/* open source client */
+	source_client = e_cal_new (primary_source, type);
+	if (!e_cal_open (source_client, TRUE, &error)) {
+		display_error_message (gtk_widget_get_toplevel (GTK_WIDGET (target->selector)), error);
+		g_object_unref (source_client);
+		g_free (dest_uri);
+		g_error_free (error);
+		return;
+	}
+
+	if (e_cal_get_object_list_as_comp (source_client, "#t", &objects, NULL)) {
+		while (objects != NULL) {
+
+			const char *uid, *categories, *url;
+			GString *comment_list_str, *contact_list_str, *description_list_str, *attendee_list_str;
+			GSList *comment_list, *contact_list, *description_list, *attendee_list;
+			struct icaltimetype *completed, *created;
+			gchar *completed_gchar, *created_gchar, *dtend_gchar, *dtstart_gchar, *dtdue_gchar;
+			ECalComponentDateTime dtend, dtstart, dtdue;
+			int *percent, *priority;
+			ECalComponentText summary; 
+			gchar *line = NULL;
+			ECalComponent *comp = objects->data;
+			e_cal_component_commit_sequence (comp);
+
+		/* g_print ("%s\n",  e_cal_component_get_as_string (comp)); */
+
+		/* Question: In what format and what information do you (bounty-team)/we want this? */
+
+			e_cal_component_get_uid (comp, &uid);
+			e_cal_component_get_categories (comp, &categories);
+			e_cal_component_get_comment_list (comp, &comment_list);
+			e_cal_component_get_completed (comp, &completed);
+			e_cal_component_get_contact_list (comp, &contact_list);
+			e_cal_component_get_created (comp, &created);
+			e_cal_component_get_description_list (comp, &description_list);
+			e_cal_component_get_dtend (comp, &dtend);
+
+			e_cal_component_get_dtstart (comp, &dtstart);
+			e_cal_component_get_due (comp, &dtdue);
+			e_cal_component_get_percent (comp, &percent);
+			e_cal_component_get_priority (comp, &priority);
+			e_cal_component_get_summary (comp, &summary);
+			e_cal_component_get_url (comp, &url);
+			e_cal_component_get_attendee_list (comp, &attendee_list);
+
+			dtend_gchar = icaltimetype_to_gchar (dtend.value);
+			dtstart_gchar = icaltimetype_to_gchar (dtstart.value);
+
+			dtdue_gchar = icaltimetype_to_gchar (dtdue.value);
+			completed_gchar = icaltimetype_to_gchar (completed);
+			created_gchar = icaltimetype_to_gchar (created);
+
+			comment_list_str = gslist_to_csv (comment_list);
+			contact_list_str = gslist_to_csv (contact_list);
+			description_list_str = gslist_to_csv (description_list);
+			attendee_list_str = gslist_to_csv (attendee_list);
+
+			line = g_strdup_printf (
+					  _("'%s', '%s', '%s', '%s', "
+						"'%s', '%s', '%s', '%s', "
+						"'%s', '%s', '%d', "
+						"'%d', '%s', '%s', '%s'\n"), 
+
+				uid?uid:"", categories?categories:"", comment_list_str->str, completed_gchar, 
+				contact_list_str->str, created_gchar, description_list_str->str, dtend_gchar, 
+				dtstart_gchar, dtdue_gchar, percent?*percent:-1,
+				priority?*priority:-1, summary.value?summary.value:"", url?url:"", attendee_list_str->str);
+
+/* There is some pointer invalid :-\ (I don't see which one)
+			g_string_free (comment_list_str, FALSE);
+			g_string_free (contact_list_str, FALSE);
+			g_string_free (description_list_str, FALSE);
+			g_string_free (attendee_list_str, FALSE);
+			g_free (completed_gchar);
+			g_free (created_gchar);
+			g_free (dtend_gchar);
+			g_free (dtstart_gchar);
+			g_free (dtdue_gchar);
+*/
+			csv_lines = g_list_append (csv_lines, line);
+			objects = g_list_next (objects);
+		}
+
+		uri = gnome_vfs_uri_new (dest_uri);
+		result = gnome_vfs_open_uri (&handle, uri, GNOME_VFS_OPEN_WRITE);
+
+		if (result != GNOME_VFS_OK) {
+			gnome_vfs_create (&handle, dest_uri, GNOME_VFS_OPEN_WRITE, TRUE, GNOME_VFS_PERM_USER_ALL);
+			result = gnome_vfs_open_uri (&handle, uri, GNOME_VFS_OPEN_WRITE);
+		}
+
+		if (result == GNOME_VFS_OK) {
+			while (csv_lines) {
+				GnomeVFSFileSize *bytes_written=NULL;
+				gchar *buffer = csv_lines->data;
+
+				gnome_vfs_write (handle, buffer, strlen (buffer),
+					bytes_written);
+
+				csv_lines = g_list_next (csv_lines);
+			}
+			gnome_vfs_close (handle);
+		} else {
+			g_print ("Write failed\n");
+		}
+	}
+}
+
 static void
 do_save_calendar (EPlugin *ep, ECalPopupTargetSource *target, ECalSourceType type)
 {
@@ -146,4 +322,16 @@
 org_gnome_save_tasks (EPlugin *ep, ECalPopupTargetSource *target)
 {
 	do_save_calendar (ep, target, E_CAL_SOURCE_TYPE_TODO);
+}
+
+void
+org_gnome_save_calendar_csv (EPlugin *ep, ECalPopupTargetSource *target)
+{
+	do_save_calendar_csv (ep, target, E_CAL_SOURCE_TYPE_EVENT);
+}
+
+void
+org_gnome_save_tasks_csv (EPlugin *ep, ECalPopupTargetSource *target)
+{
+	do_save_calendar_csv (ep, target, E_CAL_SOURCE_TYPE_TODO);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]