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