af5e0684a8d6e030f3328bf57cebcf3c6f9d4483
[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
471         g_type_init();
472
473         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
474         if (error != NULL) {
475                 _DEBUG_ERROR("Connecting to system bus failed: %s\n", error->message);
476                 g_error_free(error);
477                 _INNER_FUNC_EXIT;
478                 return -1;
479         }
480
481         dbus_proxy = dbus_g_proxy_new_for_name(connection, "com.samsung.omadsagent",
482                                                "/com/samsung/omadsagent", "com.samsung.omadsagent");
483
484         _DEBUG_INFO("dbus_proxy %x", dbus_proxy);
485
486         //dbus_g_proxy_call_no_reply(dbus_proxy, "Hello_Agent", G_TYPE_INVALID);
487         dbus_g_proxy_call(dbus_proxy, "Hello_Agent", &error, G_TYPE_INVALID, G_TYPE_INVALID);
488
489         g_object_unref(dbus_proxy);
490         dbus_g_connection_unref(connection);
491
492         _INNER_FUNC_EXIT;
493         return 0;
494 }
495
496 static int _kill_omads_agent(void)
497 {
498         _INNER_FUNC_ENTER;
499         GError *error = NULL;
500
501         DBusGConnection *connection = NULL;
502         DBusGProxy *dbus_proxy = NULL;
503         gboolean isExisting = FALSE;
504
505         isExisting = _is_existing_profile();
506
507         if(isExisting) {
508                 _DEBUG_INFO("Existing profile  !!");
509
510                 _INNER_FUNC_EXIT;
511                 return 0;
512         }
513
514         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
515         if (error != NULL) {
516                 _DEBUG_ERROR("Connecting to system bus failed: %s\n", error->message);
517                 g_error_free(error);
518                 _INNER_FUNC_EXIT;
519                 return -1;
520         }
521
522         dbus_proxy = dbus_g_proxy_new_for_name(connection, "com.samsung.omadsagent",
523                                                "/com/samsung/omadsagent", "com.samsung.omadsagent");
524
525         _DEBUG_INFO("dbus_proxy %x", dbus_proxy);
526
527         dbus_g_proxy_call_no_reply(dbus_proxy, "Goodbye_Agent", G_TYPE_INVALID);
528
529         g_object_unref(dbus_proxy);
530         dbus_g_connection_unref(connection);
531
532         _INNER_FUNC_EXIT;
533         return 0;
534 }
535
536 EXPORT_API sync_agent_ds_error_e sync_agent_ds_init()
537 {
538         _EXTERN_FUNC_ENTER;
539
540         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
541         sync_agent_init_error_e init_error = SYNC_AGENT_INIT_SUCCESS;
542         int ret = -1;
543
544         ret = _launch_omads_agent();
545
546         if (ret < 0 ) {
547                 _DEBUG_ERROR("_launch_omads_agent() failed !!");
548                 result = SYNC_AGENT_DS_FAIL;
549
550                 _EXTERN_FUNC_EXIT;
551                 return result;
552         }
553
554         init_error = sync_agent_init(OMA_DS_CONFIG_FILE);
555
556         if (init_error != SYNC_AGENT_INIT_SUCCESS) {
557                 _DEBUG_ERROR("ds init failed");
558                 result = SYNC_AGENT_DS_FAIL;
559         }
560
561         sync_agent_event_error_e err = sync_agent_run_noti_listener("omads");
562         if (err != SYNC_AGENT_EVENT_SUCCESS) {
563                 _DEBUG_ERROR("RUN NOTILISTNER is failed");
564                 result = SYNC_AGENT_DS_FAIL;
565         }
566
567         _EXTERN_FUNC_EXIT;
568
569         return result;
570 }
571
572 EXPORT_API sync_agent_ds_error_e sync_agent_ds_create_profile_info(ds_profile_h * profile_h)
573 {
574         _EXTERN_FUNC_ENTER;
575
576         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
577         sync_agent_ds_profile_info *profile_info = NULL;
578
579         // allocate memory for ds profile info
580         profile_info = (sync_agent_ds_profile_info *) calloc(1, sizeof(sync_agent_ds_profile_info));
581         if (profile_info == NULL) {
582                 _DEBUG_ERROR("calloc failed !!");
583                 return SYNC_AGENT_DS_FAIL;
584         }
585         // pass memory pointer to ds profile handle
586         *profile_h = profile_info;
587
588         _EXTERN_FUNC_EXIT;
589
590         return result;
591 }
592
593 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_profile_dir_name(ds_profile_h profile_h, char *profile_dir_name)
594 {
595         _EXTERN_FUNC_ENTER;
596
597         // check mandatory parameter
598         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
599         retvm_if(profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
600
601         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
602         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
603
604         // set profile name
605         profile_info->profile_dir_name = g_strdup(profile_dir_name);
606
607         if (profile_info->profile_dir_name == NULL) {
608                 _DEBUG_ERROR("g_strdup failed !!");
609                 return SYNC_AGENT_DS_FAIL;
610         }
611
612         _EXTERN_FUNC_EXIT;
613
614         return result;
615 }
616
617 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_profile_name(ds_profile_h profile_h, char *profile_name)
618 {
619         _EXTERN_FUNC_ENTER;
620
621         // check mandatory parameter
622         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
623         retvm_if(profile_name == NULL, SYNC_AGENT_DS_FAIL, "profile_name is null!!");
624
625         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
626         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
627
628         // set profile name
629         profile_info->profile_name = g_strdup(profile_name);
630
631         if (profile_info->profile_name == NULL) {
632                 _DEBUG_ERROR("g_strdup failed !!");
633                 return SYNC_AGENT_DS_FAIL;
634         }
635
636         _EXTERN_FUNC_EXIT;
637
638         return result;
639 }
640
641 EXPORT_API sync_agent_ds_error_e sync_agent_ds_set_server_info(ds_profile_h profile_h, char *addr, char *id, char *password)
642 {
643         _EXTERN_FUNC_ENTER;
644
645         // check mandatory parameter
646         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
647         retvm_if(addr == NULL, SYNC_AGENT_DS_FAIL, "addr is null!!");
648         retvm_if(id == NULL, SYNC_AGENT_DS_FAIL, "id is null!!");
649         retvm_if(password == NULL, SYNC_AGENT_DS_FAIL, "password is null!!");
650
651         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
652         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
653         sync_agent_ds_server_info *server_info = &profile_info->server_info;
654
655         // set server info
656         server_info->addr = g_strdup(addr);
657         server_info->id = g_strdup(id);
658         server_info->password = g_strdup(password);
659
660         if ((server_info->addr == NULL)
661             || (server_info->id == NULL)
662             || (server_info->password == NULL)) {
663                 _DEBUG_ERROR("g_strdup failed !!");
664                 return SYNC_AGENT_DS_FAIL;
665         }
666
667         _EXTERN_FUNC_EXIT;
668
669         return result;
670 }
671
672 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)
673 {
674         _EXTERN_FUNC_ENTER;
675
676         // check mandatory parameter
677         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
678         retvm_if(sync_mode < 0, SYNC_AGENT_DS_FAIL, "sync_mode is invalid!!");
679         retvm_if(sync_type < 0, SYNC_AGENT_DS_FAIL, "sync_type is invalid!!");
680         retvm_if(interval < 0, SYNC_AGENT_DS_FAIL, "interval is invalid!!");
681
682         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
683         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
684         sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
685
686         char* sync_mode_str = NULL;
687         char* sync_type_str = NULL;
688         char* interval_str = NULL;
689
690         sync_mode_str = _convert_sync_mode_str(sync_mode);
691         if (sync_mode_str == NULL) {
692                 _DEBUG_ERROR("_convert_sync_mode_str failed !!");
693                 _EXTERN_FUNC_EXIT;
694                 return SYNC_AGENT_DS_FAIL;
695         }
696
697         sync_type_str = _convert_sync_type_str(sync_type);
698         if (sync_type_str == NULL) {
699                 _DEBUG_ERROR("_convert_sync_type_str failed !!");
700                 _EXTERN_FUNC_EXIT;
701                 return SYNC_AGENT_DS_FAIL;
702         }
703
704         interval_str = _convert_sync_interval_str(interval);
705         if (interval_str == NULL) {
706                 _DEBUG_ERROR("_convert_sync_interval_str failed !!");
707                 _EXTERN_FUNC_EXIT;
708                 return SYNC_AGENT_DS_FAIL;
709         }
710
711         // set server info
712         sync_info->sync_mode = sync_mode;
713         sync_info->sync_type = sync_type;
714         sync_info->interval = interval;
715
716         _EXTERN_FUNC_EXIT;
717
718         return result;
719 }
720
721 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)
722 {
723         _EXTERN_FUNC_ENTER;
724
725         // check mandatory parameter
726         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
727         retvm_if(src_uri < 0, SYNC_AGENT_DS_FAIL, "src_uri is invalid!!");
728
729         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
730         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
731         sync_agent_ds_service_info *category_info = NULL;
732         char* src_uri_str = NULL;
733
734         src_uri_str = _convert_src_uri_str(src_uri);
735         if (src_uri_str == NULL) {
736                 _DEBUG_ERROR("_get_src_uri_string failed !!");
737                 _EXTERN_FUNC_EXIT;
738                 return SYNC_AGENT_DS_FAIL;
739         }
740
741         // allocate memory for sync_category
742         category_info = (sync_agent_ds_service_info *) calloc(1, sizeof(sync_agent_ds_service_info));
743         retvm_if(category_info == NULL, SYNC_AGENT_DS_FAIL, "calloc failed");
744         category_info->service_type = service_type;
745         category_info->enabled = enabled;
746         category_info->src_uri = src_uri;
747         category_info->tgt_uri = g_strdup(tgt_uri);
748
749         profile_info->service_list = g_list_append(profile_info->service_list, category_info);
750
751         _EXTERN_FUNC_EXIT;
752
753         return result;
754 }
755
756 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_profile_id(ds_profile_h profile_h, int *profile_id)
757 {
758         _EXTERN_FUNC_ENTER;
759
760         // check mandatory parameter
761         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
762
763         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
764
765         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
766
767         *profile_id = profile_info->profile_id;
768
769         if (*profile_id < 1) {
770                 _DEBUG_ERROR("profile_id is invalid!!");
771                 return SYNC_AGENT_DS_FAIL;
772         }
773
774         _DEBUG_INFO("profile_id = %d", *profile_id);
775
776         _EXTERN_FUNC_EXIT;
777
778         return result;
779 }
780
781 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_profile_dir_name(ds_profile_h profile_h, char **profile_dir_name)
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_dir_name = g_strdup(profile_info->profile_dir_name);
793
794         if (*profile_dir_name == NULL) {
795                 _DEBUG_ERROR("g_strdup failed !!");
796                 return SYNC_AGENT_DS_FAIL;
797         }
798
799         _DEBUG_INFO("profile_dir_name = %s", *profile_dir_name);
800
801         _EXTERN_FUNC_EXIT;
802
803         return result;
804 }
805
806 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_profile_name(ds_profile_h profile_h, char **profile_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_name = g_strdup(profile_info->profile_name);
818
819         if (*profile_name == NULL) {
820                 _DEBUG_ERROR("g_strdup failed !!");
821                 return SYNC_AGENT_DS_FAIL;
822         }
823
824         _DEBUG_INFO("profile_name = %s", *profile_name);
825
826         _EXTERN_FUNC_EXIT;
827
828         return result;
829 }
830
831 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)
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         retvm_if(server_info == NULL, SYNC_AGENT_DS_FAIL, "server_info is null!!");
838
839         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
840         sync_agent_ds_server_info *ds_server_info = &profile_info->server_info;
841
842         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
843
844         server_info->addr = g_strdup(ds_server_info->addr);
845         server_info->id = g_strdup(ds_server_info->id);
846         server_info->password = g_strdup(ds_server_info->password);
847
848         if ((server_info->addr == NULL)
849             || (server_info->id == NULL)
850             || (server_info->password == NULL)) {
851                 _DEBUG_ERROR("g_strdup failed !!");
852                 return SYNC_AGENT_DS_FAIL;
853         }
854
855         _DEBUG_INFO("get_addr = %s", server_info->addr);
856         _DEBUG_INFO("get_id = %s", server_info->id);
857         _DEBUG_INFO("get_password = %s", server_info->password);
858
859         _EXTERN_FUNC_EXIT;
860
861         return result;
862 }
863
864 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)
865 {
866         _EXTERN_FUNC_ENTER;
867
868         // check mandatory parameter
869         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
870         retvm_if(sync_info == NULL, SYNC_AGENT_DS_FAIL, "sync_info is null!!");
871
872         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
873         sync_agent_ds_sync_info *ds_sync_info = &profile_info->sync_info;
874
875         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
876
877         sync_info->sync_mode = ds_sync_info->sync_mode;
878         sync_info->sync_type = ds_sync_info->sync_type;
879         sync_info->interval = ds_sync_info->interval;
880
881         _DEBUG_INFO("get_sync_mode = %d", sync_info->sync_mode);
882         _DEBUG_INFO("get_sync_type = %d", sync_info->sync_type);
883         _DEBUG_INFO("get_interval = %d", sync_info->interval);
884
885         if (sync_info->sync_mode < 0) {
886                 _DEBUG_ERROR("sync_mode is invalid !!");
887                 return SYNC_AGENT_DS_FAIL;
888         }
889
890         if (sync_info->sync_type < 0) {
891                 _DEBUG_ERROR("sync_type is invalid !!");
892                 return SYNC_AGENT_DS_FAIL;
893         }
894
895         if (sync_info->interval < 0) {
896                 _DEBUG_ERROR("interval is invalid !!");
897                 return SYNC_AGENT_DS_FAIL;
898         }
899
900         _EXTERN_FUNC_EXIT;
901
902         return result;
903 }
904
905 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_sync_service_info(ds_profile_h profile_h, GList ** category_list)
906 {
907         _EXTERN_FUNC_ENTER;
908
909         // check mandatory parameter
910         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
911
912         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
913
914         int category_count = 0;
915         int i = 0;
916         sync_agent_ds_service_info *category_info = NULL;
917
918         *category_list = g_list_copy(profile_info->service_list);
919
920         if (*category_list == NULL) {
921                 _DEBUG_ERROR("g_list_copy failed !!");
922                 return SYNC_AGENT_DS_FAIL;
923         }
924
925         category_count = g_list_length(*category_list);
926         for (; i < category_count; i++) {
927                 category_info = (sync_agent_ds_service_info *) g_list_nth_data(*category_list, i);
928
929                 _DEBUG_INFO("category[%d]->enabled = %d", i, category_info->enabled);
930                 _DEBUG_INFO("category[%d]->src_uri = %d", i, category_info->src_uri);
931                 _DEBUG_INFO("category[%d]->tgt_uri = %s", i, category_info->tgt_uri);
932                 _DEBUG_INFO("category[%d]->id = %s", i, category_info->id);
933                 _DEBUG_INFO("category[%d]->password = %s", i, category_info->password);
934         }
935
936         _EXTERN_FUNC_EXIT;
937
938         return SYNC_AGENT_DS_SUCCESS;
939 }
940
941 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_sync_statistics(ds_profile_h profile_h, GList ** statistics_list)
942 {
943         _EXTERN_FUNC_ENTER;
944
945         // check mandatory parameter
946         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
947
948         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
949         sync_agent_ds_statistics_info *statistics = NULL;
950
951         gboolean result_sync_category = FALSE;
952
953         /* get last contacts sync result */
954         result_sync_category = _get_sync_statistics(profile_info->profile_dir_name, SYNC_AGENT_DS_SYNC_CONTACTS, &statistics);
955         if (result_sync_category)
956                 *statistics_list = g_list_append(*statistics_list, statistics);
957
958         /* get last schedule sync result */
959         statistics = NULL;
960         result_sync_category = _get_sync_statistics(profile_info->profile_dir_name, SYNC_AGENT_DS_SYNC_SCHEDULE, &statistics);
961         if (result_sync_category)
962                 *statistics_list = g_list_append(*statistics_list, statistics);
963
964         /* get last memo sync result */
965         statistics = NULL;
966         result_sync_category = _get_sync_statistics(profile_info->profile_dir_name, SYNC_AGENT_DS_SYNC_MEMO, &statistics);
967         if (result_sync_category)
968                 *statistics_list = g_list_append(*statistics_list, statistics);
969
970         _EXTERN_FUNC_EXIT;
971
972         return SYNC_AGENT_DS_SUCCESS;
973 }
974
975 EXPORT_API sync_agent_ds_error_e sync_agent_ds_add_profile(ds_profile_h profile_h, int *profile_id)
976 {
977         _EXTERN_FUNC_ENTER;
978
979         // check mandatory parameter
980         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
981
982         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
983         sync_agent_ds_server_info *server_info = &profile_info->server_info;
984         sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
985         GList *list = profile_info->service_list;
986
987         int event_type = SYNC_AGENT_DS_ADD_PROFILE;
988         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
989         sync_agent_event_data_s *request_event = NULL;
990         sync_agent_event_data_s *response_event = NULL;
991
992         int prof_id = 0;
993         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
994         sync_agent_ds_service_info *category_info = NULL;
995         int category_count = 0;
996         int i = 0;
997
998         /* create empty event packet */
999         request_event = sync_agent_create_event(event_type);
1000         if (request_event == NULL) {
1001                 _DEBUG_ERROR("event is NULL");
1002                 _EXTERN_FUNC_EXIT;
1003                 return SYNC_AGENT_DS_FAIL;
1004         }
1005
1006         /* add request parameter to event packet */
1007         _DEBUG_INFO("profile_name = %s", profile_info->profile_name);
1008         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_name);
1009         _DEBUG_INFO("addr = %s", server_info->addr);
1010         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->addr);
1011         _DEBUG_INFO("id = %s", server_info->id);
1012         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->id);
1013         _DEBUG_INFO("password = %s", server_info->password);
1014         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->password);
1015         _DEBUG_INFO("sync_mode = %d", sync_info->sync_mode);
1016         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->sync_mode));
1017         _DEBUG_INFO("sync_type = %d", sync_info->sync_type);
1018         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->sync_type));
1019         _DEBUG_INFO("interval = %d", sync_info->interval);
1020         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->interval));
1021
1022         category_count = g_list_length(list);
1023         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &category_count);
1024
1025         for (; i < category_count; i++) {
1026                 category_info = (sync_agent_ds_service_info *) g_list_nth_data(list, i);
1027
1028                 _DEBUG_INFO("category[%d]->service_type = %d", i, category_info->service_type);
1029                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->service_type));
1030                 _DEBUG_INFO("category[%d]->enabled = %d", i, category_info->enabled);
1031                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->enabled));
1032                 _DEBUG_INFO("category[%d]->src_uri = %d", i, category_info->src_uri);
1033                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->src_uri));
1034                 _DEBUG_INFO("category[%d]->tgt_uri = %s", i, category_info->tgt_uri);
1035                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->tgt_uri);
1036                 _DEBUG_INFO("category[%d]->id = %s", i, category_info->id);
1037                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->id);
1038                 _DEBUG_INFO("category[%d]->password = %s", i, category_info->password);
1039                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->password);
1040         }
1041
1042         /* send event request to ds agent daemon, waiting for response */
1043         response_event = sync_agent_send_event(request_event, &error);
1044         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1045                 _DEBUG_ERROR("error = %d", error);
1046                 sync_agent_free_event(request_event);
1047                 _EXTERN_FUNC_EXIT;
1048                 return SYNC_AGENT_DS_FAIL;
1049         }
1050         if (response_event == NULL) {
1051                 _DEBUG_ERROR("response_event is null!!");
1052                 sync_agent_free_event(request_event);
1053                 _EXTERN_FUNC_EXIT;
1054                 return SYNC_AGENT_DS_FAIL;
1055         }
1056
1057         /* get response parameter from event packet */
1058         sync_agent_get_event_data_param_int(response_event, &api_result);
1059
1060         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1061                 _DEBUG_ERROR("api_result = failed");
1062                 sync_agent_free_event(request_event);
1063                 sync_agent_free_event_data(response_event);
1064                 _EXTERN_FUNC_EXIT;
1065                 return SYNC_AGENT_DS_FAIL;
1066         }
1067
1068         sync_agent_get_event_data_param_int(response_event, &prof_id);
1069
1070         _DEBUG_VERBOSE("profile_id = %d", prof_id);
1071         *profile_id = prof_id;
1072
1073         /* free request & response event */
1074         sync_agent_free_event(request_event);
1075         sync_agent_free_event_data(response_event);
1076
1077         _EXTERN_FUNC_EXIT;
1078
1079         return SYNC_AGENT_DS_SUCCESS;
1080 }
1081
1082 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_profile(int profile_id, ds_profile_h * profile_h)
1083 {
1084         _EXTERN_FUNC_ENTER;
1085
1086         // check mandatory parameter
1087         retvm_if(profile_id < 1, SYNC_AGENT_DS_FAIL, "profile_id is invalid!!");
1088
1089         int event_type = SYNC_AGENT_DS_GET_PROFILE;
1090         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1091         sync_agent_event_data_s *request_event = NULL;
1092         sync_agent_event_data_s *response_event = NULL;
1093
1094         gboolean result_sync_category = FALSE;
1095         sync_agent_ds_error_e result = SYNC_AGENT_DS_FAIL;
1096         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1097         sync_agent_ds_service_info *category = NULL;
1098
1099         //////////////////////////////////
1100         //
1101         // Phase 1. get profile detail info
1102
1103         /* create empty event packet */
1104         request_event = sync_agent_create_event(event_type);
1105         if (request_event == NULL) {
1106                 _DEBUG_ERROR("event is NULL");
1107                 _EXTERN_FUNC_EXIT;
1108                 return SYNC_AGENT_DS_FAIL;
1109         }
1110
1111         /* add request parameter to event packet */
1112         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &profile_id);
1113         _DEBUG_INFO("profile_id = %d", profile_id);
1114
1115         /* send event request to ds agent daemon, waiting for response */
1116         response_event = sync_agent_send_event(request_event, &error);
1117         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1118                 _DEBUG_ERROR("error = %d", error);
1119                 sync_agent_free_event(request_event);
1120                 _EXTERN_FUNC_EXIT;
1121                 return SYNC_AGENT_DS_FAIL;
1122         }
1123         if (response_event == NULL) {
1124                 _DEBUG_ERROR("response_event is null!!");
1125                 sync_agent_free_event(request_event);
1126                 _EXTERN_FUNC_EXIT;
1127                 return SYNC_AGENT_DS_FAIL;
1128         }
1129
1130         /* get response parameter from event packet */
1131
1132         result = sync_agent_ds_create_profile_info(profile_h);
1133         if (result == SYNC_AGENT_DS_FAIL) {
1134                 _DEBUG_ERROR("failed to create profile info!");
1135                 sync_agent_free_event(request_event);
1136                 sync_agent_free_event_data(response_event);
1137                 return FALSE;
1138         }
1139
1140         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) (*profile_h);
1141         sync_agent_ds_server_info *server_info = &profile_info->server_info;
1142         sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
1143 //      GList * list = profile_info->category_list;
1144
1145         sync_agent_get_event_data_param_int(response_event, &api_result);
1146         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1147                 _DEBUG_ERROR("api_result = failed");
1148                 sync_agent_free_event(request_event);
1149                 sync_agent_free_event_data(response_event);
1150                 _EXTERN_FUNC_EXIT;
1151                 return SYNC_AGENT_DS_FAIL;
1152         }
1153
1154         //profile_info->profile_dir_name = g_strdup(profile_dir_name);
1155
1156         sync_agent_get_event_data_param_str(response_event, &profile_info->profile_dir_name);
1157         sync_agent_get_event_data_param_str(response_event, &profile_info->profile_name);
1158         sync_agent_get_event_data_param_str(response_event, &server_info->addr);
1159         sync_agent_get_event_data_param_str(response_event, &server_info->id);
1160         sync_agent_get_event_data_param_str(response_event, &server_info->password);
1161         sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->sync_mode);
1162         sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->sync_type);
1163         sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->interval);
1164         sync_agent_get_event_data_param_int(response_event, &profile_info->last_sync_status);
1165         sync_agent_get_event_data_param_int(response_event, &profile_info->last_sync_time);
1166
1167         _DEBUG_INFO("profile_dir_name = %s", profile_info->profile_dir_name);
1168         _DEBUG_INFO("profile_name = %s", profile_info->profile_name);
1169         _DEBUG_INFO("addr = %s", server_info->addr);
1170         _DEBUG_INFO("id = %s", server_info->id);
1171         _DEBUG_INFO("password = %s", server_info->password);
1172         _DEBUG_INFO("sync_mode = %d", sync_info->sync_mode);
1173         _DEBUG_INFO("sync_type = %d", sync_info->sync_type);
1174         _DEBUG_INFO("interval = %d", sync_info->interval);
1175         _DEBUG_VERBOSE("lastSyncStatus = %d\n", profile_info->last_sync_status);
1176         _DEBUG_VERBOSE("lastSyncTime = %d\n", profile_info->last_sync_time);
1177
1178         /* free request & response event */
1179         sync_agent_free_event(request_event);
1180         sync_agent_free_event_data(response_event);
1181
1182         // Phase 2. get sync category info
1183
1184         /* get contacts sync_category */
1185         result_sync_category = _get_sync_category(profile_info->profile_dir_name, SYNC_AGENT_DS_SYNC_CONTACTS, &category);
1186         if (result_sync_category)
1187                 profile_info->service_list = g_list_append(profile_info->service_list, category);
1188
1189         /* get schedule sync_category */
1190         category = NULL;
1191         result_sync_category = _get_sync_category(profile_info->profile_dir_name, SYNC_AGENT_DS_SYNC_SCHEDULE, &category);
1192         if (result_sync_category)
1193                 profile_info->service_list = g_list_append(profile_info->service_list, category);
1194
1195         _EXTERN_FUNC_EXIT;
1196
1197         return SYNC_AGENT_DS_SUCCESS;
1198 }
1199
1200 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_all_profile(GList ** profile_list)
1201 {
1202         _EXTERN_FUNC_ENTER;
1203
1204         int event_type = SYNC_AGENT_DS_GET_ALL_PROFILES;
1205         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1206         sync_agent_event_data_s *request_event = NULL;
1207         sync_agent_event_data_s *response_event = NULL;
1208
1209         ds_profile_h profile_h = NULL;
1210         sync_agent_ds_service_info *category_info = NULL;
1211
1212         int profile_count = 0;
1213         int category_count = 0;
1214
1215         sync_agent_ds_error_e result = SYNC_AGENT_DS_FAIL;
1216         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1217
1218         /* create empty event packet */
1219         request_event = sync_agent_create_event(event_type);
1220         if (request_event == NULL) {
1221                 _DEBUG_ERROR("event is NULL");
1222                 _EXTERN_FUNC_EXIT;
1223                 return SYNC_AGENT_DS_FAIL;
1224         }
1225
1226         /* send event request to ds agent daemon, waiting for response */
1227         response_event = sync_agent_send_event(request_event, &error);
1228         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1229                 _DEBUG_ERROR("error = %d", error);
1230                 sync_agent_free_event(request_event);
1231                 _EXTERN_FUNC_EXIT;
1232                 return SYNC_AGENT_DS_FAIL;
1233         }
1234         if (response_event == NULL) {
1235                 _DEBUG_ERROR("response_event is null!!");
1236                 sync_agent_free_event(request_event);
1237                 _EXTERN_FUNC_EXIT;
1238                 return SYNC_AGENT_DS_FAIL;
1239         }
1240
1241         sync_agent_get_event_data_param_int(response_event, &api_result);
1242         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1243                 _DEBUG_ERROR("api_result = failed");
1244                 sync_agent_free_event(request_event);
1245                 sync_agent_free_event_data(response_event);
1246                 _EXTERN_FUNC_EXIT;
1247                 return SYNC_AGENT_DS_FAIL;
1248         }
1249
1250         sync_agent_get_event_data_param_int(response_event, &profile_count);
1251         _DEBUG_INFO("profile_count = %d", profile_count);
1252
1253         int i;
1254         for (i = 0; i < profile_count; i++) {
1255
1256                 result = sync_agent_ds_create_profile_info(&profile_h);
1257                 if (result == SYNC_AGENT_DS_FAIL) {
1258                         _DEBUG_ERROR("failed to create profile info!");
1259                         sync_agent_free_event(request_event);
1260                         sync_agent_free_event_data(response_event);
1261                         return FALSE;
1262                 }
1263
1264                 sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1265                 sync_agent_ds_server_info *server_info = &profile_info->server_info;
1266                 sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
1267
1268                 sync_agent_get_event_data_param_int(response_event, &profile_info->profile_id);
1269                 sync_agent_get_event_data_param_str(response_event, &profile_info->profile_dir_name);
1270                 sync_agent_get_event_data_param_str(response_event, &profile_info->profile_name);
1271                 sync_agent_get_event_data_param_str(response_event, &server_info->addr);
1272                 sync_agent_get_event_data_param_str(response_event, &server_info->id);
1273                 sync_agent_get_event_data_param_str(response_event, &server_info->password);
1274                 sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->sync_mode);
1275                 sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->sync_type);
1276                 sync_agent_get_event_data_param_int(response_event, (int*) &sync_info->interval);
1277                 sync_agent_get_event_data_param_int(response_event, &profile_info->last_sync_status);
1278                 sync_agent_get_event_data_param_int(response_event, &profile_info->last_sync_time);
1279
1280                 _DEBUG_INFO("profile_info->profile_id = %d", profile_info->profile_id);
1281                 _DEBUG_INFO("profile_info->profile_dir_name = %s", profile_info->profile_dir_name);
1282                 _DEBUG_INFO("profile_info->profile_name = %s", profile_info->profile_name);
1283                 _DEBUG_INFO("profile_info->addr = %s", server_info->addr);
1284                 _DEBUG_INFO("profile_info->id = %s", server_info->id);
1285                 _DEBUG_INFO("profile_info->password = %s", server_info->password);
1286                 _DEBUG_INFO("profile_info->sync_mode = %d", sync_info->sync_mode);
1287                 _DEBUG_INFO("profile_info->sync_type = %d", sync_info->sync_type);
1288                 _DEBUG_INFO("profile_info->interval = %d", sync_info->interval);
1289                 _DEBUG_INFO("profile_info->lastSyncStatus = %d", profile_info->last_sync_status);
1290                 _DEBUG_INFO("profile_info->lastSyncStatus = %d", profile_info->last_sync_status);
1291
1292                 sync_agent_get_event_data_param_int(response_event, &category_count);
1293
1294                 int j;
1295                 for (j = 0; j < category_count; j++) {
1296
1297                         category_info = (sync_agent_ds_service_info *) calloc(1, sizeof(sync_agent_ds_service_info));
1298                         if (category_info == NULL) {
1299                                 _DEBUG_ERROR("calloc failed");
1300                                 sync_agent_ds_free_profile_info(profile_h);
1301                                 sync_agent_free_event(request_event);
1302                                 sync_agent_free_event_data(response_event);
1303                                 return FALSE;
1304                         }
1305
1306                         sync_agent_get_event_data_param_int(response_event, (int*) &category_info->service_type);
1307                         sync_agent_get_event_data_param_int(response_event, &category_info->enabled);
1308                         sync_agent_get_event_data_param_int(response_event, (int*) &category_info->src_uri);
1309                         sync_agent_get_event_data_param_str(response_event, &category_info->tgt_uri);
1310                         sync_agent_get_event_data_param_str(response_event, &category_info->id);
1311                         sync_agent_get_event_data_param_str(response_event, &category_info->password);
1312
1313                         _DEBUG_INFO("category_info->service_type = %d", category_info->service_type);
1314                         _DEBUG_INFO("category_info->enabled = %d", category_info->enabled);
1315                         _DEBUG_INFO("category_info->src_uri = %d", category_info->src_uri);
1316                         _DEBUG_INFO("category_info->tgt_uri = %s", category_info->tgt_uri);
1317                         _DEBUG_INFO("category_info->id = %s", category_info->id);
1318                         _DEBUG_INFO("category_info->password = %s", category_info->password);
1319
1320                         profile_info->service_list = g_list_append(profile_info->service_list, category_info);
1321                         category_info = NULL;
1322                 }
1323
1324                 *profile_list = g_list_append(*profile_list, profile_h);
1325                 profile_h = NULL;
1326         }
1327
1328         /* free request & response event */
1329         sync_agent_free_event(request_event);
1330         sync_agent_free_event_data(response_event);
1331
1332         _EXTERN_FUNC_EXIT;
1333
1334         return SYNC_AGENT_DS_SUCCESS;
1335 }
1336
1337 EXPORT_API sync_agent_ds_error_e sync_agent_ds_get_last_sync_info(ds_profile_h profile_h, int *lastSyncStatus, int *lastSyncTime)
1338 {
1339         _EXTERN_FUNC_ENTER;
1340
1341         // check mandatory parameter
1342         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1343         retvm_if(lastSyncStatus == NULL, SYNC_AGENT_DS_FAIL, "last session status is null!!");
1344         retvm_if(lastSyncTime == NULL, SYNC_AGENT_DS_FAIL, "last session time is null!!");
1345
1346         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1347
1348         *lastSyncStatus = profile_info->last_sync_status;
1349         *lastSyncTime = profile_info->last_sync_time;
1350
1351         _EXTERN_FUNC_EXIT;
1352
1353         return SYNC_AGENT_DS_SUCCESS;
1354 }
1355
1356 EXPORT_API sync_agent_ds_error_e sync_agent_ds_update_profile(ds_profile_h profile_h)
1357 {
1358         _EXTERN_FUNC_ENTER;
1359
1360         // check mandatory parameter
1361         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1362
1363         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1364         sync_agent_ds_server_info *server_info = &profile_info->server_info;
1365         sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
1366         GList *list = profile_info->service_list;
1367
1368         int event_type = SYNC_AGENT_DS_UPDATE_PROFILE;
1369         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1370         sync_agent_event_data_s *request_event = NULL;
1371         sync_agent_event_data_s *response_event = NULL;
1372
1373         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1374         sync_agent_ds_service_info *category_info = NULL;
1375         int category_count = 0;
1376         int i = 0;
1377
1378         /* create empty event packet */
1379         request_event = sync_agent_create_event(event_type);
1380         if (request_event == NULL) {
1381                 _DEBUG_ERROR("event is NULL");
1382                 _EXTERN_FUNC_EXIT;
1383                 return SYNC_AGENT_DS_FAIL;
1384         }
1385
1386         /* add request parameter to event packet */
1387         _DEBUG_INFO("profile_dir_name = %s", profile_info->profile_dir_name);
1388         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name);
1389         _DEBUG_INFO("profile_name = %s", profile_info->profile_name);
1390         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_name);
1391         _DEBUG_INFO("addr = %s", server_info->addr);
1392         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->addr);
1393         _DEBUG_INFO("id = %s", server_info->id);
1394         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->id);
1395         _DEBUG_INFO("password = %s", server_info->password);
1396         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)server_info->password);
1397         _DEBUG_INFO("sync_mode = %d", sync_info->sync_mode);
1398         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->sync_mode));
1399         _DEBUG_INFO("sync_type = %d", sync_info->sync_type);
1400         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->sync_type));
1401         _DEBUG_INFO("interval = %d", sync_info->interval);
1402         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(sync_info->interval));
1403
1404         category_count = g_list_length(list);
1405         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &category_count);
1406
1407         for (; i < category_count; i++) {
1408                 category_info = (sync_agent_ds_service_info *) g_list_nth_data(list, i);
1409
1410                 _DEBUG_INFO("category[%d]->service_type = %d", i, category_info->service_type);
1411                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->service_type));
1412                 _DEBUG_INFO("category[%d]->enabled = %d", i, category_info->enabled);
1413                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->enabled));
1414                 _DEBUG_INFO("category[%d]->src_uri = %d", i, category_info->src_uri);
1415                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &(category_info->src_uri));
1416                 _DEBUG_INFO("category[%d]->tgt_uri = %s", i, category_info->tgt_uri);
1417                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->tgt_uri);
1418                 _DEBUG_INFO("category[%d]->id = %s", i, category_info->id);
1419                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->id);
1420                 _DEBUG_INFO("category[%d]->password = %s", i, category_info->password);
1421                 sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)category_info->password);
1422         }
1423
1424         /* send event request to ds agent daemon, waiting for response */
1425         response_event = sync_agent_send_event(request_event, &error);
1426         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1427                 _DEBUG_ERROR("error = %d", error);
1428                 sync_agent_free_event(request_event);
1429                 _EXTERN_FUNC_EXIT;
1430                 return SYNC_AGENT_DS_FAIL;
1431         }
1432         if (response_event == NULL) {
1433                 _DEBUG_ERROR("response_event is null!!");
1434                 sync_agent_free_event(request_event);
1435                 _EXTERN_FUNC_EXIT;
1436                 return SYNC_AGENT_DS_FAIL;
1437         }
1438
1439         /* get response parameter from event packet */
1440         sync_agent_get_event_data_param_int(response_event, &api_result);
1441
1442         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1443                 _DEBUG_ERROR("api_result = failed");
1444                 sync_agent_free_event(request_event);
1445                 sync_agent_free_event_data(response_event);
1446                 _EXTERN_FUNC_EXIT;
1447                 return SYNC_AGENT_DS_FAIL;
1448         }
1449
1450         /* free request & response event */
1451         sync_agent_free_event(request_event);
1452         sync_agent_free_event_data(response_event);
1453
1454         _EXTERN_FUNC_EXIT;
1455
1456         return SYNC_AGENT_DS_SUCCESS;
1457 }
1458
1459 EXPORT_API sync_agent_ds_error_e sync_agent_ds_delete_profile(ds_profile_h profile_h)
1460 {
1461         _EXTERN_FUNC_ENTER;
1462
1463         // check mandatory parameter
1464         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1465
1466         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1467
1468         retvm_if(profile_info->profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
1469
1470         int event_type = SYNC_AGENT_DS_DELETE_PROFILE;
1471         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1472         sync_agent_event_data_s *request_event = NULL;
1473         sync_agent_event_data_s *response_event = NULL;
1474
1475         int count = 1;
1476         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1477
1478         /* create empty event packet */
1479         request_event = sync_agent_create_event(event_type);
1480         if (request_event == NULL) {
1481                 _DEBUG_ERROR("event is NULL");
1482                 return SYNC_AGENT_DS_FAIL;
1483         }
1484
1485         /* add request parameter to event packet */
1486         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_INTEGER, &count);
1487
1488         _DEBUG_INFO("profile_dir_name = %s", profile_info->profile_dir_name);
1489         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name);
1490
1491         /* send event request to ds agent daemon, waiting for response */
1492         response_event = sync_agent_send_event(request_event, &error);
1493         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1494                 _DEBUG_ERROR("error = %d", error);
1495                 sync_agent_free_event(request_event);
1496                 _EXTERN_FUNC_EXIT;
1497                 return SYNC_AGENT_DS_FAIL;
1498         }
1499         if (response_event == NULL) {
1500                 _DEBUG_ERROR("response_event is null!!");
1501                 sync_agent_free_event(request_event);
1502                 _EXTERN_FUNC_EXIT;
1503                 return SYNC_AGENT_DS_FAIL;
1504         }
1505
1506         /* get response parameter from event packet */
1507         sync_agent_get_event_data_param_int(response_event, &api_result);
1508
1509         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1510                 _DEBUG_ERROR("api_result = failed");
1511                 sync_agent_free_event(request_event);
1512                 sync_agent_free_event_data(response_event);
1513                 _EXTERN_FUNC_EXIT;
1514                 return SYNC_AGENT_DS_FAIL;
1515         }
1516
1517         /* free request & response event */
1518         sync_agent_free_event(request_event);
1519         sync_agent_free_event_data(response_event);
1520
1521         _EXTERN_FUNC_EXIT;
1522
1523         return SYNC_AGENT_DS_SUCCESS;
1524 }
1525
1526 EXPORT_API sync_agent_ds_error_e sync_agent_ds_start_sync(ds_profile_h profile_h)
1527 {
1528         _EXTERN_FUNC_ENTER;
1529
1530         // check mandatory parameter
1531         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1532
1533         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1534
1535         retvm_if(profile_info->profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
1536
1537         int event_type = SYNC_AGENT_DS_START_SYNC;
1538         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1539         sync_agent_event_data_s *request_event = NULL;
1540         sync_agent_event_data_s *response_event = NULL;
1541
1542         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1543
1544         /* create empty event packet */
1545         request_event = sync_agent_create_event(event_type);
1546         if (request_event == NULL) {
1547                 _DEBUG_ERROR("event is NULL");
1548                 return SYNC_AGENT_DS_FAIL;
1549         }
1550
1551         /* add request parameter to event packet */
1552         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name);
1553
1554         /* send event request to ds agent daemon, waiting for response */
1555         response_event = sync_agent_send_event(request_event, &error);
1556         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1557                 _DEBUG_ERROR("error = %d", error);
1558                 sync_agent_free_event(request_event);
1559                 _EXTERN_FUNC_EXIT;
1560                 return SYNC_AGENT_DS_FAIL;
1561         }
1562         if (response_event == NULL) {
1563                 _DEBUG_ERROR("response_event is null!!");
1564                 sync_agent_free_event(request_event);
1565                 _EXTERN_FUNC_EXIT;
1566                 return SYNC_AGENT_DS_FAIL;
1567         }
1568
1569         /* get response parameter from event packet */
1570         sync_agent_get_event_data_param_int(response_event, &api_result);
1571
1572         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1573                 _DEBUG_ERROR("api_result = failed");
1574                 sync_agent_free_event(request_event);
1575                 sync_agent_free_event_data(response_event);
1576                 _EXTERN_FUNC_EXIT;
1577                 return SYNC_AGENT_DS_FAIL;
1578         }
1579
1580         /* free request & response event */
1581         sync_agent_free_event(request_event);
1582         sync_agent_free_event_data(response_event);
1583
1584         _EXTERN_FUNC_EXIT;
1585
1586         return SYNC_AGENT_DS_SUCCESS;
1587 }
1588
1589 EXPORT_API sync_agent_ds_error_e sync_agent_ds_stop_sync(ds_profile_h profile_h)
1590 {
1591         _EXTERN_FUNC_ENTER;
1592
1593         // check mandatory parameter
1594         retvm_if(profile_h == NULL, SYNC_AGENT_DS_FAIL, "profile handle is null!!");
1595
1596         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1597
1598         retvm_if(profile_info->profile_dir_name == NULL, SYNC_AGENT_DS_FAIL, "profile_dir_name is null!!");
1599
1600         int event_type = SYNC_AGENT_DS_STOP_SYNC;
1601         sync_agent_event_error_e error = SYNC_AGENT_EVENT_SUCCESS;
1602         sync_agent_event_data_s *request_event = NULL;
1603         sync_agent_event_data_s *response_event = NULL;
1604
1605         int api_result = SYNC_AGENT_DS_API_RESULT_SUCCESS;
1606
1607         /* create empty event packet */
1608         request_event = sync_agent_create_event(event_type);
1609         if (request_event == NULL) {
1610                 _DEBUG_ERROR("event is NULL");
1611                 return SYNC_AGENT_DS_FAIL;
1612         }
1613
1614         /* add request parameter to event packet */
1615         sync_agent_append_event_data_param(request_event, SYNC_AGENT_EVENT_PARAM_TYPE_STRING, (void *)profile_info->profile_dir_name);
1616
1617         /* send event request to ds agent daemon, waiting for response */
1618         response_event = sync_agent_send_event(request_event, &error);
1619         if (error != SYNC_AGENT_EVENT_SUCCESS) {
1620                 _DEBUG_ERROR("error = %d", error);
1621                 sync_agent_free_event(request_event);
1622                 _EXTERN_FUNC_EXIT;
1623                 return SYNC_AGENT_DS_FAIL;
1624         }
1625         if (response_event == NULL) {
1626                 _DEBUG_ERROR("response_event is null!!");
1627                 sync_agent_free_event(request_event);
1628                 _EXTERN_FUNC_EXIT;
1629                 return SYNC_AGENT_DS_FAIL;
1630         }
1631
1632         /* get response parameter from event packet */
1633         sync_agent_get_event_data_param_int(response_event, &api_result);
1634
1635         if (api_result == SYNC_AGENT_DS_API_RESULT_FAILURE) {
1636                 _DEBUG_ERROR("api_result = failed");
1637                 sync_agent_free_event(request_event);
1638                 sync_agent_free_event_data(response_event);
1639                 _EXTERN_FUNC_EXIT;
1640                 return SYNC_AGENT_DS_FAIL;
1641         }
1642
1643         /* free request & response event */
1644         sync_agent_free_event(request_event);
1645         sync_agent_free_event_data(response_event);
1646
1647         _EXTERN_FUNC_EXIT;
1648
1649         return SYNC_AGENT_DS_SUCCESS;
1650 }
1651
1652 EXPORT_API void sync_agent_ds_free_profile_info(ds_profile_h profile_h)
1653 {
1654         _EXTERN_FUNC_ENTER;
1655
1656         // check mandatory parameter
1657         retm_if(profile_h == NULL, "profile handle is null!!");
1658
1659         sync_agent_ds_profile_info *profile_info = (sync_agent_ds_profile_info *) profile_h;
1660
1661         sync_agent_ds_server_info *server_info = &profile_info->server_info;
1662         //sync_agent_ds_sync_info *sync_info = &profile_info->sync_info;
1663         GList *category_list = profile_info->service_list;
1664
1665         // free profile name
1666         g_free(profile_info->profile_dir_name);
1667         g_free(profile_info->profile_name);
1668
1669         // free server information
1670         g_free(server_info->addr);
1671         g_free(server_info->id);
1672         g_free(server_info->password);
1673
1674         // free sync category information
1675         g_list_free_full(category_list, (GDestroyNotify) _free_sync_category_info);
1676
1677         // free profile information
1678         g_free(profile_info);
1679
1680         _EXTERN_FUNC_EXIT;
1681 }
1682
1683 EXPORT_API sync_agent_ds_error_e sync_agent_ds_deinit()
1684 {
1685         _EXTERN_FUNC_ENTER;
1686
1687         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
1688         sync_agent_deinit_error_e deinit = SYNC_AGENT_DEINIT_SUCCESS;
1689         int ret = -1;
1690
1691         ret = _kill_omads_agent();
1692
1693         if (ret < 0 ) {
1694                 _DEBUG_ERROR("_kill_omads_agent() failed !!");
1695                 result = SYNC_AGENT_DS_FAIL;
1696
1697                 _EXTERN_FUNC_EXIT;
1698                 return result;
1699         }
1700
1701         deinit = sync_agent_deinit();
1702         if (deinit != SYNC_AGENT_DEINIT_SUCCESS) {
1703                 _DEBUG_ERROR("sync_agent_clean_event_handler() failed !!");
1704                 result = SYNC_AGENT_DS_FAIL;
1705         }
1706 //      if (err != SYNC_AGENT_EVENT_SUCCESS) {
1707 //      sync_agent_event_error_e err = sync_agent_stop_noti_listener();
1708 //              _DEBUG_INFO("STOP NOTILISTNER is failed");
1709 //              result = SYNC_AGENT_DS_FAIL;
1710 //      }
1711 //
1712 //      err = sync_agent_clean_event_handler();
1713 //      if (err != SYNC_AGENT_EVENT_SUCCESS) {
1714 //              _DEBUG_ERROR("sync_agent_clean_event_handler() failed !!");
1715 //              result = SYNC_AGENT_DS_FAIL;
1716 //      }
1717
1718         _EXTERN_FUNC_EXIT;
1719
1720         return result;
1721 }
1722
1723 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)
1724 {
1725         _EXTERN_FUNC_ENTER;
1726
1727         sync_agent_ds_error_e result = SYNC_AGENT_DS_SUCCESS;
1728
1729         sync_agent_event_error_e err = sync_agent_set_noti_callback(type, callback, data);
1730         if (err != SYNC_AGENT_EVENT_SUCCESS) {
1731                 _DEBUG_ERROR("failed sync_agent_set_noti_callback()");
1732                 result = SYNC_AGENT_DS_FAIL;
1733         }
1734
1735         return result;
1736
1737         _EXTERN_FUNC_EXIT;
1738 }
1739