Initialize the gmime for upstream
[platform/upstream/gmime.git] / gmime / gmime-message-part.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2012 Jeffrey Stedfast
4  *
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.
9  *
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.
14  *
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
18  *  02110-1301, USA.
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #include "gmime-message-part.h"
29
30 #define d(x)
31
32
33 /**
34  * SECTION: gmime-message-part
35  * @title: GMimeMessagePart
36  * @short_description: Message parts
37  * @see_also:
38  *
39  * A #GMimeMessagePart represents message/rfc822 and message/news MIME
40  * parts.
41  **/
42
43
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);
48
49 /* GMimeObject class methods */
50 static ssize_t message_part_write_to_stream (GMimeObject *object, GMimeStream *stream);
51
52
53 static GMimeObjectClass *parent_class = NULL;
54
55
56 GType
57 g_mime_message_part_get_type (void)
58 {
59         static GType type = 0;
60         
61         if (!type) {
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),
70                         0,    /* n_preallocs */
71                         (GInstanceInitFunc) g_mime_message_part_init,
72                 };
73                 
74                 type = g_type_register_static (GMIME_TYPE_OBJECT, "GMimeMessagePart", &info, 0);
75         }
76         
77         return type;
78 }
79
80
81 static void
82 g_mime_message_part_class_init (GMimeMessagePartClass *klass)
83 {
84         GMimeObjectClass *object_class = GMIME_OBJECT_CLASS (klass);
85         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
86         
87         parent_class = g_type_class_ref (GMIME_TYPE_OBJECT);
88         
89         gobject_class->finalize = g_mime_message_part_finalize;
90         
91         object_class->write_to_stream = message_part_write_to_stream;
92 }
93
94 static void
95 g_mime_message_part_init (GMimeMessagePart *part, GMimeMessagePartClass *klass)
96 {
97         part->message = NULL;
98 }
99
100 static void
101 g_mime_message_part_finalize (GObject *object)
102 {
103         GMimeMessagePart *part = (GMimeMessagePart *) object;
104         
105         if (part->message)
106                 g_object_unref (part->message);
107         
108         G_OBJECT_CLASS (parent_class)->finalize (object);
109 }
110
111 static ssize_t
112 message_part_write_to_stream (GMimeObject *object, GMimeStream *stream)
113 {
114         GMimeMessagePart *part = (GMimeMessagePart *) object;
115         ssize_t nwritten, total = 0;
116         
117         /* write the content headers */
118         if ((nwritten = g_mime_header_list_write_to_stream (object->headers, stream)) == -1)
119                 return -1;
120         
121         total += nwritten;
122         
123         /* terminate the headers */
124         if ((nwritten = g_mime_stream_write (stream, "\n", 1)) == -1)
125                 return -1;
126         
127         total += nwritten;
128         
129         /* write the message */
130         if (part->message) {
131                 if ((nwritten = g_mime_object_write_to_stream (GMIME_OBJECT (part->message), stream)) == -1)
132                         return -1;
133                 
134                 total += nwritten;
135         }
136         
137         return total;
138 }
139
140
141 /**
142  * g_mime_message_part_new:
143  * @subtype: message subtype or %NULL for "rfc822"
144  *
145  * Creates a new MIME message part object with a default content-type
146  * of message/@subtype.
147  *
148  * Returns: an empty MIME message part object with a default
149  * content-type of message/@subtype.
150  **/
151 GMimeMessagePart *
152 g_mime_message_part_new (const char *subtype)
153 {
154         GMimeContentType *content_type;
155         GMimeMessagePart *part;
156         
157         part = g_object_newv (GMIME_TYPE_MESSAGE_PART, 0, NULL);
158         
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);
162         
163         return part;
164 }
165
166
167 /**
168  * g_mime_message_part_new_with_message:
169  * @subtype: message subtype or %NULL for "rfc822"
170  * @message: message
171  *
172  * Creates a new MIME message part object with a default content-type
173  * of message/@subtype containing @message.
174  *
175  * Returns: a MIME message part object with a default content-type of
176  * message/@subtype containing @message.
177  **/
178 GMimeMessagePart *
179 g_mime_message_part_new_with_message (const char *subtype, GMimeMessage *message)
180 {
181         GMimeMessagePart *part;
182         
183         part = g_mime_message_part_new (subtype);
184         part->message = message;
185         g_object_ref (message);
186         
187         return part;
188 }
189
190
191 /**
192  * g_mime_message_part_set_message:
193  * @part: message part
194  * @message: message
195  *
196  * Sets the @message object on the message part object @part.
197  **/
198 void
199 g_mime_message_part_set_message (GMimeMessagePart *part, GMimeMessage *message)
200 {
201         g_return_if_fail (GMIME_IS_MESSAGE_PART (part));
202         
203         if (message)
204                 g_object_ref (message);
205         
206         if (part->message)
207                 g_object_unref (part->message);
208         
209         part->message = message;
210 }
211
212
213 /**
214  * g_mime_message_part_get_message:
215  * @part: message part
216  *
217  * Gets the message object on the message part object @part.
218  *
219  * Returns: the message part contained within @part.
220  **/
221 GMimeMessage *
222 g_mime_message_part_get_message (GMimeMessagePart *part)
223 {
224         g_return_val_if_fail (GMIME_IS_MESSAGE_PART (part), NULL);
225         
226         return part->message;
227 }