messages implementation.
[profile/ivi/lemolo.git] / tizen / message_daemon.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include <Elementary.h>
5 #include <appcore-efl.h>
6 #include <Eina.h>
7 #include <E_DBus.h>
8 #include <notification.h>
9 #include <vconf.h>
10 #include <vconf-keys.h>
11 #include <power.h>
12 #include "ofono.h"
13
14 #define APP_NAME "org.tizen.message_daemon"
15
16 static OFono_Callback_List_Incoming_SMS_Node *inc_sms = NULL;
17
18 static char _img_path[PATH_MAX];
19
20 int _log_domain = -1;
21
22 #define ERR(...)      EINA_LOG_DOM_ERR(_log_domain, __VA_ARGS__)
23 #define INF(...)      EINA_LOG_DOM_INFO(_log_domain, __VA_ARGS__)
24 #define DBG(...)      EINA_LOG_DOM_DBG(_log_domain, __VA_ARGS__)
25
26 static void _notification_create(const char *sender, const char *message)
27 {
28         int id;
29         notification_h noti;
30         notification_error_e err;
31
32         noti = notification_new(NOTIFICATION_TYPE_NOTI,
33                                 NOTIFICATION_GROUP_ID_NONE,
34                                 NOTIFICATION_PRIV_ID_NONE);
35         EINA_SAFETY_ON_NULL_RETURN(noti);
36
37         err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_TITLE,
38                                         sender, NULL,
39                                         NOTIFICATION_VARIABLE_TYPE_NONE);
40
41         if (err != NOTIFICATION_ERROR_NONE)
42                 ERR("Could not set the title");
43
44         err = notification_set_text(noti, NOTIFICATION_TEXT_TYPE_CONTENT,
45                                         message, NULL,
46                                         NOTIFICATION_VARIABLE_TYPE_NONE);
47
48         if (err != NOTIFICATION_ERROR_NONE)
49                 ERR("Could not set the body");
50
51         err = notification_set_image(noti, NOTIFICATION_IMAGE_TYPE_ICON, _img_path);
52
53         if (err != NOTIFICATION_ERROR_NONE)
54                 ERR("Could not set the image");
55
56         err = notification_insert(noti, &id);
57
58         if (err != NOTIFICATION_ERROR_NONE)
59                 ERR("Could not show the notification");
60
61         notification_free(noti);
62 }
63
64 static Eina_Bool _phone_locked(void)
65 {
66         int lock;
67         if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock) == -1)
68                 return EINA_FALSE;
69
70         if (lock == VCONFKEY_IDLE_LOCK)
71                 return EINA_TRUE;
72
73         return EINA_FALSE;
74 }
75
76 static void _inc_sms_cb(void *data __UNUSED__, unsigned int sms_class,
77                         time_t timestamp __UNUSED__, const char *sender,
78                         const char *message)
79 {
80         if (sms_class != 1)
81                 return;
82
83         if (_phone_locked())
84                 power_wakeup();
85
86         _notification_create(sender, message);
87 }
88
89 static int _create(void *data __UNUSED__)
90 {
91         if (!ofono_init())
92                 return -1;
93
94         inc_sms = ofono_incoming_sms_cb_add(_inc_sms_cb, NULL);
95
96         elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
97         /* TODO SET IMAGE */
98         snprintf(_img_path, sizeof(_img_path), "%s/img",
99                         elm_app_data_dir_get());
100         return 0;
101 }
102
103 static int _reset(bundle *b __UNUSED__, void *data __UNUSED__)
104 {
105         return 0;
106 }
107
108 static int _resume(void *data __UNUSED__)
109 {
110         return 0;
111 }
112
113 static int _pause(void *data __UNUSED__)
114 {
115         return 0;
116 }
117
118 static int _terminate(void *data __UNUSED__)
119 {
120         ofono_incoming_sms_cb_del(inc_sms);
121         ofono_shutdown();
122         return 0;
123 }
124
125 int main(int argc, char **argv)
126 {
127         int r = EXIT_FAILURE;
128         struct appcore_ops ops = {
129                 .create = _create,
130                 .resume = _resume,
131                 .reset = _reset,
132                 .pause = _pause,
133                 .terminate = _terminate,
134         };
135         ops.data = NULL;
136
137         eina_init();
138         e_dbus_init();
139
140         _log_domain = eina_log_domain_register("answer_daemon", NULL);
141
142         r = appcore_efl_main(APP_NAME, &argc, &argv, &ops);
143
144         eina_shutdown();
145         e_dbus_shutdown();
146
147         return r;
148 }