2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file PhnDialPresentationModel.cpp
19 * @brief Dial presentation model class
22 #include "PhnAppUtility.h"
23 #include "PhnCalllogManager.h"
24 #include "PhnCommonUtils.h"
25 #include "PhnDialContactInfo.h"
26 #include "PhnDialPresentationModel.h"
27 #include "PhnPhoneApp.h"
28 #include "PhnSettingsManager.h"
29 #include "PhnTabDialer.h"
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Social;
38 DialPresentationModel::DialPresentationModel(DialTabDialer& pParentPanel) : __pParentPanel(pParentPanel)
41 __pNumberSuggestionList = null;
42 __pSearchString = null;
43 __pSettingsManager = null;
44 __pAsyncSearchTask = null;
45 __pSearchThread = null;
50 DialPresentationModel::~DialPresentationModel()
58 DialPresentationModel::ClearSearchResults(void)
60 if(__pNumberSuggestionList != null)
63 __pNumberSuggestionList->RemoveAll();
64 delete __pNumberSuggestionList;
65 __pNumberSuggestionList = null;
68 if(__pSearchString != null)
70 delete __pSearchString;
71 __pSearchString = null;
76 DialPresentationModel::FetchSuggestionList(const String& searchString)
78 bool newSearch = false;
79 if (__pSearchThread != null)
81 //Wait until the previous thread finished execution.
82 //otherwise "start_thread()" crashes.
83 __pSearchThread->Join();
84 delete __pSearchThread;
85 __pSearchThread = null;
86 delete __pAsyncSearchTask;
87 __pAsyncSearchTask = null;
91 __pAsyncSearchTask = new (std::nothrow) RetrieveContactsListTask( *this);
92 //check if this search string is subset of previous search string
93 if(__pSearchString != null && searchString.StartsWith(*__pSearchString,0))
96 __pSearchString->Clear();
97 __pSearchString->Append(searchString);
99 r = __pAsyncSearchTask->Construct(*__pSearchString, __pNumberSuggestionList);
105 __pSearchString = new (std::nothrow) String(searchString);
106 r = __pAsyncSearchTask->Construct(*__pSearchString, null);
108 TryCatch(r == E_SUCCESS,,"Async Task construction failed");
110 //Start Async Task to search for contacts, call logs, Speed dial.
111 __pSearchThread = new (std::nothrow) Thread();
112 r = __pSearchThread->Construct(*__pAsyncSearchTask);
113 TryCatch(r == E_SUCCESS,,"Thread construction failed");
114 r = __pSearchThread->Start();
115 TryCatch(r == E_SUCCESS,,"Failed to start Thread");
120 //clear previous search results
121 ClearSearchResults();
122 delete __pAsyncSearchTask;
123 __pAsyncSearchTask = null;
124 delete __pSearchThread;
125 __pSearchThread = null;
130 DialPresentationModel::HandleContactsRetrievalCompleted(IList* pContactsList)
133 if(__pNumberSuggestionList != null)
135 __pNumberSuggestionList->RemoveAll();
136 delete __pNumberSuggestionList;
137 __pNumberSuggestionList = null;
140 if(pContactsList != null && pContactsList->GetCount() > 0)
142 //Taking Ownership of List.
143 __pNumberSuggestionList = static_cast<ArrayList*>(pContactsList);
145 //inform Tab Dialer to update view.
146 DialContactInfo* pDialInfo = null;
147 //Fetch the contact's info at a given index
148 if (__pNumberSuggestionList != null && __pNumberSuggestionList->GetCount() > 0)
150 pDialInfo = static_cast<DialContactInfo*>(__pNumberSuggestionList->GetAt(0));
153 if(pDialInfo != null)
156 ArrayList* pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
159 //display name, mobile number
160 String* argDisplayName = new (std::nothrow) String();
161 argDisplayName->Append(pDialInfo->GetDisplayName());
162 pArgs->Add(argDisplayName);
164 String* argMobilNum = new (std::nothrow) String();
165 argMobilNum->Append(pDialInfo->GetPhoneNumber());
166 pArgs->Add(argMobilNum);
168 //fetch total no. of results
169 String* argResCount = new (std::nothrow) String();
170 argResCount->Append(__pNumberSuggestionList->GetCount());
171 pArgs->Add(argResCount);
172 __pParentPanel.SendUserEvent(REQUEST_SHOW_SUGGESTION,pArgs);
177 //remove old search key
178 if(__pSearchString != null)
180 delete __pSearchString;
181 __pSearchString = null;
183 //Show "Add to contact"
184 ArrayList* pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
186 __pParentPanel.SendUserEvent(REQUEST_SHOW_ADD_TO_CONTACTS,pArgs);
188 delete pContactsList;
189 pContactsList = null;
195 DialPresentationModel::GetSuggestionAtIndex(int index)
197 DialContactInfo* pDialInfo = null;
199 //Fetch the contact's info at a given index
200 if (__pNumberSuggestionList != null
201 && __pNumberSuggestionList->GetCount() > 0
202 && (index >= 0 && index < __pNumberSuggestionList->GetCount()))
204 pDialInfo = static_cast<DialContactInfo*>(__pNumberSuggestionList->GetAt(index));
211 DialPresentationModel::GetNumberOfSuggestions(void)
214 if(__pNumberSuggestionList != null)
217 resultCount = __pNumberSuggestionList->GetCount();
224 DialPresentationModel::FetchSpeedDialNumberN(int rowId)
226 if(__pSettingsManager == null)
228 __pSettingsManager = SettingsManager::GetInstance();
230 return __pSettingsManager->GetMappedSpeedDialNumberN(rowId);
234 DialPresentationModel::FetchLatestNumberFromLogsN(void)
236 String* pRetString = CallLogManager::GetInstance()->GetLatestCallLogFromDbN();
241 DialPresentationModel::AddContactsToAddressBook(String& contactNumber)
243 result r = E_SUCCESS;
245 extraData.Construct();
247 extraData.Add(new (std::nothrow) String(INPUT_TYPE_PHONE), new (std::nothrow) String(contactNumber));
248 AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_CONTACTS, OPERATION_ID_ADD);
251 r = pAc->Start(null, null, &extraData, this);
255 extraData.RemoveAll(true);
259 DialPresentationModel::OpenMessagingAppControl(String& contactNumber)
262 result r = E_SUCCESS;
264 extraData.Construct();
266 extraData.Add(new (std::nothrow) String(MESSAGE_TYPE), new (std::nothrow) String(MESSAGE_SMS_TYPE));
267 extraData.Add(new (std::nothrow) String(MESSAGE_TO), new (std::nothrow) String(contactNumber));
269 AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_MESSAGE, OPERATION_ID_COMPOSE);
272 r = pAc->Start(null, null, &extraData, this);
275 extraData.RemoveAll(true);
279 DialPresentationModel::OnAppControlCompleteResponseReceived(const AppId& appId, const Tizen::Base::String& operationId, AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
281 if (appId.Equals(String(PROVIDER_ID_CONTACTS)) && operationId.Equals(String(OPERATION_ID_ADD)))
283 if (appControlResult == APP_CTRL_RESULT_SUCCEEDED)
285 //delete search string to start new search
286 if(__pSearchString != null)
288 delete __pSearchString;
289 __pSearchString = null;
291 //Fetch latest added contact and show in suggestion list.
292 __pParentPanel.UpdateSearchResults();