Fix for N_SE-47660
[apps/osp/Call.git] / src / CalllogManager.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        CalllogManager.cpp
19  * @brief       This class provides call log APIs
20  */
21 #include <FLocales.h>
22 #include "CallApp.h"
23 #include "CalllogManager.h"
24
25 using namespace Tizen::Base;
26 using namespace Tizen::Base::Collection;
27 using namespace Tizen::Base::Utility;
28 using namespace Tizen::System;
29 using namespace Tizen::Locales;
30
31 CallLogManager* CallLogManager::__pCallogManager = null;
32
33 CallLogManager::CallLogManager(void)
34 {
35
36         pAddressBook = AddressbookManager::GetInstance()->GetAddressbookN();
37 }
38
39 CallLogManager::~CallLogManager(void)
40 {
41         __pCallogManager = null;
42 }
43
44 CallLogManager*
45 CallLogManager::GetInstance(void)
46 {
47         if(__pCallogManager == null)
48         {
49                 CreateInstance();
50         }
51         return __pCallogManager;
52 }
53
54 void
55 CallLogManager::CreateInstance(void)
56 {
57         __pCallogManager = new CallLogManager();
58         result r = __pCallogManager->Construct();
59         if(IsFailed(r))
60         {
61                 delete __pCallogManager;
62                 __pCallogManager = null;
63                 return;
64         }
65         std::atexit(DestroyInstance);
66 }
67
68 result
69 CallLogManager::Construct(void)
70 {
71         int ret = contacts_connect2();
72         if(ret != 0)
73         {
74                 return E_FAILURE;
75         }
76         return E_SUCCESS;
77
78 }
79
80 void
81 CallLogManager::DestroyInstance(void)
82 {
83         contacts_disconnect2();
84         delete __pCallogManager;
85 }
86
87 result
88 CallLogManager::AddCallogInfoToDatabase(AppCallInfo* calllogInfo)
89 {
90         AppLog(" AddVoiceCallInfo Entry");
91         int duration = 0;
92         long long startTime;
93
94         if(calllogInfo != null)
95         {
96                 CallLogType callLogType = calllogInfo->GetCalllogType();
97                 contacts_phone_log_type_e logType = CONTACTS_PLOG_TYPE_NONE;
98                 switch (callLogType)
99                 {
100                 case CALL_LOG_TYPE_VOICE_INCOMING:
101                         logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING;
102                         break;
103                 case CALL_LOG_TYPE_VOICE_OUTGOING:
104                         logType = CONTACTS_PLOG_TYPE_VOICE_OUTGOING;
105                         break;
106                 case CALL_LOG_TYPE_VOICE_MISSED_UNSEEN:
107                         logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN;
108                         break;
109                 case CALL_LOG_TYPE_VOICE_REJECTED:
110                         logType = CONTACTS_PLOG_TYPE_VOICE_REJECT;
111                         break;
112                 case CALL_LOG_TYPE_VOICE_BLOCKED:
113                         logType = CONTACTS_PLOG_TYPE_VOICE_BLOCKED;
114                         break;
115                 default:
116                         break;
117                 }
118                 startTime = calllogInfo->GetCallNotificationTime();
119                 if ((logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN)
120                         || (logType == CONTACTS_PLOG_TYPE_VOICE_REJECT)
121                         || (logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED))
122                 {
123                         duration = 0;
124                 }
125                 else
126                 {
127                         //start time is in miliseconds . so convert to seconds and set it to time_t format.
128                         long long connectTime = calllogInfo->GetDurationTime();
129                         if(connectTime == 0 || connectTime < 0)
130                         {
131                                 //connectTime = time(null);
132                                 CallApp* pPhoneApp = static_cast<CallApp*>(CallApp::GetInstance());
133                                 connectTime = pPhoneApp->GetCallAppTicks();
134                                 connectTime /=1000;
135                         }
136                         else
137                         {
138                                 connectTime = calllogInfo->GetDurationTime()/ 1000;
139                         }
140                         duration = (int)GetDuration(connectTime);
141                 }
142
143                 String PhNumber(L"");
144                 ByteBuffer* pByteBuffer = null;
145
146                 if(calllogInfo->GetContactNumber().IsEmpty() == false)
147                 {
148                         PhNumber.Append(calllogInfo->GetContactNumber());
149                         pByteBuffer = StringUtil::StringToUtf8N(PhNumber);
150                 }
151
152
153                 contacts_record_h hContactLog=0;
154                 int ret = contacts_record_create(_contacts_phone_log._uri,&hContactLog);
155                 if (ret != 0)
156                 {
157                         return E_FAILURE;
158                 }
159                 int id;
160
161                 int personid = GetPersonId(PhNumber);
162                 if (personid != -1)
163                 {
164                         if(contacts_record_set_int(hContactLog,_contacts_phone_log.person_id,personid) != CONTACTS_ERROR_NONE)
165                         {
166                                 AppLog(" calllog_set_type personid failed");
167                         }
168                 }
169
170                 if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_type,logType) != CONTACTS_ERROR_NONE)
171                 {
172                         AppLog(" calllog_set_type is failed");
173                 }
174                 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_time,startTime/1000) != CONTACTS_ERROR_NONE)
175                 {
176                         AppLog(" calllog_set_time is failed");
177                 }
178                 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.extra_data1,duration) != CONTACTS_ERROR_NONE)
179                 {
180                         AppLog(" calllog_set_duration is failed");
181                 }
182                 else if ((pByteBuffer != null) && (contacts_record_set_str(hContactLog,_contacts_phone_log.address,(const char*) pByteBuffer->GetPointer()) != CONTACTS_ERROR_NONE))
183                 {
184                         AppLog(" calllog_set_number is failed");
185                 }
186                 else if (contacts_db_insert_record(hContactLog,&id) != CONTACTS_ERROR_NONE)
187                 {
188                         AppLog(" calllog_insert_to_db is failed");
189                 }
190                 else
191                 {
192                         AppLog(" Call log is added successfully");
193                 }
194
195         }
196         AppLog(" AddVoiceCallInfo Exit");
197         return E_SUCCESS;
198 }
199
200 unsigned long
201 CallLogManager::GetDuration(long long start_time)
202 {
203         long long curr_time;
204         unsigned long call_duration_in_sec = 50;
205 //      curr_time = time(0);
206         CallApp* pPhoneApp = static_cast<CallApp*>(CallApp::GetInstance());
207         curr_time = pPhoneApp->GetCallAppTicks();
208         curr_time /=1000;
209         /*if(start_time != 0)
210         {
211                 start_time /= 1000;
212         }*/
213         call_duration_in_sec = curr_time - start_time;
214         return call_duration_in_sec;
215 }
216 int
217 CallLogManager::GetPersonId(Tizen::Base::String& phoneNumber)
218 {
219         AppLogDebug("Enter");
220         int personId = -1;
221         bool bContactFound = false;
222         if (pAddressBook != null)
223         {
224                 IList* pNumberContactsList = pAddressBook->SearchContactsByPhoneNumberN(phoneNumber);
225                 if (pNumberContactsList == null || IsFailed(GetLastResult()))
226                 {
227                         return -1;
228                 }
229
230                 //Fetch the contact's info to be displayed
231                 IEnumerator* pContactEnum = pNumberContactsList->GetEnumeratorN();
232                 if(pNumberContactsList != null)
233                 {
234                         while (E_SUCCESS == pContactEnum->MoveNext())
235                         {
236                                 Contact* pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
237
238                                 IList* pPhoneNumberList = pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
239                                 if (pPhoneNumberList != null)
240                                 {
241                                         IEnumerator* pPhoneEnum = pPhoneNumberList->GetEnumeratorN();
242                                         while (E_SUCCESS == pPhoneEnum->MoveNext())
243                                         {
244                                                 PhoneNumber* pPhoneNumber = (PhoneNumber*) pPhoneEnum->GetCurrent();
245                                                 //Check if this is the correct contact
246                                                 if (pPhoneNumber->GetPhoneNumber().Equals(phoneNumber))
247                                                 {
248                                                         //save newly fetched contact info.
249                                                         bContactFound = true;
250                                                         personId = pContact->GetPersonId();
251                                                         break;
252                                                 }
253                                         }
254                                         delete pPhoneEnum;
255                                         pPhoneNumberList->RemoveAll(true);
256                                         delete pPhoneNumberList;
257                                 }
258                                 if(bContactFound == true)
259                                 {
260                                         break;
261                                 }
262                         }
263                 }
264                 delete pContactEnum;
265                 pNumberContactsList->RemoveAll(true);
266                 delete pNumberContactsList;
267         }
268         return personId;
269 }
270