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