2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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
32 #undef EMAIL_ERROR_NONE
33 #undef EMAIL_ERROR_OUT_OF_MEMORY
34 #undef EMAIL_ERROR_ACCOUNT_NOT_FOUND
37 #include<email_private.h>
38 #include<email_types.h>
39 #include<email_error.h>
44 #define LOG_TAG "CAPI_EMAIL"
47 #define EM_SAFE_STRDUP(s) \
50 (_s)? strdup(_s) : NULL;\
54 email_message_sent_cb callback;
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);
66 #define CONVERT_ERROR(err) _email_error_converter(err, __FUNCTION__, __LINE__);
68 //------------------------------------
70 int email_create_message(email_h *msg)
73 email_s * msg_s = NULL;
74 email_account_t* account = NULL;
78 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is NULL.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
79 return EMAIL_ERROR_INVALID_PARAMETER;
82 // 1. create service for ipc
83 ret=email_service_begin();
84 msg_s= (email_s*)calloc(1,sizeof(email_s));
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);
91 return EMAIL_ERROR_OUT_OF_MEMORY;
94 msg_s->mbox = (email_mailbox_t *)calloc(1,sizeof(email_mailbox_t));
95 if (msg_s->mbox == NULL)
97 LOGE("[%s] OUT_OF_MEMORY(0x%08x) : fail to create msg_s->mbox", __FUNCTION__, EMAIL_ERROR_OUT_OF_MEMORY);
99 return EMAIL_ERROR_OUT_OF_MEMORY;
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;
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);
119 ret = email_get_account(default_account_id, GET_FULL_DATA, &account);
120 if(ret!=EMAIL_API_ERROR_NONE) return CONVERT_ERROR(ret);
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);
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;
131 snprintf(strfrom,len,"%s%s%s%s%s%s","\"",account->incoming_server_user_name,"\"","<",account->user_email_address,">");
134 email_mailbox_t *mbox =msg_s->mbox;
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;
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;
150 *msg = (email_h)msg_s;
151 return EMAIL_ERROR_NONE;
154 int email_destroy_message(email_h msg)
160 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is NULL.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
161 return EMAIL_ERROR_INVALID_PARAMETER;
164 email_s* msg_s = (email_s* )msg;
169 email_free_mail_data(&msg_s->mail, 1);
172 email_free_mailbox(&msg_s->mbox, 1);
178 ret=email_service_end();
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;
186 return EMAIL_ERROR_NONE;
189 int email_set_subject (email_h msg, const char *subject)
194 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is NULL", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
195 return EMAIL_ERROR_INVALID_PARAMETER;
198 email_s* msg_s = (email_s* )msg;
200 msg_s->mail->subject=(char*)calloc(1,sizeof(char)*strlen(subject)+1);
202 if(msg_s->mail->subject ==NULL)
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;
208 len =strlen(subject)+1;
209 snprintf(msg_s->mail->subject ,len,"%s",subject);
211 return EMAIL_ERROR_NONE;
214 int email_set_body (email_h msg, const char *body)
220 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
221 return EMAIL_ERROR_INVALID_PARAMETER;
224 email_s* msg_s = (email_s* )msg;
230 file = fopen("/tmp/capimail.txt", "w");
237 LOGE("[%s] OPERATION_FAILED(0x%08x) : opening file for email body failed.", __FUNCTION__, EMAIL_ERROR_OPERATION_FAILED);
238 return EMAIL_ERROR_OPERATION_FAILED;
241 msg_s->mail->file_path_plain =(char *)calloc(1,sizeof(char)*strlen("/tmp/capimail.txt")+1);
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;
248 len =strlen("/tmp/capimail.txt")+1;
249 snprintf(msg_s->mail->file_path_plain,len,"%s","/tmp/capimail.txt");
251 return EMAIL_ERROR_NONE;
256 int email_add_recipient (email_h msg, email_recipient_type_e type, const char *address)
259 int total_len,in_len,pre_len,len;
261 if(msg == NULL || type < EMAIL_RECIPIENT_TYPE_TO || type > EMAIL_RECIPIENT_TYPE_BCC )
263 return EMAIL_ERROR_INVALID_PARAMETER;
266 email_s* msg_s = (email_s* )msg;
268 if(strlen(address) > MAX_RECIPIENT_ADDRESS_LEN)
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;
274 if(type == EMAIL_RECIPIENT_TYPE_TO)
276 if(msg_s->mail->full_address_to == NULL)
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)
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;
284 len =strlen(address)+2+1+1;
285 snprintf(msg_s->mail->full_address_to,len,"%s%s%s","<",address,">");
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
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,">");
299 else if(type == EMAIL_RECIPIENT_TYPE_CC)//MESSAGING_RECIPIENT_TYPE_CC
301 if(msg_s->mail->full_address_cc == NULL)
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)
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;
309 len =strlen(address)+2+1+1;
310 snprintf(msg_s->mail->full_address_cc,len,"%s%s%s","<",address,">");
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
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,">");
325 else //MESSAGING_RECIPIENT_TYPE_BCC
327 if(msg_s->mail->full_address_bcc == NULL)
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)
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;
335 len =strlen(address)+2+1+1;
336 snprintf(msg_s->mail->full_address_bcc,len,"%s%s%s","<",address,">");
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
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,">");
352 return EMAIL_ERROR_NONE;
355 int email_remove_all_recipients(email_h msg)
359 LOGE("[%s] INVALID_PARAMETER(0x%08x) : fail to create tmp memory.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
360 return EMAIL_ERROR_INVALID_PARAMETER;
363 email_s *msg_s = (email_s *)msg;
365 if(msg_s->mail->full_address_to != NULL)
367 free(msg_s->mail->full_address_to);
368 msg_s->mail->full_address_to = NULL;
371 if(msg_s->mail->full_address_cc != NULL)
373 free(msg_s->mail->full_address_cc);
374 msg_s->mail->full_address_cc = NULL;
377 if(msg_s->mail->full_address_bcc != NULL)
379 free(msg_s->mail->full_address_bcc);
380 msg_s->mail->full_address_bcc = NULL;
382 return EMAIL_ERROR_NONE;
386 int email_add_attach (email_h msg, const char *filepath)
392 if(msg ==NULL || filepath == NULL)
394 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg or filepath is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
395 return EMAIL_ERROR_INVALID_PARAMETER;
398 email_s *msg_s = (email_s *)msg;
400 int attachment_count = msg_s->mail->attachment_count;
401 email_attachment_data_t *new_attach = msg_s->attachment;
404 if(st.st_size > 10*1024*1024)
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;
410 if (!S_ISREG(st.st_mode))
412 LOGE("[%s] INVALID_PARAMETER(0x%08x) : the filepath is not regular file.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
413 return EMAIL_ERROR_INVALID_PARAMETER;
417 pos=strpbrk(filepath, "//");
418 len =strlen(filepath);
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);
429 pos=strpbrk(pos,"//");
430 if(pos != NULL) pos++;
433 new_attach[attachment_count].attachment_name =last;
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;
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;
442 msg_s->mail->attachment_count++;
444 return EMAIL_ERROR_NONE;
448 int email_remove_all_attachments (email_h msg)
451 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
452 return EMAIL_ERROR_INVALID_PARAMETER;
455 email_s* msg_s = (email_s* )msg;
457 msg_s->mail->attachment_count = 0;
459 return EMAIL_ERROR_NONE;
463 int email_send_message (email_h msg, bool save_to_sentbox)
466 email_option_t option;
468 email_attachment_data_t *tmp_attach = NULL;
469 struct tm *struct_time;
472 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
473 return EMAIL_ERROR_INVALID_PARAMETER;
476 email_s *msg_s = (email_s *)msg;
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);
491 struct_time = localtime(&msg_s->mail->date_time);
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);
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);
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);
527 email_mailbox_t * box;
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);
543 ret=email_add_mail(msg_s->mail, msg_s->attachment, msg_s->mail->attachment_count, NULL, 0);
544 ret=CONVERT_ERROR(ret);
547 memset(&option, 0x00, sizeof(email_option_t));
548 option.keep_local_copy = save_to_sentbox ? 1 : 0;
551 ret=email_send_mail(msg_s->mail->mail_id, &option, &handle);
554 ret=CONVERT_ERROR(ret);
555 _email_add_dbus_filter();
560 email_cb_context * _email_search_callback_by_emailid(int mailid)
565 email_cb_context *cbContext;
566 count = g_slist_length( gEmailcbList );
570 node = g_slist_nth( gEmailcbList, ntmp );
575 cbContext= (email_cb_context *)node->data;
576 if(cbContext->handle->mail->mail_id == mailid)
590 int email_set_message_sent_cb (email_h handle, email_message_sent_cb cb, void *user_data)
594 int ret = EMAIL_ERROR_NONE;
596 email_cb_context *cbContext;
597 count = g_slist_length( gEmailcbList );
599 if(handle ==NULL || cb == NULL)return EMAIL_ERROR_INVALID_PARAMETER;
602 email_s* msg_s = NULL;
606 node = g_slist_nth( gEmailcbList, ntmp );
611 cbContext= (email_cb_context *)node->data;
612 if(cbContext->handle == (email_s*)handle)
614 gEmailcbList=g_slist_remove(gEmailcbList,node);
615 _email_free_cb_context(cbContext);
623 if ((ret = _email_copy_handle(&msg_s, (email_s *)handle)) != EMAIL_ERROR_NONE) {
624 LOGE("[%s] _email_copy_handle failed", __FUNCTION__);
628 email_cb_context * cbcontext= (email_cb_context*)calloc(1, sizeof(email_cb_context) );
630 cbcontext->handle = msg_s;
631 cbcontext->callback=cb;
632 cbcontext->user_data =user_data;
634 gEmailcbList = g_slist_append(gEmailcbList,cbcontext);
636 return EMAIL_ERROR_NONE;
639 int email_unset_message_sent_cb (email_h msg)
643 count = g_slist_length( gEmailcbList );
645 email_cb_context *cbContext;
649 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is null.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
650 return EMAIL_ERROR_INVALID_PARAMETER;
652 email_s* msg_s = (email_s* )msg;
653 for( i=0;i<count;i++)
655 node = g_slist_nth( gEmailcbList, i );
660 cbContext= (email_cb_context *)node->data;
661 if(cbContext->handle == msg_s)
663 gEmailcbList= g_slist_remove(gEmailcbList,node);
664 _email_free_cb_context(cbContext);
672 return EMAIL_ERROR_NONE;
675 int _email_error_converter(int err, const char *func, int line)
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;
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;
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;
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;
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;
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;
705 case EMAIL_API_ERROR_NONE :
706 return EMAIL_ERROR_NONE;
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;
717 static void _monitorSendStatusCb(void* data, DBusMessage *message)
720 email_cb_context *cbContext = NULL;
721 if(dbus_message_is_signal(message, "User.Email.NetworkStatus", "email"))
723 dbus_error_init(&error);
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,
740 cbContext = _email_search_callback_by_emailid(mailid);
741 if(cbContext == NULL)
743 LOGD_IF(DBG_MODE, "no callback matched!\n");
747 case NOTI_SEND_START:
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:
769 cbContext->callback((email_h)cbContext->handle,(email_sending_e)EMAIL_SENDING_FAILED ,cbContext->user_data);
772 case NOTI_SEND_FINISH:
773 cbContext->callback((email_h)cbContext->handle,(email_sending_e)EMAIL_SENDING_SUCCEEDED ,cbContext->user_data);
782 void _email_add_dbus_filter(void)
785 E_DBus_Connection* connection=NULL;
787 E_DBus_Signal_Handler *handler = NULL;
790 dbus_error_init(&error);
792 connection = e_dbus_bus_get(DBUS_BUS_SYSTEM);
794 if (NULL == connection) {
798 if(e_dbus_request_name(connection, "User.Email.NetworkStatus",
803 LOGD_IF(DBG_MODE, "Failed in e_dbus_request_name()\n");
806 handler = e_dbus_signal_handler_add(connection,
808 "/User/Email/NetworkStatus",
809 "User.Email.NetworkStatus",
811 _monitorSendStatusCb,
816 LOGD_IF(DBG_MODE, "Failed in e_dbus_signal_handler_add()");
820 int _email_copy_mail_data(email_mail_data_t **dst_mail_data, email_mail_data_t *src_mail_data)
822 email_mail_data_t *temp_mail_data = NULL;
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;
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;
877 *dst_mail_data = temp_mail_data;
879 return EMAIL_ERROR_NONE;
882 int _email_copy_mailbox(email_mailbox_t **dst_mailbox, email_mailbox_t *src_mailbox)
884 email_mailbox_t *temp_mailbox = NULL;
886 temp_mailbox = (email_mailbox_t *)calloc(1,sizeof(email_mailbox_t));
887 if (temp_mailbox == NULL)
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;
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;
905 *dst_mailbox = temp_mailbox;
907 return EMAIL_ERROR_NONE;
911 int _email_copy_handle(email_s **dst_handle, email_s *src_handle)
913 int ret = EMAIL_ERROR_NONE;
914 email_s *msg_s = NULL;
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__);
922 if ((ret = _email_copy_mailbox(&msg_s->mbox, src_handle->mbox)) != EMAIL_ERROR_NONE) {
923 LOGE("[%s] _email_copy_mailbox failed", __FUNCTION__);
931 void _email_free_cb_context(email_cb_context *cbcontext)
933 if(cbcontext == NULL) {
934 LOGE("[%s] INVALID_PARAMETER(0x%08x) : msg is NULL.", __FUNCTION__, EMAIL_ERROR_INVALID_PARAMETER);
938 email_s* msg_s = cbcontext->handle;
943 email_free_mail_data(&msg_s->mail, 1);
946 email_free_mailbox(&msg_s->mbox, 1);