Remove unused battery related notification code
[platform/core/system/system-popup.git] / src / launcher / launcher.c
1 /*
2  * popup-launcher
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include "launcher.h"
21
22 #define TERMINATE_TIMEOUT  5
23
24 static E_DBus_Connection *edbus_conn;
25 static DBusPendingCall *edbus_request_name;
26 static Ecore_Timer *term_timer = NULL;
27
28 extern DBusMessage *activate_notification_no_param(E_DBus_Object *obj, DBusMessage *msg, int type);
29 extern DBusMessage *launch_system_servant_app(E_DBus_Object *obj,       DBusMessage *msg, char **argv);
30 extern DBusMessage *activate_notification_single_param(E_DBus_Object *obj, DBusMessage *msg, int type);
31 extern DBusMessage *activate_notification_double_param(E_DBus_Object *obj, DBusMessage *msg, int type);
32 extern DBusMessage *deactivate_notification(E_DBus_Object *obj, DBusMessage *msg);
33 extern DBusMessage *update_notification_double_param(E_DBus_Object *obj, DBusMessage *msg, int type);
34
35 static Eina_Bool exit_idler_cb(void *data)
36 {
37         e_dbus_connection_close(edbus_conn);
38         e_dbus_shutdown();
39
40         ecore_main_loop_quit();
41         return ECORE_CALLBACK_CANCEL;
42 }
43
44 static Eina_Bool terminate_launcher(void *data)
45 {
46         if (term_timer)
47                 ecore_timer_del(term_timer);
48
49         if (ecore_idler_add(exit_idler_cb, NULL))
50                 return ECORE_CALLBACK_CANCEL;
51
52         exit_idler_cb(NULL);
53         return ECORE_CALLBACK_CANCEL;
54 }
55
56 static void set_timer_to_terminate(void)
57 {
58         if (term_timer)
59                 ecore_timer_reset(term_timer);
60         else {
61                 term_timer = ecore_timer_add(TERMINATE_TIMEOUT, terminate_launcher, NULL);
62                 if (!term_timer)
63                         terminate_launcher(NULL);
64         }
65 }
66
67 /* Basic popups */
68 static DBusMessage *system_popup(E_DBus_Object *obj, DBusMessage *msg)
69 {
70         set_timer_to_terminate();
71         return launch_popup(obj, msg, SYSTEM_SYSPOPUP);
72 }
73
74 static const struct edbus_method
75 dbus_system_methods[] = {
76         { "PopupLaunch", "a{ss}", "i", system_popup },
77         /* Add methods here */
78 };
79
80 /* Powerkey popup */
81 static DBusMessage *powerkey_popup(E_DBus_Object *obj, DBusMessage *msg)
82 {
83         set_timer_to_terminate();
84         return launch_popup(obj, msg, POWERKEY_SYSPOPUP);
85 }
86
87 /* Overheat popup */
88 static DBusMessage *overheat_popup(E_DBus_Object *obj, DBusMessage *msg)
89 {
90         set_timer_to_terminate();
91         return launch_popup(obj, msg, OVERHEAT_SYSPOPUP);
92 }
93
94 /* Crash popup */
95 static DBusMessage *crash_popup(E_DBus_Object *obj, DBusMessage *msg)
96 {
97         set_timer_to_terminate();
98         return launch_popup(obj, msg, CRASH_SYSPOPUP);
99 }
100
101 /* Battery notifications */
102 static DBusMessage *battery_full_noti_on(E_DBus_Object *obj, DBusMessage *msg)
103 {
104         set_timer_to_terminate();
105         return activate_notification_no_param(obj, msg, BATTERY_FULL);
106 }
107
108 static DBusMessage *battery_charge_noti_on(E_DBus_Object *obj, DBusMessage *msg)
109 {
110         char param[2];
111         char *args[3];
112
113         set_timer_to_terminate();
114
115         args[0] = SERVANT_APP_NAME;
116         snprintf(param, sizeof(param), "%d", CHARGER_CONNECTION);
117         args[1] = param;
118         args[2] = NULL;
119         return launch_system_servant_app(obj, msg, args);
120 }
121
122 /* Datausage */
123 static DBusMessage *data_warning_noti_on(E_DBus_Object *obj, DBusMessage *msg)
124 {
125         set_timer_to_terminate();
126         return activate_notification_no_param(obj, msg, DATAUSAGE_WARNING);
127 }
128
129 static DBusMessage *data_disabled_noti_on(E_DBus_Object *obj, DBusMessage *msg)
130 {
131         set_timer_to_terminate();
132         return activate_notification_no_param(obj, msg, DATAUSAGE_DISABLED);
133 }
134
135 /* Notification Off */
136 static DBusMessage *noti_off(E_DBus_Object *obj, DBusMessage *msg)
137 {
138         set_timer_to_terminate();
139         return deactivate_notification(obj, msg);
140 }
141
142 /* SD Card */
143 static DBusMessage *sdcard_noti(E_DBus_Object *obj, DBusMessage *msg)
144 {
145         set_timer_to_terminate();
146         return activate_notification_single_param(obj, msg, SDCARD_DEVICE);
147 }
148
149 /* LED */
150 static DBusMessage *led_torch_noti_on(E_DBus_Object *obj, DBusMessage *msg)
151 {
152         set_timer_to_terminate();
153         return activate_notification_no_param(obj, msg, LED_TORCH);
154 }
155
156 /* USB host notifications */
157 static DBusMessage *usb_storage_noti_on(E_DBus_Object *obj, DBusMessage *msg)
158 {
159         set_timer_to_terminate();
160         return activate_notification_single_param(obj, msg, USB_STORAGE);
161 }
162
163 /* Cooldown notification */
164 static DBusMessage *cooldown_noti_on(E_DBus_Object *obj, DBusMessage *msg)
165 {
166         set_timer_to_terminate();
167         return activate_notification_no_param(obj, msg, TEMP_COOLDOWN);
168 }
169
170 static DBusMessage *usb_storage_ro_noti_on(E_DBus_Object *obj, DBusMessage *msg)
171 {
172         set_timer_to_terminate();
173         return activate_notification_single_param(obj, msg, USB_STORAGE_RO);
174 }
175
176 static DBusMessage *usb_device_noti_on(E_DBus_Object *obj, DBusMessage *msg)
177 {
178         set_timer_to_terminate();
179         return activate_notification_double_param(obj, msg, USB_DEVICE);
180 }
181
182 static DBusMessage *usb_device_noti_update(E_DBus_Object *obj, DBusMessage *msg)
183 {
184         set_timer_to_terminate();
185         return update_notification_double_param(obj, msg, USB_DEVICE);
186 }
187
188 static DBusMessage *media_device_noti_on(E_DBus_Object *obj, DBusMessage *msg)
189 {
190         set_timer_to_terminate();
191         return activate_notification_no_param(obj, msg, MEDIA_DEVICE);
192 }
193
194 static const struct edbus_method
195 dbus_powerkey_methods[] = {
196         { "PopupLaunch", "a{ss}", "i", powerkey_popup },
197         /* Add methods here */
198 };
199
200 static const struct edbus_method
201 dbus_overheat_methods[] = {
202         { "PopupLaunch", "a{ss}", "i", overheat_popup },
203         /* Add methods here */
204 };
205
206 static const struct edbus_method
207 dbus_crash_methods[] = {
208         { "PopupLaunch", "a{ss}", "i", crash_popup      },
209         /* Add methods here */
210 };
211
212 static const struct edbus_method
213 dbus_noti_methods[] = {
214         /* LED */
215         { "LedTorchNotiOn"              , NULL          , "i"   , led_torch_noti_on             },
216         { "LedTorchNotiOff"             , "i"           , "i"   , noti_off                      },
217         /* USB storage */
218         { "UsbStorageNotiOn"            , "s"           , "i"   , usb_storage_noti_on           },
219         { "UsbStorageRoNotiOn"          , "s"           , "i"   , usb_storage_ro_noti_on        },
220         { "UsbStorageNotiOff"           , "i"           , "i"   , noti_off                      },
221         { "UsbDeviceNotiOn"             , "ss"          , "i"   , usb_device_noti_on            },
222         { "UsbDeviceNotiUpdate"         , "isss"        , "i"   , usb_device_noti_update        },
223         { "UsbDeviceNotiOff"            , "i"           , "i"   , noti_off                      },
224         /* usb connection */
225         { "MediaDeviceNotiOn"           , NULL          , "i"   , media_device_noti_on  },
226         { "MediaDeviceNotiOff"          , "i"           , "i"   , noti_off      },
227         /* Battery Event noti*/
228         { "BatteryFullNotiOn"           , NULL          , "i"   , battery_full_noti_on          },
229         { "BatteryFullNotiOff"          , "i"           , "i"   , noti_off                      },
230         { "BatteryChargeNotiOn"         , NULL          , "i"   , battery_charge_noti_on        },
231         /* Temperature */
232         { "TempCooldownNotiOn"          , NULL          , "i"   , cooldown_noti_on              },
233         { "TempCooldownNotiOff"         , "i"           , "i"   , noti_off                      },
234         /* SD Card */
235         { "SDcardNoti"                  , "s"           , "i"   , sdcard_noti   },
236         /* Data Usage */
237         { "DataWarningNotiOn"           , NULL          , "i"   , data_warning_noti_on          },
238         { "DataWarningNotiOff"          , "i"           , "i"   , noti_off                      },
239         { "DataDisabledNotiOn"          , NULL          , "i"   , data_disabled_noti_on },
240         { "DataDisabledNotiOff"         , "i"           , "i"   , noti_off              },
241         /* Add notifications here */
242 };
243
244 static struct edbus_object
245 edbus_objects[] = {
246         { POPUP_PATH_SYSTEM             , POPUP_IFACE_SYSTEM    , NULL  , NULL  ,
247                 dbus_system_methods     , ARRAY_SIZE(dbus_system_methods)               },
248         { POPUP_PATH_POWERKEY           , POPUP_IFACE_POWERKEY  , NULL  , NULL  ,
249                 dbus_powerkey_methods   , ARRAY_SIZE(dbus_powerkey_methods)             },
250         { POPUP_PATH_OVERHEAT           , POPUP_IFACE_OVERHEAT  , NULL  , NULL  ,
251                 dbus_overheat_methods   , ARRAY_SIZE(dbus_overheat_methods)             },
252         { POPUP_PATH_NOTI               , POPUP_IFACE_NOTI      , NULL  , NULL  ,
253                 dbus_noti_methods       , ARRAY_SIZE(dbus_noti_methods)                 },
254         { POPUP_PATH_CRASH              , POPUP_IFACE_CRASH     , NULL  , NULL  ,
255                 dbus_crash_methods      , ARRAY_SIZE(dbus_crash_methods)                },
256         /* Add new object & interface here*/
257 };
258
259 static int init_methods(void)
260 {
261         int ret;
262         int i, j;
263
264
265         for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) {
266                 for (j = 0; j < edbus_objects[i].methods_len; j++) {
267                         ret = e_dbus_interface_method_add(edbus_objects[i].iface,
268                                         edbus_objects[i].methods[j].member,
269                                         edbus_objects[i].methods[j].signature,
270                                         edbus_objects[i].methods[j].reply_signature,
271                                         edbus_objects[i].methods[j].func);
272                         if (!ret) {
273                                 _E("Failed to add method(%s).", edbus_objects[i].methods[j].member);
274                                 return -ECONNREFUSED;
275                         }
276                 }
277         }
278         return 0;
279 }
280
281 static int register_dbus(void)
282 {
283         DBusError error;
284         int retry, ret, i;
285
286         dbus_error_init(&error);
287
288         retry = 0;
289         do {
290                 if (e_dbus_init())
291                         break;
292                 if (++retry == RETRY_MAX) {
293                         _E("Failed to init edbus.");
294                         return -ECONNREFUSED;
295                 }
296         } while (retry <= RETRY_MAX);
297
298         retry = 0;
299         do {
300                 edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
301                 if (edbus_conn)
302                         break;
303                 if (++retry == RETRY_MAX) {
304                         _E("Failed to get edbus.");
305                         ret = -ECONNREFUSED;
306                         goto out1;
307                 }
308         } while (retry <= RETRY_MAX);
309
310         for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) {
311                 edbus_objects[i].obj = e_dbus_object_add(edbus_conn, edbus_objects[i].path, NULL);
312                 if (!(edbus_objects[i].obj)) {
313                         _E("Failed to add edbus obj.");
314                         ret = -ECONNREFUSED;
315                         goto out2;
316                 }
317
318                 edbus_objects[i].iface = e_dbus_interface_new(edbus_objects[i].interface);
319                 if (!(edbus_objects[i].iface)) {
320                         _E("Failed to add edbus interface.");
321                         ret = -ECONNREFUSED;
322                         goto out2;
323                 }
324
325                 e_dbus_object_interface_attach(edbus_objects[i].obj, edbus_objects[i].iface);
326         }
327
328         retry = 0;
329         do {
330                 edbus_request_name = e_dbus_request_name(edbus_conn, BUS_NAME, 0, NULL, NULL);
331                 if (edbus_request_name)
332                         break;
333                 if (++retry == RETRY_MAX) {
334                         _E("Failed to request edbus name.");
335                         ret = -ECONNREFUSED;
336                         goto out2;
337                 }
338         } while (retry <= RETRY_MAX);
339
340         return 0;
341
342 out2:
343         e_dbus_connection_close(edbus_conn);
344 out1:
345         e_dbus_shutdown();
346
347         return ret;
348 }
349
350 int main(int argc, char *argv[])
351 {
352         int ret;
353
354         ecore_init();
355
356         ret = register_dbus();
357         if (ret < 0)
358                 return ret;
359
360         ret = init_methods();
361         if (ret < 0)
362                 return ret;
363
364         ecore_main_loop_begin();
365         ecore_shutdown();
366         return 0;
367 }