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,
33 if (! (GMIME_IS_PART (part) || GMIME_IS_MULTIPART (part) || GMIME_IS_MESSAGE_PART (part))) {
34 fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
35 g_type_name (G_OBJECT_TYPE (part)));
40 fputs (format->part_sep, stdout);
42 format->part (part, part_count);
44 if (GMIME_IS_MULTIPART (part)) {
45 GMimeMultipart *multipart = GMIME_MULTIPART (part);
48 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
49 show_message_part (g_mime_multipart_get_part (multipart, i),
50 part_count, format, i == 0);
53 } else if (GMIME_IS_MESSAGE_PART (part)) {
54 GMimeMessage *mime_message;
56 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
58 show_message_part (g_mime_message_get_mime_part (mime_message),
59 part_count, format, first);
63 format->part_end (part);
67 show_message_body (const char *filename,
68 const notmuch_show_format_t *format)
70 GMimeStream *stream = NULL;
71 GMimeParser *parser = NULL;
72 GMimeMessage *mime_message = NULL;
73 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
77 file = fopen (filename, "r");
79 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
80 ret = NOTMUCH_STATUS_FILE_ERROR;
84 stream = g_mime_stream_file_new (file);
85 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
87 parser = g_mime_parser_new_with_stream (stream);
89 mime_message = g_mime_parser_construct_message (parser);
91 show_message_part (g_mime_message_get_mime_part (mime_message),
98 g_object_unref (mime_message);
101 g_object_unref (parser);
104 g_object_unref (stream);
113 show_one_part_output (GMimeObject *part)
115 GMimeStream *stream_filter = NULL;
116 GMimeDataWrapper *wrapper;
117 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
119 stream_filter = g_mime_stream_filter_new(stream_stdout);
120 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
121 if (wrapper && stream_filter)
122 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
124 g_object_unref(stream_filter);
128 show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
130 if (GMIME_IS_MULTIPART (part)) {
131 GMimeMultipart *multipart = GMIME_MULTIPART (part);
134 *part_count = *part_count + 1;
136 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
137 show_one_part_worker (g_mime_multipart_get_part (multipart, i),
138 part_count, desired_part);
143 if (GMIME_IS_MESSAGE_PART (part)) {
144 GMimeMessage *mime_message;
146 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
148 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
149 part_count, desired_part);
154 if (! (GMIME_IS_PART (part)))
157 *part_count = *part_count + 1;
159 if (*part_count == desired_part)
160 show_one_part_output (part);
164 show_one_part (const char *filename, int part)
166 GMimeStream *stream = NULL;
167 GMimeParser *parser = NULL;
168 GMimeMessage *mime_message = NULL;
169 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
173 file = fopen (filename, "r");
175 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
176 ret = NOTMUCH_STATUS_FILE_ERROR;
180 stream = g_mime_stream_file_new (file);
181 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
183 parser = g_mime_parser_new_with_stream (stream);
185 mime_message = g_mime_parser_construct_message (parser);
187 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
192 g_object_unref (mime_message);
195 g_object_unref (parser);
198 g_object_unref (stream);