94479c990219d1589c1d812f6024cc39aa1e9420
[apps/home/quickpanel.git] / daemon / service / noti_led.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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.tizenopensource.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 #ifdef QP_SERVICE_NOTI_LED_ENABLE
17 #include <vconf.h>
18 #include "common.h"
19 #include "noti_util.h"
20 #include "noti_led.h"
21
22 #define LED_ON 1
23 #define LED_OFF 0
24
25 static int g_is_led_turned_on = 0;
26
27 static inline int _is_led_notification_enabled(void) {
28         int ret = -1;
29         int status = 1;
30
31         ret = vconf_get_bool(VCONFKEY_SETAPPL_LED_INDICATOR_NOTIFICATIONS, &status);
32
33         if (ret == 0) {
34                 if (status == 0) {
35                         ERR("LED notification turned off");
36                         return 0;
37                 }
38         } else {
39                 ERR("failed to get value of VCONFKEY_SETAPPL_LED_INDICATOR_NOTIFICATIONS:%d", ret);
40         }
41
42         return 1;
43 }
44
45 static inline int __quickpanel_service_get_event_count(const char *pkgname) {
46         int count = 0;
47         notification_h noti = NULL;
48         notification_list_h noti_list = NULL;
49
50         retif(pkgname == NULL, 0, "Invalid parameter!");
51
52         notification_get_detail_list(pkgname, NOTIFICATION_GROUP_ID_NONE, NOTIFICATION_PRIV_ID_NONE, -1, &noti_list);
53         if (noti_list != NULL) {
54                 noti = notification_list_get_data(noti_list);
55                 if (noti != NULL) {
56                         count = quickpanel_noti_get_event_count_from_noti(noti);
57                 }
58                 notification_free_list(noti_list);
59                 return count;
60         } else {
61                 return 0;
62         }
63
64         return 0;
65 }
66
67 static void _noti_led_on_with_custom_color(int led_argb) {
68         int ret = 0;
69
70         if ((ret = led_set_mode_with_color(LED_MISSED_NOTI, LED_ON, led_argb)) == -1) {
71                 ERR("failed led_set_mode:%d", ret);
72         }
73         g_is_led_turned_on = 1;
74 }
75
76 static void _noti_led_on(notification_h noti) {
77         int ret = 0;
78         notification_led_op_e operation = -1;
79         int led_argb = 0x0;
80
81         if (noti == NULL) {
82                 if ((ret = led_set_mode(LED_MISSED_NOTI, LED_ON)) == -1) {
83                         ERR("failed led_set_mode:%d", ret);
84                 }
85                 g_is_led_turned_on = 1;
86         } else {
87                 notification_get_led(noti, &operation, &led_argb);
88
89                 if (operation == NOTIFICATION_LED_OP_ON) {
90                         if ((ret = led_set_mode(LED_MISSED_NOTI, LED_ON)) == -1) {
91                                 ERR("failed led_set_mode:%d", ret);
92                         }
93                         g_is_led_turned_on = 1;
94                 } else if (operation == NOTIFICATION_LED_OP_ON_CUSTOM_COLOR) {
95                         if ((ret = led_set_mode_with_color(LED_MISSED_NOTI, LED_ON, led_argb)) == -1) {
96                                 ERR("failed led_set_mode:%d", ret);
97                         }
98                         g_is_led_turned_on = 1;
99                 } else {
100                         ERR("NOTIFICATION_LED_OP_OFF");
101                 }
102         }
103 }
104
105 static void _noti_led_off(void) {
106         int ret = 0;
107
108         if ((ret = led_set_mode(LED_MISSED_NOTI, LED_OFF)) == -1) {
109                 ERR("failed led_set_mode:%d", ret);
110         }
111         g_is_led_turned_on = 0;
112 }
113
114 static int _is_keep_turn_on_led(int *op, int *argb) {
115         notification_h noti = NULL;
116         notification_list_h noti_list = NULL;
117         notification_list_h noti_list_head = NULL;
118         notification_led_op_e operation = -1;
119         int led_argb = 0;
120
121         notification_get_list(NOTIFICATION_TYPE_NOTI , -1, &noti_list_head);
122         noti_list = noti_list_head;
123
124         while (noti_list != NULL) {
125                 noti = notification_list_get_data(noti_list);
126                 if (noti != NULL) {
127                         notification_get_led(noti, &operation, &led_argb);
128                         if (operation >= NOTIFICATION_LED_OP_ON) {
129                                 notification_free_list(noti_list_head);
130                                 noti_list_head = NULL;
131
132                                 if (op != NULL) *op = operation;
133                                 if (argb != NULL) *argb = led_argb;
134                                 return 1;
135                         }
136                 }
137
138                 noti_list = notification_list_get_next(noti_list);
139         }
140
141         if (noti_list_head != NULL) {
142                 notification_free_list(noti_list_head);
143                 noti_list_head = NULL;
144         }
145
146         return 0;
147 }
148
149 HAPI void quickpanel_service_noti_led_on(notification_h noti) {
150         notification_led_op_e operation = -1;
151         int led_argb = 0;
152
153         retif(_is_led_notification_enabled() == 0, , "led noti disabled");
154
155         if (noti == NULL) {
156                 if (_is_keep_turn_on_led(&operation, &led_argb) >= 1) {
157                         if (operation == NOTIFICATION_LED_OP_ON) {
158                                 _noti_led_on(NULL);
159                         } else if (operation == NOTIFICATION_LED_OP_ON_CUSTOM_COLOR) {
160                                 _noti_led_on_with_custom_color(led_argb);
161                         }
162                 }
163         } else {
164                 _noti_led_on(noti);
165         }
166 }
167
168 HAPI void quickpanel_service_noti_led_off(notification_h noti) {
169         retif(g_is_led_turned_on == 0, , "led already turned off");
170
171         if (_is_keep_turn_on_led(NULL, NULL) == 0) {
172                 _noti_led_off();
173         }
174 }
175
176
177 static void quickpanel_service_noti_vconf_cb(keynode_t *node,
178                                                 void *data)
179 {
180         int ret = 0;
181         int is_on = 0;
182
183         is_on = _is_led_notification_enabled();
184
185         ERR("led notification status:%d", is_on);
186
187         if (is_on == 0) {
188                 if ((ret = led_set_mode(LED_MISSED_NOTI, LED_OFF)) == -1) {
189                         ERR("failed led_set_mode:%d", ret);
190                 }
191                 g_is_led_turned_on = 0;
192         } else {
193                 quickpanel_service_noti_led_on(NULL);
194         }
195 }
196
197 HAPI void quickpanel_service_noti_init(void *data) {
198         int ret = 0;
199         struct appdata *ad = data;
200         retif(ad == NULL, , "Invalid parameter!");
201
202         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_LED_INDICATOR_NOTIFICATIONS,
203                         quickpanel_service_noti_vconf_cb,
204                                 ad);
205         if (ret != 0) {
206                 ERR("failed to notify key[%s] : %d",
207                         VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, ret);
208         }
209 }
210
211 HAPI void quickpanel_service_noti_fini(void *data) {
212         int ret = 0;
213         struct appdata *ad = data;
214         retif(ad == NULL, , "Invalid parameter!");
215
216         ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_LED_INDICATOR_NOTIFICATIONS,
217                         quickpanel_service_noti_vconf_cb);
218         if (ret != 0) {
219                 ERR("failed to ignore key[%s] : %d",
220                                 VCONFKEY_SETAPPL_LED_INDICATOR_NOTIFICATIONS, ret);
221         }
222 }
223 #endif