2.0_alpha release commit
[framework/messaging/email-service.git] / utilities / test-application / testapp-mailbox.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /* common header */
25 #include <stdio.h>
26 #include <string.h>
27
28
29 /* open header */
30 #include <glib.h>
31
32 #include "email-api-account.h"
33 #include "email-api-network.h"
34 #include "email-api-mailbox.h"
35
36 /* internal header */
37 #include "testapp-utility.h"
38 #include "testapp-mailbox.h"
39
40 static gboolean testapp_print_mailbox_list(email_mailbox_t *input_mailbox_list, int input_count)
41 {
42         int i;
43         char time_string[40] = { 0, };
44
45
46
47         testapp_print("There are %d mailboxes\n", input_count);
48
49         testapp_print("============================================================================\n");
50         testapp_print("id   a_id  name\t\t\t         alias\t\t unread\t total\t total_on_ server\tmailbox_type\t last_sync_time\n");
51         testapp_print("============================================================================\n");
52         if ( input_count == 0 ) {
53                 testapp_print("No mailbox is matched\n");
54         }
55         else {
56                 for(i=0;i<input_count;i++) {
57                         strftime(time_string, 40, "%Y-%m-%d %H:%M:%S", localtime(&(input_mailbox_list[i].last_sync_time)));
58                         testapp_print("[%2d]", input_mailbox_list[i].mailbox_id);
59                         testapp_print("  %2d  [%2d]\t[%-12s]  ", input_mailbox_list[i].account_id, input_mailbox_list[i].mailbox_id, input_mailbox_list[i].alias);
60                         testapp_print(" %3d\t %3d\t %3d\t %3d\t %s\n"
61                                         , input_mailbox_list[i].unread_count
62                                         , input_mailbox_list[i].total_mail_count_on_local
63                                         , input_mailbox_list[i].total_mail_count_on_server
64                                         , input_mailbox_list[i].mailbox_type
65                                         , time_string);
66                 }
67         }
68         testapp_print("============================================================================\n");
69
70         return FALSE;
71 }
72
73 static gboolean testapp_test_add_mailbox()
74 {
75         email_mailbox_t  mailbox;
76         int account_id,mailbox_type = 0;
77         int local_yn = 0;
78         char arg[500];
79         int ret;
80     unsigned handle;
81     int result_from_scanf = 0;
82
83         memset(arg, 0x00, 500);
84         testapp_print("\n> Enter mailbox name: ");
85         result_from_scanf = scanf("%s",arg);
86         mailbox.mailbox_name = strdup(arg);
87         
88         memset(arg, 0x00, 500);
89         testapp_print("> Enter mailbox alias name: ");
90         result_from_scanf = scanf("%s",arg);
91         mailbox.alias = strdup(arg);
92
93         testapp_print("> Enter account id: ");
94         result_from_scanf = scanf("%d", &account_id);
95         mailbox.account_id = account_id;
96
97         testapp_print("> Enter local_yn (1/0): ");
98         result_from_scanf = scanf("%d", &local_yn);
99         mailbox.local= local_yn;        
100
101
102         testapp_print("> Enter mailbox type: ");
103         result_from_scanf = scanf("%d", &mailbox_type);
104         mailbox.mailbox_type= mailbox_type;
105
106         ret = email_add_mailbox(&mailbox, local_yn?0:1, &handle);
107
108         if (ret  < 0) {
109                 testapp_print("\n email_add_mailbox failed");
110         }
111         else {
112                 testapp_print("\n email_add_mailbox succeed : handle[%d], mailbox_id [%d]\n", handle, mailbox.mailbox_id);
113         }
114         
115         return FALSE;
116 }
117
118 static gboolean testapp_test_delete_mailbox()
119 {
120         int mailbox_id = 0;
121         int on_server = 0;
122         int ret;
123         unsigned handle;
124         int result_from_scanf = 0;
125
126         testapp_print("\n> Enter mailbox id:");
127         result_from_scanf = scanf("%d", &mailbox_id);
128         
129         testapp_print("> Enter on_server (1/0): ");
130         result_from_scanf = scanf("%d", &on_server);
131
132         ret = email_delete_mailbox(mailbox_id, on_server, &handle);
133
134         if ( ret < 0) {
135                 testapp_print("\n email_delete_mailbox failed");
136         }
137         else {
138                 testapp_print("\n email_delete_mailbox succeed : handle[%d]\n", handle);
139         }
140         
141         return FALSE;
142
143 }
144
145 static gboolean testapp_test_rename_mailbox()
146 {
147         testapp_print ("testapp_test_rename_mailbox\n");
148         int mailbox_id;
149         char mailbox_name[500] = { 0, };
150         char mailbox_alias[500] = { 0, };
151         int err;
152         int result_from_scanf = 0;
153         unsigned handle = 0;
154         
155         testapp_print("> Enter mailbox id: ");
156         result_from_scanf = scanf("%d", &mailbox_id);
157
158         testapp_print("> Enter new mailbox name: ");
159         result_from_scanf = scanf("%s", mailbox_name);
160
161         testapp_print("> Enter new mailbox name: ");
162         result_from_scanf = scanf("%s", mailbox_alias);
163
164         
165         if ( (err = email_rename_mailbox(mailbox_id, mailbox_name, mailbox_alias, true, &handle)) < 0) {
166                 testapp_print("\n email_rename_mailbox failed[%d]\n", err);
167         }
168         else {
169                 testapp_print("\n email_rename_mailbox succeed\n");
170         }
171
172         return FALSE;
173 }
174
175 static gboolean testapp_test_get_imap_mailbox_list()
176 {
177         int account_id = 0;
178         unsigned handle = 0;
179         int result_from_scanf = 0;
180         
181         testapp_print("> Enter account id: ");
182         result_from_scanf = scanf("%d", &account_id);
183         
184         if(  email_sync_imap_mailbox_list(account_id, &handle) < 0)
185                 testapp_print("email_sync_imap_mailbox_list failed");
186
187         return FALSE;
188
189 }
190
191 static gboolean testapp_test_get_mailbox_by_type ()
192 {
193
194         int account_id =0;      
195         int err_code = EMAIL_ERROR_NONE;
196         int result_from_scanf = 0;
197         email_mailbox_t *mailbox =NULL;
198         email_mailbox_type_e mailbox_type =0;
199         
200         testapp_print("\n > Enter account id: ");
201         result_from_scanf = scanf("%d", &account_id);
202
203         testapp_print("\n > Enter mailbox_type: ");
204         result_from_scanf = scanf("%d", (int*)&mailbox_type);
205
206         if( (err_code = email_get_mailbox_by_mailbox_type(account_id,mailbox_type,&mailbox)) < 0) {
207                 testapp_print("   email_get_mailbox_by_mailbox_type error : %d\n",err_code);
208                 return false ;
209         }
210
211         testapp_print_mailbox_list(mailbox, 1);
212         
213         email_free_mailbox(&mailbox, 1);
214         return FALSE;
215 }
216
217 static gboolean testapp_test_set_mailbox_type()
218 {
219         int  mailbox_id = 0;
220         int  mailbox_type = 0;
221         int  err_code = EMAIL_ERROR_NONE;
222         int  result_from_scanf = 0;
223
224         testapp_print("\n > Enter mailbox id : ");
225         result_from_scanf = scanf("%d", &mailbox_id);
226
227         testapp_print("\n > Enter mailbox type : ");
228         result_from_scanf = scanf("%d", &mailbox_type);
229
230         if( (err_code = email_set_mailbox_type(mailbox_id, mailbox_type) ) != EMAIL_ERROR_NONE) {
231                 testapp_print("\nemail_set_mailbox_type error : %d\n", err_code);
232         }
233
234         return FALSE;
235 }
236
237 static gboolean testapp_test_set_mail_slot_size ()
238 {
239         int account_id = 0;
240         int mailbox_id = 0;
241         int mail_slot_size = 0;
242         int err_code = EMAIL_ERROR_NONE;
243         int result_from_scanf = 0;
244         
245         testapp_print("\n > Enter account id (0: All account): ");
246         result_from_scanf = scanf("%d", &account_id);
247
248         testapp_print("\n> Enter mailbox id (0 : All mailboxes):");
249         result_from_scanf = scanf("%d", &mailbox_id);
250
251         testapp_print("\n > Enter mailbox slot size: ");
252         result_from_scanf = scanf("%d", &mail_slot_size);
253
254         if( (err_code = email_set_mail_slot_size(account_id, mailbox_id, mail_slot_size) ) < 0) {
255                 testapp_print("   testapp_test_set_mail_slot_size error : %d\n", err_code);
256                 return false ;
257         }
258
259         return FALSE;
260 }
261
262 static gboolean testapp_test_get_mailbox_list ()
263 {
264         int result_from_scanf = 0;
265         int account_id =0;
266         int mailbox_sync_type;
267         int count = 0;
268         int error_code = EMAIL_ERROR_NONE;
269         email_mailbox_t *mailbox_list=NULL;
270         testapp_print("\n > Enter account id: ");
271         result_from_scanf = scanf("%d", &account_id);
272         testapp_print("\n > Enter mailbox_sync_type\n[-1 :for all mailboxes, 0 : for mailboxes from server, 1 : local mailboxes\n : ");
273         result_from_scanf = scanf("%d", &mailbox_sync_type);
274
275         if((error_code = email_get_mailbox_list(account_id, mailbox_sync_type, &mailbox_list, &count)) < 0) {
276                 testapp_print("   email_get_mailbox_list error %d\n", error_code);
277                 return false ;
278         }
279
280         testapp_print_mailbox_list(mailbox_list, count);
281
282         email_free_mailbox(&mailbox_list, count);
283
284         if((error_code = email_get_mailbox_list_ex(account_id, mailbox_sync_type, 1, &mailbox_list, &count)) < 0) {
285                 testapp_print("   email_get_mailbox_list_ex error %d\n", error_code);
286                 return false ;
287         }
288
289         testapp_print_mailbox_list(mailbox_list, count);
290
291         email_free_mailbox(&mailbox_list, count);
292         return FALSE;
293 }
294
295 static gboolean testapp_test_sync_mailbox()
296 {
297         int result_from_scanf = 0;
298         int account_id = 0;
299         unsigned handle = 0;
300         int mailbox_id = 0;
301
302         testapp_print("\n > Enter Account id (0: for all account) : ");
303         result_from_scanf = scanf("%d",&account_id);
304
305         testapp_print("\n > Enter Mailbox id (0: for all mailboxes) : ");
306         result_from_scanf = scanf("%d",&mailbox_id);
307
308         if(account_id == ALL_ACCOUNT) {
309                 if(email_sync_header_for_all_account(&handle) < 0)
310                         testapp_print("\n email_sync_header_for_all_account failed\n");
311                 else
312                         testapp_print("\n email_sync_header_for_all_account success. Handle[%d]\n", handle);
313         }
314         else {
315                 if(email_sync_header(account_id, mailbox_id, &handle) < 0)
316                         testapp_print("\n email_sync_header failed\n");
317                 else
318                         testapp_print("\n email_sync_header success. Handle[%d]\n", handle);
319         }
320
321         return FALSE;
322 }
323
324 static gboolean testapp_test_stamp_sync_time()
325 {
326         int result_from_scanf;
327         int input_mailbox_id = 0;
328
329         testapp_print("\n > Enter Mailbox id : ");
330         result_from_scanf = scanf("%d",&input_mailbox_id);
331
332         email_stamp_sync_time_of_mailbox(input_mailbox_id);
333
334         return FALSE;
335 }
336
337 static gboolean testapp_test_interpret_command (int menu_number)
338 {
339         gboolean go_to_loop = TRUE;
340         
341         switch (menu_number) {
342                 case 1:
343                         testapp_test_add_mailbox();
344                         break;
345
346                 case 2:
347                         testapp_test_delete_mailbox();
348                         break;
349
350                 case 3:
351                         testapp_test_rename_mailbox();
352                         break;
353
354                 case 4:
355                         testapp_test_get_imap_mailbox_list();
356                         break;                  
357
358                 case 7:
359                         testapp_test_get_mailbox_by_type();
360                         break;
361
362                 case 8:
363                         testapp_test_set_mailbox_type();
364                         break;  
365
366                 case 9:
367                         testapp_test_set_mail_slot_size();
368                         break;  
369
370                 case 10:
371                         testapp_test_get_mailbox_list ();
372                         break;
373
374                 case 11:
375                         testapp_test_sync_mailbox();
376                         break;
377
378                 case 12:
379                         testapp_test_stamp_sync_time();
380                         break;
381
382                 case 0:
383                         go_to_loop = FALSE;
384                         break;
385                 default:
386                         break;
387         }
388
389         return go_to_loop;
390 }
391
392 void email_test_mailbox_main()
393 {
394         gboolean go_to_loop = TRUE;
395         int menu_number = 0;
396         int result_from_scanf = 0;
397         
398         while (go_to_loop) {
399                 testapp_show_menu (EMAIL_MAILBOX_MENU);
400                 testapp_show_prompt (EMAIL_MAILBOX_MENU);
401                         
402                 result_from_scanf = scanf("%d", &menu_number);
403
404                 go_to_loop = testapp_test_interpret_command (menu_number);
405         }
406 }
407