3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file CityListForm.cpp
20 * @brief Used to define CityListForm
29 #include "ClkAlarmPresentationModel.h"
30 #include "ClkAlarmRepeatCustomItem.h"
31 #include "ClkCityListForm.h"
32 #include "ClkClockApp.h"
33 #include "ClkCommonLib.h"
34 #include "ClkCityListCustomItem.h"
35 #include "ClkSceneRegister.h"
36 #include "ClkWorldClockPresentationModel.h"
38 using namespace Tizen::App;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Base::Runtime;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::Locales;
44 using namespace Tizen::Ui;
45 using namespace Tizen::Ui::Controls;
46 using namespace Tizen::Ui::Scenes;
47 using namespace Tizen::System;
49 const RequestId REQUEST_ID_PROCESSING = 10000;
50 const wchar_t* NO_CITYS = L"No Alarms";
52 static const int IDA_FOOTER_NAME_CLICKED = 101;
53 static const int IDA_FOOTER_TIME_CLICKED = 102;
54 static const int IDA_SEARCH_CANCEL_CLICKED = 103;
56 NameComparer::NameComparer(void)
61 NameComparer::~NameComparer(void)
67 NameComparer::Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp)const
70 TimeZone *pTz1 = dynamic_cast<TimeZone*>(const_cast<Object*>(&obj1));
71 TimeZone *pTz2 = dynamic_cast<TimeZone*>(const_cast<Object*>(&obj2));
76 int separatorIndex = 0;
79 if(pTz1 == null || pTz2 == null)
84 pTz1->GetId().IndexOf(L"/", 0, separatorIndex);
85 pTz1->GetId().SubString(separatorIndex + 1, cityNameTz1);
86 pTz1->GetId().SubString(0, separatorIndex, areaNameTz1);
88 pTz2->GetId().IndexOf(L"/", 0, separatorIndex);
89 pTz2->GetId().SubString(separatorIndex + 1, cityNameTz2);
90 pTz2->GetId().SubString(0, separatorIndex, areaNameTz2);
92 cmp = cityNameTz1.CompareTo(cityNameTz2);
96 TimeComparer::TimeComparer(void)
100 TimeComparer::~TimeComparer(void)
106 TimeComparer::Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp)const
108 result r = E_SUCCESS;
109 TimeZone *pTz1 = dynamic_cast<TimeZone*>(const_cast<Object*>(&obj1));
110 TimeZone *pTz2 = dynamic_cast<TimeZone*>(const_cast<Object*>(&obj2));
112 if(pTz1 == null || pTz2 == null)
117 cmp = pTz1->GetRawOffset() - pTz2->GetRawOffset();
121 CityListForm::CityListForm()
122 : __isOperationCompleted(true)
123 , __isProcessing(false)
124 , __modalMsgBoxResult(0)
127 , __pNameListview(null)
129 , __pTimeListview(null)
137 CityListForm::~CityListForm(void)
139 if ( __pTimer != null)
145 if (__pMsgBox != null)
153 CityListForm::Initialize(void)
155 Construct(L"IDL_CITY_LIST");
161 CityListForm::OnInitializing(void)
163 result r = E_SUCCESS;
164 String fastScrollIndex = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
165 String fastTimeScrollIndex = L"13121110987654321";
166 GetFooter()->SetItemSelected(0);
167 GetFooter()->AddActionEventListener(*this);
168 GetHeader()->SetTitleText(CommonUtil::GetString("IDS_WCL_HEADER_SELECT_TIME_ZONE"));
170 SetFormBackEventListener(this);
171 AddOrientationEventListener(*this);
173 SceneManager::GetInstance()->AddSceneEventListener(IDSCN_CITY_LIST, *this);
175 ClockApp* pClock = static_cast<ClockApp*>(ClockApp::GetInstance());
176 int heightOffset = pClock->GetHeightOffset();
178 __pSearchBar = dynamic_cast<SearchBar*>(GetControl(L"IDC_CITY_SEARCHBAR"));
180 if(__pSearchBar != null)
182 __pSearchBar->AddActionEventListener(*this);
183 __pSearchBar->AddSearchBarEventListener(*this);
184 __pSearchBar->AddKeypadEventListener(*this);
185 __pSearchBar->AddTextEventListener(*this);
186 __pSearchBar->AddFocusEventListener(*this);
189 __pNameListview = dynamic_cast<ListView*>(GetControl(L"IDC_CITY_LISTVIEW"));
191 if(__pNameListview == null)
195 __pNameListview->SetItemProvider(*this);
196 __pNameListview->AddListViewItemEventListener(*this);
197 __pNameListview->SetFastScrollIndex(fastScrollIndex, false);
198 __pNameListview->AddFastScrollListener(*this);
201 __pTimeListview = new ListView();
203 if(__pTimeListview == null)
208 __pTimeListview->Construct(Rectangle(__pNameListview->GetX(), __pSearchBar->GetY() + __pSearchBar->GetHeight() + heightOffset, __pNameListview->GetWidth(), GetClientAreaBounds().height - __pSearchBar->GetHeight()- heightOffset), true, SCROLL_STYLE_FADE_OUT);
209 __pTimeListview->SetItemProvider(*this);
210 __pTimeListview->AddListViewItemEventListener(*this);
211 __pTimeListview->SetShowState(false);
212 __pTimeListview->SetTextOfEmptyList(CommonUtil::GetString(L"IDS_COM_BODY_NO_SEARCH_RESULTS"));
213 AddControl(__pTimeListview);
215 __pTimer = new (std::nothrow) Timer();
217 if (__pTimer == null)
222 r = __pTimer->Construct(*this);
228 CityListForm::OnTerminating(void)
230 __isOperationCompleted = true;
235 CityListForm::OnActionPerformed(const Control& source, int actionId)
237 AppLogDebug("CityListForm::OnActionPerformed actionId = %d",actionId);
238 String fastScrollIndexName = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
242 case IDA_SEARCH_CANCEL_CLICKED:
246 __pSearchBar->HideKeypad();
247 __pSearchBar->Clear();
249 if(__pNameListview->GetShowState())
251 __pNameListview->SetEnabled(true);
252 __pNameListview->UpdateList();
256 __pTimeListview->SetEnabled(true);
257 __pTimeListview->UpdateList();
263 case IDA_FOOTER_NAME_CLICKED:
265 __pTimeListview->SetShowState(false);
266 __pNameListview->SetShowState(true);
270 __pNameListview->UpdateList();
273 if(__pSearchBar->GetMode() == SEARCH_BAR_MODE_INPUT)
275 if (__searchText != "")
277 __pNameListview->SetEnabled(true);
279 __pSearchBar->SetMode(SEARCH_BAR_MODE_INPUT);
283 __pNameListview->SetEnabled(true);
284 __pSearchBar->SetMode(SEARCH_BAR_MODE_NORMAL);
286 __pNameListview->SetFastScrollIndex(fastScrollIndexName, false);
291 case IDA_FOOTER_TIME_CLICKED:
293 __pTimeListview->SetBounds(__pTimeListview->GetX(), __pSearchBar->GetY() + __pSearchBar->GetHeight(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
295 __pNameListview->SetShowState(false);
296 __pTimeListview->SetShowState(true);
300 __pTimeListview->UpdateList();
303 if(__pSearchBar->GetMode() == SEARCH_BAR_MODE_INPUT)
305 if (__searchText != "")
307 __pTimeListview->SetEnabled(true);
309 __pSearchBar->SetMode(SEARCH_BAR_MODE_INPUT);
313 __pTimeListview->SetEnabled(true);
314 __pSearchBar->SetMode(SEARCH_BAR_MODE_NORMAL);
316 //__pTimeListview->SetFastScrollIndex(fastScrollIndexTime, false);
330 CityListForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
332 String fontSizeStr = "";
334 SettingInfo::GetValue("http://tizen.org/setting/font.size", fontSizeStr);
336 if(fontSizeStr == "medium")
340 else if(fontSizeStr == "huge")
345 else if(fontSizeStr == "giant")
349 else if(fontSizeStr == "small")
353 else if(fontSizeStr == "large")
363 __pTimeListview->SetBounds(__pTimeListview->GetX(), __pSearchBar->GetY() + __pSearchBar->GetHeight(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
369 CityListForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
375 CityListForm::OnFormBackRequested(Form& source)
377 AppLog("CityListForm::OnFormBackRequested");
379 if (__pSearchBar->GetMode() == SEARCH_BAR_MODE_NORMAL)
381 SceneManager::GetInstance()->GoBackward(BackwardSceneTransition());
385 __pSearchBar->SetMode(SEARCH_BAR_MODE_NORMAL);
391 CityListForm::CreateItem(int index, int itemWidth)
393 AppLog("CityListForm::CreateItem");
394 int itemHeight = 124;
396 CityListCustomItem *pItem = new CityListCustomItem();
399 String gmtValue = L"GMT ";
400 AppLog("before cast index is %d", index);
401 TimeZone *pTimeZone = dynamic_cast<TimeZone*>(__pDataList->GetAt(index));
402 bool searchBarModeOn = false;
404 String fontSizeStr = "";
406 SettingInfo::GetValue("http://tizen.org/setting/font.size", fontSizeStr);
408 if(fontSizeStr == "medium")
412 else if(fontSizeStr == "huge")
417 else if(fontSizeStr == "giant")
421 else if(fontSizeStr == "small")
425 else if(fontSizeStr == "large")
434 itemHeight += fontSize-44;
436 if ( pTimeZone == NULL)
446 AppLog("after cast");
447 int offset = pTimeZone->GetRawOffset();
449 if(__pSearchBar->GetMode() == SEARCH_BAR_MODE_INPUT)
450 searchBarModeOn = true;
452 searchBarModeOn = false;
455 result r = pItem->Construct(GetClientAreaBounds().width, itemHeight, searchBarModeOn);
456 AppLog("the result: %s",GetErrorMessage(r));
457 AppLog("Timezoneid: %ls", pTimeZone->GetId().GetPointer());
458 pTimeZone->GetId().IndexOf(L"/", 0, separatorIndex);
459 pTimeZone->GetId().SubString(separatorIndex + 1, cityName);
460 pTimeZone->GetId().SubString(0, separatorIndex, areaName);
462 cityName.Replace("_"," ");
463 areaName.Replace("_"," ");
466 gmtValue.Append("+");
470 gmtValue.Append("-");
474 gmtValue.Append(offset/60);
478 gmtValue.Append(':');
479 gmtValue.Append(offset%60);
482 pItem->SetText(cityName);
483 pItem->SetSubText(areaName);
484 pItem->SetRightSubText(gmtValue);
485 pItem->Make(__searchText,fontSize);
487 AppLog("CityListForm::CreateItem ending ");
492 CityListForm::DeleteItem(int index, ListItemBase *pItem, int itemWidth)
500 CityListForm::GetItemCount(void)
502 AppLog("CityListForm::GetItemCount");
505 __pDataList->RemoveAll(true);
508 __pDataList = WorldClockPresentationModel::GetInstance()->GetCityList(__searchText);
510 if(GetFooter()->GetSelectedItemIndex() == 0)
512 __pDataList->Sort(nameComparer);
516 __pDataList->Sort(timeComparer);
519 AppLog("SearchText : %ls, Count : %d",__searchText.GetPointer(), __pDataList->GetCount());
520 return __pDataList->GetCount();
524 CityListForm::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus status)
530 CityListForm::OnListViewItemLongPressed(ListView& listView, int index, int elementId, bool& invokeListViewItemCallback)
536 CityListForm::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
538 result r = E_FAILURE;
540 ArrayList worldClockList;
541 WorldClock* pWorldClock = null ;
544 WorldClockPresentationModel* pWorldClockPresentationModel = WorldClockPresentationModel::GetInstance();
545 pWorldClockPresentationModel->GetAddedClockCount(count);
547 // Only 20 cities allowed
550 String msg = CommonUtil::GetString(L"IDS_WCL_POP_UNABLE_TO_ADD_CITY_WORLD_CLOCK_LIST_FULL_DELETE_SOME_ITEMS_FIRST");
551 if(__isOperationCompleted == true)
558 pWorldClockPresentationModel->GetAddedWorldClock(0,count,worldClockList);
562 TimeZone *pTimeZone = dynamic_cast<TimeZone*>(__pDataList->GetAt(index));
564 if ( pTimeZone != null )
566 AppLog("CityListForm::OnListViewItemStateChanged 1");
568 pTimeZone->GetId().IndexOf(L"/", 0, separatorIndex);
569 pTimeZone->GetId().SubString(separatorIndex + 1, cityName);
571 pTimeZone->GetId().SubString(0, separatorIndex, areaName);
572 int offset = pTimeZone->GetRawOffset();
574 for(int index = 0 ; index < count ; index ++)
576 pWorldClock = dynamic_cast< WorldClock* >(worldClockList.GetAt(index));
578 if( pWorldClock == NULL)
583 AppLog("CityListForm::OnListViewItemStateChanged 2");
584 city = pWorldClock->GetWorldClockCity();
588 if( city.Equals(cityName,false))
590 String msg = CommonUtil::GetString(L"IDS_BODY_WORLD_CLOCK_EXIST");
591 if(__isOperationCompleted == true)
598 AppLog("CityListForm::OnListViewItemStateChanged 3 offset = %d", offset);
600 wc.SetWorldClockCity(cityName);
601 wc.SetWorldClockCountry(areaName);
602 wc.SetTimeZone(offset);
605 AppLog("cityName = %ls areaName = %ls offset = %d", cityName.GetPointer(), areaName.GetPointer(), offset);
607 r = pWorldClockPresentationModel->AddWorldClock(wc);
608 AppLog("the result is %s", GetErrorMessage(r));
609 if (r == E_STORAGE_FULL || r == E_INIT_FAILED || r == E_OUT_OF_MEMORY)
612 String msg = CommonUtil::GetString("IDS_MEMORY_FULL");
613 r = msgBox.Construct(L"", msg, MSGBOX_STYLE_NONE, 3000);
614 AppLog("the result is %s", GetErrorMessage(r));
615 msgBox.ShowAndWait(__modalMsgBoxResult);
618 __isOperationCompleted = false;
619 SceneManager::GetInstance()->GoBackward(BackwardSceneTransition());
621 worldClockList.RemoveAll(true);
625 CityListForm::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
631 CityListForm::OnSearchBarModeChanged(Tizen::Ui::Controls::SearchBar& source, SearchBarMode mode)
633 if(mode == SEARCH_BAR_MODE_NORMAL)
637 __pNameListview->SetEnabled(true);
638 __pTimeListview->SetEnabled(true);
639 if(__pNameListview->GetShowState())
641 __pNameListview->UpdateList();
645 __pTimeListview->UpdateList();
650 __pNameListview->SetEnabled(false);
651 __pTimeListview->SetEnabled(false);
652 if(__pNameListview->GetShowState())
654 __pNameListview->UpdateList();
658 __pTimeListview->UpdateList();
664 CityListForm::OnKeypadWillOpen(Tizen::Ui::Control& source)
667 if (GetFooter() != null)
669 GetFooter()->SetShowState(true);
670 GetFooter()->Invalidate(true);
673 if(__pTimeListview->GetShowState())
675 __pTimeListview->SetBounds(__pTimeListview->GetX(), __pTimeListview->GetY(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
678 if(__pNameListview->GetShowState())
680 __pNameListview->SetBounds(__pNameListview->GetX(), __pNameListview->GetY(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
686 CityListForm::OnKeypadOpened(Tizen::Ui::Control& source)
689 GetFooter()->SetShowState(true);
690 GetFooter()->Invalidate(true);
692 __pNameListview->SetBounds(__pNameListview->GetX(), __pNameListview->GetY(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
693 __pTimeListview->SetBounds(__pTimeListview->GetX(), __pTimeListview->GetY(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
698 CityListForm::OnKeypadClosed(Tizen::Ui::Control& source)
700 __pNameListview->SetBounds(__pNameListview->GetX(), __pNameListview->GetY(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
701 __pTimeListview->SetBounds(__pTimeListview->GetX(), __pTimeListview->GetY(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
707 CityListForm::OnKeypadBoundsChanged(Tizen::Ui::Control& source)
709 __pNameListview->SetBounds(__pNameListview->GetX(), __pNameListview->GetY(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
710 __pTimeListview->SetBounds(__pTimeListview->GetX(), __pTimeListview->GetY(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
716 CityListForm::OnKeypadActionPerformed(Tizen::Ui::Control& source, Tizen::Ui::KeypadAction keypadAction)
718 if(keypadAction == KEYPAD_ACTION_SEARCH)
720 __searchText = __pSearchBar->GetText();
722 if(__pNameListview->GetShowState() == true)
724 __pNameListview->UpdateList();
728 __pTimeListview->UpdateList();
731 __pSearchBar->HideKeypad();
733 if(__searchText == L"")
736 __pNameListview->SetEnabled(false);
737 __pTimeListview->SetEnabled(false);
746 CityListForm::OnFastScrollIndexSelected(Tizen::Ui::Control& source, Tizen::Base::String& index)
748 AppLog("OnFastScrollIndexSelected : %ls", index.GetPointer());
749 int orderby = GetFooter()->GetSelectedItemIndex();
752 if(__pDataList == null)
756 maxCount = __pDataList->GetCount();
758 for(int count = 0; count < maxCount; count++)
760 TimeZone *pTimeZone = dynamic_cast<TimeZone*>(__pDataList->GetAt(count));
762 if ( pTimeZone == NULL)
770 pTimeZone->GetId().IndexOf(L"/", 0, separatorIndex);
772 pTimeZone->GetId().SubString(separatorIndex + 1, cityName);
774 if( cityName.StartsWith(index, 0) == true)
776 __pNameListview->ScrollToItem(count);
777 __pNameListview->Invalidate(false);
785 CityListForm::CreateMessage(String& str)
787 int modalMsgBoxResult = 0;
789 if (__pMsgBox != null)
795 __pMsgBox = new(std::nothrow) MessageBox;
797 if (__pMsgBox != null)
799 __pMsgBox->Construct(L"", str, MSGBOX_STYLE_OK, 3000);
802 if (__pMsgBox != null)
804 __pMsgBox->ShowAndWait(modalMsgBoxResult);
806 if (__pMsgBox != null)
818 CityListForm::OnTextValueChanged(const Control& source)
820 AppLog("CityListForm::OnTextValueChanged");
822 __searchText = __pSearchBar->GetText();
824 if (__searchText == "")
826 __pNameListview->SetEnabled(false);
827 __pNameListview->UpdateList();
829 __pTimeListview->SetEnabled(false);
830 __pTimeListview->UpdateList();
834 if (__isProcessing == false)
843 AppLog("CityListForm::OnTextValueChanged end");
847 CityListForm::OnOrientationChanged(const Control& source, OrientationStatus orientationStatus)
849 if (GetFooter() != null)
851 GetFooter()->SetShowState(true);
852 GetFooter()->Invalidate(true);
855 if(__pNameListview->GetShowState())
857 __pNameListview->UpdateList();
861 __pTimeListview->UpdateList();
864 String fontSizeStr = "";
866 SettingInfo::GetValue("http://tizen.org/setting/font.size", fontSizeStr);
868 if(fontSizeStr == "medium")
872 else if(fontSizeStr == "huge")
877 else if(fontSizeStr == "giant")
881 else if(fontSizeStr == "small")
885 else if(fontSizeStr == "large")
894 __pTimeListview->SetBounds(__pTimeListview->GetX(), __pSearchBar->GetY() + __pSearchBar->GetHeight(), GetClientAreaBounds().width, GetClientAreaBounds().height - __pSearchBar->GetHeight());
899 CityListForm::OnFocusGained(const Tizen::Ui::Control& source)
901 if (__searchText == "")
903 __pTimeListview->SetEnabled(false);
904 __pNameListview->SetEnabled(false);
909 CityListForm::OnFocusLost(const Tizen::Ui::Control& source)
911 __pTimeListview->SetEnabled(true);
912 __pNameListview->SetEnabled(true);
916 CityListForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
918 __isProcessing = true;
919 AppLog("CityListForm::OnUserEventReceivedN");
921 if (requestId == REQUEST_ID_PROCESSING)
923 if (GetFooter()->GetShowState() == false)
925 GetFooter()->SetShowState(true);
926 GetFooter()->Invalidate(true);
929 __searchText = __pSearchBar->GetText();
931 if (__searchText == "")
933 __pNameListview->SetEnabled(false);
934 __pNameListview->UpdateList();
936 __pTimeListview->SetEnabled(false);
937 __pTimeListview->UpdateList();
941 __pNameListview->SetEnabled(true);
942 __pNameListview->UpdateList();
944 __pTimeListview->SetEnabled(true);
945 __pTimeListview->UpdateList();
949 AppLog("CityListForm::OnUserEventReceivedN end");
950 __isProcessing = false;
954 CityListForm::OnTimerExpired(Timer& timer)
956 SendUserEvent(REQUEST_ID_PROCESSING, null);