Index: contacts/googledataresource.cpp
===================================================================
--- contacts/googledataresource.cpp	(revision 1066667)
+++ contacts/googledataresource.cpp	(working copy)
@@ -124,6 +124,26 @@
 	return result;
 }
 
+KABC::PhoneNumber::Type googleLabelToAkonadiType(char *label) {
+	if (!strcmp(label,"home"))
+		return KABC::PhoneNumber::Home;
+	if (!strcmp(label,"work"))
+		return KABC::PhoneNumber::Work;
+	if (!strcmp(label,"mobile"))
+		return KABC::PhoneNumber::Cell;
+	if (!strcmp(label,"car"))
+		return KABC::PhoneNumber::Car;
+	if (!strcmp(label,"isdn"))
+		return KABC::PhoneNumber::Isdn;
+	if (!strcmp(label,"pager"))
+		return KABC::PhoneNumber::Pager;
+	if (!strcmp(label,"home_fax"))
+		return KABC::PhoneNumber::Home | KABC::PhoneNumber::Fax;
+	if (!strcmp(label,"work_fax"))
+		return KABC::PhoneNumber::Work | KABC::PhoneNumber::Fax;
+	return KABC::PhoneNumber::Home | KABC::PhoneNumber::Work; // other
+}
+
 void GoogleContactsResource::retrieveItems( const Akonadi::Collection &collection )
 {
 	Q_UNUSED( collection );
@@ -175,32 +195,62 @@
 		contact = gcal_contact_element(&all_contacts, i);
 
 		KABC::Addressee addressee;
-		KABC::PhoneNumber number;
 		KABC::Address address;
 		KABC::Picture photo;
 		QImage image;
 		QString temp;
+		int num_elem;
+		int pref;
+		int j;
+		char **values;
+		char **types;
 
 		/* name */
 		temp = QString::fromUtf8(gcal_contact_get_title(contact));
 		addressee.setNameFromString(temp);
 		/* email */
-		temp = QString::fromUtf8(gcal_contact_get_email(contact));
-		addressee.insertEmail(temp, true);
+		num_elem = gcal_contact_get_emails_nr(contact);
+		pref = gcal_contact_get_pref_email(contact);
+		values = gcal_contact_get_emails_field(contact);
+		types = gcal_contact_get_emails_type(contact);
+		for (j = 0; j < num_elem; j++) {
+			temp = QString::fromUtf8(values[j]);
+			addressee.insertEmail(temp, (j == pref));
+			temp = QString::fromUtf8(types[j]);
+			addressee.insertCustom(QString::fromUtf8("Google"), QString::fromUtf8("typeof_email_").append(QString::number(j)), temp);
+		}
 		/* address */
 		temp = QString::fromUtf8(gcal_contact_get_address(contact));
 		address.setExtended(temp);
 		addressee.insertAddress(address);
 		/* telephone */
-		temp = QString::fromUtf8(gcal_contact_get_phone(contact));
-		number.setNumber(temp);
-		addressee.insertPhoneNumber(number);
+		num_elem = gcal_contact_get_phone_numbers_nr(contact);
+		values = gcal_contact_get_phone_numbers_field(contact);
+		types = gcal_contact_get_phone_numbers_type(contact);
+
+		for (j = 0; j < num_elem; j++) {
+			KABC::PhoneNumber number;
+			temp = QString::fromUtf8(values[j]);
+			number.setNumber(temp);
+			number.setType(googleLabelToAkonadiType(types[j]));
+			addressee.insertPhoneNumber(number);
+		}
+
 		/* profission */
 		temp = QString::fromUtf8(gcal_contact_get_profission(contact));
 		addressee.setTitle(temp);
 		/* company */
 		temp = QString::fromUtf8(gcal_contact_get_organization(contact));
 		addressee.setOrganization(temp);
+		/* Google group membership */
+		num_elem = gcal_contact_get_groupMembership_nr(contact);
+		values = gcal_contact_get_groupMembership(contact);
+		addressee.insertCustom(QString::fromUtf8("Google"), QString::fromUtf8("groupMembership_nr"), QString::number(num_elem));
+
+		for (j = 0; j < num_elem; j++) {
+			temp = QString::fromUtf8(values[j]);
+			addressee.insertCustom(QString::fromUtf8("Google"), QString::fromUtf8("groupMembership_").append(QString::number(j)), temp);
+		}
 		/* description */
 		temp = QString::fromUtf8(gcal_contact_get_content(contact));
 		addressee.setNote(temp);
@@ -274,6 +324,11 @@
 	QString temp;
 	KABC::Picture photo;
 	QImage image;
+	int num_elem;
+	int pref;
+	int j;
+	char **values;
+	char **types;
 
 	/* Just in case, I'm not sure when this member function is called */
 	pending.clear();
@@ -290,7 +345,8 @@
 
 
 	/* Query is inclusive regarding timestamp */
-	if (all_contacts.length == 1) {
+	/* RFC: don't think so... */
+	if (all_contacts.length == 0) {
 		kError() << "no updates, done!";
 		itemsRetrievedIncremental(pending, deleted);
 		return result;
@@ -303,35 +359,58 @@
 		contact = gcal_contact_element(&all_contacts, i);
 		Item item(QLatin1String("text/directory"));
 		if (!strcmp(timestamp, gcal_contact_get_updated(contact))) {
-			kError() << "This is an old contact... continue.";
+		  kError() << "This is an old contact... continue.";
 			continue;
 		}
 
 		if (!gcal_contact_is_deleted(contact)) {
 			KABC::Addressee addressee;
-			KABC::PhoneNumber number;
 			KABC::Address address;
 			/* name */
 			temp = QString::fromUtf8(gcal_contact_get_title(contact));
 			addressee.setNameFromString(temp);
 			kError() << "index: " << i <<"updated: " << temp;
 			/* email */
-			temp = QString::fromUtf8(gcal_contact_get_email(contact));
-			addressee.insertEmail(temp, true);
+			num_elem = gcal_contact_get_emails_nr(contact);
+			pref = gcal_contact_get_pref_email(contact);
+			values = gcal_contact_get_emails_field(contact);
+			types = gcal_contact_get_emails_type(contact);
+			for (j = 0; j < num_elem; j++) {
+				temp = QString::fromUtf8(values[j]);
+				addressee.insertEmail(temp, (j == pref));
+				temp = QString::fromUtf8(types[j]);
+				addressee.insertCustom(QString::fromUtf8("Google"), QString::fromUtf8("typeof_email_").append(QString::number(j)), temp);
+			}
 			/* address */
 			temp = QString::fromUtf8(gcal_contact_get_address(contact));
 			address.setExtended(temp);
 			addressee.insertAddress(address);
 			/* telephone */
-			temp = QString::fromUtf8(gcal_contact_get_phone(contact));
-			number.setNumber(temp);
-			addressee.insertPhoneNumber(number);
+			num_elem = gcal_contact_get_phone_numbers_nr(contact);
+			values = gcal_contact_get_phone_numbers_field(contact);
+			types = gcal_contact_get_phone_numbers_type(contact);
+			for (j = 0; j < num_elem; j++) {
+				KABC::PhoneNumber number;
+				temp = QString::fromUtf8(values[j]);
+				number.setNumber(temp);
+				number.setType(googleLabelToAkonadiType(types[j]));
+				addressee.insertPhoneNumber(number);
+			}
 			/* profission */
 			temp = QString::fromUtf8(gcal_contact_get_profission(contact));
 			addressee.setTitle(temp);
 			/* company */
 			temp = QString::fromUtf8(gcal_contact_get_organization(contact));
 			addressee.setOrganization(temp);
+			/* Google group membership */
+			num_elem = gcal_contact_get_groupMembership_nr(contact);
+			values = gcal_contact_get_groupMembership(contact);
+			addressee.insertCustom(QString::fromUtf8("Google"), QString::fromUtf8("groupMembership_nr"), QString::number(num_elem));
+
+			for (j = 0; j < num_elem; j++) {
+				temp = QString::fromUtf8(values[j]);
+				addressee.insertCustom(QString::fromUtf8("Google"), QString::fromUtf8("groupMembership_").append(QString::number(j)), temp);
+			}
 			/* description */
 			temp = QString::fromUtf8(gcal_contact_get_content(contact));
 			addressee.setNote(temp);
@@ -414,21 +493,73 @@
 	synchronize();
 }
 
+char *akonadiTypeToGoogleLabel(KABC::PhoneNumber::Type type) {
+	switch ( type ) {
+	  case KABC::PhoneNumber::Home:
+	    return strdup("home");
+	    break;
+	  case KABC::PhoneNumber::Work:
+	    return strdup("work");
+	    break;
+	  case KABC::PhoneNumber::Cell:
+	    return strdup("mobile");
+	    break;
+	  case KABC::PhoneNumber::Car:
+	    return strdup("car");
+	    break;
+	  case KABC::PhoneNumber::Isdn:
+	    return strdup("isdn");
+	    break;
+	  case KABC::PhoneNumber::Pager:
+	    return strdup("pager");
+	    break;
+	  case KABC::PhoneNumber::Home + KABC::PhoneNumber::Fax:
+	    return strdup("home_fax");
+	    break;
+	  case KABC::PhoneNumber::Work + KABC::PhoneNumber::Fax:
+	    return strdup("work_fax");
+	    break;
+	  default:
+	    return strdup("other");
+	}
+}
+
+void freeArray(char **ptr_str, int n)
+{
+	int i;
+
+	if (ptr_str) {
+		for (i = 0; i < n; i++)
+			if (ptr_str[i])
+				free(ptr_str[i]);
+		free(ptr_str);
+	}
+}
+
+
 void GoogleContactsResource::itemAdded( const Akonadi::Item &item, const Akonadi::Collection &collection )
 {
 
 	Q_UNUSED(collection);
 
 	KABC::Addressee addressee;
-	KABC::PhoneNumber number;
 	KABC::Address address;
 	gcal_contact_t contact;
 	QString temp;
+	QStringList listEmail;
+	QStringList::iterator email;
 	QByteArray t_byte;
 	QList<KABC::Address> listAddress;
 	QList<KABC::PhoneNumber> listNumber;
+	QList<KABC::PhoneNumber>::iterator number;
 	KABC::Picture photo;
 	int result;
+	int num_elem;
+	int pref = 0;
+	int j;
+	bool ok;
+	char **values;
+	char **types;
 
 	if (!authenticated)
 		configure(0);
@@ -453,9 +584,34 @@
 	t_byte = temp.toUtf8();
 	gcal_contact_set_title(contact, t_byte.data());
 
-	temp = addressee.preferredEmail();
-	t_byte = temp.toUtf8();
-	gcal_contact_set_email(contact, t_byte.data());
+	listEmail = addressee.emails();
+	if (!listEmail.empty()) {
+		values = (char**) malloc(listEmail.size() * sizeof(char*));
+		types = (char**) malloc(listEmail.size() * sizeof(char*));
+		num_elem = 0;
+		for (email = listEmail.begin(); email != listEmail.end(); email++) {
+			temp = *email;
+			if (temp.length()) {
+				t_byte = temp.toUtf8();
+				values[num_elem] = strdup(t_byte.data());
+				if (temp == addressee.preferredEmail())
+					pref = num_elem;
+				temp = addressee.custom(QString::fromUtf8("Google"), QString::fromUtf8("typeof_email_").append(QString::number(num_elem)));
+				if (temp.length()) {
+					t_byte = temp.toUtf8();
+					types[num_elem] = strdup(t_byte.data());
+				}
+				else
+					types[num_elem] = strdup("other");
+				num_elem++;
+			}
+		}
+		if (num_elem) 
+			gcal_contact_set_email(contact, num_elem, values, types, pref);
+		
+		freeArray(values, listEmail.size());
+		freeArray(types, listEmail.size());
+	}
 
 	/* Bellow are optional */
 	listAddress = addressee.addresses();
@@ -466,17 +622,27 @@
 			t_byte = temp.toUtf8();
 			gcal_contact_set_address(contact, t_byte.data());
 		}
-
 	}
 
 	listNumber = addressee.phoneNumbers();
 	if (!listNumber.empty()) {
-		number = listNumber.first();
-		temp = number.number();
-		if (temp.length()) {
-			t_byte = temp.toUtf8();
-			gcal_contact_set_phone(contact, t_byte.data());
+		values = (char**) malloc(listNumber.size() * sizeof(char*));
+		types = (char**) malloc(listNumber.size() * sizeof(char*));
+		num_elem = 0;
+		for (number = listNumber.begin(); number != listNumber.end(); number++) {
+			temp = number->number();
+			if (temp.length()) {
+				t_byte = temp.toUtf8();
+				values[num_elem] = strdup(t_byte.data());
+				types[num_elem] = akonadiTypeToGoogleLabel(number->type());
+				num_elem++;
+			}
 		}
+		if (num_elem) 
+			gcal_contact_set_phone(contact, num_elem, values, types);
+		
+		freeArray(values, listNumber.size());
+		freeArray(types, listNumber.size());
 	}
 
 	temp = addressee.title();
@@ -491,6 +657,21 @@
 		gcal_contact_set_organization(contact, t_byte.data());
 	}
 
+	temp = addressee.custom(QString::fromUtf8("Google"), QString::fromUtf8("groupMembership_nr"));
+	num_elem = temp.toInt(&ok);
+	if (ok) {
+		values = (char**) malloc(num_elem * sizeof(char*));
+		for (j = 0; j < num_elem; j++) {
+			temp = addressee.custom(QString::fromUtf8("Google"), QString::fromUtf8("groupMembership_").append(QString::number(j)));
+			if (temp.length()) {
+				t_byte = temp.toUtf8();
+				values[j] = strdup(t_byte.data());
+			}
+		}
+		gcal_contact_set_groupMembership(contact, num_elem, values);
+		freeArray(values, num_elem);
+	}
+
 	temp = addressee.note();
 	if (temp.length()) {
 		t_byte = temp.toUtf8();
@@ -540,15 +721,23 @@
 	Q_UNUSED(parts);
 
 	KABC::Addressee addressee;
-	KABC::PhoneNumber number;
 	KABC::Address address;
 	QList<KABC::Address> listAddress;
+	QStringList listEmail;
+	QStringList::iterator email;
 	QList<KABC::PhoneNumber> listNumber;
+	QList<KABC::PhoneNumber>::iterator number;
 	gcal_contact_t contact;
 	QByteArray t_byte;
 	QString temp;
 	KABC::Picture photo;
 	int result;
+	int num_elem;
+	int j;
+	bool ok;
+	int pref = 0;
+	char **values;
+	char **types;
 
 	if (!authenticated)
 		configure(0);
@@ -580,9 +769,34 @@
 	t_byte = temp.toUtf8();
 	gcal_contact_set_title(contact, t_byte.data());
 
-	temp = addressee.preferredEmail();
-	t_byte = temp.toUtf8();
-	gcal_contact_set_email(contact, t_byte.data());
+	listEmail = addressee.emails();
+	if (!listEmail.empty()) {
+		values = (char**) malloc(listEmail.size() * sizeof(char*));
+		types = (char**) malloc(listEmail.size() * sizeof(char*));
+		num_elem = 0;
+		for (email = listEmail.begin(); email != listEmail.end(); email++) {
+			temp = *email;
+			if (temp.length()) {
+				t_byte = temp.toUtf8();
+				values[num_elem] = strdup(t_byte.data());
+				if (temp == addressee.preferredEmail())
+					pref = num_elem;
+				temp = addressee.custom(QString::fromUtf8("Google"), QString::fromUtf8("typeof_email_").append(QString::number(num_elem)));
+				if (temp.length()) {
+					t_byte = temp.toUtf8();
+					types[num_elem] = strdup(t_byte.data());
+				}
+				else
+					types[num_elem] = strdup("other");
+				num_elem++;
+			}
+		}
+		if (num_elem) 
+			gcal_contact_set_email(contact, num_elem, values, types, pref);
+		
+		freeArray(values, listEmail.size());
+		freeArray(types, listEmail.size());
+	}
 
 	/* Bellow are optional */
 	listAddress = addressee.addresses();
@@ -597,13 +811,23 @@
 
 	listNumber = addressee.phoneNumbers();
 	if (!listNumber.empty()) {
-		number = listNumber.first();
-		temp = number.number();
-		if (temp.length()) {
-			t_byte = temp.toUtf8();
-			gcal_contact_set_phone(contact, t_byte.data());
+		values = (char**) malloc(listNumber.size() * sizeof(char*));
+		types = (char**) malloc(listNumber.size() * sizeof(char*));
+		num_elem = 0;
+		for (number = listNumber.begin(); number != listNumber.end(); number++) {
+			temp = number->number();
+			if (temp.length()) {
+				t_byte = temp.toUtf8();
+				values[num_elem] = strdup(t_byte.data());
+				types[num_elem] = akonadiTypeToGoogleLabel(number->type());
+				num_elem++;
+			}
 		}
+		if (num_elem) 
+			gcal_contact_set_phone(contact, num_elem, values, types);
 
+		freeArray(values, listNumber.size());
+		freeArray(types, listNumber.size());
 	}
 
 	temp = addressee.title();
@@ -618,6 +842,21 @@
 		gcal_contact_set_organization(contact, t_byte.data());
 	}
 
+	temp = addressee.custom(QString::fromUtf8("Google"), QString::fromUtf8("groupMembership_nr"));
+	num_elem = temp.toInt(&ok);
+	if (ok) {
+		values = (char**) malloc(num_elem * sizeof(char*));
+		for (j = 0; j < num_elem; j++) {
+			temp = addressee.custom(QString::fromUtf8("Google"), QString::fromUtf8("groupMembership_").append(QString::number(j)));
+			if (temp.length()) {
+				t_byte = temp.toUtf8();
+				values[j] = strdup(t_byte.data());
+			}
+		}
+		gcal_contact_set_groupMembership(contact, num_elem, values);
+		freeArray(values, num_elem);
+	}
+
 	temp = addressee.note();
 	if (temp.length()) {
 		t_byte = temp.toUtf8();
