/* * email-service * * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. * * Contact: Kyuho Jo , Sunghyun Kwon * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include "em-core-types.h" #ifdef __FEATURE_AUTO_POLLING__ #include #include #include #include #include /* Needed for the definition of va_list */ #include #include "emflib.h" #include "emf-auto-poll.h" #include "emf-types.h" #include "emf-dbglog.h" #include "em-storage.h" typedef struct _emf_account_alarm_binder { int account_id; alarm_id_t alarm_id; int timer_interval; } emf_account_alarm_binder; /* global list */ typedef struct _emf_account_alarm_binder_list_t { emf_account_alarm_binder account_alarm_binder; struct _emf_account_alarm_binder_list_t *next; }emf_account_alarm_binder_list_t; /* sowmya.kr@samsung.com, 23022010, Implementation of auto-polling feature */ emf_account_alarm_binder_list_t *g_account_alarm_binder_list = NULL; static int emf_get_polling_alarm_and_timerinterval(int account_id, alarm_id_t *alarm_id, int *timer_interval); static int emf_get_polling_account_and_timeinterval(alarm_id_t alarm_id, int *account_id, int *timer_interval); static int emf_create_alarm(int alarm_interval, alarm_id_t *p_alarm_id); static int add_to_account_alarm_binder_list(emf_account_alarm_binder_list_t *p_account_alarm_binder); static int remove_from_account_alarm_binder_list(int account_id); static int update_account_alarm_binder_list(int account_id, alarm_id_t alarm_id); EXPORT_API int emf_add_polling_alarm(int account_id, int alarm_interval) { EM_DEBUG_FUNC_BEGIN(); if(!account_id || (alarm_interval <= 0)) { EM_DEBUG_EXCEPTION("Invalid param"); return false; } EM_DEBUG_EXCEPTION(" emf_add_polling_alarm : account_id [%d]",account_id); if(is_auto_polling_started(account_id)) { EM_DEBUG_EXCEPTION("auto polling already started for account : return"); return true; } alarm_id_t alarmID = 0; emf_account_alarm_binder_list_t *p_account_alarm_binder = NULL; if(!emf_create_alarm(alarm_interval, &alarmID)) { EM_DEBUG_EXCEPTION("emf_create_alarm failed"); return false; } p_account_alarm_binder = (emf_account_alarm_binder_list_t*)em_core_malloc(sizeof(emf_account_alarm_binder_list_t)); if(!p_account_alarm_binder) { EM_DEBUG_EXCEPTION("malloc Failed "); return false; } p_account_alarm_binder->account_alarm_binder.account_id = account_id; p_account_alarm_binder->account_alarm_binder.alarm_id = alarmID; p_account_alarm_binder->account_alarm_binder.timer_interval = alarm_interval; add_to_account_alarm_binder_list(p_account_alarm_binder); return true; } EXPORT_API int emf_remove_polling_alarm(int account_id) { EM_DEBUG_FUNC_BEGIN(); if(!account_id) { EM_DEBUG_EXCEPTION("Invalid param "); return false; } alarm_id_t alarm_id = 0; int a_nErrorCode = 0, retval =0; if(!emf_get_polling_alarm_and_timerinterval(account_id,&alarm_id,NULL)) { EM_DEBUG_EXCEPTION("emf_get_polling_alarm_and_timerinterval failed"); return false; } /* delete alarm */ if (alarm_id > 0) { a_nErrorCode = alarmmgr_remove_alarm(alarm_id); EM_DEBUG_LOG("ErrorCode :%d, Return Value:%d ",a_nErrorCode,retval); if(!retval) return false; } /* delete from list */ if(!remove_from_account_alarm_binder_list(account_id)) { EM_DEBUG_EXCEPTION("remove_from_account_alarm_binder_list failed"); return false; } return true; } EXPORT_API int is_auto_polling_started(int account_id) { EM_DEBUG_FUNC_BEGIN(); if(g_account_alarm_binder_list == NULL) { EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL "); return false; } emf_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list; int match_found = false; while(p_temp != NULL) { if(p_temp->account_alarm_binder.account_id == account_id) { EM_DEBUG_EXCEPTION("account match found : polling already started"); match_found = true; break; } p_temp = p_temp->next; } if(!match_found) { EM_DEBUG_EXCEPTION("account match not found : polling not started"); return false; } return true; } EXPORT_API int emf_alarm_polling_cb(alarm_id_t alarm_id, void* user_param) { EM_DEBUG_FUNC_BEGIN(); /* tzset(); */ time_t ct = time(&ct); struct tm* lt = localtime(&ct); if (lt) { EM_DEBUG_LOG( "Current Time : [%d-%d-%d %d:%d:%d] ", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec); } emf_mailbox_t mailbox = {0}; int account_id = 0, err = EMF_ERROR_NONE, timer_interval =0, alarmID =0,ret = false; char* mailbox_name = NULL; if(!emf_get_polling_account_and_timeinterval(alarm_id,&account_id,&timer_interval)) { EM_DEBUG_EXCEPTION("emf_get_polling_account failed"); return false; } EM_DEBUG_EXCEPTION(" emf_alarm_polling_cb : account_id [%d]",account_id); /* create alarm, for polling */ if(!emf_create_alarm(timer_interval,&alarmID)) { EM_DEBUG_EXCEPTION("emf_create_alarm failed"); return false; } /*update alarm ID in list */ /* delete from list */ if(!update_account_alarm_binder_list(account_id,alarmID)) { EM_DEBUG_EXCEPTION("update_account_alarm_binder_list failed"); return false; } memset(&mailbox, 0x00, sizeof(emf_mailbox_t)); mailbox.account_id = account_id; if (!em_storage_get_mailboxname_by_mailbox_type(mailbox.account_id,EMF_MAILBOX_TYPE_INBOX,&mailbox_name, false, &err)) { EM_DEBUG_EXCEPTION("em_storage_get_mailboxname_by_mailbox_type failed [%d]", err); err = em_storage_get_emf_error_from_em_storage_error(err); goto FINISH_OFF; } mailbox.name = mailbox_name; if (!emf_mailbox_sync_header(&mailbox, NULL, &err)) { EM_DEBUG_EXCEPTION("emf_mailbox_sync_header falied [%d]", err); goto FINISH_OFF; } ret = true; FINISH_OFF : EM_SAFE_FREE(mailbox_name); return ret; } static int emf_get_polling_alarm_and_timerinterval(int account_id, alarm_id_t *alarm_id, int *timer_interval) { EM_DEBUG_FUNC_BEGIN(); if(!account_id || !alarm_id) return false; if(g_account_alarm_binder_list == NULL) { EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL "); return false; } emf_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list; int match_found = false; while(p_temp != NULL) { if(p_temp->account_alarm_binder.account_id == account_id) { EM_DEBUG_EXCEPTION("account match found "); *alarm_id = p_temp->account_alarm_binder.alarm_id; if(timer_interval) *timer_interval = p_temp->account_alarm_binder.timer_interval; match_found = true; break; } p_temp = p_temp->next; } if(!match_found) { EM_DEBUG_EXCEPTION("account match not found "); return false; } return true; } static int emf_get_polling_account_and_timeinterval(alarm_id_t alarm_id, int *account_id, int *timer_interval) { EM_DEBUG_FUNC_BEGIN(); if(!alarm_id || !account_id) return false; if(g_account_alarm_binder_list == NULL) { EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL "); return false; } emf_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list; int match_found = false; while(p_temp != NULL) { if(p_temp->account_alarm_binder.alarm_id == alarm_id) { EM_DEBUG_EXCEPTION("aalrm match found "); *account_id = p_temp->account_alarm_binder.account_id; if(timer_interval) *timer_interval = p_temp->account_alarm_binder.timer_interval; match_found = true; break; } p_temp = p_temp->next; } if(!match_found) { EM_DEBUG_EXCEPTION("aalrm match not found "); return false; } return true; } #define AUTO_POLL_DESTINATION "email-service" static int emf_create_alarm(int alarm_interval, alarm_id_t *p_alarm_id) { EM_DEBUG_FUNC_BEGIN(); /* tzset(); */ time_t ct = time(&ct); struct tm* lt = localtime(&ct); if (lt) { EM_DEBUG_LOG( "Current Time : [%d-%d-%d %d:%d:%d] ", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec); } /* alarm_info_t alarm_info = {0}; */ int a_nErrorCode = 0; int retval =0; if((alarm_interval <= 0) || !p_alarm_id) { EM_DEBUG_EXCEPTION("Invalid param "); return false; } /* time_t current_time = {0}; */ /* struct tm current_tm = {0}; */ int error_code = 0; /* Fill alarm info */ /* int timeFormat = 0; */ /*0 means 12hrs , 1 means 24hrs*/ /* TO DO, need to findout header for DBG_MID_MSGPORTING_NORMAL and then we have to use this */ /* error_code = vconf_get_int(DBG_MID_MSGPORTING_NORMAL, &timeFormat); */ a_nErrorCode = alarmmgr_init(AUTO_POLL_DESTINATION); EM_DEBUG_LOG("ErrorCode :%d, Return Value:%d ",a_nErrorCode,retval); if(!retval) return false; a_nErrorCode = alarmmgr_set_cb(emf_alarm_polling_cb, NULL); EM_DEBUG_LOG("ErrorCode :%d, Return Value:%d ",a_nErrorCode,retval); if(!retval) return false; error_code = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, alarm_interval * 60 /*(sec)*/, ALARM_REPEAT_MODE_ONCE, AUTO_POLL_DESTINATION, p_alarm_id); EM_DEBUG_LOG("ErrorCode :%d,Return Value :%d ",error_code,retval); if(!retval) return false; return true; } EXPORT_API int emf_free_account_alarm_binder_list() { EM_DEBUG_FUNC_BEGIN(); emf_account_alarm_binder_list_t *p = g_account_alarm_binder_list, *p_next = NULL; int a_nErrorCode = 0; /* delete alarm as well */ while (p) { /* delete alarm */ if (p->account_alarm_binder.alarm_id > 0) { a_nErrorCode = alarmmgr_remove_alarm(p->account_alarm_binder.alarm_id); } p_next = p->next; EM_SAFE_FREE(p); p = p_next; } g_account_alarm_binder_list = NULL; return true; } static int add_to_account_alarm_binder_list(emf_account_alarm_binder_list_t *p_account_alarm_binder) { emf_account_alarm_binder_list_t *p_temp = NULL; if(!p_account_alarm_binder) { EM_DEBUG_EXCEPTION("Invalid param "); return false; } if(g_account_alarm_binder_list == NULL) { g_account_alarm_binder_list = p_account_alarm_binder; p_account_alarm_binder = NULL; } else { p_temp = g_account_alarm_binder_list; while(p_temp->next != NULL) p_temp = p_temp->next; p_temp->next = p_account_alarm_binder; p_account_alarm_binder = NULL; } return true; } static int remove_from_account_alarm_binder_list(int account_id) { emf_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list, *p_prev = NULL; int match_found = false; if(!account_id) { EM_DEBUG_EXCEPTION("Invalid param "); return false; } /* first node mattch */ if(p_temp->account_alarm_binder.account_id == account_id) { /* match found */ match_found = true; g_account_alarm_binder_list = p_temp->next; EM_SAFE_FREE(p_temp); } else { while(p_temp != NULL) { if(p_temp->account_alarm_binder.account_id == account_id) { EM_DEBUG_EXCEPTION("account match found "); p_prev->next = p_temp->next; EM_SAFE_FREE(p_temp); match_found = true; break; } p_prev = p_temp; p_temp = p_temp->next; } } if(!match_found) return false; return true; } static int update_account_alarm_binder_list(int account_id, alarm_id_t alarm_id) { emf_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list; int match_found = false; if( !account_id || !alarm_id) { EM_DEBUG_EXCEPTION("Invalid param "); return false; } while(p_temp != NULL) { if(p_temp->account_alarm_binder.account_id == account_id) { EM_DEBUG_EXCEPTION("account match found "); /* update alarm id */ p_temp->account_alarm_binder.alarm_id = alarm_id; match_found = true; break; } p_temp = p_temp->next; } if(!match_found) { EM_DEBUG_EXCEPTION("account match not found "); return false; } return true; } #endif