3a2d9d5e868739b4283e15c68b53fc3860f53e33
[platform/core/pim/calendar-service.git] / client / cal_client_service.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "calendar.h"
21 #include "cal_internal.h"
22 #include "cal_client_handle.h"
23 #include "cal_client_service_helper.h"
24 #include "cal_client_utils.h"
25
26 static int connection_count = 0;
27 static TLS int connection_count_on_thread = 0;
28
29 API int calendar_connect(void)
30 {
31         CAL_FN_CALL();
32         int ret;
33         calendar_h handle = NULL;
34         unsigned int id = cal_client_get_pid();
35
36         ret = cal_client_handle_get_p_with_id(id, &handle);
37         if (CALENDAR_ERROR_NO_DATA == ret) {
38                 ret = cal_client_handle_create(id, &handle);
39                 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_client_handle_create() Fail(%d)", ret);
40         } else if (CALENDAR_ERROR_NONE != ret) {
41                 ERR("cal_client_handle_get_p_with_id() Fail(%d)", ret);
42                 return ret;
43         }
44         ret = cal_client_connect(handle, id, &connection_count);
45         return ret;
46 }
47
48 API int calendar_disconnect(void)
49 {
50         int ret;
51         calendar_h handle = NULL;
52         unsigned int id = cal_client_get_pid();
53
54         ret = cal_client_handle_get_p_with_id(id, &handle);
55         if (CALENDAR_ERROR_NO_DATA == ret) {
56                 return CALENDAR_ERROR_NONE;
57         } else if (CALENDAR_ERROR_NONE != ret) {
58                 ERR("cal_client_handle_get_p_with_id() Fail(%d)", ret);
59                 return ret;
60         }
61         ret = cal_client_disconnect(handle, id, &connection_count);
62         WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_client_disconnect() Fail(%d)", ret);
63         return ret;
64 }
65
66 API int calendar_connect_on_thread(void)
67 {
68         int ret;
69         calendar_h handle = NULL;
70         unsigned int id = cal_client_get_tid();
71
72         ret = cal_client_handle_get_p_with_id(id, &handle);
73         if (CALENDAR_ERROR_NO_DATA == ret) {
74                 ret = cal_client_handle_create(id, &handle);
75                 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_client_handle_create() Fail(%d)", ret);
76         } else if (CALENDAR_ERROR_NONE != ret) {
77                 ERR("cal_client_handle_get_p_with_id() Fail(%d)", ret);
78                 return ret;
79         }
80         ret = cal_client_connect(handle, id, &connection_count_on_thread);
81         return ret;
82 }
83
84 API int calendar_disconnect_on_thread(void)
85 {
86         int ret;
87         calendar_h handle = NULL;
88         unsigned int id = cal_client_get_tid();
89
90         ret = cal_client_handle_get_p_with_id(id, &handle);
91         if (CALENDAR_ERROR_NO_DATA == ret) {
92                 return CALENDAR_ERROR_NONE;
93         } else if (CALENDAR_ERROR_NONE != ret) {
94                 ERR("cal_client_handle_get_p_with_id() Fail(%d)", ret);
95                 return ret;
96         }
97         ret = cal_client_disconnect(handle, id, &connection_count_on_thread);
98         WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_client_disconnect() Fail(%d)", ret);
99         return ret;
100 }
101
102 API int calendar_connect_with_flags(unsigned int flags)
103 {
104         int ret;
105         calendar_h handle = NULL;
106         unsigned int id = cal_client_get_pid();
107
108         ret = cal_client_handle_get_p_with_id(id, &handle);
109         if (CALENDAR_ERROR_NO_DATA == ret) {
110                 ret = cal_client_handle_create(id, &handle);
111                 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_client_handle_create() Fail(%d)", ret);
112         } else if (CALENDAR_ERROR_NONE != ret) {
113                 ERR("cal_client_handle_get_p_with_id() Fail(%d)", ret);
114                 return ret;
115         }
116         ret = cal_client_connect_with_flags(handle, id, &connection_count, flags);
117         return ret;
118 }