prevent fix
[framework/messaging/email-service.git] / email-daemon / email-daemon-auto-poll.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 #include "email-internal-types.h"
24
25 #ifdef __FEATURE_AUTO_POLLING__
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <time.h>
31 #include <stdarg.h>     /*  Needed for the definition of va_list */
32 #include <glib.h>
33
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"
41
42 typedef struct _emf_account_alarm_binder
43 {
44            int account_id;
45            alarm_id_t  alarm_id;
46            int timer_interval;
47 } email_account_alarm_binder;
48
49 /*  global  list */
50 typedef struct _emf_account_alarm_binder_list_t
51 {
52     email_account_alarm_binder account_alarm_binder;
53     struct _emf_account_alarm_binder_list_t  *next;
54 }email_account_alarm_binder_list_t;
55
56 /* sowmya.kr@samsung.com, 23022010, Implementation of auto-polling feature */
57
58 email_account_alarm_binder_list_t *g_account_alarm_binder_list  = NULL;
59
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);
66
67 INTERNAL_FUNC int emdaemon_add_polling_alarm(int account_id, int alarm_interval)
68 {
69         EM_DEBUG_FUNC_BEGIN();
70
71         if(!account_id || (alarm_interval <= 0)) {
72                 EM_DEBUG_EXCEPTION("Invalid param");
73                 return false;
74         }
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");
78                 return true;
79         }
80
81         alarm_id_t alarmID = 0;
82         email_account_alarm_binder_list_t *p_account_alarm_binder = NULL;
83
84         if(!_emdaemon_create_alarm(alarm_interval, &alarmID)) {
85                 EM_DEBUG_EXCEPTION("_emdaemon_create_alarm failed");
86                 return false;
87         }
88                         
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 ");
92                 return false;
93         }
94
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;
98         
99         _emdaemon_add_to_account_alarm_binder_list(p_account_alarm_binder);
100
101         return  true;   
102 }
103
104
105 INTERNAL_FUNC int emdaemon_remove_polling_alarm(int account_id)
106 {
107         EM_DEBUG_FUNC_BEGIN();
108
109         if(!account_id) {
110                 EM_DEBUG_EXCEPTION("Invalid param ");
111                 return false;
112         }
113
114         alarm_id_t  alarm_id = 0;
115         int a_nErrorCode = 0, retval =0;
116
117         if(!_emdaemon_get_polling_alarm_and_timerinterval(account_id,&alarm_id,NULL)) {
118                 EM_DEBUG_EXCEPTION("_emdaemon_get_polling_alarm_and_timerinterval failed");
119                 return false;
120         }
121
122         /* delete alarm */
123         if (alarm_id > 0) {
124                 a_nErrorCode = alarmmgr_remove_alarm(alarm_id);
125
126                 EM_DEBUG_LOG("ErrorCode :%d, Return Value:%d ",a_nErrorCode,retval);
127
128                 if(!retval)
129                         return false;           
130         }
131
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");
135                 return false;
136         }
137
138         return true;
139 }
140
141 INTERNAL_FUNC int emdaemon_check_auto_polling_started(int account_id)
142 {
143         EM_DEBUG_FUNC_BEGIN();
144
145         if(g_account_alarm_binder_list == NULL) {
146                 EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL ");
147                 return false;
148         }
149
150         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list;
151         int match_found = false;
152
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");                    
156                         match_found = true;
157                         break;
158                 }
159                 p_temp = p_temp->next;
160         }
161
162         if(!match_found) {
163                 EM_DEBUG_EXCEPTION("account match not found : polling not started");
164                 return false;
165         }
166         return true;
167 }
168
169 INTERNAL_FUNC int emdaemon_alarm_polling_cb(alarm_id_t  alarm_id, void* user_param)
170 {
171         EM_DEBUG_FUNC_BEGIN();
172
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;
176
177         time_t ct = time(&ct);
178         struct tm* lt = localtime(&ct);
179         
180         if (lt) {
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);                           
184         }
185
186         if(!_emdaemon_get_polling_account_and_timeinterval(alarm_id,&account_id,&timer_interval)) {
187                 EM_DEBUG_EXCEPTION("email_get_polling_account failed");
188                 return false;
189         }
190
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");
195                 return false;
196         }
197
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");
202                 return false;
203         }
204
205         memset(&mailbox, 0x00, sizeof(email_mailbox_t));
206         mailbox.account_id = account_id;
207
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);
210                 goto FINISH_OFF;
211         }
212         mailbox.mailbox_name = mailbox_name;
213
214         if (!emdaemon_sync_header(account_id, mailbox.mailbox_id, NULL, &err))  {
215                 EM_DEBUG_EXCEPTION("emdaemon_sync_header falied [%d]", err);            
216                 goto FINISH_OFF;
217         }
218
219         ret = true;
220 FINISH_OFF :
221
222         EM_SAFE_FREE(mailbox_name);
223
224         return ret;
225 }
226
227 static int _emdaemon_get_polling_alarm_and_timerinterval(int account_id, alarm_id_t *alarm_id, int *timer_interval)
228 {
229         EM_DEBUG_FUNC_BEGIN();
230
231         if(!account_id || !alarm_id)
232                 return false;   
233
234         if(g_account_alarm_binder_list == NULL) {
235                 EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL ");
236                 return false;
237         }
238
239         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list;
240         int match_found = false;
241
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;
246                         
247                         if(timer_interval)
248                                 *timer_interval = p_temp->account_alarm_binder.timer_interval;
249                         
250                         match_found = true;
251                         break;
252                 }
253
254                 p_temp = p_temp->next;
255         }
256
257         if(!match_found) {
258                 EM_DEBUG_EXCEPTION("account match not found ");
259                 return false;
260         }
261
262
263         return true;
264 }
265
266 static int _emdaemon_get_polling_account_and_timeinterval(alarm_id_t  alarm_id, int *account_id, int *timer_interval)
267 {
268         EM_DEBUG_FUNC_BEGIN();
269
270         if(!alarm_id || !account_id)
271                 return false;   
272
273         if(g_account_alarm_binder_list == NULL) {
274                 EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL ");
275                 return false;
276         }
277
278         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list;
279         int match_found = false;
280
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;
285                         
286                         if(timer_interval)
287                                 *timer_interval = p_temp->account_alarm_binder.timer_interval;
288                         
289                         match_found = true;
290                         break;
291                 }
292
293                 p_temp = p_temp->next;
294         }
295
296         if(!match_found) {
297                 EM_DEBUG_EXCEPTION("aalrm  match not found ");
298                 return false;
299         }
300         
301         return true;
302 }
303
304 #define AUTO_POLL_DESTINATION   "org.tizen.email-service"
305 static int _emdaemon_create_alarm(int alarm_interval, alarm_id_t *p_alarm_id)
306 {
307         EM_DEBUG_FUNC_BEGIN();
308
309         /*      tzset(); */
310         time_t ct = time(&ct);
311         struct tm* lt = localtime(&ct);
312         
313         if (lt) {
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);                           
317         }
318
319         /*      alarm_info_t alarm_info = {0};   */
320         int a_nErrorCode = 0;
321                 
322         if((alarm_interval <= 0) || !p_alarm_id) {
323                 EM_DEBUG_EXCEPTION("Invalid param ");
324                 return false;
325         }
326         
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*/
331
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); */
334
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);
338                 return false;
339         }
340         
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);
344                 return false;
345         }
346
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);
350                 return false;
351         }
352
353         return true;
354 }
355
356 INTERNAL_FUNC int emdaemon_free_account_alarm_binder_list()
357 {
358         EM_DEBUG_FUNC_BEGIN();
359
360         email_account_alarm_binder_list_t *p = g_account_alarm_binder_list, *p_next = NULL;     
361         int a_nErrorCode = 0;
362         
363         /* delete alarm as well */
364         while (p) {
365                 /* delete alarm */
366                 if (p->account_alarm_binder.alarm_id  > 0) {
367                         a_nErrorCode = alarmmgr_remove_alarm(p->account_alarm_binder.alarm_id);
368                 }
369
370                 p_next = p->next;
371                 EM_SAFE_FREE(p);
372                 p = p_next;
373         }
374
375         g_account_alarm_binder_list = NULL;
376         
377         return true;
378 }
379
380 static int  _emdaemon_add_to_account_alarm_binder_list(email_account_alarm_binder_list_t *p_account_alarm_binder)
381 {
382         email_account_alarm_binder_list_t  *p_temp = NULL;
383
384         if(!p_account_alarm_binder) {
385                 EM_DEBUG_EXCEPTION("Invalid param ");
386                 return false;
387         }
388
389         if(g_account_alarm_binder_list == NULL) {
390                 g_account_alarm_binder_list = p_account_alarm_binder;
391                 p_account_alarm_binder = NULL;
392         }
393         else {
394                 p_temp = g_account_alarm_binder_list;
395                 while(p_temp->next != NULL)
396                         p_temp = p_temp->next;
397
398                 p_temp->next = p_account_alarm_binder;
399                 p_account_alarm_binder = NULL;
400         }
401
402         return true;
403 }
404
405 static int  _emdaemon_remove_from_account_alarm_binder_list(int account_id)
406 {
407         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list,  *p_prev = NULL;
408         int match_found = false;
409         
410         if(!account_id) {
411                 EM_DEBUG_EXCEPTION("Invalid param ");
412                 return false;
413         }
414
415         /* first node mattch */
416         if(p_temp->account_alarm_binder.account_id == account_id) {
417                 /* match found */
418                 match_found = true;
419                 g_account_alarm_binder_list = p_temp->next;
420                 EM_SAFE_FREE(p_temp);
421         }
422         else {
423                 while(p_temp != NULL) {                 
424                         if(p_temp->account_alarm_binder.account_id == account_id) {
425                                 EM_DEBUG_EXCEPTION("account match found ");
426
427                                 p_prev->next = p_temp->next;
428                                 EM_SAFE_FREE(p_temp);
429                         
430                                 match_found = true;
431                                 break;
432                         }
433                         
434                         p_prev =  p_temp;
435                         p_temp = p_temp->next;
436                 }               
437         }
438
439         if(!match_found)
440                 return false;
441
442         return true;
443 }
444
445 static int  _emdaemon_update_account_alarm_binder_list(int account_id, alarm_id_t  alarm_id)
446 {
447         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list;
448         int match_found = false;
449
450         if( !account_id || !alarm_id) {
451                 EM_DEBUG_EXCEPTION("Invalid param ");
452                 return false;
453         }
454
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;
460                         match_found = true;
461                         break;
462                 }
463                 p_temp = p_temp->next;
464         }
465
466         if(!match_found) {
467                 EM_DEBUG_EXCEPTION("account match not found ");
468                 return false;
469         }
470
471         return true;
472 }
473 #endif
474