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