sync with private git. updated the license and the boilerplates
[apps/home/quickpanel.git] / daemon / notifications / noti_node.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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
17 #include "quickpanel-ui.h"
18 #include "common.h"
19 #include "list_util.h"
20 #include "noti_node.h"
21
22 static void _noti_node_free(noti_node_item *node);
23
24 HAPI void noti_node_create(noti_node **handle)
25 {
26         retif(handle == NULL, , "Invalid parameter!");
27
28         *handle = (noti_node *)malloc(sizeof(noti_node));
29
30         if (*handle != NULL) {
31                 (*handle)->table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
32                                                         NULL,
33                                                         (GDestroyNotify)_noti_node_free);
34
35                 (*handle)->n_ongoing = 0;
36                 (*handle)->n_noti = 0;
37         } else {
38                 *handle = NULL;
39         }
40 }
41
42 HAPI void noti_node_destroy(noti_node **handle)
43 {
44         retif(handle == NULL, , "Invalid parameter!");
45         retif(*handle == NULL, , "Invalid parameter!");
46
47         g_hash_table_remove_all((*handle)->table);
48         g_hash_table_destroy((*handle)->table);
49         (*handle)->table = NULL;
50
51         free((*handle));
52         *handle = NULL;
53 }
54
55 HAPI noti_node_item *noti_node_add(noti_node *handle, notification_h noti, void *view)
56 {
57         int priv_id = 0;
58         notification_type_e noti_type = NOTIFICATION_TYPE_NONE;
59         noti_node_item *node = NULL;
60
61         retif(handle == NULL || noti == NULL, NULL, "Invalid parameter!");
62
63         if (notification_get_id(noti, NULL, &priv_id) == NOTIFICATION_ERROR_NONE) {
64                 node = malloc(sizeof(noti_node_item));
65                 if (!node) {
66                         ERR("fail to alloc item");
67                         return NULL;
68                 }
69
70                 node->noti = noti;
71                 node->view = view;
72
73                 g_hash_table_insert(handle->table, GINT_TO_POINTER(priv_id), (gpointer *)node);
74
75                 notification_get_type(noti, &noti_type);
76
77                 if (noti_type == NOTIFICATION_TYPE_NOTI)
78                         handle->n_noti++;
79                 else if (noti_type == NOTIFICATION_TYPE_ONGOING)
80                         handle->n_ongoing++;
81
82                 return node;
83         }
84
85         return NULL;
86 }
87
88 HAPI void noti_node_remove(noti_node *handle, int priv_id)
89 {
90         notification_type_e noti_type = NOTIFICATION_TYPE_NONE;
91
92         retif(handle == NULL, , "Invalid parameter!");
93         retif(handle->table == NULL, , "Invalid parameter!");
94
95         noti_node_item *item = noti_node_get(handle, priv_id);
96
97         if (item != NULL) {
98                 if (item->noti != NULL) {
99                         notification_get_type(item->noti, &noti_type);
100
101                         if (noti_type == NOTIFICATION_TYPE_NOTI)
102                                 handle->n_noti--;
103                         else if (noti_type == NOTIFICATION_TYPE_ONGOING)
104                                 handle->n_ongoing--;
105                 }
106
107                 notification_free(item->noti);
108                 item->noti = NULL;
109                 item->view = NULL;
110
111                 if (g_hash_table_remove(handle->table, GINT_TO_POINTER(priv_id)))
112                 {
113                         INFO("success to remove %d", priv_id);
114                 }
115         }
116 }
117
118 HAPI noti_node_item *noti_node_get(noti_node *handle, int priv_id)
119 {
120         retif(handle == NULL, NULL, "Invalid parameter!");
121         retif(handle->table == NULL, NULL, "Invalid parameter!");
122
123         return (noti_node_item *)g_hash_table_lookup
124                         (handle->table, GINT_TO_POINTER(priv_id));
125 }
126
127 HAPI int noti_node_get_item_count(noti_node *handle, notification_type_e noti_type)
128 {
129         retif(handle == NULL, 0, "Invalid parameter!");
130
131         if (noti_type == NOTIFICATION_TYPE_NOTI)
132                 return handle->n_noti;
133         else if (noti_type == NOTIFICATION_TYPE_ONGOING)
134                 return handle->n_ongoing;
135
136         return 0;
137 }
138
139 static void _noti_node_free(noti_node_item *node)
140 {
141         retif(node == NULL, , "Invalid parameter!");
142
143         DBG("item_node is freed:%p", node);
144
145         free(node);
146 }