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