Tizen 2.0 Release
[platform/core/messaging/email-service.git] / utilities / test-application / testapp-mailbox.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 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     int 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         int 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         int 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         int 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_set_local_mailbox()
192 {
193         int mailbox_id = 0;
194         int is_local_mailbox = 0;
195         int result_from_scanf = 0;
196
197         testapp_print("> Enter mailbox id: ");
198         result_from_scanf = scanf("%d", &mailbox_id);
199
200         testapp_print("> Enter local: ");
201         result_from_scanf = scanf("%d", &is_local_mailbox);
202
203         if( email_set_local_mailbox(mailbox_id, is_local_mailbox) < 0)
204                 testapp_print("email_set_local_mailbox failed");
205
206         return FALSE;
207 }
208
209 static gboolean testapp_test_delete_mailbox_ex()
210 {
211         int  err = EMAIL_ERROR_NONE;
212         int  mailbox_id_count = 0 ;
213         int *mailbox_id_array = NULL;
214         int  account_id = 0;
215         int  on_server = 0;
216         int  handle = 0;
217         int  i = 0;
218         int  result_from_scanf = 0;
219
220         testapp_print("\n > Enter account_id: ");
221         result_from_scanf = scanf("%d", &account_id);
222
223         testapp_print("\n > Enter mailbox_id_count: ");
224         result_from_scanf = scanf("%d", &mailbox_id_count);
225
226         testapp_print("\n > Enter on_server: ");
227         result_from_scanf = scanf("%d", &on_server);
228
229         mailbox_id_count = (mailbox_id_count < 5000)?mailbox_id_count:5000;
230
231         if(mailbox_id_count > 0) {
232                 mailbox_id_array = malloc(sizeof(int) * mailbox_id_count);
233         }
234
235         for(i = 0; i < mailbox_id_count; i++) {
236                 testapp_print("\n > Enter mailbox id: ");
237                 result_from_scanf = scanf("%d", (mailbox_id_array + i));
238         }
239
240         err = email_delete_mailbox_ex(account_id, mailbox_id_array, mailbox_id_count, on_server, &handle);
241
242         testapp_print("\nemail_delete_mailbox_ex returns [%d], handle [%d] \n", err, handle);
243         return 0;
244 }
245
246 static gboolean testapp_test_get_mailbox_by_type()
247 {
248
249         int account_id =0;      
250         int err_code = EMAIL_ERROR_NONE;
251         int result_from_scanf = 0;
252         email_mailbox_t *mailbox =NULL;
253         email_mailbox_type_e mailbox_type =0;
254         
255         testapp_print("\n > Enter account id: ");
256         result_from_scanf = scanf("%d", &account_id);
257
258         testapp_print("\n > Enter mailbox_type: ");
259         result_from_scanf = scanf("%d", (int*)&mailbox_type);
260
261         if( (err_code = email_get_mailbox_by_mailbox_type(account_id,mailbox_type,&mailbox)) < 0) {
262                 testapp_print("   email_get_mailbox_by_mailbox_type error : %d\n",err_code);
263                 return false ;
264         }
265
266         testapp_print_mailbox_list(mailbox, 1);
267         
268         email_free_mailbox(&mailbox, 1);
269         return FALSE;
270 }
271
272 static gboolean testapp_test_set_mailbox_type()
273 {
274         int  mailbox_id = 0;
275         int  mailbox_type = 0;
276         int  err_code = EMAIL_ERROR_NONE;
277         int  result_from_scanf = 0;
278
279         testapp_print("\n > Enter mailbox id : ");
280         result_from_scanf = scanf("%d", &mailbox_id);
281
282         testapp_print("\n > Enter mailbox type : ");
283         result_from_scanf = scanf("%d", &mailbox_type);
284
285         if( (err_code = email_set_mailbox_type(mailbox_id, mailbox_type) ) != EMAIL_ERROR_NONE) {
286                 testapp_print("\nemail_set_mailbox_type error : %d\n", err_code);
287         }
288
289         return FALSE;
290 }
291
292 static gboolean testapp_test_set_mail_slot_size ()
293 {
294         int account_id = 0;
295         int mailbox_id = 0;
296         int mail_slot_size = 0;
297         int err_code = EMAIL_ERROR_NONE;
298         int result_from_scanf = 0;
299         
300         testapp_print("\n > Enter account id (0: All account): ");
301         result_from_scanf = scanf("%d", &account_id);
302
303         testapp_print("\n> Enter mailbox id (0 : All mailboxes):");
304         result_from_scanf = scanf("%d", &mailbox_id);
305
306         testapp_print("\n > Enter mailbox slot size: ");
307         result_from_scanf = scanf("%d", &mail_slot_size);
308
309         if( (err_code = email_set_mail_slot_size(account_id, mailbox_id, mail_slot_size) ) < 0) {
310                 testapp_print("   testapp_test_set_mail_slot_size error : %d\n", err_code);
311                 return false ;
312         }
313
314         return FALSE;
315 }
316
317 static gboolean testapp_test_get_mailbox_list ()
318 {
319         int result_from_scanf = 0;
320         int account_id =0;
321         int mailbox_sync_type;
322         int count = 0;
323         int error_code = EMAIL_ERROR_NONE;
324         email_mailbox_t *mailbox_list=NULL;
325         testapp_print("\n > Enter account id: ");
326         result_from_scanf = scanf("%d", &account_id);
327         testapp_print("\n > Enter mailbox_sync_type\n[-1 :for all mailboxes, 0 : for mailboxes from server, 1 : local mailboxes\n : ");
328         result_from_scanf = scanf("%d", &mailbox_sync_type);
329
330         if((error_code = email_get_mailbox_list(account_id, mailbox_sync_type, &mailbox_list, &count)) < 0) {
331                 testapp_print("   email_get_mailbox_list error %d\n", error_code);
332                 return false ;
333         }
334
335         testapp_print_mailbox_list(mailbox_list, count);
336
337         email_free_mailbox(&mailbox_list, count);
338
339         if((error_code = email_get_mailbox_list_ex(account_id, mailbox_sync_type, 1, &mailbox_list, &count)) < 0) {
340                 testapp_print("   email_get_mailbox_list_ex error %d\n", error_code);
341                 return false ;
342         }
343
344         testapp_print_mailbox_list(mailbox_list, count);
345
346         email_free_mailbox(&mailbox_list, count);
347         return FALSE;
348 }
349
350 static gboolean testapp_test_sync_mailbox()
351 {
352         int result_from_scanf = 0;
353         int account_id = 0;
354         int handle = 0;
355         int mailbox_id = 0;
356
357         testapp_print("\n > Enter Account id (0: for all account) : ");
358         result_from_scanf = scanf("%d",&account_id);
359
360         testapp_print("\n > Enter Mailbox id (0: for all mailboxes) : ");
361         result_from_scanf = scanf("%d",&mailbox_id);
362
363         if(account_id == ALL_ACCOUNT) {
364                 if(email_sync_header_for_all_account(&handle) < 0)
365                         testapp_print("\n email_sync_header_for_all_account failed\n");
366                 else
367                         testapp_print("\n email_sync_header_for_all_account success. Handle[%d]\n", handle);
368         }
369         else {
370                 if(email_sync_header(account_id, mailbox_id, &handle) < 0)
371                         testapp_print("\n email_sync_header failed\n");
372                 else
373                         testapp_print("\n email_sync_header success. Handle[%d]\n", handle);
374         }
375
376         return FALSE;
377 }
378
379 static gboolean testapp_test_stamp_sync_time()
380 {
381         int result_from_scanf;
382         int input_mailbox_id = 0;
383
384         testapp_print("\n > Enter Mailbox id : ");
385         result_from_scanf = scanf("%d",&input_mailbox_id);
386
387         email_stamp_sync_time_of_mailbox(input_mailbox_id);
388
389         return FALSE;
390 }
391
392 static gboolean testapp_test_interpret_command (int menu_number)
393 {
394         gboolean go_to_loop = TRUE;
395         
396         switch (menu_number) {
397                 case 1:
398                         testapp_test_add_mailbox();
399                         break;
400
401                 case 2:
402                         testapp_test_delete_mailbox();
403                         break;
404
405                 case 3:
406                         testapp_test_rename_mailbox();
407                         break;
408
409                 case 4:
410                         testapp_test_get_imap_mailbox_list();
411                         break;
412
413                 case 5:
414                         testapp_test_set_local_mailbox();
415                         break;
416
417                 case 6:
418                         testapp_test_delete_mailbox_ex();
419                         break;
420
421                 case 7:
422                         testapp_test_get_mailbox_by_type();
423                         break;
424
425                 case 8:
426                         testapp_test_set_mailbox_type();
427                         break;  
428
429                 case 9:
430                         testapp_test_set_mail_slot_size();
431                         break;  
432
433                 case 10:
434                         testapp_test_get_mailbox_list ();
435                         break;
436
437                 case 11:
438                         testapp_test_sync_mailbox();
439                         break;
440
441                 case 12:
442                         testapp_test_stamp_sync_time();
443                         break;
444
445                 case 0:
446                         go_to_loop = FALSE;
447                         break;
448                 default:
449                         break;
450         }
451
452         return go_to_loop;
453 }
454
455 void email_test_mailbox_main()
456 {
457         gboolean go_to_loop = TRUE;
458         int menu_number = 0;
459         int result_from_scanf = 0;
460         
461         while (go_to_loop) {
462                 testapp_show_menu (EMAIL_MAILBOX_MENU);
463                 testapp_show_prompt (EMAIL_MAILBOX_MENU);
464                         
465                 result_from_scanf = scanf("%d", &menu_number);
466
467                 go_to_loop = testapp_test_interpret_command (menu_number);
468         }
469 }
470