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