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