Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-account.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 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 /******************************************************************************
24  * File: emf-account.c
25  * Desc: email-daemon Account
26  *
27  * Auth:
28  *
29  * History:
30  *    2006.08.16 : created
31  *****************************************************************************/
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <vconf.h>
36
37 #include "email-daemon.h"
38 #include "email-storage.h"
39 #include "c-client.h"
40 #include "email-debug-log.h"
41 #include "email-daemon-account.h"
42 #include "email-daemon-auto-poll.h"
43 #include <contacts.h>
44 #include "email-types.h"
45 #include "email-core-account.h"
46 #include "email-core-event.h"
47 #include "email-core-utils.h"
48 #include "email-utilities.h"
49 #include "email-convert.h"
50
51 static int emdaemon_check_filter_id(int account_id, int filter_id, int* err_code)
52 {
53         EM_DEBUG_FUNC_BEGIN("account_id[%d], filter_id[%d], err_code[%p]", account_id, filter_id, err_code);
54
55         if (account_id != ALL_ACCOUNT) {                /*  only global rule supported. */
56                 EM_DEBUG_EXCEPTION(" account_id[%d], filter_id[%d]", account_id, filter_id);
57
58                 if (err_code != NULL)
59                         *err_code = EMAIL_ERROR_INVALID_PARAM;
60                 return false;
61         }
62
63         int ret = false;
64         int err = EMAIL_ERROR_NONE;
65         emstorage_rule_tbl_t* filter = NULL;
66
67         if (!emstorage_get_rule_by_id(account_id, filter_id, &filter, true, &err)) {
68                 EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
69
70                 goto FINISH_OFF;
71         }
72
73         ret = true;
74
75 FINISH_OFF:
76         if (filter != NULL)
77                 emstorage_free_rule(&filter, 1, NULL);
78
79         if (err_code != NULL)
80                 *err_code = err;
81         EM_DEBUG_FUNC_END();
82         return ret;
83 }
84
85
86 INTERNAL_FUNC int emdaemon_create_account(email_account_t* account, int* err_code)
87 {
88         int ret = false;
89         int err = EMAIL_ERROR_NONE;
90
91         if (!emcore_create_account(account, &err)) {
92                 EM_DEBUG_EXCEPTION(" emcore_account_add failed [%d]", err);
93                 goto FINISH_OFF;
94         }
95
96         emcore_init_account_reference();
97
98         ret = true;
99 FINISH_OFF:
100         if (err_code)
101                 *err_code = err;
102         EM_DEBUG_FUNC_END();
103         return ret;
104 }
105
106
107 INTERNAL_FUNC int emdaemon_delete_account(int account_id, int* err_code)
108 {
109         EM_DEBUG_FUNC_BEGIN();
110         int ret;
111
112         ret = emcore_delete_account(account_id, err_code);
113         EM_DEBUG_FUNC_END();
114         return ret;
115 }
116
117 static email_account_t* duplicate_account(email_account_t *src)
118 {
119         if(!src) {
120                 EM_DEBUG_EXCEPTION("INVALID_PARAM");
121                 return NULL;
122         }
123         email_account_t *dst = (email_account_t *)em_malloc(sizeof(email_account_t));
124         if (!dst) {
125                 EM_DEBUG_EXCEPTION(" malloc failed...");
126                 return NULL;
127         }
128
129         /* Need deep copy */
130         memcpy(dst, src, sizeof(email_account_t));
131         dst->account_name              = EM_SAFE_STRDUP(src->account_name);
132         dst->incoming_server_address   = EM_SAFE_STRDUP(src->incoming_server_address);
133         dst->user_email_address        = EM_SAFE_STRDUP(src->user_email_address);
134         dst->incoming_server_user_name = EM_SAFE_STRDUP(src->incoming_server_user_name);
135         dst->incoming_server_password  = EM_SAFE_STRDUP(src->incoming_server_password);
136         dst->outgoing_server_address   = EM_SAFE_STRDUP(src->outgoing_server_address);
137         dst->outgoing_server_user_name = EM_SAFE_STRDUP(src->outgoing_server_user_name);
138         dst->outgoing_server_password  = EM_SAFE_STRDUP(src->outgoing_server_password);
139         dst->user_display_name         = EM_SAFE_STRDUP(src->user_display_name);
140         dst->reply_to_address          = EM_SAFE_STRDUP(src->reply_to_address);
141         dst->return_address            = EM_SAFE_STRDUP(src->return_address);
142         dst->logo_icon_path            = EM_SAFE_STRDUP(src->logo_icon_path);
143         dst->certificate_path              = EM_SAFE_STRDUP(src->certificate_path);
144         dst->options.display_name_from = EM_SAFE_STRDUP(src->options.display_name_from);
145         dst->options.signature         = EM_SAFE_STRDUP(src->options.signature);
146         dst->user_data                = (void*) em_malloc(src->user_data_length);
147         if( !dst->user_data ) {
148                 EM_DEBUG_EXCEPTION("em_malloc failed");
149                 emcore_free_account(dst);
150                 EM_SAFE_FREE(dst);
151                 return NULL;
152         }
153
154         memcpy(dst->user_data, src->user_data, src->user_data_length);
155
156         return dst;
157 }
158
159 INTERNAL_FUNC int emdaemon_validate_account(int account_id, int *handle, int* err_code)
160 {
161         EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%p], err_code[%p]", account_id, handle, err_code);
162
163         int ret = false;
164         int err = EMAIL_ERROR_NONE;
165         email_event_t event_data = {0};
166         email_account_t* ref_account = NULL;
167
168         if (account_id < 1)  {
169                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
170                 err = EMAIL_ERROR_INVALID_PARAM;
171                 goto FINISH_OFF;
172         }
173
174         if (!(ref_account = emcore_get_account_reference(account_id))) {
175                 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed [%d]", account_id);
176                 err = EMAIL_ERROR_INVALID_ACCOUNT;
177                 goto FINISH_OFF;
178         }
179
180         event_data.type = EMAIL_EVENT_VALIDATE_ACCOUNT;
181         event_data.event_param_data_1 = NULL;
182         event_data.event_param_data_3 = NULL;
183         event_data.account_id = account_id;
184
185         if (!emcore_insert_event(&event_data, (int*)handle, &err))  {
186                 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
187         }
188
189         ret = true;
190
191 FINISH_OFF:
192
193         if (ref_account) {
194                 emcore_free_account(ref_account);
195                 EM_SAFE_FREE(ref_account);
196         }
197
198         if (err_code)
199                 *err_code = err;
200         EM_DEBUG_FUNC_END();
201         return ret;
202 }
203
204
205 INTERNAL_FUNC int emdaemon_validate_account_and_create(email_account_t* new_account, int *handle, int* err_code)
206 {
207         EM_DEBUG_FUNC_BEGIN("account[%p], handle[%p], err_code[%p]", new_account, handle, err_code);
208
209         int ret = false;
210         int err = EMAIL_ERROR_NONE;
211         email_event_t event_data = {0};
212
213         event_data.type = EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT;
214         event_data.event_param_data_1 = (void*) new_account;
215         event_data.event_param_data_3 = NULL;
216         event_data.account_id = NEW_ACCOUNT_ID;
217
218         if (!emcore_insert_event(&event_data, (int*)handle, &err))  {
219                 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
220                 goto FINISH_OFF;
221         }
222
223         ret = true;
224
225 FINISH_OFF:
226         if (err_code)
227                 *err_code = err;
228         EM_DEBUG_FUNC_END();
229         return ret;
230 }
231
232
233 INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_account, int* err_code)
234 {
235         EM_DEBUG_FUNC_BEGIN("account_id[%d], new_account[%p], err_code[%p]", account_id, new_account, err_code);
236
237         /*  default variable */
238         int ret = false;
239         int err = EMAIL_ERROR_NONE;
240         emstorage_account_tbl_t *new_account_tbl = NULL;
241         email_account_t old_account_info = {0};
242
243         if ((account_id <= 0) || !new_account)  {
244                 EM_DEBUG_EXCEPTION("Invalid Parameters.");
245                 err = EMAIL_ERROR_INVALID_PARAM;
246                 goto FINISH_OFF;
247         }
248
249         if(!emdaemon_get_account(account_id, GET_FULL_DATA, &old_account_info, &err)) {
250                 EM_DEBUG_EXCEPTION("emdaemon_get_account failed ");
251                 goto FINISH_OFF;
252         }
253
254         EM_DEBUG_LOG("new_account->email_addr[%s]", new_account->user_email_address);
255         if(new_account->user_email_address) {
256                 if (!em_verify_email_address(new_account->user_email_address, true, &err)) {
257                         err = EMAIL_ERROR_INVALID_ADDRESS;
258                         EM_DEBUG_EXCEPTION("Invalid Email Address");
259                         goto FINISH_OFF;
260                 }
261         }
262
263         new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
264         if(!new_account_tbl) {
265                 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
266                 goto FINISH_OFF;
267         }
268
269         em_convert_account_to_account_tbl(new_account, new_account_tbl);
270
271         if (!emstorage_update_account(account_id, new_account_tbl, true, &err))  {
272                 EM_DEBUG_EXCEPTION("emstorage_update_account falied [%d]", err);
273                 goto FINISH_OFF;
274         }
275
276         emcore_init_account_reference();
277
278 #ifdef __FEATURE_AUTO_POLLING__
279         int  old_check_interval = old_account_info.check_interval;
280         if( old_check_interval < 0 && new_account->check_interval > 0) {
281                 if(!emdaemon_add_polling_alarm(account_id, new_account->check_interval))
282                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
283
284         }
285         else if( (old_check_interval > 0) && (new_account->check_interval < 0)) {
286                 if(!emdaemon_remove_polling_alarm(account_id))
287                         EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
288         }
289         else if(old_check_interval != new_account->check_interval && new_account->check_interval > 0) {
290                 if(!emdaemon_remove_polling_alarm(account_id)) {
291                         EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
292                         goto FINISH_OFF;
293                 }
294                 if(!emdaemon_add_polling_alarm(account_id, new_account->check_interval))
295                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
296         }
297 #endif
298
299         ret = true;
300
301 FINISH_OFF:
302
303         emcore_free_account(&old_account_info);
304
305         if(new_account_tbl)
306                 emstorage_free_account(&new_account_tbl, 1, NULL);
307
308         if (err_code)
309                 *err_code = err;
310
311         EM_DEBUG_FUNC_END();
312         return ret;
313 }
314
315 INTERNAL_FUNC int emdaemon_validate_account_and_update(int old_account_id, email_account_t* new_account_info, int *handle,int *err_code)
316 {
317         EM_DEBUG_FUNC_BEGIN("account[%d], new_account_info[%p], handle[%p], err_code[%p]", old_account_id, new_account_info, handle, err_code);
318
319         int ret = false;
320         int err = EMAIL_ERROR_NONE;
321         email_event_t event_data = {0};
322
323         event_data.type = EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT;
324         event_data.event_param_data_1 = (char *) duplicate_account(new_account_info);
325         event_data.event_param_data_3 = NULL;
326         event_data.account_id = old_account_id;
327
328 #if 0
329         email_account_t *pAccount = (email_account_t *)em_malloc(sizeof(email_account_t));
330         if (pAccount == NULL) {
331                 EM_DEBUG_EXCEPTION(" malloc failed...");
332                 err = EMAIL_ERROR_OUT_OF_MEMORY;
333                 goto FINISH_OFF;
334         }
335
336         /* Need deep copy */
337         memcpy(pAccount, new_account_info, sizeof(email_account_t));
338         pAccount->account_name              = EM_SAFE_STRDUP(new_account_info->account_name);
339         pAccount->incoming_server_address   = EM_SAFE_STRDUP(new_account_info->incoming_server_address);
340         pAccount->user_email_address        = EM_SAFE_STRDUP(new_account_info->user_email_address);
341         pAccount->incoming_server_user_name = EM_SAFE_STRDUP(new_account_info->user_email_address);
342         pAccount->incoming_server_password  = EM_SAFE_STRDUP(new_account_info->incoming_server_password);
343         pAccount->outgoing_server_address   = EM_SAFE_STRDUP(new_account_info->incoming_server_password);
344         pAccount->outgoing_server_user_name = EM_SAFE_STRDUP(new_account_info->outgoing_server_user_name);
345         pAccount->outgoing_server_password  = EM_SAFE_STRDUP(new_account_info->outgoing_server_password);
346         pAccount->user_display_name         = EM_SAFE_STRDUP(new_account_info->user_display_name);
347         pAccount->reply_to_address          = EM_SAFE_STRDUP(new_account_info->reply_to_address);
348         pAccount->return_address            = EM_SAFE_STRDUP(new_account_info->return_address);
349         pAccount->logo_icon_path            = EM_SAFE_STRDUP(new_account_info->logo_icon_path);
350         pAccount->certificate_path                      = EM_SAFE_STRDUP(new_account_info->certificate_path);
351         pAccount->options.display_name_from = EM_SAFE_STRDUP(new_account_info->options.display_name_from);
352         pAccount->options.signature             = EM_SAFE_STRDUP(new_account_info->options.signature);
353         memcpy(pAccount->user_data, new_account_info->user_data, new_account_info->user_data_length);
354 #endif
355
356         if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
357                 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
358                 goto FINISH_OFF;
359         }
360
361         ret = true;
362
363 FINISH_OFF:
364         if (err_code)
365                 *err_code = err;
366         EM_DEBUG_FUNC_END();
367         return ret;
368 }
369
370
371 INTERNAL_FUNC int emdaemon_get_account(int account_id, int pulloption, email_account_t* account, int* err_code)
372 {
373         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption [%d], account[%p], err_code[%p]", account_id, pulloption, account, err_code);
374
375         /*  default variable */
376         int ret = false;
377         int err = EMAIL_ERROR_NONE;
378         emstorage_account_tbl_t *account_tbl = NULL;
379
380         if (!account)  {
381                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
382                 err = EMAIL_ERROR_INVALID_PARAM;
383                 goto FINISH_OFF;
384         }
385
386         if (!emstorage_get_account_by_id(account_id, pulloption, &account_tbl, true, &err)) {
387                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
388                 goto FINISH_OFF;
389         }
390
391         em_convert_account_tbl_to_account(account_tbl, account);
392
393         ret = true;
394
395 FINISH_OFF:
396         if(account_tbl)
397                 emstorage_free_account(&account_tbl, 1, NULL);
398         if (err_code)
399                 *err_code = err;
400         EM_DEBUG_FUNC_END("ret [%d]", ret);
401         return ret;
402 }
403
404 INTERNAL_FUNC int emdaemon_get_account_list(email_account_t** account_list, int* count, int* err_code)
405 {
406         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
407
408         int ret = false, err = EMAIL_ERROR_NONE, i = 0;
409         emstorage_account_tbl_t *account_tbl_array = NULL;
410
411         if (!account_list || !count)  {
412                 err = EMAIL_ERROR_INVALID_PARAM;
413                 goto FINISH_OFF;
414         }
415
416         *count = 1000;
417
418         if (!emstorage_get_account_list(count, &account_tbl_array, true, true, &err))  {
419                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
420                 goto FINISH_OFF;
421         }
422
423         if(account_tbl_array && (*count) > 0) {
424                 *account_list = (email_account_t*)em_malloc(sizeof(email_account_t) * (*count));
425                 if(!*account_list) {
426                         EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
427                         goto FINISH_OFF;
428                 }
429
430                 for(i = 0 ; i < (*count); i++)
431                         em_convert_account_tbl_to_account(account_tbl_array + i, (*account_list) + i);
432         }
433
434         ret = true;
435
436 FINISH_OFF:
437         if(account_tbl_array)
438                 emstorage_free_account(&account_tbl_array, (*count), NULL);
439
440         if (err_code != NULL)
441                 *err_code = err;
442         EM_DEBUG_FUNC_END("ret [%d]", ret);
443         return ret;
444 }
445
446
447 INTERNAL_FUNC int emdaemon_free_account(email_account_t** account_list, int count, int* err_code)
448 {
449         EM_DEBUG_FUNC_BEGIN();
450         return emcore_free_account_list(account_list, count, err_code);
451 }
452
453
454 INTERNAL_FUNC int emdaemon_get_filter(int filter_id, email_rule_t** filter_info, int* err_code)
455 {
456         EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
457
458         if (!filter_info) {
459                 EM_DEBUG_EXCEPTION("filter_id[%d], filter_info[%p]", filter_id, filter_info);
460
461                 if (err_code != NULL)
462                         *err_code = EMAIL_ERROR_INVALID_PARAM;
463                 return false;
464         }
465
466         /*  default variable */
467         int ret = false;
468         int err = EMAIL_ERROR_NONE;
469
470         if (!emstorage_get_rule_by_id(ALL_ACCOUNT, filter_id, (emstorage_rule_tbl_t**)filter_info, true, &err))  {
471                 EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
472                 goto FINISH_OFF;
473         }
474
475         ret = true;
476
477 FINISH_OFF:
478         if (err_code)
479                 *err_code = err;
480         EM_DEBUG_FUNC_END();
481         return ret;
482 }
483
484 INTERNAL_FUNC int emdaemon_get_filter_list(email_rule_t** filter_info, int* count, int* err_code)
485 {
486         EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%p], err_code[%p]", filter_info, count, err_code);
487
488         /*  default variable */
489         int ret = false;
490         int err = EMAIL_ERROR_NONE;
491         int is_completed;
492
493         if (!filter_info || !count)  {
494                 EM_DEBUG_EXCEPTION(" filter_info[%p], count[%p]", filter_info, count);
495                 err = EMAIL_ERROR_INVALID_PARAM;
496                 goto FINISH_OFF;
497         }
498
499         *count = 1000;
500
501         if (!emstorage_get_rule(ALL_ACCOUNT, 0, 0, count, &is_completed, (emstorage_rule_tbl_t**)filter_info, true, &err))  {
502                 EM_DEBUG_EXCEPTION(" emstorage_get_rule failed [%d]", err);
503                 goto FINISH_OFF;
504         }
505
506         ret = true;
507
508 FINISH_OFF:
509         if (err_code)
510                 *err_code = err;
511         EM_DEBUG_FUNC_END();
512         return ret;
513 }
514
515 INTERNAL_FUNC int emdaemon_find_filter(email_rule_t* filter_info, int* err_code)
516 {
517         EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
518
519         /*  default variable */
520         int ret = false;
521         int err = EMAIL_ERROR_NONE;
522
523         if (!filter_info)  {
524                 EM_DEBUG_EXCEPTION(" filter_info[%p]", filter_info);
525                 err = EMAIL_ERROR_INVALID_PARAM;
526                 goto FINISH_OFF;
527         }
528
529         if (filter_info->faction == EMAIL_FILTER_MOVE && !filter_info->target_mailbox_id) {
530                 EM_DEBUG_EXCEPTION(" filter_info->faction[%d], filter_info->target_mailbox_id[%d]", filter_info->faction, filter_info->target_mailbox_id);
531                 err = EMAIL_ERROR_INVALID_FILTER;
532                 goto FINISH_OFF;
533         }
534
535         filter_info->account_id = ALL_ACCOUNT;          /*  MUST BE */
536
537         if (!emstorage_find_rule((emstorage_rule_tbl_t*)filter_info, true, &err))  {
538                 EM_DEBUG_EXCEPTION(" emstorage_find_rule failed [%d]", err);
539                 err = EMAIL_ERROR_FILTER_NOT_FOUND;
540                 goto FINISH_OFF;
541         }
542
543         ret = true;
544
545 FINISH_OFF:
546         if (err_code)
547                 *err_code = err;
548         EM_DEBUG_FUNC_END();
549         return ret;
550 }
551
552 INTERNAL_FUNC int emdaemon_add_filter(email_rule_t* filter_info)
553 {
554         EM_DEBUG_FUNC_BEGIN("filter_info[%p]", filter_info);
555
556         /*  default variable */
557         int err = EMAIL_ERROR_NONE;
558         if (!filter_info || !(filter_info->value))  {
559                 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
560                 err = EMAIL_ERROR_INVALID_PARAM;
561                 goto FINISH_OFF;
562         }
563
564         /*
565         if (filter_info->faction != EMAIL_FILTER_BLOCK)  {
566                 EM_DEBUG_EXCEPTION("filter_info->faction[%d] is not supported", filter_info->faction);
567                 err = EMAIL_ERROR_NOT_SUPPORTED;
568                 goto FINISH_OFF;
569         }
570
571         if (filter_info->faction == EMAIL_FILTER_MOVE && !filter_info->mailbox)  {
572                 EM_DEBUG_EXCEPTION("filter_info->faction[%d], filter_info->mailbox[%p]", filter_info->faction, filter_info->mailbox);
573                 err = EMAIL_ERROR_INVALID_FILTER;
574                 goto FINISH_OFF;
575         }
576         */
577
578         filter_info->account_id = ALL_ACCOUNT;
579
580         if (emstorage_find_rule((emstorage_rule_tbl_t*)filter_info, true, &err))  {
581                 EM_DEBUG_EXCEPTION("emstorage_find_rule failed [%d]", err);
582                 err = EMAIL_ERROR_ALREADY_EXISTS;
583                 goto FINISH_OFF;
584         }
585
586         if (!emstorage_add_rule((emstorage_rule_tbl_t*)filter_info, true, &err))  {
587                 EM_DEBUG_EXCEPTION("emstorage_add_rule failed [%d]", err);
588                 goto FINISH_OFF;
589         }
590
591         if (filter_info->type == EMAIL_PRIORITY_SENDER) {
592                 EM_DEBUG_LOG("Priority Sender add");
593                 goto FINISH_OFF;
594         }
595
596         if (!emcore_mail_filter_by_rule((email_rule_t*)filter_info, &err))  {
597                 EM_DEBUG_EXCEPTION("emcore_mail_filter_by_rule failed [%d]", err);
598                 goto FINISH_OFF;
599         }
600
601 FINISH_OFF:
602
603         EM_DEBUG_FUNC_END();
604         return err;
605 }
606
607 INTERNAL_FUNC int emdaemon_update_filter(int filter_id, email_rule_t* filter_info, int* err_code)
608 {
609         EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
610
611         /*  default variable */
612         int ret = false;
613         int err = EMAIL_ERROR_NONE;
614
615         if ((filter_id <= 0) || !filter_info)  {
616                 EM_DEBUG_EXCEPTION("filter_id[%d], filter_info[%p]", filter_id, filter_info);
617                 err = EMAIL_ERROR_INVALID_PARAM;
618                 goto FINISH_OFF;
619         }
620
621         if (!emdaemon_check_filter_id(ALL_ACCOUNT, filter_id, &err))  {
622                 EM_DEBUG_EXCEPTION("emdaemon_check_filter_id falied [%d]", err);
623                 goto FINISH_OFF;
624         }
625
626         if (!emstorage_change_rule(ALL_ACCOUNT, filter_id, (emstorage_rule_tbl_t*)filter_info, true, &err))  {
627                 EM_DEBUG_EXCEPTION("emstorage_change_rule falied [%d]", err);
628                 goto FINISH_OFF;
629         }
630
631         ret = true;
632
633 FINISH_OFF:
634         if (err_code)
635                 *err_code = err;
636         EM_DEBUG_FUNC_END();
637         return ret;
638 }
639
640 INTERNAL_FUNC int emdaemon_delete_filter(int filter_id, int* err_code)
641 {
642         EM_DEBUG_FUNC_BEGIN("filter_id[%d, err_code[%p]", filter_id, err_code);
643
644         /*  default variable */
645         int ret = false;
646         int err = EMAIL_ERROR_NONE;
647
648         if (filter_id <= 0)  {
649                 EM_DEBUG_EXCEPTION(" fliter_id[%d]", filter_id);
650                 err = EMAIL_ERROR_INVALID_PARAM;
651                 goto FINISH_OFF;
652         }
653
654         if (!emdaemon_check_filter_id(ALL_ACCOUNT, filter_id, &err))  {
655                 EM_DEBUG_EXCEPTION(" emdaemon_check_filter_id failed [%d]", err);
656                 goto FINISH_OFF;
657         }
658
659         if (!emstorage_delete_rule(ALL_ACCOUNT, filter_id, true, &err))  {
660                 EM_DEBUG_EXCEPTION(" emstorage_delete_rule failed [%d]", err);
661                 goto FINISH_OFF;
662         }
663
664         ret = true;
665
666 FINISH_OFF:
667         if (err_code)
668                 *err_code = err;
669         EM_DEBUG_FUNC_END();
670         return ret;
671 }
672
673 INTERNAL_FUNC int emdaemon_free_filter(email_rule_t** filter_info, int count, int* err_code)
674 {
675         EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%d], err_code[%p]", filter_info, count, err_code);
676
677         /*  default variable */
678         int ret = false;
679         int err = EMAIL_ERROR_NONE;
680
681         if (count > 0)  {
682                 if (!filter_info || !*filter_info)  {
683                         EM_DEBUG_EXCEPTION(" filter_info[%p], count[%d]", filter_info, count);
684                         err = EMAIL_ERROR_INVALID_PARAM;
685                         goto FINISH_OFF;
686                 }
687
688                 email_rule_t* p = *filter_info;
689                 int i;
690
691                 for (i = 0; i < count; i++)  {
692                         EM_SAFE_FREE(p[i].value);
693                 }
694
695                 EM_SAFE_FREE(p); *filter_info  = NULL;
696         }
697
698         ret = true;
699
700 FINISH_OFF:
701         if (err_code)
702                 *err_code = err;
703         EM_DEBUG_FUNC_END();
704         return ret;
705 }
706
707
708 /* ----- internal functions --------------------------------------------*/
709 int emdaemon_initialize_account_reference()
710 {
711         EM_DEBUG_FUNC_BEGIN();
712         int err = EMAIL_ERROR_NONE;
713
714         if ((err = emcore_init_account_reference()) != EMAIL_ERROR_NONE) {
715                 if (err == EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
716                         if ((err = emcore_recover_from_secured_storage_failure()) != EMAIL_ERROR_NONE) {
717                                 EM_DEBUG_EXCEPTION("emcore_recover_from_secured_storage_failure failed [%d]", err);
718                                 goto FINISH_OFF;
719                         }
720
721                         if ((err = emcore_init_account_reference()) != EMAIL_ERROR_NONE) {
722                                 EM_DEBUG_EXCEPTION("emcore_init_account_reference failed [%d]", err);
723                                 goto FINISH_OFF;
724                         }
725                 }
726                 else {
727                         EM_DEBUG_EXCEPTION("emcore_init_account_reference failed [%d]", err);
728                         goto FINISH_OFF;
729                 }
730         }
731
732 FINISH_OFF:
733
734         EM_DEBUG_FUNC_END("err [%d]", err);
735         return err;
736 }
737
738 INTERNAL_FUNC int emdaemon_insert_accountinfo_to_contact(email_account_t* account)
739 {
740         EM_DEBUG_FUNC_BEGIN();
741
742         if(!account)
743                 return false;
744
745         int ret = false;
746         EM_DEBUG_FUNC_END();
747         return ret;
748 }
749
750 INTERNAL_FUNC int emdaemon_update_accountinfo_to_contact(email_account_t* old_account, email_account_t* new_account)
751 {
752         EM_DEBUG_FUNC_BEGIN();
753
754         if(!old_account || !new_account)
755                 return false;
756
757         int ret = false;
758         EM_DEBUG_FUNC_END();
759         return ret;
760 }
761
762 /* EOF */