Example #1
0
/*
 * Unpack address, building new data inside cache.
 */
static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] ) {
	struct Address addr;
	gchar **addrEnt;
	gint num, k;
	gint cat_id = 0;
	guint unique_id;
	guchar attrib;
	gchar fullName[ FULLNAME_BUFSIZE ];
	gchar bufEMail[ EMAIL_BUFSIZE ];
	ItemPerson *person;
	ItemEMail *email;
	gint *indPhoneLbl;
	gchar *labelEntry;
	GList *node;
	gchar* extID;
	struct AddressAppInfo *ai;
	gchar **firstName = NULL;
	gchar **lastName = NULL;
#if (PILOT_LINK_MAJOR > 11)
	pi_buffer_t *RecordBuffer;
#endif /* PILOT_LINK_0_12 */

	/* Retrieve address */
#if (PILOT_LINK_MAJOR < 12)
	num = unpack_Address( & addr, buf->buf, buf->size );
	if( num > 0 ) {
#else /* PILOT_LINK_0_12 */
	RecordBuffer = pi_buffer_new(buf->size);
	memcpy(RecordBuffer->data, buf->buf, buf->size);
	RecordBuffer->used = buf->size;
	num = unpack_Address( & addr, RecordBuffer, address_v1 );
	pi_buffer_free(RecordBuffer);
	if (num != -1) {
#endif
		gchar *nameConv;

		addrEnt = addr.entry;
		attrib = buf->attrib;
		unique_id = buf->unique_id;
		cat_id = attrib & 0x0F;

		*fullName = *bufEMail = '\0';

		if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
			firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
		}
		if( addrEnt[ IND_LABEL_LASTNAME ] ) {
			lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
		}

		if( name_order == FAMILY_LAST ) {
			g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
				    firstName ? firstName[0] : "",
				    lastName ? lastName[0] : "" );
		}
		else {
			g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
				    lastName ? lastName[0] : "",
				    firstName ? firstName[0] : "" );
		}

		if( firstName ) {
			g_strfreev( firstName );
		}
		if( lastName ) {
			g_strfreev( lastName );
		}

		g_strstrip( fullName );

		nameConv = jpilot_convert_encoding( fullName );
		strncpy2( fullName, nameConv, FULLNAME_BUFSIZE );
		g_free( nameConv );

		person = addritem_create_item_person();
		addritem_person_set_common_name( person, fullName );
		addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
		addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
		addrcache_id_person( pilotFile->addressCache, person );

		extID = g_strdup_printf( "%d", unique_id );
		addritem_person_set_external_id( person, extID );
		g_free( extID );
		extID = NULL;

		/* Pointer to address metadata. */
		ai = & pilotFile->addrInfo;

		/* Add entry for each email address listed under phone labels. */
		indPhoneLbl = addr.phoneLabel;
		for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
			gint ind;

			ind = indPhoneLbl[k];
			/*
			* fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
			* ai->phoneLabels[ind], addrEnt[3+k] );
			*/
			if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
				labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
				if( labelEntry ) {
					strcpy( bufEMail, labelEntry );
					g_strchug( bufEMail );
					g_strchomp( bufEMail );

					email = addritem_create_item_email();
					addritem_email_set_address( email, bufEMail );
					addrcache_id_email( pilotFile->addressCache, email );
					addrcache_person_add_email
						( pilotFile->addressCache, person, email );
				}
			}
		}

		/* Add entry for each custom label */
		node = pilotFile->labelInd;
		while( node ) {
			gint ind;

			ind = GPOINTER_TO_INT( node->data );
			if( ind > -1 ) {
				/*
				* fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
				* addrEnt[ind] );
				*/
				labelEntry = addrEnt[ind];
				if( labelEntry ) {
					gchar *convertBuff;

					strcpy( bufEMail, labelEntry );
					g_strchug( bufEMail );
					g_strchomp( bufEMail );

					email = addritem_create_item_email();
					addritem_email_set_address( email, bufEMail );

					convertBuff = jpilot_convert_encoding( ai->labels[ind] );
					addritem_email_set_remarks( email, convertBuff );
					g_free( convertBuff );

					addrcache_id_email( pilotFile->addressCache, email );
					addrcache_person_add_email
						( pilotFile->addressCache, person, email );
				}
			}

			node = g_list_next( node );
		}

		if( person->listEMail ) {
			if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
				/* Add to specified category */
				addrcache_folder_add_person
					( pilotFile->addressCache, folderInd[cat_id], person );
			}
			else {
				/* Add to root folder */
				addrcache_add_person( pilotFile->addressCache, person );
			}
		}
		else {
			addritem_free_item_person( person );
			person = NULL;
		}
	}
}

/*
 * Free up address list.
 */
static void jpilot_free_addrlist( GList *records ) {
	GList *node;
	buf_rec *br;

	node = records;
	while( node ) {
		br = node->data;
		free( br );
		node->data = NULL;
		node = g_list_next( node );
	}

	/* Free up list */
	g_list_free( records );
}
Example #2
0
/**
 * Build an address list entry and append to list of address items. Name is formatted
 * as "<first-name> <last-name>".
 *
 * \param  cache     Address cache to load.
 * \param  qry Query object to process.
 * \param  dn        DN for entry found on server.
 * \param  listName  List of common names for entry; see notes below.
 * \param  listAddr  List of EMail addresses for entry.
 * \param  listFirst List of first names for entry.
 * \param  listLast  List of last names for entry.
 *
 * \return List of ItemEMail objects.
 *
 * Notes:
 * 1) Each LDAP server entry may have multiple LDAP attributes with the same
 *    name. For example, a single entry for a person may have more than one
 *    common name, email address, etc.
*
 * 2) The DN for the entry is unique for the server.
 */
static GList *ldapqry_build_items_fl(
		AddressCache *cache, LdapQuery *qry, gchar *dn,
		GSList *listName, GSList *listAddr, GSList *listFirst,
		GSList *listLast, GSList *listDisplay, GSList *attributes )
{
	GSList *nodeAddress, *cur;
	gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
	gboolean allocated = FALSE;
	ItemPerson *person;
	ItemEMail *email;
	ItemFolder *folder;
	gchar *picfile = NULL;
	GList *listReturn = NULL;

	folder = ADDRQUERY_FOLDER(qry);
	if( folder == NULL ) return listReturn;
	if( listAddr == NULL ) return listReturn;

	if ( listDisplay ) {
		allocated = FALSE;
		fullName = listDisplay->data;
	}

	/* Find longest first name in list */
	firstName = mgu_slist_longest_entry( listFirst );

	/* Format last name */
	if( listLast ) {
		lastName = listLast->data;
	}

	if ( fullName == NULL ) {
		/* Find longest common name */
		allocated = FALSE;
		fullName = mgu_slist_longest_entry( listName );
		if( fullName == NULL ) {
			/* Format a full name from first and last names */
			if( firstName ) {
				if( lastName ) {
					fullName = g_strdup_printf( "%s %s", firstName, lastName );
				}
				else {
					fullName = g_strdup_printf( "%s", firstName );
				}
			}
			else {
				if( lastName ) {
					fullName = g_strdup_printf( "%s", lastName );
				}
			}
			if( fullName ) {
				g_strstrip( fullName );
				allocated = TRUE;
			}
		}
	}

	/* Add person into folder */		
	person = addritem_create_item_person();
	addritem_person_set_common_name( person, fullName );
	addritem_person_set_first_name( person, firstName );
	addritem_person_set_last_name( person, lastName );
	addritem_person_set_nick_name( person, fullName );
	addrcache_id_person( cache, person );
	addritem_person_set_external_id( person, dn );
	
	for (cur = attributes; cur; cur = cur->next) {
		UserAttribute *attrib = addritem_copy_attribute((UserAttribute *)cur->data);
		if (attrib->name && strcmp(attrib->name, "jpegPhoto")) {
			addritem_person_add_attribute( person, attrib );
		} else {
			if (qry->server && qry->server->control) {
				gchar *dir = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, 
							ADDRBOOK_DIR, G_DIR_SEPARATOR_S, NULL );
				gchar *filename = g_strdup_printf("%s-%s-%s",
					qry->server->control->hostName?qry->server->control->hostName:"nohost", 
					qry->server->control->baseDN?qry->server->control->baseDN:"nobase", 
					dn);
				picfile = g_strdup_printf("%s%s.png", dir, filename);
				addritem_person_set_picture( person, filename );
				rename_force(attrib->value, picfile);
				g_free(filename);
				g_free(picfile);
				g_free(dir);
			}
		}
	}
	
	addrcache_folder_add_person( cache, ADDRQUERY_FOLDER(qry), person );

	qry->entriesRead++;

	/* Add each address item */
	nodeAddress = listAddr;
	while( nodeAddress ) {
		email = addritem_create_item_email();
		addritem_email_set_address( email, nodeAddress->data );
		addrcache_id_email( cache, email );
		addrcache_person_add_email( cache, person, email );
		addritem_person_add_email( person, email );
		/*if (debug_get_mode()) {
			addritem_print_item_email(email, stdout);
		}*/
		listReturn = g_list_append( listReturn, email );
		nodeAddress = g_slist_next( nodeAddress );
	}

	/* Free any allocated memory */
	if( allocated ) {
		g_free( fullName );
	}
	fullName = firstName = lastName = NULL;

	return listReturn;
}
Example #3
0
/*
* Build an address list entry and append to list of address items. Name is formatted
* as "<first-name> <last-name>".
*/
static void syldap_build_items_fl( SyldapServer *ldapServer, GSList *listAddr, GSList *listFirst, GSList *listLast  ) {
	GSList *nodeFirst = listFirst;
	GSList *nodeAddress = listAddr;
	gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
	gint iLen = 0, iLenT = 0;
	ItemPerson *person;
	ItemEMail *email;

	/* Find longest first name in list */
	while( nodeFirst ) {
		if( firstName == NULL ) {
			firstName = nodeFirst->data;
			iLen = strlen( firstName );
		}
		else {
			if( ( iLenT = strlen( nodeFirst->data ) ) > iLen ) {
				firstName = nodeFirst->data;
				iLen = iLenT;
			}
		}
		nodeFirst = g_slist_next( nodeFirst );
	}

	/* Format name */
	if( listLast ) {
		lastName = listLast->data;
	}

	if( firstName ) {
		if( lastName ) {
			fullName = g_strdup_printf( "%s %s", firstName, lastName );
		}
		else {
			fullName = g_strdup_printf( "%s", firstName );
		}
	}
	else {
		if( lastName ) {
			fullName = g_strdup_printf( "%s", lastName );
		}
	}
	if( fullName ) {
		g_strchug( fullName ); g_strchomp( fullName );
	}

	if( nodeAddress ) {
		person = addritem_create_item_person();
		addritem_person_set_common_name( person, fullName );
		addritem_person_set_first_name( person, firstName );
		addritem_person_set_last_name( person, lastName );
		addrcache_id_person( ldapServer->addressCache, person );
		addrcache_add_person( ldapServer->addressCache, person );
	}

	/* Add address item */
	while( nodeAddress ) {
		email = addritem_create_item_email();
		addritem_email_set_address( email, nodeAddress->data );
		addrcache_id_email( ldapServer->addressCache, email );
		addrcache_person_add_email( ldapServer->addressCache, person, email );
		nodeAddress = g_slist_next( nodeAddress );
		ldapServer->entriesRead++;
	}
	g_free( fullName );
	fullName = firstName = lastName = NULL;

}
Example #4
0
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);
}
Example #5
0
/**
 * Build an address list entry and append to list of address items in the
 * address cache. Name is formatted as "<first-name> <last-name>".
 * \param ldifFile LDIF import control object.
 * \param rec      LDIF field object.
 * \param cache    Address cache to be populated with data.
 */
static void ldif_build_items(
		LdifFile *ldifFile, Ldif_ParsedRec *rec, AddressCache *cache )
{
	GSList *nodeFirst;
	GSList *nodeAddress;
	GSList *nodeAttr;
	gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
	gchar *nickName = NULL;
	gint iLen = 0, iLenT = 0;
	ItemPerson *person;
	ItemEMail *email;

	nodeAddress = rec->listAddress;
//	if( nodeAddress == NULL ) return;

	/* Find longest first name in list */
	nodeFirst = rec->listFName;
	while( nodeFirst ) {
		if( firstName == NULL ) {
			firstName = nodeFirst->data;
			iLen = strlen( firstName );
		}
		else {
			if( ( iLenT = strlen( nodeFirst->data ) ) > iLen ) {
				firstName = nodeFirst->data;
				iLen = iLenT;
			}
		}
		nodeFirst = g_slist_next( nodeFirst );
	}

	/* Format name */
	if( rec->listLName ) {
		lastName = rec->listLName->data;
	}

	if( firstName ) {
		if( lastName ) {
			fullName = g_strdup_printf(
				"%s %s", firstName, lastName );
		}
		else {
			fullName = g_strdup_printf( "%s", firstName );
		}
	}
	else {
		if( lastName ) {
			fullName = g_strdup_printf( "%s", lastName );
		}
	}
	
	if (!fullName || strlen(fullName) == 0) {
		g_free(fullName);
		fullName = NULL;
		if (rec->listCName)
			fullName = g_strdup(rec->listCName->data);
	}
	
	if( fullName ) {
		g_strstrip( fullName );
	}

	if( rec->listNName ) {
		nickName = rec->listNName->data;
	}

	person = addritem_create_item_person();
	addritem_person_set_common_name( person, fullName );
	addritem_person_set_first_name( person, firstName );
	addritem_person_set_last_name( person, lastName );
	addritem_person_set_nick_name( person, nickName );
	addrcache_id_person( cache, person );
	addrcache_add_person( cache, person );
	++ldifFile->importCount;

	/* Add address item */
	while( nodeAddress ) {
		email = addritem_create_item_email();
		addritem_email_set_address( email, nodeAddress->data );
		addrcache_id_email( cache, email );
		addrcache_person_add_email( cache, person, email );
		nodeAddress = g_slist_next( nodeAddress );
	}
	g_free( fullName );
	fullName = firstName = lastName = NULL;

	/* Add user attributes */
	nodeAttr = rec->userAttr;
	while( nodeAttr ) {
		Ldif_UserAttr *attr = nodeAttr->data;
		UserAttribute *attrib = addritem_create_attribute();
		addritem_attrib_set_name( attrib, attr->name );
		addritem_attrib_set_value( attrib, attr->value );
		addritem_person_add_attribute( person, attrib );
		nodeAttr = g_slist_next( nodeAttr );
	}
	nodeAttr = NULL;
}
Example #6
0
/**
 * Unpack address, building new data inside cache.
 * \param pilotFile  JPilot control data.
 * \param buf        Record buffer.
 * \param folderInd  Array of (category) folders to load.
 */
static void jpilot_load_address(
		JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] )
{
	struct Address addr;
	gchar **addrEnt;
	gint num, k;
	gint cat_id = 0;
	guint unique_id;
	guchar attrib;
	gchar fullName[ FULLNAME_BUFSIZE ];
	ItemPerson *person;
	gint *indPhoneLbl;
	gchar *labelEntry;
	GList *node;
	gchar* extID;
	struct AddressAppInfo *ai;
	gchar **firstName = NULL;
	gchar **lastName = NULL;

	/* Retrieve address */
	num = unpack_Address( & addr, buf->buf, buf->size );
	if( num > 0 ) {
		addrEnt = addr.entry;
		attrib = buf->attrib;
		unique_id = buf->unique_id;
		cat_id = attrib & 0x0F;

		*fullName = '\0';

		if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
			firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
		}

		if( addrEnt[ IND_LABEL_LASTNAME ] ) {
			lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
		}

		if( name_order == FAMILY_LAST ) {
			g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
				    firstName ? firstName[0] : "",
				    lastName ? lastName[0] : "" );
		}
		else {
			g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
				    lastName ? lastName[0] : "",
				    firstName ? firstName[0] : "" );
		}

		if( firstName ) {
			g_strfreev( firstName );
		}
		if( lastName ) {
			g_strfreev( lastName );
		}

		g_strstrip( fullName );

		if( convert_charcode ) {
			gchar *nameConv;
			nameConv = g_strdup( fullName );
			conv_sjistoeuc( fullName, FULLNAME_BUFSIZE, nameConv );
			g_free( nameConv );
		}

		person = addritem_create_item_person();
		addritem_person_set_common_name( person, fullName );
		addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
		addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
		addrcache_id_person( pilotFile->addressCache, person );

		extID = g_strdup_printf( "%d", unique_id );
		addritem_person_set_external_id( person, extID );
		g_free( extID );
		extID = NULL;

		/* Pointer to address metadata. */
		ai = & pilotFile->addrInfo;

		/* Add entry for each email address listed under phone labels. */
		indPhoneLbl = addr.phoneLabel;
		for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
			gint ind;

			ind = indPhoneLbl[k];
			/*
			* fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
			* ai->phoneLabels[ind], addrEnt[3+k] );
			*/
			if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
				labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
				jpilot_parse_label( pilotFile, labelEntry, person );
			}
		}

		/* Add entry for each custom label */
		node = pilotFile->labelInd;
		while( node ) {
			gchar convertBuff[JPILOT_LEN_LABEL];
			gint ind;

			ind = GPOINTER_TO_INT( node->data );
			if( ind > -1 ) {
				/*
				* fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
				* addrEnt[ind] );
				*/
				labelEntry = addrEnt[ind];
				jpilot_parse_label( pilotFile, labelEntry, person );
			}

			node = g_list_next( node );
		}

		if( person->listEMail ) {
			if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
				/* Add to specified category */
				addrcache_folder_add_person(
					pilotFile->addressCache,
					folderInd[cat_id], person );
			}
			else {
				/* Add to root folder */
				addrcache_add_person(
					pilotFile->addressCache, person );
			}
		}
		else {
			addritem_free_item_person( person );
			person = NULL;
		}
	}

	/* Free up pointer allocated inside address */
	free_Address( & addr );
}