Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnDialPresentationModel.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
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 "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"
30 #include "PhnTypes.h"
31
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;
37
38 DialPresentationModel::DialPresentationModel(DialTabDialer& pParentPanel) : __pParentPanel(pParentPanel)
39 {
40         AppLogDebug("ENTER");
41         __pNumberSuggestionList = null;
42         __pSearchString = null;
43         __pSettingsManager = null;
44         __pAsyncSearchTask = null;
45         __pSearchThread = null;
46         __mutex.Create();
47         AppLogDebug("EXIT");
48 }
49
50 DialPresentationModel::~DialPresentationModel()
51 {
52         AppLogDebug("ENTER");
53         ClearSearchResults();
54         AppLogDebug("EXIT");
55 }
56
57 void
58 DialPresentationModel::ClearSearchResults(void)
59 {
60         if(__pNumberSuggestionList != null)
61         {
62                 __mutex.Acquire();
63                 __pNumberSuggestionList->RemoveAll();
64                 delete __pNumberSuggestionList;
65                 __pNumberSuggestionList = null;
66                 __mutex.Release();
67         }
68         if(__pSearchString != null)
69         {
70                 delete __pSearchString;
71                 __pSearchString = null;
72         }
73 }
74
75 bool
76 DialPresentationModel::FetchSuggestionList(const String& searchString)
77 {
78         bool newSearch = false;
79         if (__pSearchThread != null)
80         {
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;
88         }
89
90         result r = E_SUCCESS;
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))
94         {
95                 newSearch = false;
96                 __pSearchString->Clear();
97                 __pSearchString->Append(searchString);
98                 __mutex.Acquire();
99                 r = __pAsyncSearchTask->Construct(*__pSearchString, __pNumberSuggestionList);
100                 __mutex.Release();
101         }
102         else
103         {
104                 newSearch = true;
105                 __pSearchString = new (std::nothrow) String(searchString);
106                 r = __pAsyncSearchTask->Construct(*__pSearchString, null);
107         }
108         TryCatch(r == E_SUCCESS,,"Async Task construction failed");
109
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");
116
117         return newSearch;
118
119 CATCH:
120         //clear previous search results
121         ClearSearchResults();
122         delete __pAsyncSearchTask;
123         __pAsyncSearchTask = null;
124         delete __pSearchThread;
125         __pSearchThread = null;
126         return true;
127 }
128
129 void
130 DialPresentationModel::HandleContactsRetrievalCompleted(IList* pContactsList)
131 {
132         __mutex.Acquire();
133         if(__pNumberSuggestionList != null)
134         {
135                 __pNumberSuggestionList->RemoveAll();
136                 delete __pNumberSuggestionList;
137                 __pNumberSuggestionList = null;
138         }
139
140         if(pContactsList != null && pContactsList->GetCount() > 0)
141         {
142                 //Taking Ownership of List.
143                 __pNumberSuggestionList = static_cast<ArrayList*>(pContactsList);
144
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)
149                 {
150                         pDialInfo = static_cast<DialContactInfo*>(__pNumberSuggestionList->GetAt(0));
151                 }
152
153                 if(pDialInfo != null)
154                 {
155
156                         ArrayList* pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
157                         pArgs->Construct();
158
159                         //display name, mobile number
160                         String* argDisplayName = new (std::nothrow) String();
161                         argDisplayName->Append(pDialInfo->GetDisplayName());
162                         pArgs->Add(argDisplayName);
163
164                         String* argMobilNum = new (std::nothrow) String();
165                         argMobilNum->Append(pDialInfo->GetPhoneNumber());
166                         pArgs->Add(argMobilNum);
167
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);
173                 }
174         }
175         else
176         {
177                 //remove old search key
178                 if(__pSearchString != null)
179                 {
180                         delete __pSearchString;
181                         __pSearchString = null;
182                 }
183                 //Show "Add to contact"
184                 ArrayList* pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
185                 pArgs->Construct();
186                 __pParentPanel.SendUserEvent(REQUEST_SHOW_ADD_TO_CONTACTS,pArgs);
187                 //empty list
188                 delete pContactsList;
189                 pContactsList = null;
190         }
191         __mutex.Release();
192 }
193
194 DialContactInfo*
195 DialPresentationModel::GetSuggestionAtIndex(int index)
196 {
197         DialContactInfo* pDialInfo = null;
198         __mutex.Acquire();
199         //Fetch the contact's info at a given index
200         if (__pNumberSuggestionList != null
201                         && __pNumberSuggestionList->GetCount() > 0
202                         && (index >= 0 && index < __pNumberSuggestionList->GetCount()))
203         {
204                 pDialInfo = static_cast<DialContactInfo*>(__pNumberSuggestionList->GetAt(index));
205         }
206         __mutex.Release();
207         return pDialInfo;
208 }
209
210 int
211 DialPresentationModel::GetNumberOfSuggestions(void)
212 {
213         int resultCount = 0;
214         if(__pNumberSuggestionList != null)
215         {
216                 __mutex.Acquire();
217                 resultCount = __pNumberSuggestionList->GetCount();
218                 __mutex.Release();
219         }
220         return resultCount;
221 }
222
223 String*
224 DialPresentationModel::FetchSpeedDialNumberN(int rowId)
225 {
226         if(__pSettingsManager == null)
227         {
228                 __pSettingsManager = SettingsManager::GetInstance();
229         }
230         return __pSettingsManager->GetMappedSpeedDialNumberN(rowId);
231 }
232
233 String*
234 DialPresentationModel::FetchLatestNumberFromLogsN(void)
235 {
236         String* pRetString = CallLogManager::GetInstance()->GetLatestCallLogFromDbN();
237         return pRetString;
238 }
239
240 void
241 DialPresentationModel::AddContactsToAddressBook(String& contactNumber)
242 {
243         result r = E_SUCCESS;
244         HashMap extraData;
245         extraData.Construct();
246
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);
249         if (pAc != null)
250         {
251                 r = pAc->Start(null, null, &extraData, this);
252                 delete pAc;
253         }
254
255         extraData.RemoveAll(true);
256 }
257
258 void
259 DialPresentationModel::OpenMessagingAppControl(String& contactNumber)
260 {
261         //launch message
262         result r = E_SUCCESS;
263         HashMap extraData;
264         extraData.Construct();
265
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));
268
269         AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_MESSAGE, OPERATION_ID_COMPOSE);
270         if (pAc != null)
271         {
272                 r = pAc->Start(null, null, &extraData, this);
273                 delete pAc;
274         }
275         extraData.RemoveAll(true);
276 }
277
278 void
279 DialPresentationModel::OnAppControlCompleteResponseReceived(const AppId& appId, const Tizen::Base::String& operationId, AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
280 {
281         if (appId.Equals(String(PROVIDER_ID_CONTACTS)) && operationId.Equals(String(OPERATION_ID_ADD)))
282         {
283                 if (appControlResult == APP_CTRL_RESULT_SUCCEEDED)
284                 {
285                         //delete search string to start new search
286                         if(__pSearchString != null)
287                         {
288                                 delete __pSearchString;
289                                 __pSearchString = null;
290                         }
291                         //Fetch latest added contact and show in suggestion list.
292                         __pParentPanel.UpdateSearchResults();
293                 }
294         }
295
296 }