bug fix: TC-1152
[profile/ivi/ico-uxf-homescreen.git] / src / statusbar / CicoComponentImplementation.cpp
1 /*
2  * Copyright (c) 2013, 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 //==========================================================================
11 /**
12  *  @file   CicoComponentimplementation.cpp
13  *
14  *  @brief  This file is implimentation of CicoStatusBarClockComponent class
15  *                                  and CicoNotificationPanelComponent class
16  */
17 //==========================================================================
18
19 #include <Edje.h>
20 #include <ctime>
21 #include <cassert>
22 #include <memory>
23 #include <tzplatform_config.h>
24
25 #include <ico_log.h>
26 #include "ico_syc_private.h"
27 #include "CicoComponentImplementation.h"
28
29 /*  define of path   */
30 #define IMG_DIR     ICO_SYC_PACKAGE_STATUSBAR "/res/images"
31 #define THEME_PATH  ICO_SYC_PACKAGE_STATUSBAR "/res/themes/statusbar.edj"
32
33 /*  Theme group name  */
34 #define THEME_CLOCK_NAME        "ClockImage"
35 #define THEME_NOTIFICATION_NAME "Notification"
36
37 // set image file path of clock
38 static const char *clock_image_path[] = {
39     "time_0.png",
40     "time_1.png",
41     "time_2.png",
42     "time_3.png",
43     "time_4.png",
44     "time_5.png",
45     "time_6.png",
46     "time_7.png",
47     "time_8.png",
48     "time_9.png",
49     "time_am.png",
50     "time_pm.png",
51     "time_ten.png",
52     NULL
53 };
54
55 /*  define of special image index  */
56 #define TIME_IMAGE_AM (10)
57 #define TIME_IMAGE_PM (11)
58 #define TIME_IMAGE_SP (12)
59
60 /*  array of clock object name  */
61 static const char *clock_image_object[] = {
62     "AM_PM_img",
63     "HOUR10_img",
64     "HOUR1_img",
65     "TIME_SP_img",
66     "MIN10_img",
67     "MIN1_img",
68     NULL
69 };
70
71 /*--------------------------------------------------------------------------*/
72 /**
73  *  @brief   default constructor
74  *
75  *  @param[in]   none
76  *  @return      none
77  */
78 /*--------------------------------------------------------------------------*/
79 CicoStatusBarClockComponent::CicoStatusBarClockComponent()
80 {
81 }
82
83 /*--------------------------------------------------------------------------*/
84 /**
85  *  @brief   destructor
86  *
87  *  @param[in]   none
88  *  @return      none
89  */
90 /*--------------------------------------------------------------------------*/
91 CicoStatusBarClockComponent::~CicoStatusBarClockComponent()
92 {
93 }
94
95 /*--------------------------------------------------------------------------*/
96 /**
97  * @brief   initialize of clock component
98  *
99  *  @param[in]  windowobj
100  *  @param[in]  posx
101  *  @param[in]  posy
102  *  @return     true: success   false: failed
103  */
104 /*--------------------------------------------------------------------------*/
105 bool
106 CicoStatusBarClockComponent::Initialize(Evas_Object *windowobj, Evas *evas)
107 {
108     ICO_TRA("CicoStatusBarClockComponent::Initialize Enter");
109
110     /*  initialize display clock image */
111     int i;
112     for(i=0; i<CLOCK_OBJECT_COUNT; i++ ){
113         last_clock_image[i] = -1;
114     }
115
116     /*  make TimeImage object  */
117     evasobj_=edje_object_add(evas);
118     char    wrk_path[256];
119     snprintf(wrk_path, sizeof(wrk_path), "%s/" THEME_PATH,
120              tzplatform_getenv(TZ_SYS_RO_APP));
121     if (!edje_object_file_set(evasobj_, wrk_path, THEME_CLOCK_NAME )) {
122         Edje_Load_Error err = edje_object_load_error_get(evasobj_);
123         const char *errmsg = edje_load_error_str(err);
124         ICO_ERR("could not load 'main' from statusbar.edj: %s",
125                 errmsg);
126         return false;
127     }
128
129     /*  move TimeImage to UpRight of window  */
130     /*  Do not need this process , if positiong of UpRight enabled with .EDC file */
131     Evas_Coord x,y,w,h;
132     Evas_Coord w_width=0;
133     Evas_Object* obj = NULL;
134     evas_object_geometry_get( windowobj, &x, &y, &w_width, &h );
135     evas_object_resize( evasobj_, w_width, h );
136     obj = (Evas_Object*)edje_object_part_object_get(evasobj_,"MIN1_img" );
137     evas_object_geometry_get( obj, &x, &y, &w, &h );
138     Evas_Coord offset = w_width - x - w -18;
139
140     for ( i=0; i<CLOCK_OBJECT_COUNT; i++ ) {
141         obj = (Evas_Object*)edje_object_part_object_get(
142                         evasobj_, clock_image_object[i] );
143         if ( obj != NULL ) {
144             evas_object_geometry_get( obj, &x, &y, &w, &h );
145             evas_object_move( obj, x+offset , y);
146             evas_object_show(obj);
147         }
148     }
149
150     /*  initial display  */
151     bool ret = true;
152     ret = Update();
153     Show();
154
155     ICO_TRA("CicoStatusBarClockComponent::Initialize Leave");
156     return ret;
157 }
158
159 /*--------------------------------------------------------------------------*/
160 /**
161  * @brief   update of clock component
162  *
163  *  @param[in]  none
164  *  @return     true: success   false: failed
165  */
166 /*--------------------------------------------------------------------------*/
167 bool
168 CicoStatusBarClockComponent::Update()
169 {
170     //ICO_TRA("CicoStatusBarClockComponent::Update Enter");
171     if (evasobj_ == NULL) {
172         return false;
173     }
174
175     time_t nowtime;
176     nowtime = std::time(NULL);
177     std::tm *tm = std::localtime(&nowtime);
178     int min = tm->tm_min;
179     int hour = tm->tm_hour;
180
181     /*  Make now clock image tabel  */
182     int now_clock_image[CLOCK_OBJECT_COUNT];
183     if (hour > 11) {
184         now_clock_image[0]=TIME_IMAGE_PM;
185     }
186     else {
187         now_clock_image[0]=TIME_IMAGE_AM;
188     }
189     time_t hour_m = hour % 12;
190     now_clock_image[1]=hour_m / 10;
191     now_clock_image[2]=hour_m % 10;
192     now_clock_image[3]=TIME_IMAGE_SP;
193     now_clock_image[4]=min / 10;
194     now_clock_image[5]=min % 10;
195
196     /*  Set now clock image  */
197     int i;
198     for ( i=0; i<CLOCK_OBJECT_COUNT; i++ ) {
199
200         /*  if now clock image different last clock  */
201         if ( now_clock_image[i] != last_clock_image[i] ) {
202
203             Evas_Object* obj = NULL;
204             obj = (Evas_Object*)edje_object_part_object_get(
205                          evasobj_,clock_image_object[i] );
206             if ( obj != NULL ) {
207                 ICO_DBG("CicoStatusBarClockComponent image set[%s][%s]",
208                         clock_image_object[i], clock_image_path[now_clock_image[i]]);
209                 char    wrk_path[256];
210                 snprintf(wrk_path, sizeof(wrk_path), "%s/" IMG_DIR "/%s",
211                          tzplatform_getenv(TZ_SYS_RO_APP),
212                          clock_image_path[now_clock_image[i]]);
213                 evas_object_image_file_set(obj, wrk_path, NULL);
214                 // add update view area
215                 //    (Omitted update area is set so that evas_object_image_file_set())
216                 //Evas_Coord x,y,w,h;
217                 //evas_object_geometry_get( obj, &x, &y, &w, &h );
218                 //evas_object_image_data_update_add( obj, x, y, w, h );
219
220             }
221             else {
222                 ICO_DBG("CicoStatusBarClockComponent image set error[object not found]" );
223             }
224             last_clock_image[i] = now_clock_image[i];
225         }
226     }
227
228     //ICO_TRA("CicoStatusBarClockComponent::Update Leave(true)");
229     return true;
230 }
231
232 /*--------------------------------------------------------------------------*/
233 /**
234  *  @brief   get clock start position
235  *
236  *  @param[out]  x  X axis
237  *  @param[out]  y  Y axis
238  *  @return     true: success   false: failed
239  */
240 /*--------------------------------------------------------------------------*/
241 bool
242 CicoStatusBarClockComponent::GetClockStart( Evas_Coord *x_ret, Evas_Coord *y_ret)
243 {
244     ICO_TRA("CicoStatusBarClockComponent::GetClockStart Enter");
245     if (evasobj_ == NULL) {
246         ICO_TRA("CicoStatusBarClockComponent::GetClockStart Leave(false)");
247         return false;
248     }
249
250     Evas_Object* obj = NULL;
251     obj = (Evas_Object*)edje_object_part_object_get(
252                  evasobj_, clock_image_object[0] );
253     if ( obj == NULL ) {
254         ICO_TRA("CicoStatusBarClockComponent::GetClockStart Leave(false)");
255         return false;
256     }
257
258     Evas_Coord x,y,w,h;
259     evas_object_geometry_get( obj, &x, &y, &w, &h );
260     *x_ret = x;
261     *y_ret = y;
262
263     ICO_TRA("CicoStatusBarClockComponent::GetClockStart Leave(true)");
264     return true;
265 }
266
267 /*--------------------------------------------------------------------------*/
268 /**
269  * @brief   default constructor
270  *
271  *  @param[in]  none
272  *  @return     none
273  */
274 /*--------------------------------------------------------------------------*/
275 CicoNotificationPanelComponent::CicoNotificationPanelComponent()
276     : CicoCommonModule()
277 {
278 }
279
280 /*--------------------------------------------------------------------------*/
281 /**
282  *  @brief   destructor
283  *
284  *  @param[in]  none
285  *  @return     none
286  */
287 /*--------------------------------------------------------------------------*/
288 CicoNotificationPanelComponent::~CicoNotificationPanelComponent()
289 {
290 }
291
292 /*--------------------------------------------------------------------------*/
293 /**
294  *  @brief   initialize of notification component
295  *
296  *  @param[in]  window
297  *  @return     true: success   false: failed
298  */
299 /*--------------------------------------------------------------------------*/
300 bool
301 CicoNotificationPanelComponent::Initialize(Evas_Object *window, Evas *evas)
302 {
303     ICO_TRA("CicoNotificationPanelComponent::Initialize Enter");
304
305     if (evasobj_ != NULL) {
306         ICO_TRA("CicoNotificationPanelComponent::Initialize Levae(false)");
307         return false;
308     }
309
310     if (window == NULL) {
311         ICO_TRA("CicoNotificationPanelComponent::Initialize Levae(false)");
312         return false;
313     }
314
315     /*  create Notification object  */
316     ICO_DBG("Create Text Module end");
317     evasobj_=edje_object_add(evas);
318     char    wrk_path[256];
319     snprintf(wrk_path, sizeof(wrk_path), "%s/" THEME_PATH,
320              tzplatform_getenv(TZ_SYS_RO_APP));
321     if (!edje_object_file_set(evasobj_, wrk_path, THEME_NOTIFICATION_NAME)) {
322         Edje_Load_Error err = edje_object_load_error_get(evasobj_);
323         const char *errmsg = edje_load_error_str(err);
324         ICO_ERR("could not load 'main' from statusbar.edj: %s",
325                 errmsg);
326         return false;
327     }
328     Evas_Coord x,y,w,h;
329     evas_object_geometry_get( window, &x, &y, &w, &h );
330     evas_object_resize( evasobj_, w, h );
331
332     /*  initial display  */
333     SetText("");
334     Hide();
335
336     ICO_TRA("CicoNotificationPanelComponent::Initialize Levae(true)");
337     return true;
338 }
339
340 /*--------------------------------------------------------------------------*/
341 /**
342  *  @brief   set notification panel
343  *
344  *  @param[in]  text
345  *  @param[in]  iconpaht
346  *  @param[in]  soundpath
347  *  @return     true: success   false: failed
348  */
349 /*--------------------------------------------------------------------------*/
350 void
351 CicoNotificationPanelComponent::SetNotification(const char *text,
352                                                 const char *iconpath,
353                                                 const char *soundpath)
354 {
355     ICO_TRA("CicoNotificationPanelComponent::SetNotification Enter"
356             "(text=%s icon=%s sound=%s)", text, iconpath, soundpath);
357
358     /*  set image file  */
359     if (iconpath != NULL) {
360         Evas_Object* obj = NULL;
361         obj = (Evas_Object*)edje_object_part_object_get(
362                      evasobj_, "noti_image");
363         if ( obj != NULL ) {
364             ICO_DBG("SetNotification image set[%s]",iconpath);
365             evas_object_image_file_set(obj, iconpath, NULL);
366         }
367         else {
368             ICO_DBG("SetNotification image set error[object not found]" );
369         }
370     }
371
372     /*  set text  */
373     if (text != NULL) {
374         SetText(text);
375     }
376
377     Show();
378
379     ICO_TRA("CicoNotificationPanelComponent::SetNotification Leave");
380 }
381
382 //--------------------------------------------------------------------------
383 /**
384  *  @brief  set text to Content_text
385  *  @param[in]  text
386  *  @return     true: success
387  */
388 //--------------------------------------------------------------------------
389 bool
390 CicoNotificationPanelComponent::SetText(const char *text)
391 {
392     ICO_TRA("CicoNotificationPanelComponent::SetText Enter(text=%s)",
393             text? text:"NULL");
394     if ( text == NULL ) {
395         edje_object_part_text_set(evasobj_, "content_text", "");
396         return true;
397     }
398     edje_object_part_text_set(evasobj_, "content_text", text);
399
400     ICO_TRA("CicoNotificationPanelComponent::SetText Leave");
401     return true;
402 }
403
404 //--------------------------------------------------------------------------
405 /**
406  *  @brief  set text to Content_text
407  *  @param[in]  text
408  *  @return     true: success
409  */
410 //--------------------------------------------------------------------------
411 void
412 CicoNotificationPanelComponent::SetTextEndPosition( Evas_Coord x_end, Evas_Coord y_end)
413 {
414     ICO_TRA("CicoNotificationPanelComponent::SetTextEndPosition Enter(%d,%d)",
415             x_end,y_end);
416
417     if (evasobj_ == NULL) {
418         ICO_TRA("CicoNotificationPanelComponent::SetTextEndPosition Leave(false)");
419         return;
420     }
421
422     Evas_Object* obj = NULL;
423     obj = (Evas_Object*)edje_object_part_object_get(
424                  evasobj_, "content_text" );
425     if ( obj == NULL ) {
426         ICO_TRA("CicoNotificationPanelComponent::SetTextEndPosition Leave(false)");
427         return;
428     }
429
430     Evas_Coord x,y,w,h;
431     evas_object_geometry_get( obj, &x, &y, &w, &h );
432     evas_object_resize( evasobj_, x_end, h );
433     ICO_TRA("CicoNotificationPanelComponent::text width %d -> %d",w, x_end - x -1);
434     evas_object_resize( obj, x_end - x -1, h );
435     edje_object_calc_force( obj );
436
437     ICO_TRA("CicoNotificationPanelComponent::SetTextEndPosition Leave" );
438 }
439
440 // vim: set expandtab ts=4 sw=4: