tizen 2.4 release
[framework/uifw/voice/voice-control-panel.git] / src / voice_control_panel_view.c
1 /*
2 *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdbool.h>
17
18 #include <app_control.h>
19 #include <dlog.h>
20 #include <Elementary.h>
21 #include <Ecore_X.h>
22
23 #include "voice_control_panel_command.h"
24 #include "voice_control_panel_main.h"
25 #include "voice_control_panel_view.h"
26
27 #define MAIN_LAYOUT_EDJ         "/usr/apps/org.tizen.voice-control-panel/res/edje/voice-control-panel.edj"
28 #define MIC_IMAGE               "/usr/apps/org.tizen.voice-control-panel/res/images/Voice_control_icon_mic.png"
29 #define MUTE_IMAGE              "/usr/apps/org.tizen.voice-control-panel/res/images/Voice_control_icon_mute.png"
30 #define SETTING_IMAGE           "/usr/apps/org.tizen.voice-control-panel/res/images/Voice_control_icon_setting.png"
31 #define ARROW_IMAGE             "/usr/apps/org.tizen.voice-control-panel/res/images/Voice_control_icon_arrow.png"
32
33 static Elm_Genlist_Item_Class *g_itc = NULL;
34 static Elm_Genlist_Item_Class *g_itc_for_app = NULL;
35 static appdata *g_ad = NULL;
36
37 static char *__help_text_get(void *data, Evas_Object *obj, const char *part)
38 {
39         if (NULL == part) {
40                 return NULL;
41         }
42
43         int idx = (int *)data;
44
45         if (!strcmp("elm.text.main.left.top", part)) {
46                 if (1 == g_ad->current_depth) {
47                         return strdup(_(g_command_1st[idx]));
48                 } else if (2 == g_ad->current_depth) {
49                         return strdup(_(g_command_2nd[g_ad->current_path[0]][idx]));
50                 }
51         } else if (!strcmp("elm.text.sub.left.bottom", part)) {
52                 if (1 == g_ad->current_depth) {
53                         return strdup(_(g_hint_1st[idx]));
54                 } else if (2 == g_ad->current_depth) {
55                         return strdup(_(g_hint_2nd[g_ad->current_path[0]][idx]));
56                 }
57         }
58         return NULL;
59 }
60
61 static char *__help_text_get_for_app(void *data, Evas_Object *obj, const char *part)
62 {
63         if (NULL == part || NULL == data) {
64                 return NULL;
65         }
66
67         char *cmd = (char *)data;
68
69         if (!strcmp("elm.text.main.left", part)) {
70                 LOGD("cmd (%s)", cmd);
71                 return strdup(cmd);
72         }
73         return NULL;
74 }
75
76 int vc_panel_view_show_help(void *data)
77 {
78         LOGD( "==== Show help ====");
79
80         appdata *ad = (appdata *)data;
81
82         if (NULL != ad->help_genlist) {
83                 elm_genlist_clear(ad->help_genlist);
84         }
85
86         int i;
87         Elm_Object_Item *it = NULL;
88         if (1 == ad->current_depth) {
89                 for (i = 0; i < NUM_COMMAND_1ST; i++) {
90                         if (NULL != g_command_1st[i]) {
91                                 it = elm_genlist_item_append(ad->help_genlist, g_itc, (void *)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
92                                 elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
93                         }
94                 }
95         } else if (2 == ad->current_depth) {
96                 for (i = 0; i < NUM_COMMAND_2ND; i++) {
97                         if (NULL != g_command_2nd[ad->current_path[0]][i]) {
98                                 it = elm_genlist_item_append(ad->help_genlist, g_itc, (void *)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
99                                 elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
100                         }
101                 }
102         }
103
104         GList *iter = NULL;
105         for (i = 0; i < g_list_length(ad->cmd_list); i++) {
106                 iter = g_list_nth(ad->cmd_list, i);
107                 if (NULL != iter) {
108                         char *cmd = iter->data;
109                         if (NULL != cmd) {
110                                 LOGD("Add list - (%s)", cmd);
111                                 char *tmp = strdup(cmd);
112                                 it = elm_genlist_item_append(ad->help_genlist, g_itc_for_app, (void *)tmp, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
113                                 elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
114                         }
115                 }
116         }
117
118         evas_object_show(ad->help_win);
119         ad->show_help = 1;
120
121         LOGD( "====");
122         LOGD( " ");
123         
124         return 0;
125 }
126
127 int vc_panel_view_hide_help(void *data)
128 {
129         LOGD( "==== Hide help ====");
130
131         appdata *ad = (appdata *)data;
132
133         evas_object_hide(ad->help_win);
134         ad->show_help = 0;
135
136         return 0;
137 }
138
139 int vc_panel_view_show_result(void *data, const char *result)
140 {
141         LOGD("==== Show result ====");
142
143         appdata *ad = (appdata *)data;
144
145         elm_box_clear(ad->content_box);
146
147         Evas_Object *label = elm_label_add(ad->content_box);
148         evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
149         evas_object_size_hint_align_set(label, 0.0, 0.0);
150         char text[256] = {'\0', };
151         snprintf(text, 256, "<font=Tizen:style=Regular><font_size=30><color=#FFFFFFFF>%s</color></font_size></font>", result);
152         elm_object_text_set(label, text);
153         elm_box_pack_end(ad->content_box, label);
154         evas_object_show(label);
155
156         return 0;
157 }
158
159 int vc_panel_view_show(void *data)
160 {
161         LOGD( "==== Show content ====");
162
163         appdata *ad = (appdata *)data;
164         LOGD( "Current Depth = %d", ad->current_depth);
165
166         Evas_Object *box = elm_box_add(ad->layout_main);
167         elm_box_padding_set(box, 0, 0);
168         elm_box_horizontal_set(box, EINA_TRUE);
169         elm_box_homogeneous_set(box, EINA_FALSE);
170         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
171         elm_box_align_set(box, 0.0, 0.0);
172         int image_size = 42 * ad->scale_h;
173
174         if (1 == ad->current_depth) {
175                 Evas_Object *label = elm_label_add(box);
176                 evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
177                 evas_object_size_hint_align_set(label, 0.0, 0.0);
178                 char text1[256] = {'\0', };
179                 snprintf(text1, 256, "<font=Tizen:style=Regular><font_size=30><color=#FFFFFFFF>%s</color></font_size></font>", _("IDS_LISTENING"));
180                 elm_object_text_set(label, text1);
181                 elm_box_pack_end(box, label);
182                 evas_object_show(label);
183         } else if (2 == ad->current_depth) {
184                 Evas_Object *label1 = elm_label_add(box);
185                 evas_object_size_hint_align_set(label1, 0.0, 0.5);
186                 char text1[256] = {'\0', };
187                 snprintf(text1, 256, "<font=Tizen:style=Regular><font_size=30><color=#FFFFFFB8>%s</color></font_size></font>", _(g_command_1st[ad->current_path[0]]));
188                 elm_object_text_set(label1, text1);
189                 elm_box_pack_end(box, label1);
190                 evas_object_show(label1);
191
192                 Evas_Object *image_arrow = elm_image_add(box);
193                 elm_image_file_set(image_arrow, ARROW_IMAGE, NULL);
194                 evas_object_size_hint_min_set(image_arrow, image_size, image_size);
195                 elm_box_pack_end(box, image_arrow);
196                 evas_object_show(image_arrow);
197
198                 Evas_Object *label2 = elm_label_add(box);
199                 evas_object_size_hint_align_set(label2, 0.0, 0.5);
200                 char text2[256] = {'\0', };
201                 snprintf(text2, 256, "<font=Tizen:style=Regular><font_size=30><color=#FFFFFFFF>%s</color><font_size></font>", _("IDS_LISTENING"));
202                 elm_object_text_set(label2, text2);
203                 elm_box_pack_end(box, label2);
204                 evas_object_show(label2);
205         }
206
207         elm_object_part_content_set(ad->layout_main, "content", box);
208         evas_object_show(box);
209         ad->content_box = box;
210
211         evas_object_show(ad->win);
212
213         return 0;
214 }
215
216 int vc_panel_view_hide(void *data)
217 {
218         appdata *ad = (appdata *)data;
219
220         evas_object_hide(ad->win);
221
222         vc_panel_view_hide_help(ad);
223
224         return 0;
225 }
226
227 static void __vc_panel_setting_clicked_cb(void *data, Evas_Object *obj, void *event_info)
228 {
229         LOGD("==== Setting Clicked ====");
230         
231         app_control_h app_control;
232         app_control_create(&app_control);
233         app_control_set_app_id(app_control, "org.tizen.voice-setting");
234         app_control_add_extra_data(app_control, "show", "voice-control");
235         app_control_send_launch_request(app_control, NULL, NULL);
236         app_control_destroy(app_control);
237
238         ui_app_exit();
239
240         LOGD("====");
241         LOGD("");
242 }
243
244 int vc_panel_view_create(void *data)
245 {
246         LOGD( "==== Create View ====");
247
248         appdata *ad = (appdata *)data;
249         g_ad = ad;
250
251         elm_theme_extension_add(NULL, MAIN_LAYOUT_EDJ);
252
253         /* Window */
254         Evas_Object *win = elm_win_add(NULL, "voice-control-panel", ELM_WIN_UTILITY);
255         int x,y,w,h;
256         elm_win_screen_size_get(win, &x, &y, &w, &h);
257         LOGD("Size x(%d), y(%d), w(%d), h(%d)", x, y, w, h);
258         elm_win_autodel_set(win, EINA_TRUE);
259         elm_win_title_set(win, "voice-control-panel");
260         elm_win_borderless_set(win, EINA_TRUE);
261         elm_win_alpha_set(win, EINA_TRUE);
262         
263         ad->scale_w = w / 720.0;
264         ad->scale_h = h / 1280.0;
265
266         int scaled_h = 90 * ad->scale_h;
267         LOGD("%d", scaled_h);
268         evas_object_resize(win, w, scaled_h);
269         evas_object_move(win, 0, h - scaled_h);
270
271         elm_win_prop_focus_skip_set(win, EINA_TRUE);
272         Ecore_X_Window_State state[3];
273         state[0] = ECORE_X_WINDOW_STATE_SKIP_TASKBAR;
274         state[1] = ECORE_X_WINDOW_STATE_SKIP_PAGER;
275         state[2] = ECORE_X_WINDOW_STATE_ABOVE;
276
277         Ecore_X_Window xwin = elm_win_xwindow_get(win);
278         ecore_x_netwm_window_state_set(xwin, state, 3);
279
280         ad->win = win;
281
282         /* Layout Main */
283         Evas_Object *layout = elm_layout_add(win);
284         elm_layout_file_set(layout, MAIN_LAYOUT_EDJ, "main");
285         elm_win_resize_object_add(win, layout);
286         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
287         evas_object_show(layout);
288         ad->layout_main = layout;
289
290         /* Image MIC */
291         Evas_Object *image_mic = elm_image_add(layout);
292         elm_image_file_set(image_mic, MIC_IMAGE, NULL);
293         elm_object_part_content_set(layout, "icon1", image_mic);
294         evas_object_show(image_mic);
295         ad->image_mic = image_mic;
296
297         /* Image arrow */
298         Evas_Object *image_arrow = elm_image_add(layout);
299         elm_image_file_set(image_arrow, ARROW_IMAGE, NULL);
300         elm_object_part_content_set(layout, "icon2", image_arrow);
301         evas_object_show(image_arrow);
302         ad->image_arrow = image_arrow;
303
304         /* Image Setting */
305         Evas_Object *image_setting = elm_image_add(layout);
306         elm_image_file_set(image_setting, SETTING_IMAGE, NULL);
307         elm_object_part_content_set(layout, "icon3", image_setting);
308         evas_object_smart_callback_add(image_setting, "clicked", __vc_panel_setting_clicked_cb, ad);
309         evas_object_show(image_setting);
310         ad->image_setting = image_setting;
311
312         ad->current_depth = 1;
313
314         Evas_Object *help_win = elm_win_add(NULL, "voice-control-panel-help", ELM_WIN_UTILITY);
315         elm_win_autodel_set(help_win, EINA_TRUE);
316         elm_win_title_set(help_win, "voice-control-panel-help");
317         elm_win_borderless_set(help_win, EINA_TRUE);
318         elm_win_alpha_set(help_win, EINA_TRUE);
319         evas_object_resize(help_win, w, h / 2);
320         evas_object_move(help_win, 0, h - (h / 2) - (90 * ad->scale_h));
321
322         elm_win_prop_focus_skip_set(help_win, EINA_TRUE);
323         Ecore_X_Window help_xwin = elm_win_xwindow_get(help_win);
324         ecore_x_netwm_window_state_set(help_xwin, state, 3);
325
326         ad->help_win = help_win;
327
328         Evas_Object *help_layout = elm_layout_add(help_win);
329         elm_layout_file_set(help_layout, "/usr/apps/org.tizen.voice-control-panel/res/edje/voice-control-panel.edj", "help");
330         elm_win_resize_object_add(help_win, help_layout);
331         evas_object_size_hint_weight_set(help_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
332         evas_object_show(help_layout);
333         ad->help_layout = help_layout;
334
335         Evas_Object *genlist = elm_genlist_add(ad->help_layout);
336         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
337         elm_genlist_homogeneous_set(genlist, EINA_FALSE);
338         elm_object_part_content_set(ad->help_layout, "content", genlist);
339         evas_object_show(genlist);
340         ad->help_genlist = genlist;
341
342         g_itc = elm_genlist_item_class_new();
343         if (NULL == g_itc) {
344                 LOGE("Fail to new item class");
345                 return -1;
346         }
347         g_itc->item_style = "2line.top";
348         g_itc->func.text_get = __help_text_get;
349         g_itc->func.content_get = NULL;
350         g_itc->func.state_get = NULL;
351         g_itc->func.del = NULL;
352
353         g_itc_for_app = elm_genlist_item_class_new();
354         if (NULL == g_itc_for_app) {
355                 LOGE("Fail to new item class");
356                 return -1;
357         }
358         g_itc_for_app->item_style = "1line";
359         g_itc_for_app->func.text_get = __help_text_get_for_app;
360         g_itc_for_app->func.content_get = NULL;
361         g_itc_for_app->func.state_get = NULL;
362         g_itc_for_app->func.del = NULL;
363
364         ad->show_help = 0;
365
366         LOGD( "====");
367         LOGD( " ");
368
369         return 0;
370 }
371
372 void vc_panel_view_destroy(void *data)
373 {
374         LOGD("");
375         if (NULL != g_itc) {
376                 elm_genlist_item_class_free(g_itc);
377                 g_itc = NULL;
378         }
379
380         if (NULL != g_itc_for_app) {
381                 elm_genlist_item_class_free(g_itc_for_app);
382                 g_itc_for_app = NULL;
383         }
384
385         return;
386 }
387
388 /*
389 vi:ts=4:ai:nowrap:expandtab
390 */