b0f0f880bbb1ab250a6a2e5aad6428bbc7fff8b3
[platform/core/system/sync-agent.git] / src / framework / event / oma_ds_api.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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <glib.h>
23 #include <dbus/dbus-glib.h>
24
25 #ifndef SYNC_AGENT_LOG
26 #undef LOG_TAG
27 #define LOG_TAG "OMADS_CLIENT"
28 #endif
29
30 //#include <sync_agent.h>
31 #include "event/event.h"
32 #include "utility/sync_util.h"
33 #include "initialization/initialization.h"
34
35 #ifndef EXPORT_API
36 #define EXPORT_API __attribute__ ((visibility("default")))
37 #endif
38
39 #define OMA_DS_CONFIG_FILE      "/usr/share/oma-ds-cfg/omadsUI_fw_config.xml"
40
41 #define SYNC_AGENT_DS_API_RESULT_FAILURE        0
42 #define SYNC_AGENT_DS_API_RESULT_SUCCESS        1
43
44 #define SYNC_TYPE_SLOW_SYNC_STR "Full"
45 #define SYNC_TYPE_TWO_WAY_STR "Update both"
46 #define SYNC_TYPE_ONE_WAY_FROM_CLIENT_STR "Update to server"
47 #define SYNC_TYPE_ONE_WAY_FROM_SERVER_STR "Update to phone"
48 #define SYNC_TYPE_REFRESH_FROM_SERVER_STR "Refresh from server"
49 #define SYNC_TYPE_REFRESH_FROM_CLIENT_STR "Refresh from phone"
50
51 #define SYNC_MODE_MANUAL_STR "Manual"
52 #define SYNC_MODE_PUSH_STR "Push"
53 #define SYNC_MODE_PERIODIC_STR "Periodic"
54
55 #define SYNC_INTERVAL_5_MINUTES "5 minutes"
56 #define SYNC_INTERVAL_15_MINUTES "15 minutes"
57 #define SYNC_INTERVAL_1_HOUR "1 hour"
58 #define SYNC_INTERVAL_4_HOURS "4 hours"
59 #define SYNC_INTERVAL_12_HOURS "12 hours"
60 #define SYNC_INTERVAL_1_DAY "1 day"
61 #define SYNC_INTERVAL_1_WEEK "1 week"
62 #define SYNC_INTERVAL_1_MONTH "1 month"
63
64 #define SRC_URI_CONTACT_STR "Contacts"
65 #define SRC_URI_CALENDAR_STR "Organizer"
66
67 typedef enum {
68         SYNC_AGENT_DS_ADD_PROFILE = 1,
69         SYNC_AGENT_DS_UPDATE_PROFILE = 2,
70         SYNC_AGENT_DS_DELETE_PROFILE = 3,
71         SYNC_AGENT_DS_START_SYNC = 4,
72         SYNC_AGENT_DS_STOP_SYNC = 5,
73         SYNC_AGENT_DS_GET_PROFILE = 6,
74         SYNC_AGENT_DS_GET_SYNC_CATEGORY = 7,
75         SYNC_AGENT_DS_GET_SYNC_STATISTICS = 8,
76         SYNC_AGENT_DS_GET_ALL_PROFILES = 9
77 } sync_agent_ds_event_e;
78
79 typedef enum {
80         SYNC_AGENT_DS_SYNC_CONTACTS = 0,
81         SYNC_AGENT_DS_SYNC_SCHEDULE = 1,
82         SYNC_AGENT_DS_SYNC_MEMO = 2
83 } sync_agent_ds_sync_e;
84
85 static gboolean _get_sync_category(char *profile_dir_name, int service_type, sync_agent_ds_service_info ** service)
86 {
87         _INNER_FUNC_ENTER;
88
89         int event_type = SYNC_AGENT_DS_GET_SYNC_CATEGORY;
90         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
91
92         // check mandatory parameter
93         retvm_if(profile_dir_name == NULL, FALSE, "profile_dir_name is null!!");
94         retvm_if(service_type < 0, FALSE, "service_type is wrong!!");
95
96         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
97         sync_agent_event_data_s *request_event = NULL;
98         sync_agent_event_data_s *response_event = NULL;
99
100         /* create empty event packet */
101         request_event = sync_agent_create_event(event_type);
102         if (request_event == NULL) {
103                 _DEBUG_ERROR("event is NULL");
104                 _EXTERN_FUNC_EXIT;
105                 return FALSE;
106         }
107
108         /* add request parameter to event packet */
109         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_dir_name);
110         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &service_type);
111
112         /* send event request to ds agent daemon, waiting for response */
113         response_event = sync_agent_send_event(request_event, &error);
114         if (error != SYNC_AGENT_EVENT_SUCCESS) {
115                 _DEBUG_ERROR("error = %d", error);
116                 sync_agent_free_event(request_event);
117                 _EXTERN_FUNC_EXIT;
118                 return FALSE;
119         }
120         if (response_event == NULL) {
121                 _DEBUG_ERROR("response_event is null!!");
122                 sync_agent_free_event(request_event);
123                 _EXTERN_FUNC_EXIT;
124                 return FALSE;
125         }
126
127         *service = (sync_agent_ds_service_info *) calloc(1, sizeof(sync_agent_ds_service_info));
128         if (*service == NULL) {
129                 _DEBUG_ERROR("calloc failed");
130                 sync_agent_free_event(request_event);
131                 sync_agent_free_event_data(response_event);
132                 return FALSE;
133         }
134
135         /* get response parameter from event packet */
136
137         sync_agent_get_event_data_param_int(response_event, &api_result);
138         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
139                 _DEBUG_ERROR("api_result = failed");
140                 sync_agent_free_event(request_event);
141                 sync_agent_free_event_data(response_event);
142                 _EXTERN_FUNC_EXIT;
143                 return SYNC_AGENT_DS_FAIL;
144         }
145
146         sync_agent_get_event_data_param_int(response_event, &((*service)->enabled));
147         sync_agent_get_event_data_param_int(response_event, (int*) &((*service)->src_uri));
148         sync_agent_get_event_data_param_str(response_event, &((*service)->tgt_uri));
149         sync_agent_get_event_data_param_str(response_event, &((*service)->id));
150         sync_agent_get_event_data_param_str(response_event, &((*service)->password));
151
152         _DEBUG_TRACE("enabled = %d", (*service)->enabled);
153         _DEBUG_TRACE("src_uri = %d", (*service)->src_uri);
154         _DEBUG_TRACE("tgt_uri = %s", (*service)->tgt_uri);
155         _DEBUG_TRACE("id = %s", (*service)->id);
156         _DEBUG_TRACE("password = %s", (*service)->password);
157
158         /* free request & response event */
159         sync_agent_free_event(request_event);
160         sync_agent_free_event_data(response_event);
161
162         _INNER_FUNC_EXIT;
163
164         return TRUE;
165 }
166
167 static gboolean _get_sync_statistics(char *profile_dir_name, int service_type, sync_agent_ds_statistics_info ** statistics)
168 {
169         _INNER_FUNC_ENTER;
170
171         int event_type = SYNC_AGENT_DS_GET_SYNC_STATISTICS;
172         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
173
174         // check mandatory parameter
175         retvm_if(profile_dir_name == NULL, FALSE, "profile_dir_name is null!!");
176         retvm_if(service_type < 0, FALSE, "service_type is wrong!!");
177
178         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
179         sync_agent_event_data_s *request_event = NULL;
180         sync_agent_event_data_s *response_event = NULL;
181
182         char *statistics_cat = NULL;
183
184         /* create empty event packet */
185         request_event = sync_agent_create_event(event_type);
186         if (request_event == NULL) {
187                 _DEBUG_ERROR("event is NULL");
188                 _EXTERN_FUNC_EXIT;
189                 return FALSE;
190         }
191
192         /* add request parameter to event packet */
193         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_dir_name);
194         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &service_type);
195
196         /* send event request to ds agent daemon, waiting for response */
197         response_event = sync_agent_send_event(request_event, &error);
198         if (error != SYNC_AGENT_EVENT_SUCCESS) {
199                 _DEBUG_ERROR("error = %d", error);
200                 sync_agent_free_event(request_event);
201                 _EXTERN_FUNC_EXIT;
202                 return FALSE;
203         }
204         if (response_event == NULL) {
205                 _DEBUG_ERROR("response_event is null!!");
206                 sync_agent_free_event(request_event);
207                 _EXTERN_FUNC_EXIT;
208                 return FALSE;
209         }
210
211         *statistics = (sync_agent_ds_statistics_info *) calloc(1, sizeof(sync_agent_ds_statistics_info));
212         if (*statistics == NULL) {
213                 _DEBUG_ERROR("calloc failed");
214                 sync_agent_free_event(request_event);
215                 sync_agent_free_event_data(response_event);
216                 return 0;
217         }
218
219         /* get response parameter from event packet */
220
221         sync_agent_get_event_data_param_int(response_event, &api_result);
222         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
223                 _DEBUG_ERROR("api_result = failed");
224                 sync_agent_free_event(request_event);
225                 sync_agent_free_event_data(response_event);
226                 _EXTERN_FUNC_EXIT;
227                 return SYNC_AGENT_DS_FAIL;
228         }
229
230         if (service_type == SYNC_AGENT_DS_SYNC_CONTACTS)
231                 statistics_cat = "contacts";
232         else if (service_type == SYNC_AGENT_DS_SYNC_SCHEDULE)
233                 statistics_cat = "schedule";
234         else if (service_type == SYNC_AGENT_DS_SYNC_MEMO)
235                 statistics_cat = "memo";
236
237         sync_agent_get_event_data_param_str(response_event, &((*statistics)->dbsynced));
238         sync_agent_get_event_data_param_int(response_event, &((*statistics)->last_session_time));
239         sync_agent_get_event_data_param_int(response_event, &((*statistics)->server2client_total));
240         sync_agent_get_event_data_param_int(response_event, &((*statistics)->server2client_nrofadd));
241         sync_agent_get_event_data_param_int(response_event, &((*statistics)->server2client_nrofdelete));
242         sync_agent_get_event_data_param_int(response_event, &((*statistics)->server2client_nrofreplace));
243         sync_agent_get_event_data_param_int(response_event, &((*statistics)->client2server_total));
244         sync_agent_get_event_data_param_int(response_event, &((*statistics)->client2server_nrofadd));
245         sync_agent_get_event_data_param_int(response_event, &((*statistics)->client2server_nrofdelete));
246         sync_agent_get_event_data_param_int(response_event, &((*statistics)->client2server_nrofreplace));
247
248         _DEBUG_TRACE("%s dbSynced = %s", statistics_cat, (*statistics)->dbsynced);
249         _DEBUG_TRACE("%s lastSessionTime = %d", statistics_cat, (*statistics)->last_session_time);
250         _DEBUG_TRACE("%s server2Client_Total = %d", statistics_cat, (*statistics)->server2client_total);
251         _DEBUG_TRACE("%s server2Client_NrOrAdd = %d", statistics_cat, (*statistics)->server2client_nrofadd);
252         _DEBUG_TRACE("%s server2Client_NrOfDelete = %d", statistics_cat, (*statistics)->server2client_nrofdelete);
253         _DEBUG_TRACE("%s server2Client_NrOfReplace = %d", statistics_cat, (*statistics)->server2client_nrofreplace);
254         _DEBUG_TRACE("%s client2Server_Total = %d", statistics_cat, (*statistics)->client2server_total);
255         _DEBUG_TRACE("%s client2Server_NrOfAdd = %d", statistics_cat, (*statistics)->client2server_nrofadd);
256         _DEBUG_TRACE("%s client2Server_NrOfDelete = %d", statistics_cat, (*statistics)->client2server_nrofdelete);
257         _DEBUG_TRACE("%s client2Server_NrOfReplace = %d", statistics_cat, (*statistics)->client2server_nrofreplace);
258
259         /* free request & response event */
260         sync_agent_free_event(request_event);
261         sync_agent_free_event_data(response_event);
262
263         _INNER_FUNC_EXIT;
264
265         return TRUE;
266 }
267
268 static char * _convert_sync_mode_str(sync_agent_ds_sync_mode_e sync_mode)
269 {
270         _INNER_FUNC_ENTER;
271
272         char* sync_mode_str = NULL;
273
274         _DEBUG_INFO("sync_mode : [%d]",sync_mode);
275         switch(sync_mode) {
276                 case SYNC_AGENT_SYNC_MODE_MANUAL:
277                         sync_mode_str = SYNC_MODE_MANUAL_STR;
278                         break;
279                 case SYNC_AGENT_SYNC_MODE_PERIODIC:
280                         sync_mode_str = SYNC_MODE_PERIODIC_STR;
281                         break;
282                 case SYNC_AGENT_SYNC_MODE_PUSH:
283                         sync_mode_str = SYNC_MODE_PUSH_STR;
284                         break;
285                 default:
286                         break;
287         }
288
289         _INNER_FUNC_EXIT;
290         return sync_mode_str;
291 }
292
293 static char * _convert_sync_type_str(sync_agent_ds_sync_type_e sync_type)
294 {
295         _INNER_FUNC_ENTER;
296
297         char* sync_type_str = NULL;
298
299         _DEBUG_INFO("sync_type : [%d]",sync_type);
300         switch(sync_type) {
301                 case SYNC_AGENT_SYNC_TYPE_FULL_SYNC:
302                         sync_type_str = SYNC_TYPE_SLOW_SYNC_STR;
303                         break;
304                 case SYNC_AGENT_SYNC_TYPE_UPDATE_BOTH:
305                         sync_type_str = SYNC_TYPE_TWO_WAY_STR;
306                         break;
307                 case SYNC_AGENT_SYNC_TYPE_UPDATE_TO_SERVER:
308                         sync_type_str = SYNC_TYPE_ONE_WAY_FROM_CLIENT_STR;
309                         break;
310                 case SYNC_AGENT_SYNC_TYPE_UPDATE_TO_PHONE:
311                         sync_type_str = SYNC_TYPE_ONE_WAY_FROM_SERVER_STR;
312                         break;
313                 case SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_SERVER:
314                         sync_type_str = SYNC_TYPE_REFRESH_FROM_SERVER_STR;
315                         break;
316                 case SYNC_AGENT_SYNC_TYPE_REFRESH_FROM_PHONE:
317                         sync_type_str = SYNC_TYPE_REFRESH_FROM_CLIENT_STR;
318                         break;
319                 default:
320                         break;
321         }
322
323         _INNER_FUNC_EXIT;
324         return sync_type_str;
325 }
326
327 static char * _convert_sync_interval_str(sync_agent_ds_sync_interval_e interval)
328 {
329         _INNER_FUNC_ENTER;
330
331         char* interval_str = NULL;
332
333         _DEBUG_INFO("interval : [%d]",interval);
334         switch(interval) {
335                 case SYNC_AGENT_SYNC_INTERVAL_5_MINUTES:
336                         interval_str = SYNC_INTERVAL_5_MINUTES;
337                         break;
338                 case SYNC_AGENT_SYNC_INTERVAL_15_MINUTES:
339                         interval_str = SYNC_INTERVAL_15_MINUTES;
340                         break;
341                 case SYNC_AGENT_SYNC_INTERVAL_1_HOUR:
342                         interval_str = SYNC_INTERVAL_1_HOUR;
343                         break;
344                 case SYNC_AGENT_SYNC_INTERVAL_4_HOURS:
345                         interval_str = SYNC_INTERVAL_4_HOURS;
346                         break;
347                 case SYNC_AGENT_SYNC_INTERVAL_12_HOURS:
348                         interval_str = SYNC_INTERVAL_12_HOURS;
349                         break;
350                 case SYNC_AGENT_SYNC_INTERVAL_1_DAY:
351                         interval_str = SYNC_INTERVAL_1_DAY;
352                         break;
353                 case SYNC_AGENT_SYNC_INTERVAL_1_WEEK:
354                         interval_str = SYNC_INTERVAL_1_WEEK;
355                         break;
356                 case SYNC_AGENT_SYNC_INTERVAL_1_MONTH:
357                         interval_str = SYNC_INTERVAL_1_MONTH;
358                         break;
359                 default:
360                         break;
361         }
362
363         _INNER_FUNC_EXIT;
364         return interval_str;
365 }
366
367 static char * _convert_src_uri_str(sync_agent_ds_src_uri_e src_uri)
368 {
369         _INNER_FUNC_ENTER;
370
371         char* src_uri_str = NULL;
372
373         _DEBUG_INFO("src_uri : [%d]",src_uri);
374         switch(src_uri) {
375                 case SYNC_AGENT_SRC_URI_CONTACT:
376                         src_uri_str = SRC_URI_CONTACT_STR;
377                         break;
378                 case SYNC_AGENT_SRC_URI_CALENDAR:
379                         src_uri_str = SRC_URI_CALENDAR_STR;
380                         break;
381                 default:
382                         break;
383         }
384
385         _INNER_FUNC_EXIT;
386         return src_uri_str;
387 }
388
389 static void _free_sync_category_info(sync_agent_ds_service_info * data)
390 {
391         g_free(data->tgt_uri);
392         g_free(data->id);
393         g_free(data->password);
394 }
395
396 static gboolean _is_existing_profile(void)
397 {
398         _INNER_FUNC_ENTER;
399
400         int event_type = SYNC_AGENT_DS_GET_ALL_PROFILES;
401         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
402         sync_agent_event_data_s *request_event = NULL;
403         sync_agent_event_data_s *response_event = NULL;
404
405         int profile_count = 0;
406
407         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
408
409         /* create empty event packet */
410         request_event = sync_agent_create_event(event_type);
411         if (request_event == NULL) {
412                 _DEBUG_ERROR("event is NULL");
413                 _INNER_FUNC_EXIT;
414                 return SYNC_AGENT_DS_FAIL;
415         }
416
417         /* send event request to ds agent daemon, waiting for response */
418         response_event = sync_agent_send_event(request_event, &error);
419         if (error != SYNC_AGENT_EVENT_SUCCESS) {
420                 _DEBUG_ERROR("error = %d", error);
421                 sync_agent_free_event(request_event);
422                 _INNER_FUNC_EXIT;
423                 return SYNC_AGENT_DS_FAIL;
424         }
425         if (response_event == NULL) {
426                 _DEBUG_ERROR("response_event is null!!");
427                 sync_agent_free_event(request_event);
428                 _INNER_FUNC_EXIT;
429                 return SYNC_AGENT_DS_FAIL;
430         }
431
432         sync_agent_get_event_data_param_int(response_event, &api_result);
433         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
434                 _DEBUG_ERROR("api_result = failed");
435                 sync_agent_free_event(request_event);
436                 sync_agent_free_event_data(response_event);
437                 _INNER_FUNC_EXIT;
438                 return SYNC_AGENT_DS_FAIL;
439         }
440
441         sync_agent_get_event_data_param_int(response_event, &profile_count);
442         _DEBUG_INFO("profile_count = %d", profile_count);
443
444         if (profile_count > 0) {
445                 sync_agent_free_event(request_event);
446                 sync_agent_free_event_data(response_event);
447
448                 _INNER_FUNC_EXIT;
449                 return TRUE;
450         }
451
452         /* free request & response event */
453         sync_agent_free_event(request_event);
454         sync_agent_free_event_data(response_event);
455
456         _INNER_FUNC_EXIT;
457
458         return FALSE;
459 }
460
461 static int _launch_omads_agent(void)
462 {
463         _INNER_FUNC_ENTER;
464         GError *error = NULL;
465
466         DBusGConnection *connection = NULL;
467         DBusGProxy *dbus_proxy = NULL;
468
469         g_type_init();
470
471         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
472         if (error != NULL) {
473                 _DEBUG_ERROR("Connecting to system bus failed: %s\n", error->message);
474                 g_error_free(error);
475                 _INNER_FUNC_EXIT;
476                 return -1;
477         }
478
479         dbus_proxy = dbus_g_proxy_new_for_name(connection, "com.samsung.omadsagent",
480                                                "/com/samsung/omadsagent", "com.samsung.omadsagent");
481
482         _DEBUG_INFO("dbus_proxy %x", dbus_proxy);
483
484         //dbus_g_proxy_call_no_reply(dbus_proxy, "Hello_Agent", G_TYPE_INVALID);
485         dbus_g_proxy_call(dbus_proxy, "Hello_Agent", &error, G_TYPE_INVALID, G_TYPE_INVALID);
486
487         g_object_unref(dbus_proxy);
488         dbus_g_connection_unref(connection);
489
490         _INNER_FUNC_EXIT;
491         return 0;
492 }
493
494 static int _kill_omads_agent(void)
495 {
496         _INNER_FUNC_ENTER;
497         GError *error = NULL;
498
499         DBusGConnection *connection = NULL;
500         DBusGProxy *dbus_proxy = NULL;
501         gboolean isExisting = FALSE;
502
503         isExisting = _is_existing_profile();
504
505         if(isExisting) {
506                 _DEBUG_INFO("Existing profile  !!");
507
508                 _INNER_FUNC_EXIT;
509                 return 0;
510         }
511
512         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
513         if (error != NULL) {
514                 _DEBUG_ERROR("Connecting to system bus failed: %s\n", error->message);
515                 g_error_free(error);
516                 _INNER_FUNC_EXIT;
517                 return -1;
518         }
519
520         dbus_proxy = dbus_g_proxy_new_for_name(connection, "com.samsung.omadsagent",
521                                                "/com/samsung/omadsagent", "com.samsung.omadsagent");
522
523         _DEBUG_INFO("dbus_proxy %x", dbus_proxy);
524
525         dbus_g_proxy_call_no_reply(dbus_proxy, "Goodbye_Agent", G_TYPE_INVALID);
526
527         g_object_unref(dbus_proxy);
528         dbus_g_connection_unref(connection);
529
530         _INNER_FUNC_EXIT;
531         return 0;
532 }
533
534 EXPORT_API sync_agent_ds_error_e sync_agent_ds_init()
535 {
536         _EXTERN_FUNC_ENTER;
537
538         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
539         sync_agent_init_error_e init_error = SYNC_AGENT_INIT_SUCCESS;
540         int ret = -1;
541
542         ret = _launch_omads_agent();
543
544         if (ret < 0 ) {
545                 _DEBUG_ERROR("_launch_omads_agent() failed !!");
546                 result = SYNC_AGENT_DS_FAIL;
547
548                 _EXTERN_FUNC_EXIT;
549                 return result;
550         }
551
552         init_error = sync_agent_init(OMA_DS_CONFIG_FILE);
553
554         if (init_error != SYNC_AGENT_INIT_SUCCESS) {
555                 _DEBUG_ERROR("ds init failed");
556                 result = SYNC_AGENT_DS_FAIL;
557         }
558
559         sync_agent_event_error_e err = sync_agent_run_noti_listener("omads");
560         if (err != SYNC_AGENT_EVENT_SUCCESS) {
561                 _DEBUG_ERROR("RUN NOTILISTNER is failed");
562                 result = SYNC_AGENT_DS_FAIL;
563         }
564
565         _EXTERN_FUNC_EXIT;
566
567         return result;
568 }
569
570 EXPORT_API sync_agent_ds_error_e sync_agent_ds_create_profile_info(ds_profile_h * profile_h)
571 {
572         _EXTERN_FUNC_ENTER;
573
574         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
575         sync_agent_ds_profile_info *profile_info = NULL;
576
577         // allocate memory for ds profile info
578         profile_info = (sync_agent_ds_profile_info *) calloc(1, sizeof(sync_agent_ds_profile_info));
579         if (profile_info == NULL) {
580                 _DEBUG_ERROR("calloc failed !!");
581                 return SYNC_AGENT_DS_FAIL;
582         }
583         // pass memory pointer to ds profile handle
584         *profile_h = profile_info;
585
586         _EXTERN_FUNC_EXIT;
587
588         return result;
589 }
590
591 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_profile_dir_name(ds_profile_h profile_h, char *profile_dir_name)
592 {
593         _EXTERN_FUNC_ENTER;
594
595         // check mandatory parameter
596         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
597         retvm_if(profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
598
599         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
600         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
601
602         // set profile name
603         profile_info->profile_dir_name = g_strdup(profile_dir_name);
604
605         if (profile_info->profile_dir_name == NULL) {
606                 _DEBUG_ERROR("g_strdup failed !!");
607                 return SYNC_AGENT_DS_FAIL;
608         }
609
610         _EXTERN_FUNC_EXIT;
611
612         return result;
613 }
614
615 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_profile_name(ds_profile_h profile_h, char *profile_name)
616 {
617         _EXTERN_FUNC_ENTER;
618
619         // check mandatory parameter
620         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
621         retvm_if(profile_name == NULL, SYNC_AGENT_DS_FAIL, "profile_name is null!!");
622
623         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
624         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
625
626         // set profile name
627         profile_info->profile_name = g_strdup(profile_name);
628
629         if (profile_info->profile_name == NULL) {
630                 _DEBUG_ERROR("g_strdup failed !!");
631                 return SYNC_AGENT_DS_FAIL;
632         }
633
634         _EXTERN_FUNC_EXIT;
635
636         return result;
637 }
638
639 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_server_info(ds_profile_h profile_h, char *addr, char *id, char *password)
640 {
641         _EXTERN_FUNC_ENTER;
642
643         // check mandatory parameter
644         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
645         retvm_if(addr == NULL, SYNC_AGENT_DS_FAIL, "addr is null!!");
646         retvm_if(id == NULL, SYNC_AGENT_DS_FAIL, "id is null!!");
647         retvm_if(password == NULL, SYNC_AGENT_DS_FAIL, "password is null!!");
648
649         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
650         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
651         sync_agent_ds_server_info *server_info = &profile_info->server_info;
652
653         // set server info
654         server_info->addr = g_strdup(addr);
655         server_info->id = g_strdup(id);
656         server_info->password = g_strdup(password);
657
658         if ((server_info->addr == NULL)
659             || (server_info->id == NULL)
660             || (server_info->password == NULL)) {
661                 _DEBUG_ERROR("g_strdup failed !!");
662                 return SYNC_AGENT_DS_FAIL;
663         }
664
665         _EXTERN_FUNC_EXIT;
666
667         return result;
668 }
669
670 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_sync_info(ds_profile_h profile_h, sync_agent_ds_sync_mode_e sync_mode, sync_agent_ds_sync_type_e sync_type, sync_agent_ds_sync_interval_e interval)
671 {
672         _EXTERN_FUNC_ENTER;
673
674         // check mandatory parameter
675         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
676         retvm_if(sync_mode < 0, SYNC_AGENT_DS_FAIL, "sync_mode is invalid!!");
677         retvm_if(sync_type < 0, SYNC_AGENT_DS_FAIL, "sync_type is invalid!!");
678         retvm_if(interval < 0, SYNC_AGENT_DS_FAIL, "interval is invalid!!");
679
680         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
681         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
682         sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
683
684         char* sync_mode_str = NULL;
685         char* sync_type_str = NULL;
686         char* interval_str = NULL;
687
688         sync_mode_str = _convert_sync_mode_str(sync_mode);
689         if (sync_mode_str == NULL) {
690                 _DEBUG_ERROR("_convert_sync_mode_str failed !!");
691                 _EXTERN_FUNC_EXIT;
692                 return SYNC_AGENT_DS_FAIL;
693         }
694
695         sync_type_str = _convert_sync_type_str(sync_type);
696         if (sync_type_str == NULL) {
697                 _DEBUG_ERROR("_convert_sync_type_str failed !!");
698                 _EXTERN_FUNC_EXIT;
699                 return SYNC_AGENT_DS_FAIL;
700         }
701
702         interval_str = _convert_sync_interval_str(interval);
703         if (interval_str == NULL) {
704                 _DEBUG_ERROR("_convert_sync_interval_str failed !!");
705                 _EXTERN_FUNC_EXIT;
706                 return SYNC_AGENT_DS_FAIL;
707         }
708
709         // set server info
710         sync_info->sync_mode = sync_mode;
711         sync_info->sync_type = sync_type;
712         sync_info->interval = interval;
713
714         _EXTERN_FUNC_EXIT;
715
716         return result;
717 }
718
719 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_sync_service_info(ds_profile_h profile_h, sync_agent_ds_service_type_e service_type, int enabled, sync_agent_ds_src_uri_e src_uri, char *tgt_uri, char *id, char *password)
720 {
721         _EXTERN_FUNC_ENTER;
722
723         // check mandatory parameter
724         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
725         retvm_if(src_uri < 0, SYNC_AGENT_DS_FAIL, "src_uri is invalid!!");
726
727         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
728         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
729         sync_agent_ds_service_info *category_info = NULL;
730         char* src_uri_str = NULL;
731
732         src_uri_str = _convert_src_uri_str(src_uri);
733         if (src_uri_str == NULL) {
734                 _DEBUG_ERROR("_get_src_uri_string failed !!");
735                 _EXTERN_FUNC_EXIT;
736                 return SYNC_AGENT_DS_FAIL;
737         }
738
739         // allocate memory for sync_category
740         category_info = (sync_agent_ds_service_info *) calloc(1, sizeof(sync_agent_ds_service_info));
741         retvm_if(category_info == NULL, SYNC_AGENT_DS_FAIL, "calloc failed");
742         category_info->service_type = service_type;
743         category_info->enabled = enabled;
744         category_info->src_uri = src_uri;
745         category_info->tgt_uri = g_strdup(tgt_uri);
746
747         profile_info->service_list = g_list_append(profile_info->service_list, category_info);
748
749         _EXTERN_FUNC_EXIT;
750
751         return result;
752 }
753
754 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_profile_dir_name(ds_profile_h profile_h, char **profile_dir_name)
755 {
756         _EXTERN_FUNC_ENTER;
757
758         // check mandatory parameter
759         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
760
761         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
762
763         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
764
765         *profile_dir_name = g_strdup(profile_info->profile_dir_name);
766
767         if (*profile_dir_name == NULL) {
768                 _DEBUG_ERROR("g_strdup failed !!");
769                 return SYNC_AGENT_DS_FAIL;
770         }
771
772         _DEBUG_INFO("profile_dir_name = %s", *profile_dir_name);
773
774         _EXTERN_FUNC_EXIT;
775
776         return result;
777 }
778
779 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_profile_name(ds_profile_h profile_h, char **profile_name)
780 {
781         _EXTERN_FUNC_ENTER;
782
783         // check mandatory parameter
784         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
785
786         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
787
788         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
789
790         *profile_name = g_strdup(profile_info->profile_name);
791
792         if (*profile_name == NULL) {
793                 _DEBUG_ERROR("g_strdup failed !!");
794                 return SYNC_AGENT_DS_FAIL;
795         }
796
797         _DEBUG_INFO("profile_name = %s", *profile_name);
798
799         _EXTERN_FUNC_EXIT;
800
801         return result;
802 }
803
804 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_server_info(ds_profile_h profile_h, sync_agent_ds_server_info * server_info)
805 {
806         _EXTERN_FUNC_ENTER;
807
808         // check mandatory parameter
809         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
810         retvm_if(server_info == NULL, SYNC_AGENT_DS_FAIL, "server_info is null!!");
811
812         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
813         sync_agent_ds_server_info *ds_server_info = &profile_info->server_info;
814
815         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
816
817         server_info->addr = g_strdup(ds_server_info->addr);
818         server_info->id = g_strdup(ds_server_info->id);
819         server_info->password = g_strdup(ds_server_info->password);
820
821         if ((server_info->addr == NULL)
822             || (server_info->id == NULL)
823             || (server_info->password == NULL)) {
824                 _DEBUG_ERROR("g_strdup failed !!");
825                 return SYNC_AGENT_DS_FAIL;
826         }
827
828         _DEBUG_INFO("get_addr = %s", server_info->addr);
829         _DEBUG_INFO("get_id = %s", server_info->id);
830         _DEBUG_INFO("get_password = %s", server_info->password);
831
832         _EXTERN_FUNC_EXIT;
833
834         return result;
835 }
836
837 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_sync_info(ds_profile_h profile_h, sync_agent_ds_sync_info * sync_info)
838 {
839         _EXTERN_FUNC_ENTER;
840
841         // check mandatory parameter
842         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
843         retvm_if(sync_info == NULL, SYNC_AGENT_DS_FAIL, "sync_info is null!!");
844
845         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
846         sync_agent_ds_sync_info *ds_sync_info = &profile_info->sync_info;
847
848         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
849
850         sync_info->sync_mode = ds_sync_info->sync_mode;
851         sync_info->sync_type = ds_sync_info->sync_type;
852         sync_info->interval = ds_sync_info->interval;
853
854         _DEBUG_INFO("get_sync_mode = %d", sync_info->sync_mode);
855         _DEBUG_INFO("get_sync_type = %d", sync_info->sync_type);
856         _DEBUG_INFO("get_interval = %d", sync_info->interval);
857
858         if (sync_info->sync_mode < 0) {
859                 _DEBUG_ERROR("sync_mode is invalid !!");
860                 return SYNC_AGENT_DS_FAIL;
861         }
862
863         if (sync_info->sync_type < 0) {
864                 _DEBUG_ERROR("sync_type is invalid !!");
865                 return SYNC_AGENT_DS_FAIL;
866         }
867
868         if (sync_info->interval < 0) {
869                 _DEBUG_ERROR("interval is invalid !!");
870                 return SYNC_AGENT_DS_FAIL;
871         }
872
873         _EXTERN_FUNC_EXIT;
874
875         return result;
876 }
877
878 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_sync_service_info(ds_profile_h profile_h, GList ** category_list)
879 {
880         _EXTERN_FUNC_ENTER;
881
882         // check mandatory parameter
883         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
884
885         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
886
887         int category_count = 0;
888         int i = 0;
889         sync_agent_ds_service_info *category_info = NULL;
890
891         *category_list = g_list_copy(profile_info->service_list);
892
893         if (*category_list == NULL) {
894                 _DEBUG_ERROR("g_list_copy failed !!");
895                 return SYNC_AGENT_DS_FAIL;
896         }
897
898         category_count = g_list_length(*category_list);
899         for (; i < category_count; i++) {
900                 category_info = (sync_agent_ds_service_info *) g_list_nth_data(*category_list, i);
901
902                 _DEBUG_INFO("category[%d]->enabled = %d", i, category_info->enabled);
903                 _DEBUG_INFO("category[%d]->src_uri = %d", i, category_info->src_uri);
904                 _DEBUG_INFO("category[%d]->tgt_uri = %s", i, category_info->tgt_uri);
905                 _DEBUG_INFO("category[%d]->id = %s", i, category_info->id);
906                 _DEBUG_INFO("category[%d]->password = %s", i, category_info->password);
907         }
908
909         _EXTERN_FUNC_EXIT;
910
911         return SYNC_AGENT_DS_SUCCESS;
912 }
913
914 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_sync_statistics(ds_profile_h profile_h, GList ** statistics_list)
915 {
916         _EXTERN_FUNC_ENTER;
917
918         // check mandatory parameter
919         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
920
921         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
922         sync_agent_ds_statistics_info *statistics = NULL;
923
924         gboolean result_sync_category = FALSE;
925
926         /* get last contacts sync result */
927         result_sync_category = _get_sync_statistics(profile_info->profile_dir_name, SYNC_AGENT_DS_SYNC_CONTACTS, &statistics);
928         if (result_sync_category)
929                 *statistics_list = g_list_append(*statistics_list, statistics);
930
931         /* get last schedule sync result */
932         statistics = NULL;
933         result_sync_category = _get_sync_statistics(profile_info->profile_dir_name, SYNC_AGENT_DS_SYNC_SCHEDULE, &statistics);
934         if (result_sync_category)
935                 *statistics_list = g_list_append(*statistics_list, statistics);
936
937         /* get last memo sync result */
938         statistics = NULL;
939         result_sync_category = _get_sync_statistics(profile_info->profile_dir_name, SYNC_AGENT_DS_SYNC_MEMO, &statistics);
940         if (result_sync_category)
941                 *statistics_list = g_list_append(*statistics_list, statistics);
942
943         _EXTERN_FUNC_EXIT;
944
945         return SYNC_AGENT_DS_SUCCESS;
946 }
947
948 EXPORT_API sync_agent_ds_error_e sync_agent_ds_add_profile(ds_profile_h profile_h, int *profile_id)
949 {
950         _EXTERN_FUNC_ENTER;
951
952         // check mandatory parameter
953         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
954
955         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
956         sync_agent_ds_server_info *server_info = &profile_info->server_info;
957         sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
958         GList *list = profile_info->service_list;
959
960         int event_type = SYNC_AGENT_DS_ADD_PROFILE;
961         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
962         sync_agent_event_data_s *request_event = NULL;
963         sync_agent_event_data_s *response_event = NULL;
964
965         int prof_id = 0;
966         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
967         sync_agent_ds_service_info *category_info = NULL;
968         int category_count = 0;
969         int i = 0;
970
971         /* create empty event packet */
972         request_event = sync_agent_create_event(event_type);
973         if (request_event == NULL) {
974                 _DEBUG_ERROR("event is NULL");
975                 _EXTERN_FUNC_EXIT;
976                 return SYNC_AGENT_DS_FAIL;
977         }
978
979         /* add request parameter to event packet */
980         _DEBUG_INFO("profile_name = %s", profile_info->profile_name);
981         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_name);
982         _DEBUG_INFO("addr = %s", server_info->addr);
983         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->addr);
984         _DEBUG_INFO("id = %s", server_info->id);
985         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->id);
986         _DEBUG_INFO("password = %s", server_info->password);
987         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->password);
988         _DEBUG_INFO("sync_mode = %d", sync_info->sync_mode);
989         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->sync_mode));
990         _DEBUG_INFO("sync_type = %d", sync_info->sync_type);
991         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->sync_type));
992         _DEBUG_INFO("interval = %d", sync_info->interval);
993         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->interval));
994
995         category_count = g_list_length(list);
996         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &category_count);
997
998         for (; i < category_count; i++) {
999                 category_info = (sync_agent_ds_service_info *) g_list_nth_data(list, i);
1000
1001                 _DEBUG_INFO("category[%d]->service_type = %d", i, category_info->service_type);
1002                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->service_type));
1003                 _DEBUG_INFO("category[%d]->enabled = %d", i, category_info->enabled);
1004                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->enabled));
1005                 _DEBUG_INFO("category[%d]->src_uri = %d", i, category_info->src_uri);
1006                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->src_uri));
1007                 _DEBUG_INFO("category[%d]->tgt_uri = %s", i, category_info->tgt_uri);
1008                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->tgt_uri);
1009                 _DEBUG_INFO("category[%d]->id = %s", i, category_info->id);
1010                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->id);
1011                 _DEBUG_INFO("category[%d]->password = %s", i, category_info->password);
1012                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->password);
1013         }
1014
1015         /* send event request to ds agent daemon, waiting for response */
1016         response_event = sync_agent_send_event(request_event, &error);
1017         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1018                 _DEBUG_ERROR("error = %d", error);
1019                 sync_agent_free_event(request_event);
1020                 _EXTERN_FUNC_EXIT;
1021                 return SYNC_AGENT_DS_FAIL;
1022         }
1023         if (response_event == NULL) {
1024                 _DEBUG_ERROR("response_event is null!!");
1025                 sync_agent_free_event(request_event);
1026                 _EXTERN_FUNC_EXIT;
1027                 return SYNC_AGENT_DS_FAIL;
1028         }
1029
1030         /* get response parameter from event packet */
1031         sync_agent_get_event_data_param_int(response_event, &api_result);
1032
1033         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1034                 _DEBUG_ERROR("api_result = failed");
1035                 sync_agent_free_event(request_event);
1036                 sync_agent_free_event_data(response_event);
1037                 _EXTERN_FUNC_EXIT;
1038                 return SYNC_AGENT_DS_FAIL;
1039         }
1040
1041         sync_agent_get_event_data_param_int(response_event, &prof_id);
1042
1043         _DEBUG_VERBOSE("profile_id = %d", prof_id);
1044         *profile_id = prof_id;
1045
1046         /* free request & response event */
1047         sync_agent_free_event(request_event);
1048         sync_agent_free_event_data(response_event);
1049
1050         _EXTERN_FUNC_EXIT;
1051
1052         return SYNC_AGENT_DS_SUCCESS;
1053 }
1054
1055 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_profile(char *profile_dir_name, ds_profile_h * profile_h)
1056 {
1057         _EXTERN_FUNC_ENTER;
1058
1059         // check mandatory parameter
1060         retvm_if(profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
1061
1062         int event_type = SYNC_AGENT_DS_GET_PROFILE;
1063         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1064         sync_agent_event_data_s *request_event = NULL;
1065         sync_agent_event_data_s *response_event = NULL;
1066
1067         gboolean result_sync_category = FALSE;
1068         sync_agent_ds_error_e result = SYNC_AGENT_DS_FAIL;
1069         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1070         sync_agent_ds_service_info *category = NULL;
1071
1072         //////////////////////////////////
1073         //
1074         // Phase 1. get profile detail info
1075
1076         /* create empty event packet */
1077         request_event = sync_agent_create_event(event_type);
1078         if (request_event == NULL) {
1079                 _DEBUG_ERROR("event is NULL");
1080                 _EXTERN_FUNC_EXIT;
1081                 return SYNC_AGENT_DS_FAIL;
1082         }
1083
1084         /* add request parameter to event packet */
1085         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_dir_name);
1086
1087         /* send event request to ds agent daemon, waiting for response */
1088         response_event = sync_agent_send_event(request_event, &error);
1089         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1090                 _DEBUG_ERROR("error = %d", error);
1091                 sync_agent_free_event(request_event);
1092                 _EXTERN_FUNC_EXIT;
1093                 return SYNC_AGENT_DS_FAIL;
1094         }
1095         if (response_event == NULL) {
1096                 _DEBUG_ERROR("response_event is null!!");
1097                 sync_agent_free_event(request_event);
1098                 _EXTERN_FUNC_EXIT;
1099                 return SYNC_AGENT_DS_FAIL;
1100         }
1101
1102         /* get response parameter from event packet */
1103
1104         result = sync_agent_ds_create_profile_info(profile_h);
1105         if (result == SYNC_AGENT_DS_FAIL) {
1106                 _DEBUG_ERROR("failed to create profile info!");
1107                 sync_agent_free_event(request_event);
1108                 sync_agent_free_event_data(response_event);
1109                 return FALSE;
1110         }
1111
1112         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) (*profile_h);
1113         sync_agent_ds_server_info *server_info = &profile_info->server_info;
1114         sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
1115 //      GList * list = profile_info->category_list;
1116
1117         sync_agent_get_event_data_param_int(response_event, &api_result);
1118         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1119                 _DEBUG_ERROR("api_result = failed");
1120                 sync_agent_free_event(request_event);
1121                 sync_agent_free_event_data(response_event);
1122                 _EXTERN_FUNC_EXIT;
1123                 return SYNC_AGENT_DS_FAIL;
1124         }
1125
1126         profile_info->profile_dir_name = g_strdup(profile_dir_name);
1127
1128         sync_agent_get_event_data_param_str(response_event, &profile_info->profile_name);
1129         sync_agent_get_event_data_param_str(response_event, &server_info->addr);
1130         sync_agent_get_event_data_param_str(response_event, &server_info->id);
1131         sync_agent_get_event_data_param_str(response_event, &server_info->password);
1132         sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->sync_mode);
1133         sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->sync_type);
1134         sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->interval);
1135         sync_agent_get_event_data_param_int(response_event, &profile_info->last_sync_status);
1136         sync_agent_get_event_data_param_int(response_event, &profile_info->last_sync_time);
1137
1138         _DEBUG_INFO("profileDirame = %s", profile_info->profile_name);
1139         _DEBUG_INFO("addr = %s", server_info->addr);
1140         _DEBUG_INFO("id = %s", server_info->id);
1141         _DEBUG_INFO("password = %s", server_info->password);
1142         _DEBUG_INFO("sync_mode = %d", sync_info->sync_mode);
1143         _DEBUG_INFO("sync_type = %d", sync_info->sync_type);
1144         _DEBUG_INFO("interval = %d", sync_info->interval);
1145         _DEBUG_VERBOSE("lastSyncStatus = %d\n", profile_info->last_sync_status);
1146         _DEBUG_VERBOSE("lastSyncTime = %d\n", profile_info->last_sync_time);
1147
1148         /* free request & response event */
1149         sync_agent_free_event(request_event);
1150         sync_agent_free_event_data(response_event);
1151
1152         //////////////////////////////////
1153         //
1154         // Phase 2. get sync category info
1155
1156         /* get contacts sync_category */
1157         result_sync_category = _get_sync_category(profile_dir_name, SYNC_AGENT_DS_SYNC_CONTACTS, &category);
1158         if (result_sync_category)
1159                 profile_info->service_list = g_list_append(profile_info->service_list, category);
1160
1161         /* get schedule sync_category */
1162         category = NULL;
1163         result_sync_category = _get_sync_category(profile_dir_name, SYNC_AGENT_DS_SYNC_SCHEDULE, &category);
1164         if (result_sync_category)
1165                 profile_info->service_list = g_list_append(profile_info->service_list, category);
1166
1167         /* get memo sync_category */
1168         category = NULL;
1169         result_sync_category = _get_sync_category(profile_dir_name, SYNC_AGENT_DS_SYNC_MEMO, &category);
1170         if (result_sync_category)
1171                 profile_info->service_list = g_list_append(profile_info->service_list, category);
1172
1173         _EXTERN_FUNC_EXIT;
1174
1175         return SYNC_AGENT_DS_SUCCESS;
1176 }
1177
1178 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_all_profile(GList ** profile_list)
1179 {
1180         _EXTERN_FUNC_ENTER;
1181
1182         int event_type = SYNC_AGENT_DS_GET_ALL_PROFILES;
1183         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1184         sync_agent_event_data_s *request_event = NULL;
1185         sync_agent_event_data_s *response_event = NULL;
1186
1187         ds_profile_h profile_h = NULL;
1188         sync_agent_ds_service_info *category_info = NULL;
1189
1190         int profile_count = 0;
1191         int category_count = 0;
1192
1193         sync_agent_ds_error_e result = SYNC_AGENT_DS_FAIL;
1194         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1195
1196         /* create empty event packet */
1197         request_event = sync_agent_create_event(event_type);
1198         if (request_event == NULL) {
1199                 _DEBUG_ERROR("event is NULL");
1200                 _EXTERN_FUNC_EXIT;
1201                 return SYNC_AGENT_DS_FAIL;
1202         }
1203
1204         /* send event request to ds agent daemon, waiting for response */
1205         response_event = sync_agent_send_event(request_event, &error);
1206         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1207                 _DEBUG_ERROR("error = %d", error);
1208                 sync_agent_free_event(request_event);
1209                 _EXTERN_FUNC_EXIT;
1210                 return SYNC_AGENT_DS_FAIL;
1211         }
1212         if (response_event == NULL) {
1213                 _DEBUG_ERROR("response_event is null!!");
1214                 sync_agent_free_event(request_event);
1215                 _EXTERN_FUNC_EXIT;
1216                 return SYNC_AGENT_DS_FAIL;
1217         }
1218
1219         sync_agent_get_event_data_param_int(response_event, &api_result);
1220         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1221                 _DEBUG_ERROR("api_result = failed");
1222                 sync_agent_free_event(request_event);
1223                 sync_agent_free_event_data(response_event);
1224                 _EXTERN_FUNC_EXIT;
1225                 return SYNC_AGENT_DS_FAIL;
1226         }
1227
1228         sync_agent_get_event_data_param_int(response_event, &profile_count);
1229         _DEBUG_INFO("profile_count = %d", profile_count);
1230
1231         int i;
1232         for (i = 0; i < profile_count; i++) {
1233
1234                 result = sync_agent_ds_create_profile_info(&profile_h);
1235                 if (result == SYNC_AGENT_DS_FAIL) {
1236                         _DEBUG_ERROR("failed to create profile info!");
1237                         sync_agent_free_event(request_event);
1238                         sync_agent_free_event_data(response_event);
1239                         return FALSE;
1240                 }
1241
1242                 sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1243                 sync_agent_ds_server_info *server_info = &profile_info->server_info;
1244                 sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
1245
1246                 sync_agent_get_event_data_param_str(response_event, &profile_info->profile_dir_name);
1247                 sync_agent_get_event_data_param_str(response_event, &profile_info->profile_name);
1248                 sync_agent_get_event_data_param_str(response_event, &server_info->addr);
1249                 sync_agent_get_event_data_param_str(response_event, &server_info->id);
1250                 sync_agent_get_event_data_param_str(response_event, &server_info->password);
1251                 sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->sync_mode);
1252                 sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->sync_type);
1253                 sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->interval);
1254                 sync_agent_get_event_data_param_int(response_event, &profile_info->last_sync_status);
1255                 sync_agent_get_event_data_param_int(response_event, &profile_info->last_sync_time);
1256
1257                 _DEBUG_INFO("profile_info->profile_dir_name = %s", profile_info->profile_dir_name);
1258                 _DEBUG_INFO("profile_info->profile_name = %s", profile_info->profile_name);
1259                 _DEBUG_INFO("profile_info->addr = %s", server_info->addr);
1260                 _DEBUG_INFO("profile_info->id = %s", server_info->id);
1261                 _DEBUG_INFO("profile_info->password = %s", server_info->password);
1262                 _DEBUG_INFO("profile_info->sync_mode = %d", sync_info->sync_mode);
1263                 _DEBUG_INFO("profile_info->sync_type = %d", sync_info->sync_type);
1264                 _DEBUG_INFO("profile_info->interval = %d", sync_info->interval);
1265                 _DEBUG_INFO("profile_info->lastSyncStatus = %d", profile_info->last_sync_status);
1266                 _DEBUG_INFO("profile_info->lastSyncStatus = %d", profile_info->last_sync_status);
1267
1268                 sync_agent_get_event_data_param_int(response_event, &category_count);
1269
1270                 int j;
1271                 for (j = 0; j < category_count; j++) {
1272
1273                         category_info = (sync_agent_ds_service_info *) calloc(1, sizeof(sync_agent_ds_service_info));
1274                         if (category_info == NULL) {
1275                                 _DEBUG_ERROR("calloc failed");
1276                                 sync_agent_ds_free_profile_info(profile_h);
1277                                 sync_agent_free_event(request_event);
1278                                 sync_agent_free_event_data(response_event);
1279                                 return FALSE;
1280                         }
1281
1282                         sync_agent_get_event_data_param_int(response_event, (int*) &category_info->service_type);
1283                         sync_agent_get_event_data_param_int(response_event, &category_info->enabled);
1284                         sync_agent_get_event_data_param_int(response_event, (int*) &category_info->src_uri);
1285                         sync_agent_get_event_data_param_str(response_event, &category_info->tgt_uri);
1286                         sync_agent_get_event_data_param_str(response_event, &category_info->id);
1287                         sync_agent_get_event_data_param_str(response_event, &category_info->password);
1288
1289                         _DEBUG_INFO("category_info->service_type = %d", category_info->service_type);
1290                         _DEBUG_INFO("category_info->enabled = %d", category_info->enabled);
1291                         _DEBUG_INFO("category_info->src_uri = %d", category_info->src_uri);
1292                         _DEBUG_INFO("category_info->tgt_uri = %s", category_info->tgt_uri);
1293                         _DEBUG_INFO("category_info->id = %s", category_info->id);
1294                         _DEBUG_INFO("category_info->password = %s", category_info->password);
1295
1296                         profile_info->service_list = g_list_append(profile_info->service_list, category_info);
1297                         category_info = NULL;
1298                 }
1299
1300                 *profile_list = g_list_append(*profile_list, profile_h);
1301                 profile_h = NULL;
1302         }
1303
1304         /* free request & response event */
1305         sync_agent_free_event(request_event);
1306         sync_agent_free_event_data(response_event);
1307
1308         _EXTERN_FUNC_EXIT;
1309
1310         return SYNC_AGENT_DS_SUCCESS;
1311 }
1312
1313 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_last_sync_info(ds_profile_h profile_h, int *lastSyncStatus, int *lastSyncTime)
1314 {
1315         _EXTERN_FUNC_ENTER;
1316
1317         // check mandatory parameter
1318         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1319         retvm_if(lastSyncStatus == NULL, SYNC_AGENT_DS_FAIL, "last session status is null!!");
1320         retvm_if(lastSyncTime == NULL, SYNC_AGENT_DS_FAIL, "last session time is null!!");
1321
1322         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1323
1324         *lastSyncStatus = profile_info->last_sync_status;
1325         *lastSyncTime = profile_info->last_sync_time;
1326
1327         _EXTERN_FUNC_EXIT;
1328
1329         return SYNC_AGENT_DS_SUCCESS;
1330 }
1331
1332 EXPORT_API sync_agent_ds_error_e sync_agent_ds_update_profile(ds_profile_h profile_h)
1333 {
1334         _EXTERN_FUNC_ENTER;
1335
1336         // check mandatory parameter
1337         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1338
1339         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1340         sync_agent_ds_server_info *server_info = &profile_info->server_info;
1341         sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
1342         GList *list = profile_info->service_list;
1343
1344         int event_type = SYNC_AGENT_DS_UPDATE_PROFILE;
1345         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1346         sync_agent_event_data_s *request_event = NULL;
1347         sync_agent_event_data_s *response_event = NULL;
1348
1349         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1350         sync_agent_ds_service_info *category_info = NULL;
1351         int category_count = 0;
1352         int i = 0;
1353
1354         /* create empty event packet */
1355         request_event = sync_agent_create_event(event_type);
1356         if (request_event == NULL) {
1357                 _DEBUG_ERROR("event is NULL");
1358                 _EXTERN_FUNC_EXIT;
1359                 return SYNC_AGENT_DS_FAIL;
1360         }
1361
1362         /* add request parameter to event packet */
1363         _DEBUG_INFO("profile_dir_name = %s", profile_info->profile_dir_name);
1364         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name);
1365         _DEBUG_INFO("profile_name = %s", profile_info->profile_name);
1366         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_name);
1367         _DEBUG_INFO("addr = %s", server_info->addr);
1368         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->addr);
1369         _DEBUG_INFO("id = %s", server_info->id);
1370         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->id);
1371         _DEBUG_INFO("password = %s", server_info->password);
1372         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->password);
1373         _DEBUG_INFO("sync_mode = %d", sync_info->sync_mode);
1374         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->sync_mode));
1375         _DEBUG_INFO("sync_type = %d", sync_info->sync_type);
1376         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->sync_type));
1377         _DEBUG_INFO("interval = %d", sync_info->interval);
1378         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->interval));
1379
1380         category_count = g_list_length(list);
1381         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &category_count);
1382
1383         for (; i < category_count; i++) {
1384                 category_info = (sync_agent_ds_service_info *) g_list_nth_data(list, i);
1385
1386                 _DEBUG_INFO("category[%d]->service_type = %d", i, category_info->service_type);
1387                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->service_type));
1388                 _DEBUG_INFO("category[%d]->enabled = %d", i, category_info->enabled);
1389                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->enabled));
1390                 _DEBUG_INFO("category[%d]->src_uri = %d", i, category_info->src_uri);
1391                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->src_uri));
1392                 _DEBUG_INFO("category[%d]->tgt_uri = %s", i, category_info->tgt_uri);
1393                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->tgt_uri);
1394                 _DEBUG_INFO("category[%d]->id = %s", i, category_info->id);
1395                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->id);
1396                 _DEBUG_INFO("category[%d]->password = %s", i, category_info->password);
1397                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->password);
1398         }
1399
1400         /* send event request to ds agent daemon, waiting for response */
1401         response_event = sync_agent_send_event(request_event, &error);
1402         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1403                 _DEBUG_ERROR("error = %d", error);
1404                 sync_agent_free_event(request_event);
1405                 _EXTERN_FUNC_EXIT;
1406                 return SYNC_AGENT_DS_FAIL;
1407         }
1408         if (response_event == NULL) {
1409                 _DEBUG_ERROR("response_event is null!!");
1410                 sync_agent_free_event(request_event);
1411                 _EXTERN_FUNC_EXIT;
1412                 return SYNC_AGENT_DS_FAIL;
1413         }
1414
1415         /* get response parameter from event packet */
1416         sync_agent_get_event_data_param_int(response_event, &api_result);
1417
1418         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1419                 _DEBUG_ERROR("api_result = failed");
1420                 sync_agent_free_event(request_event);
1421                 sync_agent_free_event_data(response_event);
1422                 _EXTERN_FUNC_EXIT;
1423                 return SYNC_AGENT_DS_FAIL;
1424         }
1425
1426         /* free request & response event */
1427         sync_agent_free_event(request_event);
1428         sync_agent_free_event_data(response_event);
1429
1430         _EXTERN_FUNC_EXIT;
1431
1432         return SYNC_AGENT_DS_SUCCESS;
1433 }
1434
1435 EXPORT_API sync_agent_ds_error_e sync_agent_ds_delete_profile(ds_profile_h profile_h)
1436 {
1437         _EXTERN_FUNC_ENTER;
1438
1439         // check mandatory parameter
1440         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1441
1442         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1443
1444         retvm_if(profile_info->profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
1445
1446         int event_type = SYNC_AGENT_DS_DELETE_PROFILE;
1447         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1448         sync_agent_event_data_s *request_event = NULL;
1449         sync_agent_event_data_s *response_event = NULL;
1450
1451         int count = 1;
1452         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1453
1454         /* create empty event packet */
1455         request_event = sync_agent_create_event(event_type);
1456         if (request_event == NULL) {
1457                 _DEBUG_ERROR("event is NULL");
1458                 return SYNC_AGENT_DS_FAIL;
1459         }
1460
1461         /* add request parameter to event packet */
1462         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &count);
1463
1464         _DEBUG_INFO("profile_dir_name = %s", profile_info->profile_dir_name);
1465         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name);
1466
1467         /* send event request to ds agent daemon, waiting for response */
1468         response_event = sync_agent_send_event(request_event, &error);
1469         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1470                 _DEBUG_ERROR("error = %d", error);
1471                 sync_agent_free_event(request_event);
1472                 _EXTERN_FUNC_EXIT;
1473                 return SYNC_AGENT_DS_FAIL;
1474         }
1475         if (response_event == NULL) {
1476                 _DEBUG_ERROR("response_event is null!!");
1477                 sync_agent_free_event(request_event);
1478                 _EXTERN_FUNC_EXIT;
1479                 return SYNC_AGENT_DS_FAIL;
1480         }
1481
1482         /* get response parameter from event packet */
1483         sync_agent_get_event_data_param_int(response_event, &api_result);
1484
1485         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1486                 _DEBUG_ERROR("api_result = failed");
1487                 sync_agent_free_event(request_event);
1488                 sync_agent_free_event_data(response_event);
1489                 _EXTERN_FUNC_EXIT;
1490                 return SYNC_AGENT_DS_FAIL;
1491         }
1492
1493         /* free request & response event */
1494         sync_agent_free_event(request_event);
1495         sync_agent_free_event_data(response_event);
1496
1497         _EXTERN_FUNC_EXIT;
1498
1499         return SYNC_AGENT_DS_SUCCESS;
1500 }
1501
1502 EXPORT_API sync_agent_ds_error_e sync_agent_ds_start_sync(ds_profile_h profile_h)
1503 {
1504         _EXTERN_FUNC_ENTER;
1505
1506         // check mandatory parameter
1507         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1508
1509         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1510
1511         retvm_if(profile_info->profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
1512
1513         int event_type = SYNC_AGENT_DS_START_SYNC;
1514         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1515         sync_agent_event_data_s *request_event = NULL;
1516         sync_agent_event_data_s *response_event = NULL;
1517
1518         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1519
1520         /* create empty event packet */
1521         request_event = sync_agent_create_event(event_type);
1522         if (request_event == NULL) {
1523                 _DEBUG_ERROR("event is NULL");
1524                 return SYNC_AGENT_DS_FAIL;
1525         }
1526
1527         /* add request parameter to event packet */
1528         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name);
1529
1530         /* send event request to ds agent daemon, waiting for response */
1531         response_event = sync_agent_send_event(request_event, &error);
1532         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1533                 _DEBUG_ERROR("error = %d", error);
1534                 sync_agent_free_event(request_event);
1535                 _EXTERN_FUNC_EXIT;
1536                 return SYNC_AGENT_DS_FAIL;
1537         }
1538         if (response_event == NULL) {
1539                 _DEBUG_ERROR("response_event is null!!");
1540                 sync_agent_free_event(request_event);
1541                 _EXTERN_FUNC_EXIT;
1542                 return SYNC_AGENT_DS_FAIL;
1543         }
1544
1545         /* get response parameter from event packet */
1546         sync_agent_get_event_data_param_int(response_event, &api_result);
1547
1548         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1549                 _DEBUG_ERROR("api_result = failed");
1550                 sync_agent_free_event(request_event);
1551                 sync_agent_free_event_data(response_event);
1552                 _EXTERN_FUNC_EXIT;
1553                 return SYNC_AGENT_DS_FAIL;
1554         }
1555
1556         /* free request & response event */
1557         sync_agent_free_event(request_event);
1558         sync_agent_free_event_data(response_event);
1559
1560         _EXTERN_FUNC_EXIT;
1561
1562         return SYNC_AGENT_DS_SUCCESS;
1563 }
1564
1565 EXPORT_API sync_agent_ds_error_e sync_agent_ds_stop_sync(ds_profile_h profile_h)
1566 {
1567         _EXTERN_FUNC_ENTER;
1568
1569         // check mandatory parameter
1570         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1571
1572         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1573
1574         retvm_if(profile_info->profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
1575
1576         int event_type = SYNC_AGENT_DS_STOP_SYNC;
1577         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1578         sync_agent_event_data_s *request_event = NULL;
1579         sync_agent_event_data_s *response_event = NULL;
1580
1581         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1582
1583         /* create empty event packet */
1584         request_event = sync_agent_create_event(event_type);
1585         if (request_event == NULL) {
1586                 _DEBUG_ERROR("event is NULL");
1587                 return SYNC_AGENT_DS_FAIL;
1588         }
1589
1590         /* add request parameter to event packet */
1591         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name);
1592
1593         /* send event request to ds agent daemon, waiting for response */
1594         response_event = sync_agent_send_event(request_event, &error);
1595         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1596                 _DEBUG_ERROR("error = %d", error);
1597                 sync_agent_free_event(request_event);
1598                 _EXTERN_FUNC_EXIT;
1599                 return SYNC_AGENT_DS_FAIL;
1600         }
1601         if (response_event == NULL) {
1602                 _DEBUG_ERROR("response_event is null!!");
1603                 sync_agent_free_event(request_event);
1604                 _EXTERN_FUNC_EXIT;
1605                 return SYNC_AGENT_DS_FAIL;
1606         }
1607
1608         /* get response parameter from event packet */
1609         sync_agent_get_event_data_param_int(response_event, &api_result);
1610
1611         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1612                 _DEBUG_ERROR("api_result = failed");
1613                 sync_agent_free_event(request_event);
1614                 sync_agent_free_event_data(response_event);
1615                 _EXTERN_FUNC_EXIT;
1616                 return SYNC_AGENT_DS_FAIL;
1617         }
1618
1619         /* free request & response event */
1620         sync_agent_free_event(request_event);
1621         sync_agent_free_event_data(response_event);
1622
1623         _EXTERN_FUNC_EXIT;
1624
1625         return SYNC_AGENT_DS_SUCCESS;
1626 }
1627
1628 EXPORT_API void sync_agent_ds_free_profile_info(ds_profile_h profile_h)
1629 {
1630         _EXTERN_FUNC_ENTER;
1631
1632         // check mandatory parameter
1633         retm_if(profile_h == NULL, "profile handle is null!!");
1634
1635         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1636
1637         sync_agent_ds_server_info *server_info = &profile_info->server_info;
1638         //sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
1639         GList *category_list = profile_info->service_list;
1640
1641         // free profile name
1642         g_free(profile_info->profile_dir_name);
1643         g_free(profile_info->profile_name);
1644
1645         // free server information
1646         g_free(server_info->addr);
1647         g_free(server_info->id);
1648         g_free(server_info->password);
1649
1650         // free sync category information
1651         g_list_free_full(category_list, (GDestroyNotify) _free_sync_category_info);
1652
1653         // free profile information
1654         g_free(profile_info);
1655
1656         _EXTERN_FUNC_EXIT;
1657 }
1658
1659 EXPORT_API sync_agent_ds_error_e sync_agent_ds_deinit()
1660 {
1661         _EXTERN_FUNC_ENTER;
1662
1663         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
1664         sync_agent_deinit_error_e deinit = SYNC_AGENT_DEINIT_SUCCESS;
1665         int ret = -1;
1666
1667         ret = _kill_omads_agent();
1668
1669         if (ret < 0 ) {
1670                 _DEBUG_ERROR("_kill_omads_agent() failed !!");
1671                 result = SYNC_AGENT_DS_FAIL;
1672
1673                 _EXTERN_FUNC_EXIT;
1674                 return result;
1675         }
1676
1677         deinit = sync_agent_deinit();
1678         if (deinit != SYNC_AGENT_DEINIT_SUCCESS) {
1679                 _DEBUG_ERROR("sync_agent_clean_event_handler() failed !!");
1680                 result = SYNC_AGENT_DS_FAIL;
1681         }
1682 //      if (err != SYNC_AGENT_EVENT_SUCCESS) {
1683 //      sync_agent_event_error_e err = sync_agent_stop_noti_listener();
1684 //              _DEBUG_INFO("STOP NOTILISTNER is failed");
1685 //              result = SYNC_AGENT_DS_FAIL;
1686 //      }
1687 //
1688 //      err = sync_agent_clean_event_handler();
1689 //      if (err != SYNC_AGENT_EVENT_SUCCESS) {
1690 //              _DEBUG_ERROR("sync_agent_clean_event_handler() failed !!");
1691 //              result = SYNC_AGENT_DS_FAIL;
1692 //      }
1693
1694         _EXTERN_FUNC_EXIT;
1695
1696         return result;
1697 }
1698
1699 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_noti_callback(sync_agent_ds_sync_callback_e type, sync_agent_noti_cb callback, void *data)
1700 {
1701         _EXTERN_FUNC_ENTER;
1702
1703         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
1704
1705         sync_agent_event_error_e err = sync_agent_set_noti_callback(type, callback, data);
1706         if (err != SYNC_AGENT_EVENT_SUCCESS) {
1707                 _DEBUG_ERROR("failed sync_agent_set_noti_callback()");
1708                 result = SYNC_AGENT_DS_FAIL;
1709         }
1710
1711         return result;
1712
1713         _EXTERN_FUNC_EXIT;
1714 }
1715