resource fixed
[apps/native/sample/SpeechApp.git] / 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 #include "AppResourceId.h"
23
24 using namespace Tizen::Base;
25 using namespace Tizen::Base::Collection;
26 using namespace Tizen::Ui;
27 using namespace Tizen::Ui::Controls;
28 using namespace Tizen::Ui::Scenes;
29 using namespace Tizen::Uix::Speech;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Locales;
32
33
34 SpeechAppSttForm::SpeechAppSttForm(void)
35         : __pStt(null)
36         , __pLabel(null)
37         , __pButtonStart(null)
38         , __pButtonStop(null)
39         , __pButtonCancel(null)
40         , __isInited(false)
41 {
42 }
43
44 SpeechAppSttForm::~SpeechAppSttForm(void)
45 {
46 }
47
48 bool
49 SpeechAppSttForm::Initialize(void)
50 {
51         Construct(IDF_FORM_STT);
52
53         return true;
54 }
55
56 result
57 SpeechAppSttForm::OnInitializing(void)
58 {
59         result r = E_SUCCESS;
60
61         SetFormBackEventListener(this);
62
63         __pButtonStart = static_cast< Button* >(GetControl(IDC_BUTTON_START));
64         if (__pButtonStart != null)
65         {
66                 __pButtonStart->SetActionId(ID_BUTTON_START);
67                 __pButtonStart->AddActionEventListener(*this);
68                 __pButtonStart->SetEnabled(true);
69         }
70
71         __pButtonStop = static_cast< Button* >(GetControl(IDC_BUTTON_STOP));
72         if (__pButtonStop != null)
73         {
74                 __pButtonStop->SetActionId(ID_BUTTON_STOP);
75                 __pButtonStop->AddActionEventListener(*this);
76                 __pButtonStop->SetEnabled(false);
77         }
78
79         __pButtonCancel = static_cast< Button* >(GetControl(IDC_BUTTON_CANCEL));
80         if (__pButtonCancel != null)
81         {
82                 __pButtonCancel->SetActionId(ID_BUTTON_CANCEL);
83                 __pButtonCancel->AddActionEventListener(*this);
84                 __pButtonCancel->SetEnabled(false);
85         }
86
87         __pLabel = static_cast< Label* >(GetControl(IDC_LABEL_INFO));
88         if (__pLabel != null)
89         {
90                 __pLabel->SetText("Information box");
91         }
92
93         __pStt = new (std::nothrow) SpeechToText();
94         if (__pStt != null)
95         {
96                 r = __pStt->Construct(*this);
97             TryLog(r == E_SUCCESS, "[%s] Construct() error", GetErrorMessage(r));
98
99                 r = __pStt->Initialize();
100                 TryLog(r == E_SUCCESS, "[%s] Initialize() error", GetErrorMessage(r));
101         }
102         else
103         {
104                 SceneManager* pSceneManager = SceneManager::GetInstance();
105                 AppAssert(pSceneManager);
106                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
107         }
108
109         SetOrientation(ORIENTATION_PORTRAIT);
110
111         return r;
112 }
113
114 result
115 SpeechAppSttForm::OnTerminating(void)
116 {
117         result r = E_SUCCESS;
118
119         delete __pStt;
120
121         return r;
122 }
123
124 void
125 SpeechAppSttForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
126 {
127         result r = E_SUCCESS;
128
129         SceneManager* pSceneManager = SceneManager::GetInstance();
130         AppAssert(pSceneManager);
131
132         switch (actionId)
133         {
134         case ID_BUTTON_START:
135                 r = __pStt->Start();
136                 if (IsFailed(r))
137                 {
138                         AppLog("[%s] Start() error", GetErrorMessage(r));
139                 }
140                 else
141                 {
142                         __pButtonStart->SetEnabled(false);
143                         __pButtonStop->SetEnabled(true);
144                         __pButtonCancel->SetEnabled(true);
145
146                         __pLabel->SetText("Recording Speech....");
147
148                         Draw();
149                 }
150                 break;
151
152         case ID_BUTTON_STOP:
153                 r = __pStt->Stop();
154                 if (IsFailed(r))
155                 {
156                         AppLog("[%s] Stop() error", GetErrorMessage(r));
157                 }
158                 else
159                 {
160                         __pButtonStart->SetEnabled(false);
161                         __pButtonStop->SetEnabled(false);
162                         __pButtonCancel->SetEnabled(true);
163                         __pLabel->SetText("Converting Speech....");
164
165                         Draw();
166                 }
167                 break;
168
169         case ID_BUTTON_CANCEL:
170                 r = __pStt->Cancel();
171             TryLog(r == E_SUCCESS, "[%s] Cancel() error", GetErrorMessage(r));
172
173                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
174                 break;
175
176         default:
177                 break;
178         }
179 }
180
181 void
182 SpeechAppSttForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
183 {
184         switch (requestId)
185         {
186         case REQUEST_ID_EXIT:
187                 ShowMsgBox();
188                 break;
189
190         default:
191                 break;
192         }
193 }
194
195 void
196 SpeechAppSttForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
197 {
198         SceneManager* pSceneManager = SceneManager::GetInstance();
199         AppAssert(pSceneManager);
200
201         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
202 }
203
204 void
205 SpeechAppSttForm::OnSpeechToTextInitialized(void)
206 {
207         Tizen::Ui::Controls::ListView* pList = static_cast< ListView* >(GetControl(IDC_LISTVIEW_LANGS));
208         if (pList != null)
209         {
210                 pList->SetItemProvider(*this);
211                 pList->AddListViewItemEventListener(*this);
212         }
213         pList->UpdateList();
214
215         __pStt->SetLocale(Locale(LANGUAGE_ENG, COUNTRY_US));
216         __isInited = true;
217
218         __pLabel->SetText("Default Language: eng_US");
219         __pLabel->Draw();
220 }
221
222 void
223 SpeechAppSttForm::OnSpeechToTextStatusChanged(Tizen::Uix::Speech::SpeechToTextStatus status)
224 {
225 }
226
227 void
228 SpeechAppSttForm::OnSpeechToTextErrorOccurred(Tizen::Uix::Speech::SpeechToTextError error)
229 {
230         if (error == SPEECH_TO_TEXT_ERROR_UNSUPPORTED_SERVICE)
231         {
232                 this->SendUserEvent(REQUEST_ID_EXIT, null);
233         }
234 }
235
236 void
237 SpeechAppSttForm::OnSpeechToTextCompleted(const Tizen::Base::String& result)
238 {
239         SceneManager* pSceneManager = SceneManager::GetInstance();
240         AppAssert(pSceneManager);
241
242         IList* pList = new (std::nothrow) ArrayList();
243         pList->Add(*(new (std::nothrow) String(result)));
244         pList->Add(*(new (std::nothrow) Locale(__pStt->GetLocale())));
245         pSceneManager->GoForward(ForwardSceneTransition(L"TtsScene", SCENE_TRANSITION_ANIMATION_TYPE_LEFT, SCENE_HISTORY_OPTION_NO_HISTORY, SCENE_DESTROY_OPTION_DESTROY), pList);
246 }
247
248 void
249 SpeechAppSttForm::ShowMsgBox(void)
250 {
251         SceneManager* pSceneManager = SceneManager::GetInstance();
252         AppAssert(pSceneManager);
253
254         MessageBox messageBox;
255         messageBox.Construct(L"Error", L"Unsupported service.", MSGBOX_STYLE_OK);
256
257         int result = 0;
258         messageBox.ShowAndWait(result);
259
260         switch (result)
261         {
262         case MSGBOX_RESULT_OK:
263                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
264                 break;
265
266         default:
267                 break;
268         }
269 }
270
271 Tizen::Ui::Controls::ListItemBase*
272 SpeechAppSttForm::CreateItem(int index, int itemWidth)
273 {
274         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
275         CustomItem* pItem = new (std::nothrow) CustomItem();
276         pItem->Construct(Tizen::Graphics::Dimension(itemWidth, 100), style);
277
278         const IList* pList = __pStt->GetSupportedLocales();
279         const Locale* pLocale = static_cast< const Locale* >(pList->GetAt(index));
280
281         pItem->AddElement(Rectangle(10, 25, itemWidth, 50), index, pLocale->GetLocaleCodeString(), true);
282
283         return pItem;
284 }
285
286 bool
287 SpeechAppSttForm::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase* pItem, int itemWidth)
288 {
289         delete pItem;
290         pItem = null;
291
292         return true;
293 }
294
295 int
296 SpeechAppSttForm::GetItemCount(void)
297 {
298         return __pStt->GetSupportedLocales()->GetCount();
299 }
300
301 void
302 SpeechAppSttForm::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state)
303 {
304 }
305
306 void
307 SpeechAppSttForm::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView& listView, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status)
308 {
309         const IList* pList = __pStt->GetSupportedLocales();
310         const Locale* pLocale = static_cast< const Locale* >(pList->GetAt(index));
311
312         result r = __pStt->SetLocale(*pLocale);
313         TryLog(r == E_SUCCESS, "[%s] Error occurred", GetErrorMessage(r));
314
315         __pLabel->SetText(String("Selected Language: ") + pLocale->GetLocaleCodeString().GetPointer());
316         __pLabel->Draw();
317 }
318
319 void
320 SpeechAppSttForm::OnListViewItemSwept(Tizen::Ui::Controls::ListView& listView, int index, Tizen::Ui::Controls::SweepDirection direction)
321 {
322 }
323
324 void
325 SpeechAppSttForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
326                                                                         const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
327 {
328         AppLog("OnSceneActivatedN()");
329 }
330 void
331 SpeechAppSttForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
332                                                                          const Tizen::Ui::Scenes::SceneId& nextSceneId)
333 {
334         AppLog("OnSceneActivatedN()");
335 }