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 void _free_item_node(sync_agent_plugin_item_node_s * node);
61 static void _set_is_noti_from_me(int set_flag);
63 static int __get_is_noti_from_me();
65 static void _process_memo_change(void *);
67 static void *__rutine_memo_change(void *);
69 static sync_agent_da_return_e _convert_service_error_to_common_error(int err);
71 static int _get_digit(int new_id);
73 EXPORT_API sync_agent_da_return_e sync_agent_plugin_open_service()
77 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
78 int err = memo_init(0);
80 _DEBUG_INFO("[da_memo_plugIn] in memo Fail!\n");
81 ret = SYNC_AGENT_DA_ERR_OPEN_FAILED;
83 _DEBUG_INFO("[da_memo_plugIn] in memo Success!");
91 EXPORT_API sync_agent_da_return_e sync_agent_plugin_close_service()
95 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
97 /* memo service api does not return value */
105 EXPORT_API sync_agent_da_return_e sync_agent_plugin_begin_transaction()
109 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
111 /* memo service api does not return value */
112 /* real transaction is not for event notification */
114 _set_is_noti_from_me(1);
121 EXPORT_API sync_agent_da_return_e sync_agent_plugin_end_transaction(int is_success)
125 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
127 /* memo service api does not return value */
128 /* real transaction is not for event notification */
129 /* memo service is not supported rollback operation */
131 /* _set_is_noti_from_me(1);*/
133 if (is_storage_changed == 0) {
134 _DEBUG_INFO("memo storaged was not Changed!!");
135 _set_is_noti_from_me(0);
137 is_storage_changed = 0;
144 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_item(int account_id, char *folder_id, void *data, char **item_id)
148 retvm_if(data == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "data is NULL. FAIL !!!");
150 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
152 struct memo_data *item = (struct memo_data *)data;
154 /* configuration item */
156 item->modi_time = time((time_t *) 0);
157 item->font_size = 32;
158 item->font_color = 0xff000000;
161 err = memo_add_data(item);
163 _DEBUG_INFO("[da_memo_plugIn] memo_add_data() Fail!\n");
164 ret = _convert_service_error_to_common_error(err);
167 _DEBUG_INFO("[da_memo_plugIn] memo_add_data() Success!\n");
168 *item_id = g_strdup_printf("%d", err);
169 is_storage_changed = 1;
174 memo_free_data(item);
181 EXPORT_API sync_agent_da_return_e sync_agent_plugin_update_item(int account_id, char *folder_id, char *item_id, void *data)
185 retvm_if(data == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "data is NULL. FAIL !!!");
187 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
189 struct memo_data *item = (struct memo_data *)data;
192 err = memo_mod_data(item);
194 _DEBUG_INFO("[da_memo_plugIn] memo_mod_data() Fail!\n");
195 ret = _convert_service_error_to_common_error(err);
197 _DEBUG_INFO("[da_memo_plugIn] memo_mod_data() Success!\n");
198 is_storage_changed = 1;
203 memo_free_data(item);
210 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_item(int account_id, char *folder_id, char *item_id)
214 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
216 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
220 err = memo_del_data(atoi(item_id));
222 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%s) Fail!\n", item_id);
223 ret = _convert_service_error_to_common_error(err);
225 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%s) Success!\n", item_id);
226 is_storage_changed = 1;
234 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_all_items(int account_id)
238 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
240 struct memo_data_list *item_list = NULL;
241 struct memo_data_list *first_item_list = NULL;
243 /* get all data in memo db */
244 item_list = memo_get_all_data_list();
245 first_item_list = item_list;
247 while (item_list != NULL) {
248 err = memo_del_data(item_list->md.id);
250 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%d) Fail!\n", item_list->md.id);
251 ret = _convert_service_error_to_common_error(err);
254 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%d) Success!\n", item_list->md.id);
255 is_storage_changed = 1;
259 if (item_list->md.comment != NULL) {
260 free(item_list->md.comment);
261 item_list->md.comment = 0;
264 if (item_list->md.doodle_path != NULL) {
265 free(item_list->md.doodle_path);
266 item_list->md.doodle_path = 0;
269 item_list = item_list->next;
273 if (first_item_list != NULL) {
274 memo_free_data_list(first_item_list);
275 _DEBUG_INFO("[da_memo_plugIn] memo_free_data_list \n");
283 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_item(int account_id, char *folder_id, char *item_id, void **data)
287 retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
289 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
290 struct memo_data *item = 0;
293 item = memo_get_data(atoi(item_id));
295 _DEBUG_INFO("[da_memo_plugIn] memo_get_data() Fail!\n");
296 ret = SYNC_AGENT_DA_ERRORS;
299 _DEBUG_INFO("[da_memo_plugIn] memo_get_data() Success!");
300 *data = (void *)item;
308 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_folder(int account_id, char *folder_name, int folder_type, char **folder_id)
312 _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
316 return SYNC_AGENT_DA_SUCCESS;
319 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_folder(int account_id, char *folder_id)
323 _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
327 return SYNC_AGENT_DA_SUCCESS;
330 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)
334 _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
338 return SYNC_AGENT_DA_SUCCESS;
341 EXPORT_API sync_agent_da_return_e sync_agent_plugin_execute(int account_ID, const char *execute_key, void *execute_values, void **result)
345 _DEBUG_INFO("[da_memo_plugIn] memo service is not supported execute. \n");
350 return SYNC_AGENT_DA_SUCCESS;
353 EXPORT_API int sync_agent_plugin_get_used_item_count()
357 int ret = SYNC_AGENT_DA_SUCCESS;
359 int err = memo_get_count(&used_cnt);
361 _DEBUG_INFO("[da_memo_plugIn] memo_get_count() Fail\n");
362 ret = _convert_service_error_to_common_error(err);
364 _DEBUG_INFO("[da_memo_plugIn] memo_get_count() Success\n");
365 _DEBUG_INFO("[da_memo_plugIn] used_count = %d\n", used_cnt);
374 EXPORT_API int sync_agent_plugin_get_used_item_count_for_folder(int account_id, char *folder_id)
378 _DEBUG_INFO("[da_memo_plugIn] Memo plugIn is not supported this function\n");
379 _DEBUG_INFO("[da_memo_plugIn] Return all memo count \n");
383 return sync_agent_plugin_get_used_item_count();
386 EXPORT_API char **sync_agent_plugin_get_folder_id_list(int account_id, int *folder_count, int **folder_type_list)
390 char **folder_id_list = (char **)calloc(1, sizeof(char *));
391 if (folder_id_list == NULL)
394 *folder_type_list = (int *)calloc(1, sizeof(int));
395 if (*folder_type_list == NULL) {
396 free(folder_id_list);
400 folder_id_list[0] = "0";
402 (*folder_type_list)[0] = DEFAULT_MEMO_FOLDER;
406 return folder_id_list;
409 EXPORT_API int *sync_agent_plugin_get_account_id_list(int *count)
413 _DEBUG_INFO("[da_memo_plugIn] start!!\n");
415 int *account_id_list = (int *)calloc(1, sizeof(int));
416 if (account_id_list == NULL)
420 account_id_list[0] = -1;
424 return account_id_list;
427 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)
431 _DEBUG_INFO("\n[da_memo_plugIn] account_id : %d\n", account_id);
432 _DEBUG_INFO("[da_memo_plugIn] folder_id : %s\n", folder_id);
433 _DEBUG_INFO("[da_memo_plugIn] changepoint : %d\n", changepoint);
437 int memo_time_stamp = changepoint;
438 struct memo_operation_list *item_list = 0;
439 struct memo_operation_list *item_list_origin = 0;
441 sync_agent_plugin_item_node_s *root_ptr = 0;
442 sync_agent_plugin_item_node_s *cursor_ptr = 0;
443 int chagned_count = 0;
445 item_list = memo_get_operation_list(memo_time_stamp);
446 if (item_list == NULL)
449 item_list_origin = item_list;
451 while ((item_list != 0) && (item_list->id > -1)) {
452 if (item_list->operation == MEMO_OPERATION_ADD) {
453 int digit = _get_digit(item_list->id);
454 char *str_item_id = (char *)calloc(digit + 1, sizeof(char));
455 if (str_item_id == NULL) {
456 _DEBUG_ERROR("str_item_id is null");
460 snprintf(str_item_id, digit + 1, "%d", item_list->id);
461 _DEBUG_INFO("[da_memo_plugIn] MEMO_OPERATION_ADD : [%s]\n", str_item_id);
463 if (root_ptr == NULL) {
464 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
465 if (root_ptr == NULL) {
466 _DEBUG_ERROR("root_ptr is null, calloc() FAIL !!!");
471 root_ptr->item_id = str_item_id;
473 cursor_ptr = root_ptr;
475 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
476 if (cursor_ptr->next == NULL) {
477 _DEBUG_ERROR("cursor_ptr->next is null, calloc() FAIL !!!");
483 cursor_ptr->next->item_id = str_item_id;
484 cursor_ptr->next->next = 0;
485 cursor_ptr = cursor_ptr->next;
490 item_list = item_list->next;
496 if (item_list_origin != NULL) {
497 memo_free_operation_list(item_list_origin);
498 item_list_origin = 0;
501 if (success != true) {
502 _free_item_node(root_ptr);
506 *changeCount = chagned_count;
514 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)
518 _DEBUG_INFO("\n[da_memo_plugIn] account_id : %d\n", account_id);
519 _DEBUG_INFO("[da_memo_plugIn] folder_id : %s\n", folder_id);
520 _DEBUG_INFO("[da_memo_plugIn] changepoint : %d\n", changepoint);
524 int memo_time_stamp = changepoint;
525 struct memo_operation_list *item_list = 0;
526 struct memo_operation_list *item_list_origin = 0;
528 sync_agent_plugin_item_node_s *root_ptr = 0;
529 sync_agent_plugin_item_node_s *cursor_ptr = 0;
530 int chagned_count = 0;
532 item_list = memo_get_operation_list(memo_time_stamp);
533 if (item_list == NULL)
536 item_list_origin = item_list;
538 while ((item_list != 0) && (item_list->id > -1)) {
539 if (item_list->operation == MEMO_OPERATION_DELETE) {
540 int digit = _get_digit(item_list->id);
541 char *str_item_id = (char *)calloc(digit + 1, sizeof(char));
542 if (str_item_id == NULL) {
543 _DEBUG_ERROR("str_item_id is null");
547 snprintf(str_item_id, digit + 1, "%d", item_list->id);
548 _DEBUG_INFO("[da_memo_plugIn] MEMO_OPERATION_DELETE : [%s]\n", str_item_id);
550 if (root_ptr == NULL) {
551 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
552 if (root_ptr == NULL) {
553 _DEBUG_ERROR("root_ptr is null, calloc() FAIL !!!");
558 root_ptr->item_id = str_item_id;
560 cursor_ptr = root_ptr;
562 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
563 if (cursor_ptr->next == NULL) {
564 _DEBUG_ERROR("cursor_ptr->next is null, calloc() FAIL !!!");
570 cursor_ptr->next->item_id = str_item_id;
571 cursor_ptr->next->next = 0;
572 cursor_ptr = cursor_ptr->next;
577 item_list = item_list->next;
583 if (item_list_origin != NULL) {
584 memo_free_operation_list(item_list_origin);
585 item_list_origin = 0;
588 if (success != true) {
589 _free_item_node(root_ptr);
593 *changeCount = chagned_count;
601 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)
605 _DEBUG_INFO("\n[da_memo_plugIn] account_id : %d\n", account_id);
606 _DEBUG_INFO("[da_memo_plugIn] folder_id : %s\n", folder_id);
607 _DEBUG_INFO("[da_memo_plugIn] changepoint : %d\n", changepoint);
611 int memo_time_stamp = changepoint;
612 struct memo_operation_list *item_list = 0;
613 struct memo_operation_list *item_list_origin = 0;
615 sync_agent_plugin_item_node_s *root_ptr = 0;
616 sync_agent_plugin_item_node_s *cursor_ptr = 0;
617 int chagned_count = 0;
619 item_list = memo_get_operation_list(memo_time_stamp);
620 if (item_list == NULL)
623 item_list_origin = item_list;
625 while ((item_list != 0) && (item_list->id > -1)) {
626 if (item_list->operation == MEMO_OPERATION_UPDATE) {
627 int digit = _get_digit(item_list->id);
628 char *str_item_id = (char *)calloc(digit + 1, sizeof(char));
629 if (str_item_id == NULL) {
630 _DEBUG_ERROR("str_item_id is null");
634 snprintf(str_item_id, digit + 1, "%d", item_list->id);
635 _DEBUG_INFO("[da_memo_plugIn] MEMO_OPERATION_UPDATE : [%s]\n", str_item_id);
637 if (root_ptr == NULL) {
638 root_ptr = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
639 if (root_ptr == NULL) {
640 _DEBUG_ERROR("root_ptr is null, calloc() FAIL !!!");
645 root_ptr->item_id = str_item_id;
647 cursor_ptr = root_ptr;
649 cursor_ptr->next = (sync_agent_plugin_item_node_s *) calloc(1, sizeof(sync_agent_plugin_item_node_s));
650 if (cursor_ptr->next == NULL) {
651 _DEBUG_ERROR("cursor_ptr->next is null, calloc() FAIL !!!");
657 cursor_ptr->next->item_id = str_item_id;
658 cursor_ptr->next->next = 0;
659 cursor_ptr = cursor_ptr->next;
664 item_list = item_list->next;
670 if (item_list_origin != NULL) {
671 memo_free_operation_list(item_list_origin);
672 item_list_origin = 0;
675 if (success != true) {
676 _free_item_node(root_ptr);
680 *changeCount = chagned_count;
688 EXPORT_API int sync_agent_plugin_get_last_change_point()
697 EXPORT_API void sync_agent_plugin_start_listening_change_noti(void *data)
701 memo_subscribe_change(_process_memo_change, 0);
706 EXPORT_API void sync_agent_plugin_set_callback_add_item(sync_agent_add_item_cb_plugin callback)
710 callback_add_item = callback;
715 EXPORT_API void sync_agent_plugin_set_callback_delete_item(sync_agent_del_item_cb_plugin callback)
719 callback_del_item = callback;
724 EXPORT_API void sync_agent_plugin_set_callback_update_item(sync_agent_update_item_cb_plugin callback)
728 callback_update_item = callback;
733 EXPORT_API void sync_agent_plugin_set_callback_get_account_id_list(sync_agent_get_account_id_list_cb_plugin callback)
737 callback_get_accountidlist = callback;
742 /********************************** static function *******************************************/
744 static void _free_item_node(sync_agent_plugin_item_node_s * node)
747 if (node->item_id != NULL) {
749 node->item_id = NULL;
751 if (node->next != NULL) {
752 _free_item_node(node->next);
760 static void _set_is_noti_from_me(int set_flag)
764 if (pthread_mutex_lock(&lockx)) {
765 _DEBUG_TRACE("[da_memo_plugIn] pthread_mutex_lock error");
768 is_noti_from_me = set_flag;
770 if (pthread_mutex_unlock(&lockx)) {
771 _DEBUG_TRACE("[da_memo_plugIn] pthread_mutex_unlock error");
777 static int __get_is_noti_from_me()
783 return is_noti_from_me;
786 void _process_memo_change(void *data)
790 _DEBUG_TRACE("[da_memo_plugIn] detected memo storage changed!!\n");
792 int from_Me = __get_is_noti_from_me();
794 _set_is_noti_from_me(0);
795 _DEBUG_TRACE("[da_memo_plugIn] This noti is from Me!! so will be ignored!!\n");
799 pthread_t thread_process_change_id;
800 pthread_create(&thread_process_change_id, 0, __rutine_memo_change, 0);
805 static void *__rutine_memo_change(void *data)
809 _DEBUG_VERBOSE("[da_memo_plugIn] Start __rutine_memo_change (create thread)\n");
811 int memo_time_stamp = get_memo_time_stamp();
816 char *str_item_id = 0;
817 int *accountList = 0;
818 struct memo_operation_list *item_list = 0;
819 struct memo_operation_list *item_list_origin = 0;
821 accountList = callback_get_accountidlist(FW_MEMO, &count);
822 if (accountList == NULL)
825 item_list = memo_get_operation_list(memo_time_stamp);
826 if (item_list == NULL)
829 item_list_origin = item_list;
831 while ((item_list != 0) && (item_list->id > -1)) {
832 str_item_id = g_strdup_printf("%d", item_list->id);
834 for (i = 0; i < count; i++) {
835 _DEBUG_VERBOSE("[da_memo_plugIn] memo_account_id : %d, count : %d\n", accountList[i], count);
836 _DEBUG_VERBOSE("[da_memo_plugIn] memo_get_operation_list : %d\n", memo_time_stamp);
838 switch (item_list->operation) {
839 case MEMO_OPERATION_ADD:
841 /* memo service is not supported folder */
842 _DEBUG_VERBOSE("[da_memo_plugIn] MEMO_OPERATION_ADD\n");
843 result = callback_add_item(accountList[i], i, str_item_id, FW_MEMO, "0", 0);
845 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_add_item() \n");
850 case MEMO_OPERATION_UPDATE:
852 _DEBUG_VERBOSE("[da_memo_plugIn] MEMO_OPERATION_UPDATE\n");
853 result = callback_update_item(accountList[i], i, str_item_id, FW_MEMO);
855 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_update_item() \n");
860 case MEMO_OPERATION_DELETE:
862 _DEBUG_VERBOSE("[da_memo_plugIn] MEMO_OPERATION_DELETE\n");
863 result = callback_del_item(accountList[i], i, str_item_id, FW_MEMO);
865 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_del_item() \n");
871 _DEBUG_VERBOSE("[da_memo_plugIn] Another Memo Change Noti\n");
875 item_list = item_list->next;
883 if (item_list_origin != NULL) {
884 memo_free_operation_list(item_list_origin);
885 item_list_origin = 0;
888 if (accountList != NULL) {
893 if (str_item_id != NULL) {
898 _DEBUG_VERBOSE("[da_memo_plugIn] End __rutine_memo_change (create thread)\n");
905 static sync_agent_da_return_e _convert_service_error_to_common_error(int err)
909 sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
910 _DEBUG_TRACE("[da_memo_plugIn] Error Code : %d\n", err);
914 ret = SYNC_AGENT_DA_ERRORS;
923 static int _get_digit(int new_id)