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