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.
26 #include "extern_info.h"
27 #include "item_change_info.h"
30 #include "utility/sync_util.h"
32 #include "plugin/data_connector_interface.h"
35 #define EXPORT_API __attribute__ ((visibility("default")))
38 #ifndef SYNC_AGENT_LOG
40 #define LOG_TAG "PLUGIN_DA_MEMO"
43 #define FW_MEMO 2 /* plugIn-Id */
45 static pthread_mutex_t lockx;
47 static int is_noti_from_me = 0;
49 static int is_storage_changed = 0;
51 static sync_agent_add_item_cb_plugin callback_add_item;
53 static sync_agent_del_item_cb_plugin callback_del_item;
55 static sync_agent_update_item_cb_plugin callback_update_item;
57 static sync_agent_get_account_id_list_cb_plugin callback_get_accountidlist;
59 static sync_agent_del_child_item_cb_plugin callback_delete_child_item;
61 static void _free_item_node(sync_agent_plugin_item_node_s * node);
63 static void _set_is_noti_from_me(int set_flag);
65 static int __get_is_noti_from_me();
67 static void _process_memo_change(void *);
69 static void *__rutine_memo_change(void *);
71 static sync_agent_da_return_e _convert_service_error_to_common_error(int err);
73 static int _get_digit(int new_id);
75 EXPORT_API sync_agent_da_return_e sync_agent_plugin_open_service()
79 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
80 int err = memo_init(0);
82 _DEBUG_INFO("[da_memo_plugIn] in memo Fail!\n");
83 ret = SYNC_AGENT_DA_ERR_OPEN_FAILED;
85 _DEBUG_INFO("[da_memo_plugIn] in memo Success!");
93 EXPORT_API sync_agent_da_return_e sync_agent_plugin_close_service()
97 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
99 /* memo service api does not return value */
107 EXPORT_API sync_agent_da_return_e sync_agent_plugin_begin_transaction()
111 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
113 /* memo service api does not return value */
114 /* real transaction is not for event notification */
116 _set_is_noti_from_me(1);
123 EXPORT_API sync_agent_da_return_e sync_agent_plugin_end_transaction(int is_success)
127 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
129 /* memo service api does not return value */
130 /* real transaction is not for event notification */
131 /* memo service is not supported rollback operation */
133 /* _set_is_noti_from_me(1);*/
135 if (is_storage_changed == 0) {
136 _DEBUG_INFO("memo storaged was not Changed!!");
137 _set_is_noti_from_me(0);
139 is_storage_changed = 0;
146 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_item(int account_id, char *folder_id, void *data, char **item_id)
150 retvm_if(data == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "data is NULL. FAIL !!!");
152 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
154 struct memo_data *item = (struct memo_data *)data;
156 /* configuration item */
158 item->modi_time = time((time_t *) 0);
159 item->font_size = 32;
160 item->font_color = 0xff000000;
163 err = memo_add_data(item);
165 _DEBUG_INFO("[da_memo_plugIn] memo_add_data() Fail!\n");
166 ret = _convert_service_error_to_common_error(err);
169 _DEBUG_INFO("[da_memo_plugIn] memo_add_data() Success!\n");
170 *item_id = g_strdup_printf("%d", err);
171 is_storage_changed = 1;
176 memo_free_data(item);
183 EXPORT_API sync_agent_da_return_e sync_agent_plugin_update_item(int account_id, char *folder_id, char *item_id, void *data)
187 retvm_if(data == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "data is NULL. FAIL !!!");
189 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
191 struct memo_data *item = (struct memo_data *)data;
194 err = memo_mod_data(item);
196 _DEBUG_INFO("[da_memo_plugIn] memo_mod_data() Fail!\n");
197 ret = _convert_service_error_to_common_error(err);
199 _DEBUG_INFO("[da_memo_plugIn] memo_mod_data() Success!\n");
200 is_storage_changed = 1;
205 memo_free_data(item);
212 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_item(int account_id, char *folder_id, char *item_id)
216 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
218 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
222 err = memo_del_data(atoi(item_id));
224 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%s) Fail!\n", item_id);
225 ret = _convert_service_error_to_common_error(err);
227 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%s) Success!\n", item_id);
228 is_storage_changed = 1;
236 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_all_items(int account_id)
240 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
242 struct memo_data_list *item_list = NULL;
243 struct memo_data_list *first_item_list = NULL;
245 /* get all data in memo db */
246 item_list = memo_get_all_data_list();
247 first_item_list = item_list;
249 while (item_list != NULL) {
250 err = memo_del_data(item_list->md.id);
252 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%d) Fail!\n", item_list->md.id);
253 ret = _convert_service_error_to_common_error(err);
256 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%d) Success!\n", item_list->md.id);
257 is_storage_changed = 1;
261 if (item_list->md.comment != NULL) {
262 free(item_list->md.comment);
263 item_list->md.comment = 0;
266 if (item_list->md.doodle_path != NULL) {
267 free(item_list->md.doodle_path);
268 item_list->md.doodle_path = 0;
271 item_list = item_list->next;
275 if (first_item_list != NULL) {
276 memo_free_data_list(first_item_list);
277 _DEBUG_INFO("[da_memo_plugIn] memo_free_data_list \n");
285 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_item(int account_id, char *folder_id, char *item_id, void **data)
289 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
291 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
292 struct memo_data *item = 0;
295 item = memo_get_data(atoi(item_id));
297 _DEBUG_INFO("[da_memo_plugIn] memo_get_data() Fail!\n");
298 ret = SYNC_AGENT_DA_ERRORS;
301 _DEBUG_INFO("[da_memo_plugIn] memo_get_data() Success!");
302 *data = (void *)item;
310 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_folder(int account_id, char *folder_name, int folder_type, char **folder_id)
314 _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
318 return SYNC_AGENT_DA_SUCCESS;
321 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_folder(int account_id, char *folder_id)
325 _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
329 return SYNC_AGENT_DA_SUCCESS;
332 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_folder(int account_id, char *folder_id, char **out_folder_name, int *out_folder_type)
336 _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
340 return SYNC_AGENT_DA_SUCCESS;
343 EXPORT_API sync_agent_da_return_e sync_agent_plugin_execute(int account_ID, const char *execute_key, void *execute_values, void **result)
347 _DEBUG_INFO("[da_memo_plugIn] memo service is not supported execute. \n");
352 return SYNC_AGENT_DA_SUCCESS;
355 EXPORT_API int sync_agent_plugin_get_used_item_count()
359 int ret = SYNC_AGENT_DA_SUCCESS;
361 int err = memo_get_count(&used_cnt);
363 _DEBUG_INFO("[da_memo_plugIn] memo_get_count() Fail\n");
364 ret = _convert_service_error_to_common_error(err);
366 _DEBUG_INFO("[da_memo_plugIn] memo_get_count() Success\n");
367 _DEBUG_INFO("[da_memo_plugIn] used_count = %d\n", used_cnt);
376 EXPORT_API int sync_agent_plugin_get_used_item_count_for_folder(int account_id, char *folder_id)
380 _DEBUG_INFO("[da_memo_plugIn] Memo plugIn is not supported this function\n");
381 _DEBUG_INFO("[da_memo_plugIn] Return all memo count \n");
385 return sync_agent_plugin_get_used_item_count();
388 EXPORT_API char **sync_agent_plugin_get_folder_id_list(int account_id, int *folder_count, int **folder_type_list)
392 char **folder_id_list = (char **)calloc(1, sizeof(char *));
393 if (folder_id_list == NULL)
396 *folder_type_list = (int *)calloc(1, sizeof(int));
397 if (*folder_type_list == NULL) {
398 free(folder_id_list);
402 folder_id_list[0] = "0";
404 (*folder_type_list)[0] = DEFAULT_MEMO_FOLDER;
408 return folder_id_list;
411 EXPORT_API int *sync_agent_plugin_get_account_id_list(int *count)
415 _DEBUG_INFO("[da_memo_plugIn] start!!\n");
417 int *account_id_list = (int *)calloc(1, sizeof(int));
418 if (account_id_list == NULL)
422 account_id_list[0] = -1;
426 return account_id_list;
429 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_add(int account_id, const char *folder_id, int changepoint, int *changeCount)
433 _DEBUG_INFO("\n[da_memo_plugIn] account_id : %d\n", account_id);
434 _DEBUG_INFO("[da_memo_plugIn] folder_id : %s\n", folder_id);
435 _DEBUG_INFO("[da_memo_plugIn] changepoint : %d\n", changepoint);
439 int memo_time_stamp = changepoint;
440 struct memo_operation_list *item_list = 0;
441 struct memo_operation_list *item_list_origin = 0;
443 sync_agent_plugin_item_node_s *root_ptr = 0;
444 sync_agent_plugin_item_node_s *cursor_ptr = 0;
445 int chagned_count = 0;
447 item_list = memo_get_operation_list(memo_time_stamp);
448 if (item_list == NULL)
451 item_list_origin = item_list;
453 while ((item_list != 0) && (item_list->id > -1)) {
454 if (item_list->operation == MEMO_OPERATION_ADD) {
455 int digit = _get_digit(item_list->id);
456 char *str_item_id = (char *)calloc(digit + 1, sizeof(char));
457 if (str_item_id == NULL) {
458 _DEBUG_ERROR("str_item_id is null");
462 snprintf(str_item_id, digit + 1, "%d", item_list->id);
463 _DEBUG_INFO("[da_memo_plugIn] MEMO_OPERATION_ADD : [%s]\n", str_item_id);
465 if (root_ptr == NULL) {
466 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
467 if (root_ptr == NULL) {
468 _DEBUG_ERROR("root_ptr is null, calloc() FAIL !!!");
473 root_ptr->item_id = str_item_id;
475 cursor_ptr = root_ptr;
477 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
478 if (cursor_ptr->next == NULL) {
479 _DEBUG_ERROR("cursor_ptr->next is null, calloc() FAIL !!!");
485 cursor_ptr->next->item_id = str_item_id;
486 cursor_ptr->next->next = 0;
487 cursor_ptr = cursor_ptr->next;
492 item_list = item_list->next;
498 if (item_list_origin != NULL) {
499 memo_free_operation_list(item_list_origin);
500 item_list_origin = 0;
503 if (success != true) {
504 _free_item_node(root_ptr);
508 *changeCount = chagned_count;
516 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_delete(int account_id, const char *folder_id, int changepoint, int *changeCount)
520 _DEBUG_INFO("\n[da_memo_plugIn] account_id : %d\n", account_id);
521 _DEBUG_INFO("[da_memo_plugIn] folder_id : %s\n", folder_id);
522 _DEBUG_INFO("[da_memo_plugIn] changepoint : %d\n", changepoint);
526 int memo_time_stamp = changepoint;
527 struct memo_operation_list *item_list = 0;
528 struct memo_operation_list *item_list_origin = 0;
530 sync_agent_plugin_item_node_s *root_ptr = 0;
531 sync_agent_plugin_item_node_s *cursor_ptr = 0;
532 int chagned_count = 0;
534 item_list = memo_get_operation_list(memo_time_stamp);
535 if (item_list == NULL)
538 item_list_origin = item_list;
540 while ((item_list != 0) && (item_list->id > -1)) {
541 if (item_list->operation == MEMO_OPERATION_DELETE) {
542 int digit = _get_digit(item_list->id);
543 char *str_item_id = (char *)calloc(digit + 1, sizeof(char));
544 if (str_item_id == NULL) {
545 _DEBUG_ERROR("str_item_id is null");
549 snprintf(str_item_id, digit + 1, "%d", item_list->id);
550 _DEBUG_INFO("[da_memo_plugIn] MEMO_OPERATION_DELETE : [%s]\n", str_item_id);
552 if (root_ptr == NULL) {
553 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
554 if (root_ptr == NULL) {
555 _DEBUG_ERROR("root_ptr is null, calloc() FAIL !!!");
560 root_ptr->item_id = str_item_id;
562 cursor_ptr = root_ptr;
564 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
565 if (cursor_ptr->next == NULL) {
566 _DEBUG_ERROR("cursor_ptr->next is null, calloc() FAIL !!!");
572 cursor_ptr->next->item_id = str_item_id;
573 cursor_ptr->next->next = 0;
574 cursor_ptr = cursor_ptr->next;
579 item_list = item_list->next;
585 if (item_list_origin != NULL) {
586 memo_free_operation_list(item_list_origin);
587 item_list_origin = 0;
590 if (success != true) {
591 _free_item_node(root_ptr);
595 *changeCount = chagned_count;
603 EXPORT_API sync_agent_plugin_item_node_s *sync_agent_plugin_get_changed_item_for_folder_update(int account_id, const char *folder_id, int changepoint, int *changeCount)
607 _DEBUG_INFO("\n[da_memo_plugIn] account_id : %d\n", account_id);
608 _DEBUG_INFO("[da_memo_plugIn] folder_id : %s\n", folder_id);
609 _DEBUG_INFO("[da_memo_plugIn] changepoint : %d\n", changepoint);
613 int memo_time_stamp = changepoint;
614 struct memo_operation_list *item_list = 0;
615 struct memo_operation_list *item_list_origin = 0;
617 sync_agent_plugin_item_node_s *root_ptr = 0;
618 sync_agent_plugin_item_node_s *cursor_ptr = 0;
619 int chagned_count = 0;
621 item_list = memo_get_operation_list(memo_time_stamp);
622 if (item_list == NULL)
625 item_list_origin = item_list;
627 while ((item_list != 0) && (item_list->id > -1)) {
628 if (item_list->operation == MEMO_OPERATION_UPDATE) {
629 int digit = _get_digit(item_list->id);
630 char *str_item_id = (char *)calloc(digit + 1, sizeof(char));
631 if (str_item_id == NULL) {
632 _DEBUG_ERROR("str_item_id is null");
636 snprintf(str_item_id, digit + 1, "%d", item_list->id);
637 _DEBUG_INFO("[da_memo_plugIn] MEMO_OPERATION_UPDATE : [%s]\n", str_item_id);
639 if (root_ptr == NULL) {
640 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
641 if (root_ptr == NULL) {
642 _DEBUG_ERROR("root_ptr is null, calloc() FAIL !!!");
647 root_ptr->item_id = str_item_id;
649 cursor_ptr = root_ptr;
651 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
652 if (cursor_ptr->next == NULL) {
653 _DEBUG_ERROR("cursor_ptr->next is null, calloc() FAIL !!!");
659 cursor_ptr->next->item_id = str_item_id;
660 cursor_ptr->next->next = 0;
661 cursor_ptr = cursor_ptr->next;
666 item_list = item_list->next;
672 if (item_list_origin != NULL) {
673 memo_free_operation_list(item_list_origin);
674 item_list_origin = 0;
677 if (success != true) {
678 _free_item_node(root_ptr);
682 *changeCount = chagned_count;
690 EXPORT_API int sync_agent_plugin_get_last_change_point()
699 EXPORT_API void sync_agent_plugin_start_listening_change_noti(void *data)
703 memo_subscribe_change(_process_memo_change, 0);
708 EXPORT_API void sync_agent_plugin_set_callback_add_item(sync_agent_add_item_cb_plugin callback)
712 callback_add_item = callback;
717 EXPORT_API void sync_agent_plugin_set_callback_delete_item(sync_agent_del_item_cb_plugin callback)
721 callback_del_item = callback;
726 EXPORT_API void sync_agent_plugin_set_callback_update_item(sync_agent_update_item_cb_plugin callback)
730 callback_update_item = callback;
735 EXPORT_API void sync_agent_plugin_set_callback_get_account_id_list(sync_agent_get_account_id_list_cb_plugin callback)
739 callback_get_accountidlist = callback;
744 EXPORT_API void sync_agent_plugin_set_callback_delete_child_item(sync_agent_del_child_item_cb_plugin callback)
747 callback_delete_child_item = callback;
751 /********************************** static function *******************************************/
753 static void _free_item_node(sync_agent_plugin_item_node_s * node)
756 if (node->item_id != NULL) {
758 node->item_id = NULL;
760 if (node->next != NULL) {
761 _free_item_node(node->next);
769 static void _set_is_noti_from_me(int set_flag)
773 if (pthread_mutex_lock(&lockx)) {
774 _DEBUG_TRACE("[da_memo_plugIn] pthread_mutex_lock error");
777 is_noti_from_me = set_flag;
779 if (pthread_mutex_unlock(&lockx)) {
780 _DEBUG_TRACE("[da_memo_plugIn] pthread_mutex_unlock error");
786 static int __get_is_noti_from_me()
792 return is_noti_from_me;
795 void _process_memo_change(void *data)
799 _DEBUG_TRACE("[da_memo_plugIn] detected memo storage changed!!\n");
801 int from_Me = __get_is_noti_from_me();
803 _set_is_noti_from_me(0);
804 _DEBUG_TRACE("[da_memo_plugIn] This noti is from Me!! so will be ignored!!\n");
808 pthread_t thread_process_change_id;
809 int status_thread_create = 0;
810 status_thread_create = pthread_create(&thread_process_change_id, 0, __rutine_memo_change, 0);
811 if(status_thread_create != 0) {
812 _DEBUG_WARNING("pthread_create [%d]", status_thread_create );
818 static void *__rutine_memo_change(void *data)
822 _DEBUG_VERBOSE("[da_memo_plugIn] Start __rutine_memo_change (create thread)\n");
824 int memo_time_stamp = get_memo_time_stamp();
829 char *str_item_id = 0;
830 int *accountList = 0;
831 struct memo_operation_list *item_list = 0;
832 struct memo_operation_list *item_list_origin = 0;
834 accountList = callback_get_accountidlist(FW_MEMO, &count);
835 if (accountList == NULL)
838 item_list = memo_get_operation_list(memo_time_stamp);
839 if (item_list == NULL)
842 item_list_origin = item_list;
844 while ((item_list != 0) && (item_list->id > -1)) {
845 str_item_id = g_strdup_printf("%d", item_list->id);
847 for (i = 0; i < count; i++) {
848 _DEBUG_VERBOSE("[da_memo_plugIn] memo_account_id : %d, count : %d\n", accountList[i], count);
849 _DEBUG_VERBOSE("[da_memo_plugIn] memo_get_operation_list : %d\n", memo_time_stamp);
851 switch (item_list->operation) {
852 case MEMO_OPERATION_ADD:
854 /* memo service is not supported folder */
855 _DEBUG_VERBOSE("[da_memo_plugIn] MEMO_OPERATION_ADD\n");
856 result = callback_add_item(accountList[i], i, str_item_id, NULL, FW_MEMO, "0", 0);
858 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_add_item() \n");
863 case MEMO_OPERATION_UPDATE:
865 _DEBUG_VERBOSE("[da_memo_plugIn] MEMO_OPERATION_UPDATE\n");
866 result = callback_update_item(accountList[i], i, str_item_id, NULL, FW_MEMO);
868 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_update_item() \n");
873 case MEMO_OPERATION_DELETE:
875 _DEBUG_VERBOSE("[da_memo_plugIn] MEMO_OPERATION_DELETE\n");
876 result = callback_del_item(accountList[i], i, str_item_id, FW_MEMO);
878 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_del_item() \n");
884 _DEBUG_VERBOSE("[da_memo_plugIn] Another Memo Change Noti\n");
888 item_list = item_list->next;
896 if (item_list_origin != NULL) {
897 memo_free_operation_list(item_list_origin);
898 item_list_origin = 0;
901 if (accountList != NULL) {
906 if (str_item_id != NULL) {
911 _DEBUG_VERBOSE("[da_memo_plugIn] End __rutine_memo_change (create thread)\n");
918 static sync_agent_da_return_e _convert_service_error_to_common_error(int err)
922 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
923 _DEBUG_TRACE("[da_memo_plugIn] Error Code : %d\n", err);
927 ret = SYNC_AGENT_DA_ERRORS;
936 static int _get_digit(int new_id)