Apply contact handle policy
[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 (CONTACTS_ERROR_NO_DATA == ret) {
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 (CONTACTS_ERROR_NO_DATA == ret) {
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(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
82         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
83
84         ret = ctsvc_client_disconnect(contact);
85         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect() Fail(%d)", ret);
86
87         if (0 == ((ctsvc_base_s *)contact)->connection_count) {
88                 ret = ctsvc_client_handle_remove(id, contact);
89                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_handle_remove() Fail(%d)", ret);
90         }
91
92         return ret;
93 }
94
95 API int contacts_connect_on_thread(void)
96 {
97         CTS_FN_CALL;
98         int ret;
99         contacts_h contact = NULL;
100         unsigned int id = ctsvc_client_get_tid();
101
102         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
103         if (CONTACTS_ERROR_NO_DATA == ret) {
104                 ret = ctsvc_client_handle_create(id, &contact);
105                 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_create() Fail(%d)", ret);
106         }
107         else if (CONTACTS_ERROR_NONE != ret) {
108                 CTS_ERR("ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
109                 return ret;
110         }
111
112         ret = ctsvc_client_connect_on_thread(contact);
113         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_connect_on_thread() Fail(%d)", ret);
114
115         return ret;
116 }
117
118 API int contacts_disconnect_on_thread(void)
119 {
120         CTS_FN_CALL;
121         int ret;
122         contacts_h contact = NULL;
123         unsigned int id = ctsvc_client_get_tid();
124
125         ret = ctsvc_client_handle_get_p_with_id(id, &contact);
126         RETV_IF(CONTACTS_ERROR_NO_DATA == ret, CONTACTS_ERROR_NONE);
127         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p_with_id() Fail(%d)", ret);
128
129         ret = ctsvc_client_disconnect_on_thread(contact);
130         WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_disconnect_on_thread() Fail(%d)", ret);
131
132         if (0 == ((ctsvc_base_s *)contact)->connection_count) {
133                 ret = ctsvc_client_handle_remove(id, contact);
134                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_client_handle_remove() Fail(%d)", ret);
135         }
136
137         return ret;
138 }
139