--- evolution-1.4.6/calendar/conduits/todo/todo-conduit.c	2003-04-07 13:06:37.000000000 -0400
+++ work_evo1.4.6/calendar/conduits/todo/todo-conduit.c	2005-06-08 23:53:32.000000000 -0400
@@ -42,6 +42,8 @@
 #include <e-pilot-util.h>
 #include <e-config-listener.h>
 
+#include "e-util/e-categories-master-list-wombat.h"
+
 GnomePilotConduit * conduit_get_gpilot_conduit (guint32);
 void conduit_destroy_gpilot_conduit (GnomePilotConduit*);
 
@@ -79,6 +81,8 @@ struct _EToDoLocalRecord {
 	struct ToDo *todo;
 };
 
+int lastDesktopUniqueID;
+
 static void
 todoconduit_destroy_record (EToDoLocalRecord *local) 
 {
@@ -563,6 +567,55 @@ local_record_to_pilot_record (EToDoLocal
 }
 
 /*
+ * Adds a category to the category app info structure (name and ID),
+ * sets category->renamed[i] to true if possible to rename.
+ * 
+ * This will be packed and written to the app info block during post_sync.
+ */
+ 
+static int
+add_category_if_possible(char *cat_to_add, struct CategoryAppInfo *category)
+{
+	int i, j;
+	int retval = 0; // 0 is the Unfiled category
+	
+	for(i=0; i<16; i++){
+		// if strlen is 0, then the category is empty
+		// the PalmOS doesn't let 0-length strings for
+		// categories
+		if(strlen(category->name[i]) == 0){
+			int cat_to_add_len = strlen(cat_to_add);
+			retval = i;
+			
+			/* only 15 characters for category, 16th is
+			 * '\0' can't do direct mem transfer due to
+			 * declaration type
+			 */
+			for(j=0; j<cat_to_add_len; j++)
+				category->name[i][j] = cat_to_add[j];
+
+			for(j=cat_to_add_len; j<16; j++)
+				category->name[i][j] = '\0';
+			
+			category->ID[i] = lastDesktopUniqueID;
+			lastDesktopUniqueID++;
+			
+			category->renamed[i] = TRUE;
+			
+			LOG(g_message("*** adding category '%s', ID %d ***",
+				category->name[i], category->ID[i]));
+			break;
+		}
+	}
+	
+	if(retval == 0){
+		LOG(g_message("*** not adding category - category list already full ***"));
+	}
+	
+	return retval;
+}
+
+/*
  * converts a CalComponent object to a EToDoLocalRecord
  */
 static void
@@ -602,10 +655,45 @@ local_record_from_comp (EToDoLocalRecord
 					ctxt->dbi->db_handle,
 					local->local.ID, &record, 
 					NULL, NULL, NULL, &cat) > 0) {
-			local->local.category = cat;			
+			local->local.category = cat;
 		}
 	}
 
+	/*
+	 * Grab category from existing category list in ctxt->ai.category
+	 */
+	if(local->local.category == 0){
+		GSList *categ_list_head, *categ_list_cur;
+		int cat = -1;
+		int i;
+		
+		cal_component_get_categories_list(comp, &categ_list_head);
+		
+		categ_list_cur = categ_list_head;
+		while (categ_list_cur && cat == -1)
+		{	
+			for(i=0; i<16; i++){
+				if(strcmp((char *)categ_list_cur->data,
+					  ctxt->ai.category.name[i]) == 0){
+					cat = i;
+					break;
+				}
+			}
+			
+			categ_list_cur = g_slist_next(categ_list_cur);
+		}
+		
+		if(cat != -1){
+			local->local.category = cat;
+		}
+		else{
+			local->local.category =
+				add_category_if_possible(
+					(char *)(categ_list_head->data),
+					&(ctxt->ai.category));
+ 		}
+ 	}
+
 	/* STOP: don't replace these with g_strdup, since free_ToDo
 	   uses free to deallocate */
 	cal_component_get_summary (comp, &summary);
@@ -678,6 +766,8 @@ local_record_from_uid (EToDoLocalRecord 
 	CalClientGetStatus status;
 
 	g_assert(local!=NULL);
+	
+	LOG(g_message("local_record_from_uid\n"));
 
 	status = cal_client_get_object (ctxt->client, uid, &comp);
 
@@ -697,12 +787,52 @@ local_record_from_uid (EToDoLocalRecord 
 
 }
 
+static ECategoriesMasterList *
+get_master_list (void)
+{
+	static ECategoriesMasterList *category_list = NULL;
+
+	if (category_list == NULL)
+		category_list = e_categories_master_list_wombat_new ();
+	return category_list;
+}
+
+static gboolean
+category_exists(char *category)
+{
+	/*
+	master_list = get_master_list ();
+	category_name = e_categories_master_list_nth (master_list, subid);
+	void        e_categories_master_list_append    (ECategoriesMasterList *ecml,
+													const char            *category,
+													const char            *color,
+													const char            *icon_file);
+    int         e_categories_master_list_count     (ECategoriesMasterList *ecml);
+	const char *e_categories_master_list_nth       (ECategoriesMasterList *ecml,
+													int                    n);
+	 */
+	
+	ECategoriesMasterList *ecml = get_master_list();
+	
+	int cat_count = e_categories_master_list_count(ecml);
+	int i;
+	
+	for(i=0; i<cat_count; i++){
+		const char *catn = e_categories_master_list_nth(ecml, i);
+		if(strcmp(catn, category) == 0){
+			return TRUE;
+		}
+	}
+	return FALSE;
+}
+
 
 static CalComponent *
 comp_from_remote_record (GnomePilotConduitSyncAbs *conduit,
 			 GnomePilotRecord *remote,
 			 CalComponent *in_comp,
-			 icaltimezone *timezone)
+			 icaltimezone *timezone,
+			 struct ToDoAppInfo *ai)
 {
 	CalComponent *comp;
 	struct ToDo todo;
@@ -712,6 +842,7 @@ comp_from_remote_record (GnomePilotCondu
 	icaltimezone *utc_zone;
 	int priority;
 	char *txt;
+	char *category;
 	
 	g_return_val_if_fail (remote != NULL, NULL);
 
@@ -788,7 +919,7 @@ comp_from_remote_record (GnomePilotCondu
 		priority = 3;
 		break;
 	case 2:
-		priority = 4;
+		priority = 5;
 		break;
 	case 3:
 		priority = 5;
@@ -808,6 +939,36 @@ comp_from_remote_record (GnomePilotCondu
 	else
 		cal_component_set_classification (comp, CAL_COMPONENT_CLASS_PUBLIC);
 
+	// set the category properly
+	category = ai->category.name[remote->category];
+	
+	
+	// TODO The Tasks editor page and search bar are not updated until
+	// a restart of the evolution client
+	
+	/*
+	master_list = get_master_list ();
+	category_name = e_categories_master_list_nth (master_list, subid);
+	void        e_categories_master_list_append    (ECategoriesMasterList *ecml,
+													const char            *category,
+													const char            *color,
+													const char            *icon_file);
+    int         e_categories_master_list_count     (ECategoriesMasterList *ecml);
+	const char *e_categories_master_list_nth       (ECategoriesMasterList *ecml,
+													int                    n);
+	 */
+	
+	if(category_exists(category) == FALSE){
+		// add if it doesn't exist
+		//categories_add(category, NULL, NULL, TRUE);
+		ECategoriesMasterList *ecml = get_master_list();
+		e_categories_master_list_append(ecml, category, NULL, NULL);
+		e_categories_master_list_commit (ecml);
+	}
+	
+	cal_component_set_categories(comp, category);
+
+
 	cal_component_commit_sequence (comp);
 	
 	free_ToDo(&todo);
@@ -960,6 +1121,8 @@ pre_sync (GnomePilotConduit *conduit,
 	unpack_ToDoAppInfo (&(ctxt->ai), buf, len);
 	g_free (buf);
 
+	lastDesktopUniqueID = 128;
+
 	check_for_slow_setting (conduit, ctxt);
 	if (ctxt->cfg->sync_type == GnomePilotConduitSyncTypeCopyToPilot
 	    || ctxt->cfg->sync_type == GnomePilotConduitSyncTypeCopyFromPilot)
@@ -975,6 +1138,25 @@ post_sync (GnomePilotConduit *conduit,
 {
 	GList *changed;
 	gchar *filename, *change_id;
+	unsigned char *buf;
+	int dlpRetVal, len;
+
+	buf = (unsigned char*)g_malloc (0xffff);
+	
+	len = pack_ToDoAppInfo (&(ctxt->ai), buf, 0xffff);
+	
+	dlpRetVal = dlp_WriteAppBlock (dbi->pilot_socket, dbi->db_handle, 
+			      (unsigned char *)buf, len);
+	
+	g_free (buf);
+			      
+	if (dlpRetVal < 0) {
+		WARN (_("Could not write pilot's ToDo application block"));
+		WARN ("dlp_WriteAppBlock(...) = %d", dlpRetVal);
+		gnome_pilot_conduit_error (conduit,
+					   _("Could not write pilot's ToDo application block"));
+		return -1;
+	}
 
 	LOG (g_message ( "post_sync: ToDo Conduit v.%s", CONDUIT_VERSION ));
 
@@ -1174,7 +1356,7 @@ add_record (GnomePilotConduitSyncAbs *co
 
 	LOG (g_message ( "add_record: adding %s to desktop\n", print_remote (remote) ));
 
-	comp = comp_from_remote_record (conduit, remote, ctxt->default_comp, ctxt->timezone);
+	comp = comp_from_remote_record (conduit, remote, ctxt->default_comp, ctxt->timezone, &(ctxt->ai));
 
 	/* Give it a new UID otherwise it will be the uid of the default comp */
 	uid = cal_component_gen_uid ();
@@ -1202,7 +1384,7 @@ replace_record (GnomePilotConduitSyncAbs
 	LOG (g_message ("replace_record: replace %s with %s\n",
 			print_local (local), print_remote (remote)));
 
-	new_comp = comp_from_remote_record (conduit, remote, local->comp, ctxt->timezone);
+	new_comp = comp_from_remote_record (conduit, remote, local->comp, ctxt->timezone, &(ctxt->ai));
 	g_object_unref (local->comp);
 	local->comp = new_comp;
 	update_comp (conduit, local->comp, ctxt);
