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, int *part_count,
27 void (*show_part) (GMimeObject *part, int *part_count))
29 if (GMIME_IS_MULTIPART (part)) {
30 GMimeMultipart *multipart = GMIME_MULTIPART (part);
33 *part_count = *part_count + 1;
34 (*show_part) (part, part_count);
36 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
37 show_message_part (g_mime_multipart_get_part (multipart, i),
38 part_count, show_part);
43 if (GMIME_IS_MESSAGE_PART (part)) {
44 GMimeMessage *mime_message;
46 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
48 show_message_part (g_mime_message_get_mime_part (mime_message),
49 part_count, show_part);
54 if (! (GMIME_IS_PART (part))) {
55 fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
56 g_type_name (G_OBJECT_TYPE (part)));
60 *part_count = *part_count + 1;
62 (*show_part) (part, part_count);
66 show_message_body (const char *filename,
67 void (*show_part) (GMimeObject *part, int *part_count))
69 GMimeStream *stream = NULL;
70 GMimeParser *parser = NULL;
71 GMimeMessage *mime_message = NULL;
72 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
76 file = fopen (filename, "r");
78 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
79 ret = NOTMUCH_STATUS_FILE_ERROR;
83 stream = g_mime_stream_file_new (file);
84 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
86 parser = g_mime_parser_new_with_stream (stream);
88 mime_message = g_mime_parser_construct_message (parser);
90 show_message_part (g_mime_message_get_mime_part (mime_message),
91 &part_count, show_part);
95 g_object_unref (mime_message);
98 g_object_unref (parser);
101 g_object_unref (stream);
110 show_one_part_output (GMimeObject *part)
112 GMimeStream *stream_filter = NULL;
113 GMimeDataWrapper *wrapper;
114 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
116 stream_filter = g_mime_stream_filter_new(stream_stdout);
117 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
118 if (wrapper && stream_filter)
119 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
121 g_object_unref(stream_filter);
125 show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
127 if (GMIME_IS_MULTIPART (part)) {
128 GMimeMultipart *multipart = GMIME_MULTIPART (part);
131 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
132 show_one_part_worker (g_mime_multipart_get_part (multipart, i),
133 part_count, desired_part);
138 if (GMIME_IS_MESSAGE_PART (part)) {
139 GMimeMessage *mime_message;
141 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
143 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
144 part_count, desired_part);
149 if (! (GMIME_IS_PART (part)))
152 *part_count = *part_count + 1;
154 if (*part_count == desired_part)
155 show_one_part_output (part);
159 show_one_part (const char *filename, int part)
161 GMimeStream *stream = NULL;
162 GMimeParser *parser = NULL;
163 GMimeMessage *mime_message = NULL;
164 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
168 file = fopen (filename, "r");
170 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
171 ret = NOTMUCH_STATUS_FILE_ERROR;
175 stream = g_mime_stream_file_new (file);
176 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
178 parser = g_mime_parser_new_with_stream (stream);
180 mime_message = g_mime_parser_construct_message (parser);
182 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
187 g_object_unref (mime_message);
190 g_object_unref (parser);
193 g_object_unref (stream);