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
28 #include "gmime-message-part.h"
34 * SECTION: gmime-message-part
35 * @title: GMimeMessagePart
36 * @short_description: Message parts
39 * A #GMimeMessagePart represents message/rfc822 and message/news MIME
44 /* GObject class methods */
45 static void g_mime_message_part_class_init (GMimeMessagePartClass *klass);
46 static void g_mime_message_part_init (GMimeMessagePart *message_part, GMimeMessagePartClass *klass);
47 static void g_mime_message_part_finalize (GObject *object);
49 /* GMimeObject class methods */
50 static ssize_t message_part_write_to_stream (GMimeObject *object, GMimeStream *stream);
53 static GMimeObjectClass *parent_class = NULL;
57 g_mime_message_part_get_type (void)
59 static GType type = 0;
62 static const GTypeInfo info = {
63 sizeof (GMimeMessagePartClass),
64 NULL, /* base_class_init */
65 NULL, /* base_class_finalize */
66 (GClassInitFunc) g_mime_message_part_class_init,
67 NULL, /* class_finalize */
68 NULL, /* class_data */
69 sizeof (GMimeMessagePart),
71 (GInstanceInitFunc) g_mime_message_part_init,
74 type = g_type_register_static (GMIME_TYPE_OBJECT, "GMimeMessagePart", &info, 0);
82 g_mime_message_part_class_init (GMimeMessagePartClass *klass)
84 GMimeObjectClass *object_class = GMIME_OBJECT_CLASS (klass);
85 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87 parent_class = g_type_class_ref (GMIME_TYPE_OBJECT);
89 gobject_class->finalize = g_mime_message_part_finalize;
91 object_class->write_to_stream = message_part_write_to_stream;
95 g_mime_message_part_init (GMimeMessagePart *part, GMimeMessagePartClass *klass)
101 g_mime_message_part_finalize (GObject *object)
103 GMimeMessagePart *part = (GMimeMessagePart *) object;
106 g_object_unref (part->message);
108 G_OBJECT_CLASS (parent_class)->finalize (object);
112 message_part_write_to_stream (GMimeObject *object, GMimeStream *stream)
114 GMimeMessagePart *part = (GMimeMessagePart *) object;
115 ssize_t nwritten, total = 0;
117 /* write the content headers */
118 if ((nwritten = g_mime_header_list_write_to_stream (object->headers, stream)) == -1)
123 /* terminate the headers */
124 if ((nwritten = g_mime_stream_write (stream, "\n", 1)) == -1)
129 /* write the message */
131 if ((nwritten = g_mime_object_write_to_stream (GMIME_OBJECT (part->message), stream)) == -1)
142 * g_mime_message_part_new:
143 * @subtype: message subtype or %NULL for "rfc822"
145 * Creates a new MIME message part object with a default content-type
146 * of message/@subtype.
148 * Returns: an empty MIME message part object with a default
149 * content-type of message/@subtype.
152 g_mime_message_part_new (const char *subtype)
154 GMimeContentType *content_type;
155 GMimeMessagePart *part;
157 part = g_object_newv (GMIME_TYPE_MESSAGE_PART, 0, NULL);
159 content_type = g_mime_content_type_new ("message", subtype ? subtype : "rfc822");
160 g_mime_object_set_content_type (GMIME_OBJECT (part), content_type);
161 g_object_unref (content_type);
168 * g_mime_message_part_new_with_message:
169 * @subtype: message subtype or %NULL for "rfc822"
172 * Creates a new MIME message part object with a default content-type
173 * of message/@subtype containing @message.
175 * Returns: a MIME message part object with a default content-type of
176 * message/@subtype containing @message.
179 g_mime_message_part_new_with_message (const char *subtype, GMimeMessage *message)
181 GMimeMessagePart *part;
183 part = g_mime_message_part_new (subtype);
184 part->message = message;
185 g_object_ref (message);
192 * g_mime_message_part_set_message:
193 * @part: message part
196 * Sets the @message object on the message part object @part.
199 g_mime_message_part_set_message (GMimeMessagePart *part, GMimeMessage *message)
201 g_return_if_fail (GMIME_IS_MESSAGE_PART (part));
204 g_object_ref (message);
207 g_object_unref (part->message);
209 part->message = message;
214 * g_mime_message_part_get_message:
215 * @part: message part
217 * Gets the message object on the message part object @part.
219 * Returns: the message part contained within @part.
222 g_mime_message_part_get_message (GMimeMessagePart *part)
224 g_return_val_if_fail (GMIME_IS_MESSAGE_PART (part), NULL);
226 return part->message;