Tizen 2.1 base
[platform/core/pim/contacts-service.git] / client / ctsvc_client_service.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2012 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 <errno.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27 #include <pims-ipc-data.h>
28
29 #include "contacts.h"
30 #include "ctsvc_internal.h"
31 #include "ctsvc_socket.h"
32 #include "ctsvc_mutex.h"
33 #include "ctsvc_inotify.h"
34 #include "ctsvc_setting.h"
35 #include "ctsvc_client_ipc.h"
36
37 static int ctsvc_connection = 0;
38
39 static int __thread ctsvc_connection_on_thread = 0;
40
41 API int contacts_connect_with_flags(unsigned int flags)
42 {
43         CTS_FN_CALL;
44         int ret = CONTACTS_ERROR_NONE;
45
46         ret = contacts_connect2();
47         if (ret == CONTACTS_ERROR_NONE)
48                 return ret;
49
50         if (flags & CONTACTS_CONNECT_FLAG_RETRY) {
51                 int i;
52                 int waiting_time = 500;
53                 for (i=0;i<6;i++) {
54                         usleep(waiting_time * 1000);
55                         DBG("retry cnt=%d, ret=%x, %d",(i+1), ret, waiting_time);
56                         ret = contacts_connect2();
57                         if (ret == CONTACTS_ERROR_NONE)
58                                 break;
59                         waiting_time *= 2;
60                 }
61         }
62
63         return ret;
64 }
65
66 API int contacts_connect2()
67 {
68         CTS_FN_CALL;
69         int ret;
70
71         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
72         if (0 == ctsvc_connection) {
73
74                 ret = ctsvc_ipc_connect();
75                 if (ret != CONTACTS_ERROR_NONE) {
76                         CTS_ERR("ctsvc_ipc_connect() Failed(%d)", ret);
77                         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
78                         return ret;
79                 }
80
81                 ret = ctsvc_socket_init();
82                 if (ret != CONTACTS_ERROR_NONE) {
83                         CTS_ERR("ctsvc_socket_init() Failed(%d)", ret);
84                         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
85                         return ret;
86                 }
87
88                 ret = ctsvc_inotify_init();
89                 if (ret != CONTACTS_ERROR_NONE) {
90                         CTS_ERR("ctsvc_inotify_init() Failed(%d)", ret);
91                         ctsvc_socket_final();
92                         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
93                         return ret;
94                 }
95
96                 ctsvc_view_uri_init();
97                 ctsvc_register_vconf();
98                 ctsvc_ipc_create_for_change_subscription();
99         }
100         else
101                 CTS_DBG("System : Contacts service has been already connected(%d)", ctsvc_connection + 1);
102
103         ctsvc_connection++;
104         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
105
106         return CONTACTS_ERROR_NONE;
107 }
108
109 API int contacts_disconnect2()
110 {
111         int ret;
112
113         CTS_FN_CALL;
114
115         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
116         if (1 == ctsvc_connection) {
117                 ctsvc_ipc_destroy_for_change_subscription();
118
119                 ret = ctsvc_ipc_disconnect();
120                 if (ret != CONTACTS_ERROR_NONE) {
121                         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
122                         CTS_ERR("ctsvc_ipc_disconnect() Failed(%d)", ret);
123                         return ret;
124                 }
125
126                 ctsvc_view_uri_deinit();
127                 ctsvc_inotify_close();
128                 ctsvc_socket_final();
129                 ctsvc_deregister_vconf();
130
131         }
132         else if (1 < ctsvc_connection)
133                 CTS_DBG("System : connection count is %d", ctsvc_connection);
134         else {
135                 CTS_DBG("System : please call contacts_connect2(), connection count is (%d)", ctsvc_connection);
136                 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
137                 return CONTACTS_ERROR_INVALID_PARAMETER;
138         }
139
140         ctsvc_connection--;
141         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
142
143         return CONTACTS_ERROR_NONE;
144 }
145
146 API int contacts_connect_on_thread()
147 {
148         int ret;
149
150         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
151
152         ret = ctsvc_ipc_connect_on_thread();
153         if (ret != CONTACTS_ERROR_NONE) {
154                 CTS_ERR("ctsvc_ipc_connect_on_thread() Failed(%d)", ret);
155                 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
156                 return ret;
157         }
158
159         if (0 < ctsvc_connection_on_thread)
160                 CTS_DBG("System : Contacts service has been already connected");
161
162         ctsvc_connection_on_thread++;
163
164         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
165
166         return CONTACTS_ERROR_NONE;
167 }
168
169 API int contacts_disconnect_on_thread()
170 {
171         int ret;
172
173         ctsvc_mutex_lock(CTS_MUTEX_CONNECTION);
174
175         ret = ctsvc_ipc_disconnect_on_thread();
176         if (ret != CONTACTS_ERROR_NONE) {
177                 CTS_ERR("ctsvc_ipc_disconnect_on_thread() Failed(%d)", ret);
178                 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
179                 return ret;
180         }
181
182         if (1 == ctsvc_connection_on_thread) {
183                 CTS_DBG("System : connection_on_thread was destroyed successfully");
184         }
185         else if (1 < ctsvc_connection_on_thread) {
186                 CTS_DBG("System : connection count is %d", ctsvc_connection_on_thread);
187         }
188         else {
189                 CTS_DBG("System : please call contacts_connect_on_thread(), connection count is (%d)", ctsvc_connection_on_thread);
190                 ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
191                 return CONTACTS_ERROR_INVALID_PARAMETER;
192         }
193
194         ctsvc_connection_on_thread--;
195
196         ctsvc_mutex_unlock(CTS_MUTEX_CONNECTION);
197
198         return CONTACTS_ERROR_NONE;
199 }