[misc] Sycn with master branch.
[apps/core/preloaded/calendar.git] / common / acct-svc.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.1 (the "License");
6   *  you may not use this file except in compliance with the License.
7   *  You may obtain a copy of the License at
8   *
9   *       http://floralicense.org/license/
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17
18 #include "acct-svc.h"
19
20 static bool __cal_account_get_capability_callback(const char* capability_type,
21                                 account_capability_state_e capability_value, void* user_data)
22 {
23         c_retvm_if(!user_data, FALSE, "user_data is null");
24
25         bool *has_calendar = user_data;
26
27         if (strcmp(capability_type, ACCOUNT_SUPPORTS_CAPABILITY_CALENDAR) == 0 &&
28                 capability_value == ACCOUNT_CAPABILITY_ENABLED)
29         {
30                 *has_calendar = TRUE;
31                 return FALSE;
32         }
33
34         return TRUE;
35 }
36
37 static void __cal_account_free_acct(struct _acct *at)
38 {
39         c_ret_if(!at);
40
41         if (at->name)
42                 free(at->name);
43
44         if (at->icon)
45                 free(at->icon);
46
47         free(at);
48 }
49
50 static bool __cal_account_get_callback(account_h handle, void* user_data)
51 {
52         c_retvm_if(!handle, FALSE, "handle is null");
53         c_retvm_if(!user_data, FALSE, "user_data is null");
54
55         Eina_List** account_list = user_data;
56         struct _acct *at = NULL;
57         char *text;
58         account_error_e ret;
59         bool has_calendar = FALSE;
60
61         ret = account_get_capability_all(handle, __cal_account_get_capability_callback, &has_calendar);
62         c_retvm_if(ret != ACCOUNT_ERROR_NONE, FALSE, "account_get_capability is failed(%x)", ret);
63
64         if (has_calendar == TRUE) {
65                 at = calloc(1, sizeof(struct _acct));
66                 c_retvm_if(!at, FALSE,"calloc is failed");
67         }
68         else
69                 return FALSE;
70
71         text = NULL;
72         ret = account_get_email_address(handle, &text);
73         if (ret != ACCOUNT_ERROR_NONE) {
74                 ERR("account_get_email_address is failed(%x)", ret);
75                 __cal_account_free_acct(at);
76                 return FALSE;
77         }
78
79         if (!CAL_STRLEN(text)) {
80                 ret = account_get_user_name(handle, &text);
81                 if (ret != ACCOUNT_ERROR_NONE) {
82                         ERR("account_get_user_name is failed(%x)", ret);
83                         __cal_account_free_acct(at);
84                         return FALSE;
85                 }
86         }
87
88         if (CAL_STRLEN(text))
89                 at->name = text;
90         else if (text)
91                 free(text);
92
93         text = NULL;
94
95         ret = account_get_icon_path(handle, &text);
96         if (ret != ACCOUNT_ERROR_NONE) {
97                 ERR("account_get_icon_path is failed(%x)", ret);
98                 __cal_account_free_acct(at);
99                 return FALSE;
100         }
101
102         if (CAL_STRLEN(text))
103                 at->icon = text;
104         else if (text)
105                 free(text);
106
107         text = NULL;
108
109         ret = account_get_domain_name(handle, &text);
110         if (ret != ACCOUNT_ERROR_NONE) {
111                 ERR("account_get_domain_name is failed(%x)", ret);
112                 __cal_account_free_acct(at);
113                 return FALSE;
114         }
115
116         if (CAL_STRLEN(text))
117                 at->domain = text;
118         else if (text)
119                 free(text);
120
121         text = NULL;
122
123         ret = account_get_account_id(handle, &at->id);
124         if (ret != ACCOUNT_ERROR_NONE)
125         {
126                 ERR("account_get_account_id is failed(%x)", ret);
127                 __cal_account_free_acct(at);
128                 return FALSE;
129         }
130
131         *account_list = eina_list_append(*account_list, at);
132
133         return TRUE;
134 }
135
136 Eina_List* cal_account_svc_get_account_list(void)
137 {
138         CAL_FN_START;
139
140         Eina_List *account_list = NULL;
141         account_error_e ret;
142
143         ret = account_foreach_account_from_db(__cal_account_get_callback, &account_list);
144         if (ret != ACCOUNT_ERROR_NONE)
145         {
146                 ERR("account_foreach_account_from_db is failed(%x)", ret);
147
148                 CAL_FN_END;
149
150                 return NULL;
151         }
152
153         CAL_FN_END;
154
155         return account_list;
156 }
157
158 void cal_account_svc_free_account_list(Eina_List **h)
159 {
160         c_ret_if(!h);
161         c_ret_if(!*h);
162
163         Eina_List *l;
164         struct _acct *at;
165
166         EINA_LIST_FOREACH(*h, l, at) {
167                 if (!at)
168                         continue;
169
170                 if (at->name)
171                         free(at->name);
172
173                 if (at->icon)
174                         free(at->icon);
175
176                 if (at->domain)
177                         free(at->domain);
178
179                 free(at);
180         }
181
182         *h = eina_list_free(*h);
183 }
184
185 char* cal_account_svc_get_account_icon_path(int account_id)
186 {
187         account_error_e ret = ACCOUNT_ERROR_NONE;
188         account_h account = NULL;
189         char* text = NULL;
190         char* icon_path = NULL;
191
192         ret = account_create(&account);
193         c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_create is failed(%d)", ret);
194
195         ret = account_query_account_by_account_id(account_id, &account);
196         if (ret != ACCOUNT_ERROR_NONE)
197         {
198                 ERR("account_query_account_by_account_id is failed(%x)", ret);
199
200                 ret = account_destroy(account);
201                 c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
202
203                 return NULL;
204         }
205
206         ret = account_get_icon_path(account, &text);
207         if (ret != ACCOUNT_ERROR_NONE)
208         {
209                 ERR("account_get_icon_path is failed(%x)", ret);
210
211                 ret = account_destroy(account);
212                 c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
213
214                 return NULL;
215         }
216
217         if (text)
218                 icon_path = strdup(text);
219
220         ret = account_destroy(account);
221         c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
222
223         return icon_path;
224 }
225
226 char* cal_account_svc_get_account_name(int account_id)
227 {
228         account_error_e ret = ACCOUNT_ERROR_NONE;
229         account_h account = NULL;
230         char* text = NULL;
231         char* name = NULL;
232
233         ret = account_create(&account);
234         c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_create is failed(%d)", ret);
235
236         ret = account_query_account_by_account_id(account_id, &account);
237         if (ret != ACCOUNT_ERROR_NONE) {
238                 ERR("account_query_account_by_account_id is failed(%x)", ret);
239
240                 ret = account_destroy(account);
241                 c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
242
243                 return NULL;
244         }
245
246         ret = account_get_user_name(account, &text);
247         if (ret != ACCOUNT_ERROR_NONE) {
248                 ERR("account_get_user_name is failed(%x)", ret);
249
250                 ret = account_destroy(account);
251                 c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
252
253                 return NULL;
254         }
255
256         if (CAL_STRLEN(text))
257                 name = strdup(text);
258
259         ret = account_destroy(account);
260         c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
261
262         return name;
263 }