3e6b4733155397407b8db7bd1ba78cdcab46de80
[apps/osp/Dial.git] / src / PhnDialPresentationModel.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    PhnDialPresentationModel.cpp
19  * @brief       Dial presentation model class
20  */
21 #include <FSocial.h>
22 #include <FTelephony.h>
23 #include "PhnAppUtility.h"
24 #include "PhnCalllogManager.h"
25 #include "PhnCommonUtils.h"
26 #include "PhnDialContactInfo.h"
27 #include "PhnDialPresentationModel.h"
28 #include "PhnPhoneApp.h"
29 #include "PhnSettingsManager.h"
30 #include "PhnTabDialer.h"
31 #include "PhnTypes.h"
32
33 using namespace Tizen::App;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Social;
38 using namespace Tizen::Telephony;
39
40 DialPresentationModel::DialPresentationModel(DialTabDialer& parentPanel) : __parentDialerPanel(parentPanel)
41 {
42         AppLogDebug("ENTER");
43         __pNumberSuggestionList = null;
44         __pSearchString = null;
45         __pSettingsManager = null;
46         __pAsyncSearchTask = null;
47         __pSearchThread = null;
48         __mutex.Create();
49         AppLogDebug("EXIT");
50 }
51
52 DialPresentationModel::~DialPresentationModel()
53 {
54         AppLogDebug("ENTER");
55         ClearSearchResults();
56         AppLogDebug("EXIT");
57 }
58
59 void
60 DialPresentationModel::ClearSearchResults(void)
61 {
62         if(__pNumberSuggestionList != null)
63         {
64                 __mutex.Acquire();
65                 __pNumberSuggestionList->RemoveAll();
66                 delete __pNumberSuggestionList;
67                 __pNumberSuggestionList = null;
68                 __mutex.Release();
69         }
70         if(__pSearchString != null)
71         {
72                 delete __pSearchString;
73                 __pSearchString = null;
74         }
75 }
76
77 bool
78 DialPresentationModel::FetchSuggestionList(const String& searchString)
79 {
80         bool newSearch = false;
81         if (__pSearchThread != null)
82         {
83                 //Wait until the previous thread finished execution.
84                 //otherwise "start_thread()" crashes.
85                 __pSearchThread->Join();
86                 delete __pSearchThread;
87                 __pSearchThread = null;
88                 delete __pAsyncSearchTask;
89                 __pAsyncSearchTask = null;
90         }
91
92         result r = E_SUCCESS;
93         __pAsyncSearchTask = new (std::nothrow) RetrieveContactsListTask( *this);
94         //check if this search string is subset of previous search string
95         if(__pSearchString != null && searchString.StartsWith(*__pSearchString,0))
96         {
97                 newSearch = false;
98                 __pSearchString->Clear();
99                 __pSearchString->Append(searchString);
100                 __mutex.Acquire();
101                 r = __pAsyncSearchTask->Construct(*__pSearchString, __pNumberSuggestionList);
102                 __mutex.Release();
103         }
104         else
105         {
106                 newSearch = true;
107                 __pSearchString = new (std::nothrow) String(searchString);
108                 r = __pAsyncSearchTask->Construct(*__pSearchString, null);
109         }
110         TryCatch(r == E_SUCCESS,,"Async Task construction failed");
111
112         //Start Async Task  to search for contacts, call logs, Speed dial.
113         __pSearchThread = new (std::nothrow) Thread();
114         r = __pSearchThread->Construct(*__pAsyncSearchTask);
115         TryCatch(r == E_SUCCESS,,"Thread construction failed");
116         r = __pSearchThread->Start();
117         TryCatch(r == E_SUCCESS,,"Failed to start Thread");
118
119         return newSearch;
120
121 CATCH:
122         //clear previous search results
123         ClearSearchResults();
124         delete __pAsyncSearchTask;
125         __pAsyncSearchTask = null;
126         delete __pSearchThread;
127         __pSearchThread = null;
128         return true;
129 }
130
131 void
132 DialPresentationModel::HandleContactsRetrievalCompleted(IList* pContactsList)
133 {
134         __mutex.Acquire();
135         if(__pNumberSuggestionList != null)
136         {
137                 __pNumberSuggestionList->RemoveAll();
138                 delete __pNumberSuggestionList;
139                 __pNumberSuggestionList = null;
140         }
141
142         if(pContactsList != null && pContactsList->GetCount() > 0)
143         {
144                 //Taking Ownership of List.
145                 __pNumberSuggestionList = static_cast<ArrayList*>(pContactsList);
146
147                 //inform Tab Dialer to update view.
148                 DialContactInfo* pDialInfo = null;
149                 //Fetch the contact's info at a given index
150                 if (__pNumberSuggestionList != null && __pNumberSuggestionList->GetCount() > 0)
151                 {
152                         pDialInfo = static_cast<DialContactInfo*>(__pNumberSuggestionList->GetAt(0));
153                 }
154
155                 if(pDialInfo != null)
156                 {
157                         //send first search result
158                         ArrayList* pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
159                         pArgs->Construct();
160
161                         //Send display name, mobile number, matched search string and result count.
162                         String* argDisplayName = new (std::nothrow) String();
163                         argDisplayName->Append(pDialInfo->GetDisplayName());
164                         pArgs->Add(argDisplayName);
165
166                         String* argMobilNum = new (std::nothrow) String();
167                         argMobilNum->Append(pDialInfo->GetPhoneNumber());
168                         pArgs->Add(argMobilNum);
169
170                         String* argSearchString = new (std::nothrow) String();
171                         argSearchString->Append(*(pDialInfo->GetSearchKey()));
172                         pArgs->Add(argSearchString);
173
174                         //fetch total no. of results
175                         String* argResCount = new (std::nothrow) String();
176                         argResCount->Append(__pNumberSuggestionList->GetCount());
177                         pArgs->Add(argResCount);
178                         __parentDialerPanel.SendUserEvent(REQUEST_SHOW_SUGGESTION,pArgs);
179                 }
180         }
181         else
182         {
183                 //remove old search key
184                 if(__pSearchString != null)
185                 {
186                         delete __pSearchString;
187                         __pSearchString = null;
188                 }
189                 //Show "Add to contact"
190                 ArrayList* pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
191                 pArgs->Construct();
192                 __parentDialerPanel.SendUserEvent(REQUEST_SHOW_ADD_TO_CONTACTS,pArgs);
193                 //empty list
194                 delete pContactsList;
195                 pContactsList = null;
196         }
197         __mutex.Release();
198 }
199
200 DialContactInfo*
201 DialPresentationModel::GetSuggestionAtIndex(int index)
202 {
203         DialContactInfo* pDialInfo = null;
204         __mutex.Acquire();
205         //Fetch the contact's info at a given index
206         if (__pNumberSuggestionList != null
207                         && __pNumberSuggestionList->GetCount() > 0
208                         && (index >= 0 && index < __pNumberSuggestionList->GetCount()))
209         {
210                 pDialInfo = static_cast<DialContactInfo*>(__pNumberSuggestionList->GetAt(index));
211         }
212         __mutex.Release();
213         return pDialInfo;
214 }
215
216 int
217 DialPresentationModel::GetNumberOfSuggestions(void)
218 {
219         int resultCount = 0;
220         if(__pNumberSuggestionList != null)
221         {
222                 __mutex.Acquire();
223                 resultCount = __pNumberSuggestionList->GetCount();
224                 __mutex.Release();
225         }
226         return resultCount;
227 }
228
229 String*
230 DialPresentationModel::FetchSpeedDialNumberN(int rowId)
231 {
232         if(__pSettingsManager == null)
233         {
234                 __pSettingsManager = SettingsManager::GetInstance();
235         }
236         return __pSettingsManager->GetMappedSpeedDialNumberN(rowId);
237 }
238
239 String*
240 DialPresentationModel::FetchLatestNumberFromLogsN(void)
241 {
242         String* pRetString = CallLogManager::GetInstance()->GetLatestCallLogFromDbN();
243         return pRetString;
244 }
245
246 result
247 DialPresentationModel::LaunchAddContactsAppControl(String& contactNumber)
248 {
249         result r = E_FAILURE;
250         HashMap extraData;
251         extraData.Construct();
252
253         extraData.Add(new (std::nothrow) String(INPUT_TYPE_PHONE), new (std::nothrow) String(contactNumber));
254         extraData.Add(new (std::nothrow) String(CONTACT_KEY_ITEM_TYPE), new (std::nothrow) String(ITEM_TYPE_CONTACT));
255         AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_CONTACTS, OPERATION_ID_ADD);
256         if (pAc != null)
257         {
258                 r = pAc->Start(null, null, &extraData, this);
259                 delete pAc;
260         }
261
262         extraData.RemoveAll(true);
263         return r;
264 }
265
266 ErrorCodes
267 DialPresentationModel::DialCall(const String& contactNumber)
268 {
269         AppLogDebug("ENTER");
270         result r = E_FAILURE;
271         r = LaunchCallAppControl(contactNumber);
272         if (r == E_SUCCESS)
273         {
274                 return ERROR_NONE;
275         }
276         else
277         {
278                 return ERROR_DIAL_FAILED;
279         }
280 }
281
282 result
283 DialPresentationModel::LaunchCallAppControl(const String& contactNumber)
284 {
285         result r = E_FAILURE;
286
287         HashMap extraData;
288         extraData.Construct();
289         extraData.Add(new (std::nothrow) String(PARAM_PHONE_NUMBER), new (std::nothrow) String(contactNumber));
290         extraData.Add(new (std::nothrow) String(PARAM_CALL_TYPE), new (std::nothrow) String(PARAM_CALL_VALUE_VOICE));
291
292         AppControl* pAppControl = AppManager::FindAppControlN(PROVIDER_ID_CALL, OPERATION_ID_CALL);
293         if (pAppControl != null)
294         {
295                 r = pAppControl->Start(null, null, &extraData, this);
296                 delete pAppControl;
297         }
298
299         extraData.RemoveAll(true);
300         return r;
301 }
302
303 result
304 DialPresentationModel::OpenMessagingAppControl(String& contactNumber)
305 {
306         //launch message
307         result r = E_SUCCESS;
308         HashMap extraData;
309         extraData.Construct();
310
311         extraData.Add(new (std::nothrow) String(MESSAGE_TYPE), new (std::nothrow) String(MESSAGE_SMS_TYPE));
312         extraData.Add(new (std::nothrow) String(MESSAGE_TO), new (std::nothrow) String(contactNumber));
313
314         AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_MESSAGE, OPERATION_ID_COMPOSE);
315         if (pAc != null)
316         {
317                 r = pAc->Start(null, null, &extraData, this);
318                 delete pAc;
319         }
320         extraData.RemoveAll(true);
321
322         return r;
323 }
324
325 void
326 DialPresentationModel::OnAppControlCompleteResponseReceived(const AppId& appId, const String& operationId, AppCtrlResult appControlResult, const IMap* pExtraData)
327 {
328         //reset 'isAppControlLaunched'
329         AppLogDebug("Enter");
330 //      __parentDialerPanel.SetAppControlLaunched(false);
331
332         if (appId.Equals(String(PROVIDER_ID_CONTACTS)) && operationId.Equals(String(OPERATION_ID_ADD)))
333         {
334                 if (appControlResult == APP_CTRL_RESULT_SUCCEEDED)
335                 {
336                         //delete search string to start new search
337                         if(__pSearchString != null)
338                         {
339                                 delete __pSearchString;
340                                 __pSearchString = null;
341                         }
342                         //Fetch latest added contact and show in suggestion list.
343                         __parentDialerPanel.UpdateSearchResults();
344                 }
345         }
346 }