4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
32 struct tcore_storage_type {
34 struct storage_operations *ops;
37 TcorePlugin *parent_plugin;
40 struct storage_callback_type{
41 TcoreStorageKeyCallback cb_fn;
45 Storage *tcore_storage_new(TcorePlugin *plugin, const char *name,
46 struct storage_operations *ops)
50 strg = calloc(1, sizeof(struct tcore_storage_type));
55 strg->name = strdup(name);
57 strg->parent_plugin = plugin;
59 strg->callback = g_hash_table_new_full(g_str_hash,g_str_equal, g_free, NULL);
61 tcore_server_add_storage(tcore_plugin_ref_server(plugin), strg);
66 void tcore_storage_free(Storage *strg)
72 free((void *)strg->name);
77 const char *tcore_storage_ref_name(Storage *strg)
85 void *tcore_storage_create_handle(Storage *strg, const char *path)
90 if (!strg || !strg->ops || !strg->ops->create_handle) {
94 return strg->ops->create_handle(strg, path);
97 gboolean tcore_storage_remove_handle(Storage *strg, void *handle)
102 if (!strg || !strg->ops || !strg->ops->remove_handle) {
106 return strg->ops->remove_handle(strg, handle);
109 gboolean tcore_storage_set_int(Storage *strg, enum tcore_storage_key key,
112 if (!strg || !strg->ops || !strg->ops->set_int) {
116 return strg->ops->set_int(strg, key, value);
119 gboolean tcore_storage_set_string(Storage *strg, enum tcore_storage_key key,
122 if (!strg || !strg->ops || !strg->ops->set_string) {
126 return strg->ops->set_string(strg, key, value);
129 gboolean tcore_storage_set_bool(Storage *strg, enum tcore_storage_key key,
132 if (!strg || !strg->ops || !strg->ops->set_bool) {
136 return strg->ops->set_bool(strg, key, value);
139 int tcore_storage_get_int(Storage *strg, enum tcore_storage_key key)
141 if (!strg || !strg->ops || !strg->ops->get_int) {
145 return strg->ops->get_int(strg, key);
148 char *tcore_storage_get_string(Storage *strg, enum tcore_storage_key key)
150 if (!strg || !strg->ops || !strg->ops->get_string) {
154 return strg->ops->get_string(strg, key);
157 gboolean tcore_storage_get_bool(Storage *strg, enum tcore_storage_key key)
159 if (!strg || !strg->ops || !strg->ops->get_bool) {
163 return strg->ops->get_bool(strg, key);
166 static void tcore_storage_vkey_callback_dispatcher(Storage *strg,
167 enum tcore_storage_key key, void *value)
169 gchar *key_gen = NULL;
170 GSList *cb_data = NULL;
171 struct storage_callback_type *tmp_cb = NULL;
173 key_gen = g_strdup_printf("%d", key);
177 cb_data = g_hash_table_lookup(strg->callback, key_gen);
179 tmp_cb = cb_data->data;
182 tmp_cb->cb_fn(key, value, tmp_cb->user_data);
184 cb_data = g_slist_next(cb_data);
190 gboolean tcore_storage_set_key_callback(Storage *strg,
191 enum tcore_storage_key key, TcoreStorageKeyCallback cb, void *user_data)
194 gchar *key_gen = NULL;
195 struct storage_callback_type *strg_cb_data = NULL;
196 struct storage_callback_type *tmp_cb = NULL;
198 if (!strg || !strg->ops || !strg->ops->set_key_callback)
203 strg_cb_data = g_new0(struct storage_callback_type, 1);
204 strg_cb_data->cb_fn = cb;
205 strg_cb_data->user_data = user_data;
207 key_gen = g_strdup_printf("%d", key);
208 tmp = g_hash_table_lookup(strg->callback, key_gen);
210 GSList *cb_data = tmp;
213 tmp_cb = cb_data->data;
215 if ((tmp_cb->cb_fn == cb)
216 && (tmp_cb->user_data == user_data)) {
218 g_free(strg_cb_data);
223 cb_data = g_slist_next(cb_data);
226 tmp = g_slist_append(tmp, strg_cb_data);
227 g_hash_table_replace(strg->callback, g_strdup(key_gen), tmp);
231 data = g_slist_append(data, strg_cb_data);
232 g_hash_table_insert(strg->callback, g_strdup(key_gen), data);
233 strg->ops->set_key_callback(strg, key, tcore_storage_vkey_callback_dispatcher);
240 gboolean tcore_storage_remove_key_callback(Storage *strg,
241 enum tcore_storage_key key, TcoreStorageKeyCallback cb)
244 gchar *key_gen = NULL;
245 GSList *cb_data = NULL;
247 struct storage_callback_type *tmp_cb = NULL;
249 if (!strg || !strg->ops || !strg->ops->remove_key_callback) {
253 key_gen = g_strdup_printf("%d", key);
254 tmp = g_hash_table_lookup(strg->callback, key_gen);
262 tmp_cb = cb_data->data;
264 if (tmp_cb->cb_fn == cb) {
265 tmp = g_slist_remove(tmp, tmp_cb);
271 cb_data = g_slist_next(cb_data);
274 cb_cnt = g_slist_length(tmp);
275 dbg("glist cnt (%d)", cb_cnt);
278 g_hash_table_remove(strg->callback, key_gen);
279 strg->ops->remove_key_callback(strg, key);
286 gboolean tcore_storage_update_query_database(Storage *strg, void *handle,
287 const char *query, GHashTable *in_param)
289 if (!strg || !handle || !query)
292 if (!strg->ops || !strg->ops->update_query_database) {
296 return strg->ops->update_query_database(strg, handle, query, in_param);
299 gboolean tcore_storage_read_query_database(Storage *strg, void *handle,
300 const char *query, GHashTable *in_param,
301 GHashTable *out_param, int out_param_cnt)
303 if (!strg || !handle || !query)
306 if (!strg->ops || !strg->ops->read_query_database) {
310 return strg->ops->read_query_database(strg, handle, query,
311 in_param, out_param, out_param_cnt);
314 gboolean tcore_storage_insert_query_database(Storage *strg, void *handle,
315 const char *query, GHashTable *in_param)
317 if (!strg || !handle || !query)
320 if (!strg->ops || !strg->ops->insert_query_database) {
324 return strg->ops->insert_query_database(strg, handle, query, in_param);
327 gboolean tcore_storage_remove_query_database(Storage *strg, void *handle,
328 const char *query, GHashTable *in_param)
330 if (!strg || !handle || !query)
333 if (!strg->ops || !strg->ops->remove_query_database) {
337 return strg->ops->remove_query_database(strg, handle, query, in_param);