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 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
34 show_message_part (g_mime_multipart_get_part (multipart, i),
35 part_count, show_part);
40 if (GMIME_IS_MESSAGE_PART (part)) {
41 GMimeMessage *mime_message;
43 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
45 show_message_part (g_mime_message_get_mime_part (mime_message),
46 part_count, show_part);
51 if (! (GMIME_IS_PART (part))) {
52 fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
53 g_type_name (G_OBJECT_TYPE (part)));
57 *part_count = *part_count + 1;
59 (*show_part) (part, part_count);
63 show_message_body (const char *filename,
64 void (*show_part) (GMimeObject *part, int *part_count))
66 GMimeStream *stream = NULL;
67 GMimeParser *parser = NULL;
68 GMimeMessage *mime_message = NULL;
69 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
73 file = fopen (filename, "r");
75 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
76 ret = NOTMUCH_STATUS_FILE_ERROR;
80 stream = g_mime_stream_file_new (file);
81 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
83 parser = g_mime_parser_new_with_stream (stream);
85 mime_message = g_mime_parser_construct_message (parser);
87 show_message_part (g_mime_message_get_mime_part (mime_message),
88 &part_count, show_part);
92 g_object_unref (mime_message);
95 g_object_unref (parser);
98 g_object_unref (stream);
107 show_one_part_output (GMimeObject *part)
109 GMimeStream *stream_filter = NULL;
110 GMimeDataWrapper *wrapper;
111 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
113 stream_filter = g_mime_stream_filter_new(stream_stdout);
114 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
115 if (wrapper && stream_filter)
116 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
118 g_object_unref(stream_filter);
122 show_one_part_worker (GMimeObject *part, int *part_count, int desired_part)
124 if (GMIME_IS_MULTIPART (part)) {
125 GMimeMultipart *multipart = GMIME_MULTIPART (part);
128 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
129 show_one_part_worker (g_mime_multipart_get_part (multipart, i),
130 part_count, desired_part);
135 if (GMIME_IS_MESSAGE_PART (part)) {
136 GMimeMessage *mime_message;
138 mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
140 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
141 part_count, desired_part);
146 if (! (GMIME_IS_PART (part)))
149 *part_count = *part_count + 1;
151 if (*part_count == desired_part)
152 show_one_part_output (part);
156 show_one_part (const char *filename, int part)
158 GMimeStream *stream = NULL;
159 GMimeParser *parser = NULL;
160 GMimeMessage *mime_message = NULL;
161 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
165 file = fopen (filename, "r");
167 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
168 ret = NOTMUCH_STATUS_FILE_ERROR;
172 stream = g_mime_stream_file_new (file);
173 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
175 parser = g_mime_parser_new_with_stream (stream);
177 mime_message = g_mime_parser_construct_message (parser);
179 show_one_part_worker (g_mime_message_get_mime_part (mime_message),
184 g_object_unref (mime_message);
187 g_object_unref (parser);
190 g_object_unref (stream);