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 void (*show_part) (GMimeObject *part, int *part_count, int first),
29 void (*show_part_end) (GMimeObject *part),
32 if (GMIME_IS_MULTIPART (part)) {
33 GMimeMultipart *multipart = GMIME_MULTIPART (part);
36 *part_count = *part_count + 1;
37 (*show_part) (part, part_count, first);
39 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
40 show_message_part (g_mime_multipart_get_part (multipart, i),
41 part_count, show_part, show_part_end, i == 0);
45 (*show_part_end) (part);
50 if (GMIME_IS_MESSAGE_PART (part)) {
51 GMimeMessage *mime_message;
53 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
55 show_message_part (g_mime_message_get_mime_part (mime_message),
56 part_count, show_part, show_part_end, first);
61 if (! (GMIME_IS_PART (part))) {
62 fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
63 g_type_name (G_OBJECT_TYPE (part)));
67 *part_count = *part_count + 1;
69 (*show_part) (part, part_count, first);
71 (*show_part_end) (part);
75 show_message_body (const char *filename,
76 void (*show_part) (GMimeObject *part, int *part_count, int first),
77 void (*show_part_end) (GMimeObject *part))
79 GMimeStream *stream = NULL;
80 GMimeParser *parser = NULL;
81 GMimeMessage *mime_message = NULL;
82 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
86 file = fopen (filename, "r");
88 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
89 ret = NOTMUCH_STATUS_FILE_ERROR;
93 stream = g_mime_stream_file_new (file);
94 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
96 parser = g_mime_parser_new_with_stream (stream);
98 mime_message = g_mime_parser_construct_message (parser);
100 show_message_part (g_mime_message_get_mime_part (mime_message),
101 &part_count, show_part, show_part_end, TRUE);
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 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
142 show_one_part_worker (g_mime_multipart_get_part (multipart, i),
143 part_count, desired_part);
148 if (GMIME_IS_MESSAGE_PART (part)) {
149 GMimeMessage *mime_message;
151 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
153 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
154 part_count, desired_part);
159 if (! (GMIME_IS_PART (part)))
162 *part_count = *part_count + 1;
164 if (*part_count == desired_part)
165 show_one_part_output (part);
169 show_one_part (const char *filename, int part)
171 GMimeStream *stream = NULL;
172 GMimeParser *parser = NULL;
173 GMimeMessage *mime_message = NULL;
174 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
178 file = fopen (filename, "r");
180 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
181 ret = NOTMUCH_STATUS_FILE_ERROR;
185 stream = g_mime_stream_file_new (file);
186 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
188 parser = g_mime_parser_new_with_stream (stream);
190 mime_message = g_mime_parser_construct_message (parser);
192 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
197 g_object_unref (mime_message);
200 g_object_unref (parser);
203 g_object_unref (stream);