summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/tests/frm-tree.c
Unidiff
Diffstat (limited to 'kmicromail/libetpan/tests/frm-tree.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/tests/frm-tree.c234
1 files changed, 234 insertions, 0 deletions
diff --git a/kmicromail/libetpan/tests/frm-tree.c b/kmicromail/libetpan/tests/frm-tree.c
new file mode 100644
index 0000000..3a8464a
--- a/dev/null
+++ b/kmicromail/libetpan/tests/frm-tree.c
@@ -0,0 +1,234 @@
1#include "option-parser.h"
2#include "frm-common.h"
3
4#include <libetpan/libetpan.h>
5
6#include <stdlib.h>
7#include <string.h>
8
9#define DEST_CHARSET "iso-8859-1"
10
11/* display tree */
12
13static void
14display_sub_tree(MMAPString * prefix,
15 struct mailmessage_tree * msg_tree,
16 int level, int has_next, unsigned int * pcount)
17{
18 carray * list;
19 uint32_t cur;
20
21 if (msg_tree->node_msg != NULL) {
22 print_mail_info(prefix->str, msg_tree->node_msg);
23 (* pcount) ++;
24 }
25
26 list = msg_tree->node_children;
27
28 if (carray_count(list) != 0) {
29 char old_prefix[2];
30
31 if (level > 1) {
32 memcpy(old_prefix, prefix->str + prefix->len - 2, 2);
33 if (has_next)
34 memcpy(prefix->str + prefix->len - 2, "| ", 2);
35 else
36 memcpy(prefix->str + prefix->len - 2, " ", 2);
37 }
38 for(cur = 0 ; cur < carray_count(list) ; cur ++) {
39 int sub_has_next;
40
41 if (cur != carray_count(list) - 1) {
42 if (level > 0) {
43 if (mmap_string_append(prefix, "+-") == NULL)
44 return;
45 }
46 sub_has_next = 1;
47 }
48 else {
49 if (level > 0) {
50 if (mmap_string_append(prefix, "\\-") == NULL)
51 return;
52 }
53 sub_has_next = 0;
54 }
55
56 display_sub_tree(prefix, carray_get(list, cur),
57 level + 1, sub_has_next, pcount);
58
59 if (mmap_string_truncate(prefix, prefix->len - 2) == NULL) {
60 return;
61 }
62 }
63 if (level > 1) {
64 memcpy(prefix->str + prefix->len - 2, old_prefix, 2);
65 }
66 }
67}
68
69static void display_tree(struct mailmessage_tree * env_tree,
70 unsigned int * pcount)
71{
72 MMAPString * prefix;
73
74 prefix = mmap_string_new("");
75 if (prefix == NULL)
76 return;
77
78 display_sub_tree(prefix, env_tree, 0, 0, pcount);
79
80 mmap_string_free(prefix);
81}
82
83/* get the message list and display it */
84
85static void print_message_list(mailsession * session)
86{
87 int r;
88 struct mailmessage_list * env_list;
89 struct mailmessage_tree * env_tree;
90 unsigned int count;
91
92 /* get the list of messages numbers of the folder */
93
94 r = mailsession_get_messages_list(session, &env_list);
95 if (r != MAIL_NO_ERROR) {
96 printf("error message list\n");
97 goto err;
98 }
99
100 /* get fields content of these messages */
101
102 r = mailsession_get_envelopes_list(session, env_list);
103 if (r != MAIL_NO_ERROR) {
104 printf("error envelopes list\n");
105 goto free_msg_list;
106 }
107
108 /* build threads */
109
110 r = mail_build_thread(MAIL_THREAD_REFERENCES_NO_SUBJECT,
111 DEST_CHARSET,
112 env_list, &env_tree,
113 mailthread_tree_timecomp);
114 if (r != MAIL_NO_ERROR) {
115 printf("can't build tree\n");
116 goto free_msg_list;
117 }
118
119 /* display message tree */
120
121 count = 0;
122 display_tree(env_tree, &count);
123
124 printf(" %i messages\n", count);
125
126 /* free structure */
127
128 mailmessage_tree_free_recursive(env_tree);
129 mailmessage_list_free(env_list);
130
131 return;
132
133 free_msg_list:
134 mailmessage_list_free(env_list);
135 err:
136}
137
138int main(int argc, char ** argv)
139{
140 int r;
141 int driver;
142 char * server;
143 int port;
144 int connection_type;
145 char * user;
146 char * password;
147 int auth_type;
148 char * path;
149 char * cache_directory;
150 char * flags_directory;
151 struct mailstorage * storage;
152 int cached;
153 struct mailfolder * folder;
154
155 /* get options */
156
157 r = parse_options(argc, argv,
158 &driver, &server, &port, &connection_type,
159 &user, &password, &auth_type,
160 &path, &cache_directory, &flags_directory);
161
162 cached = (cache_directory != NULL);
163
164 /* build the storage structure */
165
166 storage = mailstorage_new(NULL);
167 if (storage == NULL) {
168 printf("error initializing storage\n");
169 goto free_opt;
170 }
171
172 r = init_storage(storage, driver, server, port, connection_type,
173 user, password, auth_type, path, cache_directory, flags_directory);
174 if (r != MAIL_NO_ERROR) {
175 printf("error initializing storage\n");
176 goto free_opt;
177 }
178
179 /* get the folder structure */
180
181 folder = mailfolder_new(storage, path, NULL);
182 if (folder == NULL) {
183 printf("error initializing folder\n");
184 goto free_storage;
185 }
186
187 r = mailfolder_connect(folder);
188 if (r != MAIL_NO_ERROR) {
189 printf("error initializing folder\n");
190 goto free_folder;
191 }
192
193 /* get and display the list of messages */
194
195 print_message_list(folder->fld_session);
196
197 mailfolder_free(folder);
198 mailstorage_free(storage);
199
200 if (server != NULL)
201 free(server);
202 if (user != NULL)
203 free(user);
204 if (password != NULL)
205 free(password);
206 if (path != NULL)
207 free(path);
208 if (cache_directory != NULL)
209 free(cache_directory);
210 if (flags_directory != NULL)
211 free(flags_directory);
212
213 return 0;
214
215 free_folder:
216 mailfolder_free(folder);
217 free_storage:
218 mailstorage_free(storage);
219 free_opt:
220 if (server != NULL)
221 free(server);
222 if (user != NULL)
223 free(user);
224 if (password != NULL)
225 free(password);
226 if (path != NULL)
227 free(path);
228 if (cache_directory != NULL)
229 free(cache_directory);
230 if (flags_directory != NULL)
231 free(flags_directory);
232 return -1;
233}
234