add utils for creating widget and launch app
[profile/tv/apps/native/air_home.git] / src / utils.c
1 /*
2  * Copyright (c) 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 #include <Elementary.h>
18 #include <app_debug.h>
19 #include <stdbool.h>
20 #include <app.h>
21
22 #include "utils.h"
23 #include "defs.h"
24
25 Evas_Object *utils_add_layout(Evas_Object *base, const char *group,
26                 bool focus_allow, const char *part)
27 {
28         Evas_Object *ly;
29
30         if (!base || !group) {
31                 _ERR("Invalid argument");
32                 return NULL;
33         }
34
35         ly = elm_layout_add(base);
36         if (!ly) {
37                 _ERR("failed to add layout");
38                 return false;
39         }
40         elm_layout_file_set(ly, EDJEFILE, group);
41
42         if (focus_allow)
43                 elm_object_focus_allow_set(ly, EINA_TRUE);
44         if (part)
45                 elm_object_part_content_set(base, part, ly);
46
47         evas_object_show(ly);
48
49         return ly;
50 }
51
52 Evas_Object *utils_add_icon(Evas_Object *base, const char *file,
53                 const char *part)
54 {
55         Evas_Object *ic;
56
57         if (!base || !file) {
58                 _ERR("Invalid argument");
59                 return NULL;
60         }
61
62         ic = elm_icon_add(base);
63         if (!ic) {
64                 _ERR("failed to add icon");
65                 return NULL;
66         }
67
68         elm_image_file_set(ic, file, NULL);
69
70         if (part)
71                 elm_object_part_content_set(base, part, ic);
72
73         evas_object_show(ic);
74
75         return ic;
76 }
77
78 Evas_Object *utils_add_label(Evas_Object *base, char *text,
79                 const char *style, const char *part)
80 {
81         Evas_Object *lbl;
82         const char *s;
83
84         if (!base || !text) {
85                 _ERR("Invalid argument");
86                 return NULL;
87         }
88
89         lbl = elm_label_add(base);
90         if (!lbl) {
91                 _ERR("failed to add label");
92                 return NULL;
93         }
94
95         if (style)
96                 elm_object_style_set(lbl, style);
97
98         s = edje_object_data_get(elm_layout_edje_get(base), TITLE_WIDTH);
99         if (s)
100                 elm_label_wrap_width_set(lbl, atoi(s));
101
102         elm_object_text_set(lbl, text);
103
104         if (part)
105                 elm_object_part_content_set(base, part, lbl);
106
107         evas_object_show(lbl);
108
109         return lbl;
110 }
111
112 Evas_Object *utils_add_bg(Evas_Object *base, int r, int g, int b, int a,
113                 const char *part)
114 {
115         Evas_Object *bg;
116
117         if (!base) {
118                 _ERR("Invalid argument");
119                 return NULL;
120         }
121
122         bg = evas_object_rectangle_add(evas_object_evas_get(base));
123         if (!bg) {
124                 _ERR("failed to add label");
125                 return NULL;
126         }
127
128         evas_object_color_set(bg, r, g, b, a);
129
130         if (part)
131                 elm_object_part_content_set(base, part, bg);
132
133         evas_object_show(bg);
134
135         return bg;
136 }
137
138 Evas_Object *utils_add_scroller(Evas_Object *base)
139 {
140         Evas_Object *scr;
141
142         scr = elm_scroller_add(base);
143         if (!scr) {
144                 _ERR("failed to add scroller");
145                 return NULL;
146         }
147
148         elm_scroller_policy_set(scr, ELM_SCROLLER_POLICY_OFF,
149                         ELM_SCROLLER_POLICY_OFF);
150         evas_object_show(scr);
151
152         return scr;
153 }
154
155 Evas_Object *utils_add_box(Evas_Object *base, bool horizon)
156 {
157         Evas_Object *box;
158
159         box = elm_box_add(base);
160         if (!box) {
161                 _ERR("failed to add box");
162                 return NULL;
163         }
164
165         if (horizon)
166                 elm_box_horizontal_set(box, EINA_TRUE);
167
168         evas_object_show(box);
169
170         return box;
171 }
172
173 bool utils_launch_app(const char *pkg)
174 {
175         app_control_h app_control;
176         int r;
177
178         if (!pkg) {
179                 _ERR("Invalid argument");
180                 return false;
181         }
182
183         app_control_create(&app_control);
184         app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
185         app_control_set_app_id(app_control, pkg);
186
187         r = app_control_send_launch_request(app_control, NULL, NULL);
188         if (r != APP_CONTROL_ERROR_NONE) {
189                 _ERR("failed to launch pkg");
190                 app_control_destroy(app_control);
191                 return false;
192         }
193
194         app_control_destroy(app_control);
195
196         return true;
197 }