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"
25 typedef struct show_message_state {
28 } show_message_state_t;
31 show_message_part (GMimeObject *part,
32 show_message_state_t *state,
33 const notmuch_show_format_t *format,
34 notmuch_show_params_t *params,
38 state->part_count += 1;
40 if (! (GMIME_IS_PART (part) || GMIME_IS_MULTIPART (part) || GMIME_IS_MESSAGE_PART (part))) {
41 fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n",
42 g_type_name (G_OBJECT_TYPE (part)));
46 selected = (params->part <= 0 || state->part_count == params->part);
48 if (selected || state->in_zone) {
49 if (!first && (params->part <= 0 || state->in_zone))
50 fputs (format->part_sep, stdout);
52 if (format->part_start)
53 format->part_start (part, &(state->part_count));
54 format->part_content (part);
57 if (GMIME_IS_MULTIPART (part)) {
58 GMimeMultipart *multipart = GMIME_MULTIPART (part);
64 for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
65 show_message_part (g_mime_multipart_get_part (multipart, i),
66 state, format, params, i == 0);
72 } else if (GMIME_IS_MESSAGE_PART (part)) {
73 GMimeMessage *mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
78 show_message_part (g_mime_message_get_mime_part (mime_message),
79 state, format, params, TRUE);
85 if (selected || state->in_zone) {
87 format->part_end (part);
92 show_message_body (const char *filename,
93 const notmuch_show_format_t *format,
94 notmuch_show_params_t *params)
96 GMimeStream *stream = NULL;
97 GMimeParser *parser = NULL;
98 GMimeMessage *mime_message = NULL;
99 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
101 show_message_state_t state;
103 state.part_count = 0;
106 file = fopen (filename, "r");
108 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
109 ret = NOTMUCH_STATUS_FILE_ERROR;
113 stream = g_mime_stream_file_new (file);
114 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE);
116 parser = g_mime_parser_new_with_stream (stream);
118 mime_message = g_mime_parser_construct_message (parser);
120 show_message_part (g_mime_message_get_mime_part (mime_message),
128 g_object_unref (mime_message);
131 g_object_unref (parser);
134 g_object_unref (stream);