used boolean defines from stdbool.h
[apps/core/preloaded/indicator-win.git] / src / util.c
1 /*
2  *  Indicator
3  *
4  * Copyright (c) 2000 - 2015 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
21
22 #include <vconf.h>
23 #include <app.h>
24 #include <app_common.h>
25
26 #include "common.h"
27 #include "indicator.h"
28 #include "main.h"
29 #include "indicator_gui.h"
30 #include "util.h"
31 #include "icon.h"
32 #include "box.h"
33 #include "log.h"
34
35 #define APP_CONTROL_OPERATION_POPUP_SEARCH "http://samsung.com/appcontrol/operation/search"
36
37 #define DEFAULT_DIR     ICONDIR
38 #define DIR_PREFIX      "Theme_%02d_"
39 #define LABEL_STRING    "<color=#%02x%02x%02x%02x>%s</color>"
40
41
42 char *util_set_label_text_color(const char *txt)
43 {
44         Eina_Strbuf *temp_buf = NULL;
45         Eina_Bool buf_result = EINA_FALSE;
46         char *ret_str = NULL;
47
48         retif(txt == NULL, NULL, "Invalid parameter!");
49
50         temp_buf = eina_strbuf_new();
51         buf_result = eina_strbuf_append_printf(temp_buf,
52                                 LABEL_STRING, FONT_COLOR, txt);
53
54         if (buf_result == EINA_FALSE)
55                 DBG("Failed to make label string!");
56         else
57                 ret_str = eina_strbuf_string_steal(temp_buf);
58
59         eina_strbuf_free(temp_buf);
60         return ret_str;
61 }
62
63 const char *util_get_icon_dir(void)
64 {
65         return util_get_res_file_path(DEFAULT_DIR);
66 }
67
68
69
70 void util_signal_emit(void* data, const char *emission, const char *source)
71 {
72         struct appdata *ad = NULL;
73         Evas_Object *edje = NULL;
74
75         ret_if(!data);
76
77         ad = (struct appdata *)data;
78
79         char *filter1 = "indicator.connection.updown";
80         char *filter2 = "indicator.wifi.updown";
81         if (strncmp(filter1, emission, strlen(filter1)) != 0
82                         && strncmp(filter2, emission, strlen(filter2)) != 0) {
83                 SECURE_DBG("emission %s",emission);
84         }
85
86         edje = elm_layout_edje_get(ad->win.layout);
87         ret_if(!edje);
88         edje_object_signal_emit(edje, emission, source);
89 }
90
91
92
93 void util_part_text_emit(void* data, const char *part, const char *text)
94 {
95         struct appdata *ad = (struct appdata *)data;
96         retif(data == NULL, , "Invalid parameter!");
97         Evas_Object *edje;
98
99         retif(ad->win.layout == NULL, , "Invalid parameter!");
100         edje = elm_layout_edje_get(ad->win.layout);
101         edje_object_part_text_set(edje, part, text);
102 }
103
104
105
106 void util_signal_emit_by_win(void* data, const char *emission, const char *source)
107 {
108         win_info *win = NULL;
109         Evas_Object *edje = NULL;
110
111         ret_if(!data);
112
113         win = (win_info*)data;
114         ret_if(!win->layout);
115
116         _D("emission %s", emission);
117
118         edje = elm_layout_edje_get(win->layout);
119         edje_object_signal_emit(edje, emission, source);
120 }
121
122
123
124 void util_part_text_emit_by_win(void* data, const char *part, const char *text)
125 {
126         win_info *win = (win_info*)data;
127         retif(data == NULL, , "Invalid parameter!");
128         Evas_Object *edje;
129
130         retif(win->layout == NULL, , "Invalid parameter!");
131         edje = elm_layout_edje_get(win->layout);
132         edje_object_part_text_set(edje, part, text);
133 }
134
135
136
137 void util_battery_percentage_part_content_set(void* data, const char *part, const char *img_path)
138 {
139         struct appdata *ad = (struct appdata *)data;
140         retif(data == NULL, , "Invalid parameter!");
141         Evas_Object *icon = NULL;
142         char path[PATH_MAX];
143
144         retif(ad->win.layout == NULL, , "Invalid parameter!");
145
146         icon = elm_image_add(ad->win.layout);
147         retif(!icon, , "Cannot create elm icon object!");
148
149         if (strncmp(img_path, "/", 1) != 0)
150         {
151                 snprintf(path, sizeof(path), "%s/%s", util_get_icon_dir(), img_path);
152         }
153         else
154         {
155                 strncpy(path, img_path, sizeof(path)-1);
156         }
157
158         if (!ecore_file_exists(path))
159         {
160                 ERR("icon file does not exist!!: %s",path);
161                 return;
162         }
163         elm_image_file_set(icon, path, NULL);
164         evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
165         elm_object_part_content_set(ad->win.layout, part, icon);
166 }
167
168
169
170 void util_send_status_message_start(void* data,double duration)
171 {
172         Ecore_Evas *ee_port;
173         win_info* win = (win_info*)data;
174         retif(data == NULL, , "Invalid parameter!");
175         struct appdata *ad = win->data;
176         Indicator_Data_Animation msg = {0,};
177
178         msg.xwin = ad->active_indi_win;
179         msg.duration = duration;
180
181         DBG("status start %x, %f",ad->active_indi_win,duration);
182         ee_port = ecore_evas_ecore_evas_get(evas_object_evas_get(win->win));
183         ecore_evas_msg_send(ee_port, MSG_DOMAIN_CONTROL_INDICATOR, MSG_ID_INDICATOR_ANI_START, &(msg), sizeof(Indicator_Data_Animation));
184
185 }
186
187
188
189 void util_launch_search(void* data)
190 {
191         int lock_state = VCONFKEY_IDLE_UNLOCK;
192         int lock_ret = -1;
193         app_control_h service;
194         int ret = APP_CONTROL_ERROR_NONE;
195
196         lock_ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
197
198         /* In Lock Screen, home button don't have to do */
199         if (lock_ret == 0 && lock_state == VCONFKEY_IDLE_LOCK) {
200                 return;
201         }
202
203         if (util_check_system_status() == FAIL) {
204                 DBG("util_check_system_status failed");
205                 return;
206         }
207
208         app_control_create(&service);
209         app_control_set_operation(service, APP_CONTROL_OPERATION_MAIN);
210         app_control_set_app_id(service, SEARCH_PKG_NAME);
211
212         ret = app_control_send_launch_request(service, NULL, NULL);
213
214         if(ret != APP_CONTROL_ERROR_NONE) {
215                 ERR("Cannot launch app");
216         }
217
218         app_control_destroy(service);
219 }
220
221
222
223 int util_check_system_status(void)
224 {
225         int ret, value = -1;
226
227         ret = vconf_get_int(VCONFKEY_PWLOCK_STATE, &value);
228         if (ret == OK &&
229             (value == VCONFKEY_PWLOCK_BOOTING_LOCK ||
230              value == VCONFKEY_PWLOCK_RUNNING_LOCK))
231                 return FAIL;
232
233         return OK;
234 }
235
236
237
238 #ifdef _SUPPORT_SCREEN_READER
239 Evas_Object *util_access_object_register(Evas_Object *object, Evas_Object *layout)
240 {
241         if ((object == NULL) || (layout == NULL)) {
242                 ERR("Access object doesn't exist!!! %x %x",object,layout);
243                 return NULL;
244         }
245
246         return elm_access_object_register(object, layout);
247 }
248
249
250
251 void util_access_object_unregister(Evas_Object *object)
252 {
253         if (object == NULL) {
254                 ERR("Access object doesn't exist!!! %x",object);
255                 return NULL;
256         }
257
258         elm_access_object_unregister(object);
259 }
260
261
262
263 void util_access_object_info_set(Evas_Object *object, int info_type, char *info_text)
264 {
265         if ((object == NULL) || (info_text == NULL)) {
266                 ERR("Access info set fails %x, %x!!!",object,info_text);
267                 return;
268         }
269
270         elm_access_info_set(object, info_type, (const char *)info_text);
271 }
272
273
274
275 void util_access_object_activate_cb_set(Evas_Object *object, Elm_Access_Activate_Cb activate_cb, void *cb_data)
276 {
277         if ((object == NULL) || (activate_cb == NULL)) {
278                 ERR("Access activated cb set fails %x %x!!!",object,activate_cb);
279                 return;
280         }
281
282         elm_access_activate_cb_set(object, activate_cb, cb_data);
283 }
284
285
286
287 void util_access_object_info_cb_set(Evas_Object *object, int type, Elm_Access_Info_Cb info_cb, void *cb_data)
288 {
289         if ((object == NULL) || (info_cb == NULL)) {
290                 ERR("Access info cb set fails  %x %x!!!",object,info_cb);
291                 return;
292         }
293
294         elm_access_info_cb_set(object, type, info_cb, cb_data);
295 }
296
297
298
299 void util_icon_access_register(icon_s *icon)
300 {
301
302         if(icon == NULL)
303         {
304                 ERR("ICON NULL");
305                 return;
306         }
307
308         if(icon->tts_enable == EINA_TRUE && icon->ao==NULL)
309         {
310                 Evas_Object *to = NULL;
311
312                 to = (Evas_Object *) edje_object_part_object_get(elm_layout_edje_get(icon->img_obj.obj), "elm.rect.icon.access");
313                 icon->ao = util_access_object_register(to, icon->img_obj.obj);
314
315                 if(icon->access_cb!=NULL)
316                 {
317                         util_access_object_info_cb_set(icon->ao,ELM_ACCESS_INFO,icon->access_cb,icon->ad);
318                 }
319         }
320 }
321
322
323
324 void util_icon_access_unregister(icon_s *icon)
325 {
326         if(icon == NULL)
327         {
328                 ERR("ICON NULL");
329                 return;
330         }
331
332         if(icon->tts_enable == EINA_TRUE&&icon->ao!=NULL)
333         {
334                 util_access_object_unregister(icon->ao);
335                 icon->ao = NULL;
336         }
337 }
338 #endif /* _SUPPORT_SCREEN_READER */
339
340
341
342 static char* _get_timezone_from_vconf(void)
343 {
344         char *szTimezone = NULL;
345         szTimezone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID);
346         if(szTimezone == NULL)
347         {
348                 ERR("Cannot get time zone.");
349                 return strdup("N/A");
350         }
351
352         return szTimezone;
353 }
354
355
356
357 char* util_get_timezone_str(void)
358 {
359         enum { BUFFERSIZE = 1024 };
360         char buf[BUFFERSIZE];
361         ssize_t len = readlink("/opt/etc/localtime", buf, sizeof(buf)-1);
362
363         if (len != -1)
364         {
365                 buf[len] = '\0';
366         }
367         else
368         {
369                 ERR("NO TIMEZONEINFO");
370                 return _get_timezone_from_vconf();
371         }
372         return (char*)strdup(buf+20);   // Asia/Seoul
373 }
374
375
376
377 Eina_Bool util_win_prop_angle_get(Ecore_X_Window win, int *req)
378 {
379         Eina_Bool res = EINA_FALSE;
380 #if 0
381         int ret, count;
382         int angle[2] = {-1, -1};
383         unsigned char* prop_data = NULL;
384         ret = ecore_x_window_prop_property_get(win, ECORE_X_ATOM_E_ILLUME_ROTATE_WINDOW_ANGLE, ECORE_X_ATOM_CARDINAL, 32, &prop_data, &count);
385         if (ret <= 0) {
386                 if (prop_data) free(prop_data);
387                 return res;
388         }
389
390         if (ret && prop_data) {
391                 memcpy (&angle, prop_data, sizeof (int)*count);
392                 if (count == 2) res = EINA_TRUE;
393         }
394
395         if (prop_data) free(prop_data);
396         *req  = angle[0]; //current angle
397
398         if (angle[0] == -1 && angle[1] == -1) res = EINA_FALSE;
399 #endif
400
401         return res;
402 }
403
404
405 #if 0
406 int util_get_block_width(void* data, const char* part)
407 {
408         Evas_Object * eo = NULL;
409         int geo_dx = 0;
410         int geo_dy = 0;
411         retif(data == NULL,-1, "Invalid parameter!");
412         retif(part == NULL,-1, "Invalid parameter!");
413
414         win_info* win = (win_info*)data;
415
416         eo = (Evas_Object *) edje_object_part_object_get(elm_layout_edje_get(win->layout), part);
417
418         evas_object_geometry_get(eo, NULL, NULL, &geo_dx, &geo_dy);
419
420         return geo_dx;
421 }
422
423
424
425 int util_get_string_width(void* data, const char* part)
426 {
427         Evas_Object * eo = NULL;
428         int text_dx = 0;
429         int text_dy = 0;
430         retif(data == NULL,-1, "Invalid parameter!");
431         retif(part == NULL,-1, "Invalid parameter!");
432
433         win_info* win = (win_info*)data;
434
435         eo = (Evas_Object *) edje_object_part_object_get(elm_layout_edje_get(win->layout), part);
436
437         evas_object_textblock_size_formatted_get(eo, &text_dx, &text_dy);
438
439         return text_dx;
440 }
441 #endif
442
443
444 int util_is_orf(void)
445 {
446         return 1;
447 }
448
449
450
451 int util_check_noti_ani(const char* path)
452 {
453         retv_if(!path, 0);
454         if (!strcmp(path,"reserved://indicator/ani/downloading")
455                 || !strcmp(path,"reserved://indicator/ani/uploading")) {
456                 return 1;
457         }
458         return 0;
459 }
460
461
462
463 void util_start_noti_ani(void* data)
464 {
465         icon_s *icon = (icon_s *)data;
466         retif(data == NULL, , "Invalid parameter!");
467
468         if(util_check_noti_ani(icon->img_obj.data))
469         {
470                 DBG("%s",icon->name);
471                 if(!strcmp(icon->img_obj.data,"reserved://indicator/ani/downloading"))
472                 {
473                         icon_ani_set(icon,ICON_ANI_DOWNLOADING);
474                 }
475                 else
476                 {
477                         icon_ani_set(icon,ICON_ANI_UPLOADING);
478                 }
479         }
480 }
481
482
483
484 void util_stop_noti_ani(void* data)
485 {
486         icon_s *icon = (icon_s *)data;
487         retif(data == NULL, , "Invalid parameter!");
488
489         if(util_check_noti_ani(icon->img_obj.data))
490         {
491                 Evas_Object *img_edje;
492                 img_edje = elm_layout_edje_get(icon->img_obj.obj);
493                 DBG("%s",icon->name);
494                 if(!strcmp(icon->img_obj.data,"reserved://indicator/ani/downloading"))
495                 {
496                         edje_object_signal_emit(img_edje, "indicator.ani.downloading.stop","elm.swallow.icon");
497                 }
498                 else
499                 {
500                         edje_object_signal_emit(img_edje, "indicator.ani.uploading.stop","elm.swallow.icon");
501                 }
502         }
503 }
504
505
506
507 void util_char_replace(char *text, char s, char t)
508 {
509         retif(text == NULL, , "invalid argument");
510
511         int i = 0, text_len = 0;
512
513         text_len = strlen(text);
514
515         for (i = 0; i < text_len; i++) {
516                 if (*(text + i) == s) {
517                         *(text + i) = t;
518                 }
519         }
520 }
521
522
523
524 static bool _is_empty_str(const char *str)
525 {
526         if (NULL == str || '\0' == str[0])
527                 return true;
528         return false;
529 }
530
531
532
533 char *util_safe_str(const char *str, const char *strSearch)
534 {
535         if (_is_empty_str(str))
536                 return NULL;
537
538         return strstr(str, strSearch);
539 }
540
541
542
543 int util_dynamic_state_get(void)
544 {
545         int val = 0;
546         //vconf_get_bool(VCONFKEY_SETAPPL_DYNAMIC_STATUS_BAR, &val);
547         return val;
548 }
549
550
551
552 Ecore_File_Monitor* util_file_monitor_add(const char* file_path, Ecore_File_Monitor_Cb callback_func, void *ad)
553 {
554         SECURE_DBG("File path : %s", file_path);
555         Ecore_File_Monitor* pFileMonitor = NULL;
556         pFileMonitor = ecore_file_monitor_add(file_path, callback_func, ad);
557         if(pFileMonitor == NULL) SECURE_DBG("ecore_file_monitor_add return NULL !!");
558
559         return pFileMonitor;
560 }
561
562
563
564 void util_file_monitor_remove(Ecore_File_Monitor* pFileMonitor)
565 {
566         if(pFileMonitor == NULL) return;
567
568         ecore_file_monitor_del(pFileMonitor);
569         pFileMonitor = NULL;
570 }
571
572 const char *util_get_file_path(enum app_subdir dir, const char *relative)
573 {
574         static char buf[PATH_MAX];
575         char *prefix;
576
577         switch (dir) {
578         case APP_DIR_DATA:
579                 prefix = app_get_data_path();
580                 break;
581         case APP_DIR_CACHE:
582                 prefix = app_get_cache_path();
583                 break;
584         case APP_DIR_RESOURCE:
585                 prefix = app_get_resource_path();
586                 break;
587         case APP_DIR_SHARED_DATA:
588                 prefix = app_get_shared_data_path();
589                 break;
590         case APP_DIR_SHARED_RESOURCE:
591                 prefix = app_get_shared_resource_path();
592                 break;
593         case APP_DIR_SHARED_TRUSTED:
594                 prefix = app_get_shared_trusted_path();
595                 break;
596         case APP_DIR_EXTERNAL_DATA:
597                 prefix = app_get_external_data_path();
598                 break;
599         case APP_DIR_EXTERNAL_CACHE:
600                 prefix = app_get_external_cache_path();
601                 break;
602         case APP_DIR_EXTERNAL_SHARED_DATA:
603                 prefix = app_get_external_shared_data_path();
604                 break;
605         default:
606                 LOGE("Not handled directory type.");
607                 return NULL;
608         }
609         size_t res = eina_file_path_join(buf, sizeof(buf), prefix, relative);
610         free(prefix);
611         if (res > sizeof(buf)) {
612                 LOGE("Path exceeded PATH_MAX");
613                 return NULL;
614         }
615
616         return &buf[0];
617 }
618 /* End of file */