void
couchdb_document_contact_set_urls (CouchdbDocumentContact *document, GSList *list)
{
	GSList *l;
	JsonObject *urls_json;

	g_return_if_fail (COUCHDB_IS_DOCUMENT_CONTACT (document));

	urls_json = json_object_new ();
	for (l = list; l != NULL; l = l->next) {
		const gchar *address_str;
		CouchdbStructField *sf = (CouchdbStructField *) l->data;

		address_str = couchdb_document_contact_url_get_address (sf);
		if (address_str) {
			JsonObject *this_url;

			this_url = json_object_new ();
			json_object_set_string_member (this_url, "address", address_str);
			json_object_set_string_member (this_url, "description",
						       couchdb_document_contact_url_get_description (sf));

			json_object_set_object_member (urls_json,
						       couchdb_struct_field_get_uuid (sf),
						       this_url);
		}
	}

	json_object_set_object_member (couchdb_document_get_json_object (COUCHDB_DOCUMENT (document)),
				       "urls", urls_json);
}
void
couchdb_document_contact_set_phone_numbers (CouchdbDocumentContact *document, GSList *list)
{
	GSList *l;
	JsonObject *phone_numbers;

	g_return_if_fail (COUCHDB_IS_DOCUMENT_CONTACT (document));

	phone_numbers = json_object_new ();
	for (l = list; l != NULL; l = l->next) {
		const gchar *number_str;
		CouchdbStructField *sf = (CouchdbStructField *) l->data;

		number_str = couchdb_document_contact_phone_get_number (sf);
		if (number_str) {
			JsonObject *this_phone;

			this_phone = json_object_new ();
			json_object_set_string_member (this_phone, "number", number_str);
			json_object_set_string_member (this_phone, "description",
						       couchdb_document_contact_phone_get_description (sf));
			json_object_set_int_member (this_phone, "priority",
						    couchdb_document_contact_phone_get_priority (sf));

			json_object_set_object_member (phone_numbers,
						       couchdb_struct_field_get_uuid (sf),
						       this_phone);
		}
	}

	json_object_set_object_member (couchdb_document_get_json_object (COUCHDB_DOCUMENT (document)),
				       "phone_numbers", phone_numbers);
}
void
desktopcouch_document_contact_set_im_addresses (CouchdbDocument *document, GSList *list)
{
	GSList *l;
	JsonObject *im_addresses_json;

	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));

	im_addresses_json = json_object_new ();
	for (l = list; l != NULL; l = l->next) {
		const gchar *address_str;
		CouchdbStructField *sf = (CouchdbStructField *) l->data;

		address_str = desktopcouch_document_contact_im_get_address (sf);
		if (address_str != NULL) {
			JsonObject *this_address;

			this_address = json_object_new ();
			json_object_set_string_member (this_address, "address", address_str);
			json_object_set_string_member (this_address, "description",
						       desktopcouch_document_contact_im_get_description (sf));
			json_object_set_string_member (this_address, "protocol",
						       desktopcouch_document_contact_im_get_protocol (sf));

			json_object_set_object_member (im_addresses_json,
						       couchdb_struct_field_get_uuid (sf),
						       this_address);
		}
	}

	json_object_set_object_member (couchdb_document_get_json_object (document), "im_addresses", im_addresses_json);
}
/**
 * couchdb_document_contact_get_urls:
 * @document: A #CouchdbDocumentContact object
 *
 * Get the list of URLs for this contact document.
 *
 * Returns: (element-type Couchdb.StructField) (transfer full): A #GSList of
 * #CouchdbStructField's representing all the URLs stored for this
 * contact.
 */
GSList *
couchdb_document_contact_get_urls (CouchdbDocumentContact *document)
{
	GSList *list = NULL;
	JsonObject *urls;

	g_return_val_if_fail (COUCHDB_IS_DOCUMENT_CONTACT (document), NULL);
	g_return_val_if_fail (couchdb_document_is_contact (COUCHDB_DOCUMENT (document)), NULL);

	if (json_object_has_member (couchdb_document_get_json_object (COUCHDB_DOCUMENT (document)),
				    "urls")) {
		urls = json_object_get_object_member (
			couchdb_document_get_json_object (COUCHDB_DOCUMENT (document)), "urls");
		if (urls) {
			json_object_foreach_member (urls,
						    (JsonObjectForeach) foreach_object_cb,
						    &list);
		}
	}

	return list;
}
/**
 * couchdb_document_contact_get_email_addresses:
 * @document: A #CouchdbDocumentContact object representing a contact
 *
 * Retrieve a list of email addresses from the specified contact document.
 * Email addresses are returned in a GSList of #CouchdbStructField objects,
 * which can be manipulated with the couchdb_document_contact_email_* functions
 * and freed with:
 *     g_slist_foreach (list, (GFunc) couchdb_struct_field_unref, NULL);
 *     g_slist_free (list);
 *
 * Returns: (element-type Couchdb.StructField) (transfer full): a #GSList of #CouchdbStructField objects.
 */
GSList *
couchdb_document_contact_get_email_addresses (CouchdbDocumentContact *document)
{
	GSList *list = NULL;
	JsonObject *addresses_json;

	g_return_val_if_fail (COUCHDB_IS_DOCUMENT_CONTACT (document), NULL);
	g_return_val_if_fail (couchdb_document_is_contact (COUCHDB_DOCUMENT (document)), NULL);

	if (json_object_has_member (couchdb_document_get_json_object (COUCHDB_DOCUMENT (document)),
				    "email_addresses")) {
		addresses_json = json_object_get_object_member (
			couchdb_document_get_json_object (COUCHDB_DOCUMENT (document)), "email_addresses");;
		if (addresses_json) {
			json_object_foreach_member (addresses_json,
						    (JsonObjectForeach) foreach_object_cb,
						    &list);
		}
	}

	return list;
}
void
couchdb_document_contact_set_addresses (CouchdbDocumentContact *document, GSList *list)
{
	GSList *l;
	JsonObject *addresses;

	g_return_if_fail (COUCHDB_IS_DOCUMENT_CONTACT (document));

	addresses = json_object_new ();
	for (l = list; l != NULL; l = l->next) {
		const gchar *street_str;
		CouchdbStructField *sf = (CouchdbStructField *) l->data;

		street_str = couchdb_document_contact_address_get_street (sf);
		if (street_str) {
			JsonObject *this_address;

			this_address = json_object_new ();

			json_object_set_string_member (this_address, "address1", street_str);
			json_object_set_string_member (this_address, "address2",
						       couchdb_document_contact_address_get_ext_street (sf));
			json_object_set_string_member (this_address, "city",
						       couchdb_document_contact_address_get_city (sf));
			json_object_set_string_member (this_address, "state",
						       couchdb_document_contact_address_get_state (sf));
			json_object_set_string_member (this_address, "country",
						       couchdb_document_contact_address_get_country (sf));
			json_object_set_string_member (this_address, "postalcode",
						       couchdb_document_contact_address_get_postalcode (sf));
			json_object_set_string_member (this_address, "pobox",
						       couchdb_document_contact_address_get_pobox (sf));
			json_object_set_string_member (this_address, "description",
						       couchdb_document_contact_address_get_description (sf));

			json_object_set_object_member (addresses,
						       couchdb_struct_field_get_uuid (sf),
						       this_address);
		}
	}

	json_object_set_object_member (couchdb_document_get_json_object (COUCHDB_DOCUMENT (document)),
				       "addresses", addresses);
}
GSList *
desktopcouch_document_contact_get_im_addresses (CouchdbDocument *document)
{
	GSList *list = NULL;
	JsonObject *im_addresses;

	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
	g_return_val_if_fail (desktopcouch_document_is_contact (document), NULL);

	im_addresses = json_object_get_object_member (
		couchdb_document_get_json_object (document), "im_addresses");
	if (im_addresses != NULL) {
		json_object_foreach_member (im_addresses,
					    (JsonObjectForeach) foreach_object_cb,
					    &list);
	}

	return list;
}
GSList *
desktopcouch_document_contact_get_phone_numbers (CouchdbDocument *document)
{
	GSList *list = NULL;
	JsonObject *phone_numbers;

	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
	g_return_val_if_fail (desktopcouch_document_is_contact (document), NULL);

	phone_numbers = json_object_get_object_member (
		couchdb_document_get_json_object (document), "phone_numbers");
	if (phone_numbers) {
		json_object_foreach_member (phone_numbers,
					    (JsonObjectForeach) foreach_object_cb,
					    &list);
	}

	return list;
}