Applied latest source code
[apps/native/preloaded/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_COUNT = 1;
39 static const int Y_TABLE_VIEW_AREA = H_GROUP_ITEM_DEFAULT;
40
41 static const int H_SEARCH_BAR = 86;
42
43 LocaleRegionForm::LocaleRegionForm(void)
44         : __pTotalLocale(null)
45         , __pSearchedText(null)
46 {
47 }
48
49 LocaleRegionForm::~LocaleRegionForm(void)
50 {
51 }
52
53 void
54 LocaleRegionForm::CreateFooter(void)
55 {
56         Footer* pFooter = GetFooter();
57         AppAssert(pFooter);
58
59         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
60         pFooter->AddActionEventListener(*this);
61
62         SetFormBackEventListener(this);
63 }
64
65 void
66 LocaleRegionForm::CreateTableView(void)
67 {
68         Rectangle bounds = GetClientAreaBounds();
69         Rectangle tableViewBounds = bounds;
70
71         tableViewBounds.y = Y_TABLE_VIEW_AREA;
72         tableViewBounds.height = (bounds.height - tableViewBounds.y);
73
74         __pTableView = new (std::nothrow) GroupedTableView();
75         __pTableView->Construct(tableViewBounds, true, TABLE_VIEW_SCROLL_BAR_STYLE_FADE_OUT);
76         __pTableView->SetItemProvider(this);
77
78         AddControl(__pTableView);
79
80         __pTableView->AddGroupedTableViewItemEventListener(*this);
81         
82         RelativeLayout* pRelativeLayout = dynamic_cast<RelativeLayout*>(this->GetLayoutN());
83         if (pRelativeLayout != null)
84         {
85                 pRelativeLayout->SetHorizontalFitPolicy(*__pSearchBar, FIT_POLICY_PARENT);
86                 pRelativeLayout->SetVerticalFitPolicy(*__pSearchBar, FIT_POLICY_FIXED);
87                 pRelativeLayout->SetHorizontalFitPolicy(*__pTableView, FIT_POLICY_PARENT);
88                 pRelativeLayout->SetRelation(*__pTableView, __pSearchBar, RECT_EDGE_RELATION_TOP_TO_BOTTOM);
89                 pRelativeLayout->SetRelation(*__pTableView, this, RECT_EDGE_RELATION_BOTTOM_TO_BOTTOM);
90                 delete pRelativeLayout;
91         }
92 }
93
94 void
95 LocaleRegionForm::GetLocaleList(void)
96 {
97         String localeLanguage;
98         if (SettingInfo::GetValue(SETTING_INFO_KEY_LOCALE_LANGUAGE, localeLanguage) != E_SUCCESS)
99         {
100                 AppLogDebug("GetValue: SETTING_INFO_KEY_LOCALE_LANGUAGE failed [%s]", GetErrorMessage(GetLastResult()));
101                 localeLanguage = L"eng_GB";
102         }
103
104         LanguageCode langCode = LANGUAGE_INVALID;
105         CountryCode countryCode = COUNTRY_INVALID;
106         StringTokenizer strTok(localeLanguage, L"_");
107         String token;
108
109         if (strTok.HasMoreTokens())
110         {
111                 strTok.GetNextToken(token);
112                 langCode = Locale::StringToLanguageCode(token);
113         }
114
115         if (strTok.HasMoreTokens())
116         {
117                 strTok.GetNextToken(token);
118                 countryCode = Locale::StringToCountryCode(token);
119         }
120
121         Locale currentLocale(langCode, countryCode);
122
123         LocaleManager localeManager;
124         localeManager.Construct();
125
126         IList* pAvailableLocales = localeManager.GetAvailableLocalesN();
127         String test = localeManager.GetSelectedLanguage();
128
129         __pTotalLocale = new (std::nothrow) ArrayList(SingleObjectDeleter);
130         __pTotalLocale->Construct();
131
132         __pSearchedText = new (std::nothrow) ArrayList(SingleObjectDeleter);
133         __pSearchedText->Construct();
134
135         for (int i = 0; i < pAvailableLocales->GetCount(); i++)
136         {
137                 Locale* pLocale = (Locale*) (pAvailableLocales->GetAt(i));
138                 String localeData;
139                 String country;
140                 String languageCode;
141                 String countryCode;
142                 String variantCode;
143
144                 pLocale->GetLanguageName(currentLocale, localeData);
145                 pLocale->GetCountryName(currentLocale, country);
146
147                 variantCode = pLocale->GetVariantCodeString();
148                 languageCode = pLocale->GetLanguageCodeString();
149                 countryCode = pLocale->GetCountryCodeString();
150                 languageCode.Append(L"_" + countryCode);
151
152                 localeData.Append(L" (" + country + L")" + L":" + languageCode);
153
154                 if (languageCode.Equals(L"eng_US", false))
155                 {
156                         if (variantCode.IsEmpty() == false)
157                         {
158                                 continue;
159                         }
160                 }
161                 __pTotalLocale->Add((new (std::nothrow) String(localeData)));
162                 __pSearchedText->Add((new (std::nothrow) String(localeData)));
163         }
164
165         __pTotalLocale->Sort(StringComparer());
166         __pSearchedText->Sort(StringComparer());
167 }
168
169 void
170 LocaleRegionForm::CreateSearchBar(void)
171 {
172         Rectangle bounds = GetClientAreaBounds();
173         Rectangle searchbarBounds = bounds;
174
175         searchbarBounds.y = 0;
176         searchbarBounds.height = H_SEARCH_BAR;
177
178         __pSearchBar = new (std::nothrow) SearchBar();
179         __pSearchBar->Construct(searchbarBounds, true, KEYPAD_ACTION_SEARCH);
180         __pSearchBar->SetGuideText(ResourceManager::GetString(L"IDS_COM_BODY_SEARCH"));
181         __pSearchBar->AddSearchBarEventListener(*this);
182         __pSearchBar->AddTextEventListener(*this);
183         __pSearchBar->AddKeypadEventListener(*this);
184         __pSearchBar->SetContentAreaSize(Dimension(0, 0));
185
186         AddControl(__pSearchBar);
187 }
188
189 result
190 LocaleRegionForm::OnInitializing(void)
191 {
192         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_REGION"));
193         SettingInfo::GetValue(SETTING_INFO_KEY_LOCALE_COUNTRY, __previousSelectedRegion);
194         GetLocaleList();
195         CreateSearchBar();
196         CreateTableView();
197
198         return E_SUCCESS;
199 }
200
201 result
202 LocaleRegionForm::OnTerminating(void)
203 {
204         __pTableView = null;
205
206         SetFormBackEventListener(null);
207         return E_SUCCESS;
208 }
209
210 void
211 LocaleRegionForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
212 {
213         if (__pSearchedText == null)
214         {
215                 AppLogDebug("__pSearchedText is null");
216                 return;
217         }
218         __pTableView->UpdateTableView();
219         SceneManager* pSceneManager = SceneManager::GetInstance();
220         AppAssert(pSceneManager);
221         String selectData;
222
223         if (SettingInfo::GetValue(SETTING_INFO_KEY_LOCALE_COUNTRY, selectData) != E_SUCCESS)
224         {
225                 AppLogDebug("GetValue: SETTING_INFO_KEY_LOCALE_COUNTRY failed [%s]", GetErrorMessage(GetLastResult()));
226                 return;
227         }
228
229         Invalidate(true);
230 }
231
232 void
233 LocaleRegionForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
234 {
235 }
236
237 void
238 LocaleRegionForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
239 {
240         SceneManager* pSceneManager = SceneManager::GetInstance();
241         AppAssert(pSceneManager);
242
243         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
244 }
245
246 int
247 LocaleRegionForm::GetGroupCount(void)
248 {
249         AppLogDebug("ENTER");
250         return ID_GROUP_COUNT;
251 }
252
253 int
254 LocaleRegionForm::GetItemCount(int groupIndex)
255 {
256         int itemCount = __pSearchedText->GetCount();
257
258         AppLogDebug("GetItemCount %d", itemCount);
259
260         return itemCount;
261 }
262
263 TableViewGroupItem*
264 LocaleRegionForm::CreateGroupItem(int groupIndex, int itemWidth)
265 {
266         AppLogDebug("ENTER");
267
268         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
269         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
270         Rectangle itemMainRectangle;
271         String groupText;
272         Label* pLabel = null;
273
274         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
275
276         itemMainRectangle.y = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
277         itemMainRectangle.height = itemHeight;
278         itemMainRectangle.width = itemWidth;
279
280         RelativeLayout relativeLayout;
281         relativeLayout.Construct();
282
283         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
284         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
285
286         pLabel = new (std::nothrow) Label();
287         pLabel->Construct(itemMainRectangle, groupText);
288         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
289         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
290         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
291         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
292
293         pItem->AddControl(pLabel);
294         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
295         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
296         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
297         pItem->SetEnabled(false);
298
299         return pItem;
300 }
301
302 TableViewItem*
303 LocaleRegionForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
304 {
305         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
306
307         Rectangle itemMainRectangle;
308         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
309         String* pKey = null;
310         String itemMainText;
311         String currentRegion;
312         String listRegion;
313         Label* pLabel = null;
314         int FontSize = GetFontSize();
315         LocaleManager localeManager;
316         localeManager.Construct();
317
318         Locale systemLocale = localeManager.GetSystemLocale();
319         systemLocale.GetCountryName(currentRegion);
320
321         pKey = static_cast<String*>(__pSearchedText->GetAt(itemIndex));
322
323         StringTokenizer stringTokenizer(*pKey, L":");
324         stringTokenizer.GetNextToken(itemMainText);
325
326         TableViewItem* pItem = new (std::nothrow) TableViewItem();
327
328         if (itemMainText.Contains(currentRegion) == true)
329         {
330                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
331         }
332
333         ItemTypeOneLine(itemMainRectangle);
334         itemMainRectangle.width = itemWidth;
335
336         RelativeLayout relativeLayout;
337         relativeLayout.Construct();
338
339         pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_ITEM_DEFAULT), style);
340         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
341
342         pLabel = new (std::nothrow) Label();
343         pLabel->Construct(itemMainRectangle, itemMainText);
344         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
345         pLabel->SetTextConfig(FontSize, LABEL_TEXT_STYLE_NORMAL);
346         pLabel->SetTextColor(COLOR_MAIN_TEXT);
347
348         pItem->AddControl(pLabel);
349
350         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
351         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
352
353         return pItem;
354 }
355
356 bool
357 LocaleRegionForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
358 {
359         AppLogDebug("ENTER");
360
361         delete pItem;
362         pItem = null;
363
364         return true;
365 }
366
367 bool
368 LocaleRegionForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
369 {
370         AppLogDebug("ENTER");
371
372         delete pItem;
373         pItem = null;
374
375         return true;
376 }
377
378 void
379 LocaleRegionForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
380 {
381         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
382
383         SceneManager* pSceneManager = SceneManager::GetInstance();
384         AppAssert(pSceneManager);
385         bool checkStatus = false;
386         String* pKey = null;
387         String selectData;
388         String key = *(static_cast<String*>(__pSearchedText->GetAt(itemIndex)));
389         AppLogDebug("selected key [%ls]", key.GetPointer());
390
391         String noSearchResult = ResourceManager::GetString(L"IDS_COM_BODY_NO_SEARCH_RESULTS");
392
393         if (key.Equals(noSearchResult, true))
394         {
395                 AppLogDebug("selected key is \"No Search Result\"");
396                 return;
397         }
398
399         for (int count = 0; count < __pSearchedText->GetCount(); count++)
400         {
401                 if (count == itemIndex)
402                 {
403                         checkStatus = !checkStatus;
404
405                         pKey = (static_cast<String*>(__pSearchedText->GetAt(count)));
406
407                         StringTokenizer stringTokenizer(*pKey, L":");
408                         for (int i = 0; i <= stringTokenizer.GetTokenCount(); i++)
409                         {
410                                 stringTokenizer.GetNextToken(selectData);
411                         }
412                         if(selectData.CompareTo(__previousSelectedRegion) != 0)
413                         {
414                                 if (SettingInfo::SetValue(SETTING_INFO_KEY_LOCALE_COUNTRY, selectData) != E_SUCCESS)
415                                 {
416                                         AppLogDebug("SetValue: SETTING_INFO_KEY_LOCALE_COUNTRY failed [%s]", GetErrorMessage(GetLastResult()));
417                                 }
418                                 else
419                                 {
420                                         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
421                                 }
422                         }
423                         else
424                         {
425                                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
426                         }
427
428                         __previousSelectedRegion = selectData;
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 }