sync with master
[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 }
36
37 CallLogManager::~CallLogManager(void)
38 {
39         __pCallogManager = null;
40 }
41
42 CallLogManager*
43 CallLogManager::GetInstance(void)
44 {
45         if(__pCallogManager == null)
46         {
47                 CreateInstance();
48         }
49         return __pCallogManager;
50 }
51
52 void
53 CallLogManager::CreateInstance(void)
54 {
55         __pCallogManager = new CallLogManager();
56         result r = __pCallogManager->Construct();
57         if(IsFailed(r))
58         {
59                 delete __pCallogManager;
60                 __pCallogManager = null;
61                 return;
62         }
63         std::atexit(DestroyInstance);
64 }
65
66 result
67 CallLogManager::Construct(void)
68 {
69         int ret = contacts_connect2();
70         if(ret != 0)
71         {
72                 return E_FAILURE;
73         }
74         return E_SUCCESS;
75
76 }
77
78 void
79 CallLogManager::DestroyInstance(void)
80 {
81         contacts_disconnect2();
82         delete __pCallogManager;
83 }
84
85 result
86 CallLogManager::AddCallogInfoToDatabase(CallInfo* calllogInfo)
87 {
88         AppLog(" AddVoiceCallInfo Entry");
89         int duration = 0;
90         long long startTime;
91
92         if(calllogInfo != null)
93         {
94                 CallLogType callLogType = calllogInfo->GetCalllogType();
95                 contacts_phone_log_type_e logType = CONTACTS_PLOG_TYPE_NONE;
96                 switch (callLogType)
97                 {
98                 case CALL_LOG_TYPE_VOICE_INCOMING:
99                         logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING;
100                         break;
101                 case CALL_LOG_TYPE_VOICE_OUTGOING:
102                         logType = CONTACTS_PLOG_TYPE_VOICE_OUTGOING;
103                         break;
104                 case CALL_LOG_TYPE_VOICE_MISSED:
105                         logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN;
106                         break;
107                 case CALL_LOG_TYPE_VOICE_REJECTED:
108                         logType = CONTACTS_PLOG_TYPE_VOICE_REJECT;
109                         break;
110                 case CALL_LOG_TYPE_VOICE_BLOCKED:
111                         logType = CONTACTS_PLOG_TYPE_VOICE_BLOCKED;
112                         break;
113                 default:
114                         break;
115                 }
116                 startTime = calllogInfo->GetCallNotificationTime();
117                 if ((logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN)
118                         || (logType == CONTACTS_PLOG_TYPE_VOICE_REJECT)
119                         || (logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED))
120                 {
121                         duration = 0;
122                 }
123                 else
124                 {
125                         //start time is in miliseconds . so convert to seconds and set it to time_t format.
126                         long long connectTime = calllogInfo->GetCallConnectTime();
127                         if(connectTime == 0 || connectTime < 0)
128                         {
129                                 connectTime = time(null);
130                         }
131                         else
132                         {
133                                 connectTime = calllogInfo->GetCallConnectTime()/ 1000;
134                         }
135                         duration = (int)GetDuration(connectTime);
136                 }
137
138                 String PhNumber(L"");
139                 ByteBuffer* pByteBuffer = null;
140
141                 if(calllogInfo->GetContactNumber().IsEmpty() == false)
142                 {
143                         PhNumber.Append(calllogInfo->GetContactNumber());
144                         pByteBuffer = StringUtil::StringToUtf8N(PhNumber);
145                 }
146
147
148                 contacts_record_h hContactLog=0;
149                 int ret = contacts_record_create(_contacts_phone_log._uri,&hContactLog);
150                 if (ret != 0)
151                 {
152                         return E_FAILURE;
153                 }
154                 int id;
155
156
157                 if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_type,logType) != CONTACTS_ERROR_NONE)
158                 {
159                         AppLog(" calllog_set_type is failed");
160                 }
161                 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_time,startTime/1000) != CONTACTS_ERROR_NONE)
162                 {
163                         AppLog(" calllog_set_time is failed");
164                 }
165                 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.extra_data1,duration) != CONTACTS_ERROR_NONE)
166                 {
167                         AppLog(" calllog_set_duration is failed");
168                 }
169                 else if ((pByteBuffer != null) && (contacts_record_set_str(hContactLog,_contacts_phone_log.address,(const char*) pByteBuffer->GetPointer()) != CONTACTS_ERROR_NONE))
170                 {
171                         AppLog(" calllog_set_number is failed");
172                 }
173                 else if (contacts_db_insert_record(hContactLog,&id) != CONTACTS_ERROR_NONE)
174                 {
175                         AppLog(" calllog_insert_to_db is failed");
176                 }
177                 else
178                 {
179                         AppLog(" Call log is added successfully");
180                 }
181
182         }
183         AppLog(" AddVoiceCallInfo Exit");
184         return E_SUCCESS;
185 }
186
187 unsigned long
188 CallLogManager::GetDuration(long long start_time)
189 {
190         time_t curr_time;
191         unsigned long call_duration_in_sec = 50;
192         curr_time = time(0);
193         /*if(start_time != 0)
194         {
195                 start_time /= 1000;
196         }*/
197         call_duration_in_sec = curr_time - start_time;
198         return call_duration_in_sec;
199 }
200