merge with master
[platform/core/system/sync-agent.git] / src / framework / account / service.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 "plugin/data_connector_plugin.h"
19 #include "plugin/account_plugin.h"
20 #include "utility/fw_alloc.h"
21 #include "utility/sync_util.h"
22
23 #include "data-adapter/agent_handler_manager.h"
24 #include "data-adapter/agent_handler_manager_internal.h"
25
26 #include "account/service.h"
27 #include "account/util_internal.h"
28
29 #ifndef EXPORT_API
30 #define EXPORT_API __attribute__ ((visibility("default")))
31 #endif
32
33 #ifndef SYNC_AGENT_LOG
34 #undef LOG_TAG
35 #define LOG_TAG "AF_ACCOUNT"
36 #endif
37
38 EXPORT_API sync_agent_acc_error_e sync_agent_construct_account_tbl_from_service()
39 {
40         _EXTERN_FUNC_ENTER;
41
42         sync_agent_acc_error_e err_code = SYNC_AGENT_ACC_SUCCESS;
43         int fw_account_id_cnt = 0;
44         int content_count = 0;
45         int *fw_account_id_list = NULL;
46         int *service_plugin_id_list = plugin_get_data_connector_plugin_id_list(&content_count);
47
48         if (service_plugin_id_list == NULL) {
49                 _DEBUG_INFO("No Data PlugIn loaded!!");
50                 err_code = SYNC_AGENT_ACC_SUCCESS;
51                 goto return_part;
52         }
53
54         int index = 0;
55         for (; index < content_count; index++) {
56                 int service = service_plugin_id_list[index];
57                 _DEBUG_INFO("Service ID : %d", service);
58
59                 // Get account id List from agent account table
60                 fw_account_id_list = da_get_account_id_list(&fw_account_id_cnt);
61
62                 _DEBUG_INFO("fw_account_id_cnt : %d", fw_account_id_cnt);
63
64                 //Get current All Service Account ID
65                 plugin_get_account_id_list_cb func_get_service_account_id_list = plugin_get_function_get_account_id_list(service);
66                 if (func_get_service_account_id_list == NULL) {
67                         _DEBUG_INFO("func_get_service_account_id_list is null : %d", service);
68
69                         if (fw_account_id_list != NULL) {
70                                 free(fw_account_id_list);
71                                 fw_account_id_list = NULL;
72                         }
73                         continue;
74                 }
75
76                 int service_account_id_cnt;
77                 int *service_account_id_list = func_get_service_account_id_list(&service_account_id_cnt);
78                 if (service_account_id_list == NULL) {
79                         _DEBUG_ERROR("service_account_id_list is NULL!!");
80                         err_code = SYNC_AGENT_ACC_FAIL;
81                         goto return_part;
82                 }
83
84                 _DEBUG_INFO("[%d] service_account_id_cnt : %d", service, service_account_id_cnt);
85
86                 int *service_account_id_new = 0;
87                 if (service_account_id_cnt != 0) {
88                         service_account_id_new = (int *)UTIL_MALLOC(sizeof(int) * service_account_id_cnt);
89                         if (service_account_id_new == NULL) {
90                                 _DEBUG_ERROR("fw_malloc Failed!!");
91                                 err_code = SYNC_AGENT_ACC_FAIL;
92                                 goto return_part;
93                         }
94
95                         int i = 0;
96                         for (; i < service_account_id_cnt; i++) {
97                                 service_account_id_new[i] = 1;
98                                 _DEBUG_INFO("service_account_id_list[%d] : %d", i, service_account_id_list[i]);
99                         }
100                 }
101
102                 int i = 0;
103                 for (; fw_account_id_list != 0 && i < fw_account_id_cnt; i++) {
104
105                         plugin_has_platform_account_service_cb func_has_platform_account_service = plugin_get_function_has_platform_account_service(plugin_get_account_plugin_id());
106                         int has_service_account_id = func_has_platform_account_service(fw_account_id_list[i], service);
107                         if (has_service_account_id) {
108
109                                 // Get Service Account ID, Fw has
110                                 int fw_service_account_id = acc_get_service_account_id(service, fw_account_id_list[i]);
111                                 _DEBUG_INFO("fw_account_id_list[%d] : %d", i, fw_account_id_list[i]);
112                                 _DEBUG_INFO("fw_service_account_id : %d", fw_service_account_id);
113
114                                 int k = 0;
115                                 for (; k < service_account_id_cnt; k++) {
116                                         if (fw_service_account_id == service_account_id_list[k]) {
117                                                 _DEBUG_INFO("service_account_id_list[%d] : %d", k, service_account_id_list[k]);
118                                                 service_account_id_new[k] = 0;
119                                                 break;
120                                         }
121                                 }
122
123                                 //_DEBUG_INFO("\n\nk : %d\n", k);
124                                 // Delete fw_service_account_id from vconf
125                                 if (k >= service_account_id_cnt) {
126                                         plugin_delete_platform_account_service_cb func_del_platform_account_service = plugin_get_function_delete_platform_account_service(plugin_get_account_plugin_id());
127                                         func_del_platform_account_service(fw_account_id_list[i], service);
128                                         err_code = SYNC_AGENT_ACC_CHANGED;
129                                 }
130                         }
131                 }
132
133                 i = 0;
134                 for (; i < service_account_id_cnt; i++) {
135                         if (service_account_id_new[i] == 1) {
136                                 int k = 0;
137                                 for (; fw_account_id_list != 0 && k < fw_account_id_cnt; k++) {
138                                         // find empty slot in fw_account_id_list[k]
139                                         plugin_has_platform_account_service_cb func_has_platform_account_service = plugin_get_function_has_platform_account_service(plugin_get_account_plugin_id());
140                                         int has_service_account_id = func_has_platform_account_service(fw_account_id_list[k], service);
141                                         if (!has_service_account_id) {
142                                                 plugin_set_platform_account_service_cb pFunc_Set_PlatformAccount_Service = plugin_get_function_set_platform_account_service(plugin_get_account_plugin_id());
143                                                 pFunc_Set_PlatformAccount_Service(fw_account_id_list[k], service, service_account_id_list[i]);
144                                                 service_account_id_new[i] = 0;
145                                                 break;
146                                         }
147                                 }
148
149                                 if (service_account_id_new[i] == 1) {
150                                         // Create new FW account and append service account id
151                                         _DEBUG_INFO("Create new FW account!!");
152                                         sync_agent_fw_account_s account;
153                                         account.email = "0";
154                                         account.password = "0";
155                                         account.access_name = "Account";
156
157                                         int fw_account_id = da_add_fw_account_id(&account);;
158                                         plugin_set_platform_account_service_cb func_set_platform_account_service = plugin_get_function_set_platform_account_service(plugin_get_account_plugin_id());
159                                         func_set_platform_account_service(fw_account_id, service, service_account_id_list[i]);
160                                 }
161                                 err_code = SYNC_AGENT_ACC_CHANGED;
162                         }
163                 }
164
165                 if (fw_account_id_list != NULL) {
166                         free(fw_account_id_list);
167                         fw_account_id_list = NULL;
168                 }
169         }
170
171  return_part:
172         _EXTERN_FUNC_EXIT;
173
174         if (service_plugin_id_list != NULL) {
175                 free(service_plugin_id_list);
176                 service_plugin_id_list = NULL;
177         }
178         if (fw_account_id_list != NULL) {
179                 free(fw_account_id_list);
180                 fw_account_id_list = NULL;
181         }
182
183         _DEBUG_INFO("err_code = %d", err_code);
184         return err_code;
185 }
186
187 EXPORT_API sync_agent_acc_error_e sync_agent_create_service_account_info(sync_agent_service_account_info_s ** account_info)
188 {
189         _EXTERN_FUNC_ENTER;
190
191         sync_agent_acc_error_e err_code = SYNC_AGENT_ACC_SUCCESS;
192
193         sync_agent_service_account_info_s *service_account_info = (sync_agent_service_account_info_s *) calloc(1, sizeof(sync_agent_service_account_info_s));
194         if (service_account_info == NULL) {
195                 _DEBUG_ERROR("service_account_info is null!!");
196                 *account_info = NULL;
197                 err_code = SYNC_AGENT_ACC_FAIL;
198                 return err_code;
199         }
200
201         *account_info = service_account_info;
202
203         _EXTERN_FUNC_EXIT;
204
205         return err_code;
206 }
207
208 EXPORT_API sync_agent_acc_error_e sync_agent_get_service_account(int fw_account_id, int service_type, sync_agent_service_account_info_s ** account_info)
209 {
210         _EXTERN_FUNC_ENTER;
211
212         sync_agent_acc_error_e err_code = SYNC_AGENT_ACC_SUCCESS;
213
214         plugin_get_service_account_info_cb func_get_service_account_info = plugin_get_function_get_service_account_info(plugin_get_account_plugin_id());
215
216         if (func_get_service_account_info == NULL) {
217                 _DEBUG_ERROR("pFunc_Get_Service_AccountInfo is null!!");
218                 err_code = SYNC_AGENT_ACC_FAIL;
219                 return err_code;
220         }
221
222         int result = func_get_service_account_info(fw_account_id, service_type, account_info);
223         if (result == 0) {
224                 _DEBUG_ERROR("failed in get_service_account_info");
225                 err_code = SYNC_AGENT_ACC_FAIL;
226                 return err_code;
227         }
228
229         _EXTERN_FUNC_EXIT;
230
231         return err_code;
232 }
233
234 EXPORT_API void sync_agent_free_service_account_info(sync_agent_service_account_info_s * account_info)
235 {
236         _EXTERN_FUNC_ENTER;
237
238         if (account_info != NULL) {
239                 if (account_info->email != NULL)
240                         free(account_info->email);
241
242                 free(account_info);
243         }
244
245         _EXTERN_FUNC_EXIT;
246 }