Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / account / util.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 "utility/sync_util.h"
19 #include "utility/fw_ref.h"
20 #include "plugin/account_plugin.h"
21
22 #include "data-adapter/agent_handler_manager.h"
23 #include "data-adapter/agent_handler_manager_internal.h"
24
25 #include "account/manager.h"
26 #include "account/util_internal.h"
27
28 #ifndef SYNC_AGENT_LOG
29 #undef LOG_TAG
30 #define LOG_TAG "AF_ACCOUNT"
31 #endif
32
33 int acc_is_exist_account(int account_id)
34 {
35         _EXTERN_FUNC_ENTER;
36
37         return da_is_exist_account_id(account_id);
38
39         _EXTERN_FUNC_EXIT;
40 }
41
42 int *acc_get_service_account_id_list(int service_type, int *cnt)
43 {
44         _EXTERN_FUNC_ENTER;
45
46         int *account_id_list = acc_get_account_list(cnt);
47
48         if ((*cnt != 0) && (account_id_list != NULL)) {
49                 int *service_account_id_list = (int *)calloc(*cnt, sizeof(int));
50                 if (service_account_id_list == NULL) {
51
52                         if (account_id_list != NULL) {
53                                 free(account_id_list);
54                         }
55
56                         *cnt = 0;
57                         return 0;
58                 }
59
60                 int i = 0;
61                 for (; i < *cnt; i++) {
62                         service_account_id_list[i] = acc_get_service_account_id(service_type, account_id_list[i]);
63                 }
64                 if (account_id_list != NULL) {
65                         free(account_id_list);
66                 }
67                 return service_account_id_list;
68         }
69
70         if (account_id_list != NULL) {
71                 free(account_id_list);
72         }
73
74         _EXTERN_FUNC_EXIT;
75
76         return 0;
77 }
78
79 int acc_get_service_account_id(int service_type, int account_id)
80 {
81         _EXTERN_FUNC_ENTER;
82
83         plugin_get_service_account_id_cb func = plugin_get_function_get_service_account_id(plugin_get_account_plugin_id());
84
85         if (func != NULL) {
86                 int service_account_id = func(service_type, account_id);
87
88                 return service_account_id;
89         }
90
91         _EXTERN_FUNC_EXIT;
92
93         return -1;
94 }
95
96 int acc_get_fw_account_id(int service_type, int service_account_id, int index)
97 {
98         _EXTERN_FUNC_ENTER;
99
100         plugin_get_fw_account_id_cb func = plugin_get_function_get_fw_account_id(plugin_get_account_plugin_id());
101
102         if (func != NULL) {
103                 int account_id = func(service_type, service_account_id, index);
104
105                 return account_id;
106         }
107
108         _EXTERN_FUNC_EXIT;
109
110         return -1;
111 }
112
113 int *acc_get_account_list(int *cnt)
114 {
115         _EXTERN_FUNC_ENTER;
116
117         sync_agent_da_return_e da_err = sync_agent_open_agent();
118         _DEBUG_INFO("Done Open Agent DB : %d", da_err);
119
120         int *account_list = da_get_account_id_list(cnt);
121
122         da_err = sync_agent_close_agent();
123         _DEBUG_INFO("Done Close Agent DB : %d", da_err);
124
125         _EXTERN_FUNC_EXIT;
126
127         return account_list;
128 }
129
130 void acc_add_platform_account(int fw_account_id)
131 {
132         _EXTERN_FUNC_ENTER;
133
134         _DEBUG_INFO("sync_agent_add_platform_account called");
135         plugin_add_platform_account_cb func = plugin_get_function_add_platform_account(plugin_get_account_plugin_id());
136
137         if (func != NULL) {
138                 _DEBUG_INFO("pFunc is not null");
139                 func(fw_account_id);
140         } else {
141                 _DEBUG_INFO("pFunc is null");
142         }
143
144         _EXTERN_FUNC_EXIT;
145 }
146
147 void acc_delete_platform_account(int fw_account_id)
148 {
149         _EXTERN_FUNC_ENTER;
150
151         _DEBUG_INFO("sync_agent_delete_platform_account called");
152         plugin_delete_platform_account_cb func = plugin_get_function_delete_platform_account(plugin_get_account_plugin_id());
153
154         if (func != NULL) {
155                 _DEBUG_INFO("pFunc is not null");
156                 func(fw_account_id);
157         } else {
158                 _DEBUG_INFO("pFunc is null");
159         }
160
161         _EXTERN_FUNC_EXIT;
162 }
163
164 int *acc_get_fw_account_id_list(int content_type, int *count)
165 {
166         _EXTERN_FUNC_ENTER;
167
168         *count = 0;
169         int fw_account_count = 0;
170         int *fw_account_id_list = da_get_account_id_list(&fw_account_count);
171
172         if (fw_account_count == 0 || fw_account_id_list == NULL) {
173
174                 if (fw_account_id_list != NULL)
175                         free(fw_account_id_list);
176
177                 return 0;
178         }
179
180         int *account_id_list = (int *)calloc(fw_account_count, sizeof(int));
181         if (account_id_list == NULL) {
182
183                 if (fw_account_id_list != NULL)
184                         free(fw_account_id_list);
185
186                 return 0;
187         }
188
189         int i = 0;
190         for (; i < fw_account_count; i++) {
191                 plugin_has_platform_account_service_cb func_has_platform_account_service = plugin_get_function_has_platform_account_service(plugin_get_account_plugin_id());
192                 int has_service_account_id = func_has_platform_account_service(fw_account_id_list[i], content_type);
193
194                 if (has_service_account_id) {
195                         account_id_list[*count] = fw_account_id_list[i];
196                         *count = *count + 1;
197                 }
198         }
199
200         if (fw_account_id_list != NULL) {
201                 free(fw_account_id_list);
202         }
203
204         _EXTERN_FUNC_EXIT;
205
206         return account_id_list;
207 }