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