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