message service dependency removed
[platform/core/messaging/email-service.git] / test / testapp-gmime.c
1 #include <glib.h>
2 #include <gmime/gmime.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <errno.h>
12
13 #include "testapp-utility.h"
14 #include "testapp-gmime.h"
15
16
17 #define TEST_EML_PATH "/tmp/test.eml"
18 /*
19 static void print_depth(int depth)
20 {
21         int i;
22         for (i = 0; i < depth; i++)
23                 testapp_print("    \n");
24 }
25 */
26 /*
27 static void print_mime_struct(GMimeObject *part, int depth)
28 {
29         const GMimeContentType *type;
30         GMimeMultipart *multipart;
31         GMimeObject *subpart;
32         int i, n;
33
34         print_depth(depth);
35         type = g_mime_object_get_content_type(part);
36         testapp_print("Content-Type: %s/%s\n", type->type, type->subtype);
37
38         if (GMIME_IS_MULTIPART(part)) {
39                 multipart = (GMimeMultipart *)part;
40
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);
46                 }
47         }
48 }
49 */
50 static void
51 test_eml_parsing_foreach_callback(GMimeObject *parent, GMimeObject *part, gpointer user_data)
52 {
53         int *cnt = user_data;
54
55         (*cnt)++;
56
57         if (GMIME_IS_PART(part)) {
58                 testapp_print ("Part\n");
59                 GMimePart *leaf_part = NULL;
60                 leaf_part = (GMimePart *)part;
61
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));
66
67                 GMimeContentEncoding enc = g_mime_part_get_content_encoding(leaf_part);
68                 switch (enc) {
69                 case GMIME_CONTENT_ENCODING_DEFAULT:
70                         testapp_print("Encoding:ENCODING_DEFAULT\n");
71                         break;
72                 case GMIME_CONTENT_ENCODING_7BIT:
73                         testapp_print("Encoding:ENCODING_7BIT\n");
74                         break;
75                 case GMIME_CONTENT_ENCODING_8BIT:
76                         testapp_print("Encoding:ENCODING_8BIT\n");
77                         break;
78                 case GMIME_CONTENT_ENCODING_BINARY:
79                         testapp_print("Encoding:ENCODING_BINARY\n");
80                         break;
81                 case GMIME_CONTENT_ENCODING_BASE64:
82                         testapp_print("Encoding:ENCODING_BASE64\n");
83                         break;
84                 case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
85                         testapp_print("Encoding:ENCODING_QUOTEDPRINTABLE\n");
86                         break;
87                 case GMIME_CONTENT_ENCODING_UUENCODE:
88                         testapp_print("Encoding:ENCODING_UUENCODE\n");
89                         break;
90
91                 default:
92                         break;
93                 }
94
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);
97
98                 if (data) {
99                         char tmp_path[50] = {0,};
100                         snprintf(tmp_path, sizeof(tmp_path), "/tmp/gmime_content%d", *cnt);
101
102                         int fd;
103                         if ((fd = open(tmp_path, O_RDWR|O_CREAT, 0)) == -1) {
104                                 testapp_print ("open fail\n");
105                                 return;
106                         }
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);
111                 }
112         }
113 }
114
115 static gboolean testapp_test_gmime_eml_parsing(void)
116 {
117         GMimeMessage *message;
118         GMimeParser *parser;
119         GMimeStream *stream;
120         int fd;
121         int count = 0;
122
123         if ((fd = open(TEST_EML_PATH, O_RDONLY, 0)) == -1) {
124                 testapp_print ("open fail\n");
125                 return FALSE;
126         }
127
128         /* init the gmime library */
129         g_mime_init(0);
130
131         /* create a stream to read from the file descriptor */
132         stream = g_mime_stream_fs_new(fd);
133
134         /* create a new parser object to parse the stream */
135         parser = g_mime_parser_new_with_stream(stream);
136
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);
139
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");
144                 return FALSE;
145         }
146
147         /* free the parser (and the stream) */
148         g_object_unref(parser);
149
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));
155
156         GMimeObject *po = (GMimeObject *)message;
157         testapp_print("Header String:%s\n\n\n\n", g_mime_header_list_to_string(po->headers));
158
159         g_mime_message_foreach(message, test_eml_parsing_foreach_callback, &count);
160         return TRUE;
161 }
162
163 static gboolean testapp_test_interpret_command(int menu_number)
164 {
165         gboolean go_to_loop = TRUE;
166
167         switch (menu_number) {
168         case 1:
169                 testapp_test_gmime_eml_parsing();
170                 break;
171         case 0:
172                 go_to_loop = FALSE;
173                 break;
174         default:
175                 break;
176         }
177
178         return go_to_loop;
179 }
180
181 void testapp_gmime_main()
182 {
183         gboolean go_to_loop = TRUE;
184         int menu_number = 0;
185         while (go_to_loop) {
186                 testapp_show_menu(EMAIL_GMIME_MENU);
187                 testapp_show_prompt(EMAIL_GMIME_MENU);
188
189                 if (0 >= scanf("%d", &menu_number)) {
190                         testapp_print ("input fail\n");
191                         continue;
192                 }
193
194                 go_to_loop = testapp_test_interpret_command(menu_number);
195         }
196 }