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