cleanup build scripts and filenames
[platform/core/api/system-settings.git] / src / sys_settings_multi_callback.c
1 /*
2  * Copyright (c) 2017-2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <stdlib.h>
17 #include "sys_settings.h"
18
19 void clean_node(callback_node* node)
20 {
21         if (!node)
22                 return;
23
24         node->callback = NULL;
25         node->user_data = NULL;
26 }
27
28
29 callback_node* alloc_multi_callback_node()
30 {
31         callback_node* node = NULL;
32         node = (callback_node*)malloc(sizeof(callback_node));
33
34         if (node == NULL) {
35                 SETTING_TRACE("Not enough node....");
36                 return NULL;
37         }
38
39         clean_node(node);
40
41         return node;
42 }
43
44 void free_multi_callback_node(callback_node* node)
45 {
46         if (node)
47                 free(node);
48         return;
49 }
50
51 static gint _compare_cb(gconstpointer val, gconstpointer s_val)
52 {
53         if (NULL == val) return 1;
54         if (NULL == s_val) return 1;
55
56         callback_node* node = (callback_node*)val;
57         if (node->callback == s_val)
58                 return 0;
59         return -1;
60 }
61
62 int delete_multi_callback(callback_list *handle, system_settings_changed_cb ptr)
63 {
64         SETTING_TRACE_BEGIN;
65         if (!handle || !ptr)
66                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
67
68         GList *found = g_list_find_custom(handle->list, ptr, _compare_cb);
69         if (found) {
70                 handle->list = g_list_remove_link(handle->list, found);
71                 free_multi_callback_node((callback_node*)found->data);
72                 g_list_free(found);
73         }
74
75         SETTING_TRACE_END;
76         return SYSTEM_SETTINGS_ERROR_NONE;
77 }
78
79 int add_multi_callback(callback_list *handle, system_settings_changed_cb ptr, void *user_data)
80 {
81         SETTING_TRACE_BEGIN;
82         if (!handle || !ptr)
83                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
84
85         GList *found = g_list_find_custom(handle->list, ptr, _compare_cb);
86         if (found != NULL)
87                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
88
89         callback_node* node = alloc_multi_callback_node();
90
91         if (node == NULL)
92                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
93
94         node->callback = ptr;
95         node->user_data = user_data;
96
97         /* append the node to list. free_multi_callback_node will free the node */
98         handle->list = g_list_append(handle->list, node);
99
100         SETTING_TRACE_END;
101         return SYSTEM_SETTINGS_ERROR_NONE;
102 }
103
104 int invoke_callback_list(callback_list *handle, system_settings_key_e key)
105 {
106         SETTING_TRACE_BEGIN;
107         if (!handle)
108                 return SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER;
109
110         GList *itr = handle->list;
111         while (itr != NULL) {
112                 callback_node* node = (callback_node*)itr->data;
113                 if (node->callback)
114                         node->callback(key, node->user_data);
115                 itr = g_list_next(itr);
116         }
117
118         SETTING_TRACE_END;
119         return SYSTEM_SETTINGS_ERROR_NONE;
120 }