2 #include <gmime/gmime.h>
13 #include "testapp-utility.h"
14 #include "testapp-gmime.h"
17 #define TEST_EML_PATH "/tmp/test.eml"
19 static void print_depth(int depth)
22 for (i = 0; i < depth; i++)
27 static void print_mime_struct(GMimeObject *part, int depth)
29 const GMimeContentType *type;
30 GMimeMultipart *multipart;
35 type = g_mime_object_get_content_type(part);
36 testapp_print("Content-Type: %s/%s\n", type->type, type->subtype);
38 if (GMIME_IS_MULTIPART(part)) {
39 multipart = (GMimeMultipart *)part;
41 n = g_mime_multipart_get_count(multipart);
42 for (i = 0; i < n; i++) {
43 subpart = g_mime_multipart_get_part(multipart, i);
44 print_mime_struct(subpart, depth + 1);
45 g_object_unref(subpart);
51 test_eml_parsing_foreach_callback(GMimeObject *parent, GMimeObject *part, gpointer user_data)
57 if (GMIME_IS_PART(part)) {
58 testapp_print ("Part\n");
59 GMimePart *leaf_part = NULL;
60 leaf_part = (GMimePart *)part;
62 testapp_print("Content ID:%s\n", g_mime_part_get_content_id(leaf_part));
63 testapp_print("Description:%s\n", g_mime_part_get_content_description(leaf_part));
64 testapp_print("MD5:%s\n", g_mime_part_get_content_md5(leaf_part));
65 testapp_print("Location:%s\n", g_mime_part_get_content_location(leaf_part));
67 GMimeContentEncoding enc = g_mime_part_get_content_encoding(leaf_part);
69 case GMIME_CONTENT_ENCODING_DEFAULT:
70 testapp_print("Encoding:ENCODING_DEFAULT\n");
72 case GMIME_CONTENT_ENCODING_7BIT:
73 testapp_print("Encoding:ENCODING_7BIT\n");
75 case GMIME_CONTENT_ENCODING_8BIT:
76 testapp_print("Encoding:ENCODING_8BIT\n");
78 case GMIME_CONTENT_ENCODING_BINARY:
79 testapp_print("Encoding:ENCODING_BINARY\n");
81 case GMIME_CONTENT_ENCODING_BASE64:
82 testapp_print("Encoding:ENCODING_BASE64\n");
84 case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
85 testapp_print("Encoding:ENCODING_QUOTEDPRINTABLE\n");
87 case GMIME_CONTENT_ENCODING_UUENCODE:
88 testapp_print("Encoding:ENCODING_UUENCODE\n");
95 testapp_print("File name:%s\n\n\n", g_mime_part_get_filename(leaf_part));
96 GMimeDataWrapper *data = g_mime_part_get_content_object(leaf_part);
99 char tmp_path[50] = {0,};
100 snprintf(tmp_path, sizeof(tmp_path), "/tmp/gmime_content%d", *cnt);
103 if ((fd = open(tmp_path, O_RDWR|O_CREAT, 0)) == -1) {
104 testapp_print ("open fail\n");
107 GMimeStream *out_stream;
108 out_stream = g_mime_stream_fs_new(fd);
109 g_mime_data_wrapper_write_to_stream(data, out_stream);
110 g_object_unref(out_stream);
115 static gboolean testapp_test_gmime_eml_parsing(void)
117 GMimeMessage *message;
123 if ((fd = open(TEST_EML_PATH, O_RDONLY, 0)) == -1) {
124 testapp_print ("open fail\n");
128 /* init the gmime library */
131 /* create a stream to read from the file descriptor */
132 stream = g_mime_stream_fs_new(fd);
134 /* create a new parser object to parse the stream */
135 parser = g_mime_parser_new_with_stream(stream);
137 /* unref the stream (parser owns a ref, so this object does not actually get free'd until we destroy the parser) */
138 g_object_unref(stream);
140 /* parse the message from the stream */
141 message = g_mime_parser_construct_message(parser);
142 if (message == NULL){
143 testapp_print("g_mime_parser_construct_message error");
147 /* free the parser (and the stream) */
148 g_object_unref(parser);
150 testapp_print("Sender:%s\n", g_mime_message_get_sender(message));
151 testapp_print("Reply To:%s\n", g_mime_message_get_reply_to(message));
152 testapp_print("Subject:%s\n", g_mime_message_get_subject(message));
153 testapp_print("Date:%s\n", g_mime_message_get_date_as_string(message));
154 testapp_print("Message ID:%s\n", g_mime_message_get_message_id(message));
156 GMimeObject *po = (GMimeObject *)message;
157 testapp_print("Header String:%s\n\n\n\n", g_mime_header_list_to_string(po->headers));
159 g_mime_message_foreach(message, test_eml_parsing_foreach_callback, &count);
163 static gboolean testapp_test_interpret_command(int menu_number)
165 gboolean go_to_loop = TRUE;
167 switch (menu_number) {
169 testapp_test_gmime_eml_parsing();
181 void testapp_gmime_main()
183 gboolean go_to_loop = TRUE;
186 testapp_show_menu(EMAIL_GMIME_MENU);
187 testapp_show_prompt(EMAIL_GMIME_MENU);
189 if (0 >= scanf("%d", &menu_number)) {
190 testapp_print ("input fail\n");
194 go_to_loop = testapp_test_interpret_command(menu_number);