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