Applied latest source code
[apps/native/preloaded/Settings.git] / src / StLanguageForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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  * @file                StLanguageForm.cpp
19  * @brief               This is the implementation file for LanguageForm class.
20  */
21
22 #include <FLocales.h>
23 #include "StLanguageForm.h"
24 #include "StResourceManager.h"
25 #include "StTypes.h"
26
27 using namespace Tizen::App;
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Base::Utility;
31 using namespace Tizen::Graphics;
32 using namespace Tizen::Locales;
33 using namespace Tizen::System;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Ui::Controls;
36 using namespace Tizen::Ui::Scenes;
37
38 static const int ID_GROUP_COUNT = 1;
39 static const int H_FONT_HEIGHT_GAP = 20;
40
41 LocaleLanguageForm::LocaleLanguageForm(void)
42         : __pLanguageDataList(null)
43 {
44 }
45
46 LocaleLanguageForm::~LocaleLanguageForm(void)
47 {
48 }
49
50 void
51 LocaleLanguageForm::CreateFooter(void)
52 {
53         Footer* pFooter = GetFooter();
54         AppAssert(pFooter);
55
56         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
57         pFooter->AddActionEventListener(*this);
58
59         SetFormBackEventListener(this);
60 }
61
62 result
63 LocaleLanguageForm::OnInitializing(void)
64 {
65         CreateHeader(ResourceManager::GetString(L"IDS_ST_HEADER_DISPLAY_LANGUAGE"));
66         CreateTableView();
67
68         SettingInfo::GetValue(SETTING_INFO_KEY_LOCALE_LANGUAGE, __previousSelectedLanguage);
69         return SettingInfo::AddSettingEventListener(*this);
70 }
71
72 result
73 LocaleLanguageForm::OnTerminating(void)
74 {
75         SettingInfo::RemoveSettingEventListener(*this);
76         __pTableView = null;
77
78         delete __pLanguageDataList;
79         __pLanguageDataList = null;
80
81         SetFormBackEventListener(null);
82         return E_SUCCESS;
83 }
84
85 void
86 LocaleLanguageForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
87 {
88         String langCode;
89
90         if (pArgs != null)
91         {
92                 __pLanguageDataList = static_cast<ArrayList*>(pArgs->GetItemsN(0, pArgs->GetCount()));
93         }
94         __pTableView->UpdateTableView();
95 }
96
97 void
98 LocaleLanguageForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
99 {
100 }
101
102 void
103 LocaleLanguageForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
104 {
105         SceneManager* pSceneManager = SceneManager::GetInstance();
106         AppAssert(pSceneManager);
107
108         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
109 }
110
111 int
112 LocaleLanguageForm::GetGroupCount(void)
113 {
114         // Temporary code due to the UI Framework defect
115         if (!__pLanguageDataList)
116         {
117                 return 0;
118         }
119
120         AppLogDebug("ENTER");
121         return ID_GROUP_COUNT;
122 }
123
124 int
125 LocaleLanguageForm::GetItemCount(int groupIndex)
126 {
127         // Temporary code due to the UI Framework defect
128         if (!__pLanguageDataList)
129         {
130                 return 0;
131         }
132
133         int itemCount = __pLanguageDataList->GetCount();
134
135         AppLogDebug("GetItemCount %d", itemCount);
136
137         return itemCount;
138 }
139
140 TableViewGroupItem*
141 LocaleLanguageForm::CreateGroupItem(int groupIndex, int itemWidth)
142 {
143         AppLogDebug("ENTER");
144
145         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
146         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
147         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
148         Rectangle itemMainRectangle;
149         String groupText;
150         Label* pLabel = null;
151
152         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
153
154         yItemOffset = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
155         itemHeight = Y_GROUP_DEFAULT;
156
157         ItemTypeOneLine(itemMainRectangle);
158
159         RelativeLayout relativeLayout;
160         relativeLayout.Construct();
161
162         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
163         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
164
165         pLabel = new (std::nothrow) Label();
166         pLabel->Construct(itemMainRectangle, groupText);
167         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
168         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
169         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
170         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
171
172         pItem->AddControl(pLabel);
173         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
174         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
175         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
176         pItem->SetEnabled(false);
177
178         return pItem;
179 }
180
181 TableViewItem*
182 LocaleLanguageForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
183 {
184         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
185
186         Rectangle itemMainRectangle;
187         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
188         String itemMainText;
189         String fontReturnValue;
190         Label* pLabel = null;
191         int fontSize = GetFontSize();
192         TableViewItem* pItem = new (std::nothrow) TableViewItem();
193         String* pKey = static_cast<String*>(__pLanguageDataList->GetAt(itemIndex));
194
195         ItemTypeOneLine(itemMainRectangle);
196         if (fontSize > FONT_MAIN_TEXT_SIZE_LARGE)
197         {
198                 itemMainRectangle.height = itemMainRectangle.height + H_FONT_HEIGHT_GAP;
199         }
200         itemWidth = itemMainRectangle.width;
201
202         if (pKey && pKey->GetLength() > 0)
203         {
204                 StringTokenizer stringTokenizer(*pKey, L"/");
205                 stringTokenizer.GetNextToken(itemMainText);
206         }
207
208         RelativeLayout relativeLayout;
209         relativeLayout.Construct();
210
211         pItem->Construct(relativeLayout, Dimension(itemWidth, itemMainRectangle.height), style);
212         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
213
214         pLabel = new (std::nothrow) Label();
215         pLabel->Construct(itemMainRectangle, itemMainText);
216         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
217
218         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
219         pLabel->SetTextColor(COLOR_MAIN_TEXT);
220
221         pItem->AddControl(pLabel);
222         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
223         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
224         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
225
226         return pItem;
227 }
228
229 bool
230 LocaleLanguageForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
231 {
232         AppLogDebug("ENTER");
233
234         delete pItem;
235         pItem = null;
236
237         return true;
238 }
239
240 bool
241 LocaleLanguageForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
242 {
243         AppLogDebug("ENTER");
244
245         delete pItem;
246         pItem = null;
247
248         return true;
249 }
250
251 void
252 LocaleLanguageForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
253 {
254         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
255
256         SceneManager* pSceneManager = SceneManager::GetInstance();
257         AppAssert(pSceneManager);
258
259         bool ttsStatus = false;
260         result r = E_FAILURE;
261
262         String langCode;
263         String* pKey = static_cast<String*>(__pLanguageDataList->GetAt(itemIndex));
264
265         StringTokenizer stringTokenizer(*pKey, L"/");
266
267         for (int i = 0; i <= (stringTokenizer.GetTokenCount()); i++)
268         {
269                 stringTokenizer.GetNextToken(langCode);
270         }
271
272         r = SettingInfo::GetValue(SETTING_INFO_KEY_TTS, ttsStatus);
273         if (r == E_SUCCESS && ttsStatus)
274         {
275                 if (langCode.CompareTo(L"eng_GB") != 0 && langCode.CompareTo(L"eng_PH") != 0 && langCode.CompareTo(L"eng_US") != 0
276                                 && langCode.CompareTo(L"spa_ES") != 0 && langCode.CompareTo(L"spa_MX") !=0
277                                 && langCode.CompareTo(L"fra_CA") != 0 && langCode.CompareTo(L"fra_FR") != 0
278                                 && langCode.CompareTo(L"deu_DE") != 0 && langCode.CompareTo(L"ita_IT") != 0 && langCode.CompareTo(L"kor_KR") != 0)
279                 {
280                         // TTS on, language other than eng_GB, eng_PH, eng_US, spa_ES, spa_MX, fra_CA, fra_FR, deu_DE,ita_IT, kor_KR are not supported
281                         ShowMessageBox(ResourceManager::GetString(L"IDS_EMAIL_POP_ALERT"), ResourceManager::GetString(L"IDS_ST_POP_NOT_SUPPORTED"));
282                         return;
283                 }
284         }
285
286         if(langCode.CompareTo(__previousSelectedLanguage) != 0)
287         {
288                 r = SettingInfo::SetValue(SETTING_INFO_KEY_LOCALE_LANGUAGE, langCode);
289                 if (r == E_INVALID_ARG)
290                 {
291                         AppLogDebug("set langCode[%ls] result[%s]", langCode.GetPointer(), GetErrorMessage(r));
292                         ShowMessageBox(L"", L"Language not supported.");
293                         return;
294                 }
295                 else
296                 {
297                         AppLog("else set langCode[%ls] result[%s]", langCode.GetPointer(), GetErrorMessage(r));
298                 }
299         }
300         else
301         {
302                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
303         }
304
305         __previousSelectedLanguage = langCode;
306 }
307
308 void
309 LocaleLanguageForm::OnSettingChanged(Tizen::Base::String& key)
310 {
311         AppLog("Enter");
312         if (key.Equals(SETTING_INFO_KEY_LOCALE_LANGUAGE, false))
313         {
314                 SceneManager* pSceneManager = SceneManager::GetInstance();
315                 AppAssert(pSceneManager);
316
317                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
318         }
319 }
320
321 int
322 LocaleLanguageForm::GetDefaultGroupItemHeight(void)
323 {
324         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
325 }
326
327 int
328 LocaleLanguageForm::GetDefaultItemHeight(void)
329 {
330         return H_GROUP_ITEM_DEFAULT;
331 }
332
333 void
334 LocaleLanguageForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
335 {
336 }
337
338 void
339 LocaleLanguageForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
340 {
341 }
342
343 void
344 LocaleLanguageForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
345 {
346 }
347
348 void
349 LocaleLanguageForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
350 {
351 }