1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2012 Jeffrey Stedfast
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 #include "gmime-common.h"
30 #include "gmime-object.h"
31 #include "gmime-stream-mem.h"
32 #include "gmime-events.h"
33 #include "gmime-utils.h"
37 * SECTION: gmime-object
39 * @short_description: Abstract MIME objects
42 * #GMimeObject is an abstract class from which all message and MIME
50 GHashTable *subtype_hash;
53 struct _subtype_bucket {
58 static void _g_mime_object_set_content_disposition (GMimeObject *object, GMimeContentDisposition *disposition);
59 void _g_mime_object_set_content_type (GMimeObject *object, GMimeContentType *content_type);
61 static void g_mime_object_class_init (GMimeObjectClass *klass);
62 static void g_mime_object_init (GMimeObject *object, GMimeObjectClass *klass);
63 static void g_mime_object_finalize (GObject *object);
65 static void object_prepend_header (GMimeObject *object, const char *name, const char *value);
66 static void object_append_header (GMimeObject *object, const char *name, const char *value);
67 static void object_set_header (GMimeObject *object, const char *name, const char *value);
68 static const char *object_get_header (GMimeObject *object, const char *name);
69 static gboolean object_remove_header (GMimeObject *object, const char *name);
70 static void object_set_content_type (GMimeObject *object, GMimeContentType *content_type);
71 static char *object_get_headers (GMimeObject *object);
72 static ssize_t object_write_to_stream (GMimeObject *object, GMimeStream *stream);
73 static void object_encode (GMimeObject *object, GMimeEncodingConstraint constraint);
75 static ssize_t write_content_type (GMimeStream *stream, const char *name, const char *value);
76 static ssize_t write_disposition (GMimeStream *stream, const char *name, const char *value);
78 static void content_type_changed (GMimeContentType *content_type, gpointer args, GMimeObject *object);
79 static void content_disposition_changed (GMimeContentDisposition *disposition, gpointer args, GMimeObject *object);
82 static GHashTable *type_hash = NULL;
84 static GObjectClass *parent_class = NULL;
88 g_mime_object_get_type (void)
90 static GType type = 0;
93 static const GTypeInfo info = {
94 sizeof (GMimeObjectClass),
95 NULL, /* base_class_init */
96 NULL, /* base_class_finalize */
97 (GClassInitFunc) g_mime_object_class_init,
98 NULL, /* class_finalize */
99 NULL, /* class_data */
100 sizeof (GMimeObject),
102 (GInstanceInitFunc) g_mime_object_init,
105 type = g_type_register_static (G_TYPE_OBJECT, "GMimeObject",
106 &info, G_TYPE_FLAG_ABSTRACT);
114 g_mime_object_class_init (GMimeObjectClass *klass)
116 GObjectClass *object_class = G_OBJECT_CLASS (klass);
118 parent_class = g_type_class_ref (G_TYPE_OBJECT);
120 object_class->finalize = g_mime_object_finalize;
122 klass->prepend_header = object_prepend_header;
123 klass->append_header = object_append_header;
124 klass->remove_header = object_remove_header;
125 klass->set_header = object_set_header;
126 klass->get_header = object_get_header;
127 klass->set_content_type = object_set_content_type;
128 klass->get_headers = object_get_headers;
129 klass->write_to_stream = object_write_to_stream;
130 klass->encode = object_encode;
134 g_mime_object_init (GMimeObject *object, GMimeObjectClass *klass)
136 object->headers = g_mime_header_list_new ();
137 object->content_type = NULL;
138 object->disposition = NULL;
139 object->content_id = NULL;
141 g_mime_header_list_register_writer (object->headers, "Content-Type", write_content_type);
142 g_mime_header_list_register_writer (object->headers, "Content-Disposition", write_disposition);
146 g_mime_object_finalize (GObject *object)
148 GMimeObject *mime = (GMimeObject *) object;
150 if (mime->content_type) {
151 g_mime_event_remove (mime->content_type->priv, (GMimeEventCallback) content_type_changed, object);
152 g_object_unref (mime->content_type);
155 if (mime->disposition) {
156 g_mime_event_remove (mime->disposition->priv, (GMimeEventCallback) content_disposition_changed, object);
157 g_object_unref (mime->disposition);
161 g_mime_header_list_destroy (mime->headers);
163 g_free (mime->content_id);
165 G_OBJECT_CLASS (parent_class)->finalize (object);
170 write_content_type (GMimeStream *stream, const char *name, const char *value)
172 GMimeContentType *content_type;
177 out = g_string_new ("");
178 g_string_printf (out, "%s: ", name);
180 content_type = g_mime_content_type_new_from_string (value);
182 val = g_mime_content_type_to_string (content_type);
183 g_string_append (out, val);
186 g_mime_param_write_to_string (content_type->params, TRUE, out);
187 g_object_unref (content_type);
189 nwritten = g_mime_stream_write (stream, out->str, out->len);
190 g_string_free (out, TRUE);
196 content_type_changed (GMimeContentType *content_type, gpointer args, GMimeObject *object)
202 string = g_string_new ("Content-Type: ");
204 type = g_mime_content_type_to_string (content_type);
205 g_string_append (string, type);
208 if ((params = content_type->params))
209 g_mime_param_write_to_string (params, FALSE, string);
212 g_string_free (string, FALSE);
214 type = p + strlen ("Content-Type: ");
215 g_mime_header_list_set (object->headers, "Content-Type", type);
220 write_disposition (GMimeStream *stream, const char *name, const char *value)
222 GMimeContentDisposition *disposition;
226 out = g_string_new ("");
227 g_string_printf (out, "%s: ", name);
229 disposition = g_mime_content_disposition_new_from_string (value);
230 g_string_append (out, disposition->disposition);
232 g_mime_param_write_to_string (disposition->params, TRUE, out);
233 g_object_unref (disposition);
235 nwritten = g_mime_stream_write (stream, out->str, out->len);
236 g_string_free (out, TRUE);
242 content_disposition_changed (GMimeContentDisposition *disposition, gpointer args, GMimeObject *object)
246 if (object->disposition) {
247 str = g_mime_content_disposition_to_string (object->disposition, FALSE);
248 g_mime_header_list_set (object->headers, "Content-Disposition", str);
251 g_mime_header_list_remove (object->headers, "Content-Disposition");
257 * g_mime_object_register_type:
259 * @subtype: mime subtype
260 * @object_type: object type
262 * Registers the object type @object_type for use with the
263 * g_mime_object_new_type() convenience function.
265 * Note: You may use the wildcard "*" to match any type and/or
269 g_mime_object_register_type (const char *type, const char *subtype, GType object_type)
271 struct _type_bucket *bucket;
272 struct _subtype_bucket *sub;
274 g_return_if_fail (object_type != 0);
275 g_return_if_fail (subtype != NULL);
276 g_return_if_fail (type != NULL);
278 if (!(bucket = g_hash_table_lookup (type_hash, type))) {
279 bucket = g_new (struct _type_bucket, 1);
280 bucket->type = g_strdup (type);
281 bucket->object_type = *type == '*' ? object_type : 0;
282 bucket->subtype_hash = g_hash_table_new (g_mime_strcase_hash, g_mime_strcase_equal);
283 g_hash_table_insert (type_hash, bucket->type, bucket);
286 sub = g_new (struct _subtype_bucket, 1);
287 sub->subtype = g_strdup (subtype);
288 sub->object_type = object_type;
289 g_hash_table_insert (bucket->subtype_hash, sub->subtype, sub);
295 * @content_type: a #GMimeContentType object
297 * Performs a lookup of registered #GMimeObject subclasses, registered
298 * using g_mime_object_register_type(), to find an appropriate class
299 * capable of handling MIME parts of the specified Content-Type. If no
300 * class has been registered to handle that type, it looks for a
301 * registered class that can handle @content_type's media type. If
302 * that also fails, then it will use the generic part class,
305 * Returns: an appropriate #GMimeObject registered to handle MIME
306 * parts appropriate for @content_type.
309 g_mime_object_new (GMimeContentType *content_type)
311 struct _type_bucket *bucket;
312 struct _subtype_bucket *sub;
316 g_return_val_if_fail (GMIME_IS_CONTENT_TYPE (content_type), NULL);
318 if ((bucket = g_hash_table_lookup (type_hash, content_type->type))) {
319 if (!(sub = g_hash_table_lookup (bucket->subtype_hash, content_type->subtype)))
320 sub = g_hash_table_lookup (bucket->subtype_hash, "*");
322 obj_type = sub ? sub->object_type : 0;
324 bucket = g_hash_table_lookup (type_hash, "*");
325 obj_type = bucket ? bucket->object_type : 0;
329 /* use the default mime object */
330 if ((bucket = g_hash_table_lookup (type_hash, "*"))) {
331 sub = g_hash_table_lookup (bucket->subtype_hash, "*");
332 obj_type = sub ? sub->object_type : 0;
339 object = g_object_newv (obj_type, 0, NULL);
341 g_mime_object_set_content_type (object, content_type);
348 * g_mime_object_new_type:
350 * @subtype: mime subtype
352 * Performs a lookup of registered #GMimeObject subclasses, registered
353 * using g_mime_object_register_type(), to find an appropriate class
354 * capable of handling MIME parts of type @type/@subtype. If no class
355 * has been registered to handle that type, it looks for a registered
356 * class that can handle @type. If that also fails, then it will use
357 * the generic part class, #GMimePart.
359 * Returns: an appropriate #GMimeObject registered to handle mime-types
363 g_mime_object_new_type (const char *type, const char *subtype)
365 struct _type_bucket *bucket;
366 struct _subtype_bucket *sub;
369 g_return_val_if_fail (type != NULL, NULL);
371 if ((bucket = g_hash_table_lookup (type_hash, type))) {
372 if (!(sub = g_hash_table_lookup (bucket->subtype_hash, subtype)))
373 sub = g_hash_table_lookup (bucket->subtype_hash, "*");
375 obj_type = sub ? sub->object_type : 0;
377 bucket = g_hash_table_lookup (type_hash, "*");
378 obj_type = bucket ? bucket->object_type : 0;
382 /* use the default mime object */
383 if ((bucket = g_hash_table_lookup (type_hash, "*"))) {
384 sub = g_hash_table_lookup (bucket->subtype_hash, "*");
385 obj_type = sub ? sub->object_type : 0;
392 return g_object_newv (obj_type, 0, NULL);
397 object_set_content_type (GMimeObject *object, GMimeContentType *content_type)
399 if (object->content_type) {
400 g_mime_event_remove (object->content_type->priv, (GMimeEventCallback) content_type_changed, object);
401 g_object_unref (object->content_type);
404 g_mime_event_add (content_type->priv, (GMimeEventCallback) content_type_changed, object);
405 object->content_type = content_type;
406 g_object_ref (content_type);
411 * _g_mime_object_set_content_type:
412 * @object: a #GMimeObject
413 * @content_type: a #GMimeContentType object
415 * Sets the content-type for the specified MIME object.
417 * Note: This method is meant for internal-use only and avoids
418 * serialization of @content_type to the Content-Type header field.
421 _g_mime_object_set_content_type (GMimeObject *object, GMimeContentType *content_type)
423 GMIME_OBJECT_GET_CLASS (object)->set_content_type (object, content_type);
428 * g_mime_object_set_content_type:
429 * @object: a #GMimeObject
430 * @content_type: a #GMimeContentType object
432 * Sets the content-type for the specified MIME object and then
433 * serializes it to the Content-Type header field.
436 g_mime_object_set_content_type (GMimeObject *object, GMimeContentType *content_type)
438 g_return_if_fail (GMIME_IS_CONTENT_TYPE (content_type));
439 g_return_if_fail (GMIME_IS_OBJECT (object));
441 if (object->content_type == content_type)
444 GMIME_OBJECT_GET_CLASS (object)->set_content_type (object, content_type);
446 content_type_changed (content_type, NULL, object);
451 * g_mime_object_get_content_type:
452 * @object: a #GMimeObject
454 * Gets the #GMimeContentType object for the given MIME object or
457 * Returns: the content-type object for the specified MIME object.
460 g_mime_object_get_content_type (GMimeObject *object)
462 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
464 return object->content_type;
469 * g_mime_object_set_content_type_parameter:
470 * @object: a #GMimeObject
472 * @value: param value
474 * Sets the content-type param @name to the value @value.
477 g_mime_object_set_content_type_parameter (GMimeObject *object, const char *name, const char *value)
479 g_return_if_fail (GMIME_IS_OBJECT (object));
480 g_return_if_fail (name != NULL);
482 g_mime_content_type_set_parameter (object->content_type, name, value);
487 * g_mime_object_get_content_type_parameter:
488 * @object: a #GMimeObject
491 * Gets the value of the content-type param @name set on the MIME part
494 * Returns: the value of the requested content-type param or %NULL on
495 * if the param doesn't exist.
498 g_mime_object_get_content_type_parameter (GMimeObject *object, const char *name)
500 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
501 g_return_val_if_fail (name != NULL, NULL);
503 return g_mime_content_type_get_parameter (object->content_type, name);
508 * g_mime_object_get_content_disposition:
509 * @object: a #GMimeObject
511 * Gets the #GMimeContentDisposition for the specified MIME object.
513 * Returns: the #GMimeContentDisposition set on the MIME object.
515 GMimeContentDisposition *
516 g_mime_object_get_content_disposition (GMimeObject *object)
518 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
520 return object->disposition;
524 * g_mime_object_set_content_disposition:
525 * @object: a #GMimeObject
526 * @disposition: a #GMimeContentDisposition object
528 * Set the content disposition for the specified mime part.
530 * Note: This method is meant for internal-use only and avoids
531 * serialization of @disposition to the Content-Disposition header
535 _g_mime_object_set_content_disposition (GMimeObject *object, GMimeContentDisposition *disposition)
537 if (object->disposition) {
538 g_mime_event_remove (object->disposition->priv, (GMimeEventCallback) content_disposition_changed, object);
539 g_object_unref (object->disposition);
542 g_mime_event_add (disposition->priv, (GMimeEventCallback) content_disposition_changed, object);
543 object->disposition = disposition;
544 g_object_ref (disposition);
549 * g_mime_object_set_content_disposition:
550 * @object: a #GMimeObject
551 * @disposition: a #GMimeContentDisposition object
553 * Set the content disposition for the specified mime part and then
554 * serializes it to the Content-Disposition header field.
557 g_mime_object_set_content_disposition (GMimeObject *object, GMimeContentDisposition *disposition)
559 g_return_if_fail (GMIME_IS_CONTENT_DISPOSITION (disposition));
560 g_return_if_fail (GMIME_IS_OBJECT (object));
562 if (object->disposition == disposition)
565 _g_mime_object_set_content_disposition (object, disposition);
567 content_disposition_changed (disposition, NULL, object);
572 * g_mime_object_set_disposition:
573 * @object: a #GMimeObject
574 * @disposition: disposition ("attachment" or "inline")
576 * Sets the disposition to @disposition which may be one of
577 * #GMIME_DISPOSITION_ATTACHMENT or #GMIME_DISPOSITION_INLINE or, by
578 * your choice, any other string which would indicate how the MIME
579 * part should be displayed by the MUA.
582 g_mime_object_set_disposition (GMimeObject *object, const char *disposition)
584 GMimeContentDisposition *disp;
586 g_return_if_fail (GMIME_IS_OBJECT (object));
587 g_return_if_fail (disposition != NULL);
589 if (object->disposition) {
590 g_mime_content_disposition_set_disposition (object->disposition, disposition);
594 disp = g_mime_content_disposition_new ();
595 g_mime_content_disposition_set_disposition (disp, disposition);
596 g_mime_object_set_content_disposition (object, disp);
597 g_object_unref (disp);
602 * g_mime_object_get_disposition:
603 * @object: a #GMimeObject
605 * Gets the MIME object's disposition if set or %NULL otherwise.
607 * Returns: the disposition string which is probably one of
608 * #GMIME_DISPOSITION_ATTACHMENT or #GMIME_DISPOSITION_INLINE.
611 g_mime_object_get_disposition (GMimeObject *object)
613 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
615 if (object->disposition)
616 return g_mime_content_disposition_get_disposition (object->disposition);
623 * g_mime_object_set_content_disposition_parameter:
624 * @object: a #GMimeObject
625 * @attribute: parameter name
626 * @value: parameter value
628 * Add a content-disposition parameter to the specified mime part.
631 g_mime_object_set_content_disposition_parameter (GMimeObject *object, const char *attribute, const char *value)
633 GMimeContentDisposition *disposition;
635 g_return_if_fail (GMIME_IS_OBJECT (object));
636 g_return_if_fail (attribute != NULL);
638 if (!object->disposition) {
639 disposition = g_mime_content_disposition_new ();
640 _g_mime_object_set_content_disposition (object, disposition);
643 g_mime_content_disposition_set_parameter (object->disposition, attribute, value);
648 * g_mime_object_get_content_disposition_parameter:
649 * @object: a #GMimeObject
650 * @attribute: parameter name
652 * Gets the value of the Content-Disposition parameter specified by
653 * @attribute, or %NULL if the parameter does not exist.
655 * Returns: the value of a previously defined content-disposition
656 * parameter specified by @attribute.
659 g_mime_object_get_content_disposition_parameter (GMimeObject *object, const char *attribute)
661 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
662 g_return_val_if_fail (attribute != NULL, NULL);
664 if (!object->disposition)
667 return g_mime_content_disposition_get_parameter (object->disposition, attribute);
672 * g_mime_object_set_content_id:
673 * @object: a #GMimeObject
674 * @content_id: content-id (addr-spec portion)
676 * Sets the Content-Id of the MIME object.
679 g_mime_object_set_content_id (GMimeObject *object, const char *content_id)
683 g_return_if_fail (GMIME_IS_OBJECT (object));
685 g_free (object->content_id);
686 object->content_id = g_strdup (content_id);
688 msgid = g_strdup_printf ("<%s>", content_id);
689 g_mime_object_set_header (object, "Content-Id", msgid);
695 * g_mime_object_get_content_id:
696 * @object: a #GMimeObject
698 * Gets the Content-Id of the MIME object or NULL if one is not set.
700 * Returns: a const pointer to the Content-Id header.
703 g_mime_object_get_content_id (GMimeObject *object)
705 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
707 return object->content_id;
712 HEADER_CONTENT_DISPOSITION,
718 static char *content_headers[] = {
719 "Content-Disposition",
725 process_header (GMimeObject *object, const char *header, const char *value)
727 GMimeContentDisposition *disposition;
728 GMimeContentType *content_type;
731 if (g_ascii_strncasecmp (header, "Content-", 8) != 0)
734 for (i = 0; i < G_N_ELEMENTS (content_headers); i++) {
735 if (!g_ascii_strcasecmp (content_headers[i] + 8, header + 8))
740 case HEADER_CONTENT_DISPOSITION:
741 disposition = g_mime_content_disposition_new_from_string (value);
742 _g_mime_object_set_content_disposition (object, disposition);
743 g_object_unref (disposition);
745 case HEADER_CONTENT_TYPE:
746 content_type = g_mime_content_type_new_from_string (value);
747 _g_mime_object_set_content_type (object, content_type);
748 g_object_unref (content_type);
750 case HEADER_CONTENT_ID:
751 g_free (object->content_id);
752 object->content_id = g_mime_utils_decode_message_id (value);
758 g_mime_header_list_set (object->headers, header, value);
764 object_prepend_header (GMimeObject *object, const char *header, const char *value)
766 if (!process_header (object, header, value))
767 g_mime_header_list_prepend (object->headers, header, value);
772 * g_mime_object_prepend_header:
773 * @object: a #GMimeObject
774 * @header: header name
775 * @value: header value
777 * Prepends a raw, unprocessed header to the MIME object.
780 g_mime_object_prepend_header (GMimeObject *object, const char *header, const char *value)
782 g_return_if_fail (GMIME_IS_OBJECT (object));
783 g_return_if_fail (header != NULL);
784 g_return_if_fail (value != NULL);
786 GMIME_OBJECT_GET_CLASS (object)->prepend_header (object, header, value);
790 object_append_header (GMimeObject *object, const char *header, const char *value)
792 if (!process_header (object, header, value))
793 g_mime_header_list_append (object->headers, header, value);
798 * g_mime_object_append_header:
799 * @object: a #GMimeObject
800 * @header: header name
801 * @value: header value
803 * Appends a raw, unprocessed header to the MIME object.
806 g_mime_object_append_header (GMimeObject *object, const char *header, const char *value)
808 g_return_if_fail (GMIME_IS_OBJECT (object));
809 g_return_if_fail (header != NULL);
810 g_return_if_fail (value != NULL);
812 GMIME_OBJECT_GET_CLASS (object)->append_header (object, header, value);
817 object_set_header (GMimeObject *object, const char *header, const char *value)
819 if (!process_header (object, header, value))
820 g_mime_header_list_set (object->headers, header, value);
825 * g_mime_object_set_header:
826 * @object: a #GMimeObject
827 * @header: header name
828 * @value: header value
830 * Sets an arbitrary raw, unprocessed header on the MIME object.
833 g_mime_object_set_header (GMimeObject *object, const char *header, const char *value)
835 g_return_if_fail (GMIME_IS_OBJECT (object));
836 g_return_if_fail (header != NULL);
837 g_return_if_fail (value != NULL);
839 GMIME_OBJECT_GET_CLASS (object)->set_header (object, header, value);
844 object_get_header (GMimeObject *object, const char *header)
846 return g_mime_header_list_get (object->headers, header);
851 * g_mime_object_get_header:
852 * @object: a #GMimeObject
853 * @header: header name
855 * Gets the raw, unprocessed value of the requested header.
857 * Returns: the raw, unprocessed value of the requested header if it
858 * exists or %NULL otherwise.
861 g_mime_object_get_header (GMimeObject *object, const char *header)
863 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
864 g_return_val_if_fail (header != NULL, NULL);
866 return GMIME_OBJECT_GET_CLASS (object)->get_header (object, header);
871 object_remove_header (GMimeObject *object, const char *header)
875 for (i = 0; i < G_N_ELEMENTS (content_headers); i++) {
876 if (!g_ascii_strcasecmp (content_headers[i], header))
881 case HEADER_CONTENT_DISPOSITION:
882 if (object->disposition) {
883 g_mime_event_remove (object->disposition->priv, (GMimeEventCallback) content_disposition_changed, object);
884 g_object_unref (object->disposition);
885 object->disposition = NULL;
888 case HEADER_CONTENT_TYPE:
889 /* never allow the removal of the Content-Type header */
891 case HEADER_CONTENT_ID:
892 g_free (object->content_id);
893 object->content_id = NULL;
899 return g_mime_header_list_remove (object->headers, header);
904 * g_mime_object_remove_header:
905 * @object: a #GMimeObject
906 * @header: header name
908 * Removed the specified header if it exists.
910 * Returns: %TRUE if the header was removed or %FALSE if it could not
914 g_mime_object_remove_header (GMimeObject *object, const char *header)
916 g_return_val_if_fail (GMIME_IS_OBJECT (object), FALSE);
917 g_return_val_if_fail (header != NULL, FALSE);
919 return GMIME_OBJECT_GET_CLASS (object)->remove_header (object, header);
924 object_get_headers (GMimeObject *object)
926 return g_mime_header_list_to_string (object->headers);
931 * g_mime_object_get_headers:
932 * @object: a #GMimeObject
934 * Allocates a string buffer containing all of the MIME object's raw
937 * Returns: an allocated string containing all of the raw MIME headers.
940 g_mime_object_get_headers (GMimeObject *object)
942 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
944 return GMIME_OBJECT_GET_CLASS (object)->get_headers (object);
949 object_write_to_stream (GMimeObject *object, GMimeStream *stream)
956 * g_mime_object_write_to_stream:
957 * @object: a #GMimeObject
960 * Write the contents of the MIME object to @stream.
962 * Returns: the number of bytes written or %-1 on fail.
965 g_mime_object_write_to_stream (GMimeObject *object, GMimeStream *stream)
967 g_return_val_if_fail (GMIME_IS_OBJECT (object), -1);
968 g_return_val_if_fail (GMIME_IS_STREAM (stream), -1);
970 return GMIME_OBJECT_GET_CLASS (object)->write_to_stream (object, stream);
975 object_encode (GMimeObject *object, GMimeEncodingConstraint constraint)
982 * g_mime_object_encode:
983 * @object: a #GMimeObject
984 * @constraint: a #GMimeEncodingConstraint
986 * Calculates and sets the most efficient Content-Transfer-Encoding
987 * for this #GMimeObject and all child parts based on the @constraint
991 g_mime_object_encode (GMimeObject *object, GMimeEncodingConstraint constraint)
993 g_return_if_fail (GMIME_IS_OBJECT (object));
995 GMIME_OBJECT_GET_CLASS (object)->encode (object, constraint);
1000 * g_mime_object_to_string:
1001 * @object: a #GMimeObject
1003 * Allocates a string buffer containing the contents of @object.
1005 * Returns: an allocated string containing the contents of the mime
1009 g_mime_object_to_string (GMimeObject *object)
1011 GMimeStream *stream;
1015 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
1017 array = g_byte_array_new ();
1018 stream = g_mime_stream_mem_new ();
1019 g_mime_stream_mem_set_byte_array (GMIME_STREAM_MEM (stream), array);
1021 g_mime_object_write_to_stream (object, stream);
1023 g_object_unref (stream);
1024 g_byte_array_append (array, (unsigned char *) "", 1);
1025 str = (char *) array->data;
1026 g_byte_array_free (array, FALSE);
1033 * g_mime_object_get_header_list:
1034 * @object: a #GMimeObject
1036 * Get the header list for @object.
1038 * Returns: the #GMimeHeaderList for @object. Do not free this pointer
1039 * when you are done with it.
1042 g_mime_object_get_header_list (GMimeObject *object)
1044 g_return_val_if_fail (GMIME_IS_OBJECT (object), NULL);
1046 return object->headers;
1051 subtype_bucket_foreach (gpointer key, gpointer value, gpointer user_data)
1053 struct _subtype_bucket *bucket = value;
1055 g_free (bucket->subtype);
1060 type_bucket_foreach (gpointer key, gpointer value, gpointer user_data)
1062 struct _type_bucket *bucket = value;
1064 g_free (bucket->type);
1066 if (bucket->subtype_hash) {
1067 g_hash_table_foreach (bucket->subtype_hash, subtype_bucket_foreach, NULL);
1068 g_hash_table_destroy (bucket->subtype_hash);
1075 g_mime_object_type_registry_shutdown (void)
1077 g_hash_table_foreach (type_hash, type_bucket_foreach, NULL);
1078 g_hash_table_destroy (type_hash);
1083 g_mime_object_type_registry_init (void)
1088 type_hash = g_hash_table_new (g_mime_strcase_hash, g_mime_strcase_equal);