l10n: Updates to Portuguese (Brazilian) (pt_BR) translation
[siplcs.git] / src / core / sipe-mime.c
blobae1f538d6208bbbef0b5b761b2591c26f2922df1
1 /**
2 * @file sipe-mime.c
4 * pidgin-sipe
6 * Copyright (C) 2010 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <string.h>
25 #include <glib.h>
26 #include <gmime/gmime.h>
28 #include "sipe-common.h"
29 #include "sipe-mime.h"
31 #include "sipe-backend.h"
33 struct gmime_callback_data {
34 sipe_mime_parts_cb callback;
35 gpointer user_data;
38 static void gmime_callback(SIPE_UNUSED_PARAMETER GMimeObject *parent,
39 GMimeObject *part,
40 gpointer user_data)
42 GMimeDataWrapper *data = g_mime_part_get_content_object((GMimePart *)part);
44 if (data) {
45 GMimeStream *stream = g_mime_data_wrapper_get_stream(data);
47 if (stream) {
48 ssize_t length = g_mime_stream_length(stream);
50 if (length != -1) {
51 gchar *content = g_malloc(length + 1);
53 if (g_mime_stream_read(stream, content, length) == length) {
54 struct gmime_callback_data *cd = user_data;
55 gchar *type_name = g_mime_content_type_to_string(
56 g_mime_object_get_content_type(part));
58 SIPE_DEBUG_INFO("sipe_mime_parts_foreach: type '%s' length %" G_GSSIZE_FORMAT, type_name, length);
60 (*(cd->callback))(cd->user_data, type_name, content, length);
62 g_free(type_name);
64 g_free(content);
66 g_object_unref(stream);
71 void sipe_mime_parts_foreach(const gchar *type,
72 const gchar *body,
73 sipe_mime_parts_cb callback,
74 gpointer user_data)
76 gchar *doc = g_strdup_printf("Content-Type: %s\r\n\r\n%s", type, body);
77 GMimeStream *stream = g_mime_stream_mem_new_with_buffer(doc, strlen(doc));
79 if (stream) {
80 GMimeParser *parser = g_mime_parser_new_with_stream(stream);
81 GMimeMultipart *multipart = (GMimeMultipart *)g_mime_parser_construct_part(parser);
83 if (multipart) {
84 struct gmime_callback_data cd = {callback, user_data};
86 SIPE_DEBUG_INFO("sipe_mime_parts_foreach: %d parts", g_mime_multipart_get_count(multipart));
88 g_mime_multipart_foreach(multipart, gmime_callback, &cd);
91 g_object_unref(parser);
92 g_object_unref(stream);
94 g_free(doc);
98 Local Variables:
99 mode: c
100 c-file-style: "bsd"
101 indent-tabs-mode: t
102 tab-width: 8
103 End: