Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnCalllogManager.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        PhnCalllogManager.cpp
19  * @brief       This class provides call log APIs
20  */
21 #include "PhnCalllogManager.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 static const int  MAX_CALL_LOG_COUNT = 100;
31 CallLogManager* CallLogManager::__pCallogManager = null;
32
33 result
34 LogListComparer::Compare(const CallLogDetails& obj1, const CallLogDetails& obj2, int& cmp) const
35 {
36         CallLogDetails* log1 = const_cast<CallLogDetails*>((&obj1));
37         CallLogDetails* log2 = const_cast<CallLogDetails*>((&obj2));
38         if(log1 == null || log2 == null)
39         {
40                 return E_FAILURE;
41         }
42         log1->ConvertTimetoDateTime();
43         DateTime date1 = log1->GetDateTime();
44         //AppLog(" %d %d %s %d:%d:%d", log1->GetCalllogDbId(), log1->GetCalllogType(), log1->GetPhoneNumber(), date1.GetDay(), date1.GetMonth(), date1.GetYear());
45         log2->ConvertTimetoDateTime();
46         DateTime date2 = log2->GetDateTime();
47         //AppLog(" %d %d %s %d:%d:%d", log2->GetCalllogDbId(), log2->GetCalllogType(), log2->GetPhoneNumber(), date2.GetDay(), date2.GetMonth(), date2.GetYear());
48         cmp = date2.CompareTo(date1);
49         return E_SUCCESS;
50 }
51
52 CallLogDetails::CallLogDetails(void)
53 {
54         dateTime.SetValue(0,0,0);
55         first_name = null;
56         last_name = null;
57         display_name = null;
58         contact_image_path = null;
59         phone_number = null;
60         short_message = null;
61         contact_db_id = -1;
62 }
63
64 CallLogDetails::~CallLogDetails(void)
65 {
66         //NOTE: none of the fields are owned. Free() them will lead to crash.
67 }
68
69 bool
70 CallLogDetails::operator ==(const CallLogDetails& rhs) const
71 {
72         if(this->calllog_db_id == rhs.calllog_db_id)
73         {
74                 return true;
75         }
76         return false;
77 }
78
79 bool
80 CallLogDetails::operator !=(const CallLogDetails& rhs) const
81 {
82         if(this->calllog_db_id != rhs.calllog_db_id)
83         {
84                 return true;
85         }
86         return false;
87 }
88
89 CallLogDetails&
90 CallLogDetails::operator =(const CallLogDetails& rhs)
91 {
92         this->calllog_db_id = rhs.calllog_db_id;
93         this->contact_db_id = rhs.contact_db_id;
94         this->calllog_type = rhs.calllog_type;
95         /*if(rhs.contact_image_path != null)
96         {
97                 this->contact_image_path = strdup(rhs.contact_image_path);
98         }
99         if(rhs.display_name != null)
100         {
101                 this->display_name = strdup(rhs.display_name);
102         }*/
103
104         this->duration_sec = rhs.duration_sec;
105
106         /*if(rhs.first_name != null)
107         {
108                 this->first_name = strdup(rhs.first_name);
109         }
110
111         if(rhs.last_name != null)
112         {
113                 this->last_name = strdup(rhs.last_name);
114         }
115 */
116         if(rhs.phone_number != null)
117         {
118                 this->phone_number = strdup(rhs.phone_number);
119         }
120
121         /*if(rhs.short_message != null)
122         {
123                 this->short_message = strdup(rhs.short_message);
124         }*/
125
126         this->startTime = rhs.startTime;
127         return *this;
128 }
129
130
131 void
132 CallLogDetails::SetCalllogDbId(int id)
133 {
134         calllog_db_id = id;
135 }
136
137
138 int
139 CallLogDetails::GetCalllogDbId(void)
140 {
141         return calllog_db_id;
142 }
143
144 void
145 CallLogDetails::SetCalllogType(contacts_phone_log_type_e type)
146 {
147         switch (type)
148         {
149         case CONTACTS_PLOG_TYPE_VOICE_INCOMMING:
150                 calllog_type = CALL_LOG_TYPE_VOICE_INCOMING;
151                 break;
152         case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
153                 calllog_type = CALL_LOG_TYPE_VOICE_OUTGOING;
154                 break;
155         case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN:
156                 calllog_type = CALL_LOG_TYPE_VOICE_MISSED;
157                 break;
158         case CONTACTS_PLOG_TYPE_VOICE_REJECT:
159                 calllog_type = CALL_LOG_TYPE_VOICE_REJECTED;
160                 break;
161         case CONTACTS_PLOG_TYPE_VOICE_BLOCKED:
162                 calllog_type = CALL_LOG_TYPE_VOICE_BLOCKED;
163                 break;
164         default:
165                 break;
166         }
167 }
168
169 CallLogType
170 CallLogDetails::GetCalllogType(void)
171 {
172         return calllog_type;
173 }
174
175 void
176 CallLogDetails::SetContactDbId(int id)
177 {
178         contact_db_id = id;
179 }
180
181 int
182 CallLogDetails::GetContactDbId(void)
183 {
184         return contact_db_id;
185 }
186
187 void
188 CallLogDetails::SetFirstName(char* name)
189 {
190         first_name = strdup(name);
191 }
192
193 char*
194 CallLogDetails::GetFirstName(void)
195 {
196         return first_name;
197 }
198
199 void
200 CallLogDetails::SetLastName(char* name)
201 {
202         last_name = strdup(name);
203 }
204
205 char*
206 CallLogDetails::GetLastName(void)
207 {
208         return last_name;
209 }
210
211 void
212 CallLogDetails::SetDisplayName(char* name)
213 {
214         display_name = strdup(name);
215 }
216
217 char*
218 CallLogDetails::GetDisplayName(void)
219 {
220         return display_name;
221 }
222
223 void
224 CallLogDetails::SetContactImagePath(char* name)
225 {
226         contact_image_path = strdup(name);
227 }
228
229 char*
230 CallLogDetails::GetContactImagePath(void)
231 {
232         return contact_image_path;
233 }
234
235 void
236 CallLogDetails::SetPhoneNumber(char* name)
237 {
238         phone_number = strdup(name);
239 }
240
241 char*
242 CallLogDetails::GetPhoneNumber(void)
243 {
244         return phone_number;
245 }
246
247 void
248 CallLogDetails::SetShortMessage(char* name)
249 {
250         short_message = strdup(name);
251 }
252
253 char*
254 CallLogDetails::GetShortMessage(void)
255 {
256         return short_message;
257 }
258
259 void
260 CallLogDetails::ConvertTimetoDateTime(void)
261 {
262         DateTime dtNow;
263         struct tm* time = null;
264         time = gmtime(&startTime);
265         LocaleManager localManager;
266         TimeZone tz;
267         if(time != null)
268         {
269                 dtNow.SetValue(time->tm_year+1900,time->tm_mon+1, time->tm_mday,time->tm_hour, time->tm_min, time->tm_sec);
270
271         }
272         localManager.Construct();
273         tz = localManager.GetSystemTimeZone();
274         dateTime = tz.UtcTimeToWallTime(dtNow);
275
276
277 }
278
279 void
280 CallLogDetails::SetStartTime(time_t time)
281 {
282         startTime = time;
283         ConvertTimetoDateTime();
284 }
285
286 time_t
287 CallLogDetails::GetStartTime(void)
288 {
289         return startTime;
290 }
291
292 DateTime
293 CallLogDetails::GetDateTime(void)
294 {
295         return dateTime;
296 }
297
298 void
299 CallLogDetails::SetDuration(int duration)
300 {
301         duration_sec = duration;
302 }
303
304 int
305 CallLogDetails::GetDuration(void)
306 {
307         return duration_sec;
308 }
309
310 CallLogManager*
311 CallLogManager::GetInstance(void)
312 {
313         if(__pCallogManager == null)
314         {
315                 CreateInstance();
316         }
317         return __pCallogManager;
318 }
319
320 void
321 CallLogManager::CreateInstance(void)
322 {
323         __pCallogManager = new CallLogManager();
324         result r = __pCallogManager->Construct();
325         if(IsFailed(r))
326         {
327                 delete __pCallogManager;
328                 __pCallogManager = null;
329                 return;
330         }
331         std::atexit(DestroyInstance);
332 }
333 void
334 CallLogManager::DestroyInstance(void)
335 {
336         contacts_disconnect2();
337         delete __pCallogManager;
338 }
339 result
340 CallLogManager::Construct(void)
341 {
342         int ret = contacts_connect2();
343         if(ret != 0)
344         {
345                 return E_FAILURE;
346         }
347
348         __pCalllogList = new (std::nothrow) LinkedListT<CallLogDetails>;
349         __pCalllogChangeListerners = new (std::nothrow) ArrayListT<ICalllogChangeListener*>;
350         __pCalllogChangeListerners->Construct(10);
351         __pCalllogDBIds = new (std::nothrow) HashMapT<int,int>();
352         __pCalllogDBIds->Construct(100);
353
354         GetAllCallogDataFromDatabseCB();
355         SortCalllogList();
356         //TODO
357         //calllog_add_calllog_db_changed_cb(changed_db_cb, this);
358         return E_SUCCESS;
359
360 }
361 ///////////////////////////////////////Calllog Manager////////////////////////////////////////////////////////////////////////////////
362 CallLogManager::CallLogManager(void)
363 {
364         __pCalllogList = null;
365         __pCalllogListByNumber = null;
366         __pNumber = null;
367         __pCalllogChangeListerners = null;
368 }
369
370 CallLogManager::~CallLogManager(void)
371 {
372         if (__pCalllogList != null)
373         {
374                 __pCalllogList->RemoveAll();
375                 delete __pCalllogList;
376                 __pCalllogList = null;
377         }
378         if(__pCalllogListByNumber != null)
379         {
380                 __pCalllogListByNumber = null;
381         }
382         if(__pNumber != null)
383         {
384                 delete __pNumber;
385                 __pNumber = null;
386         }
387         if(__pCalllogChangeListerners != null)
388         {
389                 delete __pCalllogChangeListerners;
390         }
391         __pCallogManager = null;
392 }
393
394 unsigned long
395 CallLogManager::GetDuration(long long start_time)
396 {
397         time_t curr_time;
398         unsigned long call_duration_in_sec = 50;
399         curr_time = time(0);
400         /*if(start_time != 0)
401         {
402                 start_time /= 1000;
403         }*/
404         call_duration_in_sec = curr_time - start_time;
405         return call_duration_in_sec;
406 }
407
408 result
409 CallLogManager::AddCallogInfoToDatabase(CallInfo* calllogInfo)
410 {
411         AppLog(" AddVoiceCallInfo Entry");
412         int duration = 0;
413         long long startTime;
414
415         if(calllogInfo != null)
416         {
417                 CallLogType callLogType = calllogInfo->GetCalllogType();
418                 contacts_phone_log_type_e logType = CONTACTS_PLOG_TYPE_NONE;
419                 switch (callLogType)
420                 {
421                 case CALL_LOG_TYPE_VOICE_INCOMING:
422                         logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING;
423                         break;
424                 case CALL_LOG_TYPE_VOICE_OUTGOING:
425                         logType = CONTACTS_PLOG_TYPE_VOICE_OUTGOING;
426                         break;
427                 case CALL_LOG_TYPE_VOICE_MISSED:
428                         logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN;
429                         break;
430                 case CALL_LOG_TYPE_VOICE_REJECTED:
431                         logType = CONTACTS_PLOG_TYPE_VOICE_REJECT;
432                         break;
433                 case CALL_LOG_TYPE_VOICE_BLOCKED:
434                         logType = CONTACTS_PLOG_TYPE_VOICE_BLOCKED;
435                         break;
436                 default:
437                         break;
438                 }
439                 startTime = calllogInfo->GetCallNotificationTime();
440                 if ((logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN)
441                         || (logType == CONTACTS_PLOG_TYPE_VOICE_REJECT)
442                         || (logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED))
443                 {
444                         duration = 0;
445                 }
446                 else
447                 {
448                         //start time is in miliseconds . so convert to seconds and set it to time_t format.
449                         long long connectTime = calllogInfo->GetCallConnectTime();
450                         if(connectTime == 0 || connectTime < 0)
451                         {
452                                 connectTime = time(null);
453                         }
454                         else
455                         {
456                                 connectTime = calllogInfo->GetCallConnectTime()/ 1000;
457                         }
458                         duration = (int)GetDuration(connectTime);
459                 }
460
461                 String PhNumber(L"");
462                 ByteBuffer* pByteBuffer = null;
463
464                 if(calllogInfo->GetContactNumber().IsEmpty() == false)
465                 {
466                         PhNumber.Append(calllogInfo->GetContactNumber());
467                         pByteBuffer = StringUtil::StringToUtf8N(PhNumber);
468                 }
469
470
471                 contacts_record_h hContactLog=0;
472                 int ret = contacts_record_create(_contacts_phone_log._uri,&hContactLog);
473                 if (ret != 0)
474                 {
475                         return E_FAILURE;
476                 }
477                 int id;
478
479
480                 if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_type,logType) != CONTACTS_ERROR_NONE)
481                 {
482                         AppLog(" calllog_set_type is failed");
483                 }
484                 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_time,startTime/1000) != CONTACTS_ERROR_NONE)
485                 {
486                         AppLog(" calllog_set_time is failed");
487                 }
488                 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.extra_data1,duration) != CONTACTS_ERROR_NONE)
489                 {
490                         AppLog(" calllog_set_duration is failed");
491                 }
492                 else if ((pByteBuffer != null) && (contacts_record_set_str(hContactLog,_contacts_phone_log.address,(const char*) pByteBuffer->GetPointer()) != CONTACTS_ERROR_NONE))
493                 {
494                         AppLog(" calllog_set_number is failed");
495                 }
496                 else if (contacts_db_insert_record(hContactLog,&id) != CONTACTS_ERROR_NONE)
497                 {
498                         AppLog(" calllog_insert_to_db is failed");
499                 }
500                 else
501                 {
502                         AppLog(" Call log is added successfully");
503                 }
504
505         }
506         changed_db_cb();
507         AppLog(" AddVoiceCallInfo Exit");
508         return E_SUCCESS;
509 }
510
511 void
512 CallLogManager::CopyDataToCallLogDetails(CallLogDetails* calllogInfo , contacts_record_h getrec)
513 {
514         int logid=0;
515         contacts_record_get_int(getrec,_contacts_phone_log.id,&logid);
516         calllogInfo->SetCalllogDbId(logid);
517
518         contacts_phone_log_type_e logType=CONTACTS_PLOG_TYPE_NONE;
519         contacts_record_get_int(getrec,_contacts_phone_log.log_type,(int*)&logType);
520         calllogInfo->SetCalllogType(logType);
521
522         int log_time=0;
523         contacts_record_get_int(getrec,_contacts_phone_log.log_time,&log_time);
524         calllogInfo->SetStartTime((time_t)log_time);
525
526         char* address = null;
527         int ret = contacts_record_get_str(getrec,_contacts_phone_log.address,&address);
528         if(ret == 0 && address != null)
529         {
530                 calllogInfo->SetPhoneNumber(address);
531         }
532
533         int duration=0;
534         contacts_record_get_int(getrec,_contacts_phone_log.extra_data1,&duration);
535         calllogInfo->SetDuration(duration);
536
537         char* shortMsg=0;
538         contacts_record_get_str(getrec,_contacts_phone_log.extra_data2,&shortMsg);
539         if(shortMsg)
540         calllogInfo->SetShortMessage(shortMsg);
541
542 }
543
544 bool
545 CallLogManager::GetAllCallogDataFromDatabseCB()
546 {
547         contacts_list_h list;
548         int ret;
549         int calLogCount =  0;
550
551         contacts_db_get_count(_contacts_phone_log._uri,&calLogCount);
552
553         if(calLogCount > MAX_CALL_LOG_COUNT)
554         {
555                 ret = contacts_db_get_all_records(_contacts_phone_log._uri,calLogCount - MAX_CALL_LOG_COUNT,MAX_CALL_LOG_COUNT,&list);
556         }
557         else
558         {
559                 ret = contacts_db_get_all_records(_contacts_phone_log._uri,0,MAX_CALL_LOG_COUNT,&list);
560         }
561         if (ret != 0)
562         {
563                 return false;
564         }
565
566         unsigned int count;
567
568         ret =  contacts_list_get_count(list,&count);
569         contacts_list_first(list);
570         for (unsigned int i = 0; i < count; i++)
571         {
572                 CallLogDetails* calllogInfo = new CallLogDetails();
573
574                 contacts_record_h getrec=0;
575                 ret = contacts_list_get_current_record_p(list,&getrec);
576                 if(ret == 0)
577                 {
578                         contacts_phone_log_type_e logType=CONTACTS_PLOG_TYPE_NONE;
579                         contacts_record_get_int(getrec,_contacts_phone_log.log_type,(int*)&logType);
580
581                         if(logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING ||
582                                         logType == CONTACTS_PLOG_TYPE_VOICE_OUTGOING ||
583                                         logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN||
584                                         logType == CONTACTS_PLOG_TYPE_VOICE_REJECT ||
585                                         logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED)
586                         {
587
588                                 CopyDataToCallLogDetails(calllogInfo, getrec);
589                                 if(__pCalllogDBIds->Add(calllogInfo->GetCalllogDbId(),calllogInfo->GetCalllogDbId()) == E_SUCCESS)
590                                 {
591                                         __pCalllogList->InsertAt(*calllogInfo, 0);
592                                 }
593                         }
594                 }
595                 delete calllogInfo;
596                 //move to next record
597                 ret = contacts_list_next(list);
598                 if (ret != 0)
599                 {
600                         continue;
601                 }
602                 contacts_record_destroy(getrec,true);
603         }
604
605         return true;
606 }
607
608
609 void
610 CallLogManager::ResetAndNotifyCalllogData(void)
611 {
612         if(__pCalllogList != null)
613         {
614                 __pCalllogList->RemoveAll();
615                 delete __pCalllogList;
616         }
617         if(__pCalllogDBIds != null)
618         {
619                 __pCalllogDBIds->RemoveAll();
620                 delete __pCalllogDBIds;
621         }
622
623         __pCalllogList = new (std::nothrow) LinkedListT<CallLogDetails>;
624         __pCalllogDBIds = new (std::nothrow) HashMapT<int,int>;
625         __pCalllogDBIds->Construct(100);
626
627         GetAllCallogDataFromDatabseCB();
628         SortCalllogList();
629
630         //__pCalllogChangeListerners->GetEnumeratorN();
631
632         IEnumeratorT<ICalllogChangeListener*>* pListenerEnum = null;
633         result r = E_FAILURE;
634         if (__pCalllogChangeListerners != null)
635         {
636                 pListenerEnum = __pCalllogChangeListerners->GetEnumeratorN();
637                 while ((r = pListenerEnum->MoveNext()) != E_OUT_OF_RANGE)
638                 {
639                         ICalllogChangeListener* pListener;
640                         r = pListenerEnum->GetCurrent(pListener);
641                         pListener->OnCalllogChanged();
642
643                 }
644         }
645
646 }
647
648
649
650 IListT<CallLogDetails>*
651 CallLogManager::GetCalllogListByNumValues(void)  //to be verified
652 {
653         if(__pCalllogListByNumber != null)
654         {
655                 //return __pCalllogListByNumber->GetValuesN();
656                 return __pCalllogListByNumber->GetItemsN(0, __pCalllogListByNumber->GetCount());
657         }
658         return null;
659 }
660
661 bool
662 CallLogManager::GetCallogListByNumberFromDatabaseCB(char* number)
663 {
664
665         contacts_filter_h filter=NULL;
666         contacts_list_h list=NULL;
667         contacts_query_h query = NULL;
668         unsigned int count = 0;
669         int ret = 0;
670
671         contacts_filter_create(_contacts_phone_log._uri,&filter);
672         contacts_filter_add_str(filter,_contacts_phone_log.address,CONTACTS_MATCH_EXACTLY,number);
673         contacts_query_create(_contacts_phone_log._uri,&query);
674         contacts_query_set_filter(query,filter);
675
676         ret = contacts_db_get_records_with_query(query,0,0,&list);
677         if (ret != 0)
678         {
679                 contacts_filter_destroy(filter);
680                 contacts_query_destroy(query);
681                 return false;
682         }
683         contacts_filter_destroy(filter);
684         contacts_query_destroy(query);
685
686
687         contacts_list_get_count(list,&count);
688         contacts_list_first(list);
689         for(unsigned int i=0;i<count;i++)
690         {
691                 CallLogDetails* calllogInfo = new CallLogDetails();
692                 contacts_record_h getrec=0;
693                 result ret = contacts_list_get_current_record_p(list,&getrec);
694                 if(ret == 0)
695                 {
696                         //__pCalllogListByNumber->Add( calllogInfo->GetCalllogDbId(), *calllogInfo);
697                         contacts_phone_log_type_e logType=CONTACTS_PLOG_TYPE_NONE;
698                         contacts_record_get_int(getrec,_contacts_phone_log.log_type,(int*)&logType);
699
700                         if(logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING ||
701                                         logType == CONTACTS_PLOG_TYPE_VOICE_OUTGOING ||
702                                         logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN||
703                                         logType == CONTACTS_PLOG_TYPE_VOICE_REJECT ||
704                                         logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED)
705                         {
706                                 CopyDataToCallLogDetails(calllogInfo, getrec);
707                                 __pCalllogListByNumber->Add(*calllogInfo);  //to be verified
708                         }
709                 }
710
711                 delete calllogInfo;
712                 ret = contacts_list_next(list);
713                 if (ret != 0)
714                 {
715                         continue;
716                 }
717                 contacts_record_destroy(getrec,true);
718         }
719
720         return true;
721 }
722
723 Tizen::Base::Collection::ArrayListT<CallLogDetails>*
724 CallLogManager::GetCallogListByNumberFromDatabaseN(char* number)
725 {
726         __pCalllogListByNumber = new (std::nothrow) Tizen::Base::Collection::ArrayListT<CallLogDetails>();  //to be verified
727         __pCalllogListByNumber->Construct(100);
728         if(__pNumber != null)
729         {
730                 delete __pNumber;
731         }
732         int len = strlen(number) + 1;
733         __pNumber = new (std::nothrow) char[len];
734         strcpy(__pNumber,number);
735
736         GetCallogListByNumberFromDatabaseCB(number);
737
738         if (__pCalllogListByNumber->GetCount() > 0)
739         {
740                 SortCalllogListByNumber();
741                 return __pCalllogListByNumber;
742         }
743         delete __pCalllogListByNumber;
744         __pCalllogListByNumber = null;
745         return null;
746 }
747
748 HashMapT<int, CallLogDetails>*
749 CallLogManager::GetCallogListByUnknownNumberFromDatabaseN(char* number)
750 {
751         //Used to fetch list of call from unknown numbers from logs.
752         __pCalllogUnkownListByNumber = new (std::nothrow) HashMapT<int, CallLogDetails>();
753         __pCalllogUnkownListByNumber->Construct(100);
754         if(__pNumber != null)
755         {
756                 delete __pNumber;
757         }
758         int len = strlen(number) + 1;
759         __pNumber = new (std::nothrow) char[len];
760         strcpy(__pNumber,number);
761
762         GetCallogListByNumberFromDatabaseCB(number);
763         if (__pCalllogUnkownListByNumber->GetCount() > 0)
764         {
765                 return __pCalllogUnkownListByNumber;
766         }
767         delete __pCalllogUnkownListByNumber;
768         __pCalllogUnkownListByNumber = null;
769         return null;
770 }
771
772 void
773 CallLogManager::changed_db_cb()
774 {
775         ResetAndNotifyCalllogData();
776 }
777
778
779 void
780 CallLogManager::DeleteCalllogByDbId(int dbId)
781 {
782         int ret = 0;
783         contacts_filter_h filter=NULL;
784         contacts_list_h list=NULL;
785         contacts_query_h query = NULL;
786         unsigned int count;
787
788         contacts_filter_create(_contacts_phone_log._uri,&filter);
789         contacts_filter_add_int(filter,_contacts_phone_log.id,CONTACTS_MATCH_EQUAL,dbId);
790         contacts_query_create(_contacts_phone_log._uri,&query);
791         contacts_query_set_filter(query,filter);
792
793         ret = contacts_db_get_records_with_query(query,0,0,&list);
794         if (ret != 0)
795         {
796                 contacts_filter_destroy(filter);
797                 contacts_query_destroy(query);
798                 return ;
799         }
800         contacts_filter_destroy(filter);
801         contacts_query_destroy(query);
802
803
804         ret =  contacts_list_get_count(list,&count);
805         contacts_list_first(list);
806         for(unsigned int i=0;i<count;i++)
807         {
808                 contacts_record_h getrec=0;
809                 ret = contacts_list_get_current_record_p(list,&getrec);
810                 contacts_db_delete_record(_contacts_phone_log._uri,dbId);
811                 contacts_record_destroy(getrec,true);
812                 changed_db_cb();
813         }
814
815 }
816
817 void
818 CallLogManager::DeleteAllCalllog(void)
819 {
820         int count =0;
821
822         AppLogDebug("Enter");
823         count = __pCalllogList->GetCount();
824
825         for(int i=0; i< count; i++)
826         {
827                 CallLogDetails latestCallLog;
828                 if(__pCalllogList->GetAt(i,latestCallLog) == E_SUCCESS)
829                 {
830                         contacts_db_delete_record(_contacts_phone_log._uri,latestCallLog.GetCalllogDbId());
831                 }
832         }
833         AppLogDebug("Exit");
834         contacts_phone_log_reset_statistics();
835         changed_db_cb();
836 }
837
838 int
839 CallLogManager::GetAllCalllogCount(void)
840 {
841         if(__pCalllogList != null)
842         {
843                 return __pCalllogList->GetCount();
844         }
845         return 0;
846 }
847
848 int
849 CallLogManager::GetCalllogCountByNumber(void)
850 {
851         if(__pCalllogListByNumber != null)
852         {
853                 return __pCalllogListByNumber->GetCount();
854         }
855         return 0;
856 }
857
858 void
859 CallLogManager::SortCalllogList(void)
860 {
861         LogListComparer cmp;
862         __pCalllogList->Sort(cmp);
863 }
864
865 void
866 CallLogManager::SortCalllogListByNumber(void) //to be verified
867 {
868         LogListComparer cmp;
869         __pCalllogListByNumber->Sort(cmp);
870 }
871
872 String*
873 CallLogManager::GetLatestCallLogFromDbN(void)
874 {
875         String* latestNumber = null;
876         if(__pCalllogList != null && __pCalllogList->GetCount() > 0)
877         {
878                 CallLogDetails latestCallLog;
879                 if(__pCalllogList->GetAt(0,latestCallLog) == E_SUCCESS)
880                 {
881                         latestNumber = new String(latestCallLog.GetPhoneNumber());
882                 }
883         }
884
885         return latestNumber;
886 }
887
888 result CallLogManager::GetCallLogItemAtIndex(int index, CallLogDetails& calllogInfo)
889 {
890         return __pCalllogList->GetAt(index,calllogInfo);
891 }
892
893
894
895
896 void CallLogManager::AddCalllogChangeListener(ICalllogChangeListener& listner )
897 {
898         if(!__pCalllogChangeListerners->Contains(&listner))
899         __pCalllogChangeListerners->Add(&listner);
900
901 }
902
903 void CallLogManager::RemoveCalllogChangeListner(ICalllogChangeListener& listner)
904 {
905         __pCalllogChangeListerners->Remove(&listner);
906 }
907
908 void CallLogManager::GetCalllogContactName(String number,String& name)
909 {
910
911         ByteBuffer* pByteBuffer;
912         char * charname=NULL;
913         pByteBuffer = StringUtil::StringToUtf8N(number);
914
915         contacts_filter_h filter=NULL;
916         contacts_list_h list=NULL;
917         contacts_query_h query = NULL;
918         unsigned int count = 0;
919
920         contacts_filter_create(_contacts_contact_number._uri,&filter);
921         contacts_filter_add_str(filter,_contacts_contact_number.number,CONTACTS_MATCH_EXACTLY,(const char*)pByteBuffer->GetPointer());
922         contacts_query_create(_contacts_contact_number._uri,&query);
923         contacts_query_set_filter(query,filter);
924
925         int ret = contacts_db_get_records_with_query(query,0,0,&list);
926         if (ret != 0)
927         {
928                 contacts_filter_destroy(filter);
929                 contacts_query_destroy(query);
930                 return ;
931         }
932
933         contacts_filter_destroy(filter);
934         contacts_query_destroy(query);
935
936
937         contacts_list_get_count(list,&count);
938         AppLogTag("Amith","%d",count);
939         if(count > 0)
940         {
941                 contacts_record_h getrec=0;
942                 contacts_list_first(list);
943                 contacts_list_get_current_record_p(list,&getrec);
944
945                 contacts_record_get_str(getrec,_contacts_contact_number.display_name,&charname);
946                 AppLogTag("Amith","%s",charname);
947                 if(charname)
948                 {
949                         String buffer(charname);
950                         name.Clear();
951                         name.Append(buffer);
952
953                 }
954                 contacts_record_destroy(getrec,true);
955         }
956
957
958
959
960 }