2510f0be4553d10fae211c45ce3b0683799d896a
[platform/core/uifw/inputdelegator.git] / src / MoreOption.cpp
1 /*
2  * Copyright (c) 2016 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 "Debug.h"
18
19 #include <string>
20 #include <assert.h>
21 #include <iostream>
22 #include <stdexcept>
23
24 #include <app.h>
25 #include <app_common.h>
26
27 #include "MoreOption.h"
28 #include "w-input-selector.h"
29 #include "w-input-stt-ise.h"
30 #include "w-input-stt-voice.h"
31
32 using namespace std;
33
34 extern Evas_Object *g_setting_window;
35
36 MoreOption::MoreOption(Evas_Object *naviframe, void* voicedata)
37         : nf(naviframe)
38         , more_option_layout(NULL)
39 #ifdef _WEARABLE
40         , item(NULL)
41 #endif
42         , option_opened(EINA_FALSE)
43         , voicedata(voicedata) {
44         /** todo. implement constructor */
45 }
46
47 MoreOption::~MoreOption() {
48         /** todo. implement destructor */
49
50 //      if(more_option_layout)
51 //              evas_object_del(more_option_layout);
52 }
53
54 void MoreOption::Create() {
55         try {
56                 AddLayout();
57                 AddMorePage();
58         }
59         catch(std::exception &e) {
60                 PRINTFUNC(DLOG_ERROR, "%s", e.what());
61                 assert(0);
62         }
63 }
64
65 void MoreOption::AddLayout() {
66         /** validation */
67         if(!nf)
68                 PRINTFUNC(DLOG_ERROR, "Invalid naviframe.");
69
70         /** add layout */
71         more_option_layout = eext_more_option_add(nf);
72
73         if(!more_option_layout)
74                 PRINTFUNC(DLOG_ERROR, "It's failed to create layout");
75
76         evas_object_smart_callback_add(more_option_layout, "more,option,opened",
77                 [](void *data, Evas_Object *obj, void *event_info){
78                         PRINTFUNC(DLOG_DEBUG, "more option is opened!!! \n");
79
80                         if(!data) return;
81
82                         /**
83                         * if more option is completed, stt have to be stopped.
84                         *
85                         */
86                         MoreOption *opt = (MoreOption *)data;
87                         VoiceData *vd = (VoiceData *) opt->voicedata;
88                         try {
89                                    if (vd->sttmanager->GetCurrent() == STT_STATE_RECORDING ||
90                                                    vd->sttmanager->GetCurrent() == STT_STATE_PROCESSING) {
91                                                    vd->sttmanager->Cancel();
92                                   }
93                         }
94                         catch(is::stt::SttException &e) {
95                                 PRINTFUNC(DLOG_ERROR, "%s", e.what());
96                         }
97
98                         opt->option_opened = EINA_TRUE;
99         }, this);
100
101         evas_object_smart_callback_add(more_option_layout, "more,option,closed",
102                 [](void *data, Evas_Object *obj, void *event_info){
103                 PRINTFUNC(DLOG_DEBUG, "more option is closed!!! \n");
104
105                 MoreOption *opt = (MoreOption *)data;
106                 VoiceData *vd = (VoiceData *) opt->voicedata;
107
108                 activate_circle_scroller_for_stt_textbox(vd, EINA_TRUE);
109
110                 opt->option_opened = EINA_FALSE;
111         }, this);
112
113         evas_object_show(more_option_layout);
114 }
115
116 void MoreOption::SetContentLayout(Evas_Object *content) {
117         /**
118          * Set content layout
119          *
120          */
121         elm_object_part_content_set(more_option_layout, "elm.swallow.content", content);
122
123         Elm_Object_Item *nit = NULL;
124         nit = elm_naviframe_item_push(nf, NULL, NULL, NULL, more_option_layout, "empty");
125         elm_naviframe_item_title_enabled_set(nit, EINA_FALSE, EINA_FALSE);
126
127         elm_naviframe_item_pop_cb_set(nit,
128                 [](void *data, Elm_Object_Item *it)->Eina_Bool
129                 {
130                         MoreOption *opt = (MoreOption *)data;
131                         VoiceData *vd = (VoiceData *) opt->voicedata;
132
133                         if(vd->disclaimer  == 1){
134                                 PRINTFUNC(DLOG_ERROR, "pop to top"); //inb case of (selector view -> disclaimer view-> stt view)
135
136                                 if(opt->option_opened == EINA_FALSE){
137                                         PRINTFUNC(DLOG_DEBUG, "pop_cb called in STT view\n");
138
139                                         if(vd->sttmanager){
140                                                 if (vd->sttmanager->GetCurrent() == STT_STATE_CREATED ||
141                                                                    vd->sttmanager->GetCurrent() == STT_STATE_READY) {
142                                                         PRINTFUNC(DLOG_DEBUG, "STT_STATE_CREATED || STT_STATE_READY\n");
143
144                                                 } else if (vd->sttmanager->GetCurrent() == STT_STATE_RECORDING) {
145                                                         PRINTFUNC(DLOG_DEBUG, "STT_STATE_RECORDING\n");
146                                                         vd->effector->Stop();
147                                                         try{
148                                                                 vd->sttmanager->Stop();
149                                                         }catch(is::stt::SttException &e){
150                                                                 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
151                                                         }
152
153                                                 } else if (vd->sttmanager->GetCurrent() == STT_STATE_PROCESSING) {
154                                                         PRINTFUNC(DLOG_DEBUG, "STT_STATE_RECORDING\n");
155                                                         try{
156                                                                 vd->sttmanager->Cancel();
157                                                         }catch(is::stt::SttException &e){
158                                                                 PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
159                                                         }
160                                                 }
161                                         }
162                                 }
163
164
165                                 elm_naviframe_item_pop_to(elm_naviframe_bottom_item_get(vd->naviframe));
166                                 vd->disclaimer  = 0;
167
168                                 powerUnlock();
169                                 _back_to_genlist_for_selector();
170
171                                 return EINA_TRUE;
172                         }
173
174                         if(opt->option_opened == EINA_FALSE){
175                                 PRINTFUNC(DLOG_DEBUG, "pop_cb called in STT view\n");
176
177                                 if(vd->sttmanager){
178                                         if (vd->sttmanager->GetCurrent() == STT_STATE_CREATED ||
179                                                            vd->sttmanager->GetCurrent() == STT_STATE_READY) {
180                                                 PRINTFUNC(DLOG_DEBUG, "STT_STATE_CREATED || STT_STATE_READY\n");
181
182                                         } else if (vd->sttmanager->GetCurrent() == STT_STATE_RECORDING) {
183                                                 PRINTFUNC(DLOG_DEBUG, "STT_STATE_RECORDING\n");
184                                                 vd->effector->Stop();
185                                                 try{
186                                                         vd->sttmanager->Stop();
187                                                 }catch(is::stt::SttException &e){
188                                                         PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
189                                                 }
190
191                                         } else if (vd->sttmanager->GetCurrent() == STT_STATE_PROCESSING) {
192                                                 PRINTFUNC(DLOG_DEBUG, "STT_STATE_RECORDING\n");
193                                                 try{
194                                                         vd->sttmanager->Cancel();
195                                                 }catch(is::stt::SttException &e){
196                                                         PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
197                                                 }
198                                         }
199
200                                         _back_to_genlist_for_selector();
201
202                                         destroy_voice();
203
204                                         return EINA_TRUE;
205                                 } else {
206                                         PRINTFUNC(DLOG_DEBUG, "naviframe transition, not finished\n");
207                                         return EINA_FALSE;
208                                 }
209                         }
210                         return EINA_FALSE;
211                 }, this);
212 }
213
214 void MoreOption::Update()
215 {
216         char lang[6] = {0, };
217         strncpy(lang, ((VoiceData *)voicedata)->kbd_lang , 5);
218
219         const char* display_lang = get_lang_label(lang);
220         eext_more_option_item_part_text_set(item, "selector,sub_text", display_lang);
221 }
222
223 Evas_Object* MoreOption::AddLanguageIcon(Evas_Object *parent) {
224         Evas_Object *icon = elm_image_add(parent);
225         if (!icon) {
226                 PRINTFUNC(DLOG_ERROR, "It's failed to add image.");
227         }
228
229         string res_path = get_resource_path();
230         if (_WEARABLE)
231                 res_path = res_path + "wearable/";
232         else
233                 res_path = res_path + "mobile/";
234
235         std::string image_path = res_path + "images/prompt_ic_languages.png";
236
237         elm_image_file_set(icon, image_path.c_str(), NULL);
238         evas_object_size_hint_weight_set(icon, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
239         evas_object_size_hint_align_set(icon, EVAS_HINT_FILL, EVAS_HINT_FILL);
240         evas_object_show(icon);
241
242         return icon;
243 }
244
245 void MoreOption::AddMorePage() {
246         Evas_Object *img;
247
248         item  = eext_more_option_item_append(more_option_layout);
249
250         /* Slider content */
251         eext_more_option_item_part_text_set(item, "selector,main_text", _("WDS_IME_HEADER_INPUT_LANGUAGES_ABB"));
252
253         img = AddLanguageIcon(more_option_layout);
254
255         eext_more_option_item_part_content_set(item, "item,icon", img);
256
257         evas_object_smart_callback_add(more_option_layout, "item,clicked",
258         [](void *data, Evas_Object *obj, void *event_info)
259         {
260                 PRINTFUNC(DLOG_DEBUG, "item,clicked");
261                 if(g_setting_window == NULL)
262                         create_setting_window(obj);
263         }, NULL);
264
265         evas_object_smart_callback_add(more_option_layout, "item,selected",
266         [](void *data, Evas_Object *obj, void *event_info)
267         {
268                 PRINTFUNC(DLOG_DEBUG, "item,selected");
269         }, NULL);
270 }
271