Merge "add missing packages requires" into tizen
[profile/ivi/ico-uxf-homescreen.git] / src / onscreen / CicoOSPopWindow.cpp
1 /*
2  * Copyright (c) 2014, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   On Screen Popup Window
11  *
12  * @date    Feb-17-2014
13  */
14 #include <Ecore.h>
15 #include <Ecore_Wayland.h>
16 #include <appsvc/appsvc.h>
17 #include "CicoOnScreen.h"
18 #include "CicoNotification.h"
19 #include "CicoOSPopWindow.h"
20 #include "ico_syc_type.h"
21 #include "ico_syc_appresctl.h"
22 #include "CicoOSClient.h"
23
24 using namespace std;
25
26 //==========================================================================
27 // static members
28 //==========================================================================
29 Ecore_Evas*  CicoOSPopWindow::m_window = NULL;
30
31 //==========================================================================
32 // functions
33 //==========================================================================
34
35 //--------------------------------------------------------------------------
36 /**
37  * @brief   CicoOnScreen::CicoOnScreen
38  *          Constractor
39  *
40  * @param[in]   none
41  * @return      none
42  */
43 //--------------------------------------------------------------------------
44 CicoOSPopWindow::CicoOSPopWindow(notification_type_e type) : CicoNotification(type)
45 {
46     ICO_TRA("Enter type(%d)", (int)type);
47     m_icon        = NULL;
48     m_theme       = NULL;
49     m_resourceId  = CicoOnScreen::GetResourceId();
50     m_context     = NULL;
51     m_buttonTouch = false;
52     ICO_TRA("Leave");
53 }
54
55 CicoOSPopWindow::CicoOSPopWindow(notification_h noti) : CicoNotification(noti)
56 {
57     ICO_TRA("Enter");
58     m_icon        = NULL;
59     m_theme       = NULL;
60     m_resourceId  = CicoOnScreen::GetResourceId();
61     m_context     = NULL;
62     m_buttonTouch = false;
63     ICO_TRA("Leave");
64 }
65
66 //--------------------------------------------------------------------------
67 /**
68  * @brief   CicoOnScreen::~CicoOnScreen
69  *          Destractor
70  *
71  * @param[in]   none
72  * @return      none
73  */
74 //--------------------------------------------------------------------------
75 CicoOSPopWindow::~CicoOSPopWindow(void)
76 {
77     ICO_TRA("Enter sur:%08x, priv_id:%d", m_resourceId, GetPrivId());
78
79     if (NULL != m_context) {
80         int r = ico_syc_release_res(m_context);
81         ICO_DBG("_____ %d = ico_syc_release_res", r);
82     }
83     ICO_DBG("_____ %d, %d", (int)m_buttonTouch, (int)m_appsvc_pkgname.empty());
84
85     if ((true == m_buttonTouch) && (false == m_appsvc_pkgname.empty())) {
86         CicoOSClient* cosc = CicoOSClient::getInstance();
87         if (NULL != cosc) {
88             int r = cosc->sendLaunchMessage(m_appsvc_pkgname);
89             ICO_DBG("_____ SendMsg:%d, %s", r, m_appsvc_pkgname.c_str());
90         }
91     }
92
93     if (m_theme)    {
94         usleep(POPUP_DELETE_WAIT*1000);
95         evas_object_hide(m_theme);
96         evas_object_del(m_theme);
97         m_theme = NULL;
98     }
99     if (m_icon) {
100         evas_object_hide(m_icon);
101         evas_object_del(m_icon);
102         m_icon = NULL;
103     }
104     ICO_TRA("Leave");
105 }
106
107 //--------------------------------------------------------------------------
108 /**
109  * @brief   CicoOnScreen::ShowPopup
110  *          Show popup window
111  *
112  * @param[in]   none
113  * @return      none
114  */
115 //--------------------------------------------------------------------------
116 bool
117 CicoOSPopWindow::showPopup()
118 {
119     ICO_TRA("Enter");
120
121     if (NULL == m_theme) {
122         if (false == InitializeWindow()) {
123             ICO_TRA("Leave false");
124             return false;
125         }
126     }
127     // get caller pachage name
128     const char *pkgname = GetPkgname();
129
130     // get priv_id
131     int priv_id = GetPrivId();
132
133     // Get title
134     const char *title = GetTitle();
135
136     // Get content
137     const char *content = GetContent();
138
139     // Get execute option
140     const char *text = NULL;
141     bundle *service_handle = NULL;
142     (void)GetExecuteOption(NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, &text,
143                            &service_handle);
144
145     // Get icon path
146     const char *icon = GetIconPath();
147     ICO_DBG("Received: %s : %i : %s : %s : %s : %p",
148             pkgname, priv_id, title, content, text, (void *)service_handle);
149
150     if (NULL != service_handle) {
151         const char* pn = appsvc_get_pkgname(service_handle);
152         if (NULL != pn) {
153             ICO_DBG("Received: appsvc_get_pkgname:%s", pn);
154             m_appsvc_pkgname = pn;
155             m_buttonTouch = false;
156         }
157     }
158
159     if (icon) {
160         if (NULL != m_icon) {
161             evas_object_image_file_set(m_icon, icon, NULL);
162             evas_object_show(m_icon);
163         }
164     }
165
166     if (title) {
167         if (!edje_object_part_text_set(m_theme, "title_text", title)) {
168             ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
169         }
170     }
171
172     if (content) {
173         if (strlen(content) <= 25) {
174             ICO_DBG("content text center");
175             if (!edje_object_part_text_set(m_theme, "content_text_center", content)) {
176                 ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
177             }
178             if (!edje_object_part_text_set(m_theme, "content_text_left", "")) {
179                 ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
180             }
181         }
182         else {
183             ICO_DBG("content text left");
184             if (!edje_object_part_text_set(m_theme, "content_text_left", content)) {
185                 ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
186             }
187             if (!edje_object_part_text_set(m_theme, "content_text_center", "")) {
188                 ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
189             }
190         }
191     }
192
193     if (text) {
194         if (!edje_object_part_text_set(m_theme, "button_text", text)) {
195             ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
196         }
197     }
198     else {
199         if (!edje_object_part_text_set(m_theme, "button_text", "OK")) {
200             ICO_WRN("could not set the text. Maybe part 'text' does not exist?");
201         }
202     }
203
204     evas_object_show(m_theme);
205     ICO_TRA("Leave true");
206     return true;
207 }
208
209 bool
210 CicoOSPopWindow::acquireRes()
211 {
212     ICO_TRA("Enter");
213     if (NULL != m_context) {
214         ICO_TRA("Leave false");
215         return false;
216     }
217     ico_syc_res_window_t w;
218     makeResWindowT(w);
219     m_context = ico_syc_acquire_res(&w, NULL, NULL, ICO_SYC_RES_ONSCREEN);
220     if (NULL == m_context) {
221         ICO_TRA("Leave false");
222         return false;
223     }
224     ICO_TRA("Leave true");
225     return true;
226 }
227
228 //--------------------------------------------------------------------------
229 /**
230  * @brief   CicoOnScreen::HidePopup
231  *          Hide popup window
232  *
233  * @param[in]   data        The user data passed from the callback
234  *                          registration function
235  * @param[in]   e           The handle to the popup window
236  * @param[in]   obj         The handle to the Evas object
237  * @param[in]   event_info  Event information
238  * @return      none
239  */
240 //--------------------------------------------------------------------------
241 void
242 CicoOSPopWindow::hidePopup(bool buttonTouch)
243 {
244     ICO_TRA("Enter %s", buttonTouch? "true": "false");
245     m_buttonTouch = buttonTouch;
246     releaseRes();
247     ICO_TRA("Leave");
248 }
249
250 bool
251 CicoOSPopWindow::releaseRes()
252 {
253     ICO_TRA("Enter");
254     if (NULL != m_context) {
255         int r = ico_syc_release_res(m_context);
256         ICO_DBG("_____ %d = ico_syc_release_res", r);
257         if (ICO_SYC_ERR_NONE == r) {
258             m_context = NULL;
259             ICO_TRA("Leave true");
260             return true;
261         }
262     }
263     ICO_TRA("Leave false");
264     return false;
265 }
266
267 //--------------------------------------------------------------------------
268 /**
269  * @brief   CicoOnScreen::InitializeWindow
270  *          Initialize popup window
271  *
272  * @param[in]   none
273  * @return      none
274  */
275 //--------------------------------------------------------------------------
276 bool
277 CicoOSPopWindow::InitializeWindow(void)
278 {
279     ICO_TRA("Enter");
280
281     if (NULL == m_window) {
282         if (false == createMainWindow()) {
283             ICO_TRA("Leave(ERR)");
284             return false;
285         }
286     }
287     m_theme = edje_object_add(ecore_evas_get(m_window));
288     if (NULL == m_theme) {
289         ICO_ERR("could not create edje object!");
290         ICO_TRA("Leave(ERR)");
291         return false;
292     }
293     if (! edje_object_file_set(m_theme, ICO_OS_THEMES_EDJ_FILEPATH, "main")) {
294         Edje_Load_Error err = edje_object_load_error_get(m_theme);
295         const char *errmsg = edje_load_error_str(err);
296         ICO_ERR("could not load 'main' from onscreen.edj: %s", errmsg);
297         evas_object_del(m_theme);
298         m_theme = NULL;
299         return false;
300     }
301     // icon setup
302     m_icon = evas_object_image_filled_add(ecore_evas_get(m_window));
303     evas_object_pointer_mode_set(m_icon, EVAS_OBJECT_POINTER_MODE_NOGRAB);
304     edje_object_part_swallow(m_theme, "icon", m_icon);
305
306     /* getting size of screen */
307     /* home screen size is full of display */
308     int display_width  = 0;
309     int display_height = 0;
310     ecore_wl_screen_size_get(&display_width, &display_height);
311
312     ICO_DBG("display size w/h=%d/%d", display_width, display_height);
313     int popup_x = (display_width / 2) - (POPUP_FRAME_WIDTH / 2);
314     int popup_y = (display_height / 2) - (POPUP_FRAME_HEIGHT/ 2);
315     ICO_DBG("popup postion x/y=%d/%d", popup_x, popup_y);
316     evas_object_move(m_theme, popup_x, popup_y);
317     evas_object_resize(m_theme, POPUP_FRAME_WIDTH, POPUP_FRAME_HEIGHT);
318
319     Evas_Object* obj = NULL;
320     obj = (Evas_Object*)edje_object_part_object_get(m_theme, "button_text");
321     if (NULL != obj) {
322         evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
323                                        evasMouseUpCB, this);
324     }
325
326     obj = (Evas_Object*)edje_object_part_object_get(m_theme, "button");
327     if (NULL != obj) {
328         evas_object_event_callback_add(obj, EVAS_CALLBACK_MOUSE_UP,
329                                        evasMouseUpCB, this);
330     }
331     evas_object_show(m_theme);
332     ICO_TRA("Leave(OK)");
333     return true;
334 }
335
336 bool
337 CicoOSPopWindow::createMainWindow()
338 {
339     ICO_TRA("Enter");
340
341     // Window setup
342     m_window = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, "frame=0");
343     if (NULL == m_window) {
344         ICO_ERR("ecore_evas_new() error");
345         ICO_TRA("Leave(ERR)");
346         return false;
347     }
348
349     ecore_evas_title_set(m_window, POPUP_WINDOW_TITLE);
350     ecore_evas_alpha_set(m_window, EINA_TRUE);
351     ecore_evas_show(m_window);
352
353     ICO_TRA("Leave");
354     return true;
355 }
356
357 void
358 CicoOSPopWindow::removeMainWindow()
359 {
360     ICO_TRA("Enter");
361
362     if (NULL != m_window) {
363         ecore_evas_free(m_window);
364         m_window = NULL;
365     }
366     ICO_TRA("Leave");
367 }
368
369 //--------------------------------------------------------------------------
370 /**
371  * @brief   callback function of evas mouse up event
372  *
373  * @param [in] data         The user data passed from the callback
374  *                          registration function
375  * @param [in] e            The handle to the popup window
376  * @param [in] obj          The handle to the Evas object
377  * @param [in] event_info   Event information
378  *
379  */
380 //--------------------------------------------------------------------------
381 void
382 CicoOSPopWindow::evasMouseUpCB(void *data, Evas *e, Evas_Object *obj,
383                                void *event_info)
384 {
385     ICO_TRA("Enter %x, %x, %x, %x", data, e, obj, event_info);
386     if (NULL != obj) {
387         ICO_DBG("_____ obj name=%s", evas_object_name_get(obj));
388     }
389     static_cast<CicoOSPopWindow*>(data)->hidePopup(true);
390     ICO_TRA("Leave");
391 }
392
393
394 static const char id0_ECU[]        = "Center";
395
396 static const char id00_display[]   = "Center";
397 static const char id006_layer[]    = "OnScreen";
398 static const char id00611_layout[] = "Whole";
399 static const char id006111_area[]  = "Full";
400
401 static const char role_incoming[]  = "incoming";
402 static const char role_message[]   = "message";
403
404 void
405 CicoOSPopWindow::makeResWindowT(ico_syc_res_window_t& w)
406 {
407     w.ECU         = id0_ECU;
408     w.display     = id00_display;
409     w.layer       = id006_layer;
410     w.layout      = id00611_layout;
411     w.area        = id006111_area;
412     w.dispatchApp = (char*)GetPkgname();
413     if (0 == strcmp(w.dispatchApp,"org.tizen.dialer")) {
414         w.role    = role_incoming;
415     }
416     else {
417         w.role    = role_message;
418     }
419     w.resourceId  = m_resourceId;
420 }
421
422 // vim: set expandtab ts=4 sw=4: