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 <glib/gprintf.h>
22 #include "utility/sync_util.h"
23 #include "data-adapter/agent_handler_manager.h"
24 #include "data-adapter/agent_handler_manager_internal.h"
25 #include "data-adapter/agent.h"
27 #include "data-adapter/interface_item.h"
28 #include "data-adapter/changelog.h"
31 #define EXPORT_API __attribute__ ((visibility("default")))
34 #ifndef SYNC_AGENT_LOG
36 #define LOG_TAG "AF_DACI"
39 EXPORT_API sync_agent_da_return_e sync_agent_create_item(sync_agent_da_item_s ** sync_agent_item)
43 *sync_agent_item = (sync_agent_da_item_s *) calloc(1, sizeof(sync_agent_da_item_s));
44 if (*sync_agent_item == NULL) {
45 _DEBUG_ERROR("calloc failed !!");
46 return SYNC_AGENT_DA_ERRORS;
50 return SYNC_AGENT_DA_SUCCESS;
53 EXPORT_API sync_agent_da_return_e sync_agent_add_item(sync_agent_da_item_s * sync_agent_item, char **item_id, int use_changelog)
56 retvm_if(sync_agent_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_item_s is NULL !!");
58 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
59 if (use_changelog == 1) {
60 ret = sync_agent_changelog_add_item_new(sync_agent_item, item_id);
61 if (ret != SYNC_AGENT_DA_SUCCESS) {
62 _DEBUG_ERROR("sync_agent_changelog_add_item_new() failed !!");
65 ret = sync_agent_add_item_new(sync_agent_item, 1, item_id);
66 if (ret != SYNC_AGENT_DA_SUCCESS) {
67 _DEBUG_ERROR("sync_agent_add_item_new() failed !!");
75 EXPORT_API sync_agent_da_return_e sync_agent_add_bulk_item(sync_agent_da_item_s * sync_agent_item, char **item_id, int use_changelog)
79 retvm_if(sync_agent_item == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_item_s is NULL !!");
81 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
83 if (use_changelog == 1) {
84 ret = sync_agent_changelog_add_item_new(sync_agent_item, item_id);
85 if (ret != SYNC_AGENT_DA_SUCCESS) {
86 _DEBUG_ERROR("sync_agent_changelog_add_item_new() failed !!");
89 ret = sync_agent_add_item_new(sync_agent_item, 1, item_id);
90 if (ret != SYNC_AGENT_DA_SUCCESS) {
91 _DEBUG_ERROR("sync_agent_add_item_new() failed !!");
99 EXPORT_API sync_agent_da_return_e sync_agent_update_item(char *item_id, int use_changelog)
103 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
105 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
106 sync_agent_da_item_s *get_item = NULL;
108 if (use_changelog == 1) {
109 ret = sync_agent_create_item(&get_item);
110 if (ret != SYNC_AGENT_DA_SUCCESS) {
111 _DEBUG_ERROR("sync_agent_create_item() failed !!");
115 ret = sync_agent_get_item(item_id, &get_item);
116 if (ret != SYNC_AGENT_DA_SUCCESS) {
117 _DEBUG_ERROR("sync_agent_get_item() failed !!");
118 int ret_temp = sync_agent_free_item(get_item);
119 if (ret_temp != SYNC_AGENT_DA_SUCCESS) {
120 _DEBUG_ERROR("sync_agent_free_item() failed !!");
125 ret = sync_agent_changelog_update_item_new(get_item->account_id, item_id);
126 if (ret != SYNC_AGENT_DA_SUCCESS) {
127 _DEBUG_ERROR("sync_agent_changelog_update_item_new() failed !!");
130 ret = sync_agent_free_item(get_item);
131 if (ret != SYNC_AGENT_DA_SUCCESS) {
132 _DEBUG_ERROR("sync_agent_free_item() failed !!");
135 ret = SYNC_AGENT_DA_SUCCESS;
143 EXPORT_API sync_agent_da_return_e sync_agent_delete_item(char *item_id, int use_changelog)
147 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
149 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
150 sync_agent_da_item_s *get_item = NULL;
152 if (use_changelog == 1) {
153 ret = sync_agent_create_item(&get_item);
154 if (ret != SYNC_AGENT_DA_SUCCESS) {
155 _DEBUG_ERROR("sync_agent_create_item() failed, ret[%d]", ret);
159 ret = sync_agent_get_item_account_id(item_id, &get_item);
160 if (ret != SYNC_AGENT_DA_SUCCESS) {
161 _DEBUG_ERROR("sync_agent_get_item_account_id() failed, ret[%d]", ret);
162 int ret_temp = sync_agent_free_item(get_item);
163 if (ret_temp != SYNC_AGENT_DA_SUCCESS) {
164 _DEBUG_ERROR("sync_agent_get_item_account_id() failed, ret[%d]", ret);
169 ret = sync_agent_changelog_delete_item_new(get_item->account_id, item_id);
170 if (ret != SYNC_AGENT_DA_SUCCESS) {
171 _DEBUG_ERROR("da_changelog_delete_item_internal() failed, ret[%d]", ret);
174 ret = sync_agent_free_item(get_item);
175 if (ret != SYNC_AGENT_DA_SUCCESS) {
176 _DEBUG_ERROR("sync_agent_free_item() failed, ret[%d]", ret);
179 ret = da_delete_item_by_item_id_internal(item_id);
180 if (ret != SYNC_AGENT_DA_SUCCESS) {
181 _DEBUG_ERROR("da_delete_item_by_item_id_internal() failed, ret[%d]", ret);
190 EXPORT_API sync_agent_da_return_e sync_agent_query_delete_item(sync_agent_da_delete_item_query_s * query)
194 retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_delete_item_query_s is NULL !!");
196 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
198 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
199 if (daci_handler == NULL) {
200 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
201 return SYNC_AGENT_DA_ERR_DB_HANDLER_MGR;
204 switch (query->option) {
205 case SYNC_AGENT_DA_DELETE_ITEM_OPTION_ITEM_TYPE_ID:
206 ret = da_delete_item_by_item_type_id_new(daci_handler, query->account_id, query->item_type_id);
207 if (ret != SYNC_AGENT_DA_SUCCESS) {
208 _DEBUG_ERROR("da_delete_item_by_item_type_id() failed !!");
220 EXPORT_API sync_agent_da_return_e sync_agent_get_item(char *item_id, sync_agent_da_item_s ** sync_agent_item)
224 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
226 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
228 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
229 if (daci_handler == NULL) {
230 _DEBUG_ERROR("[%s] no handler for key");
234 ret = da_get_item_new(daci_handler, item_id, sync_agent_item);
235 if (ret != SYNC_AGENT_DA_SUCCESS) {
236 _DEBUG_ERROR("da_get_item() faied !!");
244 EXPORT_API sync_agent_da_return_e sync_agent_get_item_data_store_id(char *item_id, sync_agent_da_item_s ** sync_agent_item)
248 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
250 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
252 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
253 if (daci_handler == NULL) {
254 _DEBUG_ERROR("[%s] no handler for key");
258 ret = da_get_item_data_store_id_new(daci_handler, item_id, sync_agent_item);
259 if (ret != SYNC_AGENT_DA_SUCCESS) {
260 _DEBUG_ERROR("da_get_item_data_store_id_new() faied, ret[%d]", ret);
267 EXPORT_API sync_agent_da_return_e sync_agent_get_item_account_id(char *item_id, sync_agent_da_item_s ** sync_agent_item)
271 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
273 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
275 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
276 if (daci_handler == NULL) {
277 _DEBUG_ERROR("[%s] no handler for key");
281 ret = da_get_item_account_id_new(daci_handler, item_id, sync_agent_item);
282 if (ret != SYNC_AGENT_DA_SUCCESS) {
283 _DEBUG_ERROR("da_get_item_account_id_new() faied, ret[%d]", ret);
290 EXPORT_API sync_agent_da_return_e sync_agent_get_item_list(int account_id, sync_agent_da_get_item_list_query_s * query, GList ** list)
294 retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_get_item_list_query_s is NULL !!");
296 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
298 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
299 if (daci_handler == NULL) {
300 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
304 switch (query->option) {
305 case SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_ACCOUNT_ID:
306 ret = da_get_item_by_account_id_new(daci_handler, account_id, query->item_type_id, list);
307 if (ret != SYNC_AGENT_DA_SUCCESS) {
308 _DEBUG_ERROR("da_get_item_by_account_id() failed !!");
311 case SYNC_AGENT_DA_GET_ITEM_LIST_OPTION_SERVICE_ID_MAPPING:
312 retvm_if(query->item_id == NULL, SYNC_AGENT_DA_ERRORS, "item id is NULL !!");
314 ret = da_get_item_mapping_service_id_new(daci_handler, account_id, query->item_id, list);
315 if (ret != SYNC_AGENT_DA_SUCCESS) {
316 _DEBUG_ERROR("da_get_item_mapping_service_id() failed !!");
328 EXPORT_API sync_agent_da_return_e sync_agent_get_item_id(sync_agent_da_get_item_id_query_s * query, char **item_id)
332 retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_get_item_id_query_s is NULL !!");
333 retvm_if(query->service_id == NULL, SYNC_AGENT_DA_ERRORS, "service id is NULL !!");
335 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
337 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
338 if (daci_handler == NULL) {
339 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
343 ret = da_get_item_item_id_new(daci_handler, query->account_id, query->service_id, query->item_type_id, item_id);
344 if (ret != SYNC_AGENT_DA_SUCCESS) {
345 _DEBUG_ERROR("da_get_item_item_id_new() failed !!");
353 EXPORT_API sync_agent_da_return_e sync_agent_get_item_id_list(sync_agent_da_get_item_id_list_query_s * query, GList ** list)
357 retvm_if(query == NULL, SYNC_AGENT_DA_ERRORS, "sync_agent_da_get_item_id_list_query_s is NULL !!");
359 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
361 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
362 if (daci_handler == NULL) {
363 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
367 switch (query->option) {
368 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_COLUMN_NAME:
369 retvm_if(query->folder_id == NULL, SYNC_AGENT_DA_ERRORS, "folder id is NULL !!");
371 ret = da_get_item_id_list_new(daci_handler, query->folder_id, query->column_name, list);
372 if (ret != SYNC_AGENT_DA_SUCCESS) {
373 _DEBUG_ERROR("da_get_item_id_list_new() failed !!");
376 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID:
377 ret = da_get_item_id_list_by_operation_id_new(daci_handler, query->account_id, query->item_type_id, query->operation_id, list);
378 if (ret != SYNC_AGENT_DA_SUCCESS) {
379 _DEBUG_ERROR("da_get_item_id_list_by_operation_id_new() failed !!");
382 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID_N_FOLDER_ID:
383 retvm_if(query->folder_id == NULL, SYNC_AGENT_DA_ERRORS, "folder id is NULL !!");
385 ret = da_get_item_id_list_by_operation_id_and_folder_id_new(daci_handler, query->account_id, query->folder_id, query->item_type_id, query->operation_id, list);
386 if (ret != SYNC_AGENT_DA_SUCCESS) {
387 _DEBUG_ERROR("da_get_item_id_list_by_operation_id_and_folder_id_new() failed !!");
390 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_OPERATION_ID_N_ITEM_TYPE_ID:
391 ret = da_get_item_info_by_item_type_id_and_operation_id_new(daci_handler, query->account_id, query->item_type_id, query->operation_id, list);
392 if (ret != SYNC_AGENT_DA_SUCCESS) {
393 _DEBUG_ERROR("da_get_item_info_by_item_type_id_and_operation_id_new() failed !!");
396 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_ITEM_TYPE_ID:
397 ret = da_get_item_id_list_by_datastore_id_new(daci_handler, query->item_type_id, list);
398 if (ret != SYNC_AGENT_DA_SUCCESS) {
399 _DEBUG_ERROR("da_get_item_id_list_by_datastore_id_new() failed !!");
402 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_ITEM_TYPE_ID_N_ACCOUNT_ID:
403 ret = da_get_item_id_list_by_datastore_id_with_account_id_new(daci_handler, query->account_id, query->item_type_id, list);
404 if (ret != SYNC_AGENT_DA_SUCCESS) {
405 _DEBUG_ERROR("da_get_item_id_list_by_datastore_id_with_account_id_new() failed !!");
408 case SYNC_AGENT_DA_GET_ITEM_ID_LIST_OPTION_FOLDER_ID_N_ACCOUNT_ID:
409 retvm_if(query->folder_id == NULL, SYNC_AGENT_DA_ERRORS, "folder id is NULL !!");
411 ret = da_get_item_id_list_by_folder_id_and_account_id(daci_handler, query->account_id, query->folder_id, list);
412 if (ret != SYNC_AGENT_DA_SUCCESS) {
413 _DEBUG_ERROR("da_get_item_id_list_by_folder_id_and_account_id() failed !!");
425 EXPORT_API sync_agent_da_return_e sync_agent_free_item(sync_agent_da_item_s * sync_agent_item)
429 if (sync_agent_item != NULL) {
430 if (sync_agent_item->item_id != NULL)
431 free(sync_agent_item->item_id);
432 sync_agent_item->item_id = NULL;
434 if (sync_agent_item->folder_id != NULL)
435 free(sync_agent_item->folder_id);
436 sync_agent_item->folder_id = NULL;
438 if (sync_agent_item->service_id != NULL)
439 free(sync_agent_item->service_id);
440 sync_agent_item->service_id = NULL;
442 if (sync_agent_item->parent_service_id != NULL)
443 free(sync_agent_item->parent_service_id);
444 sync_agent_item->parent_service_id = NULL;
446 if (sync_agent_item->access_name != NULL)
447 free(sync_agent_item->access_name);
448 sync_agent_item->access_name = NULL;
450 free(sync_agent_item);
451 sync_agent_item = NULL;
453 _DEBUG_ERROR("sync_agent_item is NULL !!");
454 return SYNC_AGENT_DA_ERRORS;
457 return SYNC_AGENT_DA_SUCCESS;
460 EXPORT_API sync_agent_da_return_e sync_agent_free_item_list(GList * list)
464 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
468 sync_agent_da_item_s *iter_data;
470 for (iter = list; iter != NULL;) {
472 iter_data = (sync_agent_da_item_s *) (iter->data);
474 iter = g_list_next(iter);
475 list = g_list_remove(list, iter_data);
477 ret = sync_agent_free_item(iter_data);
478 if (ret != SYNC_AGENT_DA_SUCCESS) {
479 _DEBUG_ERROR("sync_agent_free_item() failed !!");
486 _DEBUG_ERROR("list is NULL !!");
487 return SYNC_AGENT_DA_ERRORS;
493 EXPORT_API sync_agent_da_return_e sync_agent_free_item_id(char *item_id)
497 if (item_id != NULL) {
501 _DEBUG_ERROR("item id is NULL !!");
502 return SYNC_AGENT_DA_ERRORS;;
506 return SYNC_AGENT_DA_SUCCESS;
509 EXPORT_API sync_agent_da_return_e sync_agent_free_item_id_list(GList * list)
513 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
519 for (iter = list; iter != NULL;) {
521 iter_data = (char *)(iter->data);
523 iter = g_list_next(iter);
524 list = g_list_remove(list, iter_data);
526 ret = sync_agent_free_item_id(iter_data);
527 if (ret != SYNC_AGENT_DA_SUCCESS) {
528 _DEBUG_ERROR("sync_agent_free_item_id() failed !!");
534 _DEBUG_INFO("list is NULL !!");
541 EXPORT_API sync_agent_da_return_e sync_agent_get_item_count(int data_store_id, int *item_count)
545 retvm_if(item_count == NULL, SYNC_AGENT_DA_ERRORS, "*item_count is NULL !!");
547 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
549 GList *account_info_list = NULL;
551 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
552 if (daci_handler == NULL) {
553 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
554 ret = SYNC_AGENT_DA_ERRORS;
558 sync_agent_fw_account_query_s account_query;
559 account_query.query = ACCOUNT_QUERY_BY_SERVICE_TYPE;
560 account_query.service_type = data_store_id;
561 sync_agent_acc_error_e acc_err = sync_agent_query_fw_account(&account_query, &account_info_list);
562 if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
563 _DEBUG_ERROR("sync_agent_query_fw_account is failed");
564 ret = SYNC_AGENT_DA_ERRORS;
568 GList *account_iter = NULL;
569 for (account_iter = account_info_list; account_iter != NULL; account_iter = g_list_next(account_iter)) {
570 sync_agent_fw_account_s *fw_account = (sync_agent_fw_account_s *)account_iter->data;
571 int fw_account_id = fw_account->account_id;
574 ret = da_get_item_count_by_datastore_id(daci_handler, data_store_id, fw_account_id, &temp_count);
575 if (ret != SYNC_AGENT_DA_SUCCESS) {
576 _DEBUG_ERROR("da_get_item_count_by_datastore_id() failed !!");
578 total_count += temp_count;
582 *item_count = total_count;
583 _DEBUG_INFO("item_count = %d", *item_count);
585 if(account_info_list != NULL) {
586 sync_agent_free_fw_account_list(account_info_list);
587 account_info_list = NULL;
593 EXPORT_API sync_agent_da_return_e sync_agent_get_updated_exdate_item_id_list(char *fw_id, GList ** list)
596 retvm_if(fw_id == NULL, SYNC_AGENT_DA_ERRORS, "fw_id is NULL !!");
597 sync_agent_da_return_e ret = SYNC_AGENT_DA_ERRORS;
599 SYNC_AGENT_DA_HANDLER *daci_handler = da_get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
600 goto_if(daci_handler == NULL, "daci_handler is null");
602 ret = da_get_updated_exdate_item_id_list(daci_handler, fw_id, list);
603 goto_if(ret != SYNC_AGENT_DA_SUCCESS, "da_get_updated_exdate_item_id_list() failed, ret = [%d]", ret);