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