fixed random bs in tc
[platform/core/pim/contacts-service.git] / client / ctsvc_client_service.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Dohyung Jin <dh.jin@samsung.com>
7  *                 Jongwon Lee <gogosing.lee@samsung.com>
8  *                 Donghee Ye <donghee.ye@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23 #include "contacts.h"
24 #include "ctsvc_internal.h"
25 #include "ctsvc_client_ipc.h"
26 #include "ctsvc_client_utils.h"
27 #include "ctsvc_client_handle.h"
28 #include "ctsvc_client_service_helper.h"
29
30 API int contacts_connect_with_flags(unsigned int flags)
31 {
32         CTS_FN_CALL;
33         int ret;
34         contacts_h contact = NULL;
35         unsigned int id = ctsvc_client_get_pid();
36
37         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
38         if (NULL == contact) {
39                 ret = ctsvc_client_handle_create(id, &contact);
40                 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_create() Fail(%d)", ret);
41         }
42         else if (CONTACTS_ERROR_NONE != ret) {
43                 CTS_ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
44                 return ret;
45         }
46
47         ret = ctsvc_client_connect_with_flags(contact, flags);
48
49         return ret;
50 }
51
52 API int contacts_connect(void)
53 {
54         CTS_FN_CALL;
55         int ret;
56         contacts_h contact = NULL;
57         unsigned int id = ctsvc_client_get_pid();
58
59         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
60         if (NULL == contact) {
61                 ret = ctsvc_client_handle_create(id, &contact);
62                 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_create() Fail(%d)", ret);
63         }
64         else if (CONTACTS_ERROR_NONE != ret) {
65                 CTS_ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
66                 return ret;
67         }
68         ret = ctsvc_client_connect(contact);
69
70         return ret;
71 }
72
73 API int contacts_disconnect(void)
74 {
75         CTS_FN_CALL;
76         int ret;
77         contacts_h contact = NULL;
78         unsigned int id = ctsvc_client_get_pid();
79
80         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
81         RETV_IF(NULL == contact, CONTACTS_ERROR_NONE);
82         RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
83         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
84
85         ret = ctsvc_client_disconnect(contact);
86         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect() Fail(%d)", ret);
87
88         if (0 == ((ctsvc_base_s *)contact)->connection_count) {
89                 ret = ctsvc_client_handle_remove(id, contact);
90                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_handle_remove() Fail(%d)", ret);
91         }
92
93         return ret;
94 }
95
96 API int contacts_connect_on_thread(void)
97 {
98         CTS_FN_CALL;
99         int ret;
100         contacts_h contact = NULL;
101         unsigned int id = ctsvc_client_get_tid();
102
103         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
104         if (NULL == contact) {
105                 ret = ctsvc_client_handle_create(id, &contact);
106                 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_create() Fail(%d)", ret);
107         }
108         else if (CONTACTS_ERROR_NONE != ret) {
109                 CTS_ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
110                 return ret;
111         }
112
113         ret = ctsvc_client_connect_on_thread(contact);
114         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_connect_on_thread() Fail(%d)", ret);
115
116         return ret;
117 }
118
119 API int contacts_disconnect_on_thread(void)
120 {
121         CTS_FN_CALL;
122         int ret;
123         contacts_h contact = NULL;
124         unsigned int id = ctsvc_client_get_tid();
125
126         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
127         RETV_IF(NULL == contact, CONTACTS_ERROR_NONE);
128         RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
129         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
130
131         ret = ctsvc_client_disconnect_on_thread(contact);
132         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect_on_thread() Fail(%d)", ret);
133
134         if (0 == ((ctsvc_base_s *)contact)->connection_count) {
135                 ret = ctsvc_client_handle_remove(id, contact);
136                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_handle_remove() Fail(%d)", ret);
137         }
138
139         return ret;
140 }
141