Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-core / email-core-global.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 /******************************************************************************
25  * File :  email-core-global.c
26  * Desc :  Mail Engine Global
27  *
28  * Auth :  Kyuho Jo 
29  *
30  * History : 
31  *    2010.08.25  :  created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "email-core-global.h"
37 #include "email-debug-log.h"
38 #include "email-types.h"
39 #include "email-utilities.h"
40 #include "email-core-account.h"
41
42 static email_account_list_t *g_unvalidated_account_list = NULL;
43 static pthread_mutex_t _unvalidated_account_lock = PTHREAD_MUTEX_INITIALIZER;
44
45 extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind) __THROW __nonnull ((1));
46
47 INTERNAL_FUNC int emcore_get_account_from_unvalidated_account_list(int input_unvalidated_account_id, email_account_t **oupput_account)
48 {
49         EM_DEBUG_FUNC_BEGIN("input_unvalidated_account_id[%d], oupput_account[%p]", input_unvalidated_account_id, oupput_account);
50         email_account_list_t *account_node = NULL;
51         int err = EMAIL_ERROR_NONE;
52
53         if(oupput_account == NULL) {
54                 err = EMAIL_ERROR_INVALID_PARAM;
55                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
56                 goto FINISH_OFF;
57         }
58
59         *oupput_account = NULL;
60
61         ENTER_CRITICAL_SECTION(_unvalidated_account_lock);
62         account_node = g_unvalidated_account_list;
63         while(account_node) {
64                 if(account_node->account->account_id == input_unvalidated_account_id) {
65                         emcore_duplicate_account(account_node->account, oupput_account, NULL);
66                         break;
67                 }
68                 account_node = account_node->next;
69         }
70         LEAVE_CRITICAL_SECTION(_unvalidated_account_lock);
71
72         if(*oupput_account == NULL)
73                 err = EMAIL_ERROR_DATA_NOT_FOUND;
74
75 FINISH_OFF:
76         EM_DEBUG_FUNC_END("err [%d]", err);
77         return err;
78 }
79
80 INTERNAL_FUNC int emcore_add_account_to_unvalidated_account_list(email_account_t *input_new_account)
81 {
82         EM_DEBUG_FUNC_BEGIN("input_new_account[%p]", input_new_account);
83         email_account_list_t **account_node = NULL;
84         email_account_list_t *new_account_node = NULL;
85         int new_account_id = -1;
86         int err = EMAIL_ERROR_NONE;
87
88         if(input_new_account == NULL) {
89                 err = EMAIL_ERROR_INVALID_PARAM;
90                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
91                 goto FINISH_OFF;
92         }
93
94         new_account_node = em_malloc(sizeof(email_account_list_t));
95
96         if(new_account_node == NULL) {
97                 err = EMAIL_ERROR_OUT_OF_MEMORY;
98                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
99                 goto FINISH_OFF;
100         }
101
102         new_account_node->account = input_new_account;
103         new_account_node->next = NULL;
104
105         ENTER_CRITICAL_SECTION(_unvalidated_account_lock);
106         account_node = &g_unvalidated_account_list;
107
108         while(*account_node) {
109                 if((*account_node)->account->account_id < new_account_id) {
110                         new_account_id = (*account_node)->account->account_id - 1;
111                 }
112                 account_node = &((*account_node)->next);
113         }
114
115         input_new_account->account_id = new_account_id;
116         *account_node = new_account_node;
117         LEAVE_CRITICAL_SECTION(_unvalidated_account_lock);
118
119 FINISH_OFF:
120         EM_DEBUG_FUNC_END("err [%d]", err);
121         return err;
122 }
123
124 INTERNAL_FUNC int emcore_delete_account_from_unvalidated_account_list(int input_account_id)
125 {
126         EM_DEBUG_FUNC_BEGIN("input_account_id[%d]", input_account_id);
127         email_account_list_t *account_node = NULL, *prev_node = NULL;
128         email_account_t *found_account = NULL;
129         int err = EMAIL_ERROR_NONE;
130
131         ENTER_CRITICAL_SECTION(_unvalidated_account_lock);
132         account_node = g_unvalidated_account_list;
133
134         while(account_node) {
135                 if(account_node->account->account_id == input_account_id) {
136                         found_account = account_node->account;
137                         if(prev_node)
138                                 prev_node->next = account_node->next;
139                         else
140                                 g_unvalidated_account_list = account_node->next;
141                         break;
142                 }
143                 prev_node = account_node;
144                 account_node = account_node->next;
145         }
146
147         if(found_account) {
148                 emcore_free_account(found_account);
149                 EM_SAFE_FREE(found_account);
150                 EM_SAFE_FREE(account_node);
151         } else {
152                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_DATA_NOT_FOUND");
153                 err = EMAIL_ERROR_DATA_NOT_FOUND;
154         }
155         LEAVE_CRITICAL_SECTION(_unvalidated_account_lock);
156
157         EM_DEBUG_FUNC_END("err [%d]", err);
158         return err;
159 }
160
161
162