2.0_alpha release commit
[framework/messaging/email-service.git] / email-common-use / email-utilities.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  * email-utilities.c
24  *
25  *  Created on: 2012. 3. 6.
26  *      Author: kyuho.jo@samsung.com
27  */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <sys/time.h>
34 #include <sys/vfs.h>
35 #include <unistd.h>
36 #include <malloc.h>
37 #include <pthread.h>
38 #include <regex.h>
39
40 #include "c-client.h"
41
42 #include "email-types.h"
43 #include "email-internal-types.h"
44 #include "email-utilities.h"
45
46 INTERNAL_FUNC void* em_malloc(int len)
47 {
48         /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
49         if (len <= 0) {
50                 EM_DEBUG_EXCEPTION("len should be positive.[%d]", len);
51                 return NULL;
52         }
53
54         void *p = calloc(1,len);
55         if (!p)
56                 EM_DEBUG_PERROR("malloc failed");
57
58         return p;
59 }
60
61
62 INTERNAL_FUNC void* em_memdup(void* src, int len)
63 {
64         /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
65         if (len <= 0) {
66                 EM_DEBUG_EXCEPTION("len should be positive.[%d]", len);
67                 return NULL;
68         }
69
70         void *p = calloc(1,len);
71         if (!p)
72                 EM_DEBUG_EXCEPTION("malloc failed");
73
74         memcpy(p, src, len);
75
76         return p;
77 }
78
79
80 /*  remove left space, tab, CR, L */
81 INTERNAL_FUNC char *em_trim_left(char *str)
82 {
83         char *p, *temp_buffer = NULL;
84
85         /* EM_DEBUG_FUNC_BEGIN() */
86         if (!str) return NULL;
87
88         p = str;
89         while (*p && (*p == ' ' || *p == '\t' || *p == LF || *p == CR)) p++;
90
91         if (!*p) return NULL;
92
93         temp_buffer = EM_SAFE_STRDUP(p);
94
95         strncpy(str, temp_buffer, strlen(str));
96         str[strlen(temp_buffer)] = NULL_CHAR;
97
98         EM_SAFE_FREE(temp_buffer);
99
100         return str;
101 }
102
103 /*  remove right space, tab, CR, L */
104 INTERNAL_FUNC char *em_trim_right(char *str)
105 {
106         char *p;
107
108         /* EM_DEBUG_FUNC_BEGIN() */
109         if (!str) return NULL;
110
111         p = str+strlen(str)-1;
112         while (((int)p >= (int)str) && (*p == ' ' || *p == '\t' || *p == LF || *p == CR))
113                 *p --= '\0';
114
115         if ((int) p < (int)str)
116                 return NULL;
117
118         return str;
119 }
120
121 INTERNAL_FUNC char* em_upper_string(char *str)
122 {
123         char *p = str;
124         while (*p)  {
125                 *p = toupper(*p);
126                 p++;
127         }
128         return str;
129 }
130
131 INTERNAL_FUNC char*  em_lower_string(char *str)
132 {
133         char *p = str;
134         while (*p)  {
135                 *p = tolower(*p);
136                 p++;
137         }
138         return str;
139 }
140
141 INTERNAL_FUNC int em_upper_path(char *path)
142 {
143         int i = 0, is_utf7 = 0, len = path ? (int)strlen(path) : -1;
144         for (; i < len; i++) {
145                 if (path[i] == '&' || path[i] == 5) {
146                         is_utf7 = 1;
147                 }
148                 else {
149                         if (is_utf7) {
150                                 if (path[i] == '-') is_utf7 = 0;
151                         }
152                         else {
153                                 path[i] = toupper(path[i]);
154                         }
155                 }
156         }
157
158         return 1;
159 }
160
161 INTERNAL_FUNC void em_skip_whitespace(char *addr_str, char **pAddr)
162 {
163         EM_DEBUG_FUNC_BEGIN("addr_str[%p]", addr_str);
164
165         if (!addr_str)
166                 return ;
167         char *str = addr_str;
168         char ptr[strlen(str)+1]  ;
169         int i, j = 0;
170
171         str = addr_str ;
172         for (i = 0; str[i] != NULL_CHAR ; i++) {
173                 if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
174                         ptr[j++] = str[i];
175         }
176         ptr[j] = NULL_CHAR;
177
178         *pAddr = EM_SAFE_STRDUP(ptr);
179         EM_DEBUG_FUNC_END("ptr[%s]", ptr);
180 }
181
182 INTERNAL_FUNC char* em_skip_whitespace_without_strdup(char *source_string)
183 {
184         EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
185
186         if (!source_string)
187                 return NULL;
188         int i;
189
190         for (i = 0; source_string[i] != NULL_CHAR ; i++) {
191                 if (source_string[i] != SPACE) /*  || source_string[i] != TAB || source_string[i] != CR || source_string[i] || LF) */
192                         break;
193         }
194
195         EM_DEBUG_FUNC_END("i[%d]", i);
196         return source_string + i;
197 }
198
199
200 INTERNAL_FUNC char* em_replace_all_string(char *source_string, char *old_string, char *new_string)
201 {
202         EM_DEBUG_FUNC_BEGIN();
203         char *result_buffer = NULL;
204         char *p = NULL;
205         int i = 0, count = 0;
206         int old_str_length = 0;
207         int new_str_length = 0;
208         
209         EM_IF_NULL_RETURN_VALUE(source_string, NULL);
210         EM_IF_NULL_RETURN_VALUE(old_string, NULL);
211         EM_IF_NULL_RETURN_VALUE(new_string, NULL);
212
213         old_str_length = strlen(old_string);
214         new_str_length = strlen(new_string);
215         
216         if (old_str_length != new_str_length) {
217                 for (i = 0; source_string[i] != '\0';) {
218                         if (memcmp(&source_string[i], old_string, old_str_length) == 0) {
219                                 count++;
220                                 i += old_str_length;
221                         } else {
222                                 i++;
223                         }
224                 }
225         } else {
226                 i = strlen(source_string);
227         }
228
229         result_buffer = (char *)malloc(i + 1 + count*(new_str_length-old_str_length));
230         if (result_buffer == NULL) 
231                 return NULL;
232
233         p = result_buffer;
234         while (*source_string) {
235                 if (memcmp(source_string, old_string, old_str_length) == 0) {
236                         memcpy(p, new_string, new_str_length);
237                         p += new_str_length;
238                         source_string += old_str_length;
239                 } else {
240                         *p++ = *source_string++;
241                 }
242         }
243         *p = '\0';
244         
245         EM_DEBUG_FUNC_END("result_buffer : %s", result_buffer);
246         return result_buffer;
247 }
248
249 INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, char *new_string)
250 {
251         EM_DEBUG_FUNC_BEGIN();
252         char *result_buffer = NULL;
253         char *p = NULL;
254         int   buffer_length = 0;
255
256         EM_IF_NULL_RETURN_VALUE(source_string, NULL);
257         EM_IF_NULL_RETURN_VALUE(old_string, NULL);
258         EM_IF_NULL_RETURN_VALUE(new_string, NULL);
259
260         p = strstr(source_string, old_string);
261
262         if (p == NULL) {
263                 EM_DEBUG_EXCEPTION("old_string not found in source_string");
264                 EM_DEBUG_FUNC_END("return NULL");
265                 return NULL;
266         }
267
268         buffer_length   = strlen(source_string) + 1024;
269         result_buffer  = (char *)em_malloc(buffer_length);
270
271         if (!result_buffer) {
272                 EM_DEBUG_EXCEPTION("em_malloc failed");
273                 return NULL;
274         }
275
276         strncpy(result_buffer, source_string, p - source_string);
277         snprintf(result_buffer + strlen(result_buffer), buffer_length - strlen(result_buffer), "%s%s", new_string, p + strlen(old_string));
278
279         EM_DEBUG_FUNC_END("result_buffer[%s]", result_buffer);
280         return result_buffer;
281 }
282
283 /* Memory clean up */
284 #include <sys/mman.h>
285
286 /* #define GETSP()                              ({ unsigned int sp; asm volatile ("mov %0, sp " : "=r"(sp)); sp;}) */
287 #define BUF_SIZE                                256
288 #define PAGE_SIZE                           (1 << 12)
289 #define _ALIGN_UP(addr, size)   (((addr)+((size)-1))&(~((size)-1)))
290 #define _ALIGN_DOWN(addr, size) ((addr)&(~((size)-1)))
291 #define PAGE_ALIGN(addr)        _ALIGN_DOWN(addr, PAGE_SIZE)
292
293 int stack_trim(void)
294 {
295         /*
296         char buf[BUF_SIZE];
297         FILE *file;
298         unsigned int stacktop;
299         int found = 0;
300         unsigned int sp;
301
302         asm volatile ("mov %0, sp " : "=r"(sp));
303
304         sprintf(buf, "/proc/%d/maps", getpid());
305         file = fopen(buf, "r");
306         while (fgets(buf, BUF_SIZE, file) != NULL) {
307                 if (strstr(buf, "[stack]")) {
308                         found = 1;
309                         break;
310                 }
311         }
312
313         fclose(file);
314
315         if (found) {
316                 sscanf(buf, "%x-", &stacktop);
317                 if (madvise((void *)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp)-stacktop, MADV_DONTNEED) < 0)
318                         perror("stack madvise fail");
319         }
320         */
321         return 1;
322 }
323
324 INTERNAL_FUNC void em_flush_memory()
325 {
326         EM_DEBUG_FUNC_BEGIN();
327         /*  flush memory in heap */
328         malloc_trim(0);
329
330         /*  flush memory in stack */
331         stack_trim();
332
333         /*  flush memory for sqlite */
334         emstorage_flush_db_cache();
335         EM_DEBUG_FUNC_END();
336 }
337
338 INTERNAL_FUNC int em_get_file_name_and_extension_from_file_path(char *input_source_file_path, char **output_file_name, char **output_extension)
339 {
340         EM_DEBUG_FUNC_BEGIN("input_source_file_path[%s], output_file_name [%p], output_extension [%p]", input_source_file_path, output_file_name, output_extension);
341         int   err = EMAIL_ERROR_NONE;
342         int   pos_on_string = 0;
343         int   file_name_length = 0;
344         int   extention_length = 0;
345         char *start_pos_of_file_name = NULL;
346         char *end_pos_of_file_name = NULL;
347         char *dot_pos_of_file_path = NULL;
348         char *end_pos_of_file_path = NULL;
349         char  file_name_string[MAX_PATH] = { 0, };
350         char  extension_string[MAX_PATH] = { 0, };
351
352         if (!input_source_file_path || !output_file_name || !output_extension) {
353                 EM_DEBUG_EXCEPTION("Invalid Parameter");
354                 err  = EMAIL_ERROR_INVALID_PARAM;
355                 goto FINISH_OFF;
356         }
357
358         pos_on_string        = strlen(input_source_file_path) - 1;
359         end_pos_of_file_path = input_source_file_path + pos_on_string;
360         end_pos_of_file_name = end_pos_of_file_path;
361
362         while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
363                 if(input_source_file_path[pos_on_string] == '.') {
364                         if(dot_pos_of_file_path == NULL) {
365                                 end_pos_of_file_name = input_source_file_path + pos_on_string;
366                                 dot_pos_of_file_path = end_pos_of_file_name;
367                         }
368                 }
369                 pos_on_string--;
370         }
371
372         pos_on_string++;
373
374         if(pos_on_string >= 0) {
375                 start_pos_of_file_name = input_source_file_path + pos_on_string;
376                 file_name_length       = end_pos_of_file_name - start_pos_of_file_name;
377                 memcpy(file_name_string, start_pos_of_file_name, file_name_length);
378         }
379
380         if(dot_pos_of_file_path != NULL) {
381                 extention_length       = (end_pos_of_file_path + 1) - (dot_pos_of_file_path + 1);
382                 memcpy(extension_string, dot_pos_of_file_path + 1, extention_length);
383         }
384
385         EM_DEBUG_LOG("*file_name_string [%s] pos_on_string [%d]", file_name_string, pos_on_string);
386
387         *output_file_name = EM_SAFE_STRDUP(file_name_string);
388         *output_extension = EM_SAFE_STRDUP(extension_string);
389
390 FINISH_OFF:
391         EM_DEBUG_FUNC_END("err = [%d]", err);
392         return err;
393 }
394
395 INTERNAL_FUNC char *em_get_extension_from_file_path(char *source_file_path, int *err_code)
396 {
397         EM_DEBUG_FUNC_BEGIN("source_file_path[%s]", source_file_path);
398         int err = EMAIL_ERROR_NONE, pos_on_string = 0;
399         char *extension = NULL;
400
401         if (!source_file_path) {
402                 EM_DEBUG_EXCEPTION("Invalid Parameter");
403                 err  = EMAIL_ERROR_INVALID_PARAM;
404                 goto FINISH_OFF;
405         }
406
407         pos_on_string = strlen(source_file_path) - 1;
408
409         while(pos_on_string > 0 && source_file_path[pos_on_string--] != '.') ;
410
411         if(pos_on_string > 0)
412                 extension = source_file_path + pos_on_string + 2;
413
414         EM_DEBUG_LOG("*extension [%s] pos_on_string [%d]", extension, pos_on_string);
415
416 FINISH_OFF:
417         if (err_code)
418                 *err_code = err;
419         EM_DEBUG_FUNC_END();
420         return extension;
421 }
422
423 INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type)
424 {
425         EM_DEBUG_FUNC_BEGIN("input_file_path[%d], output_encoding_type[%p]", input_file_path, output_encoding_type);
426         int   err = EMAIL_ERROR_NONE;
427         int   pos_of_filename = 0;
428         int   pos_of_dot = 0;
429         int   enf_of_string = 0;
430         int   result_string_length = 0;
431         char *filename = NULL;
432         char *result_encoding_type = NULL;
433
434         if (!input_file_path || !output_encoding_type) {
435                 EM_DEBUG_EXCEPTION("Invalid Parameter");
436                 err  = EMAIL_ERROR_INVALID_PARAM;
437                 goto FINISH_OFF;
438         }
439
440         enf_of_string = pos_of_filename = strlen(input_file_path);
441
442         while(pos_of_filename >= 0 && input_file_path[pos_of_filename--] != '/') {
443                 if(input_file_path[pos_of_filename] == '.')
444                         pos_of_dot = pos_of_filename;
445         }
446
447         if(pos_of_filename != 0)
448                 pos_of_filename += 2;
449
450         filename = (char*)input_file_path + pos_of_filename;
451
452         if(pos_of_dot != 0 && pos_of_dot > pos_of_filename)
453                 result_string_length = pos_of_dot - pos_of_filename;
454         else
455                 result_string_length = enf_of_string - pos_of_filename;
456
457         EM_DEBUG_LOG("pos_of_dot [%d], pos_of_filename [%d], enf_of_string[%d],result_string_length [%d]", pos_of_dot, pos_of_filename, enf_of_string, result_string_length);
458
459         if( !(result_encoding_type =    em_malloc(sizeof(char) * (result_string_length + 1))) ) {
460                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
461                 err  = EMAIL_ERROR_OUT_OF_MEMORY;
462                 goto FINISH_OFF;
463         }
464
465         memcpy(result_encoding_type, input_file_path + pos_of_filename, result_string_length);
466
467         EM_DEBUG_LOG("*result_encoding_type [%s]", result_encoding_type);
468
469         *output_encoding_type = result_encoding_type;
470
471 FINISH_OFF:
472         EM_DEBUG_FUNC_END("err [%d]", err);
473         return err;
474 }
475
476 INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extension_string, int *err_code)
477 {
478         EM_DEBUG_FUNC_BEGIN("extension_string[%s]", extension_string);
479         int i = 0, err = EMAIL_ERROR_NONE, result_content_type = TYPEAPPLICATION;
480         char *image_extension[] = { "jpeg", "jpg", "png", "gif", "bmp", "pic", "agif", "tif", "wbmp" , NULL};
481
482         if (!extension_string) {
483                 EM_DEBUG_EXCEPTION("Invalid Parameter");
484                 err  = EMAIL_ERROR_INVALID_PARAM;
485                 goto FINISH_OFF;
486         }
487
488         while(image_extension[i]) {
489                 EM_DEBUG_LOG("image_extension[%d] [%s]", i, image_extension[i]);
490                 if(strcasecmp(image_extension[i], extension_string) == 0) {
491                         result_content_type = TYPEIMAGE;
492                         break;
493                 }
494                 i++;
495         }
496
497 FINISH_OFF:
498         if (err_code)
499                 *err_code = err;
500         EM_DEBUG_FUNC_END();
501         return result_content_type;
502 }
503
504 #define EMAIL_ACCOUNT_RGEX                     "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)*[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
505 #define EMAIL_DOMAIN_RGEX                      "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)+[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
506
507 #define EMAIL_ADDR_RGEX                        "[[:space:]]*<"EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX">[[:space:]]*"
508 #define EMAIL_ALIAS_RGEX                       "([[:space:]]*\"[^\"]*\")?"EMAIL_ADDR_RGEX
509 #define EMAIL_ALIAS_LIST_RGEX                  "^("EMAIL_ALIAS_RGEX"[;,])*"EMAIL_ALIAS_RGEX"[;,]?[[:space:]]*$"
510
511 #define EMAIL_ADDR_WITHOUT_BRACKET_RGEX        "[[:space:]]*"EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX"[[:space:]]*"
512 #define EMAIL_ALIAS_WITHOUT_BRACKET_RGEX       "([[:space:]]*\"[^\"]*\")?"EMAIL_ADDR_WITHOUT_BRACKET_RGEX
513 #define EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX  "("EMAIL_ALIAS_WITHOUT_BRACKET_RGEX"[;,])*"EMAIL_ADDR_WITHOUT_BRACKET_RGEX"[;,]?[[:space:]]*$"
514
515 INTERNAL_FUNC int em_verify_email_address(char *address, int without_bracket, int *err_code)
516 {
517         EM_DEBUG_FUNC_BEGIN("address[%s] without_bracket[%d]", address, without_bracket);
518
519         /*  this following code verfies the email alias string using reg. exp. */
520         regex_t alias_list_regex = {0};
521         int ret = false, error = EMAIL_ERROR_NONE;
522         char *reg_rule = NULL;
523
524         if(!address || strlen(address) == 0) {
525                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
526                 if (err_code)
527                         *err_code = EMAIL_ERROR_INVALID_PARAM;
528                 return false;
529         }
530
531         if(without_bracket)
532                 reg_rule = EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX;
533         else
534                 reg_rule = EMAIL_ALIAS_LIST_RGEX;
535
536         if (regcomp(&alias_list_regex, reg_rule, REG_ICASE | REG_EXTENDED)) {
537                 EM_DEBUG_EXCEPTION("email alias regex unrecognized");
538                 if (err_code)
539                         *err_code = EMAIL_ERROR_UNKNOWN;
540                 return false;
541         }
542
543         int alias_len = strlen(address) + 1;
544         regmatch_t pmatch[alias_len];
545
546         bzero(pmatch, alias_len);
547
548         if (regexec(&alias_list_regex, address, alias_len, pmatch, 0) == REG_NOMATCH)
549                 EM_DEBUG_LOG("failed :[%s]", address);
550         else {
551                 EM_DEBUG_LOG("success :[%s]", address);
552                 ret = true;
553         }
554
555         regfree(&alias_list_regex);
556
557         if (err_code)
558                 *err_code = error;
559
560         EM_DEBUG_FUNC_END("ret [%d]", ret);
561         return ret;
562 }
563
564 INTERNAL_FUNC int em_verify_email_address_of_mail_data(email_mail_data_t *mail_data, int without_bracket, int *err_code)
565 {
566         EM_DEBUG_FUNC_BEGIN("mail_data[%p] without_bracket[%d]", mail_data, without_bracket);
567         char *address_array[4] = { mail_data->full_address_from, mail_data->full_address_to, mail_data->full_address_cc, mail_data->full_address_bcc};
568         int  ret = false, err = EMAIL_ERROR_NONE, i;
569
570         /* check for email_address validation */
571         for (i = 0; i < 4; i++) {
572                 if (address_array[i] && address_array[i][0] != 0) {
573                         if (!em_verify_email_address(address_array[i] , without_bracket, &err)) {
574                                 err = EMAIL_ERROR_INVALID_ADDRESS;
575                                 EM_DEBUG_EXCEPTION("Invalid Email Address [%d][%s]", i, address_array[i]);
576                                 goto FINISH_OFF;
577                         }
578                 }
579         }
580         ret = true;
581 FINISH_OFF:
582         EM_DEBUG_FUNC_END("ret [%d]", ret);
583         return ret;
584 }
585
586 INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl, int input_without_bracket)
587 {
588         EM_DEBUG_FUNC_BEGIN("input_mail_tbl[%p] input_without_bracket[%d]", input_mail_tbl, input_without_bracket);
589         char *address_array[4] = { input_mail_tbl->full_address_to, input_mail_tbl->full_address_cc, input_mail_tbl->full_address_bcc, input_mail_tbl->full_address_from};
590         int  err = EMAIL_ERROR_NONE, i;
591
592         /* check for email_address validation */
593         for (i = 0; i < 4; i++) {
594                 if (address_array[i] && address_array[i][0] != 0) {
595                         if (!em_verify_email_address(address_array[i] , input_without_bracket, &err)) {
596                                 err = EMAIL_ERROR_INVALID_ADDRESS;
597                                 EM_DEBUG_EXCEPTION("Invalid Email Address [%d][%s]", i, address_array[i]);
598                                 goto FINISH_OFF;
599                         }
600                 }
601         }
602
603 FINISH_OFF:
604         EM_DEBUG_FUNC_END("err [%d]", err);
605         return err;
606 }
607
608 INTERNAL_FUNC int em_find_tag_for_thread_view(char *subject, int *result)
609 {
610         EM_DEBUG_FUNC_BEGIN();
611         int error_code = EMAIL_ERROR_NONE;
612         char *copy_of_subject = NULL;
613
614         EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
615         EM_IF_NULL_RETURN_VALUE(result, EMAIL_ERROR_INVALID_PARAM);
616
617         *result = FALSE;
618
619         copy_of_subject = EM_SAFE_STRDUP(subject);
620
621         if (copy_of_subject == NULL) {
622                 EM_DEBUG_EXCEPTION("strdup is failed.");
623                 goto FINISH_OFF;
624         }
625
626         em_upper_string(copy_of_subject);
627         EM_DEBUG_LOG("em_upper_string result : %s\n", copy_of_subject);
628
629         if (strstr(copy_of_subject, "RE:") == NULL) {
630                 if (strstr(copy_of_subject, "FWD:") == NULL) {
631                         if (strstr(copy_of_subject, "FW:") != NULL)
632                                 *result = TRUE;
633                 }
634                 else
635                         *result = TRUE;
636         }
637         else
638                 *result = TRUE;
639
640 FINISH_OFF:
641         EM_SAFE_FREE(copy_of_subject);
642
643         EM_DEBUG_FUNC_END("result : %d", *result);
644
645         return error_code;
646 }
647
648 INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, char *stripped_subject)
649 {
650         EM_DEBUG_FUNC_BEGIN();
651         int error_code = EMAIL_ERROR_NONE;
652         int gap;
653         char *copy_of_subject = NULL, *curpos = NULL, *result;
654
655         EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
656         EM_IF_NULL_RETURN_VALUE(stripped_subject, EMAIL_ERROR_INVALID_PARAM);
657
658         copy_of_subject = EM_SAFE_STRDUP(subject);
659
660         if (copy_of_subject == NULL) {
661                 EM_DEBUG_EXCEPTION("strdup is failed");
662                 goto FINISH_OFF;
663         }
664
665         em_upper_string(copy_of_subject);
666         curpos = copy_of_subject;
667
668         EM_DEBUG_LOG("em_upper_string result : %s", copy_of_subject);
669
670         while ((result = strstr(curpos, "RE:")) != NULL) {
671                 curpos = result + 3;
672                 EM_DEBUG_LOG("RE result : %s", curpos);
673         }
674
675         while ((result = strstr(curpos, "FWD:")) != NULL) {
676                 curpos = result + 4;
677                 EM_DEBUG_LOG("FWD result : %s", curpos);
678         }
679
680         while ((result = strstr(curpos, "FW:")) != NULL) {
681                 curpos = result + 3;
682                 EM_DEBUG_LOG("FW result : %s", curpos);
683         }
684
685         while (curpos != NULL && *curpos == ' ') {
686                 curpos++;
687         }
688
689         gap = curpos - copy_of_subject;
690
691         strcpy(stripped_subject, subject + gap);
692
693 FINISH_OFF:
694         EM_SAFE_FREE(copy_of_subject);
695
696         if (error_code == EMAIL_ERROR_NONE && stripped_subject)
697                 EM_DEBUG_LOG("result[%s]", stripped_subject);
698
699         EM_DEBUG_FUNC_END("error_code[%d]", error_code);
700         return error_code;
701 }
702
703
704 /*
705  * encoding base64
706  */
707 INTERNAL_FUNC int em_encode_base64(char *src, unsigned long src_len, char **enc, unsigned long* enc_len, int *err_code)
708 {
709         EM_DEBUG_FUNC_BEGIN();
710
711     unsigned char *content;
712     int ret = true, err = EMAIL_ERROR_NONE;
713
714         if (err_code != NULL) {
715                 *err_code = EMAIL_ERROR_NONE;
716         }
717
718     content = rfc822_binary(src, src_len, enc_len);
719
720     if (content)
721         *enc = (char *)content;
722     else {
723         err = EMAIL_ERROR_UNKNOWN;
724         ret = false;
725     }
726
727     if (err_code)
728         *err_code = err;
729
730         EM_DEBUG_FUNC_END();
731     return ret;
732 }
733
734 /*
735  * decoding base64
736  */
737 INTERNAL_FUNC int em_decode_base64(unsigned char *enc_text, unsigned long enc_len, char **dec_text, unsigned long* dec_len, int *err_code)
738 {
739     unsigned char *text = enc_text;
740     unsigned long size = enc_len;
741     unsigned char *content;
742     int ret = true, err = EMAIL_ERROR_NONE;
743
744         if (err_code != NULL) {
745                 *err_code = EMAIL_ERROR_NONE;
746         }
747
748     EM_DEBUG_FUNC_BEGIN();
749
750     content = rfc822_base64(text, size, dec_len);
751     if (content)
752         *dec_text = (char *)content;
753     else
754     {
755         err = EMAIL_ERROR_UNKNOWN;
756         ret = false;
757     }
758
759     if (err_code)
760         *err_code = err;
761
762     return ret;
763 }
764
765 INTERNAL_FUNC int em_get_account_server_type_by_account_id(int account_id, email_account_server_t* account_server_type, int flag, int *error)
766 {
767         EM_DEBUG_FUNC_BEGIN();
768         emstorage_account_tbl_t *account_tbl_data = NULL;
769         int ret = false;
770         int err= EMAIL_ERROR_NONE;
771
772         if (account_server_type == NULL ) {
773                 EM_DEBUG_EXCEPTION("account_server_type is NULL");
774                 err = EMAIL_ERROR_INVALID_PARAM;
775                 ret = false;
776                 goto FINISH_OFF;
777         }
778
779         if( !emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &account_tbl_data, false, &err)) {
780                 EM_DEBUG_EXCEPTION ("emstorage_get_account_by_id failed [%d] ", err);
781                 ret = false;
782                 goto FINISH_OFF;
783         }
784
785         if ( flag == false )  { /*  sending server */
786                 *account_server_type = account_tbl_data->outgoing_server_type;
787         } else if ( flag == true ) {    /*  receiving server */
788                 *account_server_type = account_tbl_data->incoming_server_type;
789         }
790
791         ret = true;
792
793 FINISH_OFF:
794         if ( account_tbl_data != NULL ) {
795                 emstorage_free_account(&account_tbl_data, 1, NULL);
796         }
797         if ( error != NULL ) {
798                 *error = err;
799         }
800
801         return ret;
802 }
803
804 #include <vconf.h>
805 #include <dbus/dbus.h>
806
807 #define ACTIVE_SYNC_HANDLE_INIT_VALUE           (-1)
808 #define ACTIVE_SYNC_HANDLE_BOUNDARY                     (-100000000)
809
810
811 INTERNAL_FUNC int em_get_handle_for_activesync(int *handle, int *error)
812 {
813         EM_DEBUG_FUNC_BEGIN();
814
815         static int next_handle = 0;
816         int ret = false;
817         int err = EMAIL_ERROR_NONE;
818
819         if ( handle == NULL ) {
820                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed : handle is NULL");
821                 err = EMAIL_ERROR_INVALID_PARAM;
822                 goto FINISH_OFF;
823         }
824
825         if ( vconf_get_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, &next_handle)  != 0 ) {
826                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
827                 if ( next_handle != 0 ) {
828                         err = EMAIL_ERROR_GCONF_FAILURE;
829                         goto FINISH_OFF;
830                 }
831         }
832
833         EM_DEBUG_LOG(">>>>>> VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE : get lastest handle[%d]", next_handle);
834
835         /*  set the value of the handle for active sync */
836         next_handle--;
837         if ( next_handle < ACTIVE_SYNC_HANDLE_BOUNDARY ) {
838                 next_handle = ACTIVE_SYNC_HANDLE_INIT_VALUE;
839         }
840         if ( vconf_set_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, next_handle) != 0) {
841                 EM_DEBUG_EXCEPTION("vconf_set_int failed");
842                 err = EMAIL_ERROR_GCONF_FAILURE;
843                 goto FINISH_OFF;
844         }
845         ret = true;
846         *handle = next_handle;
847         EM_DEBUG_LOG(">>>>>> return next handle[%d]", *handle);
848
849 FINISH_OFF:
850         if ( error != NULL ) {
851                 *error = err;
852         }
853
854         return ret;
855 }
856
857 INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNotiData *data)
858 {
859         EM_DEBUG_FUNC_BEGIN("subType [%d], data [%p]", subType, data);
860
861         DBusConnection     *connection;
862         DBusMessage        *signal = NULL;
863         DBusError           error;
864         const char         *nullString = "";
865         int                 i = 0;
866
867         dbus_error_init (&error);
868         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
869
870         if(connection == NULL)
871                 goto FINISH_OFF;
872
873         signal = dbus_message_new_signal("/User/Email/ActiveSync", EMAIL_ACTIVE_SYNC_NOTI, "email");
874
875         dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
876         switch ( subType ) {
877                 case ACTIVE_SYNC_NOTI_SEND_MAIL:
878                         EM_DEBUG_LOG("handle:[%d]", data->send_mail.handle);
879                         EM_DEBUG_LOG("account_id:[%d]", data->send_mail.account_id);
880                         EM_DEBUG_LOG("mail_id:[%d]", data->send_mail.mail_id);
881                         EM_DEBUG_LOG("options.priority:[%d]", data->send_mail.options.priority);
882                         EM_DEBUG_LOG("options.keep_local_copy:[%d]", data->send_mail.options.keep_local_copy);
883                         EM_DEBUG_LOG("options.req_delivery_receipt:[%d]", data->send_mail.options.req_delivery_receipt);
884                         EM_DEBUG_LOG("options.req_read_receipt:[%d]", data->send_mail.options.req_read_receipt);
885                         /* download_limit, block_address, block_subject might not be needed */
886                         EM_DEBUG_LOG("options.download_limit:[%d]", data->send_mail.options.download_limit);
887                         EM_DEBUG_LOG("options.block_address:[%d]", data->send_mail.options.block_address);
888                         EM_DEBUG_LOG("options.block_subject:[%d]", data->send_mail.options.block_subject);
889                         EM_DEBUG_LOG("options.display_name_from:[%s]", data->send_mail.options.display_name_from);
890                         EM_DEBUG_LOG("options.reply_with_body:[%d]", data->send_mail.options.reply_with_body);
891                         EM_DEBUG_LOG("options.forward_with_files:[%d]", data->send_mail.options.forward_with_files);
892                         EM_DEBUG_LOG("options.add_myname_card:[%d]", data->send_mail.options.add_myname_card);
893                         EM_DEBUG_LOG("options.add_signature:[%d]", data->send_mail.options.add_signature);
894                         EM_DEBUG_LOG("options.signature:[%s]", data->send_mail.options.signature);
895
896                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.handle), DBUS_TYPE_INVALID);
897                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.account_id), DBUS_TYPE_INVALID);
898                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.mail_id), DBUS_TYPE_INVALID);
899                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.priority), DBUS_TYPE_INVALID);
900                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.keep_local_copy), DBUS_TYPE_INVALID);
901                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.req_delivery_receipt), DBUS_TYPE_INVALID);
902                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.req_read_receipt), DBUS_TYPE_INVALID);
903                         if ( data->send_mail.options.display_name_from == NULL )
904                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(nullString), DBUS_TYPE_INVALID);
905                         else
906                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->send_mail.options.display_name_from), DBUS_TYPE_INVALID);
907
908                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.reply_with_body), DBUS_TYPE_INVALID);
909                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.forward_with_files), DBUS_TYPE_INVALID);
910                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.add_myname_card), DBUS_TYPE_INVALID);
911                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.options.add_signature), DBUS_TYPE_INVALID);
912                         if ( data->send_mail.options.signature == NULL )
913                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(nullString), DBUS_TYPE_INVALID);
914                         else
915                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->send_mail.options.signature), DBUS_TYPE_INVALID);
916
917                         break;
918                 case ACTIVE_SYNC_NOTI_SEND_SAVED:                               /*  publish a send notification to ASE (active sync engine) */
919                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_SAVED]", subType);
920                         break;
921                 case ACTIVE_SYNC_NOTI_SEND_REPORT:
922                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_REPORT]", subType);
923                         break;
924                 case ACTIVE_SYNC_NOTI_SYNC_HEADER:
925                         EM_DEBUG_LOG("handle:[%d]", data->sync_header.handle);
926                         EM_DEBUG_LOG("account_id:[%d]", data->sync_header.account_id);
927                         EM_DEBUG_LOG("mailbox_id:[%d]", data->sync_header.mailbox_id);
928
929                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.handle ), DBUS_TYPE_INVALID);
930                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.account_id ), DBUS_TYPE_INVALID);
931                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.mailbox_id ), DBUS_TYPE_INVALID);
932                         break;
933                 case ACTIVE_SYNC_NOTI_DOWNLOAD_BODY:                    /*  publish a download body notification to ASE */
934                         EM_DEBUG_LOG("handle:[%d]", data->download_body.handle);
935                         EM_DEBUG_LOG("account_id:[%d]", data->download_body.account_id);
936                         EM_DEBUG_LOG("mail_id:[%d]", data->download_body.mail_id);
937                         EM_DEBUG_LOG("with_attachment:[%d]", data->download_body.with_attachment);
938
939                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.handle  ), DBUS_TYPE_INVALID);
940                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.account_id  ), DBUS_TYPE_INVALID);
941                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.mail_id ), DBUS_TYPE_INVALID);
942                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.with_attachment  ), DBUS_TYPE_INVALID);
943                         break;
944                 case ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT:
945                         EM_DEBUG_LOG("handle:[%d]", data->download_attachment.handle);
946                         EM_DEBUG_LOG("account_id:[%d]", data->download_attachment.account_id );
947                         EM_DEBUG_LOG("mail_id:[%d]", data->download_attachment.mail_id);
948                         EM_DEBUG_LOG("with_attachment:[%d]", data->download_attachment.attachment_order );
949
950                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.handle  ), DBUS_TYPE_INVALID);
951                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.account_id  ), DBUS_TYPE_INVALID);
952                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.mail_id ), DBUS_TYPE_INVALID);
953                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.attachment_order), DBUS_TYPE_INVALID);
954                         break;
955                 case ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT:
956                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT]", subType);
957                         break;
958                 case ACTIVE_SYNC_NOTI_CANCEL_JOB:
959                         EM_DEBUG_LOG("account_id:[%d]",       data->cancel_job.account_id );
960                         EM_DEBUG_LOG("handle to cancel:[%d]", data->cancel_job.handle);
961                         EM_DEBUG_LOG("cancel_type:[%d]",      data->cancel_job.cancel_type);
962
963                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.account_id  ), DBUS_TYPE_INVALID);
964                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.handle  ), DBUS_TYPE_INVALID);
965                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.cancel_type  ), DBUS_TYPE_INVALID);
966                         break;
967                 case ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER:
968                         EM_DEBUG_LOG("account_id:[%d]",          data->search_mail_on_server.account_id );
969                         EM_DEBUG_LOG("mailbox_id:[%d]",        data->search_mail_on_server.mailbox_id );
970                         EM_DEBUG_LOG("search_filter_count:[%d]", data->search_mail_on_server.search_filter_count );
971                         EM_DEBUG_LOG("handle to cancel:[%d]",    data->search_mail_on_server.handle);
972
973                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.account_id), DBUS_TYPE_INVALID);
974                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.mailbox_id), DBUS_TYPE_INVALID);
975                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.search_filter_count), DBUS_TYPE_INVALID);
976                         for(i = 0; i < data->search_mail_on_server.search_filter_count; i++) {
977                                 dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.search_filter_list[i].search_filter_type), DBUS_TYPE_INVALID);
978                                 switch(data->search_mail_on_server.search_filter_list[i].search_filter_type) {
979                                         case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       :
980                                         case EMAIL_SEARCH_FILTER_TYPE_UID              :
981                                         case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      :
982                                         case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     :
983                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   :
984                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    :
985                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      :
986                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     :
987                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     :
988                                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       :
989                                                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.integer_type_key_value), DBUS_TYPE_INVALID);
990                                                 break;
991
992                                         case EMAIL_SEARCH_FILTER_TYPE_BCC              :
993                                         case EMAIL_SEARCH_FILTER_TYPE_CC               :
994                                         case EMAIL_SEARCH_FILTER_TYPE_FROM             :
995                                         case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
996                                         case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
997                                         case EMAIL_SEARCH_FILTER_TYPE_TO               :
998                                         case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
999                                                 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.string_type_key_value), DBUS_TYPE_INVALID);
1000                                                 break;
1001
1002                                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
1003                                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     :
1004                                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  :
1005                                                 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value), DBUS_TYPE_INVALID);
1006                                                 break;
1007                                         default :
1008                                                 EM_DEBUG_EXCEPTION("Invalid filter type [%d]", data->search_mail_on_server.search_filter_list[i].search_filter_type);
1009                                                 break;
1010                                 }
1011                         }
1012                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.handle), DBUS_TYPE_INVALID);
1013                         break;
1014
1015                 case ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER :
1016                         EM_DEBUG_LOG("account_id:[%d]",          data->clear_result_of_search_mail_on_server.account_id );
1017                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.account_id), DBUS_TYPE_INVALID);
1018                         break;
1019
1020                 case ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED :
1021                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.mailbox_id  ), DBUS_TYPE_INVALID);
1022                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.on_server  ), DBUS_TYPE_INVALID);
1023                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.handle  ), DBUS_TYPE_INVALID);
1024                         break;
1025
1026                 case ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT :
1027                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->get_resolve_recipients.account_id), DBUS_TYPE_INVALID);
1028                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->get_resolve_recipients.email_address), DBUS_TYPE_INVALID);
1029                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->get_resolve_recipients.handle), DBUS_TYPE_INVALID);
1030                         break;
1031
1032                 case ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE :
1033                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->validate_certificate.account_id), DBUS_TYPE_INVALID);
1034                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->validate_certificate.email_address), DBUS_TYPE_INVALID);
1035                         dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->validate_certificate.handle), DBUS_TYPE_INVALID);
1036                         break;
1037
1038                 case ACTIVE_SYNC_NOTI_ADD_MAILBOX :
1039                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->add_mailbox.account_id), DBUS_TYPE_INVALID);
1040                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->add_mailbox.mailbox_path), DBUS_TYPE_INVALID);
1041                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->add_mailbox.mailbox_alias), DBUS_TYPE_INVALID);
1042                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->add_mailbox.handle), DBUS_TYPE_INVALID);
1043                         break;
1044
1045                 case ACTIVE_SYNC_NOTI_RENAME_MAILBOX :
1046                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->rename_mailbox.account_id), DBUS_TYPE_INVALID);
1047                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->rename_mailbox.mailbox_id), DBUS_TYPE_INVALID);
1048                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->rename_mailbox.mailbox_name), DBUS_TYPE_INVALID);
1049                         dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->rename_mailbox.mailbox_alias), DBUS_TYPE_INVALID);
1050                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->rename_mailbox.handle), DBUS_TYPE_INVALID);
1051                         break;
1052
1053                 case ACTIVE_SYNC_NOTI_DELETE_MAILBOX :
1054                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->delete_mailbox.account_id), DBUS_TYPE_INVALID);
1055                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->delete_mailbox.mailbox_id), DBUS_TYPE_INVALID);
1056                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->delete_mailbox.handle), DBUS_TYPE_INVALID);
1057                         break;
1058
1059                 default:
1060                         EM_DEBUG_EXCEPTION("Invalid Notification type of Active Sync : subType[%d]", subType);
1061                         return FAILURE;
1062         }
1063
1064         if(!dbus_connection_send (connection, signal, NULL)) {
1065                 EM_DEBUG_EXCEPTION("dbus_connection_send is failed");
1066                 return FAILURE;
1067         } else
1068                 EM_DEBUG_LOG("dbus_connection_send is successful");
1069
1070         dbus_connection_flush(connection);
1071
1072 FINISH_OFF:
1073
1074         if(signal)
1075                 dbus_message_unref(signal);
1076
1077         EM_DEBUG_FUNC_END();
1078         return true;
1079 }