29d5d941f7609128d98ca74ea5139ceb98a51ff5
[platform/core/pim/calendar-service.git] / client / cal_client_handle.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 <stdlib.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <pthread.h>
24 #include <glib.h>
25
26 #include "cal_internal.h"
27 #include "cal_typedef.h"
28 #include "cal_mutex.h"
29 #include "cal_client_handle.h"
30 #include "cal_client_utils.h"
31
32 static GHashTable *_cal_handle_table = NULL;
33
34 static void _foreach_cb(gpointer key, gpointer value, gpointer user_data)
35 {
36         DBG("[hash check]--------- key[%s] value[%p]", key, value);
37 }
38 static void _print_hash(GHashTable *table)
39 {
40         if (table)
41                 g_hash_table_foreach(table, _foreach_cb, NULL);
42 }
43
44 static int _cal_client_handle_get_key(unsigned int id, char *key, int key_len)
45 {
46         RETV_IF(NULL == key, CALENDAR_ERROR_INVALID_PARAMETER);
47
48         snprintf(key, key_len, "%d:%u", getuid(), id);
49         DBG("[%s]", key);
50         return CALENDAR_ERROR_NONE;
51 }
52
53 int cal_client_handle_get_p(calendar_h *out_handle)
54 {
55         int ret = 0;
56
57         RETVM_IF(NULL == _cal_handle_table, CALENDAR_ERROR_NO_DATA, "No handle table");
58         RETV_IF(NULL == out_handle, CALENDAR_ERROR_INVALID_PARAMETER);
59
60         unsigned int tid = cal_client_get_tid();
61         unsigned int pid = cal_client_get_pid();
62
63         char key[CAL_STR_MIDDLE_LEN] = {0};
64         ret = _cal_client_handle_get_key(tid, key, sizeof(key));
65         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_client_handle_get_key() Fail(%d)", ret);
66
67         calendar_h handle = NULL;
68         cal_mutex_lock(CAL_MUTEX_HANDLE);
69         handle = g_hash_table_lookup(_cal_handle_table, key);
70         cal_mutex_unlock(CAL_MUTEX_HANDLE);
71
72         if (NULL == handle && tid != pid) {
73                 ret = _cal_client_handle_get_key(pid, key, sizeof(key));
74                 if (CALENDAR_ERROR_NONE != ret) {
75                         ERR("_cal_client_handle_get_key() Fail(%d):No handle(key[%s])", ret, key);
76                         _print_hash(_cal_handle_table);
77                         return ret;
78                 }
79
80                 cal_mutex_lock(CAL_MUTEX_HANDLE);
81                 handle = g_hash_table_lookup(_cal_handle_table, key);
82                 cal_mutex_unlock(CAL_MUTEX_HANDLE);
83                 if (NULL == handle) {
84                         ERR("g_hash_table_lookup() Fail:No handle(key[%s])", key);
85                         _print_hash(_cal_handle_table);
86                         return CALENDAR_ERROR_NO_DATA;
87                 }
88         }
89         *out_handle = handle;
90         return CALENDAR_ERROR_NONE;
91 }
92
93 int cal_client_handle_get_p_with_id(unsigned int id, calendar_h *out_handle)
94 {
95         int ret = 0;
96
97         RETVM_IF(NULL == _cal_handle_table, CALENDAR_ERROR_NO_DATA, "No handle table");
98         RETV_IF(NULL == out_handle, CALENDAR_ERROR_INVALID_PARAMETER);
99
100         char key[CAL_STR_MIDDLE_LEN] = {0};
101         ret = _cal_client_handle_get_key(id, key, sizeof(key));
102         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_client_handle_get_key_with_id() Fail(%d)", ret);
103
104         calendar_h handle = NULL;
105         cal_mutex_lock(CAL_MUTEX_HANDLE);
106         handle = g_hash_table_lookup(_cal_handle_table, key);
107         cal_mutex_unlock(CAL_MUTEX_HANDLE);
108         if (NULL == handle) {
109                 ERR("g_hash_table_lookup() Fail:No handle:key[%s]", key);
110                 _print_hash(_cal_handle_table);
111                 return CALENDAR_ERROR_NO_DATA;
112         }
113         *out_handle = handle;
114         return CALENDAR_ERROR_NONE;
115 }
116
117 static int _cal_client_handle_add(calendar_h handle, const char *key)
118 {
119         RETV_IF(NULL == key, CALENDAR_ERROR_INVALID_PARAMETER);
120         cal_mutex_lock(CAL_MUTEX_HANDLE);
121
122         if (NULL == _cal_handle_table)
123                 _cal_handle_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
124
125         g_hash_table_insert(_cal_handle_table, strdup(key), handle);
126         cal_mutex_unlock(CAL_MUTEX_HANDLE);
127         DBG("[HASH:handle] insert key[%s] value[%p]", key, handle);
128         return CALENDAR_ERROR_NONE;
129 }
130
131 int cal_client_handle_create(unsigned int id, calendar_h *out_handle)
132 {
133         int ret = 0;
134         calendar_h handle = NULL;
135
136         RETV_IF(NULL == out_handle, CALENDAR_ERROR_NONE);
137
138         char key[CAL_STR_MIDDLE_LEN] = {0};
139         ret = _cal_client_handle_get_key(id, key, sizeof(key));
140         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_client_handle_get_key() Fail(%d)", ret);
141
142         ret = cal_handle_create(&handle);
143         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_handle_create() Fail(%d)", ret);
144
145         ret = _cal_client_handle_add(handle, key);
146         if (CALENDAR_ERROR_NONE != ret) {
147                 ERR("_cal_client_handle_add() Fail(%d)", ret);
148                 cal_handle_destroy(handle);
149                 return ret;
150         }
151
152         *out_handle = handle;
153         return CALENDAR_ERROR_NONE;
154 }
155
156 int cal_client_handle_remove(unsigned int id, calendar_h handle)
157 {
158         int ret = 0;
159         char key[CAL_STR_MIDDLE_LEN] = {0};
160
161         RETVM_IF(NULL == _cal_handle_table, CALENDAR_ERROR_NONE, "No handle table");
162
163         ret = _cal_client_handle_get_key(id, key, sizeof(key));
164         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_client_handle_get_key() Fail(%d)", ret);
165
166         cal_mutex_lock(CAL_MUTEX_HANDLE);
167         g_hash_table_remove(_cal_handle_table, key);
168         DBG("[HASH:handle] remove [%s]", key);
169         if (0 == g_hash_table_size(_cal_handle_table)) {
170                 g_hash_table_destroy(_cal_handle_table);
171                 _cal_handle_table = NULL;
172         }
173         cal_mutex_unlock(CAL_MUTEX_HANDLE);
174         cal_handle_destroy(handle);
175         return CALENDAR_ERROR_NONE;
176 }
177
178 void cal_client_handle_set_version(calendar_h handle, int version)
179 {
180         RET_IF(NULL == handle);
181         cal_s *h = (cal_s *)handle;
182         h->version = version;
183 }