gmime r1411 - in trunk: . docs/reference gmime tests



Author: fejj
Date: Mon Aug 25 02:13:46 2008
New Revision: 1411
URL: http://svn.gnome.org/viewvc/gmime?rev=1411&view=rev

Log:
2008-08-24  Jeffrey Stedfast  <fejj novell com>

	* tests/test-headers.c (test_header_sync): Check that changes to
	the GMimeContentType, GMimeDisposition, and InternetAddressList
	objects cause a re-sync of the GMimeObject/GMimeMessage headers.

	* gmime/internet-address.c (internet_address_list_add): Connect to
	the added address's changed signal. Exposed by my new test-suite
	code.
	(internet_address_list_insert): Assert index >= 0, not < 0.
	(internet_address_list_remove_at): Same.
	(internet_address_list_get_address): Same.
	(internet_address_list_set_address): Same.
	(internet_address_list_to_string): Return NULL if the list is
	empty. This makes it easier to keep the new behavior of keeping
	around InternetAddressList objects in GMimeMessage the same as the
	old behavior when all of the addresses have been removed.

	* gmime/gmime-disposition.c (g_mime_content_disposition_set_params):
	New function.

	* gmime/gmime-content-type.c (g_mime_content_type_set_media_type):
	New function.
	(g_mime_content_type_set_media_subtype): Another new function.
	(g_mime_content_type_set_params): New function.



Modified:
   trunk/ChangeLog
   trunk/docs/reference/gmime-sections.txt
   trunk/gmime/gmime-content-type.c
   trunk/gmime/gmime-content-type.h
   trunk/gmime/gmime-disposition.c
   trunk/gmime/gmime-disposition.h
   trunk/gmime/gmime-message.c
   trunk/gmime/internet-address.c
   trunk/tests/test-headers.c

Modified: trunk/docs/reference/gmime-sections.txt
==============================================================================
--- trunk/docs/reference/gmime-sections.txt	(original)
+++ trunk/docs/reference/gmime-sections.txt	Mon Aug 25 02:13:46 2008
@@ -615,10 +615,13 @@
 g_mime_content_type_to_string
 g_mime_content_type_is_type
 g_mime_content_type_get_media_type
+g_mime_content_type_set_media_type
 g_mime_content_type_get_media_subtype
+g_mime_content_type_set_media_subtype
 g_mime_content_type_get_params
-g_mime_content_type_set_parameter
+g_mime_content_type_set_params
 g_mime_content_type_get_parameter
+g_mime_content_type_set_parameter
 </SECTION>
 
 <SECTION>
@@ -632,6 +635,7 @@
 g_mime_content_disposition_set_disposition
 g_mime_content_disposition_get_disposition
 g_mime_content_disposition_get_params
+g_mime_content_disposition_set_params
 g_mime_content_disposition_set_parameter
 g_mime_content_disposition_get_parameter
 g_mime_content_disposition_to_string

Modified: trunk/gmime/gmime-content-type.c
==============================================================================
--- trunk/gmime/gmime-content-type.c	(original)
+++ trunk/gmime/gmime-content-type.c	Mon Aug 25 02:13:46 2008
@@ -258,6 +258,30 @@
 
 
 /**
+ * g_mime_content_type_set_media_type:
+ * @mime_type: MIME Content-Type
+ * @type: media type
+ *
+ * Sets the Content-Type's media type.
+ **/
+void
+g_mime_content_type_set_media_type (GMimeContentType *mime_type, const char *type)
+{
+	char *buf;
+	
+	g_return_if_fail (mime_type != NULL);
+	g_return_if_fail (type != NULL);
+	
+	buf = g_strdup (type);
+	g_free (mime_type->type);
+	mime_type->type = buf;
+	
+	if (mime_type->parent_object)
+		_g_mime_object_content_type_changed (mime_type->parent_object);
+}
+
+
+/**
  * g_mime_content_type_get_media_type:
  * @mime_type: MIME Content-Type
  *
@@ -275,6 +299,30 @@
 
 
 /**
+ * g_mime_content_type_set_media_subtype:
+ * @mime_type: MIME Content-Type
+ * @subtype: media subtype
+ *
+ * Sets the Content-Type's media subtype.
+ **/
+void
+g_mime_content_type_set_media_subtype (GMimeContentType *mime_type, const char *subtype)
+{
+	char *buf;
+	
+	g_return_if_fail (mime_type != NULL);
+	g_return_if_fail (subtype != NULL);
+	
+	buf = g_strdup (subtype);
+	g_free (mime_type->subtype);
+	mime_type->subtype = buf;
+	
+	if (mime_type->parent_object)
+		_g_mime_object_content_type_changed (mime_type->parent_object);
+}
+
+
+/**
  * g_mime_content_type_get_media_subtype:
  * @mime_type: MIME Content-Type
  *
@@ -292,6 +340,39 @@
 
 
 /**
+ * g_mime_content_type_set_params:
+ * @mime_type: MIME Content-Type
+ * @params: a list of #GMimeParam objects
+ *
+ * Sets the Content-Type's parameter list.
+ **/
+void
+g_mime_content_type_set_params (GMimeContentType *mime_type, GMimeParam *params)
+{
+	g_return_if_fail (mime_type != NULL);
+	
+	/* destroy the current list/hash */
+	if (mime_type->param_hash)
+		g_hash_table_destroy (mime_type->param_hash);
+	
+	g_mime_param_destroy (mime_type->params);
+	mime_type->params = params;
+	
+	if (params != NULL) {
+		mime_type->param_hash = g_hash_table_new (g_mime_strcase_hash, g_mime_strcase_equal);
+		while (params != NULL) {
+			g_hash_table_insert (mime_type->param_hash, params->name, params);
+			params = params->next;
+		}
+	} else {
+		mime_type->param_hash = NULL;
+	}
+	
+	if (mime_type->parent_object)
+		_g_mime_object_content_type_changed (mime_type->parent_object);
+}
+
+/**
  * g_mime_content_type_get_params:
  * @mime_type: MIME Content-Type
  *

Modified: trunk/gmime/gmime-content-type.h
==============================================================================
--- trunk/gmime/gmime-content-type.h	(original)
+++ trunk/gmime/gmime-content-type.h	Mon Aug 25 02:13:46 2008
@@ -59,14 +59,18 @@
 
 gboolean g_mime_content_type_is_type (const GMimeContentType *mime_type, const char *type, const char *subtype);
 
+void g_mime_content_type_set_media_type (GMimeContentType *mime_type, const char *type);
 const char *g_mime_content_type_get_media_type (const GMimeContentType *mime_type);
+
+void g_mime_content_type_set_media_subtype (GMimeContentType *mime_type, const char *subtype);
 const char *g_mime_content_type_get_media_subtype (const GMimeContentType *mime_type);
+
+void g_mime_content_type_set_params (GMimeContentType *mime_type, GMimeParam *params);
 const GMimeParam *g_mime_content_type_get_params (const GMimeContentType *mime_type);
 
 void g_mime_content_type_set_parameter (GMimeContentType *mime_type, const char *attribute, const char *value);
 const char *g_mime_content_type_get_parameter (const GMimeContentType *mime_type, const char *attribute);
 
-
 G_END_DECLS
 
-#endif /* __GMIME_PART_H__ */
+#endif /* __GMIME_CONTENT_TYPE_H__ */

Modified: trunk/gmime/gmime-disposition.c
==============================================================================
--- trunk/gmime/gmime-disposition.c	(original)
+++ trunk/gmime/gmime-disposition.c	Mon Aug 25 02:13:46 2008
@@ -178,6 +178,40 @@
 
 
 /**
+ * g_mime_content_disposition_set_params:
+ * @disposition: a #GMimeContentDisposition object
+ * @params: a list of #GMimeParam objects
+ *
+ * Sets the Content-Disposition's parameter list.
+ **/
+void
+g_mime_content_disposition_set_params (GMimeContentDisposition *disposition, GMimeParam *params)
+{
+	g_return_if_fail (disposition != NULL);
+	
+	/* destroy the current list/hash */
+	if (disposition->param_hash)
+		g_hash_table_destroy (disposition->param_hash);
+	
+	g_mime_param_destroy (disposition->params);
+	disposition->params = params;
+	
+	if (params != NULL) {
+		disposition->param_hash = g_hash_table_new (g_mime_strcase_hash, g_mime_strcase_equal);
+		while (params != NULL) {
+			g_hash_table_insert (disposition->param_hash, params->name, params);
+			params = params->next;
+		}
+	} else {
+		disposition->param_hash = NULL;
+	}
+	
+	if (disposition->parent_object)
+		_g_mime_object_content_disposition_changed (disposition->parent_object);
+}
+
+
+/**
  * g_mime_content_disposition_get_params:
  * @disposition: a #GMimeContentDisposition object
  *

Modified: trunk/gmime/gmime-disposition.h
==============================================================================
--- trunk/gmime/gmime-disposition.h	(original)
+++ trunk/gmime/gmime-disposition.h	Mon Aug 25 02:13:46 2008
@@ -73,6 +73,7 @@
 void g_mime_content_disposition_set_disposition (GMimeContentDisposition *disposition, const char *value);
 const char *g_mime_content_disposition_get_disposition (const GMimeContentDisposition *disposition);
 
+void g_mime_content_disposition_set_params (GMimeContentDisposition *disposition, GMimeParam *params);
 const GMimeParam *g_mime_content_disposition_get_params (const GMimeContentDisposition *disposition);
 
 void g_mime_content_disposition_set_parameter (GMimeContentDisposition *disposition,

Modified: trunk/gmime/gmime-message.c
==============================================================================
--- trunk/gmime/gmime-message.c	(original)
+++ trunk/gmime/gmime-message.c	Mon Aug 25 02:13:46 2008
@@ -1110,17 +1110,20 @@
 static void
 sync_recipient_header (GMimeMessage *message, GMimeRecipientType type)
 {
+	GMimeObject *object = (GMimeObject *) message;
 	const char *name = recipient_types[type].name;
-	InternetAddressList *recipients;
+	InternetAddressList *list;
 	char *string;
 	
 	/* sync the specified recipient header */
-	if ((recipients = g_mime_message_get_recipients (message, type))) {
-		string = internet_address_list_to_string (recipients, TRUE);
-		g_mime_header_list_set (GMIME_OBJECT (message)->headers, name, string);
+	if ((list = g_hash_table_lookup (message->recipients, recipient_types[type].name))) {
+		string = internet_address_list_to_string (list, TRUE);
+		g_mime_header_list_set (object->headers, name, string);
 		g_free (string);
-	} else
-		g_mime_header_list_set (GMIME_OBJECT (message)->headers, name, NULL);
+	} else {
+		/* list should never be NULL... */
+		g_mime_header_list_set (object->headers, name, NULL);
+	}
 }
 
 static void

Modified: trunk/gmime/internet-address.c
==============================================================================
--- trunk/gmime/internet-address.c	(original)
+++ trunk/gmime/internet-address.c	Mon Aug 25 02:13:46 2008
@@ -158,9 +158,6 @@
 	
 	g_return_if_fail (IS_INTERNET_ADDRESS (ia));
 	
-	if (ia->name == name)
-		return;
-	
 	if (name) {
 		buf = g_mime_utils_header_decode_phrase (name);
 		g_mime_utils_unquote_string (buf);
@@ -696,6 +693,8 @@
 	g_return_val_if_fail (IS_INTERNET_ADDRESS_LIST (list), -1);
 	g_return_val_if_fail (IS_INTERNET_ADDRESS (ia), -1);
 	
+	g_signal_connect (ia, "changed", G_CALLBACK (address_changed), list);
+	
 	index = list->array->len;
 	g_ptr_array_add (list->array, ia);
 	g_object_ref (ia);
@@ -750,7 +749,7 @@
 	
 	g_return_if_fail (IS_INTERNET_ADDRESS_LIST (list));
 	g_return_if_fail (IS_INTERNET_ADDRESS (ia));
-	g_return_if_fail (index < 0);
+	g_return_if_fail (index >= 0);
 	
 	g_signal_connect (ia, "changed", G_CALLBACK (address_changed), list);
 	g_object_ref (ia);
@@ -817,7 +816,7 @@
 	InternetAddress *ia;
 	
 	g_return_val_if_fail (IS_INTERNET_ADDRESS_LIST (list), FALSE);
-	g_return_val_if_fail (index < 0, FALSE);
+	g_return_val_if_fail (index >= 0, FALSE);
 	
 	if (index >= list->array->len)
 		return FALSE;
@@ -899,7 +898,7 @@
 	InternetAddress *ia;
 	
 	g_return_val_if_fail (IS_INTERNET_ADDRESS_LIST (list), NULL);
-	g_return_val_if_fail (index < 0, NULL);
+	g_return_val_if_fail (index >= 0, NULL);
 	
 	if ((guint) index >= list->array->len)
 		return NULL;
@@ -926,7 +925,7 @@
 	
 	g_return_if_fail (IS_INTERNET_ADDRESS_LIST (list));
 	g_return_if_fail (IS_INTERNET_ADDRESS (ia));
-	g_return_if_fail (index < 0);
+	g_return_if_fail (index >= 0);
 	
 	if ((guint) index > list->array->len)
 		return;
@@ -1146,7 +1145,8 @@
  * Allocates a string buffer containing the rfc822 formatted addresses
  * in @list.
  *
- * Returns: a string containing the list of addresses in rfc822 format.
+ * Returns: a string containing the list of addresses in rfc822 format
+ * or %NULL if no addresses are contained in the list.
  **/
 char *
 internet_address_list_to_string (InternetAddressList *list, gboolean encode)
@@ -1156,6 +1156,11 @@
 	GString *string;
 	char *str;
 	
+	g_return_val_if_fail (IS_INTERNET_ADDRESS_LIST (list), NULL);
+	
+	if (list->array->len == 0)
+		return NULL;
+	
 	string = g_string_new ("");
 	_internet_address_list_to_string (list, flags, &linelen, string);
 	str = string->str;
@@ -1180,6 +1185,9 @@
 	guint32 flags = INTERNET_ADDRESS_ENCODE | INTERNET_ADDRESS_FOLD;
 	size_t linelen = str->len;
 	
+	g_return_if_fail (IS_INTERNET_ADDRESS_LIST (list));
+	g_return_if_fail (str != NULL);
+	
 	_internet_address_list_to_string (list, flags, &linelen, str);
 }
 

Modified: trunk/tests/test-headers.c
==============================================================================
--- trunk/tests/test-headers.c	(original)
+++ trunk/tests/test-headers.c	Mon Aug 25 02:13:46 2008
@@ -365,6 +365,181 @@
 	g_mime_header_list_destroy (list);
 }
 
+static void
+test_header_sync (void)
+{
+	InternetAddressList *members, *list;
+	InternetAddress *addr, *ia;
+	GMimeMessage *message;
+	GMimeObject *object;
+	const char *value;
+	GMimePart *part;
+	int i;
+	
+	part = g_mime_part_new_with_type ("application", "octet-stream");
+	object = (GMimeObject *) part;
+	
+	testsuite_check ("content-type synchronization");
+	try {
+		/* first, check that the current Content-Type header
+		 * value is "application/octet-stream" as expected */
+		if (!(value = g_mime_object_get_header (object, "Content-Type")))
+			throw (exception_new ("initial content-type header was unexpectedly null"));
+		
+		if (strcmp ("application/octet-stream", value) != 0)
+			throw (exception_new ("initial content-type header had unexpected value"));
+		
+		/* now change the content-type's media type... */
+		g_mime_content_type_set_media_type (object->content_type, "text");
+		if (!(value = g_mime_object_get_header (object, "Content-Type")))
+			throw (exception_new ("content-type header was unexpectedly null after changing type"));
+		if (strcmp ("text/octet-stream", value) != 0)
+			throw (exception_new ("content-type header had unexpected value after changing type"));
+		
+		/* now change the content-type's media subtype... */
+		g_mime_content_type_set_media_subtype (object->content_type, "plain");
+		if (!(value = g_mime_object_get_header (object, "Content-Type")))
+			throw (exception_new ("content-type header was unexpectedly null after changing subtype"));
+		if (strcmp ("text/plain", value) != 0)
+			throw (exception_new ("content-type header had unexpected value after changing subtype"));
+		
+		/* now change the content-type's parameters by setting a param */
+		g_mime_content_type_set_parameter (object->content_type, "format", "flowed");
+		if (!(value = g_mime_object_get_header (object, "Content-Type")))
+			throw (exception_new ("content-type header was unexpectedly null after setting a param"));
+		if (strcmp ("text/plain; format=flowed", value) != 0)
+			throw (exception_new ("content-type header had unexpected value after setting a param"));
+		
+		/* now change the content-type's parameters by setting a param list */
+		g_mime_content_type_set_params (object->content_type, NULL);
+		if (!(value = g_mime_object_get_header (object, "Content-Type")))
+			throw (exception_new ("content-type header was unexpectedly null after setting params"));
+		if (strcmp ("text/plain", value) != 0)
+			throw (exception_new ("content-type header had unexpected value after setting params"));
+		
+		testsuite_check_passed ();
+	} catch (ex) {
+		testsuite_check_failed ("content-type header not synchronized: %s", ex->message);
+	} finally;
+	
+	testsuite_check ("content-disposition synchronization");
+	try {
+		g_mime_object_set_disposition (object, "attachment");
+		
+		/* first, check that the current Content-Disposition header
+		 * value is "application/octet-stream" as expected */
+		if (!(value = g_mime_object_get_header (object, "Content-Disposition")))
+			throw (exception_new ("initial content-disposition header was unexpectedly null"));
+		
+		if (strcmp ("attachment", value) != 0)
+			throw (exception_new ("initial content-disposition header had unexpected value"));
+		
+		/* now change the content-disposition's disposition */
+		g_mime_content_disposition_set_disposition (object->disposition, "inline");
+		if (!(value = g_mime_object_get_header (object, "Content-Disposition")))
+			throw (exception_new ("content-disposition header was unexpectedly null after changing type"));
+		if (strcmp ("inline", value) != 0)
+			throw (exception_new ("content-disposition header had unexpected value after changing type"));
+		
+		/* now change the content-disposition's parameters by setting a param */
+		g_mime_content_disposition_set_parameter (object->disposition, "filename", "hello.txt");
+		if (!(value = g_mime_object_get_header (object, "Content-Disposition")))
+			throw (exception_new ("content-disposition header was unexpectedly null after setting a param"));
+		if (strcmp ("inline; filename=hello.txt", value) != 0)
+			throw (exception_new ("content-disposition header had unexpected value after setting a param"));
+		
+		/* now change the content-disposition's parameters by setting a param list */
+		g_mime_content_disposition_set_params (object->disposition, NULL);
+		if (!(value = g_mime_object_get_header (object, "Content-Disposition")))
+			throw (exception_new ("content-disposition header was unexpectedly null after setting params"));
+		if (strcmp ("inline", value) != 0)
+			throw (exception_new ("content-disposition header had unexpected value after setting params"));
+		
+		testsuite_check_passed ();
+	} catch (ex) {
+		testsuite_check_failed ("content-disposition header not synchronized: %s", ex->message);
+	} finally;
+	
+	g_object_unref (part);
+	
+	message = g_mime_message_new (TRUE);
+	list = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
+	object = (GMimeObject *) message;
+	
+	testsuite_check ("address header synchronization");
+	try {
+		/* first, check that the To recipients are empty */
+		if (list == NULL || internet_address_list_length (list) != 0)
+			throw (exception_new ("unexpected initial internet address list"));
+		
+		/* now check that the initial header value is null */
+		if ((value = g_mime_object_get_header (object, "To")) != NULL)
+			throw (exception_new ("unexpected initial address list header"));
+		
+		/* now try adding an address */
+		addr = internet_address_mailbox_new ("Tester", "tester localhost com");
+		internet_address_list_add (list, addr);
+		
+		if (!(value = g_mime_object_get_header (object, "To")))
+			throw (exception_new ("address list header unexpectedly null after adding recipient"));
+		
+		if (strcmp ("Tester <tester localhost com>", value) != 0)
+			throw (exception_new ("unexpected address list header after adding recipient"));
+		
+		/* now let's try changing the address name to make sure signals properly chain up */
+		internet_address_set_name (addr, "Eva Lucy-Ann Tester");
+		if (!(value = g_mime_object_get_header (object, "To")))
+			throw (exception_new ("address list header unexpectedly null after changing name"));
+		
+		if (strcmp ("Eva Lucy-Ann Tester <tester localhost com>", value) != 0)
+			throw (exception_new ("unexpected address list header after changing name"));
+		
+		/* now let's try changing the address mailbox... */
+		internet_address_mailbox_set_addr ((InternetAddressMailbox *) addr,
+						   "evalucyann ximian com");
+		if (!(value = g_mime_object_get_header (object, "To")))
+			throw (exception_new ("address list header unexpectedly null after changing mailbox"));
+		
+		if (strcmp ("Eva Lucy-Ann Tester <evalucyann ximian com>", value) != 0)
+			throw (exception_new ("unexpected address list header after changing mailbox"));
+		
+		/* now let's try inserting a group address */
+		g_object_unref (addr);
+		addr = internet_address_group_new ("Group");
+		internet_address_list_insert (list, 0, addr);
+		
+		if (!(value = g_mime_object_get_header (object, "To")))
+			throw (exception_new ("address list header unexpectedly null after inserting group"));
+		
+		if (strcmp ("Group: ;, Eva Lucy-Ann Tester <evalucyann ximian com>", value) != 0)
+			throw (exception_new ("unexpected address list header after inserting group"));
+		
+		/* now let's try removing the original recipient */
+		internet_address_list_remove_at (list, 1);
+		if (!(value = g_mime_object_get_header (object, "To")))
+			throw (exception_new ("address list header unexpectedly null after removing recipient"));
+		
+		if (strcmp ("Group: ;", value) != 0)
+			throw (exception_new ("unexpected address list header after removing recipient"));
+		
+		/* now let's try adding an address to the group... */
+		ia = internet_address_mailbox_new ("Tester", "tester hotmail com");
+		internet_address_list_add (((InternetAddressGroup *) addr)->members, ia);
+		if (!(value = g_mime_object_get_header (object, "To")))
+			throw (exception_new ("address list header unexpectedly null after adding addr to group"));
+		
+		if (strcmp ("Group: Tester <tester hotmail com>;", value) != 0)
+			throw (exception_new ("unexpected address list header after adding addr to group"));
+		
+		testsuite_check_passed ();
+	} catch (ex) {
+		testsuite_check_failed ("address header not synchronized: %s", ex->message);
+	} finally;
+	
+	g_object_unref (message);
+	g_object_unref (list);
+}
+
 int main (int argc, char **argv)
 {
 	g_mime_init (0);
@@ -383,6 +558,10 @@
 	test_iter_remove ();
 	testsuite_end ();
 	
+	testsuite_start ("header synchronization");
+	test_header_sync ();
+	testsuite_end ();
+	
 	g_mime_shutdown ();
 	
 	return testsuite_exit ();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]