2.0 alpha
[apps/home/calendar.git] / common / acct-svc.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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(account_capability_type_e capability_type, account_capability_state_e capability_value, void* user_data)
21 {
22         c_retvm_if(!user_data, FALSE, "user_data is null");
23
24         bool *has_calendar = user_data;
25
26         if (capability_type == ACCOUNT_CAPABILITY_CALENDAR && capability_value == ACCOUNT_CAPABILITY_ENABLED)
27         {
28                 *has_calendar = TRUE;
29                 return FALSE;
30         }
31
32         return TRUE;
33 }
34
35 static void __cal_account_free_acct(struct _acct *at)
36 {
37         c_ret_if(!at);
38
39         if (at->name)
40                 free(at->name);
41
42         if (at->icon)
43                 free(at->icon);
44
45         free(at);
46 }
47
48 static bool __cal_account_get_callback(account_h handle, void* user_data)
49 {
50         c_retvm_if(!handle, FALSE, "handle is null");
51         c_retvm_if(!user_data, FALSE, "user_data is null");
52
53         Eina_List** account_list = user_data;
54         struct _acct *at = NULL;
55         char *text;
56         account_error_e ret;
57         bool has_calendar = FALSE;
58
59         ret = account_get_capability(handle, __cal_account_get_capability_callback, &has_calendar);
60         c_retvm_if(ret != ACCOUNT_ERROR_NONE, FALSE, "account_get_capability is failed(%x)", ret);
61
62         if (has_calendar == TRUE) {
63                 at = calloc(1, sizeof(struct _acct));
64                 c_retvm_if(!at, FALSE,"calloc is failed");
65         }
66         else
67                 return FALSE;
68
69         text = NULL;
70         ret = account_get_email_address(handle, &text);
71         if (ret != ACCOUNT_ERROR_NONE) {
72                 ERR("account_get_email_address is failed(%x)", ret);
73                 __cal_account_free_acct(at);
74                 return FALSE;
75         }
76
77         if (!CAL_STRLEN(text)) {
78                 ret = account_get_user_name(handle, &text);
79                 if (ret != ACCOUNT_ERROR_NONE) {
80                         ERR("account_get_user_name is failed(%x)", ret);
81                         __cal_account_free_acct(at);
82                         return FALSE;
83                 }
84         }
85
86         if (CAL_STRLEN(text))
87                 at->name = text;
88         else if (text)
89                 free(text);
90
91         text = NULL;
92
93         ret = account_get_icon_path(handle, &text);
94         if (ret != ACCOUNT_ERROR_NONE) {
95                 ERR("account_get_icon_path is failed(%x)", ret);
96                 __cal_account_free_acct(at);
97                 return FALSE;
98         }
99
100         if (CAL_STRLEN(text))
101                 at->icon = text;
102         else if (text)
103                 free(text);
104
105         text = NULL;
106
107         ret = account_get_domain_name(handle, &text);
108         if (ret != ACCOUNT_ERROR_NONE) {
109                 ERR("account_get_domain_name is failed(%x)", ret);
110                 __cal_account_free_acct(at);
111                 return FALSE;
112         }
113
114         if (CAL_STRLEN(text))
115                 at->domain = text;
116         else if (text)
117                 free(text);
118
119         text = NULL;
120
121         ret = account_get_account_id(handle, &at->id);
122         if (ret != ACCOUNT_ERROR_NONE)
123         {
124                 ERR("account_get_account_id is failed(%x)", ret);
125                 __cal_account_free_acct(at);
126                 return FALSE;
127         }
128
129         *account_list = eina_list_append(*account_list, at);
130
131         return TRUE;
132 }
133
134 Eina_List* cal_account_svc_get_account_list(void)
135 {
136         CAL_FN_START;
137
138         Eina_List *account_list = NULL;
139         account_error_e ret;
140
141         ret = account_foreach_account_from_db(__cal_account_get_callback, &account_list);
142         if (ret != ACCOUNT_ERROR_NONE)
143         {
144                 ERR("account_foreach_account_from_db is failed(%x)", ret);
145
146                 CAL_FN_END;
147
148                 return NULL;
149         }
150
151         CAL_FN_END;
152
153         return account_list;
154 }
155
156 void cal_account_svc_free_account_list(Eina_List **h)
157 {
158         c_ret_if(!h);
159         c_ret_if(!*h);
160
161         Eina_List *l;
162         struct _acct *at;
163
164         EINA_LIST_FOREACH(*h, l, at) {
165                 if (at)
166                         continue;
167
168                 if (at->name)
169                         free(at->name);
170
171                 if (at->icon)
172                         free(at->icon);
173
174                 if (at->icon)
175                         free(at->domain);
176
177                 free(at);
178         }
179
180         *h = eina_list_free(*h);
181 }
182
183 char* cal_account_svc_get_account_icon_path(int account_id)
184 {
185         account_error_e ret;
186         account_h account;
187         char* text = NULL;
188         char* icon_path = NULL;
189
190         ret = account_create(&account);
191         c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_create is failed(%d)", ret);
192
193         ret = account_query_account_by_account_id(account_id, &account);
194         if (ret != ACCOUNT_ERROR_NONE)
195         {
196                 ERR("account_query_account_by_account_id is failed(%x)", ret);
197
198                 ret = account_destroy(account);
199                 c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
200
201                 return NULL;
202         }
203
204         ret = account_get_icon_path(account, &text);
205         if (ret != ACCOUNT_ERROR_NONE)
206         {
207                 ERR("account_get_icon_path is failed(%x)", ret);
208
209                 ret = account_destroy(account);
210                 c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
211
212                 return NULL;
213         }
214
215         if (text)
216                 icon_path = strdup(text);
217
218         ret = account_destroy(account);
219         c_retvm_if(ret != ACCOUNT_ERROR_NONE, NULL, "account_destroy is failed(%d)", ret);
220
221         return icon_path;
222 }
223
224 int cal_account_svc_get_facebook_calendar_id(void)
225 {
226         int r = -1;
227         Eina_List *web_account_list = NULL;
228         Eina_List *calendar_list = NULL;
229         Eina_List* list_account;
230         Eina_List* list_calendar;
231         cal_struct* cs;
232
233         struct _acct *at;
234
235         list_account = cal_account_svc_get_account_list();
236
237         if (list_account)
238                 web_account_list = eina_list_merge(web_account_list, list_account);
239
240         EINA_LIST_FOREACH(web_account_list, list_account, at) {
241
242                 at->data = CALENDAR_SVC_GET_ALL(at->id, ALL_CALENDAR_ID, CAL_STRUCT_CALENDAR);
243
244                 if (at->data) {
245                         EINA_LIST_FOREACH(at->data, list_calendar, cs) {
246                                 if (cs) {
247                                         char *calendar_name = CALENDAR_SVC_STRUCT_GET_STR(cs, CAL_TABLE_TXT_NAME);
248                                         if (calendar_name) {
249                                                 if (!CAL_STRCMP(calendar_name, "Facebook")) {
250                                                         r = CALENDAR_SVC_STRUCT_GET_INT(cs, CAL_TABLE_INT_INDEX);
251                                                         CALENDAR_SVC_FREE_CS_LIST(&at->data);
252                                                         cal_account_svc_free_account_list(&web_account_list);
253                                                         return r;
254                                                 }
255                                         }
256                                 }
257                         }
258                 }
259
260                 CALENDAR_SVC_FREE_CS_LIST(&at->data);
261         }
262
263         cal_account_svc_free_account_list(&web_account_list);
264
265         return r;
266 }