2.0_alpha release commit
[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 INTERNAL_FUNC int emdaemon_alarm_polling_cb(alarm_id_t  alarm_id, void* user_param)
169 {
170         EM_DEBUG_FUNC_BEGIN();
171
172 /*      tzset(); */
173          time_t ct = time(&ct);
174         struct tm* lt = localtime(&ct);
175         
176         if (lt) {
177                 EM_DEBUG_LOG( "Current Time :  [%d-%d-%d %d:%d:%d] ",
178                         lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
179                         lt->tm_hour, lt->tm_min, lt->tm_sec);                           
180         }
181         
182
183         email_mailbox_t mailbox = {0};
184         int account_id = 0, err = EMAIL_ERROR_NONE, timer_interval =0, alarmID =0,ret = false;
185         char* mailbox_name = NULL;
186
187         if(!_emdaemon_get_polling_account_and_timeinterval(alarm_id,&account_id,&timer_interval)) {
188                 EM_DEBUG_EXCEPTION("email_get_polling_account failed");
189                 return false;
190         }
191
192         EM_DEBUG_EXCEPTION(" emdaemon_alarm_polling_cb : account_id [%d]",account_id);
193         /* create alarm, for polling */ 
194         if(!_emdaemon_create_alarm(timer_interval,&alarmID)) {
195                 EM_DEBUG_EXCEPTION("_emdaemon_create_alarm failed");
196                 return false;
197         }
198
199         /*update alarm ID in list */
200         /* delete from list */
201         if(!_emdaemon_update_account_alarm_binder_list(account_id,alarmID)) {
202                 EM_DEBUG_EXCEPTION("_emdaemon_update_account_alarm_binder_list  failed");
203                 return false;
204         }
205
206         memset(&mailbox, 0x00, sizeof(email_mailbox_t));
207         mailbox.account_id = account_id;
208
209                 if (!emstorage_get_mailbox_name_by_mailbox_type(mailbox.account_id,EMAIL_MAILBOX_TYPE_INBOX,&mailbox_name, false, &err))  {
210                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
211                                                 
212         
213                         goto FINISH_OFF;
214                 }
215                 mailbox.mailbox_name = mailbox_name;
216
217         if (!emdaemon_sync_header(account_id, mailbox.mailbox_id, NULL, &err))  {
218                 EM_DEBUG_EXCEPTION("emdaemon_sync_header falied [%d]", err);            
219                 goto FINISH_OFF;
220         }
221
222         ret = true;
223 FINISH_OFF :
224         EM_SAFE_FREE(mailbox_name);
225
226         return ret;
227 }
228
229 static int _emdaemon_get_polling_alarm_and_timerinterval(int account_id, alarm_id_t *alarm_id, int *timer_interval)
230 {
231         EM_DEBUG_FUNC_BEGIN();
232
233         if(!account_id || !alarm_id)
234                 return false;   
235
236         if(g_account_alarm_binder_list == NULL) {
237                 EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL ");
238                 return false;
239         }
240
241         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list;
242         int match_found = false;
243
244         while(p_temp != NULL) {
245                 if(p_temp->account_alarm_binder.account_id == account_id) {
246                         EM_DEBUG_EXCEPTION("account match found ");
247                         *alarm_id =  p_temp->account_alarm_binder.alarm_id;
248                         
249                         if(timer_interval)
250                                 *timer_interval = p_temp->account_alarm_binder.timer_interval;
251                         
252                         match_found = true;
253                         break;
254                 }
255
256                 p_temp = p_temp->next;
257         }
258
259         if(!match_found) {
260                 EM_DEBUG_EXCEPTION("account match not found ");
261                 return false;
262         }
263
264
265         return true;
266 }
267
268 static int _emdaemon_get_polling_account_and_timeinterval(alarm_id_t  alarm_id, int *account_id, int *timer_interval)
269 {
270         EM_DEBUG_FUNC_BEGIN();
271
272         if(!alarm_id || !account_id)
273                 return false;   
274
275         if(g_account_alarm_binder_list == NULL) {
276                 EM_DEBUG_EXCEPTION("g_account_alarm_binder_list NULL ");
277                 return false;
278         }
279
280         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list;
281         int match_found = false;
282
283         while(p_temp != NULL) {
284                 if(p_temp->account_alarm_binder.alarm_id == alarm_id) {
285                         EM_DEBUG_EXCEPTION("aalrm match found ");
286                         *account_id =  p_temp->account_alarm_binder.account_id;
287                         
288                         if(timer_interval)
289                                 *timer_interval = p_temp->account_alarm_binder.timer_interval;
290                         
291                         match_found = true;
292                         break;
293                 }
294
295                 p_temp = p_temp->next;
296         }
297
298         if(!match_found) {
299                 EM_DEBUG_EXCEPTION("aalrm  match not found ");
300                 return false;
301         }
302         
303         return true;
304 }
305
306 #define AUTO_POLL_DESTINATION   "org.tizen.email-service"
307 static int _emdaemon_create_alarm(int alarm_interval, alarm_id_t *p_alarm_id)
308 {
309         EM_DEBUG_FUNC_BEGIN();
310
311         /*      tzset(); */
312         time_t ct = time(&ct);
313         struct tm* lt = localtime(&ct);
314         
315         if (lt) {
316                 EM_DEBUG_LOG( "Current Time : [%d-%d-%d %d:%d:%d] ",
317                         lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
318                         lt->tm_hour, lt->tm_min, lt->tm_sec);                           
319         }
320
321         /*      alarm_info_t alarm_info = {0};   */
322         int a_nErrorCode = 0;
323         int retval =0;
324                 
325         if((alarm_interval <= 0) || !p_alarm_id) {
326                 EM_DEBUG_EXCEPTION("Invalid param ");
327                 return false;
328         }
329         
330         /*      time_t current_time = {0}; */
331         /*      struct tm current_tm = {0};      */
332         int error_code = 0;
333
334         /* Fill alarm info */   
335         /*      int                     timeFormat = 0; */ /*0 means 12hrs , 1 means 24hrs*/
336
337         /* TO DO, need to findout header for DBG_MID_MSGPORTING_NORMAL and then we have to use this */
338         /* error_code = vconf_get_int(DBG_MID_MSGPORTING_NORMAL, &timeFormat); */
339
340         a_nErrorCode = alarmmgr_init(AUTO_POLL_DESTINATION);
341         EM_DEBUG_LOG("ErrorCode :%d, Return Value:%d ",a_nErrorCode,retval);
342
343         if(!retval)
344                 return false;
345         
346         a_nErrorCode = alarmmgr_set_cb(emdaemon_alarm_polling_cb, NULL);
347         EM_DEBUG_LOG("ErrorCode :%d, Return Value:%d ",a_nErrorCode,retval);
348
349         if(!retval)
350                 return false;   
351
352         error_code = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, alarm_interval * 60 /*(sec)*/, ALARM_REPEAT_MODE_ONCE, AUTO_POLL_DESTINATION, p_alarm_id);
353         
354         EM_DEBUG_LOG("ErrorCode :%d,Return Value :%d ",error_code,retval);
355
356         if(!retval)
357                 return false;   
358
359         return true;
360 }
361
362 INTERNAL_FUNC int emdaemon_free_account_alarm_binder_list()
363 {
364         EM_DEBUG_FUNC_BEGIN();
365
366         email_account_alarm_binder_list_t *p = g_account_alarm_binder_list, *p_next = NULL;     
367         int a_nErrorCode = 0;
368         
369         /* delete alarm as well */
370         while (p) {
371                 /* delete alarm */
372                 if (p->account_alarm_binder.alarm_id  > 0) {
373                         a_nErrorCode = alarmmgr_remove_alarm(p->account_alarm_binder.alarm_id);
374                 }
375
376                 p_next = p->next;
377                 EM_SAFE_FREE(p);
378                 p = p_next;
379         }
380
381         g_account_alarm_binder_list = NULL;
382         
383         return true;
384 }
385
386 static int  _emdaemon_add_to_account_alarm_binder_list(email_account_alarm_binder_list_t *p_account_alarm_binder)
387 {
388         email_account_alarm_binder_list_t  *p_temp = NULL;
389
390         if(!p_account_alarm_binder) {
391                 EM_DEBUG_EXCEPTION("Invalid param ");
392                 return false;
393         }
394
395         if(g_account_alarm_binder_list == NULL) {
396                 g_account_alarm_binder_list = p_account_alarm_binder;
397                 p_account_alarm_binder = NULL;
398         }
399         else {
400                 p_temp = g_account_alarm_binder_list;
401                 while(p_temp->next != NULL)
402                         p_temp = p_temp->next;
403
404                 p_temp->next = p_account_alarm_binder;
405                 p_account_alarm_binder = NULL;
406         }
407
408         return true;
409 }
410
411 static int  _emdaemon_remove_from_account_alarm_binder_list(int account_id)
412 {
413         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list,  *p_prev = NULL;
414         int match_found = false;
415         
416         if(!account_id) {
417                 EM_DEBUG_EXCEPTION("Invalid param ");
418                 return false;
419         }
420
421         /* first node mattch */
422         if(p_temp->account_alarm_binder.account_id == account_id) {
423                 /* match found */
424                 match_found = true;
425                 g_account_alarm_binder_list = p_temp->next;
426                 EM_SAFE_FREE(p_temp);
427         }
428         else {
429                 while(p_temp != NULL) {                 
430                         if(p_temp->account_alarm_binder.account_id == account_id) {
431                                 EM_DEBUG_EXCEPTION("account match found ");
432
433                                 p_prev->next = p_temp->next;
434                                 EM_SAFE_FREE(p_temp);
435                         
436                                 match_found = true;
437                                 break;
438                         }
439                         
440                         p_prev =  p_temp;
441                         p_temp = p_temp->next;
442                 }               
443         }
444
445         if(!match_found)
446                 return false;
447
448         return true;
449 }
450
451 static int  _emdaemon_update_account_alarm_binder_list(int account_id, alarm_id_t  alarm_id)
452 {
453         email_account_alarm_binder_list_t  *p_temp = g_account_alarm_binder_list;
454         int match_found = false;
455
456         if( !account_id || !alarm_id) {
457                 EM_DEBUG_EXCEPTION("Invalid param ");
458                 return false;
459         }
460
461         while(p_temp != NULL) {
462                 if(p_temp->account_alarm_binder.account_id == account_id) {
463                         EM_DEBUG_EXCEPTION("account match found ");
464                         /* update alarm id */
465                         p_temp->account_alarm_binder.alarm_id = alarm_id;
466                         match_found = true;
467                         break;
468                 }
469                 p_temp = p_temp->next;
470         }
471
472         if(!match_found) {
473                 EM_DEBUG_EXCEPTION("account match not found ");
474                 return false;
475         }
476
477         return true;
478 }
479 #endif
480