4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "email-internal-types.h"
25 #ifdef __FEATURE_AUTO_POLLING__
31 #include <stdarg.h> /* Needed for the definition of va_list */
34 #include "email-types.h"
35 #include "email-daemon.h"
36 #include "email-daemon-auto-poll.h"
37 #include "email-core-utils.h"
38 #include "email-debug-log.h"
39 #include "email-storage.h"
40 #include "email-utilities.h"
42 typedef struct _emf_account_alarm_binder
47 } email_account_alarm_binder;
50 typedef struct _emf_account_alarm_binder_list_t
52 email_account_alarm_binder account_alarm_binder;
53 struct _emf_account_alarm_binder_list_t *next;
54 }email_account_alarm_binder_list_t;
56 /* sowmya.kr@samsung.com, 23022010, Implementation of auto-polling feature */
58 email_account_alarm_binder_list_t *g_account_alarm_binder_list = NULL;
60 static int _emdaemon_get_polling_alarm_and_timerinterval(int account_id, alarm_id_t *alarm_id, int *timer_interval);
61 static int _emdaemon_get_polling_account_and_timeinterval(alarm_id_t alarm_id, int *account_id, int *timer_interval);
62 static int _emdaemon_create_alarm(int alarm_interval, alarm_id_t *p_alarm_id);
63 static int _emdaemon_add_to_account_alarm_binder_list(email_account_alarm_binder_list_t *p_account_alarm_binder);
64 static int _emdaemon_remove_from_account_alarm_binder_list(int account_id);
65 static int _emdaemon_update_account_alarm_binder_list(int account_id, alarm_id_t alarm_id);
67 INTERNAL_FUNC int emdaemon_add_polling_alarm(int account_id, int alarm_interval)
69 EM_DEBUG_FUNC_BEGIN();
71 if(!account_id || (alarm_interval <= 0)) {
72 EM_DEBUG_EXCEPTION("Invalid param");
75 EM_DEBUG_EXCEPTION(" emdaemon_add_polling_alarm : account_id [%d]",account_id);
76 if(emdaemon_check_auto_polling_started(account_id)) {
77 EM_DEBUG_EXCEPTION("auto polling already started for account : return");
81 alarm_id_t alarmID = 0;
82 email_account_alarm_binder_list_t *p_account_alarm_binder = NULL;
84 if(!_emdaemon_create_alarm(alarm_interval, &alarmID)) {
85 EM_DEBUG_EXCEPTION("_emdaemon_create_alarm failed");
89 p_account_alarm_binder = (email_account_alarm_binder_list_t*)em_malloc(sizeof(email_account_alarm_binder_list_t));
90 if(!p_account_alarm_binder) {
91 EM_DEBUG_EXCEPTION("malloc Failed ");
95 p_account_alarm_binder->account_alarm_binder.account_id = account_id;
96 p_account_alarm_binder->account_alarm_binder.alarm_id = alarmID;
97 p_account_alarm_binder->account_alarm_binder.timer_interval = alarm_interval;
99 _emdaemon_add_to_account_alarm_binder_list(p_account_alarm_binder);
105 INTERNAL_FUNC int emdaemon_remove_polling_alarm(int account_id)
107 EM_DEBUG_FUNC_BEGIN();
110 EM_DEBUG_EXCEPTION("Invalid param ");
114 alarm_id_t alarm_id = 0;
115 int a_nErrorCode = 0, retval =0;
117 if(!_emdaemon_get_polling_alarm_and_timerinterval(account_id,&alarm_id,NULL)) {
118 EM_DEBUG_EXCEPTION("_emdaemon_get_polling_alarm_and_timerinterval failed");
124 a_nErrorCode = alarmmgr_remove_alarm(alarm_id);
126 EM_DEBUG_LOG("ErrorCode :%d, Return Value:%d ",a_nErrorCode,retval);
132 /* delete from list */
133 if(!_emdaemon_remove_from_account_alarm_binder_list(account_id)) {
134 EM_DEBUG_EXCEPTION("_emdaemon_remove_from_account_alarm_binder_list failed");
141 INTERNAL_FUNC int emdaemon_check_auto_polling_started(int account_id)
143 EM_DEBUG_FUNC_BEGIN();
145 if(g_account_alarm_binder_list == NULL) {
146 EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL ");
150 email_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list;
151 int match_found = false;
153 while(p_temp != NULL) {
154 if(p_temp->account_alarm_binder.account_id == account_id) {
155 EM_DEBUG_EXCEPTION("account match found : polling already started");
159 p_temp = p_temp->next;
163 EM_DEBUG_EXCEPTION("account match not found : polling not started");
169 INTERNAL_FUNC int emdaemon_alarm_polling_cb(alarm_id_t alarm_id, void* user_param)
171 EM_DEBUG_FUNC_BEGIN();
173 email_mailbox_t mailbox = {0};
174 int account_id = 0, err = EMAIL_ERROR_NONE, timer_interval =0, alarmID =0,ret = false;
175 char* mailbox_name = NULL;
177 time_t ct = time(&ct);
178 struct tm* lt = localtime(&ct);
181 EM_DEBUG_LOG( "Current Time : [%d-%d-%d %d:%d:%d] ",
182 lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
183 lt->tm_hour, lt->tm_min, lt->tm_sec);
186 if(!_emdaemon_get_polling_account_and_timeinterval(alarm_id,&account_id,&timer_interval)) {
187 EM_DEBUG_EXCEPTION("email_get_polling_account failed");
191 EM_DEBUG_EXCEPTION(" emdaemon_alarm_polling_cb : account_id [%d]",account_id);
192 /* create alarm, for polling */
193 if(!_emdaemon_create_alarm(timer_interval,&alarmID)) {
194 EM_DEBUG_EXCEPTION("_emdaemon_create_alarm failed");
198 /*update alarm ID in list */
199 /* delete from list */
200 if(!_emdaemon_update_account_alarm_binder_list(account_id,alarmID)) {
201 EM_DEBUG_EXCEPTION("_emdaemon_update_account_alarm_binder_list failed");
205 memset(&mailbox, 0x00, sizeof(email_mailbox_t));
206 mailbox.account_id = account_id;
208 if (!emstorage_get_mailbox_name_by_mailbox_type(mailbox.account_id,EMAIL_MAILBOX_TYPE_INBOX,&mailbox_name, false, &err)) {
209 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
212 mailbox.mailbox_name = mailbox_name;
214 if (!emdaemon_sync_header(account_id, mailbox.mailbox_id, NULL, &err)) {
215 EM_DEBUG_EXCEPTION("emdaemon_sync_header falied [%d]", err);
222 EM_SAFE_FREE(mailbox_name);
227 static int _emdaemon_get_polling_alarm_and_timerinterval(int account_id, alarm_id_t *alarm_id, int *timer_interval)
229 EM_DEBUG_FUNC_BEGIN();
231 if(!account_id || !alarm_id)
234 if(g_account_alarm_binder_list == NULL) {
235 EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL ");
239 email_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list;
240 int match_found = false;
242 while(p_temp != NULL) {
243 if(p_temp->account_alarm_binder.account_id == account_id) {
244 EM_DEBUG_EXCEPTION("account match found ");
245 *alarm_id = p_temp->account_alarm_binder.alarm_id;
248 *timer_interval = p_temp->account_alarm_binder.timer_interval;
254 p_temp = p_temp->next;
258 EM_DEBUG_EXCEPTION("account match not found ");
266 static int _emdaemon_get_polling_account_and_timeinterval(alarm_id_t alarm_id, int *account_id, int *timer_interval)
268 EM_DEBUG_FUNC_BEGIN();
270 if(!alarm_id || !account_id)
273 if(g_account_alarm_binder_list == NULL) {
274 EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL ");
278 email_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list;
279 int match_found = false;
281 while(p_temp != NULL) {
282 if(p_temp->account_alarm_binder.alarm_id == alarm_id) {
283 EM_DEBUG_EXCEPTION("aalrm match found ");
284 *account_id = p_temp->account_alarm_binder.account_id;
287 *timer_interval = p_temp->account_alarm_binder.timer_interval;
293 p_temp = p_temp->next;
297 EM_DEBUG_EXCEPTION("aalrm match not found ");
304 #define AUTO_POLL_DESTINATION "com.samsung.email-service"
305 static int _emdaemon_create_alarm(int alarm_interval, alarm_id_t *p_alarm_id)
307 EM_DEBUG_FUNC_BEGIN();
310 time_t ct = time(&ct);
311 struct tm* lt = localtime(&ct);
314 EM_DEBUG_LOG( "Current Time : [%d-%d-%d %d:%d:%d] ",
315 lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
316 lt->tm_hour, lt->tm_min, lt->tm_sec);
319 /* alarm_info_t alarm_info = {0}; */
320 int a_nErrorCode = 0;
322 if((alarm_interval <= 0) || !p_alarm_id) {
323 EM_DEBUG_EXCEPTION("Invalid param ");
327 /* time_t current_time = {0}; */
328 /* struct tm current_tm = {0}; */
329 /* Fill alarm info */
330 /* int timeFormat = 0; */ /*0 means 12hrs , 1 means 24hrs*/
332 /* TO DO, need to findout header for DBG_MID_MSGPORTING_NORMAL and then we have to use this */
333 /* error_code = vconf_get_int(DBG_MID_MSGPORTING_NORMAL, &timeFormat); */
335 a_nErrorCode = alarmmgr_init(AUTO_POLL_DESTINATION);
336 if (a_nErrorCode != ALARMMGR_RESULT_SUCCESS) {
337 EM_DEBUG_EXCEPTION("alarmmgr_init failed : ErrorCode[%d]",a_nErrorCode);
341 a_nErrorCode = alarmmgr_set_cb(emdaemon_alarm_polling_cb, NULL);
342 if (a_nErrorCode != ALARMMGR_RESULT_SUCCESS) {
343 EM_DEBUG_EXCEPTION("alarmmgr_set_cb : ErrorCode[%d]",a_nErrorCode);
347 a_nErrorCode = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, alarm_interval * 60 /*(sec)*/, ALARM_REPEAT_MODE_ONCE, AUTO_POLL_DESTINATION, p_alarm_id); /*prevent 23154*/
348 if (a_nErrorCode != ALARMMGR_RESULT_SUCCESS) {
349 EM_DEBUG_EXCEPTION("alarmmgr_add_alarm : ErrorCode[%d]",a_nErrorCode);
356 INTERNAL_FUNC int emdaemon_free_account_alarm_binder_list()
358 EM_DEBUG_FUNC_BEGIN();
360 email_account_alarm_binder_list_t *p = g_account_alarm_binder_list, *p_next = NULL;
361 int a_nErrorCode = 0;
363 /* delete alarm as well */
366 if (p->account_alarm_binder.alarm_id > 0) {
367 a_nErrorCode = alarmmgr_remove_alarm(p->account_alarm_binder.alarm_id);
375 g_account_alarm_binder_list = NULL;
380 static int _emdaemon_add_to_account_alarm_binder_list(email_account_alarm_binder_list_t *p_account_alarm_binder)
382 email_account_alarm_binder_list_t *p_temp = NULL;
384 if(!p_account_alarm_binder) {
385 EM_DEBUG_EXCEPTION("Invalid param ");
389 if(g_account_alarm_binder_list == NULL) {
390 g_account_alarm_binder_list = p_account_alarm_binder;
391 p_account_alarm_binder = NULL;
394 p_temp = g_account_alarm_binder_list;
395 while(p_temp->next != NULL)
396 p_temp = p_temp->next;
398 p_temp->next = p_account_alarm_binder;
399 p_account_alarm_binder = NULL;
405 static int _emdaemon_remove_from_account_alarm_binder_list(int account_id)
407 email_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list, *p_prev = NULL;
408 int match_found = false;
411 EM_DEBUG_EXCEPTION("Invalid param ");
415 /* first node mattch */
416 if(p_temp->account_alarm_binder.account_id == account_id) {
419 g_account_alarm_binder_list = p_temp->next;
420 EM_SAFE_FREE(p_temp);
423 while(p_temp != NULL) {
424 if(p_temp->account_alarm_binder.account_id == account_id) {
425 EM_DEBUG_EXCEPTION("account match found ");
427 p_prev->next = p_temp->next;
428 EM_SAFE_FREE(p_temp);
435 p_temp = p_temp->next;
445 static int _emdaemon_update_account_alarm_binder_list(int account_id, alarm_id_t alarm_id)
447 email_account_alarm_binder_list_t *p_temp = g_account_alarm_binder_list;
448 int match_found = false;
450 if( !account_id || !alarm_id) {
451 EM_DEBUG_EXCEPTION("Invalid param ");
455 while(p_temp != NULL) {
456 if(p_temp->account_alarm_binder.account_id == account_id) {
457 EM_DEBUG_EXCEPTION("account match found ");
458 /* update alarm id */
459 p_temp->account_alarm_binder.alarm_id = alarm_id;
463 p_temp = p_temp->next;
467 EM_DEBUG_EXCEPTION("account match not found ");