diff options
Diffstat (limited to 'camel/gmime-content-field.c')
-rw-r--r-- | camel/gmime-content-field.c | 228 |
1 files changed, 55 insertions, 173 deletions
diff --git a/camel/gmime-content-field.c b/camel/gmime-content-field.c index 02b44ab7be..6d0abce4e8 100644 --- a/camel/gmime-content-field.c +++ b/camel/gmime-content-field.c @@ -28,7 +28,7 @@ #include "gmime-content-field.h" #include "string-utils.h" #include <string.h> -#include "hash-table-utils.h" +#include "camel-mime-utils.h" /** * gmime_content_field_new: Creates a new GMimeContentField object @@ -49,44 +49,13 @@ gmime_content_field_new (const gchar *type, const gchar *subtype) GMimeContentField *ctf; ctf = g_new (GMimeContentField, 1); - ctf->type = g_strdup (type); - ctf->subtype = g_strdup (subtype); - ctf->parameters = g_hash_table_new (g_strcase_hash, g_strcase_equal); + ctf->content_type = header_content_type_new(type, subtype); + ctf->type = ctf->content_type->type; + ctf->subtype = ctf->content_type->subtype; ctf->ref = 1; - return ctf; } - -static void -_free_parameter (gpointer name, gpointer value, gpointer user_data) -{ - g_free (name); - g_free (value); -} - -/** - * gmime_content_field_free: free a GMimeContentField object - * @content_field: GMimeContentField object - * - * This method destroys the object and should be used very carefully. - * Use gmime_content_field_unref instead. - * - **/ -void -gmime_content_field_free (GMimeContentField *content_field) -{ - if (!content_field) return; - - g_assert (content_field->ref <= 0); - - g_hash_table_foreach (content_field->parameters, _free_parameter, NULL); - g_free (content_field->type); - g_free (content_field->subtype); - g_hash_table_destroy (content_field->parameters); - g_free (content_field); -} - /** * gmime_content_field_ref: add a reference to a GMimeContentField object * @content_field: GMimeContentField object @@ -100,6 +69,7 @@ void gmime_content_field_ref (GMimeContentField *content_field) { content_field->ref += 1; + header_content_type_ref (content_field->content_type); } /** @@ -118,8 +88,9 @@ gmime_content_field_unref (GMimeContentField *content_field) if (!content_field) return; content_field->ref -= 1; + header_content_type_unref (content_field->content_type); if (content_field->ref <= 0) - gmime_content_field_free (content_field); + g_free (content_field); } @@ -139,44 +110,7 @@ gmime_content_field_unref (GMimeContentField *content_field) void gmime_content_field_set_parameter (GMimeContentField *content_field, const gchar *attribute, const gchar *value) { - gboolean attribute_exists; - gchar *old_attribute; - gchar *old_value; - attribute_exists = g_hash_table_lookup_extended (content_field->parameters, - attribute, - (gpointer *) &old_attribute, - (gpointer *) &old_value); - /** CHECK THAT : is normal to free pointers before insertion ? **/ - if (attribute_exists) { - g_hash_table_remove(content_field->parameters, attribute); - g_free (old_value); - g_free (old_attribute); - } - - g_hash_table_insert (content_field->parameters, g_strdup (attribute), g_strdup (value)); -} - - -/** - * _print_parameter: print a parameter/value pair to a stream as described in RFC 2045 - * @name: name of the parameter - * @value: value of the parameter - * @user_data: CamelStream object to write the text to. - * - * - **/ -static void -_print_parameter (gpointer name, gpointer value, gpointer user_data) -{ - CamelStream *stream = (CamelStream *)user_data; - - camel_stream_write_strings (stream, - "; \n ", - (gchar *)name, - "=", - (gchar *)value, - NULL); - + header_content_type_set_param(content_field->content_type, attribute, value); } /** @@ -189,16 +123,21 @@ _print_parameter (gpointer name, gpointer value, gpointer user_data) void gmime_content_field_write_to_stream (GMimeContentField *content_field, CamelStream *stream) { + struct _header_param *p; + if (!content_field) return; g_assert (stream); - if (content_field->type) { - camel_stream_write_strings (stream, "Content-Type: ", content_field->type, NULL); - if (content_field->subtype) { - camel_stream_write_strings (stream, "/", content_field->subtype, NULL); - } + if (content_field->content_type) { + camel_stream_write_strings (stream, "Content-Type: ", content_field->content_type->type?content_field->content_type->type:"text", + "/", content_field->content_type->subtype?content_field->content_type->subtype:"plain", NULL); + /* print all parameters */ - g_hash_table_foreach (content_field->parameters, _print_parameter, stream); + p = content_field->content_type->params; + while (p) { + camel_stream_write_strings (stream, ";\n ", p->name, "= \"", p->value, "\"", NULL); + p = p->next; + } camel_stream_write_string (stream, "\n"); } } @@ -219,23 +158,15 @@ gmime_content_field_get_mime_type (GMimeContentField *content_field) { gchar *mime_type; - if (!content_field->type) return NULL; + if (!content_field->content_type->type) return NULL; - if (content_field->subtype) - mime_type = g_strdup_printf ("%s/%s", content_field->type, content_field->subtype); + if (content_field->content_type->subtype) + mime_type = g_strdup_printf ("%s/%s", content_field->content_type->type, content_field->content_type->subtype); else - mime_type = g_strdup (content_field->type); + mime_type = g_strdup (content_field->content_type->type); return mime_type; } -static void -___debug_print_parameter (gpointer name, gpointer value, gpointer user_data) -{ - - printf ("****** parameter \"%s\"=\"%s\"\n", (gchar *)name, (gchar *)value); - -} - /** * gmime_content_field_get_parameter: return the value of a mime type parameter * @content_field: content field object @@ -252,22 +183,10 @@ ___debug_print_parameter (gpointer name, gpointer value, gpointer user_data) const gchar * gmime_content_field_get_parameter (GMimeContentField *content_field, const gchar *name) { - const gchar *parameter; - const gchar *old_name; - gboolean parameter_exists; + g_assert (content_field); - g_assert (content_field->parameters); g_assert (name); - /* parameter = (const gchar *)g_hash_table_lookup (content_field->parameters, name); */ - parameter_exists = g_hash_table_lookup_extended (content_field->parameters, - name, - (gpointer *) &old_name, - (gpointer *) ¶meter); - if (!parameter_exists) { - g_hash_table_foreach (content_field->parameters, ___debug_print_parameter, NULL); - return NULL; - } - return parameter; + return header_content_type_param(content_field->content_type, name); } @@ -287,76 +206,39 @@ gmime_content_field_get_parameter (GMimeContentField *content_field, const gchar void gmime_content_field_construct_from_string (GMimeContentField *content_field, const gchar *string) { - gint first, len; - gint i=0; - gchar *type, *subtype; - gchar *param_name, *param_value; - gboolean param_end; - + struct _header_content_type *new; + g_assert (string); g_assert (content_field); - - g_free (content_field->type); - g_free (content_field->subtype); - - - first = 0; - len = strlen (string); - if (!len) - return; - - /* find the type */ - while ( (i<len) && (!strchr ("/;", string[i])) ) i++; - - if (i == 0) - return; - - type = g_strndup (string, i); - string_trim (type, " \t\"", STRING_TRIM_STRIP_TRAILING | STRING_TRIM_STRIP_LEADING); - content_field->type = type; - if (i >= len-1) { - content_field->subtype = NULL; - return; - } - - first = i+1; - /* find the subtype, if any */ - if (string[i++] == '/') { - while ( (i<len) && (string[i] != ';') ) i++; - if (i != first) { - subtype = g_strndup (string+first, i-first); - string_trim (subtype, " \t\"", STRING_TRIM_STRIP_TRAILING | STRING_TRIM_STRIP_LEADING); - content_field->subtype = subtype; - if (i >= len-1) - return; - } - } - first = i+1; - - /* parse parameters list */ - param_end = FALSE; - do { - while ( (i<len) && (string[i] != '=') ) i++; - if ((i == len) || (i==first)) param_end = TRUE; - else { - /* we have found parameter name */ - param_name = g_strndup (string+first, i-first); - string_trim (param_name, " \"", STRING_TRIM_STRIP_TRAILING | STRING_TRIM_STRIP_LEADING); - i++; - first = i; - /* Let's find parameter value */ - while ( (i<len) && (string[i] != ';') ) i++; - if (i != first) param_value = g_strndup (string+first, i-first); - else param_value = g_strdup (""); - string_trim (param_value, " \t\"", STRING_TRIM_STRIP_TRAILING | STRING_TRIM_STRIP_LEADING); - gmime_content_field_set_parameter (content_field, param_name, param_value); - g_free (param_name); - g_free (param_value); - i++; - first = i; - } - } while ((!param_end) && (first < len)); + new = header_content_type_decode(string); + if (content_field->content_type) + header_content_type_unref(content_field->content_type); + if (new == NULL) { + new = header_content_type_new(NULL, NULL); + g_warning("Cannot parse content-type string: %s", string); + } + content_field->content_type = new; + content_field->type = new->type; + content_field->subtype = new->subtype; } +/** + * gmime_content_field_is_type: + * @content_field: An initialised GMimeContentField. + * @type: MIME Major type name. + * @subtype: MIME subtype. + * + * Returns true if the content_field is of the type @type and subtype @subtype. + * If @subtype is the special wildcard "*", then it will match any type. + * + * If the @content_field is empty, then it will match "text/plain", or "text/ *". + * + * Return value: + **/ +int +gmime_content_field_is_type (GMimeContentField *content_field, const char *type, const char *subtype) +{ + return header_content_type_is(content_field->content_type, type, subtype); +} |