[Tizen 3.0] 2.4 source code merge
[apps/core/preloaded/quickpanel.git] / daemon / voice_control.c
1 /*
2  * Copyright (c) 2009-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
18
19 #include <Elementary.h>
20
21 #include <voice_control_setting.h>
22 #include <app_control_internal.h>
23 #include <tzsh.h>
24 #include <tzsh_quickpanel_service.h>
25 #include <E_DBus.h>
26
27 #include "common.h"
28 #include "common_uic.h"
29 #include "voice_control.h"
30 #include "quickpanel-ui.h"
31 #include "list_util.h"
32
33
34 static int _init(void *data);
35 static int _fini(void *data);
36 Evas_Object* _voice_control_view_create(void *data);
37 static void _voice_control_register_event(void *data);
38 static bool _check_voice_control_enabled(void);
39
40 QP_Module voice_control = {
41         .name = "voice_control",
42         .init = _init,
43         .fini = _fini,
44         .suspend = NULL,
45         .resume = NULL,
46         .hib_enter = NULL,
47         .hib_leave = NULL,
48         .lang_changed = NULL,
49         .refresh = NULL,
50         .get_height = NULL,
51         .qp_opened = NULL,
52         .qp_closed = NULL,
53 };
54
55 static Evas_Object *g_layout = NULL;
56
57 static void _voice_control_view_destroy(void *data)
58 {
59         DBG("_voice_control_view_destroy");
60         struct appdata *ad = data;
61
62         if (ad == NULL)
63         {
64                 ERR("invalid data");
65                 return;
66         }
67
68         if (g_layout == NULL)
69         {
70                 ERR("g_layout is not exist");
71                 return;
72         }
73
74         quickpanel_list_util_item_unpack_by_object(ad->list, g_layout, 0, 0);
75         quickpanel_list_util_item_del_tag(g_layout);
76         elm_object_signal_emit(ad->ly, "voice_icon.hide", "quickpanel.prog");
77
78         if (g_layout != NULL) {
79                 evas_object_del(g_layout);
80                 g_layout = NULL;
81         }
82 }
83
84 static int _init(void *data)
85 {
86         struct appdata *ad = (struct appdata *)data;
87
88         if (0 != vc_setting_initialize()) {
89                 ERR("Fail to init");
90                 return QP_FAIL;
91         }
92
93         _voice_control_register_event(data);
94
95         if (_check_voice_control_enabled()) { //when module restart
96                 _voice_control_view_create(ad);
97         }
98
99         return QP_OK;
100 }
101
102 static int _fini(void *data)
103 {
104         struct appdata *ad = (struct appdata *)data;
105
106         if (0 != vc_setting_deinitialize()) {
107                 ERR("Fail to vc_setting_deinitialize");
108         }
109
110         _voice_control_view_destroy(ad);
111
112         return QP_OK;
113 }
114
115 static void _vc_enabled_changed_cb(bool enabled, void* user_data)
116 {
117         DBG("_vc_enabled_changed_cb");
118         struct appdata *ad = user_data;
119
120         if( !enabled ) { //deactivated voice controller
121                 _voice_control_view_destroy(ad);
122         } else {
123                 _voice_control_view_create(ad);
124         }
125 }
126
127 static void _voice_control_register_event(void *data)
128 {
129         DBG("_voice_control_register_event");
130         if (0 != vc_setting_set_enabled_changed_cb(_vc_enabled_changed_cb, data)) {
131                 ERR("Fail to set enabled cb");
132                 return;
133         }
134 }
135
136 static bool _check_voice_control_enabled(void)
137 {
138         bool enabled = false;
139
140         if (0 != vc_setting_get_enabled(&enabled)) {
141                 ERR("Fail to get enabled");
142         }
143         DBG("_check_voice_control_enabled enabled %d",enabled);
144
145         return enabled;
146 }
147
148 static void _button_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
149 {
150         DBG("_button_clicked_cb");
151         int ret = 0;
152         quickpanel_uic_launch_ug_by_appcontrol(VOICE_CONTOL_REF_APP, NULL);
153         quickpanel_uic_launch_app_inform_result(VOICE_CONTOL_REF_APP, ret);
154         quickpanel_uic_close_quickpanel(true, 1);
155
156 }
157
158 Evas_Object* _voice_control_view_create(void *data)
159 {
160         DBG("_voice_control_view_create");
161         struct appdata *ad = data;
162         Evas_Object *layout = NULL;
163         Eina_Bool ret = EINA_FALSE;
164
165         if (!_check_voice_control_enabled()) {
166                 ERR("voice control is not enabled.");
167                 return layout;
168         }
169         if (ad->win == NULL)
170         {
171                 ERR("invalid parent");
172                 return layout;
173         }
174
175         if (g_layout)
176         {
177                 ERR("voice control view is already created.");
178                 return g_layout;
179         }
180
181         layout = elm_layout_add(ad->win);
182         if (layout == NULL)
183         {
184                 ERR("Failed to create voice control layout");
185                 return layout;
186         }
187
188         g_layout = layout;
189
190         ret = elm_layout_file_set(layout, VOICE_CONTOL_EDJ, "quickpanel/voice_control/default");
191         if (ret == EINA_FALSE)
192         {
193                 ERR("Failed to set layout file[%d]", ret);
194                 evas_object_del(layout);
195                 return NULL;
196         }
197
198         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
199         evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
200         evas_object_show(layout);
201
202         elm_object_signal_callback_add(layout, "button_clicked" , "", _button_clicked_cb, ad);
203         elm_object_signal_emit(ad->ly, "voice_icon.show", "quickpanel.prog");
204
205         // attach to list
206         qp_item_data *qid = quickpanel_list_util_item_new(QP_ITEM_TYPE_VOICE_CONTOL, layout);
207         quickpanel_list_util_item_set_tag(layout, qid);
208         quickpanel_list_util_sort_insert(ad->list, layout);
209         quickpanel_uic_initial_resize(layout, QP_VOICE_CONTOL_HEIGHT);
210
211         return layout;
212 }
213