Release version 0.2.4
[platform/core/appfw/event-system.git] / src / esd_system_event.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <glib.h>
4 #include <dlog.h>
5 #include <vconf.h>
6 #include <bundle.h>
7 #include <eventsystem.h>
8
9 #include "eventsystem_daemon.h"
10
11 /* table item : sent system-event by esd */
12 static GHashTable *esd_sent_table;
13
14 typedef struct __esd_sent_table_item {
15         char *event_name;
16         bundle *event_data;
17 } esd_sent_item;
18
19 struct esd_vconf_handler {
20         const char *key;
21         void (*esd_vconfcb_fn) (keynode_t *node, void *user_data);
22 };
23
24 static int __esd_event_data_compare(bundle *b1, bundle *b2, const char *key)
25 {
26         int ret = 0;
27         int tmp1 = 0;
28         int tmp2 = 0;
29         char *str1 = NULL;
30         char *str2 = NULL;
31
32         if (bundle_get_count(b1) == bundle_get_count(b2)) {
33                 tmp1 = bundle_get_str(b1, key, &str1);
34                 tmp2 = bundle_get_str(b2, key, &str2);
35                 if (tmp1 == BUNDLE_ERROR_NONE && tmp2 == BUNDLE_ERROR_NONE) {
36                         if (strcmp(str1, str2) != 0) {
37                                 _D("new event_data : value check");
38                                 ret = 1;
39                         }
40                 }
41         } else {
42                 _D("new event_data : bundle_count check");
43                 ret = 1;
44         }
45
46         if (ret == 0)
47                 _D("same event_data");
48
49         return ret;
50 }
51
52 static int __esd_send_system_event(const char *event_name, bundle *b, const char *key)
53 {
54         int ret = ES_R_OK;
55         esd_sent_item *item =
56                 (esd_sent_item *)g_hash_table_lookup(esd_sent_table, event_name);
57
58         if (item && __esd_event_data_compare(item->event_data, b, key) == 0) {
59                 _D("skip send: same with previous data");
60         } else {
61                 ret = eventsystem_send_system_event(event_name, b);
62                 if (ret != ES_R_OK) {
63                         _E("failed to send event");
64                         goto out;
65                 }
66
67                 if (item) {
68                         bundle_free(item->event_data);
69                         item->event_data = bundle_dup(b);
70                 } else {
71                         item = calloc(1, sizeof(esd_sent_item));
72                         if (item == NULL) {
73                                 _E("memory alloc failed");
74                                 ret = ES_R_ERROR;
75                                 goto out;
76                         }
77                         item->event_name = strdup(event_name);
78                         if (item->event_name == NULL) {
79                                 _E("out of memory");
80                                 FREE_AND_NULL(item);
81                                 ret = ES_R_ERROR;
82                                 goto out;
83                         }
84                         item->event_data = bundle_dup(b);
85                 }
86
87                 g_hash_table_insert(esd_sent_table, item->event_name, item);
88         }
89
90 out:
91         return ret;
92 }
93
94 static void __esd_vconfcb_location_use_mylocation(keynode_t *node, void *user_data)
95 {
96         int ret = 0;
97         int enabled = 0;
98         bundle *b = NULL;
99         const char *key = NULL;
100         const char *val = NULL;
101
102         _D("vconfcb called");
103
104         ret = vconf_get_int(VCONFKEY_LOCATION_USE_MY_LOCATION, &enabled);
105         if (ret != VCONF_OK) {
106                 _E("failed to get vconf (%d)", ret);
107                 return;
108         }
109
110         key = EVT_KEY_LOCATION_ENABLE_STATE;
111
112         if (enabled)
113                 val = EVT_VAL_LOCATION_ENABLED;
114         else
115                 val = EVT_VAL_LOCATION_DISABLED;
116
117         b = bundle_create();
118         bundle_add_str(b, key, val);
119
120         if (__esd_send_system_event(SYS_EVENT_LOCATION_ENABLE_STATE, b, key) != ES_R_OK)
121                 _E("failed to send event");
122
123         if (b)
124                 bundle_free(b);
125 }
126
127 static void __esd_vconfcb_location_enabled(keynode_t *node, void *user_data)
128 {
129         int ret = 0;
130         int enabled = 0;
131         bundle *b = NULL;
132         const char *key = NULL;
133         const char *val = NULL;
134
135         _D("vconfcb called");
136
137         ret = vconf_get_int(VCONFKEY_LOCATION_ENABLED, &enabled);
138         if (ret != VCONF_OK) {
139                 _E("failed to get vconf (%d)", ret);
140                 return;
141         }
142
143         key = EVT_KEY_GPS_ENABLE_STATE;
144
145         if (enabled)
146                 val = EVT_VAL_GPS_ENABLED;
147         else
148                 val = EVT_VAL_GPS_DISABLED;
149
150         b = bundle_create();
151         bundle_add_str(b, key, val);
152
153         if (__esd_send_system_event(SYS_EVENT_GPS_ENABLE_STATE, b, key) != ES_R_OK)
154                 _E("failed to send event");
155
156         if (b)
157                 bundle_free(b);
158 }
159
160 static void __esd_vconfcb_location_network_enabled(keynode_t *node, void *user_data)
161 {
162         int ret = 0;
163         int enabled = 0;
164         bundle *b = NULL;
165         const char *key = NULL;
166         const char *val = NULL;
167
168         _D("vconfcb called");
169
170         ret = vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &enabled);
171         if (ret != VCONF_OK) {
172                 _E("failed to get vconf (%d)", ret);
173                 return;
174         }
175
176         key = EVT_KEY_NPS_ENABLE_STATE;
177
178         if (enabled)
179                 val = EVT_VAL_NPS_ENABLED;
180         else
181                 val = EVT_VAL_NPS_DISABLED;
182
183         b = bundle_create();
184         bundle_add_str(b, key, val);
185
186         if (__esd_send_system_event(SYS_EVENT_NPS_ENABLE_STATE, b, key) != ES_R_OK)
187                 _E("failed to send event");
188
189         if (b)
190                 bundle_free(b);
191 }
192
193 static void __esd_vconfcb_language_set(keynode_t *node, void *user_data)
194 {
195         char *str = 0;
196         bundle *b = NULL;
197         const char *key = NULL;
198
199         _D("vconfcb called");
200
201         str = vconf_get_str(VCONFKEY_LANGSET);
202         if (str == NULL) {
203                 _E("failed to get vconf str");
204                 return;
205         }
206
207         key = EVT_KEY_LANGUAGE_SET;
208
209         b = bundle_create();
210         bundle_add_str(b, key, str);
211
212         if (__esd_send_system_event(SYS_EVENT_LANGUAGE_SET, b, key) != ES_R_OK)
213                 _E("failed to send event");
214
215         if (b)
216                 bundle_free(b);
217         if (str)
218                 free(str);
219 }
220
221 static void __esd_vconfcb_hour_format(keynode_t *node, void *user_data)
222 {
223         int ret = 0;
224         int hours = 0;
225         bundle *b = NULL;
226         const char *key = NULL;
227         const char *val = NULL;
228
229         _D("vconfcb called");
230
231         ret = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &hours);
232         if (ret != VCONF_OK) {
233                 _E("failed to get vconf (%d)", ret);
234                 return;
235         }
236
237         key = EVT_KEY_HOUR_FORMAT;
238
239         if (hours == VCONFKEY_TIME_FORMAT_24)
240                 val = EVT_VAL_HOURFORMAT_24;
241         else
242                 val = EVT_VAL_HOURFORMAT_12;
243
244         b = bundle_create();
245         bundle_add_str(b, key, val);
246
247         if (__esd_send_system_event(SYS_EVENT_HOUR_FORMAT, b, key) != ES_R_OK)
248                 _E("failed to send event");
249
250         if (b)
251                 bundle_free(b);
252 }
253
254 static void __esd_vconfcb_region_format(keynode_t *node, void *user_data)
255 {
256         char *str = 0;
257         bundle *b = NULL;
258         const char *key = NULL;
259
260         _D("vconfcb called");
261
262         str = vconf_get_str(VCONFKEY_REGIONFORMAT);
263         if (str == NULL) {
264                 _E("failed to get vconf str");
265                 return;
266         }
267
268         key = EVT_KEY_REGION_FORMAT;
269
270         b = bundle_create();
271         bundle_add_str(b, key, str);
272
273         if (__esd_send_system_event(SYS_EVENT_REGION_FORMAT, b, key) != ES_R_OK)
274                 _E("failed to send event");
275
276         if (b)
277                 bundle_free(b);
278         if (str)
279                 free(str);
280 }
281
282 static void __esd_vconfcb_vibration_status(keynode_t *node, void *user_data)
283 {
284         int ret = 0;
285         int vibration_on = 0;
286         int sound_on = 0;
287         bundle *b = NULL;
288         char *key = NULL;
289         char *val = NULL;
290
291         _D("vconfcb called");
292
293         ret = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibration_on);
294         if (ret != VCONF_OK) {
295                 _E("failed to get vconf (%d)", ret);
296                 return;
297         }
298
299         ret = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &sound_on);
300         if (ret != VCONF_OK) {
301                 _E("failed to get vconf (%d)", ret);
302                 return;
303         }
304
305         if (vibration_on) {
306                 key = EVT_KEY_VIBRATION_STATE;
307                 val = EVT_VAL_VIBRATION_ON;
308                 b = bundle_create();
309                 bundle_add_str(b, key, val);
310                 if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK)
311                         _E("failed to send event");
312
313                 if (b)
314                         bundle_free(b);
315
316                 key = EVT_KEY_SILENT_MODE;
317                 val = EVT_VAL_SILENTMODE_OFF;
318                 b = bundle_create();
319                 bundle_add_str(b, key, val);
320                 if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK)
321                         _E("failed to send event");
322
323                 if (b)
324                         bundle_free(b);
325         } else {
326                 key = EVT_KEY_VIBRATION_STATE;
327                 val = EVT_VAL_VIBRATION_OFF;
328                 b = bundle_create();
329                 bundle_add_str(b, key, val);
330                 if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK)
331                         _E("failed to send event");
332
333                 if (b)
334                         bundle_free(b);
335
336                 if (!sound_on) {
337                         key = EVT_KEY_SILENT_MODE;
338                         val = EVT_VAL_SILENTMODE_ON;
339                         b = bundle_create();
340                         bundle_add_str(b, key, val);
341                         if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK)
342                                 _E("failed to send event");
343
344                         if (b)
345                                 bundle_free(b);
346                 }
347         }
348 }
349
350 static void __esd_vconfcb_sound_status(keynode_t *node, void *user_data)
351 {
352         int ret = 0;
353         int vibration_on = 0;
354         int sound_on = 0;
355         bundle *b = NULL;
356         char *key = NULL;
357         char *val = NULL;
358
359         _D("vconfcb called");
360
361         ret = vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vibration_on);
362         if (ret != VCONF_OK) {
363                 _E("failed to get vconf (%d)", ret);
364                 return;
365         }
366
367         ret = vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &sound_on);
368         if (ret != VCONF_OK) {
369                 _E("failed to get vconf (%d)", ret);
370                 return;
371         }
372
373         if (sound_on) {
374                 key = EVT_KEY_VIBRATION_STATE;
375                 val = EVT_VAL_VIBRATION_OFF;
376                 b = bundle_create();
377                 bundle_add_str(b, key, val);
378                 if (__esd_send_system_event(SYS_EVENT_VIBRATION_STATE, b, key) != ES_R_OK)
379                         _E("failed to send event");
380
381                 if (b)
382                         bundle_free(b);
383
384                 key = EVT_KEY_SILENT_MODE;
385                 val = EVT_VAL_SILENTMODE_OFF;
386                 b = bundle_create();
387                 bundle_add_str(b, key, val);
388                 if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK)
389                         _E("failed to send event");
390
391                 if (b)
392                         bundle_free(b);
393         } else {
394                 if (!vibration_on) {
395                         key = EVT_KEY_SILENT_MODE;
396                         val = EVT_VAL_SILENTMODE_ON;
397                         b = bundle_create();
398                         bundle_add_str(b, key, val);
399                         if (__esd_send_system_event(SYS_EVENT_SILENT_MODE, b, key) != ES_R_OK)
400                                 _E("failed to send event");
401
402                         if (b)
403                                 bundle_free(b);
404                 }
405         }
406 }
407
408 static void __esd_vconfcb_auto_rotate(keynode_t *node, void *user_data)
409 {
410         int ret = 0;
411         int enabled = 0;
412         bundle *b = NULL;
413         const char *key = NULL;
414         const char *val = NULL;
415
416         _D("vconfcb called");
417
418         ret = vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &enabled);
419         if (ret != VCONF_OK) {
420                 _E("failed to get vconf (%d)", ret);
421                 return;
422         }
423
424         key = EVT_KEY_SCREEN_AUTOROTATE_STATE;
425
426         if (enabled)
427                 val = EVT_VAL_SCREEN_AUTOROTATE_ON;
428         else
429                 val = EVT_VAL_SCREEN_AUTOROTATE_OFF;
430
431         b = bundle_create();
432         bundle_add_str(b, key, val);
433
434         if (__esd_send_system_event(SYS_EVENT_SCREEN_AUTOROTATE_STATE, b, key) != ES_R_OK)
435                 _E("failed to send event");
436
437         if (b)
438                 bundle_free(b);
439 }
440
441 static void __esd_vconfcb_mobiledata_state(keynode_t *node, void *user_data)
442 {
443         int ret = 0;
444         int enabled = 0;
445         bundle *b = NULL;
446         const char *key = NULL;
447         const char *val = NULL;
448
449         _D("vconfcb called");
450
451         ret = vconf_get_bool(VCONFKEY_3G_ENABLE, &enabled);
452         if (ret != VCONF_OK) {
453                 _E("failed to get vconf (%d)", ret);
454                 return;
455         }
456
457         key = EVT_KEY_MOBILE_DATA_STATE;
458
459         if (enabled)
460                 val = EVT_VAL_MOBILE_DATA_ON;
461         else
462                 val = EVT_VAL_MOBILE_DATA_OFF;
463
464         b = bundle_create();
465         bundle_add_str(b, key, val);
466
467         if (__esd_send_system_event(SYS_EVENT_MOBILE_DATA_STATE, b, key) != ES_R_OK)
468                 _E("failed to send event");
469
470         if (b)
471                 bundle_free(b);
472 }
473
474 static void __esd_vconfcb_roaming_state(keynode_t *node, void *user_data)
475 {
476         int ret = 0;
477         int enabled = 0;
478         bundle *b = NULL;
479         const char *key = NULL;
480         const char *val = NULL;
481
482         _D("vconfcb called");
483
484         ret = vconf_get_bool(VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL, &enabled);
485         if (ret != VCONF_OK) {
486                 _E("failed to get vconf (%d)", ret);
487                 return;
488         }
489
490         key = EVT_KEY_DATA_ROAMING_STATE;
491
492         if (enabled)
493                 val = EVT_VAL_DATA_ROAMING_ON;
494         else
495                 val = EVT_VAL_DATA_ROAMING_OFF;
496
497         b = bundle_create();
498         bundle_add_str(b, key, val);
499
500         if (__esd_send_system_event(SYS_EVENT_DATA_ROAMING_STATE, b, key) != ES_R_OK)
501                 _E("failed to send event");
502
503         if (b)
504                 bundle_free(b);
505 }
506
507 static void __esd_vconfcb_font_set(keynode_t *node, void *user_data)
508 {
509         char *str = 0;
510         bundle *b = NULL;
511         const char *key = NULL;
512
513         _D("vconfcb called");
514
515         str = vconf_get_str(VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME);
516         if (str == NULL) {
517                 _E("failed to get vconf str");
518                 return;
519         }
520
521         key = EVT_KEY_FONT_SET;
522
523         b = bundle_create();
524         bundle_add_str(b, key, str);
525
526         if (__esd_send_system_event(SYS_EVENT_FONT_SET, b, key) != ES_R_OK)
527                 _E("failed to send event");
528
529         if (b)
530                 bundle_free(b);
531         if (str)
532                 free(str);
533 }
534
535 static struct esd_vconf_handler vconf_callbacks[] = {
536         {VCONFKEY_LOCATION_USE_MY_LOCATION, __esd_vconfcb_location_use_mylocation},
537         {VCONFKEY_LOCATION_ENABLED, __esd_vconfcb_location_enabled},
538         {VCONFKEY_LOCATION_NETWORK_ENABLED, __esd_vconfcb_location_network_enabled},
539         {VCONFKEY_LANGSET, __esd_vconfcb_language_set},
540         {VCONFKEY_REGIONFORMAT_TIME1224, __esd_vconfcb_hour_format},
541         {VCONFKEY_REGIONFORMAT, __esd_vconfcb_region_format},
542         {VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, __esd_vconfcb_vibration_status},
543         {VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, __esd_vconfcb_sound_status},
544         {VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __esd_vconfcb_auto_rotate},
545         {VCONFKEY_3G_ENABLE, __esd_vconfcb_mobiledata_state},
546         {VCONFKEY_SETAPPL_STATE_DATA_ROAMING_BOOL, __esd_vconfcb_roaming_state},
547         {VCONFKEY_SETAPPL_ACCESSIBILITY_FONT_NAME, __esd_vconfcb_font_set},
548 };
549
550 static int vconfcbs_size = sizeof(vconf_callbacks)/sizeof(struct esd_vconf_handler);
551
552 int __esd_register_vconf_callbacks(void)
553 {
554         int i = 0;
555         int ret = 0;
556         int result = ES_R_OK;
557
558 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
559         g_type_init();
560 #endif
561
562         esd_sent_table = g_hash_table_new(g_str_hash, g_str_equal);
563
564         _D("vconf callbacks size(%d)", vconfcbs_size);
565         for (i = 0; i < vconfcbs_size; i++) {
566                 ret = vconf_notify_key_changed(vconf_callbacks[i].key,
567                         vconf_callbacks[i].esd_vconfcb_fn, NULL);
568                 if (ret != VCONF_OK) {
569                         _E("failed to register vconf callback (%s)", vconf_callbacks[i].key);
570                         result = ES_R_ERROR;
571                         break;
572                 }
573         }
574
575         return result;
576 }