Fixed the define
[framework/api/email.git] / src / email.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18 #include <memory.h>
19 #include <dlog.h>
20 #include <E_DBus.h>
21 #include <email-api-mail.h>
22 #include <email-api-account.h>
23 #include <email-api-network.h>
24 #include <email-api-mailbox.h>
25 #include <email-api-init.h>
26 #include <email-api.h>
27
28 #define EMAIL_API_ERROR_NONE                    1
29 #define EMAIL_API_ERROR_OUT_OF_MEMORY           -1028
30 #define EMAIL_API_ERROR_ACCOUNT_NOT_FOUND       -1014
31
32 #undef EMAIL_ERROR_NONE
33 #undef EMAIL_ERROR_OUT_OF_MEMORY
34 #undef EMAIL_ERROR_ACCOUNT_NOT_FOUND
35
36 #include<email.h>
37 #include<email_private.h>
38 #include<email_types.h>
39 #include<email_error.h>
40
41 #ifdef LOG_TAG
42 #undef LOG_TAG
43 #endif
44 #define LOG_TAG "CAPI_EMAIL"
45 #define DBG_MODE (1)
46
47 #define EM_SAFE_STRDUP(s) \
48         ({\
49                 char* _s = (char*)s;\
50                 (_s)? strdup(_s) : NULL;\
51         })
52
53 typedef struct {
54         email_message_sent_cb  callback;
55         email_s *handle;
56         void *user_data;
57 } email_cb_context;
58
59 GSList *gEmailcbList= NULL;
60 //------------- Utility Or Miscellaneous
61 void _email_add_dbus_filter(void);
62 int _email_error_converter(int err, const char *func, int line);
63 int _email_copy_handle(email_s **dst_handle, email_s *src_handle);
64 void _email_free_cb_context(email_cb_context *cbcontext);
65
66 #define CONVERT_ERROR(err) _email_error_converter(err, __FUNCTION__, __LINE__);
67
68 //------------------------------------
69
70 int email_create_message(email_h *msg)
71 {
72         int ret;
73         email_s * msg_s = NULL;
74         email_account_t* account = NULL;
75         int len;
76
77         if(msg == NULL) {
78                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is NULL.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
79                 return EMAIL_ERROR_INVALID_PARAMETER;
80         }
81
82         // 1. create service for ipc
83         ret=email_service_begin();
84         msg_s= (email_s*)calloc(1,sizeof(email_s));
85         if (msg_s != NULL)
86         {
87                 msg_s->mail = (email_mail_data_t *)calloc(1,sizeof(email_mail_data_t));
88                 if (msg_s->mail == NULL) {
89                         LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create msg_s->mail", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
90                         free(msg_s);
91                         return EMAIL_ERROR_OUT_OF_MEMORY;
92                 }
93
94                 msg_s->mbox = (email_mailbox_t *)calloc(1,sizeof(email_mailbox_t));
95                 if (msg_s->mbox == NULL)
96                 {
97                         LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create msg_s->mbox", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
98                         free(msg_s);
99                         return EMAIL_ERROR_OUT_OF_MEMORY;
100                 }
101                 
102         
103         }
104         else
105         {
106                 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create msg_s", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
107                 return EMAIL_ERROR_OUT_OF_MEMORY;
108         }
109
110                 
111         //return error from F/W 
112         //EMAIL_ERROR_INVALID_PARAM/EMAIL_API_ERROR_NONE/EMAIL_ERROR_DB_FAILURE/EMAIL_ERROR_ACCOUNT_NOT_FOUND/EMAIL_ERROR_OUT_OF_MEMORY
113         int default_account_id = 0;
114         if ((ret = email_load_default_account_id(&default_account_id)) != EMAIL_API_ERROR_NONE) {
115                 LOGE("[%s] email_load_default_account_id failed : [%d]",__FUNCTION__, ret);
116                 return CONVERT_ERROR(ret);
117         }
118         
119         ret = email_get_account(default_account_id, GET_FULL_DATA, &account);
120         if(ret!=EMAIL_API_ERROR_NONE) return CONVERT_ERROR(ret);
121
122         LOGD_IF(DBG_MODE,"account address = %s",account->user_email_address);
123         LOGD_IF(DBG_MODE,"account id = %d",account->account_id);
124         LOGD_IF(DBG_MODE,"account name = %s",account->account_name);
125         LOGD_IF(DBG_MODE,"account user_name = %s",account->incoming_server_user_name);
126         
127         msg_s->mail->full_address_from = (char *)calloc(1,sizeof(char)*(strlen(account->incoming_server_user_name)+strlen(account->user_email_address)+1+1+1+1+1));//"++"+<+ address +> + NULL
128         len= (strlen(account->incoming_server_user_name)+strlen(account->user_email_address)+1+1+1+1+1);
129         char *strfrom = msg_s->mail->full_address_from;
130
131         snprintf(strfrom,len,"%s%s%s%s%s%s","\"",account->incoming_server_user_name,"\"","<",account->user_email_address,">");
132
133         //mbox
134         email_mailbox_t *mbox =msg_s->mbox;
135
136         if ( (ret = email_get_mailbox_by_mailbox_type(default_account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &mbox)) != EMAIL_API_ERROR_NONE) {
137                 LOGE("[%s] email_get_mailbox_by_mailbox_type failed %d", __FUNCTION__, ret);
138                 return EMAIL_ERROR_DB_FAILED;
139         }
140
141         //info
142         msg_s->mail->account_id = account->account_id;
143         msg_s->mail->flags_draft_field = 1;
144         msg_s->mail->flags_seen_field = 1;
145         msg_s->mail->priority = EMAIL_MAIL_PRIORITY_NORMAL;
146         msg_s->mail->mailbox_id = mbox->mailbox_id;
147         msg_s->mail->mailbox_type = mbox->mailbox_type;
148         msg_s->mail->attachment_count = 0;
149
150         *msg = (email_h)msg_s;
151         return EMAIL_ERROR_NONE;
152 }
153
154 int email_destroy_message(email_h msg)
155 {
156         int ret;
157
158
159         if(msg == NULL) {
160                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is NULL.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
161                 return EMAIL_ERROR_INVALID_PARAMETER;
162         }
163                 
164         email_s* msg_s = (email_s* )msg;
165
166         if(msg_s)
167         {
168                 if (msg_s->mail)
169                         email_free_mail_data(&msg_s->mail, 1);
170
171                 if (msg_s->mbox)
172                         email_free_mailbox(&msg_s->mbox, 1);
173
174                 free(msg_s);            
175         }
176
177         
178         ret=email_service_end();
179         
180         if(ret!=EMAIL_API_ERROR_NONE){
181                 LOGE("[%s] OPERATION_FAILED(0x%08x) : Finishing email service failed", __FUNCTION__, EMAIL_ERROR_OPERATION_FAILED);
182                 return EMAIL_ERROR_OPERATION_FAILED;
183                 }
184         
185                 
186         return EMAIL_ERROR_NONE;
187 }
188
189 int email_set_subject (email_h msg, const char *subject)
190 {
191         int len;
192         if(msg ==NULL)
193         {
194                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is NULL", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
195                 return EMAIL_ERROR_INVALID_PARAMETER;
196                 }
197                 
198         email_s* msg_s = (email_s* )msg;        
199
200         msg_s->mail->subject=(char*)calloc(1,sizeof(char)*strlen(subject)+1);
201
202         if(msg_s->mail->subject ==NULL)
203         {
204                 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create msg_s->mail->head->subject", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
205                 return EMAIL_ERROR_OUT_OF_MEMORY;
206                 }
207         
208         len =strlen(subject)+1;
209         snprintf(msg_s->mail->subject ,len,"%s",subject); 
210         
211         return EMAIL_ERROR_NONE;
212 }
213
214 int email_set_body (email_h msg, const char *body)
215 {
216         int len;
217
218         if (msg == NULL)
219         {
220                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
221                 return EMAIL_ERROR_INVALID_PARAMETER;
222         }
223         
224         email_s* msg_s = (email_s* )msg;        
225
226
227         FILE* file = NULL;
228
229         
230     file = fopen("/tmp/capimail.txt", "w");
231
232         if (file != NULL)
233         {
234                  fputs(body, file);
235                  fclose(file);
236         } else {
237                 LOGE("[%s] OPERATION_FAILED(0x%08x) : opening file for email body failed.", __FUNCTION__, EMAIL_ERROR_OPERATION_FAILED);
238                 return EMAIL_ERROR_OPERATION_FAILED;
239         }
240
241         msg_s->mail->file_path_plain =(char *)calloc(1,sizeof(char)*strlen("/tmp/capimail.txt")+1);
242
243         if(msg_s->mail->file_path_plain == NULL) {
244                 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to allocate body(plain).", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
245                 return EMAIL_ERROR_OUT_OF_MEMORY;
246         }
247         
248         len =strlen("/tmp/capimail.txt")+1;
249         snprintf(msg_s->mail->file_path_plain,len,"%s","/tmp/capimail.txt");
250         
251         return EMAIL_ERROR_NONE;
252 }
253
254  
255
256 int email_add_recipient (email_h msg, email_recipient_type_e type, const char *address)
257 {
258         char *tmp;
259         int total_len,in_len,pre_len,len;
260
261         if(msg == NULL || type < EMAIL_RECIPIENT_TYPE_TO || type > EMAIL_RECIPIENT_TYPE_BCC )
262         {
263                 return EMAIL_ERROR_INVALID_PARAMETER;
264         }
265         
266         email_s* msg_s = (email_s* )msg;
267         
268         if(strlen(address) > MAX_RECIPIENT_ADDRESS_LEN)
269         {
270                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : The length of address should be less than 234.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
271                 return EMAIL_ERROR_INVALID_PARAMETER;
272         }
273
274         if(type == EMAIL_RECIPIENT_TYPE_TO)
275         {
276                 if(msg_s->mail->full_address_to == NULL)
277                 {
278                         msg_s->mail->full_address_to = (char*)calloc(1,sizeof(char)*strlen(address)+2+1+1);//<>+;+end of string
279                         if(msg_s->mail->full_address_to == NULL)
280                         {
281                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create head->to.", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
282                                 return EMAIL_ERROR_OUT_OF_MEMORY;
283                         }
284                         len =strlen(address)+2+1+1;
285                         snprintf(msg_s->mail->full_address_to,len,"%s%s%s","<",address,">");
286                 } else {
287                         in_len = strlen(address);
288                         pre_len = strlen(msg_s->mail->full_address_to);
289                         total_len = pre_len+in_len+3+1;// length of ",<>" + NULL
290
291                         //add new address
292                         tmp = msg_s->mail->full_address_to;
293                         msg_s->mail->full_address_to = (char*)calloc(1,sizeof(char)*total_len);
294                         snprintf(msg_s->mail->full_address_to,total_len,"%s%s%s%s",tmp,",<",address,">");
295                         free(tmp);
296                 }
297                 
298         }
299         else if(type == EMAIL_RECIPIENT_TYPE_CC)//MESSAGING_RECIPIENT_TYPE_CC
300         {
301                 if(msg_s->mail->full_address_cc == NULL)
302                 {
303                         msg_s->mail->full_address_cc = (char*)calloc(1,sizeof(char)*strlen(address)+2+1+1);//<>+;+end of string
304                         if(msg_s->mail->full_address_cc == NULL)
305                         {
306                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create head->cc.", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
307                                 return EMAIL_ERROR_OUT_OF_MEMORY;
308                         }
309                         len =strlen(address)+2+1+1;
310                         snprintf(msg_s->mail->full_address_cc,len,"%s%s%s","<",address,">");
311                 } else {
312
313                         in_len = strlen(address);
314                         pre_len = strlen(msg_s->mail->full_address_cc);
315                         total_len = pre_len+in_len+3+1;// length of ",<>" + NULL
316
317                         //add new address
318                         tmp = msg_s->mail->full_address_cc;
319                         msg_s->mail->full_address_cc = (char*)calloc(1,sizeof(char)*total_len);
320                         snprintf(msg_s->mail->full_address_cc,total_len,"%s%s%s%s",tmp,",<",address,">");
321                         free(tmp);
322                 }
323                 
324         }
325         else //MESSAGING_RECIPIENT_TYPE_BCC
326         {
327                 if(msg_s->mail->full_address_bcc == NULL)
328                 {
329                         msg_s->mail->full_address_bcc = (char*)calloc(1,sizeof(char)*strlen(address)+2+1+1);//<>+;+end of string
330                         if(msg_s->mail->full_address_bcc==NULL)
331                         {
332                                 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create head->bcc.", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
333                                 return EMAIL_ERROR_OUT_OF_MEMORY;
334                         }
335                         len =strlen(address)+2+1+1;
336                         snprintf(msg_s->mail->full_address_bcc,len,"%s%s%s","<",address,">");
337                 }
338                 else{
339
340                         in_len = strlen(address);
341                         pre_len = strlen(msg_s->mail->full_address_bcc);
342                         total_len = pre_len+in_len+3+1;// length of ",<>" + NULL
343
344                         //add new address
345                         tmp = msg_s->mail->full_address_bcc;
346                         msg_s->mail->full_address_bcc = (char*)calloc(1,sizeof(char)*total_len);
347                         snprintf(msg_s->mail->full_address_bcc,total_len,"%s%s%s%s",tmp,",<",address,">");
348                         free(tmp);
349                 }
350         }
351
352         return EMAIL_ERROR_NONE;
353 }
354
355 int email_remove_all_recipients(email_h msg)
356 {
357         if(msg ==NULL)
358         {
359                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : fail to create tmp memory.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
360                 return EMAIL_ERROR_INVALID_PARAMETER;
361         }
362         
363         email_s *msg_s = (email_s *)msg;
364
365         if(msg_s->mail->full_address_to != NULL)
366         {
367                 free(msg_s->mail->full_address_to);
368                 msg_s->mail->full_address_to = NULL;
369         }
370
371         if(msg_s->mail->full_address_cc != NULL)
372         {
373                 free(msg_s->mail->full_address_cc);
374                 msg_s->mail->full_address_cc = NULL;
375         }
376
377         if(msg_s->mail->full_address_bcc != NULL)
378         {
379                 free(msg_s->mail->full_address_bcc);
380                 msg_s->mail->full_address_bcc = NULL;
381         }
382         return EMAIL_ERROR_NONE;
383 }
384
385
386 int email_add_attach (email_h msg, const char *filepath)
387 {
388         int len;
389         char *pos,*last;
390         struct stat st;
391
392         if(msg ==NULL || filepath == NULL)
393         {
394                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg or filepath is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
395                 return EMAIL_ERROR_INVALID_PARAMETER;
396         }
397
398         email_s *msg_s = (email_s *)msg;
399
400         int attachment_count = msg_s->mail->attachment_count;
401         email_attachment_data_t *new_attach = msg_s->attachment;
402
403         stat(filepath, &st);
404         if(st.st_size > 10*1024*1024)
405         {
406                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : the size of attachment file is beyond the limit(MAX:10M).", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
407                 return EMAIL_ERROR_INVALID_PARAMETER;
408         }
409
410         if (!S_ISREG(st.st_mode))
411         {
412                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : the filepath is not regular file.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
413                 return EMAIL_ERROR_INVALID_PARAMETER;
414         }
415                 
416
417         pos=strpbrk(filepath, "//");
418         len =strlen(filepath); 
419         if(pos == NULL)
420         {
421                 new_attach[attachment_count].attachment_name = (char*)calloc(1,sizeof(char)*len+1);
422                 snprintf(new_attach[attachment_count].attachment_name,len+1,"%s",filepath); 
423         }
424         else
425         {
426                 while(pos != NULL)
427                 {
428                         last=pos;
429                         pos=strpbrk(pos,"//");  
430                         if(pos != NULL) pos++;
431                 }
432
433                 new_attach[attachment_count].attachment_name =last;
434         }
435         new_attach[attachment_count].attachment_path =(char*)calloc(1,sizeof(char)*len+1);
436         if (new_attach[attachment_count].attachment_path == NULL) return EMAIL_ERROR_OUT_OF_MEMORY;
437
438         snprintf(new_attach[attachment_count].attachment_path, len+1, "%s", filepath); 
439         new_attach[attachment_count].attachment_size = st.st_size;
440         new_attach[attachment_count].save_status = 1;
441
442         msg_s->mail->attachment_count++;
443
444         return EMAIL_ERROR_NONE;
445
446 }
447
448 int email_remove_all_attachments (email_h msg)
449 {
450         if (msg ==NULL) {
451                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
452                 return EMAIL_ERROR_INVALID_PARAMETER;
453         }
454
455         email_s* msg_s = (email_s* )msg;
456
457         msg_s->mail->attachment_count = 0;
458
459         return EMAIL_ERROR_NONE;
460 }
461
462
463 int email_send_message (email_h msg, bool save_to_sentbox)
464 {
465         int i, ret;
466         email_option_t option;
467         unsigned  handle;
468         email_attachment_data_t *tmp_attach = NULL;
469         struct tm *struct_time;
470
471         if (msg == NULL) {
472                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
473                 return EMAIL_ERROR_INVALID_PARAMETER;
474         }       
475
476         email_s *msg_s = (email_s *)msg;
477         
478         {
479                 /*--------- head ----------*/
480                 LOGD_IF(DBG_MODE, " ----------head---------");
481                 LOGD_IF(DBG_MODE, "  mid: %s\n",msg_s->mail->message_id);
482                 LOGD_IF(DBG_MODE, "  subject: %s\n",msg_s->mail->subject);
483                 LOGD_IF(DBG_MODE, "  to: %s\n",msg_s->mail->full_address_to);
484                 LOGD_IF(DBG_MODE, "  from: %s\n",msg_s->mail->full_address_from);
485                 LOGD_IF(DBG_MODE, "  cc: %s\n",msg_s->mail->full_address_cc);
486                 LOGD_IF(DBG_MODE, "  bcc: %s\n",msg_s->mail->full_address_bcc);
487                 LOGD_IF(DBG_MODE, "  reply_to: %s\n",msg_s->mail->full_address_reply);
488                 LOGD_IF(DBG_MODE, "  return_path: %s\n",msg_s->mail->full_address_return);
489                 LOGD_IF(DBG_MODE, "  previewBodyText: %s\n",msg_s->mail->preview_text);
490
491                 struct_time = localtime(&msg_s->mail->date_time);
492
493                 LOGD_IF(DBG_MODE, "  %4d year\n", struct_time->tm_year+1900);
494                 LOGD_IF(DBG_MODE, "  %2d month\n", struct_time->tm_mon+1);
495                 LOGD_IF(DBG_MODE, "  %2d day\n", struct_time->tm_mday);
496                 LOGD_IF(DBG_MODE, "  %2d:%2d:%2d \n", struct_time->tm_hour, struct_time->tm_min, struct_time->tm_sec);
497
498                 /*--------- body ----------*/
499                 LOGD_IF(DBG_MODE, " ----------body---------");
500                 LOGD_IF(DBG_MODE, "  body\n");
501                 LOGD_IF(DBG_MODE, "  plain: %s\n",msg_s->mail->file_path_plain);
502                 LOGD_IF(DBG_MODE, "  html: %s\n",msg_s->mail->file_path_html);
503                 LOGD_IF(DBG_MODE, "  attachment_num: %d\n",msg_s->mail->attachment_count);
504                 tmp_attach = msg_s->attachment;
505                 for (i=0; i < msg_s->mail->attachment_count; i++) {
506                         LOGD_IF(DBG_MODE, " ----------attachment[%d]---------", i+1);
507                         LOGD_IF(DBG_MODE, "  name: %s\n",tmp_attach[i].attachment_name);
508                         LOGD_IF(DBG_MODE, "  savename: %s\n",tmp_attach[i].attachment_path);
509                         LOGD_IF(DBG_MODE, "  downloaded: %d\n",tmp_attach[i].save_status);
510                         LOGD_IF(DBG_MODE, "  size: %d\n",tmp_attach[i].attachment_size);
511                 }               
512         }
513
514         {
515                         /*--------- info ----------*/
516                 LOGD_IF(DBG_MODE, " ----------info---------");
517                 LOGD_IF(DBG_MODE, "  account_id: %d\n",msg_s->mail->account_id);
518                 LOGD_IF(DBG_MODE, "  mail_id: %d\n",msg_s->mail->mail_id);
519                 LOGD_IF(DBG_MODE, "  mail_size: %d\n",msg_s->mail->mail_size);
520                 LOGD_IF(DBG_MODE, "  body_download_status: %d\n",msg_s->mail->body_download_status);
521                 LOGD_IF(DBG_MODE, "  server_id: %s\n",msg_s->mail->server_mail_id);
522                 LOGD_IF(DBG_MODE, "  meeting_request_status: %d\n",msg_s->mail->meeting_request_status);        
523         }
524
525         
526         {
527                 email_mailbox_t * box;
528                 box=msg_s->mbox;
529                 LOGD_IF(DBG_MODE, " ----------box---------");
530                 LOGD_IF(DBG_MODE, "  email_mailbox_t \n");
531                 LOGD_IF(DBG_MODE, "  name: %s\n",box->mailbox_name);
532                 LOGD_IF(DBG_MODE, "  mailbox_type: %d\n",box->mailbox_type);
533                 LOGD_IF(DBG_MODE, "  alias: %s\n",box->alias);
534                 LOGD_IF(DBG_MODE, "  unread_count: %d\n",box->unread_count);
535                 LOGD_IF(DBG_MODE, "  total_mail_count_on_local: %d\n",box->total_mail_count_on_local);
536                 LOGD_IF(DBG_MODE, "  total_mail_count_on_server: %d\n",box->total_mail_count_on_server);
537                 LOGD_IF(DBG_MODE, "  local: %d\n",box->local);
538                 LOGD_IF(DBG_MODE, "  account_id: %d\n",box->account_id);
539                 LOGD_IF(DBG_MODE, "  mail_slot_size: %d\n",box->mail_slot_size);
540         }
541
542
543         ret=email_add_mail(msg_s->mail, msg_s->attachment, msg_s->mail->attachment_count, NULL, 0);
544         ret=CONVERT_ERROR(ret);
545         
546
547         memset(&option, 0x00, sizeof(email_option_t));
548         option.keep_local_copy = save_to_sentbox ? 1 : 0;
549         
550
551         ret=email_send_mail(msg_s->mail->mail_id, &option, &handle);
552
553
554         ret=CONVERT_ERROR(ret);
555         _email_add_dbus_filter();
556         return ret;
557 }
558
559
560 email_cb_context * _email_search_callback_by_emailid(int mailid)
561 {
562         int count;
563         int ntmp=0;
564         GSList * node;
565         email_cb_context *cbContext;
566         count = g_slist_length( gEmailcbList );
567
568         while( count )
569         {
570                 node = g_slist_nth( gEmailcbList, ntmp );
571                         
572                         if( node == NULL )
573                                 break;
574                         
575                         cbContext= (email_cb_context *)node->data; 
576                         if(cbContext->handle->mail->mail_id == mailid)
577                         {
578                                 return cbContext;
579                         }
580                                 
581                                 
582                 ntmp++; 
583                 count--;
584         }
585
586         return NULL;
587 }
588
589
590 int email_set_message_sent_cb (email_h handle, email_message_sent_cb cb, void *user_data)
591 {
592         int count;
593         int ntmp=0;
594         int ret = EMAIL_ERROR_NONE;
595         GSList * node;
596         email_cb_context *cbContext;
597         count = g_slist_length( gEmailcbList );
598
599         if(handle ==NULL || cb == NULL)return EMAIL_ERROR_INVALID_PARAMETER;
600                 
601         
602         email_s* msg_s = NULL;
603         
604         while( count )
605         {
606                 node = g_slist_nth( gEmailcbList, ntmp );
607                         
608                 if( node == NULL )
609                         break;
610                 
611                 cbContext= (email_cb_context *)node->data; 
612                 if(cbContext->handle == (email_s*)handle)
613                 {
614                         gEmailcbList=g_slist_remove(gEmailcbList,node);
615                         _email_free_cb_context(cbContext);      
616                         break;
617                 }
618                                 
619                 ntmp++; 
620                 count--;
621         }
622
623         if ((ret = _email_copy_handle(&msg_s, (email_s *)handle)) != EMAIL_ERROR_NONE) {
624                 LOGE("[%s] _email_copy_handle failed", __FUNCTION__);
625                 return ret;
626         }
627
628         email_cb_context * cbcontext= (email_cb_context*)calloc(1, sizeof(email_cb_context) );
629
630         cbcontext->handle = msg_s;
631         cbcontext->callback=cb;
632         cbcontext->user_data =user_data;
633
634         gEmailcbList = g_slist_append(gEmailcbList,cbcontext);
635
636         return EMAIL_ERROR_NONE;
637 }
638
639 int email_unset_message_sent_cb (email_h msg)
640 {
641
642         int i,count;
643         count = g_slist_length( gEmailcbList );
644         GSList * node;
645         email_cb_context *cbContext;
646
647         if(msg ==NULL )
648         {
649                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
650                 return EMAIL_ERROR_INVALID_PARAMETER;
651                 }
652         email_s* msg_s = (email_s* )msg;
653         for( i=0;i<count;i++)
654         {
655                 node = g_slist_nth( gEmailcbList, i );
656                 if( node == NULL )
657                 break;
658                 
659                 
660                 cbContext= (email_cb_context *)node->data; 
661                 if(cbContext->handle == msg_s)
662                 {       
663                         gEmailcbList= g_slist_remove(gEmailcbList,node);
664                         _email_free_cb_context(cbContext);
665                         break;
666                 }
667                 
668
669                         
670         }
671
672         return EMAIL_ERROR_NONE;
673 }
674
675 int _email_error_converter(int err, const char *func, int line)
676 {
677         switch(err) 
678         {
679                 case EMAIL_ERROR_INVALID_PARAM :
680                         LOGE("[%s:%d] INVALID_PARAM(0x%08x) : Error from Email F/W. ret: (0x%08x) ", func, line, EMAIL_ERROR_INVALID_PARAMETER, err);
681                         return EMAIL_ERROR_INVALID_PARAMETER;
682
683                 case EMAIL_ERROR_DB_FAILURE :
684                         LOGE("[%s:%d] DB_FAILURE(0x%08x) : Error from Email F/W. ret: (0x%08x) ", func, line, EMAIL_ERROR_DB_FAILED, err);
685                         return EMAIL_ERROR_DB_FAILED;
686
687                 case EMAIL_API_ERROR_ACCOUNT_NOT_FOUND :
688                         LOGE("[%s:%d] ACCOUNT_NOT_FOUND(0x%08x) : Error from Email F/W. ret: (0x%08x) ", func, line, EMAIL_ERROR_ACCOUNT_NOT_FOUND,err);
689                         return EMAIL_ERROR_ACCOUNT_NOT_FOUND;
690
691                 case EMAIL_API_ERROR_OUT_OF_MEMORY :
692                         LOGE("[%s:%d] OUT_OF_MEMORY(0x%08x) : Error from Email F/W. ret: (0x%08x) ", func, line, EMAIL_ERROR_OUT_OF_MEMORY,err);
693                         return EMAIL_ERROR_OUT_OF_MEMORY;
694                         
695                 // Tizen email F/W  is often using this error type when it gets a null value from server
696                 //It could be caused from server or IPC.
697                 case EMAIL_ERROR_NULL_VALUE :
698                         LOGE("[%s:%d] NULL_VALUE(0x%08x) : Error from Email F/W. ret: (0x%08x) ", func, line, EMAIL_ERROR_COMMUNICATION_WITH_SERVER_FAILED,err);
699                         return EMAIL_ERROR_COMMUNICATION_WITH_SERVER_FAILED;
700
701                 case EMAIL_ERROR_IPC_SOCKET_FAILURE :
702                         LOGE("[%s:%d] IPC_SOCKET_FAILURE(0x%08x) : Error from Email F/W. ret: (0x%08x) ", func, line, EMAIL_ERROR_COMMUNICATION_WITH_SERVER_FAILED,err);
703                         return EMAIL_ERROR_COMMUNICATION_WITH_SERVER_FAILED;
704
705                 case EMAIL_API_ERROR_NONE :
706                         return EMAIL_ERROR_NONE;
707
708                 default :
709                         LOGE("[%s:%d] OPERATION_FAILED(0x%08x) : Error from Email F/W. ret: (0x%08x) ", func, line, EMAIL_ERROR_OPERATION_FAILED,err);
710                         return EMAIL_ERROR_OPERATION_FAILED;
711
712         }
713 }
714
715
716
717 static void _monitorSendStatusCb(void* data, DBusMessage *message)
718 {
719         DBusError error;
720         email_cb_context *cbContext = NULL;
721         if(dbus_message_is_signal(message, "User.Email.NetworkStatus", "email"))
722         {
723                 dbus_error_init(&error);
724
725                 int status=0;
726                 int accountid=0;
727                 char* fileid=NULL;
728                 int mailid=0;
729                 int errorcode=0;
730                 if(dbus_message_get_args(message, &error, 
731                         DBUS_TYPE_INT32, &status, 
732                         DBUS_TYPE_INT32, &accountid, 
733                         DBUS_TYPE_STRING, &fileid, 
734                         DBUS_TYPE_INT32, &mailid,
735                         DBUS_TYPE_INT32, &errorcode,
736                         DBUS_TYPE_INVALID))
737                         {
738
739
740                                 cbContext = _email_search_callback_by_emailid(mailid);
741                                 if(cbContext == NULL)
742                                         {
743                                                 LOGD_IF(DBG_MODE, "no callback matched!\n");
744                                 }else{
745                                         
746                                                 switch (status) {
747                                                         case NOTI_SEND_START:
748                                                                 break;
749                                                                 
750                                                         case NOTI_SEND_FAIL:
751
752                                                                  switch(errorcode)
753                                                              {
754                                                                 case EMAIL_ERROR_NO_SIM_INSERTED:
755                                                                 case EMAIL_ERROR_FLIGHT_MODE:
756                                                                 case EMAIL_ERROR_SMTP_SEND_FAILURE:
757                                                                 case EMAIL_ERROR_NO_SUCH_HOST:
758                                                                 case EMAIL_ERROR_CONNECTION_FAILURE:
759                                                                 case EMAIL_ERROR_CONNECTION_BROKEN:
760                                                                 case EMAIL_ERROR_INVALID_SERVER:
761                                                                 case EMAIL_ERROR_NO_RESPONSE:
762                                                                     
763                                                                     break;
764
765                                                                 default:
766                                                                                                 break;
767                                                              }
768                                                                 
769                                                                  cbContext->callback((email_h)cbContext->handle,(email_sending_e)EMAIL_SENDING_FAILED ,cbContext->user_data);
770                                                                 break;
771
772                                                         case NOTI_SEND_FINISH:
773                                                                 cbContext->callback((email_h)cbContext->handle,(email_sending_e)EMAIL_SENDING_SUCCEEDED ,cbContext->user_data);
774                                                                 break;
775                                         
776                                                 }
777                                         }
778                         }
779         }
780 }
781
782 void _email_add_dbus_filter(void)
783 {
784         
785         E_DBus_Connection* connection=NULL;
786         DBusError error;
787         E_DBus_Signal_Handler *handler = NULL;
788         
789
790         dbus_error_init(&error);
791
792         connection = e_dbus_bus_get(DBUS_BUS_SYSTEM);
793
794         if (NULL == connection) {
795  
796         }
797
798         if(e_dbus_request_name(connection, "User.Email.NetworkStatus",
799                                      0,
800                                      NULL,
801                                      NULL) == NULL)
802         {
803                 LOGD_IF(DBG_MODE, "Failed in e_dbus_request_name()\n");
804         }
805
806          handler = e_dbus_signal_handler_add(connection,
807                         NULL,
808                         "/User/Email/NetworkStatus", 
809                         "User.Email.NetworkStatus", 
810                         "email",
811                         _monitorSendStatusCb, 
812                         NULL);
813
814         if(handler == NULL)
815         {
816                 LOGD_IF(DBG_MODE, "Failed in e_dbus_signal_handler_add()");
817         }
818 }
819
820 int _email_copy_mail_data(email_mail_data_t **dst_mail_data, email_mail_data_t *src_mail_data)
821 {
822         email_mail_data_t *temp_mail_data = NULL;
823         
824         temp_mail_data = (email_mail_data_t *)calloc(1, sizeof(email_mail_data_t));
825         if (temp_mail_data == NULL) {
826                 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create email_mail_data_t", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
827                 return EMAIL_ERROR_OUT_OF_MEMORY;
828         }
829
830         temp_mail_data->mail_id                 = src_mail_data->mail_id;
831         temp_mail_data->account_id              = src_mail_data->account_id;
832         temp_mail_data->mailbox_id              = src_mail_data->mailbox_id;
833         temp_mail_data->mailbox_type            = src_mail_data->mailbox_type;
834         temp_mail_data->subject                 = EM_SAFE_STRDUP(src_mail_data->subject);
835         temp_mail_data->date_time               = src_mail_data->date_time;
836         temp_mail_data->server_mail_status      = src_mail_data->server_mail_status;
837         temp_mail_data->server_mailbox_name     = EM_SAFE_STRDUP(src_mail_data->server_mailbox_name);
838         temp_mail_data->server_mail_id          = EM_SAFE_STRDUP(src_mail_data->server_mail_id);
839         temp_mail_data->message_id              = EM_SAFE_STRDUP(src_mail_data->message_id);
840         temp_mail_data->full_address_from       = EM_SAFE_STRDUP(src_mail_data->full_address_from);
841         temp_mail_data->full_address_reply      = EM_SAFE_STRDUP(src_mail_data->full_address_reply);
842         temp_mail_data->full_address_to         = EM_SAFE_STRDUP(src_mail_data->full_address_to);
843         temp_mail_data->full_address_cc         = EM_SAFE_STRDUP(src_mail_data->full_address_cc);
844         temp_mail_data->full_address_bcc        = EM_SAFE_STRDUP(src_mail_data->full_address_bcc);
845         temp_mail_data->full_address_return     = EM_SAFE_STRDUP(src_mail_data->full_address_return);
846         temp_mail_data->email_address_sender    = EM_SAFE_STRDUP(src_mail_data->email_address_sender);
847         temp_mail_data->email_address_recipient = EM_SAFE_STRDUP(src_mail_data->email_address_recipient);
848         temp_mail_data->alias_sender            = EM_SAFE_STRDUP(src_mail_data->alias_sender);
849         temp_mail_data->alias_recipient         = EM_SAFE_STRDUP(src_mail_data->alias_recipient);
850         temp_mail_data->body_download_status    = src_mail_data->body_download_status;
851         temp_mail_data->file_path_plain         = EM_SAFE_STRDUP(src_mail_data->file_path_plain);
852         temp_mail_data->file_path_html          = EM_SAFE_STRDUP(src_mail_data->file_path_html);
853         temp_mail_data->file_path_mime_entity   = EM_SAFE_STRDUP(src_mail_data->file_path_mime_entity);
854         temp_mail_data->mail_size               = src_mail_data->mail_size;
855         temp_mail_data->flags_seen_field        = src_mail_data->flags_seen_field;
856         temp_mail_data->flags_deleted_field     = src_mail_data->flags_deleted_field;
857         temp_mail_data->flags_flagged_field     = src_mail_data->flags_flagged_field;
858         temp_mail_data->flags_answered_field    = src_mail_data->flags_answered_field;
859         temp_mail_data->flags_recent_field      = src_mail_data->flags_recent_field;
860         temp_mail_data->flags_draft_field       = src_mail_data->flags_draft_field;
861         temp_mail_data->flags_forwarded_field   = src_mail_data->flags_forwarded_field;
862         temp_mail_data->DRM_status              = src_mail_data->DRM_status;
863         temp_mail_data->priority                = src_mail_data->priority;
864         temp_mail_data->save_status             = src_mail_data->save_status;
865         temp_mail_data->lock_status             = src_mail_data->lock_status;
866         temp_mail_data->report_status           = src_mail_data->report_status;
867         temp_mail_data->attachment_count        = src_mail_data->attachment_count;
868         temp_mail_data->inline_content_count    = src_mail_data->inline_content_count;
869         temp_mail_data->thread_id               = src_mail_data->thread_id;
870         temp_mail_data->thread_item_count       = src_mail_data->thread_item_count;
871         temp_mail_data->preview_text            = EM_SAFE_STRDUP(src_mail_data->preview_text);
872         temp_mail_data->meeting_request_status  = src_mail_data->meeting_request_status;
873         temp_mail_data->message_class           = src_mail_data->message_class;
874         temp_mail_data->digest_type             = src_mail_data->digest_type;
875         temp_mail_data->smime_type              = src_mail_data->smime_type;
876
877         *dst_mail_data = temp_mail_data;
878
879         return EMAIL_ERROR_NONE;
880 }
881
882 int _email_copy_mailbox(email_mailbox_t **dst_mailbox, email_mailbox_t *src_mailbox)
883 {
884         email_mailbox_t *temp_mailbox = NULL;
885
886         temp_mailbox = (email_mailbox_t *)calloc(1,sizeof(email_mailbox_t));
887         if (temp_mailbox == NULL)
888         {
889                 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create mailbox", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
890                 return EMAIL_ERROR_OUT_OF_MEMORY;
891         }
892
893         temp_mailbox->mailbox_id                    = src_mailbox->mailbox_id;
894         temp_mailbox->mailbox_name                   = EM_SAFE_STRDUP(src_mailbox->mailbox_name);
895         temp_mailbox->mailbox_type                  = src_mailbox->mailbox_type;
896         temp_mailbox->alias                         = EM_SAFE_STRDUP(src_mailbox->alias);
897         temp_mailbox->unread_count                  = src_mailbox->unread_count;
898         temp_mailbox->total_mail_count_on_local     = src_mailbox->total_mail_count_on_local;
899         temp_mailbox->total_mail_count_on_server    = src_mailbox->total_mail_count_on_server;
900         temp_mailbox->local                         = src_mailbox->local;
901         temp_mailbox->account_id                    = src_mailbox->account_id;
902         temp_mailbox->mail_slot_size                = src_mailbox->mail_slot_size;
903         temp_mailbox->last_sync_time                = src_mailbox->last_sync_time;
904
905         *dst_mailbox = temp_mailbox;
906
907         return EMAIL_ERROR_NONE;
908 }
909
910
911 int _email_copy_handle(email_s **dst_handle, email_s *src_handle)
912 {
913         int ret = EMAIL_ERROR_NONE;
914         email_s *msg_s = NULL;
915         
916         msg_s = (email_s *)calloc(1,sizeof(email_s));
917         if ((ret = _email_copy_mail_data(&msg_s->mail, src_handle->mail)) != EMAIL_ERROR_NONE) {
918                 LOGE("[%s] _email_copy_mail_data failed", __FUNCTION__);
919                 return ret;
920         }
921
922         if ((ret = _email_copy_mailbox(&msg_s->mbox, src_handle->mbox)) != EMAIL_ERROR_NONE) {
923                 LOGE("[%s] _email_copy_mailbox failed", __FUNCTION__);
924                 return ret;
925         }
926
927         *dst_handle = msg_s;
928         return ret;
929 }
930
931 void _email_free_cb_context(email_cb_context *cbcontext)
932 {
933         if(cbcontext == NULL) {
934                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is NULL.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
935                 return;
936         }
937                 
938         email_s* msg_s = cbcontext->handle;
939
940         if(msg_s)
941         {
942                 if (msg_s->mail)
943                         email_free_mail_data(&msg_s->mail, 1);
944
945                 if (msg_s->mbox)
946                         email_free_mailbox(&msg_s->mbox, 1);
947
948                 free(msg_s);            
949         }
950
951         cbcontext = NULL;
952 }