sync-agent: Change the system-info api to 2.3 public api
[platform/core/system/sync-agent.git] / src / fw-plugins / common-public / memo / src / plugin_interface.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <stdlib.h>
19 #include <glib.h>
20 #include <pthread.h>
21 #include <string.h>
22 #include <malloc.h>
23
24 #include <memo-db.h>
25
26 #include "extern_info.h"
27 #include "item_change_info.h"
28
29 /* for log */
30 #include "utility/sync_util.h"
31
32 #include "plugin/data_connector_interface.h"
33
34 #ifndef EXPORT_API
35 #define EXPORT_API __attribute__ ((visibility("default")))
36 #endif
37
38 #ifndef SYNC_AGENT_LOG
39 #undef LOG_TAG
40 #define LOG_TAG "PLUGIN_DA_MEMO"
41 #endif
42
43 #define FW_MEMO         2       /* plugIn-Id */
44
45 static pthread_mutex_t lockx;
46
47 static int is_noti_from_me = 0;
48
49 static int is_storage_changed = 0;
50
51 static sync_agent_add_item_cb_plugin callback_add_item;
52
53 static sync_agent_del_item_cb_plugin callback_del_item;
54
55 static sync_agent_update_item_cb_plugin callback_update_item;
56
57 static sync_agent_get_account_id_list_cb_plugin callback_get_accountidlist;
58
59 static sync_agent_del_child_item_cb_plugin callback_delete_child_item;
60
61 static void _free_item_node(sync_agent_plugin_item_node_s * node);
62
63 static void _set_is_noti_from_me(int set_flag);
64
65 static int __get_is_noti_from_me();
66
67 static void _process_memo_change(void *);
68
69 static void *__rutine_memo_change(void *);
70
71 static sync_agent_da_return_e _convert_service_error_to_common_error(int err);
72
73 static int _get_digit(int new_id);
74
75 EXPORT_API sync_agent_da_return_e sync_agent_plugin_open_service()
76 {
77         _EXTERN_FUNC_ENTER;
78
79         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
80         int err = memo_init(0);
81         if (err < 0) {
82                 _DEBUG_INFO("[da_memo_plugIn] in memo Fail!\n");
83                 ret = SYNC_AGENT_DA_ERR_OPEN_FAILED;
84         } else {
85                 _DEBUG_INFO("[da_memo_plugIn] in memo Success!");
86         }
87
88         _EXTERN_FUNC_EXIT;
89
90         return ret;
91 }
92
93 EXPORT_API sync_agent_da_return_e sync_agent_plugin_close_service()
94 {
95         _EXTERN_FUNC_ENTER;
96
97         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
98
99         /* memo service api does not return value */
100         memo_fini();
101
102         _EXTERN_FUNC_EXIT;
103
104         return ret;
105 }
106
107 EXPORT_API sync_agent_da_return_e sync_agent_plugin_begin_transaction()
108 {
109         _EXTERN_FUNC_ENTER;
110
111         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
112
113         /* memo service api does not return value */
114         /* real transaction is not for event notification */
115         memo_begin_trans();
116         _set_is_noti_from_me(1);
117
118         _EXTERN_FUNC_EXIT;
119
120         return ret;
121 }
122
123 EXPORT_API sync_agent_da_return_e sync_agent_plugin_end_transaction(int is_success)
124 {
125         _EXTERN_FUNC_ENTER;
126
127         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
128
129         /* memo service api does not return value */
130         /* real transaction is not for event notification */
131         /* memo service is not supported rollback operation */
132         memo_end_trans();
133 /*      _set_is_noti_from_me(1);*/
134
135         if (is_storage_changed == 0) {
136                 _DEBUG_INFO("memo storaged was not Changed!!");
137                 _set_is_noti_from_me(0);
138         }
139         is_storage_changed = 0;
140
141         _EXTERN_FUNC_EXIT;
142
143         return ret;
144 }
145
146 EXPORT_API sync_agent_da_return_e sync_agent_plugin_add_item(int account_id, char *folder_id, void *data, char **item_id)
147 {
148         _EXTERN_FUNC_ENTER;
149
150         retvm_if(data == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "data is NULL. FAIL !!!");
151
152         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
153         int err = 0;
154         struct memo_data *item = (struct memo_data *)data;
155
156         /* configuration item */
157         item->id = -1;
158         item->modi_time = time((time_t *) 0);
159         item->font_size = 32;
160         item->font_color = 0xff000000;
161
162         /*  add item */
163         err = memo_add_data(item);
164         if (err < 0) {
165                 _DEBUG_INFO("[da_memo_plugIn] memo_add_data() Fail!\n");
166                 ret = _convert_service_error_to_common_error(err);
167                 *item_id = 0;
168         } else {
169                 _DEBUG_INFO("[da_memo_plugIn] memo_add_data() Success!\n");
170                 *item_id = g_strdup_printf("%d", err);
171                 is_storage_changed = 1;
172         }
173
174         /* memory free */
175         if (item != NULL)
176                 memo_free_data(item);
177
178         _EXTERN_FUNC_EXIT;
179
180         return ret;
181 }
182
183 EXPORT_API sync_agent_da_return_e sync_agent_plugin_update_item(int account_id, char *folder_id, char *item_id, void *data)
184 {
185         _EXTERN_FUNC_ENTER;
186
187         retvm_if(data == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "data is NULL. FAIL !!!");
188
189         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
190         int err = 0;
191         struct memo_data *item = (struct memo_data *)data;
192
193         /* update item */
194         err = memo_mod_data(item);
195         if (err < 0) {
196                 _DEBUG_INFO("[da_memo_plugIn] memo_mod_data() Fail!\n");
197                 ret = _convert_service_error_to_common_error(err);
198         } else {
199                 _DEBUG_INFO("[da_memo_plugIn] memo_mod_data() Success!\n");
200                 is_storage_changed = 1;
201         }
202
203         /*  memory free */
204         if (item != NULL)
205                 memo_free_data(item);
206
207         _EXTERN_FUNC_EXIT;
208
209         return ret;
210 }
211
212 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_item(int account_id, char *folder_id, char *item_id)
213 {
214         _EXTERN_FUNC_ENTER;
215
216         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
217
218         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
219         int err = 0;
220
221         /* delete item */
222         err = memo_del_data(atoi(item_id));
223         if (err < 0) {
224                 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%s) Fail!\n", item_id);
225                 ret = _convert_service_error_to_common_error(err);
226         } else {
227                 _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%s) Success!\n", item_id);
228                 is_storage_changed = 1;
229         }
230
231         _EXTERN_FUNC_EXIT;
232
233         return ret;
234 }
235
236 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_all_items(int account_id)
237 {
238         _EXTERN_FUNC_ENTER;
239
240         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
241         int err = 1;
242         struct memo_data_list *item_list = NULL;
243         struct memo_data_list *first_item_list = NULL;
244
245         /* get all data in memo db */
246         item_list = memo_get_all_data_list();
247         first_item_list = item_list;
248
249         while (item_list != NULL) {
250                 err = memo_del_data(item_list->md.id);
251                 if (err < 0) {
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);
254                         break;
255                 } else {
256                         _DEBUG_INFO("[da_memo_plugIn] memo_del_data(%d) Success!\n", item_list->md.id);
257                         is_storage_changed = 1;
258                 }
259
260                 /* memory free */
261                 if (item_list->md.comment != NULL) {
262                         free(item_list->md.comment);
263                         item_list->md.comment = 0;
264                 }
265
266                 if (item_list->md.doodle_path != NULL) {
267                         free(item_list->md.doodle_path);
268                         item_list->md.doodle_path = 0;
269                 }
270
271                 item_list = item_list->next;
272         }
273
274         /* memory free */
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");
278         }
279
280         _EXTERN_FUNC_EXIT;
281
282         return ret;
283 }
284
285 EXPORT_API sync_agent_da_return_e sync_agent_plugin_get_item(int account_id, char *folder_id, char *item_id, void **data)
286 {
287         _EXTERN_FUNC_ENTER;
288
289         retvm_if(item_id == NULL, SYNC_AGENT_DA_ERR_INVALID_CONTENT, "item_id is NULL. FAIL !!!");
290
291         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
292         struct memo_data *item = 0;
293
294         /* get item */
295         item = memo_get_data(atoi(item_id));
296         if (item == NULL) {
297                 _DEBUG_INFO("[da_memo_plugIn] memo_get_data() Fail!\n");
298                 ret = SYNC_AGENT_DA_ERRORS;
299                 *data = 0;
300         } else {
301                 _DEBUG_INFO("[da_memo_plugIn] memo_get_data() Success!");
302                 *data = (void *)item;
303         }
304
305         _EXTERN_FUNC_EXIT;
306
307         return ret;
308 }
309
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)
311 {
312         _EXTERN_FUNC_ENTER;
313
314         _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
315
316         _EXTERN_FUNC_EXIT;
317
318         return SYNC_AGENT_DA_SUCCESS;
319 }
320
321 EXPORT_API sync_agent_da_return_e sync_agent_plugin_delete_folder(int account_id, char *folder_id)
322 {
323         _EXTERN_FUNC_ENTER;
324
325         _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
326
327         _EXTERN_FUNC_EXIT;
328
329         return SYNC_AGENT_DA_SUCCESS;
330 }
331
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)
333 {
334         _EXTERN_FUNC_ENTER;
335
336         _DEBUG_INFO("[da_memo_plugIn] memo service is not supported folder\n");
337
338         _EXTERN_FUNC_EXIT;
339
340         return SYNC_AGENT_DA_SUCCESS;
341 }
342
343 EXPORT_API sync_agent_da_return_e sync_agent_plugin_execute(int account_ID, const char *execute_key, void *execute_values, void **result)
344 {
345         _EXTERN_FUNC_ENTER;
346
347         _DEBUG_INFO("[da_memo_plugIn] memo service is not supported execute. \n");
348         *result = 0;
349
350         _EXTERN_FUNC_EXIT;
351
352         return SYNC_AGENT_DA_SUCCESS;
353 }
354
355 EXPORT_API int sync_agent_plugin_get_used_item_count()
356 {
357         _EXTERN_FUNC_ENTER;
358
359         int ret = SYNC_AGENT_DA_SUCCESS;
360         int used_cnt = 0;
361         int err = memo_get_count(&used_cnt);
362         if (err == -1) {
363                 _DEBUG_INFO("[da_memo_plugIn] memo_get_count() Fail\n");
364                 ret = _convert_service_error_to_common_error(err);
365         } else {
366                 _DEBUG_INFO("[da_memo_plugIn] memo_get_count() Success\n");
367                 _DEBUG_INFO("[da_memo_plugIn] used_count = %d\n", used_cnt);
368                 ret = used_cnt;
369         }
370
371         _EXTERN_FUNC_EXIT;
372
373         return ret;
374 }
375
376 EXPORT_API int sync_agent_plugin_get_used_item_count_for_folder(int account_id, char *folder_id)
377 {
378         _EXTERN_FUNC_ENTER;
379
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");
382
383         _EXTERN_FUNC_EXIT;
384
385         return sync_agent_plugin_get_used_item_count();
386 }
387
388 EXPORT_API char **sync_agent_plugin_get_folder_id_list(int account_id, int *folder_count, int **folder_type_list)
389 {
390         _EXTERN_FUNC_ENTER;
391
392         char **folder_id_list = (char **)calloc(1, sizeof(char *));
393         if (folder_id_list == NULL)
394                 return 0;
395
396         *folder_type_list = (int *)calloc(1, sizeof(int));
397         if (*folder_type_list == NULL) {
398                 free(folder_id_list);
399                 return 0;
400         }
401
402         folder_id_list[0] = "0";
403         *folder_count = 1;
404         (*folder_type_list)[0] = DEFAULT_MEMO_FOLDER;
405
406         _EXTERN_FUNC_EXIT;
407
408         return folder_id_list;
409 }
410
411 EXPORT_API int *sync_agent_plugin_get_account_id_list(int *count)
412 {
413         _EXTERN_FUNC_ENTER;
414
415         _DEBUG_INFO("[da_memo_plugIn] start!!\n");
416
417         int *account_id_list = (int *)calloc(1, sizeof(int));
418         if (account_id_list == NULL)
419                 return 0;
420
421         *count = 1;
422         account_id_list[0] = -1;
423
424         _EXTERN_FUNC_EXIT;
425
426         return account_id_list;
427 }
428
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)
430 {
431         _EXTERN_FUNC_ENTER;
432
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);
436
437         bool success = true;
438
439         int memo_time_stamp = changepoint;
440         struct memo_operation_list *item_list = 0;
441         struct memo_operation_list *item_list_origin = 0;
442
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;
446
447         item_list = memo_get_operation_list(memo_time_stamp);
448         if (item_list == NULL)
449                 goto DACI_FINISH;
450
451         item_list_origin = item_list;
452
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");
459                                 goto DACI_FINISH;
460                         }
461
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);
464
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 !!!");
469                                         free(str_item_id);
470                                         goto DACI_FINISH;
471                                 }
472
473                                 root_ptr->item_id = str_item_id;
474                                 root_ptr->next = 0;
475                                 cursor_ptr = root_ptr;
476                         } else {
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 !!!");
480                                         free(str_item_id);
481                                         success = false;
482                                         goto DACI_FINISH;
483                                 }
484
485                                 cursor_ptr->next->item_id = str_item_id;
486                                 cursor_ptr->next->next = 0;
487                                 cursor_ptr = cursor_ptr->next;
488                         }
489
490                         chagned_count++;
491                 }
492                 item_list = item_list->next;
493         }
494
495  DACI_FINISH:
496
497         /*  memory free */
498         if (item_list_origin != NULL) {
499                 memo_free_operation_list(item_list_origin);
500                 item_list_origin = 0;
501         }
502
503         if (success != true) {
504                 _free_item_node(root_ptr);
505                 root_ptr = NULL;
506                 *changeCount = 0;
507         } else {
508                 *changeCount = chagned_count;
509         }
510
511         _EXTERN_FUNC_EXIT;
512
513         return root_ptr;
514 }
515
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)
517 {
518         _EXTERN_FUNC_ENTER;
519
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);
523
524         bool success = true;
525
526         int memo_time_stamp = changepoint;
527         struct memo_operation_list *item_list = 0;
528         struct memo_operation_list *item_list_origin = 0;
529
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;
533
534         item_list = memo_get_operation_list(memo_time_stamp);
535         if (item_list == NULL)
536                 goto DACI_FINISH;
537
538         item_list_origin = item_list;
539
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");
546                                 goto DACI_FINISH;
547                         }
548
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);
551
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 !!!");
556                                         free(str_item_id);
557                                         goto DACI_FINISH;
558                                 }
559
560                                 root_ptr->item_id = str_item_id;
561                                 root_ptr->next = 0;
562                                 cursor_ptr = root_ptr;
563                         } else {
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 !!!");
567                                         free(str_item_id);
568                                         success = false;
569                                         goto DACI_FINISH;
570                                 }
571
572                                 cursor_ptr->next->item_id = str_item_id;
573                                 cursor_ptr->next->next = 0;
574                                 cursor_ptr = cursor_ptr->next;
575                         }
576
577                         chagned_count++;
578                 }
579                 item_list = item_list->next;
580         }
581
582  DACI_FINISH:
583
584         /*  memory free */
585         if (item_list_origin != NULL) {
586                 memo_free_operation_list(item_list_origin);
587                 item_list_origin = 0;
588         }
589
590         if (success != true) {
591                 _free_item_node(root_ptr);
592                 root_ptr = NULL;
593                 *changeCount = 0;
594         } else {
595                 *changeCount = chagned_count;
596         }
597
598         _EXTERN_FUNC_EXIT;
599
600         return root_ptr;
601 }
602
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)
604 {
605         _EXTERN_FUNC_ENTER;
606
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);
610
611         bool success = true;
612
613         int memo_time_stamp = changepoint;
614         struct memo_operation_list *item_list = 0;
615         struct memo_operation_list *item_list_origin = 0;
616
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;
620
621         item_list = memo_get_operation_list(memo_time_stamp);
622         if (item_list == NULL)
623                 goto DACI_FINISH;
624
625         item_list_origin = item_list;
626
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");
633                                 goto DACI_FINISH;
634                         }
635
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);
638
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 !!!");
643                                         free(str_item_id);
644                                         goto DACI_FINISH;
645                                 }
646
647                                 root_ptr->item_id = str_item_id;
648                                 root_ptr->next = 0;
649                                 cursor_ptr = root_ptr;
650                         } else {
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 !!!");
654                                         free(str_item_id);
655                                         success = false;
656                                         goto DACI_FINISH;
657                                 }
658
659                                 cursor_ptr->next->item_id = str_item_id;
660                                 cursor_ptr->next->next = 0;
661                                 cursor_ptr = cursor_ptr->next;
662                         }
663
664                         chagned_count++;
665                 }
666                 item_list = item_list->next;
667         }
668
669  DACI_FINISH:
670
671         /*  memory free */
672         if (item_list_origin != NULL) {
673                 memo_free_operation_list(item_list_origin);
674                 item_list_origin = 0;
675         }
676
677         if (success != true) {
678                 _free_item_node(root_ptr);
679                 root_ptr = NULL;
680                 *changeCount = 0;
681         } else {
682                 *changeCount = chagned_count;
683         }
684
685         _EXTERN_FUNC_EXIT;
686
687         return root_ptr;
688 }
689
690 EXPORT_API int sync_agent_plugin_get_last_change_point()
691 {
692         _EXTERN_FUNC_ENTER;
693
694         _EXTERN_FUNC_EXIT;
695
696         return (int)time(0);
697 }
698
699 EXPORT_API void sync_agent_plugin_start_listening_change_noti(void *data)
700 {
701         _EXTERN_FUNC_ENTER;
702
703         memo_subscribe_change(_process_memo_change, 0);
704
705         _EXTERN_FUNC_EXIT;
706 }
707
708 EXPORT_API void sync_agent_plugin_set_callback_add_item(sync_agent_add_item_cb_plugin callback)
709 {
710         _EXTERN_FUNC_ENTER;
711
712         callback_add_item = callback;
713
714         _EXTERN_FUNC_EXIT;
715 }
716
717 EXPORT_API void sync_agent_plugin_set_callback_delete_item(sync_agent_del_item_cb_plugin callback)
718 {
719         _EXTERN_FUNC_ENTER;
720
721         callback_del_item = callback;
722
723         _EXTERN_FUNC_EXIT;
724 }
725
726 EXPORT_API void sync_agent_plugin_set_callback_update_item(sync_agent_update_item_cb_plugin callback)
727 {
728         _EXTERN_FUNC_ENTER;
729
730         callback_update_item = callback;
731
732         _EXTERN_FUNC_EXIT;
733 }
734
735 EXPORT_API void sync_agent_plugin_set_callback_get_account_id_list(sync_agent_get_account_id_list_cb_plugin callback)
736 {
737         _EXTERN_FUNC_ENTER;
738
739         callback_get_accountidlist = callback;
740
741         _EXTERN_FUNC_EXIT;
742 }
743
744 EXPORT_API void sync_agent_plugin_set_callback_delete_child_item(sync_agent_del_child_item_cb_plugin callback)
745 {
746         _EXTERN_FUNC_ENTER;
747         callback_delete_child_item = callback;
748         _EXTERN_FUNC_EXIT;
749 }
750
751 /********************************** static function *******************************************/
752
753 static void _free_item_node(sync_agent_plugin_item_node_s * node)
754 {
755         if (node != NULL) {
756                 if (node->item_id != NULL) {
757                         free(node->item_id);
758                         node->item_id = NULL;
759                 }
760                 if (node->next != NULL) {
761                         _free_item_node(node->next);
762                         node->next = NULL;
763                 }
764
765                 free(node);
766         }
767 }
768
769 static void _set_is_noti_from_me(int set_flag)
770 {
771         _INNER_FUNC_ENTER;
772
773         if (pthread_mutex_lock(&lockx)) {
774                 _DEBUG_TRACE("[da_memo_plugIn] pthread_mutex_lock error");
775         }
776
777         is_noti_from_me = set_flag;
778
779         if (pthread_mutex_unlock(&lockx)) {
780                 _DEBUG_TRACE("[da_memo_plugIn] pthread_mutex_unlock error");
781         }
782
783         _INNER_FUNC_EXIT;
784 }
785
786 static int __get_is_noti_from_me()
787 {
788         _INNER_FUNC_ENTER;
789
790         _INNER_FUNC_EXIT;
791
792         return is_noti_from_me;
793 }
794
795 void _process_memo_change(void *data)
796 {
797         _INNER_FUNC_ENTER;
798
799         _DEBUG_TRACE("[da_memo_plugIn] detected memo storage changed!!\n");
800
801         int from_Me = __get_is_noti_from_me();
802         if (from_Me == 1) {
803                 _set_is_noti_from_me(0);
804                 _DEBUG_TRACE("[da_memo_plugIn] This noti is from Me!! so will be ignored!!\n");
805                 return;
806         }
807
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 );
813         }
814
815         _INNER_FUNC_EXIT;
816 }
817
818 static void *__rutine_memo_change(void *data)
819 {
820         _INNER_FUNC_ENTER;
821
822         _DEBUG_VERBOSE("[da_memo_plugIn] Start __rutine_memo_change (create thread)\n");
823
824         int memo_time_stamp = get_memo_time_stamp();
825
826         int i = 0;
827         int count = 0;
828         int result = 0;
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;
833
834         accountList = callback_get_accountidlist(FW_MEMO, &count);
835         if (accountList == NULL)
836                 goto DACI_FINISH;
837
838         item_list = memo_get_operation_list(memo_time_stamp);
839         if (item_list == NULL)
840                 goto DACI_FINISH;
841
842         item_list_origin = item_list;
843
844         while ((item_list != 0) && (item_list->id > -1)) {
845                 str_item_id = g_strdup_printf("%d", item_list->id);
846
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);
850
851                         switch (item_list->operation) {
852                         case MEMO_OPERATION_ADD:
853                                 {
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);
857                                         if (!result) {
858                                                 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_add_item() \n");
859                                                 goto DACI_FINISH;
860                                         }
861                                 }
862                                 break;
863                         case MEMO_OPERATION_UPDATE:
864                                 {
865                                         _DEBUG_VERBOSE("[da_memo_plugIn] MEMO_OPERATION_UPDATE\n");
866                                         result = callback_update_item(accountList[i], i, str_item_id, NULL, FW_MEMO);
867                                         if (!result) {
868                                                 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_update_item() \n");
869                                                 goto DACI_FINISH;
870                                         }
871                                 }
872                                 break;
873                         case MEMO_OPERATION_DELETE:
874                                 {
875                                         _DEBUG_VERBOSE("[da_memo_plugIn] MEMO_OPERATION_DELETE\n");
876                                         result = callback_del_item(accountList[i], i, str_item_id, FW_MEMO);
877                                         if (!result) {
878                                                 _DEBUG_ERROR("[da_memo_plugIn] Failed to call callback_del_item() \n");
879                                                 goto DACI_FINISH;
880                                         }
881                                 }
882                                 break;
883                         default:
884                                 _DEBUG_VERBOSE("[da_memo_plugIn] Another Memo Change Noti\n");
885                                 break;
886                         }
887                 }
888                 item_list = item_list->next;
889                 free(str_item_id);
890                 str_item_id = 0;
891         }
892
893  DACI_FINISH:
894
895         /*  memory free */
896         if (item_list_origin != NULL) {
897                 memo_free_operation_list(item_list_origin);
898                 item_list_origin = 0;
899         }
900
901         if (accountList != NULL) {
902                 free(accountList);
903                 accountList = 0;
904         }
905
906         if (str_item_id != NULL) {
907                 free(str_item_id);
908                 str_item_id = 0;
909         }
910
911         _DEBUG_VERBOSE("[da_memo_plugIn] End __rutine_memo_change (create thread)\n");
912
913         _INNER_FUNC_EXIT;
914
915         return 0;
916 }
917
918 static sync_agent_da_return_e _convert_service_error_to_common_error(int err)
919 {
920         _INNER_FUNC_ENTER;
921
922         sync_agent_da_return_e ret = SYNC_AGENT_DA_SUCCESS;
923         _DEBUG_TRACE("[da_memo_plugIn] Error Code : %d\n", err);
924
925         switch (err) {
926         default:
927                 ret = SYNC_AGENT_DA_ERRORS;
928                 break;
929         }
930
931         _INNER_FUNC_EXIT;
932
933         return ret;
934 }
935
936 static int _get_digit(int new_id)
937 {
938         _INNER_FUNC_ENTER;
939
940         int temp_id = 0;
941         int count = 1;
942
943         temp_id = new_id;
944
945         while (1) {
946                 if (temp_id >= 10) {
947                         temp_id /= 10;
948                         count++;
949                 } else
950                         break;
951         }
952
953         _INNER_FUNC_EXIT;
954
955         return count;
956 }