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.
20 #include "initialization/parser.h"
22 #include "utility/fw_alloc.h"
23 #include "utility/sync_util.h"
24 #include "utility/fw_file.h"
26 #include "plugin/data_connector_plugin.h"
27 #include "plugin/account_plugin.h"
29 #include "account/manager.h"
30 #include "account/util_internal.h"
32 #include "data-adapter/changelog.h"
33 #include "data-adapter/luid.h"
35 #include "data-adapter/agent_handler_manager.h"
36 #include "data-adapter/agent_handler_manager_internal.h"
38 #include "data-adapter/agent_manager.h"
39 #include "data-adapter/agent_manager_internal.h"
41 #include "data-adapter/interface_service_item.h"
44 #define EXPORT_API __attribute__ ((visibility("default")))
47 #ifndef SYNC_AGENT_LOG
49 #define LOG_TAG "AF_DACI"
53 * Key : db/Apps/AgentFramework/[Agent Name]/ServiceChangePoint/[FW AccountID]/[Service_Type]
54 * Value : Change Point (TimeStamp)
56 #define VCONF_KEY_SERIVCE_CHANGEPOINT ".%s_service_change_point_%d_%d"
58 static void _set_serivce_change_point(int fw_account_id, int service_type, int change_point);
60 static int _get_serivce_change_point(int fw_account_id, int service_type);
62 static sync_agent_da_return_e _construct_change_log_tbl_from_service(int fw_account_id, int service_type);
64 EXPORT_API sync_agent_da_return_e sync_agent_construct_item_tbl_from_service(int fw_account_id, int service_type)
67 sync_agent_da_return_e err_code = SYNC_AGENT_DA_SUCCESS;
68 err_code = _construct_change_log_tbl_from_service(fw_account_id, service_type);
73 EXPORT_API sync_agent_da_return_e sync_agent_construct_folder_tbl_from_service(int service_type)
77 int fw_account_cnt = 0;
78 int *fw_account_id_list = da_get_account_id_list(&fw_account_cnt);
80 if (fw_account_id_list == NULL || fw_account_cnt == 0) {
81 if (fw_account_id_list != NULL) {
82 free(fw_account_id_list);
83 fw_account_id_list = NULL;
85 return SYNC_AGENT_DA_SUCCESS;
88 int *service_account_id_list = 0;
89 if (fw_account_cnt != 0) {
90 service_account_id_list = (int *)calloc(fw_account_cnt, sizeof(int));
91 if (service_account_id_list == NULL) {
92 if (fw_account_id_list != NULL) {
93 free(fw_account_id_list);
94 fw_account_id_list = NULL;
96 return SYNC_AGENT_DA_ERR_MEMORY_FULL;
100 for (; i < fw_account_cnt; i++) {
101 service_account_id_list[i] = acc_get_service_account_id(service_type, fw_account_id_list[i]);
102 _DEBUG_INFO("service_account_id_list[%d] : %d", i, service_account_id_list[i]);
106 int service_account_id_cnt = fw_account_cnt;
107 _DEBUG_INFO("service_account_id_cnt : %d", service_account_id_cnt);
110 for (; i < service_account_id_cnt; i++) {
111 int fw_account_id = acc_get_fw_account_id(service_type, service_account_id_list[i], 0);
112 _DEBUG_INFO("fw_account_id[%d] : %d", i, fw_account_id);
114 plugin_get_folder_id_list_cb func_get_folder_id_list = plugin_get_function_get_folder_id_list(service_type);
115 if (func_get_folder_id_list == NULL) {
116 _DEBUG_ERROR("func_get_folder_id_list is NULL !!");
117 if (fw_account_id_list != NULL) {
118 free(fw_account_id_list);
120 // if (service_account_id_list != NULL) {
121 free(service_account_id_list);
123 return SYNC_AGENT_DA_NOT_FOUND_PLUG_IN;
126 int service_folder_cnt = 0;
127 int *service_folder_type_list = 0;
128 char **service_folder_list = func_get_folder_id_list(service_account_id_list[i], &service_folder_cnt, &service_folder_type_list);
129 if (service_folder_list == NULL) {
130 _DEBUG_ERROR("service folder list is NULL !!");
131 if (fw_account_id_list != NULL) {
132 free(fw_account_id_list);
133 fw_account_id_list = NULL;
135 // if (service_account_id_list != NULL) {
136 free(service_account_id_list);
138 return SYNC_AGENT_DA_ERRORS;
141 _DEBUG_INFO("service_folder_cnt : %d", service_folder_cnt);
144 //Hard Coding - If account has one folder, consider folder' state complete.
146 t_sync_agent_da_id_list_s_t *foder_id_list_info = T_DACI_Get_Folder_Folder_Id_List_By_Account_Id(fw_account_id);
148 if (foder_id_list_info != NULL) {
149 if (foder_id_list_info->count > 0) {
150 _DEBUG_INFO("foder_id_list_info->count : %d", foder_id_list_info->count);
151 return SYNC_AGENT_DA_SUCCESS;
156 for (; i < service_folder_cnt; i++) {
158 /* checking whether the service_id exist in Folder table */
159 char *temp_folder_id = da_get_folder_id_by_service_id_wrapper(fw_account_id, service_type, service_folder_list[i], service_folder_type_list[i]);
161 _DEBUG_INFO("fw_account_id : %d", fw_account_id);
162 _DEBUG_INFO("service_folder_list[i] : %s", service_folder_list[i]);
163 _DEBUG_INFO("service_folder_type_list[i] : %d", service_folder_type_list[i]);
164 _DEBUG_INFO("temp_folder_id : %s", temp_folder_id);
165 if (temp_folder_id != NULL) {
166 free(temp_folder_id);
168 /* Add Agent Folder table */
169 sync_agent_da_folder_s folder;
170 folder.account_id = fw_account_id;
171 folder.folder_id = sync_agent_generate_folder_luid();
172 folder.parent_folder_id = "0";
173 folder.data_store_id = service_type;
174 folder.folder_type_id = service_folder_type_list[i];
175 folder.service_id = service_folder_list[i];
176 folder.access_name = "DACI";
178 da_add_folder_internal(&folder, 1);
183 if (fw_account_id_list != NULL) {
184 free(fw_account_id_list);
185 fw_account_id_list = NULL;
187 if (service_account_id_list != NULL) {
188 free(service_account_id_list);
189 service_account_id_list = NULL;
194 return SYNC_AGENT_DA_SUCCESS;
197 EXPORT_API sync_agent_da_return_e sync_agent_refresh_item_tbl_from_service(int fw_account_id, int service_type)
202 * todo Delete folder table
206 * Delete Changelog table
208 da_delete_item_changelog_by_item_type_id_internal(fw_account_id, service_type);
213 da_delete_item_by_item_type_id_internal(fw_account_id, service_type);
216 * todo Insert folder table
222 plugin_get_changed_item_for_folder_add_cb func_get_changed_item_for_folder_add = plugin_get_function_get_changed_item_for_folder_add(service_type);
223 plugin_get_changed_item_for_folder_update_cb func_get_changed_item_for_folder_update = plugin_get_function_get_changed_item_for_folder_update(service_type);
225 int service_account_id = acc_get_service_account_id(service_type, fw_account_id);
226 _DEBUG_INFO("service_account_id : %d", service_account_id);
228 sync_agent_da_id_list_s *fw_folder_id_list_info = da_get_folder_id_list_by_item_type_id_internal(fw_account_id, service_type);
230 retvm_if(fw_folder_id_list_info == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_id_list_s is NULL !!");
232 char **fw_folder_id_list = 0;
233 int fw_folder_id_cnt = 0;
234 if (fw_folder_id_list_info != NULL) {
235 fw_folder_id_list = fw_folder_id_list_info->id;
236 fw_folder_id_cnt = fw_folder_id_list_info->count;
238 _DEBUG_INFO("fw_folder_id_cnt : %d", fw_folder_id_cnt);
239 _DEBUG_INFO("fw_folder_id_list[0] : %s", fw_folder_id_list_info->id[0]);
240 _DEBUG_INFO("fw_folder_id_list[0] : %s", fw_folder_id_list[0]);
244 for (; i < fw_folder_id_cnt; i++) {
245 _DEBUG_INFO("fw_folder_id_list[%d] : %s", i, fw_folder_id_list[i]);
247 char *service_folder_id = sync_agent_get_service_folder_id(fw_folder_id_list[i]);
249 _DEBUG_INFO("service_folder_id : %s", service_folder_id);
251 int added_item_cnt = 0;
252 sync_agent_plugin_item_node_s *added_service_item_id_list = func_get_changed_item_for_folder_add(service_account_id, service_folder_id, 0, &added_item_cnt);
253 sync_agent_plugin_item_node_s *cursor_ptr = added_service_item_id_list;
255 _DEBUG_INFO("added_item_cnt : %d", added_item_cnt);
260 sync_agent_begin_transaction();
261 char **fw_item_id_list = sync_agent_generate_item_luid(1, added_item_cnt);
262 if (fw_item_id_list != NULL) {
264 for (; y < added_item_cnt; y++) {
265 sync_agent_da_item_s item;
266 item.account_id = fw_account_id;
267 item.item_id = fw_item_id_list[y];
268 item.folder_id = fw_folder_id_list[i];
269 item.data_store_id = service_type;
270 item.service_id = cursor_ptr->item_id;
271 item.parent_service_id = cursor_ptr->parent_item_id;
272 item.access_name = "DACI";
274 sync_agent_da_return_e ret = da_add_item_internal(&item, 1);
275 if (ret != SYNC_AGENT_DA_SUCCESS) {
276 _DEBUG_ERROR("add item failed return : %d", ret);
279 sync_agent_plugin_item_node_s *temp = cursor_ptr;
280 cursor_ptr = cursor_ptr->next;
282 free(temp->parent_item_id);
285 if (fw_item_id_list[y] != NULL) {
286 free(fw_item_id_list[y]);
289 free(fw_item_id_list);
291 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
294 * Process Update Item
296 int updated_item_cnt = 0;
297 sync_agent_plugin_item_node_s *updated_service_item_id_list = func_get_changed_item_for_folder_update(service_account_id, service_folder_id, 0, &updated_item_cnt);
298 cursor_ptr = updated_service_item_id_list;
300 _DEBUG_INFO("updated_item_cnt : %d", updated_item_cnt);
302 sync_agent_begin_transaction();
303 fw_item_id_list = sync_agent_generate_item_luid(1, updated_item_cnt);
304 if (fw_item_id_list != NULL) {
306 for (; y < updated_item_cnt; y++) {
307 sync_agent_da_item_s item;
308 item.account_id = fw_account_id;
309 item.item_id = fw_item_id_list[y];
310 item.folder_id = fw_folder_id_list[i];
311 item.data_store_id = service_type;
312 item.service_id = cursor_ptr->item_id;
313 item.parent_service_id = cursor_ptr->parent_item_id;
314 item.access_name = "DACI";
316 sync_agent_da_return_e ret = da_add_item_internal(&item, 1);
317 if (ret != SYNC_AGENT_DA_SUCCESS) {
318 _DEBUG_ERROR("add item failed return : %d", ret);
321 sync_agent_plugin_item_node_s *temp = cursor_ptr;
322 cursor_ptr = cursor_ptr->next;
324 free(temp->parent_item_id);
327 if (fw_item_id_list[y] != NULL) {
328 free(fw_item_id_list[y]);
331 free(fw_item_id_list);
333 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
335 if (fw_folder_id_list[i] != NULL) {
336 free(fw_folder_id_list[i]);
339 if (service_folder_id != NULL) {
340 free(service_folder_id);
344 if (fw_folder_id_list_info != NULL) {
345 if (fw_folder_id_list_info->id != NULL) {
346 free(fw_folder_id_list_info->id);
348 free(fw_folder_id_list_info);
353 return SYNC_AGENT_DA_SUCCESS;
356 sync_agent_da_return_e da_refresh_changelog_tbl_from_service(int fw_account_id, int service_type)
360 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
363 * Delete Changelog table
365 ret = da_delete_item_changelog_by_item_type_id_internal(fw_account_id, service_type);
370 ret = da_delete_item_by_item_type_id_internal(fw_account_id, service_type);
373 * Construct Item & changelog table
375 plugin_get_changed_item_for_folder_add_cb func_get_changed_item_for_folder_add = plugin_get_function_get_changed_item_for_folder_add(service_type);
377 int service_account_id = acc_get_service_account_id(service_type, fw_account_id);
378 _DEBUG_INFO("service_account_id : %d", service_account_id);
380 sync_agent_da_id_list_s *fw_folder_id_list_info = da_get_folder_id_list_by_item_type_id_internal(fw_account_id, service_type);
382 retvm_if(fw_folder_id_list_info == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_id_list_s is NULL !!");
384 char **fw_folder_id_list = 0;
385 int fw_folder_id_cnt = 0;
386 if (fw_folder_id_list_info != NULL) {
387 fw_folder_id_list = fw_folder_id_list_info->id;
388 fw_folder_id_cnt = fw_folder_id_list_info->count;
390 _DEBUG_INFO("[RJW] fw_folder_id_cnt : %d", fw_folder_id_cnt);
391 _DEBUG_INFO("[RJW] fw_folder_id_list[0] : %s", fw_folder_id_list_info->id[0]);
392 _DEBUG_INFO("[RJW] fw_folder_id_list[0] : %s", fw_folder_id_list[0]);
396 for (; i < fw_folder_id_cnt; i++) {
397 _DEBUG_INFO("fw_folder_id_list[%d] : %s", i, fw_folder_id_list[i]);
399 char *service_folder_id = sync_agent_get_service_folder_id(fw_folder_id_list[i]);
401 _DEBUG_INFO("service_folder_id : %s", service_folder_id);
404 sync_agent_plugin_item_node_s *service_item_id_list = func_get_changed_item_for_folder_add(service_account_id, service_folder_id, 0, &item_cnt);
405 sync_agent_plugin_item_node_s *cursor_ptr = service_item_id_list;
407 _DEBUG_INFO("item_cnt : %d", item_cnt);
409 sync_agent_begin_transaction();
410 char **fw_item_id_list = sync_agent_generate_item_luid(1, item_cnt);
411 if (fw_item_id_list != NULL) {
413 for (; k < item_cnt; k++) {
414 sync_agent_da_item_s item;
415 char *service_item_id = cursor_ptr->item_id;
416 _DEBUG_INFO("service_item_id[%d] : %s", k, service_item_id);
417 _DEBUG_INFO("fw_item_id : %s", fw_item_id_list[k]);
419 item.item_id = fw_item_id_list[k];
420 item.data_store_id = service_type;
421 item.account_id = fw_account_id;
422 item.folder_id = fw_folder_id_list[i];
423 item.service_id = cursor_ptr->item_id;
424 item.parent_service_id = cursor_ptr->parent_item_id;
425 da_changelog_add_item_internal(&item);
427 sync_agent_plugin_item_node_s *temp = cursor_ptr;
428 cursor_ptr = cursor_ptr->next;
431 if (fw_item_id_list[k] != NULL) {
432 free(fw_item_id_list[k]);
435 free(fw_item_id_list);
437 sync_agent_end_transaction(SYNC_AGENT_DA_TRANSACTION_COMMIT);
440 if (fw_folder_id_list_info != NULL) {
441 if (fw_folder_id_list_info->id != NULL) {
442 free(fw_folder_id_list_info->id);
444 free(fw_folder_id_list_info);
452 EXPORT_API sync_agent_da_return_e sync_agent_set_service_change_point(int fw_account_id, int service_type)
456 plugin_get_last_change_point_cb func_get_last_changepoint = plugin_get_function_get_last_change_point(service_type);
458 int new_last_change_point = func_get_last_changepoint();
460 _DEBUG_INFO("new_last_change_point : %d", new_last_change_point);
462 _set_serivce_change_point(fw_account_id, service_type, new_last_change_point);
466 return SYNC_AGENT_DA_SUCCESS;
469 /********************************************* static function ****************************/
471 static void _set_serivce_change_point(int fw_account_id, int service_type, int change_point)
475 const char *agent_key = init_get_agent_key();
478 snprintf(vconf_key, sizeof(vconf_key), VCONF_KEY_SERIVCE_CHANGEPOINT, agent_key, fw_account_id, service_type);
480 _DEBUG_TRACE("vConfKey : %s", vconf_key);
482 if (!sync_agent_set_int_into_file(vconf_key, change_point)) {
483 _DEBUG_ERROR("vconf_set_id FAIL");
485 _DEBUG_TRACE("Sucess Vconf Set Id");
491 static int _get_serivce_change_point(int fw_account_id, int service_type)
495 const char *agent_key = init_get_agent_key();
498 snprintf(vconf_key, sizeof(vconf_key), VCONF_KEY_SERIVCE_CHANGEPOINT, agent_key, fw_account_id, service_type);
500 int change_point = 0;
501 if (!sync_agent_get_int_from_file(vconf_key, &change_point)) {
502 _DEBUG_TRACE("vconf_set_id FAIL - First attempt??");
511 sync_agent_da_return_e _construct_change_log_tbl_from_service(int fw_account_id, int service_type)
515 int service_account_id = acc_get_service_account_id(service_type, fw_account_id);
516 _DEBUG_TRACE("service_account_id : %d", service_account_id);
518 int changed_flag = 0;
519 int fw_change_point = _get_serivce_change_point(fw_account_id, service_type);
520 _DEBUG_TRACE("fw_last_change_point : %d", fw_change_point);
522 plugin_get_changed_item_for_folder_add_cb func_get_changed_item_for_folder_add = plugin_get_function_get_changed_item_for_folder_add(service_type);
523 plugin_get_changed_item_for_folder_delete_cb func_get_changed_item_for_folder_del = plugin_get_function_get_changed_item_for_folder_delete(service_type);
524 plugin_get_changed_item_for_folder_update_cb func_get_changed_item_for_folder_update = plugin_get_function_get_changed_item_for_folder_update(service_type);
527 * todo Get fw_folder_id list in the service
529 sync_agent_da_id_list_s *fw_folder_id_list_info = da_get_folder_id_list_by_item_type_id_internal(fw_account_id, service_type);
531 retvm_if(fw_folder_id_list_info == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_id_list_s is NULL !!");
533 char **fw_folder_id_list = 0;
534 int fw_folder_id_cnt = 0;
535 if (fw_folder_id_list_info != NULL) {
536 fw_folder_id_list = fw_folder_id_list_info->id;
537 fw_folder_id_cnt = fw_folder_id_list_info->count;
539 _DEBUG_TRACE("fw_folder_id_cnt : %d", fw_folder_id_cnt);
540 _DEBUG_TRACE("fw_folder_id_list[0] : %s", fw_folder_id_list_info->id[0]);
541 _DEBUG_TRACE("fw_folder_id_list[0] : %s", fw_folder_id_list[0]);
545 for (; i < fw_folder_id_cnt; i++) {
546 _DEBUG_TRACE("fw_folder_id_list[%d] : %s", i, fw_folder_id_list[i]);
548 char *service_folder_id = sync_agent_get_service_folder_id(fw_folder_id_list[i]);
549 _DEBUG_TRACE("service_folder_id : %s", service_folder_id);
552 * 1. Process Add Item
554 int added_item_cnt = 0;
555 sync_agent_plugin_item_node_s *added_service_item_id_list = func_get_changed_item_for_folder_add(service_account_id, service_folder_id, fw_change_point, &added_item_cnt);
556 sync_agent_plugin_item_node_s *cursor_ptr = added_service_item_id_list;
558 _DEBUG_TRACE("added_item_cnt : %d", added_item_cnt);
559 if (added_item_cnt > 0) {
563 char **fw_item_id_list = sync_agent_generate_item_luid(1, added_item_cnt);
565 if (fw_item_id_list != NULL) {
566 for (; y < added_item_cnt; y++) {
567 sync_agent_da_item_s item;
568 item.item_id = fw_item_id_list[y];
569 item.data_store_id = service_type;
570 item.account_id = fw_account_id;
571 item.folder_id = fw_folder_id_list[i];
572 item.service_id = cursor_ptr->item_id;
573 item.parent_service_id = cursor_ptr->parent_item_id;
574 da_changelog_add_item_internal(&item);
575 sync_agent_plugin_item_node_s *temp = cursor_ptr;
576 cursor_ptr = cursor_ptr->next;
579 if (fw_item_id_list[y] != NULL) {
580 free(fw_item_id_list[y]);
584 free(fw_item_id_list);
588 * 2. Process Delete Item
590 int deleted_item_cnt = 0;
591 sync_agent_plugin_item_node_s *deleted_service_item_id_list = func_get_changed_item_for_folder_del(service_account_id, service_folder_id, fw_change_point, &deleted_item_cnt);
592 cursor_ptr = deleted_service_item_id_list;
594 _DEBUG_TRACE("deleted_item_cnt : %d", deleted_item_cnt);
595 if (deleted_item_cnt > 0) {
600 for (; y < deleted_item_cnt; y++) {
601 char *fw_item_id = da_get_item_id_internal(fw_account_id, cursor_ptr->item_id, service_type);
602 sync_agent_plugin_item_node_s *temp = cursor_ptr;
603 cursor_ptr = cursor_ptr->next;
607 da_changelog_delete_item_internal(fw_account_id, fw_item_id, service_type);
613 * 3. Process Update Item
615 int updated_item_cnt = 0;
616 sync_agent_plugin_item_node_s *updated_service_item_id_list = func_get_changed_item_for_folder_update(service_account_id, service_folder_id, fw_change_point, &updated_item_cnt);
617 cursor_ptr = updated_service_item_id_list;
619 _DEBUG_TRACE("updated_item_cnt : %d", updated_item_cnt);
620 if (updated_item_cnt > 0) {
624 fw_item_id_list = sync_agent_generate_item_luid(1, updated_item_cnt);
625 if (fw_item_id_list != NULL) {
627 for (; y < updated_item_cnt; y++) {
628 char *fw_item_id = da_get_item_id_internal(fw_account_id, cursor_ptr->item_id, service_type);
630 if (fw_item_id == NULL) {
631 sync_agent_da_item_s item;
632 item.item_id = fw_item_id_list[y];
633 item.data_store_id = service_type;
634 item.account_id = fw_account_id;
635 item.folder_id = fw_folder_id_list[i];
636 item.service_id = cursor_ptr->item_id;
637 item.parent_service_id = cursor_ptr->parent_item_id;
638 da_changelog_add_item_internal(&item);
640 da_changelog_update_item_internal(fw_account_id, fw_item_id, service_type);
643 sync_agent_plugin_item_node_s *temp = cursor_ptr;
644 cursor_ptr = cursor_ptr->next;
649 if (fw_item_id_list[y] != NULL) {
650 free(fw_item_id_list[y]);
653 if (fw_item_id != NULL) {
658 free(fw_item_id_list);
661 if (fw_folder_id_list[i] != NULL)
662 free(fw_folder_id_list[i]);
664 if (service_folder_id != NULL)
665 free(service_folder_id);
668 if (fw_folder_id_list_info != NULL) {
669 if (fw_folder_id_list_info->id != NULL) {
670 free(fw_folder_id_list_info->id);
672 free(fw_folder_id_list_info);
677 return SYNC_AGENT_DA_SUCCESS;