tizen 2.4 release
[apps/home/quickpanel.git] / daemon / common_uic.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
19 #include <Elementary.h>
20 #include <sys/utsname.h>
21 #include <Ecore_Input.h>
22
23 #include <app.h>
24 #include <vconf.h>
25 #include <notification.h>
26 #include <app_control_internal.h>
27 #include <bundle_internal.h>
28 #include <system_info.h>
29 #include <common_uic.h>
30
31 #if defined(WINSYS_X11)
32 #include <Ecore_X.h>
33 #include <X11/Xlib.h>
34 #include <utilX.h>
35 #endif
36
37 #include <tzsh.h>
38 #include <tzsh_quickpanel_service.h>
39
40 #include "common.h"
41 #include "quickpanel-ui.h"
42
43
44 /* binary information */
45 #define QP_EMUL_STR             "Emulator"
46 #define DEL_TIMER_VALUE 0.480
47 #define SYSTEM_INFO_KEY_MODEL "http://tizen.org/system/model_name"
48 static Ecore_Timer *_close_timer = NULL;
49
50
51 static void _quickpanel_move_data_to_service(const char *key, const char *val, void *data)
52 {
53         retif(data == NULL || key == NULL || val == NULL, , "Invialid parameter!");
54
55         app_control_h service = data;
56         app_control_add_extra_data(service, key, val);
57 }
58
59 HAPI Evas_Object *quickpanel_uic_load_edj(Evas_Object * parent, const char *file, const char *group, int is_just_load)
60 {
61         Eina_Bool r;
62         Evas_Object *eo = NULL;
63
64         retif(parent == NULL, NULL, "Invalid parameter!");
65
66         eo = elm_layout_add(parent);
67         retif(eo == NULL, NULL, "Failed to add layout object!");
68
69         r = elm_layout_file_set(eo, file, group);
70         retif(r != EINA_TRUE, NULL, "Failed to set edje object file[%s-%s]!", file, group);
71
72         evas_object_size_hint_weight_set(eo, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
73         evas_object_size_hint_align_set(eo, EVAS_HINT_FILL, EVAS_HINT_FILL);
74
75         if (is_just_load == 1) {
76                 elm_win_resize_object_add(parent, eo);
77         }
78         evas_object_show(eo);
79
80         return eo;
81 }
82
83 HAPI int quickpanel_uic_is_opened(void)
84 {
85         struct appdata *ad = quickpanel_get_app_data();
86         retif(ad == NULL, 0, "invalid data.");
87
88         return ad->is_opened;
89 }
90
91 HAPI int quickpanel_uic_is_suspended(void)
92 {
93         struct appdata *ad = quickpanel_get_app_data();
94         retif(ad == NULL, 0, "invalid data.");
95
96         return ad->is_suspended;
97 }
98
99 HAPI int quickpanel_uic_is_emul(void)
100 {
101         int is_emul = 0;
102         char *info = NULL;
103
104         if (system_info_get_platform_string(SYSTEM_INFO_KEY_MODEL, &info) == 0) {
105                 if (info == NULL) {
106                         return 0;
107                 }
108                 if (!strncmp(QP_EMUL_STR, info, strlen(info))) {
109                         is_emul = 1;
110                 }
111         }
112
113         free(info);
114
115         return is_emul;
116 }
117
118 HAPI void quickpanel_uic_initial_resize(Evas_Object *obj, int height)
119 {
120         struct appdata *ad = quickpanel_get_app_data();
121         retif(ad == NULL, , "invalid data.");
122
123         height = (height % 2 != 0) ? height + 1 : height;
124
125         if (ad->angle == 90 || ad->angle == 270) {
126                 evas_object_resize(obj, ad->win_height, height * ad->scale);
127         } else  {
128                 evas_object_resize(obj, ad->win_width, height * ad->scale);
129         }
130 }
131
132 HAPI int quickpanel_uic_launch_app(char *app_id, void *data)
133 {
134         int ret = APP_CONTROL_ERROR_NONE;
135         app_control_h service = NULL;
136         char *app_id_from_service = NULL;
137
138         retif(app_id == NULL && data == NULL, APP_CONTROL_ERROR_INVALID_PARAMETER, "Invialid parameter!");
139
140         ret = app_control_create(&service);
141         if (ret != APP_CONTROL_ERROR_NONE) {
142                 ERR("app_control_create() return error : %d", ret);
143                 return ret;
144         }
145         retif(service == NULL, APP_CONTROL_ERROR_INVALID_PARAMETER, "fail to create service handle!");
146
147         if (app_id != NULL) {
148                 app_control_set_operation(service, APP_CONTROL_OPERATION_DEFAULT);
149                 app_control_set_app_id(service, app_id);
150
151                 if (data != NULL) {
152                         bundle_iterate((bundle *)data, _quickpanel_move_data_to_service, service);
153                 }
154         } else {
155                 if (data != NULL) {
156                         ret = app_control_import_from_bundle(service, data);
157                         if (ret != APP_CONTROL_ERROR_NONE) {
158                                 ERR("Failed to import[%d]", ret);
159                         }
160                 }
161         }
162
163         ret = app_control_send_launch_request(service, NULL, NULL);
164         if (ret != APP_CONTROL_ERROR_NONE) {
165                 ERR("app_control_send_launch_request() is failed : %d", ret);
166                 app_control_get_app_id(service, &app_id_from_service);
167                 if (app_id_from_service != NULL) {
168                         quickpanel_uic_launch_app_inform_result(app_id_from_service, ret);
169                         free(app_id_from_service);
170                 } else {
171                         quickpanel_uic_launch_app_inform_result(app_id, ret);
172                 }
173                 app_control_destroy(service);
174                 return ret;
175         }
176         app_control_destroy(service);
177         return ret;
178 }
179
180 HAPI int quickpanel_uic_launch_ug_by_appcontrol(const char *package, void *data)
181 {
182         int ret = APP_CONTROL_ERROR_NONE;
183         app_control_h service = NULL;
184         struct appdata *ad = quickpanel_get_app_data();
185         retif(ad == NULL, APP_CONTROL_ERROR_INVALID_PARAMETER, "ad null");
186         retif(package == NULL, APP_CONTROL_ERROR_INVALID_PARAMETER, "package null");
187
188         ret = app_control_create(&service);
189         if (ret != APP_CONTROL_ERROR_NONE) {
190                 ERR("app_control_create() return error : %d", ret);
191                 return ret;
192         }
193         retif(service == NULL, APP_CONTROL_ERROR_INVALID_PARAMETER, "fail to create service handle!");
194
195         app_control_set_operation(service, APP_CONTROL_OPERATION_DEFAULT);
196         app_control_set_app_id(service, package);
197
198         if (data != NULL) {
199                 bundle_iterate((bundle *)data, _quickpanel_move_data_to_service, service);
200         }
201
202         ret = app_control_send_launch_request(service, NULL, NULL);
203         if (ret != APP_CONTROL_ERROR_NONE) {
204                 ERR("app_control_send_launch_request() is failed : %d", ret);
205                 app_control_destroy(service);
206                 return ret;
207         }
208         app_control_destroy(service);
209         return ret;
210 }
211
212 HAPI void quickpanel_uic_launch_app_inform_result(const char *pkgname, int retcode)
213 {
214         retif(retcode == APP_CONTROL_ERROR_NONE, , "retcode = APP_CONTROL_ERROR_NONE!");
215         retif(pkgname == NULL && retcode != APP_CONTROL_ERROR_APP_NOT_FOUND, , "Invialid parameter!");
216
217         const char *msg = NULL;
218         char *app_label = NULL;
219
220         if (retcode == APP_CONTROL_ERROR_APP_NOT_FOUND) {
221                 notification_status_message_post(_NOT_LOCALIZED("Unable to find application to perform this action."));
222         } else {
223                 Eina_Strbuf *strbuf = eina_strbuf_new();
224                 char *format = _("IDS_QP_TPOP_UNABLE_TO_OPEN_PS");
225
226                 if (strbuf != NULL) {
227                         app_label = quickpanel_common_ui_get_pkginfo_label(pkgname);
228                         if (app_label != NULL) {
229                                 eina_strbuf_append_printf(strbuf, format, app_label);
230                                 free(app_label);
231                         } else {
232                                 eina_strbuf_append_printf(strbuf, format, pkgname);
233                         }
234                         eina_strbuf_append_printf(strbuf, "(%x)", retcode);
235                         msg = eina_strbuf_string_get(strbuf);
236
237                         if (msg != NULL) {
238                                 notification_status_message_post(msg);
239                         }
240                         eina_strbuf_free(strbuf);
241                 }
242         }
243 }
244
245 HAPI void quickpanel_uic_open_quickpanel(int reason)
246 {
247         struct appdata *ad = quickpanel_get_app_data();
248
249         DBG("reason:%d", reason);
250
251         retif(ad == NULL, , "Invalid parameter!");
252         retif(ad->win == NULL, , "Invalid parameter!");
253
254 #if defined(WINSYS_X11)
255         Ecore_X_Window xwin;
256         Ecore_X_Window zone;
257         xwin = elm_win_xwindow_get(ad->win);
258         if (xwin != 0) {
259                 if ((zone = ecore_x_e_illume_zone_get(xwin)) != 0) {
260                         if (ecore_x_e_illume_quickpanel_state_get(zone) == ECORE_X_ILLUME_QUICKPANEL_STATE_OFF) {
261                                 ecore_x_e_illume_quickpanel_state_send(zone, ECORE_X_ILLUME_QUICKPANEL_STATE_ON);
262                                 quickpanel_uic_opened_reason_set(reason);
263                         }
264                 } else {
265                         ERR("failed to get zone");
266                 }
267         } else {
268                 ERR("failed to get xwin");
269         }
270 #else
271         ERR("Not yet implemented");
272 #endif
273 }
274
275 HAPI void quickpanel_uic_opened_reason_set(int reason)
276 {
277         struct appdata *ad = quickpanel_get_app_data();
278         retif(ad == NULL, , "Invalid parameter!");
279
280         ad->opening_reason = reason;
281 }
282
283 HAPI int quickpanel_uic_opened_reason_get(void)
284 {
285         struct appdata *ad = quickpanel_get_app_data();
286         retif(ad == NULL, OPENED_NO_REASON, "Invalid parameter!");
287
288         return ad->opening_reason;
289 }
290
291 static void _quickpanel_close(void)
292 {
293         struct appdata *ad = quickpanel_get_app_data();
294
295         DBG("");
296
297         if (!ad || !ad->win) {
298                 ERR("Invalid parameter");
299                 return;
300         }
301
302 #if defined(WINSYS_X11)
303         Ecore_X_Window xwin;
304         Ecore_X_Window zone;
305         xwin = elm_win_xwindow_get(ad->win);
306         if (xwin != 0) {
307                 if ((zone = ecore_x_e_illume_zone_get(xwin)) != 0) {
308                         if (ecore_x_e_illume_quickpanel_state_get(zone) == ECORE_X_ILLUME_QUICKPANEL_STATE_ON) {
309                                 ecore_x_e_illume_quickpanel_state_send(zone, ECORE_X_ILLUME_QUICKPANEL_STATE_OFF);
310                         }
311                 } else {
312                         ERR("failed to get zone");
313                 }
314         } else {
315                 ERR("failed to get xwin");
316         }
317 #else
318         ERR("Not yet implemented");
319 #endif
320 }
321
322 static Eina_Bool _quickpanel_close_timer_cb(void *data)
323 {
324         if (_close_timer != NULL) {
325                 _close_timer = NULL;
326         }
327         _quickpanel_close();
328
329         return ECORE_CALLBACK_CANCEL;
330 }
331
332 HAPI void quickpanel_uic_close_quickpanel(bool is_check_lock, int is_delay_needed) {
333         int ret = 0;
334         int is_lock_launched = VCONFKEY_IDLE_UNLOCK;
335
336         if (is_check_lock == true) {
337                 if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &is_lock_launched) == 0) {
338                         if (is_lock_launched == VCONFKEY_IDLE_LOCK) {
339                                 ret = vconf_set_int(VCONFKEY_IDLE_LOCK_STATE, VCONFKEY_IDLE_UNLOCK);
340                                 if (ret == 0) {
341                                         ERR("unlock the lockscreen from quickpanel");
342                                 } else {
343                                         ERR("failed to unlock the lockscreen from quickpanel");
344                                 }
345                         }
346                 }
347         }
348
349         if (is_delay_needed) {
350                 if( _close_timer == NULL ) {
351                         _close_timer = ecore_timer_add(DEL_TIMER_VALUE, _quickpanel_close_timer_cb, NULL);
352                 }
353         } else {
354                 _quickpanel_close();
355         }
356 }
357
358 HAPI void quickpanel_uic_toggle_openning_quickpanel(void)
359 {
360         Ecore_X_Window xwin;
361         Ecore_X_Window zone;
362         struct appdata *ad = quickpanel_get_app_data();
363
364         DBG("");
365
366         if (!ad || !ad->win) {
367                 ERR("Invalid parameters");
368                 return;
369         }
370
371         xwin = elm_win_xwindow_get(ad->win);
372         if (xwin != 0) {
373                 if ((zone = ecore_x_e_illume_zone_get(xwin)) != 0) {
374                         if (ecore_x_e_illume_quickpanel_state_get(zone) == ECORE_X_ILLUME_QUICKPANEL_STATE_ON) {
375                                 ecore_x_e_illume_quickpanel_state_send(zone, ECORE_X_ILLUME_QUICKPANEL_STATE_OFF);
376                         } else {
377                                 ecore_x_e_illume_quickpanel_state_send(zone, ECORE_X_ILLUME_QUICKPANEL_STATE_ON);
378                         }
379                 } else {
380                         ERR("failed to get zone");
381                 }
382         } else {
383                 ERR("failed to get xwin");
384         }
385 }