1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see http://www.gnu.org/licenses/ .
19 * Authors: Carl Worth <cworth@cworth.org>
20 * Keith Packard <keithp@keithp.com>
23 #include "notmuch-client.h"
26 show_message_part (GMimeObject *part,
28 const notmuch_show_format_t *format,
31 if (GMIME_IS_MULTIPART (part)) {
32 GMimeMultipart *multipart = GMIME_MULTIPART (part);
35 *part_count = *part_count + 1;
36 format->part (part, part_count, first);
38 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
39 show_message_part (g_mime_multipart_get_part (multipart, i),
40 part_count, format, i == 0);
44 format->part_end (part);
49 if (GMIME_IS_MESSAGE_PART (part)) {
50 GMimeMessage *mime_message;
52 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
54 show_message_part (g_mime_message_get_mime_part (mime_message),
55 part_count, format, first);
60 if (! (GMIME_IS_PART (part))) {
61 fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
62 g_type_name (G_OBJECT_TYPE (part)));
66 *part_count = *part_count + 1;
68 format->part (part, part_count, first);
70 format->part_end (part);
74 show_message_body (const char *filename,
75 const notmuch_show_format_t *format)
77 GMimeStream *stream = NULL;
78 GMimeParser *parser = NULL;
79 GMimeMessage *mime_message = NULL;
80 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
84 file = fopen (filename, "r");
86 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
87 ret = NOTMUCH_STATUS_FILE_ERROR;
91 stream = g_mime_stream_file_new (file);
92 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
94 parser = g_mime_parser_new_with_stream (stream);
96 mime_message = g_mime_parser_construct_message (parser);
98 show_message_part (g_mime_message_get_mime_part (mime_message),
105 g_object_unref (mime_message);
108 g_object_unref (parser);
111 g_object_unref (stream);
120 show_one_part_output (GMimeObject *part)
122 GMimeStream *stream_filter = NULL;
123 GMimeDataWrapper *wrapper;
124 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
126 stream_filter = g_mime_stream_filter_new(stream_stdout);
127 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
128 if (wrapper && stream_filter)
129 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
131 g_object_unref(stream_filter);
135 show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
137 if (GMIME_IS_MULTIPART (part)) {
138 GMimeMultipart *multipart = GMIME_MULTIPART (part);
141 *part_count = *part_count + 1;
143 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
144 show_one_part_worker (g_mime_multipart_get_part (multipart, i),
145 part_count, desired_part);
150 if (GMIME_IS_MESSAGE_PART (part)) {
151 GMimeMessage *mime_message;
153 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
155 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
156 part_count, desired_part);
161 if (! (GMIME_IS_PART (part)))
164 *part_count = *part_count + 1;
166 if (*part_count == desired_part)
167 show_one_part_output (part);
171 show_one_part (const char *filename, int part)
173 GMimeStream *stream = NULL;
174 GMimeParser *parser = NULL;
175 GMimeMessage *mime_message = NULL;
176 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
180 file = fopen (filename, "r");
182 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
183 ret = NOTMUCH_STATUS_FILE_ERROR;
187 stream = g_mime_stream_file_new (file);
188 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
190 parser = g_mime_parser_new_with_stream (stream);
192 mime_message = g_mime_parser_construct_message (parser);
194 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
199 g_object_unref (mime_message);
202 g_object_unref (parser);
205 g_object_unref (stream);