Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / data / my_tools / jj / rsa / apps / osp / Settings / src / StRegionForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://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                StRegionForm.cpp
19  * @brief               This is the implementation file for RegionForm class.
20  */
21
22 #include <FLocales.h>
23 #include "StRegionForm.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_REGION_MAIN = 0;
39 static const int ID_GROUP_COUNT = 1;
40 static const int ITEM_WIDTH_GAP = 100;
41
42 static const int H_SEARCH_BAR_ITEM = H_GROUP_ITEM_DEFAULT;
43 static const int Y_TABLE_VIEW_AREA = H_GROUP_ITEM_DEFAULT;
44
45 static const int H_PORTRAIT_KEY_PAD_INPUT_MODE_GAP = 0;
46 static const int H_LANDSCAPE_KEY_PAD_INPUT_MODE_GAP = 0;
47
48 LocaleRegionForm::LocaleRegionForm(void)
49         : __pTotalLocale(null)
50         , __pSearchedText(null)
51 {
52 }
53
54 LocaleRegionForm::~LocaleRegionForm(void)
55 {
56 }
57
58 void
59 LocaleRegionForm::CreateFooter(void)
60 {
61         Footer* pFooter = GetFooter();
62         AppAssert(pFooter);
63
64         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
65         pFooter->SetBackButton();
66         pFooter->AddActionEventListener(*this);
67
68         SetFormBackEventListener(this);
69 }
70
71 void
72 LocaleRegionForm::CreateTableView(void)
73 {
74         Rectangle bounds = GetClientAreaBounds();
75         Rectangle tableViewBounds = bounds;
76
77         tableViewBounds.y = Y_TABLE_VIEW_AREA;
78         tableViewBounds.height = (bounds.height - tableViewBounds.y);
79
80         __pTableView = new (std::nothrow) GroupedTableView();
81         __pTableView->Construct(tableViewBounds, true, TABLE_VIEW_SCROLL_BAR_STYLE_FADE_OUT);
82         __pTableView->SetItemProvider(this);
83
84         AddControl(*__pTableView);
85
86         __pTableView->AddGroupedTableViewItemEventListener(*this);
87         
88         RelativeLayout* pRelativeLayout = dynamic_cast<RelativeLayout*>(this->GetLayoutN());
89         if (pRelativeLayout != null)
90         {
91                 pRelativeLayout->SetHorizontalFitPolicy(*__pSearchBar, FIT_POLICY_PARENT);
92                 pRelativeLayout->SetVerticalFitPolicy(*__pSearchBar, FIT_POLICY_FIXED);
93                 pRelativeLayout->SetHorizontalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
94                 pRelativeLayout->SetRelation(*__pTableView, *__pSearchBar, RECT_EDGE_RELATION_TOP_TO_BOTTOM);
95                 pRelativeLayout->SetRelation(*__pTableView, *this, RECT_EDGE_RELATION_BOTTOM_TO_BOTTOM);
96                 delete pRelativeLayout;
97         }
98 }
99
100 void
101 LocaleRegionForm::GetLocaleList(void)
102 {
103         String localeLanguage;
104         if (SettingInfo::GetValue(SETTING_INFO_KEY_LOCALE_LANGUAGE, localeLanguage) != E_SUCCESS)
105         {
106                 AppLogDebug("GetValue: SETTING_INFO_KEY_LOCALE_LANGUAGE failed [%s]", GetErrorMessage(GetLastResult()));
107                 localeLanguage = L"eng_GB";
108         }
109
110         LanguageCode langCode = LANGUAGE_INVALID;
111         CountryCode countryCode = COUNTRY_INVALID;
112         StringTokenizer strTok(localeLanguage, L"_");
113         String token;
114
115         if (strTok.HasMoreTokens())
116         {
117                 strTok.GetNextToken(token);
118                 langCode = Locale::StringToLanguageCode(token);
119         }
120
121         if (strTok.HasMoreTokens())
122         {
123                 strTok.GetNextToken(token);
124                 countryCode = Locale::StringToCountryCode(token);
125         }
126
127         Locale currentLocale(langCode, countryCode);
128
129         LocaleManager localeManager;
130         localeManager.Construct();
131
132         IList* pAvailableLocales = localeManager.GetAvailableLocalesN();
133         String test = localeManager.GetSelectedLanguage();
134
135         __pTotalLocale = new (std::nothrow) ArrayList(SingleObjectDeleter);
136         __pTotalLocale->Construct();
137
138         __pSearchedText = new (std::nothrow) ArrayList(SingleObjectDeleter);
139         __pSearchedText->Construct();
140
141         for (int i = 0; i < pAvailableLocales->GetCount(); i++)
142         {
143                 Locale* pLocale = (Locale*) (pAvailableLocales->GetAt(i));
144                 String localeData;
145                 String country;
146                 String languageCode;
147                 String countryCode;
148                 String variantCode;
149
150                 pLocale->GetLanguageName(currentLocale, localeData);
151                 pLocale->GetCountryName(currentLocale, country);
152
153                 variantCode = pLocale->GetVariantCodeString();
154                 languageCode = pLocale->GetLanguageCodeString();
155                 countryCode = pLocale->GetCountryCodeString();
156                 languageCode.Append(L"_" + countryCode);
157
158                 localeData.Append(L" (" + country + L")" + L":" + languageCode);
159
160                 if (languageCode.Equals(L"eng_US", false))
161                 {
162                         if (variantCode.IsEmpty() == false)
163                         {
164                                 continue;
165                         }
166                 }
167                 __pTotalLocale->Add((new (std::nothrow) String(localeData)));
168                 __pSearchedText->Add((new (std::nothrow) String(localeData)));
169         }
170
171         __pTotalLocale->Sort(StringComparer());
172         __pSearchedText->Sort(StringComparer());
173 }
174
175 void
176 LocaleRegionForm::CreateSearchBar(void)
177 {
178         Rectangle bounds = GetClientAreaBounds();
179         Rectangle searchbarBounds = bounds;
180
181         searchbarBounds.y = 0;
182         searchbarBounds.height = H_GROUP_ITEM_DEFAULT;
183
184         __pSearchBar = new (std::nothrow) SearchBar();
185         __pSearchBar->Construct(searchbarBounds, true, KEYPAD_ACTION_SEARCH);
186         __pSearchBar->SetGuideText(L"Search");
187         __pSearchBar->AddSearchBarEventListener(*this);
188         __pSearchBar->AddTextEventListener(*this);
189         __pSearchBar->AddKeypadEventListener(*this);
190         __pSearchBar->SetContentAreaSize(Dimension(0, 0));
191
192         AddControl(*__pSearchBar);
193 }
194
195 result
196 LocaleRegionForm::OnInitializing(void)
197 {
198         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_REGION"));
199         CreateFooter();
200         GetLocaleList();
201         CreateSearchBar();
202         CreateTableView();
203
204         return E_SUCCESS;
205 }
206
207 result
208 LocaleRegionForm::OnTerminating(void)
209 {
210         __pTableView = null;
211
212         SetFormBackEventListener(null);
213         return E_SUCCESS;
214 }
215
216 void
217 LocaleRegionForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
218 {
219         if (__pSearchedText == null)
220         {
221                 AppLogDebug("__pSearchedText is null");
222                 return;
223         }
224         __pTableView->UpdateTableView();
225         SceneManager* pSceneManager = SceneManager::GetInstance();
226         AppAssert(pSceneManager);
227         String selectData;
228
229         if (SettingInfo::GetValue(SETTING_INFO_KEY_LOCALE_COUNTRY, selectData) != E_SUCCESS)
230         {
231                 AppLogDebug("GetValue: SETTING_INFO_KEY_LOCALE_COUNTRY failed [%s]", GetErrorMessage(GetLastResult()));
232                 return;
233         }
234
235         Invalidate(true);
236 }
237
238 void
239 LocaleRegionForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
240 {
241 }
242
243 void
244 LocaleRegionForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
245 {
246         SceneManager* pSceneManager = SceneManager::GetInstance();
247         AppAssert(pSceneManager);
248
249         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
250 }
251
252 int
253 LocaleRegionForm::GetGroupCount(void)
254 {
255         AppLogDebug("ENTER");
256         return ID_GROUP_COUNT;
257 }
258
259 int
260 LocaleRegionForm::GetItemCount(int groupIndex)
261 {
262         int itemCount = __pSearchedText->GetCount();
263
264         AppLogDebug("GetItemCount %d", itemCount);
265
266         return itemCount;
267 }
268
269 TableViewGroupItem*
270 LocaleRegionForm::CreateGroupItem(int groupIndex, int itemWidth)
271 {
272         AppLogDebug("ENTER");
273
274         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
275         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
276         Rectangle itemRectangle;
277         String groupText;
278         Label* pLabel = null;
279
280         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
281
282         itemRectangle.y = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
283         itemRectangle.height = itemHeight;
284         itemRectangle.width = itemWidth;
285
286         RelativeLayout relativeLayout;
287         relativeLayout.Construct();
288
289         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
290         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
291
292         pLabel = new (std::nothrow) Label();
293         pLabel->Construct(itemRectangle, groupText);
294         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
295         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
296         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
297         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
298
299         pItem->AddControl(*pLabel);
300         relativeLayout.SetMargin(*pLabel, itemRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
301         relativeLayout.SetRelation(*pLabel, *pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
302         relativeLayout.SetRelation(*pLabel, *pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
303         pItem->SetEnabled(false);
304
305         return pItem;
306 }
307
308 TableViewItem*
309 LocaleRegionForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
310 {
311         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
312
313         Rectangle itemRectangle;
314         Rectangle itemSecondLineTextRect;
315         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
316         String* pKey = null;
317         String itemText;
318         String currentRegion;
319         String listRegion;
320         Label* pLabel = null;
321         int FontSize = GetFontSize();
322         LocaleManager localeManager;
323         localeManager.Construct();
324
325         Locale systemLocale = localeManager.GetSystemLocale();
326         systemLocale.GetCountryName(currentRegion);
327
328         pKey = static_cast<String*>(__pSearchedText->GetAt(itemIndex));
329
330         StringTokenizer stringTokenizer(*pKey, L":");
331         stringTokenizer.GetNextToken(itemText);
332
333         TableViewItem* pItem = new (std::nothrow) TableViewItem();
334
335         if (itemText.Contains(currentRegion) == true)
336         {
337                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
338         }
339
340         ItemTypeOneLine(itemRectangle);
341         itemRectangle.width = itemWidth;
342
343         RelativeLayout relativeLayout;
344         relativeLayout.Construct();
345
346         pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_ITEM_DEFAULT), style);
347         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
348
349         pLabel = new (std::nothrow) Label();
350         pLabel->Construct(itemRectangle, itemText);
351         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
352         pLabel->SetTextConfig(FontSize, LABEL_TEXT_STYLE_NORMAL);
353         pLabel->SetTextColor(COLOR_MAIN_TEXT);
354
355         pItem->AddControl(*pLabel);
356         
357         relativeLayout.SetRelation(*pLabel, *pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
358         relativeLayout.SetRelation(*pLabel, *pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
359
360         return pItem;
361 }
362
363 bool
364 LocaleRegionForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
365 {
366         AppLogDebug("ENTER");
367
368         delete pItem;
369         pItem = null;
370
371         return true;
372 }
373
374 bool
375 LocaleRegionForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
376 {
377         AppLogDebug("ENTER");
378
379         delete pItem;
380         pItem = null;
381
382         return true;
383 }
384
385 void
386 LocaleRegionForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
387 {
388         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
389
390         SceneManager* pSceneManager = SceneManager::GetInstance();
391         AppAssert(pSceneManager);
392         bool checkStatus = false;
393         String* pKey = null;
394         String selectData;
395         String key = *(static_cast<String*>(__pSearchedText->GetAt(itemIndex)));
396         AppLogDebug("selected key [%ls]", key.GetPointer());
397
398         if (key.Equals(L"No Search Result", true))
399         {
400                 AppLogDebug("selected key is \"No Search Result\"");
401                 return;
402         }
403
404         for (int count = 0; count < __pSearchedText->GetCount(); count++)
405         {
406                 if (count == itemIndex)
407                 {
408                         checkStatus = !checkStatus;
409
410                         pKey = (static_cast<String*>(__pSearchedText->GetAt(count)));
411
412                         StringTokenizer stringTokenizer(*pKey, L":");
413                         for (int i = 0; i <= stringTokenizer.GetTokenCount(); i++)
414                         {
415                                 stringTokenizer.GetNextToken(selectData);
416                         }
417
418                         if (SettingInfo::SetValue(SETTING_INFO_KEY_LOCALE_COUNTRY, selectData) != E_SUCCESS)
419                         {
420                                 AppLogDebug("SetValue: SETTING_INFO_KEY_LOCALE_COUNTRY failed [%s]", GetErrorMessage(GetLastResult()));
421                         }
422                         else
423                         {
424                                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
425                         }
426                         tableView.SetItemChecked(groupIndex, count, true);
427                 }
428                 else
429                 {
430                         tableView.SetItemChecked(groupIndex, count, false);
431                 }
432         }
433         Invalidate(true);
434 }
435
436 int
437 LocaleRegionForm::GetDefaultGroupItemHeight(void)
438 {
439         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
440 }
441
442 int
443 LocaleRegionForm::GetDefaultItemHeight(void)
444 {
445         return H_GROUP_ITEM_DEFAULT;
446 }
447
448 void
449 LocaleRegionForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
450 {
451 }
452
453 void
454 LocaleRegionForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
455 {
456 }
457
458 void
459 LocaleRegionForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
460 {
461 }
462
463 void
464 LocaleRegionForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
465 {
466 }
467
468 void
469 LocaleRegionForm::OnSearchBarModeChanged(Tizen::Ui::Controls::SearchBar& source, Tizen::Ui::Controls::SearchBarMode mode)
470 {
471         if (mode == SEARCH_BAR_MODE_INPUT)
472         {
473                 // Empty statement
474         }
475         else
476         {
477                 __pSearchedText->RemoveAll();
478                 __pSearchedText =  static_cast<ArrayList*>(__pTotalLocale->GetItemsN(0, __pTotalLocale->GetCount()));
479
480                 __pTableView->UpdateTableView();
481         }
482         Invalidate(true);
483 }
484
485 void
486 LocaleRegionForm::OnKeypadClosed(Tizen::Ui::Control& source)
487 {
488         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, true);
489         Invalidate(true);
490 }
491
492 void
493 LocaleRegionForm::OnKeypadWillOpen(Tizen::Ui::Control& source)
494 {
495         SetActionBarsVisible(FORM_ACTION_BAR_FOOTER, false);
496 }
497
498 void
499 LocaleRegionForm::OnKeypadOpened(Tizen::Ui::Control& source)
500 {
501         Invalidate(true);
502 }
503
504 void
505 LocaleRegionForm::OnTextValueChanged(const Tizen::Ui::Control& source)
506 {
507         String searchData;
508         String inputsearchText = __pSearchBar->GetText();
509         int GetItemCount = __pTotalLocale->GetCount();
510
511         if (inputsearchText.CompareTo(L"") != 0)
512         {
513                 __pSearchedText->RemoveAll();
514
515                 for (int i = 0; i < GetItemCount; i++)
516                 {
517                         String toLowerSearchData;
518                         String toLowerInputSearchText;
519                         searchData = *(static_cast<String*>(__pTotalLocale->GetAt(i)));
520
521                         searchData.ToLower(toLowerSearchData);
522                         inputsearchText.ToLower(toLowerInputSearchText);
523
524                         if ((toLowerSearchData).Contains(toLowerInputSearchText))
525                         {
526                                 if (searchData != null)
527                                 {
528                                         __pSearchedText->Add(*(new (std::nothrow) String(searchData)));
529                                 }
530                         }
531                 }
532         }
533         else
534         {
535                 __pSearchedText->RemoveAll();
536                 __pSearchedText = static_cast<ArrayList*>(__pTotalLocale->GetItemsN(0, __pTotalLocale->GetCount()));
537         }
538
539         if (__pSearchedText->GetCount() <= 0)
540         {
541                  __pSearchedText->Add(*(new (std::nothrow) String(L"No Search Result")));
542         }
543
544         __pTableView->UpdateTableView();
545 }
546
547 void
548 LocaleRegionForm::OnKeypadActionPerformed(Tizen::Ui::Control& source, Tizen::Ui::KeypadAction keypadAction)
549 {
550         switch (keypadAction)
551         {
552         case KEYPAD_ACTION_SEARCH:
553                 {
554                         __pSearchBar->HideKeypad();
555                         __pSearchBar->Invalidate(false);
556                 }
557                 break;
558
559         default:
560                 break;
561         }
562 }