3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
23 #include "data-adapter/agent_handler_manager.h"
24 #include "data-adapter/agent_handler_manager_internal.h"
26 #include "account/service.h"
27 #include "account/util_internal.h"
30 #define EXPORT_API __attribute__ ((visibility("default")))
33 #ifndef SYNC_AGENT_LOG
35 #define LOG_TAG "AF_ACCOUNT"
38 EXPORT_API sync_agent_acc_error_e sync_agent_construct_account_tbl_from_service()
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);
48 if (service_plugin_id_list == NULL) {
49 _DEBUG_INFO("No Data PlugIn loaded!!");
50 err_code = SYNC_AGENT_ACC_SUCCESS;
55 for (; index < content_count; index++) {
56 int service = service_plugin_id_list[index];
57 _DEBUG_INFO("Service ID : %d", service);
59 // Get account id List from agent account table
60 fw_account_id_list = da_get_account_id_list(&fw_account_id_cnt);
62 _DEBUG_INFO("fw_account_id_cnt : %d", fw_account_id_cnt);
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);
69 if (fw_account_id_list != NULL) {
70 free(fw_account_id_list);
71 fw_account_id_list = NULL;
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;
84 _DEBUG_INFO("[%d] service_account_id_cnt : %d", service, service_account_id_cnt);
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;
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]);
103 for (; fw_account_id_list != 0 && i < fw_account_id_cnt; i++) {
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) {
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);
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;
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;
134 for (; i < service_account_id_cnt; i++) {
135 if (service_account_id_new[i] == 1) {
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;
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;
154 account.password = "0";
155 account.access_name = "Account";
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]);
161 err_code = SYNC_AGENT_ACC_CHANGED;
165 if (fw_account_id_list != NULL) {
166 free(fw_account_id_list);
167 fw_account_id_list = NULL;
174 if (service_plugin_id_list != NULL) {
175 free(service_plugin_id_list);
176 service_plugin_id_list = NULL;
178 if (fw_account_id_list != NULL) {
179 free(fw_account_id_list);
180 fw_account_id_list = NULL;
183 _DEBUG_INFO("err_code = %d", err_code);
187 EXPORT_API sync_agent_acc_error_e sync_agent_create_service_account_info(sync_agent_service_account_info_s ** account_info)
191 sync_agent_acc_error_e err_code = SYNC_AGENT_ACC_SUCCESS;
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;
201 *account_info = service_account_info;
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)
212 sync_agent_acc_error_e err_code = SYNC_AGENT_ACC_SUCCESS;
214 plugin_get_service_account_info_cb func_get_service_account_info = plugin_get_function_get_service_account_info(plugin_get_account_plugin_id());
216 if (func_get_service_account_info == NULL) {
217 _DEBUG_ERROR("pFunc_Get_Service_AccountInfo is null!!");
218 err_code = SYNC_AGENT_ACC_FAIL;
222 int result = func_get_service_account_info(fw_account_id, service_type, account_info);
224 _DEBUG_ERROR("failed in get_service_account_info");
225 err_code = SYNC_AGENT_ACC_FAIL;
234 EXPORT_API void sync_agent_free_service_account_info(sync_agent_service_account_info_s * account_info)
238 if (account_info != NULL) {
239 if (account_info->email != NULL)
240 free(account_info->email);