Beispiel #1
0
/*
* Remove email address in address cache for specified person.
* param: person	Person.
*        email	EMail to remove.
* return: EMail object, or NULL if not found. Note that object should still be freed.
*/
ItemEMail *addrcache_person_remove_email( AddressCache *cache, ItemPerson *person, ItemEMail *email ) {
	ItemEMail *found = NULL;

	cm_return_val_if_fail( cache != NULL, NULL );

	if( person && email ) {
		found = addritem_person_remove_email( person, email );
		if( found ) {
			/* Remove email from all groups. */
			g_hash_table_foreach( cache->itemHash, addrcache_allgrp_rem_email_vis, email );

			/* Remove email from person's address list */
			if( person->listEMail ) {
				person->listEMail = g_list_remove( person->listEMail, email );
			}
			/* Unlink reference to person. */
			ADDRITEM_PARENT(email) = NULL;
			cache->dirtyFlag = TRUE;
		}
	}
	return found;
}
static void update_ItemPerson_from_vcard(AddressBookFile *abf,
																				 ItemPerson *item, gchar *vcard)
{
	VFormat *vformat;
	GList *attr_list, *walk;
	gint numEmail;
	GList *emailWalk;
	GList *savedMailList;

	numEmail = 0;

	vformat = vformat_new_from_string(vcard);

	/* steal email list */
	savedMailList = NULL;
	for (emailWalk = item->listEMail; emailWalk; emailWalk = emailWalk->next) {
		ItemEMail *itemEMail;
		itemEMail = emailWalk->data;
		itemEMail = addritem_person_remove_email(item, itemEMail);
		if (itemEMail)
			savedMailList = g_list_append(savedMailList, itemEMail);
	}

	attr_list = vformat_get_attributes(vformat);
	for (walk = attr_list; walk; walk = walk->next) {
		VFormatAttribute *attr;
		const char *attr_name;

		attr = walk->data;
		attr_name = vformat_attribute_get_name(attr);

		/* We won't be treating the UID here. */

		/* Name */
		if (!strcmp(attr_name, "N")) {
			const gchar *first_name;
			const gchar *last_name;
			gchar *display_name;

			last_name = vformat_attribute_get_nth_value(attr,0);
			first_name = vformat_attribute_get_nth_value(attr, 1);

			if(last_name)
				addritem_person_set_last_name(item, last_name);
			if(first_name)
				addritem_person_set_first_name(item, first_name);

			if(last_name || first_name) {
				display_name = g_strdup_printf("%s %s",
																			 first_name?first_name:"",
																			 last_name?last_name:"");
				addritem_person_set_common_name(item, display_name);
				g_free(display_name);
			}
		}

		/* display name */
		else if (!strcmp(attr_name, "FN")) {
			const gchar *display_name;

			display_name = vformat_attribute_get_nth_value(attr,0);
			addritem_person_set_common_name(item, display_name?display_name:"");
		}

		/* Internet EMail addresses */
		else if (!strcmp(attr_name, "EMAIL")) {
			if (!vformat_attribute_is_single_valued(attr))
				g_print("Error: EMAIL is supposed to be single valued\n");
			else {
				GList *paramList, *paramWalk;
				numEmail++;
				paramList = vformat_attribute_get_params(attr);
				if (!paramList) {
					/* INTERNET is default */
					const gchar *email;
					email = vformat_attribute_get_nth_value(attr, 0);
					savedMailList = restore_or_add_email_address(abf, item,
																											 savedMailList,email);
				}
				else {
					for (paramWalk = paramList; paramWalk; paramWalk = paramWalk->next) {
						VFormatParam *param;
						param = paramWalk->data;
						if (param && param->name && param->values && param->values->data &&
								!strcmp(param->name, "TYPE") &&
								(!strcmp((char*)param->values->data, "INTERNET") ||
								 /* Evolution may also put HOME or WORK, though I don't
									* think this is legal. */
								 !strcmp((char*)param->values->data, "HOME") ||
								 !strcmp((char*)param->values->data, "WORK"))) {
							const gchar *email;
							email = vformat_attribute_get_nth_value(attr, 0);
							savedMailList = restore_or_add_email_address(abf, item,
																													 savedMailList,
																													 email);
						}
					}
				}
			}
		} /* INTERNET Email addresses */

	} /* for all attributes */

	/* if no mails were included, keep the old email list */
	if(numEmail > 0) {
		while(savedMailList) {
			ItemEMail *emailItem;
			emailItem = savedMailList->data;
			savedMailList = restore_or_add_email_address(abf, item, savedMailList,
																									 emailItem->address);
		}
	}

	/* savedMailList now contains the left-overs. Free it.
		 (if the sync went well, those are deleted entries) */
	for (emailWalk = savedMailList; emailWalk; emailWalk = emailWalk->next) {
		ItemEMail *itemEMail;
		itemEMail = emailWalk->data;
		addritem_free_item_email(itemEMail);
	}
	g_list_free(savedMailList);

	item->status = UPDATE_ENTRY;
	addrbook_set_dirty(abf,TRUE);
}