Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / partner / cpp / Sample / Tizen C++ / SpeechApp / SpeechApp / project / src / SpeechAppSttForm.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.tizenopensource.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FUiScenes.h>
19 #include <FGraphics.h>
20 #include <FLocales.h>
21 #include "SpeechAppSttForm.h"
22
23 using namespace Osp::Base;
24 using namespace Osp::Base::Collection;
25 using namespace Osp::Ui;
26 using namespace Osp::Ui::Controls;
27 using namespace Osp::Ui::Scenes;
28 using namespace Osp::Uix::Speech;
29 using namespace Osp::Graphics;
30 using namespace Osp::Locales;
31
32
33 SpeechAppSttForm::SpeechAppSttForm(void)
34     : __pStt(null)
35     , __pLabel(null)
36     , __pButtonStart(null)
37     , __pButtonStop(null)
38     , __pButtonCancel(null)
39     , __isInited(false)
40 {
41 }
42
43 SpeechAppSttForm::~SpeechAppSttForm(void)
44 {
45 }
46
47 bool
48 SpeechAppSttForm::Initialize()
49 {
50         Construct(L"IDF_FORM_STT");
51
52         return true;
53 }
54
55 result
56 SpeechAppSttForm::OnInitializing(void)
57 {
58         result r = E_SUCCESS;
59
60         // TODO: Add your initialization code here
61
62     SetFormBackEventListener(this);
63
64     __pButtonStart = static_cast<Button*>(GetControl(L"IDC_BUTTON_START"));
65     if (__pButtonStart != null)
66     {
67         __pButtonStart->SetActionId(ID_BUTTON_START);
68         __pButtonStart->AddActionEventListener(*this);
69         __pButtonStart->SetEnabled(true);
70     }
71
72     __pButtonStop = static_cast<Button*>(GetControl(L"IDC_BUTTON_STOP"));
73     if (__pButtonStop != null)
74     {
75         __pButtonStop->SetActionId(ID_BUTTON_STOP);
76         __pButtonStop->AddActionEventListener(*this);
77         __pButtonStop->SetEnabled(false);
78     }
79
80     __pButtonCancel = static_cast<Button*>(GetControl(L"IDC_BUTTON_CANCEL"));
81     if (__pButtonCancel != null)
82     {
83         __pButtonCancel->SetActionId(ID_BUTTON_CANCEL);
84         __pButtonCancel->AddActionEventListener(*this);
85         __pButtonCancel->SetEnabled(false);
86     }
87
88     __pLabel = static_cast<Label*>(GetControl(L"IDC_LABEL_INFO"));
89     if (__pLabel != null)
90     {
91         __pLabel->SetText("Information box");
92     }
93
94     __pStt = new SpeechToText();
95     if (__pStt != null)
96     {
97         r = __pStt->Construct(*this);
98         if (IsFailed(r))
99         {
100             AppLog("[%s] Construct() error", GetErrorMessage(r));
101         }
102
103         r = __pStt->Initialize();
104         if (IsFailed(r))
105         {
106             AppLog("[%s] Initialize() error", GetErrorMessage(r));
107         }
108     }
109     else
110     {
111         SceneManager* pSceneManager = SceneManager::GetInstance();
112         AppAssert(pSceneManager);
113         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
114     }
115
116         return r;
117 }
118
119 result
120 SpeechAppSttForm::OnTerminating(void)
121 {
122         result r = E_SUCCESS;
123
124         delete __pStt;
125
126         return r;
127 }
128
129 void
130 SpeechAppSttForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
131 {
132     result r = E_SUCCESS;
133
134     SceneManager* pSceneManager = SceneManager::GetInstance();
135     AppAssert(pSceneManager);
136
137     switch(actionId)
138     {
139     case ID_BUTTON_START:
140         r = __pStt->Start();
141         if (IsFailed(r))
142         {
143             AppLog("[%s] Start() error", GetErrorMessage(r));
144         }
145         else
146         {
147             __pButtonStart->SetEnabled(false);
148             __pButtonStop->SetEnabled(true);
149             __pButtonCancel->SetEnabled(true);
150
151             __pLabel->SetText("Recording Speech....");
152
153             Draw();
154         }
155         break;
156     case ID_BUTTON_STOP:
157         r = __pStt->Stop();
158         if (IsFailed(r))
159         {
160             AppLog("[%s] Stop() error", GetErrorMessage(r));
161         }
162         else
163         {
164             __pButtonStart->SetEnabled(false);
165             __pButtonStop->SetEnabled(false);
166             __pButtonCancel->SetEnabled(true);
167             __pLabel->SetText("Converting Speech....");
168
169             Draw();
170         }
171         break;
172     case ID_BUTTON_CANCEL:
173         r = __pStt->Cancel();
174         if (IsFailed(r))
175         {
176             AppLog("[%s] Cancle() error", GetErrorMessage(r));
177         }
178                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
179         break;
180
181     default:
182         break;
183     }
184 }
185
186 void
187 SpeechAppSttForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs)
188 {
189     switch(requestId)
190     {
191     case REQUEST_ID_EXIT:
192         ShowMsgBox();
193         break;
194     default:
195         break;
196     }
197 }
198
199 void
200 SpeechAppSttForm::OnFormBackRequested(Osp::Ui::Controls::Form& source)
201 {
202     SceneManager* pSceneManager = SceneManager::GetInstance();
203     AppAssert(pSceneManager);
204
205     pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
206 }
207
208 void
209 SpeechAppSttForm::OnSpeechToTextInitialized(void)
210 {
211     Osp::Ui::Controls::ListView *pList = static_cast<ListView*>(GetControl(L"IDC_LISTVIEW_LANGS"));
212     if (pList != null)
213     {
214         pList->SetItemProvider(*this);
215         pList->AddListViewItemEventListener(*this);
216     }
217     pList->UpdateList();
218
219     __pStt->SetLocale(Locale(LANGUAGE_ENG, COUNTRY_US));
220     __isInited = true;
221
222     __pLabel->SetText("Default Language: eng_US");
223     __pLabel->Draw();
224 }
225
226 void
227 SpeechAppSttForm::OnSpeechToTextStatusChanged(Osp::Uix::Speech::SpeechToTextStatus status)
228 {
229 }
230
231 void
232 SpeechAppSttForm::OnSpeechToTextErrorOccurred(Osp::Uix::Speech::SpeechToTextError error)
233 {
234     if (error == SPEECH_TO_TEXT_ERROR_UNSUPPORTED_SERVICE)
235     {
236         this->SendUserEvent(REQUEST_ID_EXIT, null);
237     }
238 }
239
240 void
241 SpeechAppSttForm::OnSpeechToTextCompleted(const Osp::Base::String& result)
242 {
243     SceneManager* pSceneManager = SceneManager::GetInstance();
244     AppAssert(pSceneManager);
245
246     IList* pList = new ArrayList();
247     pList->Add(*(new String(result)));
248     pList->Add(*(new Locale(__pStt->GetLocale())));
249     pSceneManager->GoForward(ForwardSceneTransition(L"TtsScene", SCENE_TRANSITION_ANIMATION_TYPE_LEFT, SCENE_HISTORY_OPTION_NO_HISTORY, SCENE_DESTROY_OPTION_DESTROY), pList);
250 }
251
252 void
253 SpeechAppSttForm::ShowMsgBox(void)
254 {
255     SceneManager* pSceneManager = SceneManager::GetInstance();
256     AppAssert(pSceneManager);
257
258     MessageBox messageBox;
259     messageBox.Construct(L"Error", L"Unsupported service.", MSGBOX_STYLE_OK);
260
261     int result = 0;
262     messageBox.ShowAndWait(result);
263
264     switch(result)
265     {
266     case MSGBOX_RESULT_OK:
267         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
268         break;
269     default:
270         break;
271     }
272 }
273
274 Osp::Ui::Controls::ListItemBase*
275 SpeechAppSttForm::CreateItem(int index, int itemWidth)
276 {
277     ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
278     CustomItem* pItem = new CustomItem();
279     pItem->Construct(Osp::Graphics::Dimension(itemWidth, 100), style);
280
281     const IList* pList = __pStt->GetSupportedLocales();
282     const Locale* pLocale = static_cast<const Locale*>(pList->GetAt(index));
283
284     pItem->AddElement(Rectangle(10, 25, itemWidth, 50), index, pLocale->GetLocaleCodeString(), true);
285
286     return pItem;
287 }
288
289 bool
290 SpeechAppSttForm::DeleteItem(int index, Osp::Ui::Controls::ListItemBase* pItem, int itemWidth)
291 {
292    delete pItem;
293    pItem = null;
294
295    return true;
296 }
297
298 int
299 SpeechAppSttForm::GetItemCount(void)
300 {
301     return __pStt->GetSupportedLocales()->GetCount();
302 }
303
304 void
305 SpeechAppSttForm::OnListViewContextItemStateChanged(Osp::Ui::Controls::ListView &listView, int index, int elementId, Osp::Ui::Controls::ListContextItemStatus state)
306 {
307 }
308
309 void
310 SpeechAppSttForm::OnListViewItemStateChanged(Osp::Ui::Controls::ListView &listView, int index, int elementId, Osp::Ui::Controls::ListItemStatus status)
311 {
312
313     const IList* pList = __pStt->GetSupportedLocales();
314     const Locale* pLocale = static_cast<const Locale*>(pList->GetAt(index));
315
316     result r = __pStt->SetLocale(*pLocale);
317     if (IsFailed(r))
318     {
319         AppLog("[%s] Error occurred", GetErrorMessage(r));
320     }
321
322     __pLabel->SetText(String("Selected Language: ") + pLocale->GetLocaleCodeString().GetPointer());
323     __pLabel->Draw();
324
325 }
326
327 void
328 SpeechAppSttForm::OnListViewItemSwept(Osp::Ui::Controls::ListView &listView, int index, Osp::Ui::Controls::SweepDirection direction)
329 {
330 }
331
332 void
333 SpeechAppSttForm::OnSceneActivatedN(const Osp::Ui::Scenes::SceneId& previousSceneId,
334                                const Osp::Ui::Scenes::SceneId& currentSceneId, Osp::Base::Collection::IList* pArgs)
335 {
336     AppLog("OnSceneActivatedN()");
337 }
338 void
339 SpeechAppSttForm::OnSceneDeactivated(const Osp::Ui::Scenes::SceneId& currentSceneId,
340                                 const Osp::Ui::Scenes::SceneId& nextSceneId)
341 {
342     AppLog("OnSceneActivatedN()");
343 }