Tizen 2.0 Release
[platform/core/messaging/email-service.git] / utilities / test-application / testapp-others.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 /* common header */
24 #include <stdio.h>
25 #include <string.h>
26
27 /* open header */
28 #include <glib.h>
29 #include <dlfcn.h>
30 #include <vconf.h>
31 #include "c-client.h" 
32
33
34 #include "email-api.h"
35 #include "email-api-account.h"
36 #include "email-api-network.h"
37
38
39 /* internal header */
40 #include "testapp-utility.h"
41 #include "testapp-others.h"
42 #include "email-ipc.h"
43 #include "email-core-utils.h"
44
45 static gboolean testapp_test_get_network_status()
46 {
47         int on_sending = 0;
48         int on_receiving = 0;
49         email_get_network_status(&on_sending, &on_receiving);
50         testapp_print("\tNetwork status : \n On sending - %d \n On Receiving - %d \n", on_sending, on_receiving);
51         return FALSE;
52 }
53
54 static gboolean testapp_test_get_pending_job()
55 {
56         int action = -1;
57         int account_id = 0;
58         int mail_id = 0;
59         int result_from_scanf = 0;
60         email_event_status_type_t status = -1;
61         testapp_print( " Enter Action \n SEND_MAIL = 0 \n SYNC_HEADER = 1 \n" \
62                             " DOWNLOAD_BODY,= 2 \n DOWNLOAD_ATTACHMENT = 3 \n" \
63                             " DELETE_MAIL = 4 \n SEARCH_MAIL = 5 \n SAVE_MAIL = 6 \n" \
64                             " NUM = 7 \n");
65         result_from_scanf = scanf("%d",&action);
66
67         testapp_print("\n > Enter account_id: ");
68         result_from_scanf = scanf("%d", &account_id);
69
70         testapp_print("\n > Enter Mail Id: ");
71         result_from_scanf = scanf("%d", &mail_id);
72
73         if( email_get_pending_job( action, account_id, mail_id, &status) >= 0)
74                 testapp_print("\t status - %d \n",status);
75
76         return FALSE;
77 }
78
79 static gboolean testapp_test_cancel_job ()
80 {
81         int account_id = 0;
82         int handle = 0;
83         int result_from_scanf = 0;
84
85         testapp_print("\n > Enter account_id (0: all account): ");
86         result_from_scanf = scanf("%d", &account_id);
87
88         testapp_print("\n > Enter handle: ");
89         result_from_scanf = scanf("%d", &handle);
90
91         if(email_cancel_job(account_id, handle, EMAIL_CANCELED_BY_USER) < 0)
92                 testapp_print("email_cancel_job failed..!");
93         return FALSE;
94 }
95
96 static gboolean testapp_test_set_dnet_proper_profile_type()
97 {
98         testapp_print("NOT Support\n");
99         
100         return TRUE;
101 }
102
103 static gboolean testapp_test_get_dnet_proper_profile_type()
104 {
105         testapp_print("NOT Support\n");
106
107         return TRUE;
108 }
109
110 static gboolean testapp_test_get_preview_text_from_file()
111 {
112         int   result_from_scanf;
113         char *preview_buffer = NULL;
114         char  html_file_path[1024] = { 0, };
115
116         testapp_print("\n > Enter file path : ");
117         result_from_scanf = scanf("%s", html_file_path);
118
119         emcore_get_preview_text_from_file(NULL, html_file_path, 1024, &preview_buffer);
120
121         testapp_print("\n result :\n %s ", preview_buffer);
122
123         return TRUE;
124 }
125
126 static gboolean testapp_test_print_receving_queue_via_debug_msg()
127 {
128         int err;
129         void* hAPI = (void*)emipc_create_email_api(_EMAIL_API_PRINT_RECEIVING_EVENT_QUEUE);
130
131         if(hAPI == NULL)
132                 return EMAIL_ERROR_NULL_VALUE;
133                 
134         if(emipc_execute_proxy_api(hAPI) != EMAIL_ERROR_NONE) {
135                 testapp_print("testapp_test_print_receving_queue_via_debug_msg - emipc_execute_proxy_api failed \n ");
136                 if(hAPI == NULL)
137                         return EMAIL_ERROR_NULL_VALUE;
138         }
139
140         emipc_get_parameter(hAPI, 1, 0, sizeof(int), &err);
141         
142         testapp_print(" >>>> RETURN VALUE : %d \n", err);
143
144         emipc_destroy_email_api(hAPI);
145
146         hAPI = NULL;
147         testapp_print("testapp_test_print_receving_queue_via_debug_msg  ..........End\n");
148         return err;
149 }
150
151 static gboolean testapp_test_create_db_full()
152 {
153         int err;
154
155         err = email_create_db_full();
156         
157         testapp_print("testapp_test_create_db_full returns [%d]", err);
158
159         return err;
160 }
161
162 static int encode_base64(char *src, unsigned long src_len, char **enc, unsigned long* enc_len, int *err_code)
163 {
164         unsigned char *content = NULL;
165         int ret = true; 
166         int err = EMAIL_ERROR_NONE;
167
168         if (err_code != NULL) {
169                 *err_code = EMAIL_ERROR_NONE;
170         }
171
172         content = rfc822_binary(src, src_len, enc_len);
173
174         if (content)
175                 *enc = (char *)content;
176         else {
177                 err = EMAIL_ERROR_UNKNOWN;
178                 ret = false;
179         }
180
181         if (err_code)
182             *err_code = err;
183
184         return ret;
185 }
186
187
188 static gboolean testapp_test_encoding_test()
189 {
190         int error = EMAIL_ERROR_NONE;
191         int has_special_character = 0, base64_file_name_length = 0, i;
192         gsize bytes_read, bytes_written;
193         char *encoded_file_name = NULL, *base64_file_name = NULL;
194         SIZEDTEXT source_text;
195         GError *glib_error = NULL;
196         CHARSET *result_charset = NULL;
197         char filename[] = {0xEB, 0xB0, 0x94, 0xED, 0x83, 0x95, 0x32, 0x2E, 0x70, 0x6E, 0x67, 0x00}; /* UTF-8 */
198
199         source_text.data = (unsigned char*)filename; 
200         source_text.size = strlen(filename);
201
202         result_charset = (CHARSET*)utf8_infercharset(&source_text);
203
204         if(result_charset) {
205                 testapp_print("return_charset->name [%s]", result_charset->name);
206                 encoded_file_name = (char*)g_convert (filename, -1, "UTF-8", result_charset->name, &bytes_read, &bytes_written, &glib_error);
207         }
208         else {
209                 i = 0;
210                 while(filename[i++] & 0x80)
211                         has_special_character = 1;
212                 testapp_print("has_special_character [%d]", has_special_character);
213                 if(has_special_character)
214                         encoded_file_name = (char*)g_convert (filename, -1, "UTF-8", "EUC-KR", &bytes_read, &bytes_written, &glib_error);
215         }
216         
217         if(encoded_file_name == NULL)
218                 encoded_file_name = strdup(filename);
219
220         testapp_print("encoded_file_name [%s]", encoded_file_name);
221
222         if(!encode_base64(encoded_file_name, strlen(encoded_file_name), &base64_file_name, (unsigned long*)&base64_file_name_length, &error)) {
223                 testapp_print("encode_base64 failed. error [%d]", error);
224                 goto FINISH_OFF;
225         }
226
227         testapp_print("base64_file_name [%s]", base64_file_name);
228
229         if(base64_file_name) {
230                 free(encoded_file_name);
231                 encoded_file_name = malloc(strlen(base64_file_name) + 15);
232                 if(!encoded_file_name) {
233                         testapp_print("em_malloc failed.");
234                         goto FINISH_OFF;
235                 }
236                 snprintf(encoded_file_name, strlen(base64_file_name) + 15, "=?UTF-8?B?%s?=", base64_file_name);
237                 testapp_print("encoded_file_name [%s]", encoded_file_name);
238         }
239 FINISH_OFF:
240
241         if (encoded_file_name)
242                 free(encoded_file_name);
243
244         if (base64_file_name)
245                 free(base64_file_name);
246
247         return error;
248 }
249
250 #define LIB_EMAIL_SERVICE_PATH  "/usr/lib/libemail-api.so"
251
252 int (*Datastore_FI_EMTB)(char **);
253 int (*Datastore_FI_EMSB)(char **);
254 int (*Datastore_FI_EMOB)(char **);
255 int (*Datastore_FI_EMDR)(char **);
256 int (*Datastore_FI_EMMF)(char **);
257 int (*Datastore_FI_EMTR)(char **);
258 int (*Datastore_FI_EMSP)(char **);
259
260 static gboolean email_test_dtt_Datastore_FI()
261 {
262         void *handle = NULL;
263         char *dl_error = NULL, *output_str = NULL;
264
265         handle = dlopen(LIB_EMAIL_SERVICE_PATH, RTLD_LAZY | RTLD_GLOBAL);
266         if (!handle) {
267                 dl_error = dlerror();
268                 if (dl_error)
269                         testapp_print("\t dlopen error : Open Library with absolute path return  :  %s\n", dl_error);
270                 return false;
271         }       
272
273         Datastore_FI_EMTB = dlsym(handle, "Datastore_FI_EMTB");
274         Datastore_FI_EMSB = dlsym(handle, "Datastore_FI_EMSB");
275         Datastore_FI_EMOB = dlsym(handle, "Datastore_FI_EMOB");
276         Datastore_FI_EMDR = dlsym(handle, "Datastore_FI_EMDR");
277         Datastore_FI_EMMF = dlsym(handle, "Datastore_FI_EMMF");
278         Datastore_FI_EMTR = dlsym(handle, "Datastore_FI_EMTR");
279         Datastore_FI_EMSP = dlsym(handle, "Datastore_FI_EMSP");
280
281         Datastore_FI_EMTB(&output_str);
282
283         testapp_print("\nemail_test_dtt_Datastore_FI\n%s\n", output_str);
284         
285         if(output_str)
286                 free(output_str);
287
288         if (handle) {
289                 dlclose(handle);
290                 dl_error = dlerror();
291                 if (dl_error)
292                         testapp_print("\t Close handle return  :  %s\n", dl_error);
293         }
294
295         return true;
296 }
297
298 int (*Datastore_R_EMTB)(char **);
299 int (*Datastore_R_EMSB)(char **);
300 int (*Datastore_R_EMOB)(char **);
301 int (*Datastore_R_EMDR)(char **);
302 int (*Datastore_R_EMMF)(char **);
303 int (*Datastore_R_EMTR)(char **);
304 int (*Datastore_R_EMSP)(char **);
305
306
307 static gboolean email_test_dtt_Datastore_R()
308 {
309         void *handle = NULL;
310         char *dl_error = NULL, *output_str = NULL;
311
312         handle = dlopen(LIB_EMAIL_SERVICE_PATH, RTLD_LAZY | RTLD_GLOBAL);
313         if (!handle) {
314                 dl_error = dlerror();
315                 if (dl_error)
316                         testapp_print("\t dlopen error : Open Library with absolute path return  :  %s\n", dl_error);
317                 return false;
318         }       
319
320         Datastore_R_EMTB = dlsym(handle, "Datastore_R_EMTB");
321         Datastore_R_EMSB = dlsym(handle, "Datastore_R_EMSB");
322         Datastore_R_EMOB = dlsym(handle, "Datastore_R_EMOB");
323         Datastore_R_EMDR = dlsym(handle, "Datastore_R_EMDR");
324         Datastore_R_EMMF = dlsym(handle, "Datastore_R_EMMF");
325         Datastore_R_EMTR = dlsym(handle, "Datastore_R_EMTR");
326         Datastore_R_EMSP = dlsym(handle, "Datastore_R_EMSP");
327
328         Datastore_R_EMTB(&output_str);
329
330         testapp_print("\nemail_test_dtt_Datastore_R\n%s\n", output_str);
331
332         if(output_str)
333                 free(output_str);
334
335         if (handle) {
336                 dlclose(handle);
337                 dl_error = dlerror();
338                 if (dl_error)
339                         testapp_print("\t Close handle return  :  %s\n", dl_error);
340         }
341
342         return true;
343 }
344
345 int (*Datastore_C_EMTB)(char **);
346 int (*Datastore_C_EMSB)(char **);
347 int (*Datastore_C_EMOB)(char **);
348 int (*Datastore_C_EMDR)(char **);
349 int (*Datastore_C_EMMF)(char **);
350 int (*Datastore_C_EMTR)(char **);
351 int (*Datastore_C_EMSP)(char **);
352
353 static gboolean email_test_dtt_Datastore_C()
354 {
355         void *handle = NULL;
356         char *dl_error = NULL, *output_str = NULL;
357
358         handle = dlopen(LIB_EMAIL_SERVICE_PATH, RTLD_LAZY | RTLD_GLOBAL);
359         if (!handle) {
360                 dl_error = dlerror();
361                 if (dl_error)
362                         testapp_print("\t dlopen error : Open Library with absolute path return  :  %s\n", dl_error);
363                 return false;
364         }       
365
366         Datastore_C_EMTB = dlsym(handle, "Datastore_C_EMTB");
367         Datastore_C_EMSB = dlsym(handle, "Datastore_C_EMSB");
368         Datastore_C_EMOB = dlsym(handle, "Datastore_C_EMOB");
369         Datastore_C_EMDR = dlsym(handle, "Datastore_C_EMDR");
370         Datastore_C_EMMF = dlsym(handle, "Datastore_C_EMMF");
371         Datastore_C_EMTR = dlsym(handle, "Datastore_C_EMTR");
372         Datastore_C_EMSP = dlsym(handle, "Datastore_C_EMSP");
373
374         Datastore_C_EMTB(&output_str);
375
376
377         testapp_print("\nemail_test_dtt_Datastore_C\n%s\n", output_str);
378         
379         if(output_str)
380                 free(output_str);
381
382         if (handle) {
383                 dlclose(handle);
384                 dl_error = dlerror();
385                 if (dl_error)
386                         testapp_print("\t Close handle return  :  %s\n", dl_error);
387         }
388
389         return true;
390 }
391
392 static gboolean testapp_test_show_user_message()
393 {
394         int mail_id;
395         int result_from_scanf = 0;
396
397         testapp_print("\n > Enter mail id : ");
398         result_from_scanf = scanf("%d", &mail_id);
399
400         email_show_user_message(mail_id, EMAIL_ACTION_SEND_MAIL, EMAIL_ERROR_NETWORK_NOT_AVAILABLE);
401         return FALSE;
402 }
403
404 static gboolean testapp_test_get_mime_entity()
405 {
406         char mime_path[512] = {0, };
407         int result_from_scanf = 0;
408         char *mime_entity = NULL;
409
410         testapp_print("\n > Enter mime path for parsing : ");
411         result_from_scanf = scanf("%s", mime_path);
412         
413         email_get_mime_entity(mime_path, &mime_entity);
414
415         testapp_print("\nmime_entity = %s\n", mime_entity);
416         return true;
417 }
418
419 static gboolean testapp_test_interpret_command (int menu_number)
420 {
421         gboolean go_to_loop = TRUE;
422
423         switch (menu_number) {
424                 case 1:
425                         testapp_test_get_network_status();
426                         break;
427                 case 2:
428                         testapp_test_get_pending_job ();
429                         break;
430                 case 3:
431                         testapp_test_cancel_job ();
432                         break;
433                 case 5: 
434                         testapp_test_set_dnet_proper_profile_type();
435                         break;
436                 case 6:
437                         testapp_test_get_dnet_proper_profile_type();
438                         break;
439                 case 7:
440                         testapp_test_get_preview_text_from_file();
441                         break;
442                 case 11:
443                         testapp_test_print_receving_queue_via_debug_msg();
444                         break;
445                 case 12:
446                         testapp_test_create_db_full();
447                         break;
448                 case 13:
449                         testapp_test_encoding_test();
450                         break;
451                 case 14:
452                         email_test_dtt_Datastore_FI();
453                         email_test_dtt_Datastore_C();
454                         email_test_dtt_Datastore_R();
455                         break;
456                 case 15:
457                         testapp_test_show_user_message();
458                         break;
459                 case 16:
460                         testapp_test_get_mime_entity();
461                         break;
462                 case 0:
463                         go_to_loop = FALSE;
464                         break;
465                 default:
466                         break;
467         }
468
469         return go_to_loop;
470 }
471
472 void testapp_others_main()
473 {
474         gboolean go_to_loop = TRUE;
475         int menu_number = 0;
476         int result_from_scanf = 0;
477
478         while (go_to_loop) {
479                 testapp_show_menu (EMAIL_OTHERS_MENU);
480                 testapp_show_prompt (EMAIL_OTHERS_MENU);
481
482                 result_from_scanf = scanf ("%d", &menu_number);
483
484                 go_to_loop = testapp_test_interpret_command (menu_number);
485         }
486 }
487