2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include "CallHistory.h"
25 #include <Commons/Exception.h>
26 #include <CommonsJavaScript/JSCallbackManager.h>
27 #include <dpl/shared_ptr.h>
28 #include <dpl/log/log.h>
30 #include <IFilterVisitor.h>
31 #include "CallHistoryDefine.h"
33 #include "CallHistoryFilter.h"
35 using namespace DeviceAPI::Tizen;
36 using namespace WrtDeviceApis;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
42 namespace CallHistory {
44 std::vector<CallHistory::WatcherPtr> CallHistory::m_watchers;
46 CallHistory::CallHistory()
49 if (contacts_connect2() == CONTACTS_ERROR_NONE) {
50 LogDebug("Successful to connect Call history DB ");
52 LogDebug("Failed to connect Call history DB ");
55 EventCallHistoryPtr event(new EventCallHistory());
56 event->setCmdType(EventCallHistory::CONNECT);
57 event->setForSynchronousCall();
58 EventRequestReceiver<EventCallHistory>::PostRequest(event);
61 CallHistory::~CallHistory()
65 if (contacts_disconnect2() == CONTACTS_ERROR_NONE) {
66 LogDebug("Successful to disconnect Call history DB ");
68 LogDebug("Failed to disconnect Call history DB ");
71 int ret = CONTACTS_ERROR_NONE;
72 std::vector<CallHistory::WatcherPtr>::iterator it = CallHistory::m_watchers.begin();
74 if (m_watchers.size() > 0) {
75 for (;it < CallHistory::m_watchers.end();) {
76 ret = contacts_db_remove_changed_cb(_contacts_phone_log._uri, callhistoryListenerCB, it->Get());
78 if (ret != CONTACTS_ERROR_NONE) {
79 LogDebug("callhistory remove listener error [" << ret << "]");
82 it = CallHistory::m_watchers.erase(it);
84 LogDebug("CallHistory Watcher is removed. (" << CallHistory::m_watchers.size() << ")");
87 EventCallHistoryPtr event(new EventCallHistory());
88 event->setCmdType(EventCallHistory::DISCONNECT);
89 event->setForSynchronousCall();
90 EventRequestReceiver<EventCallHistory>::PostRequest(event);
93 void CallHistory::callhistoryListenerCB(const char* view_uri, void* user_data)
96 if (user_data != NULL) {
97 ((CallHistory::Watcher*)user_data)->addedListenerCB();
98 ((CallHistory::Watcher*)user_data)->changedListenerCB();
102 void CallHistory::removeBatchCB(int error, void *user_data)
105 if (user_data != NULL) {
106 CallHistory* chManager = (CallHistory *)((EventCallHistoryPtrs<EventRemoveBatch> *)user_data)->getThisPtr();
109 EventRemoveBatchPtr event = ((EventCallHistoryPtrs<EventRemoveBatch> *)user_data)->getEventPtrs();
110 chManager->callRemoveBatchEvent(error, event);
116 void CallHistory::removeAllCB(int error, void *user_data)
119 if (user_data != NULL) {
120 CallHistory* chManager = (CallHistory *)((EventCallHistoryPtrs<EventRemoveAll> *)user_data)->getThisPtr();
123 EventRemoveAllPtr event = ((EventCallHistoryPtrs<EventRemoveAll> *)user_data)->getEventPtrs();
124 chManager->callRemoveAllEvent(error, event);
130 bool CallHistory::convertCallHistory(callhistory_query_s *query_log, CallHistoryEntryListPtr &callEntries)
132 if (query_log == NULL || callEntries == NULL)
135 CallHistoryEntryPropertiesPtr callHistoryItem(new CallHistoryEntryProperties());
136 StringArrayPtr stringArray(new StringArray());
137 RemotePartyPtr remoteParty(new RemoteParty());
138 RemotePartyListPtr remotePartyList(new RemotePartyList());
140 std::string callType("");
141 std::string tags("");
142 std::string direction("");
143 std::string number("");
145 if (query_log->phone_number != NULL) {
146 number.append(query_log->phone_number);
149 switch(query_log->calllog_type) {
150 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING:
151 callType.append(STR_TIZEN_TEL);
152 tags.append(STR_CALL_VOICE);
153 direction.append(STR_RECEIVED);
155 case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
156 callType.append(STR_TIZEN_TEL);
157 tags.append(STR_CALL_VOICE);
158 direction.append(STR_DIALED);
160 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN:
161 callType.append(STR_TIZEN_TEL);
162 tags.append(STR_CALL_VOICE);
163 direction.append(STR_MISSED);
165 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN:
166 callType.append(STR_TIZEN_TEL);
167 tags.append(STR_CALL_VOICE);
168 direction.append(STR_MISSED_NEW);
170 case CONTACTS_PLOG_TYPE_VOICE_REJECT:
171 callType.append(STR_TIZEN_TEL);
172 tags.append(STR_CALL_VOICE);
173 direction.append(STR_REJECTED);
175 case CONTACTS_PLOG_TYPE_VOICE_BLOCKED:
176 callType.append(STR_TIZEN_TEL);
177 tags.append(STR_CALL_VOICE);
178 direction.append(STR_BLOCKED);
180 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING:
181 callType.append(STR_TIZEN_TEL);
182 tags.append(STR_CALL_VIDEO);
183 direction.append(STR_RECEIVED);
185 case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
186 callType.append(STR_TIZEN_TEL);
187 tags.append(STR_CALL_VIDEO);
188 direction.append(STR_DIALED);
190 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN:
191 callType.append(STR_TIZEN_TEL);
192 tags.append(STR_CALL_VIDEO);
193 direction.append(STR_MISSED);
195 case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN:
196 callType.append(STR_TIZEN_TEL);
197 tags.append(STR_CALL_VIDEO);
198 direction.append(STR_MISSED_NEW);
200 case CONTACTS_PLOG_TYPE_VIDEO_REJECT:
201 callType.append(STR_TIZEN_TEL);
202 tags.append(STR_CALL_VIDEO);
203 direction.append(STR_REJECTED);
205 case CONTACTS_PLOG_TYPE_VIDEO_BLOCKED:
206 callType.append(STR_TIZEN_TEL);
207 tags.append(STR_CALL_VIDEO);
208 direction.append(STR_BLOCKED);
214 callHistoryItem->setEntryId(query_log->calllog_db_id);
215 callHistoryItem->setCallType(callType);
216 stringArray->push_back(tags);
217 callHistoryItem->setTags(stringArray);
219 remoteParty->setRemoteParty(number);
220 std::stringstream personIdStream;
221 personIdStream << query_log->person_db_id;
222 remoteParty->setPersonId(personIdStream.str());
224 remotePartyList->push_back(remoteParty);
225 callHistoryItem->setRemoteParties(remotePartyList);
227 callHistoryItem->setStartTime(query_log->timestamp);
228 callHistoryItem->setDuration((unsigned long)(query_log->duration_sec));
229 callHistoryItem->setDirection(direction);
231 callEntries->insert(callEntries->end(), callHistoryItem);
236 void CallHistory::find(const EventFindCallHistoryPtr& event)
238 EventRequestReceiver<EventFindCallHistory>::PostRequest(event);
241 bool CallHistory::remove(const unsigned long entryId)
244 int ret = contacts_db_delete_record(_contacts_phone_log._uri, (int)entryId);
246 if (ret == CONTACTS_ERROR_NONE) {
251 } catch (const Commons::PlatformException& ex) {
252 ThrowMsg(Commons::PlatformException, ex.GetMessage());
256 void CallHistory::removeBatch(const EventRemoveBatchPtr& event)
258 EventRequestReceiver<EventRemoveBatch>::PostRequest(event);
261 void CallHistory::removeAll(const EventRemoveAllPtr& event)
263 EventRequestReceiver<EventRemoveAll>::PostRequest(event);
266 long CallHistory::addListener(const EventCallHistoryListenerEmitterPtr& emitter)
270 if (CallHistory::m_watchers.size() == 0) {
271 WatcherPtr watcher(new Watcher(0, emitter));
272 ret = contacts_db_add_changed_cb(_contacts_phone_log._uri, callhistoryListenerCB, watcher.Get());
274 if (ret == CONTACTS_ERROR_NONE) {
275 CallHistoryEntryListPtr callHistoryListPtr(new CallHistoryEntryList());
277 contacts_query_h query = NULL;
278 contacts_filter_h filter = NULL;
279 contacts_list_h recordList = NULL;
281 contacts_query_create(_contacts_phone_log._uri, &query);
282 contacts_filter_create(_contacts_phone_log._uri, &filter);
283 contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_LESS_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VIDEO_BLOCKED);
285 contacts_query_set_filter(query, filter);
286 contacts_query_set_sort(query, _contacts_phone_log.id, false);
287 ret = contacts_db_get_records_with_query(query, 0, 1, &recordList);
289 if (ret != CONTACTS_ERROR_NONE) {
290 LogDebug("callhistory query error [" << ret << "]");
293 if (parseRecord(&recordList, callHistoryListPtr)) {
294 if (callHistoryListPtr->size() > 0) {
295 watcher->setCurrentLogId((*callHistoryListPtr)[0]->getEntryId());
298 contacts_list_destroy(recordList, true);
299 contacts_query_destroy(query);
300 contacts_filter_destroy(filter);
302 MissedCallListPtr missedCallList(updateCurrentMissedCall());
303 watcher->setMissedCallList(missedCallList);
306 watcher->setHandle(static_cast<long>(emitter->getId()));
307 CallHistory::m_watchers.push_back(watcher);
308 return watcher->getHandle();
314 void CallHistory::removeListener(const long id)
316 std::vector<CallHistory::WatcherPtr>::iterator it = CallHistory::m_watchers.begin();
318 int ret = CONTACTS_ERROR_NONE;
320 for (;it < CallHistory::m_watchers.end();) {
322 LogDebug("removeListener : id ["<<id<<"]");
323 LogDebug("removeListener : handle() ["<<(*it)->getHandle()<<"]");
325 if (id == (*it)->getHandle()) {
327 ret = contacts_db_remove_changed_cb(_contacts_phone_log._uri, callhistoryListenerCB, it->Get());
329 if (ret != CONTACTS_ERROR_NONE) {
330 LogDebug("callhistory remove listener error [" << ret << "]");
333 it = CallHistory::m_watchers.erase(it);
339 LogDebug("CallHistory Watcher is removed. (" << CallHistory::m_watchers.size() << ")");
342 ThrowMsg(Commons::InvalidArgumentException, "Invalid values error : handle");
346 bool CallHistory::parseRecord(contacts_list_h *recordList, CallHistoryEntryListPtr &callEntries)
348 int ret = CONTACTS_ERROR_NONE;
349 unsigned int total = 0;
350 contacts_record_h record = NULL;
351 callhistory_query_s query_data;
355 contacts_list_get_count(*recordList, &total);
357 LogDebug("total [" << total << "]");
359 for (int i = 0; i < total; i++) {
360 ret = contacts_list_get_current_record_p(*recordList, &record);
362 if (record == NULL) {
363 ret = contacts_list_next(*recordList);
364 if (ret != CONTACTS_ERROR_NONE && ret != CONTACTS_ERROR_NO_DATA) {
365 LogDebug("callhistory parse error [" << ret << "]");
372 memset(&query_data, 0x00, sizeof(callhistory_query_s));
374 if (ret == CONTACTS_ERROR_NONE) {
375 contacts_record_get_int(record, _contacts_phone_log.id, &(query_data.calllog_db_id));
376 contacts_record_get_int(record, _contacts_phone_log.log_type , &logType);
377 contacts_record_get_int(record, _contacts_phone_log.person_id, &(query_data.person_db_id));
378 contacts_record_get_str_p(record, _contacts_phone_log.address, &(query_data.phone_number));
379 contacts_record_get_int(record, _contacts_phone_log.log_time, &time);
380 contacts_record_get_int(record, _contacts_phone_log.extra_data1, &(query_data.duration_sec));
381 query_data.calllog_type = static_cast<contacts_phone_log_type_e>(logType);
382 query_data.timestamp = static_cast<time_t>(time);
383 convertCallHistory(&query_data, callEntries);
386 ret = contacts_list_next(*recordList);
387 if (ret != CONTACTS_ERROR_NONE && ret != CONTACTS_ERROR_NO_DATA) {
388 LogDebug("callhistory parse error [" << ret << "]");
397 unsigned int CallHistory::convertAttrName(std::string &name)
399 std::string attrName("");
400 if (name.compare(STR_RP_REMOTEPARTY) == 0) {
401 return _contacts_phone_log.address;
402 } else if (name.compare(STR_START_TIME) == 0) {
403 return _contacts_phone_log.log_time;
404 } else if (name.compare(STR_DURATION) == 0) {
405 return _contacts_phone_log.extra_data1;
406 } else if (name.compare(STR_DIRECTION) == 0) {
407 return _contacts_phone_log.log_type;
408 } else if (name.compare(STR_ENTRY_ID) == 0) {
409 return _contacts_phone_log.id;
415 MissedCallListPtr CallHistory::updateCurrentMissedCall()
417 CallHistoryEntryListPtr callHistoryListPtr(new CallHistoryEntryList());
418 MissedCallListPtr missedCallList(new MissedCallList());
419 int ret = CONTACTS_ERROR_NONE;
421 contacts_query_h query = NULL;
422 contacts_filter_h filter = NULL;
423 contacts_list_h recordList = NULL;
425 contacts_query_create(_contacts_phone_log._uri, &query);
426 contacts_filter_create(_contacts_phone_log._uri, &filter);
427 contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_EQUAL, CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN);
428 contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
429 contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_EQUAL, CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN);
431 contacts_query_set_filter(query, filter);
432 contacts_query_set_sort(query, _contacts_phone_log.log_time, false);
433 ret = contacts_db_get_records_with_query(query, 0, 0, &recordList);
435 if (ret != CONTACTS_ERROR_NONE) {
436 LogDebug("callhistory query error [" << ret << "]");
439 if (parseRecord(&recordList, callHistoryListPtr)) {
440 LogDebug("result counter [" << callHistoryListPtr->size() << "]");
441 CallHistoryEntryList::iterator it = callHistoryListPtr->begin();
443 for (;it != callHistoryListPtr->end(); ++it) {
444 missedCallList->push_back((*it)->getEntryId());
446 LogDebug("missed Call size (" << missedCallList->size() << ")");
449 contacts_list_destroy(recordList, true);
450 contacts_query_destroy(query);
451 contacts_filter_destroy(filter);
453 return missedCallList;
456 void CallHistory::OnRequestReceived(const EventCallHistoryPtr& event)
459 if (event->getCmdType() == EventCallHistory::CONNECT) {
460 if (contacts_connect2() == CONTACTS_ERROR_NONE) {
461 LogDebug("Successful to connect Call history DB by sub thread");
463 LogDebug("Failed to connect Call history DB by sub thread");
465 } else if (event->getCmdType() == EventCallHistory::DISCONNECT) {
466 if (contacts_disconnect2() == CONTACTS_ERROR_NONE) {
467 LogDebug("Successful to disconnect Call history DB by sub thread");
469 LogDebug("Failed to disconnect Call history DB by sub thread");
472 } catch (const Commons::PlatformException& ex) {
473 LogError("Exception: " << ex.GetMessage());
474 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
478 void CallHistory::OnRequestReceived(const EventFindCallHistoryPtr& event)
482 CallHistoryEntryListPtr callHistoryListPtr(new CallHistoryEntryList());
483 int ret = CONTACTS_ERROR_NONE;
485 contacts_query_h query = NULL;
486 contacts_filter_h filter = NULL;
487 contacts_list_h recordList = NULL;
488 bool isAscending = false;
492 contacts_query_create(_contacts_phone_log._uri, &query);
493 contacts_filter_create(_contacts_phone_log._uri, &filter);
495 CallHistoryFilterPtr filtering(new CallHistoryFilter(filter));
496 IFilterVisitorPtr filterTraversal = DPL::StaticPointerCast<IFilterVisitor>(filtering);
498 if (event->getFilterIsSet()) {
499 FilterPtr filterPtr = event->getFilter();
500 filterPtr->travel(filterTraversal, 0);
502 contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
503 contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_LESS_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VIDEO_BLOCKED);
505 contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_LESS_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VIDEO_BLOCKED);
507 contacts_query_set_filter(query, filter);
509 if (event->getSortModesIsSet()) {
510 if (event->getSortMode()->getOrder() == DeviceAPI::Tizen::ASCENDING_SORT_ORDER) {
515 std::string attriName(event->getSortMode()->getAttributeName());
516 contacts_query_set_sort(query, convertAttrName(attriName), isAscending);
518 contacts_query_set_sort(query, _contacts_phone_log.id, isAscending);
521 if (event->getLimitIsSet()) {
522 limit = static_cast<int>(event->getLimit());
525 if (event->getOffsetIsSet()) {
526 offset = static_cast<int>(event->getOffset());
529 ret = contacts_db_get_records_with_query(query, offset, limit, &recordList);
531 if (ret != CONTACTS_ERROR_NONE) {
532 LogDebug("callhistory query error [" << ret << "]");
535 if (parseRecord(&recordList, callHistoryListPtr)) {
536 event->setResult(callHistoryListPtr);
539 contacts_list_destroy(recordList, true);
540 contacts_query_destroy(query);
541 contacts_filter_destroy(filter);
542 } catch (const Commons::PlatformException& ex) {
543 LogError("Exception: " << ex.GetMessage());
544 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
548 void CallHistory::OnRequestReceived(const EventRemoveBatchPtr& event)
553 std::vector<unsigned long> entryIds = event->getEntryIds();
554 unsigned int cnt = 0;
555 int ret = CONTACTS_ERROR_NONE;
556 list = new int[entryIds.size()];
558 for (cnt = 0; cnt < entryIds.size(); cnt++) {
559 list[cnt] = static_cast<int>(entryIds[cnt]);
562 if (entryIds.size() > 0) {
563 EventRemoveBatchDataPtr eventData( new EventCallHistoryPtrs<EventRemoveBatch>(event, this));
565 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
567 if (data->getOnSuccess() != NULL) {
568 ret = contacts_db_delete_records_async(_contacts_phone_log._uri, list, entryIds.size(), removeBatchCB, eventData.Get());
569 if (ret == CONTACTS_ERROR_NONE) {
570 m_removeBatchEvent.push_back(eventData);
571 event->switchToManualAnswer();
573 LogDebug("callhistory remove Batch error [" << ret << "]");
574 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
577 ret = contacts_db_delete_records(_contacts_phone_log._uri, list, entryIds.size());
578 if (ret != CONTACTS_ERROR_NONE) {
579 LogDebug("callhistory remove Batch error [" << ret << "]");
580 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
584 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
588 } catch (const Commons::PlatformException& ex) {
589 LogError("Exception: " << ex.GetMessage());
590 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
599 void CallHistory::OnRequestReceived(const EventRemoveAllPtr& event)
604 int ret = CONTACTS_ERROR_NONE;
605 contacts_list_h record_list = NULL;
606 contacts_record_h record = NULL;
607 unsigned int total = 0;
608 unsigned int cnt = 0;
611 ret = contacts_db_get_all_records(_contacts_phone_log._uri, 0, 0, &record_list);
613 if (record_list == NULL) {
614 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
618 contacts_list_get_count(record_list, &total);
620 list = new int[total];
621 LogDebug("total [" << total << "]");
622 for (unsigned int i = 0; i < total; i++) {
623 ret = contacts_list_get_current_record_p(record_list, &record);
625 if (record == NULL) {
626 ret = contacts_list_next(record_list);
627 if ( !(ret==CONTACTS_ERROR_NONE || ret==CONTACTS_ERROR_NO_DATA) ){
628 LogDebug("callhistory remove All error [" << ret << "]");
629 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
634 if (ret == CONTACTS_ERROR_NONE) {
635 ret = contacts_record_get_int(record, _contacts_phone_log.id , &value);
636 if (ret == CONTACTS_ERROR_NONE) {
642 ret = contacts_list_next(record_list);
643 if ( !(ret==CONTACTS_ERROR_NONE || ret==CONTACTS_ERROR_NO_DATA) ){
644 LogDebug("callhistory remove All error [" << ret << "]");
645 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
649 LogDebug("cnt [" << cnt << "]");
652 EventRemoveAllDataPtr eventData( new EventCallHistoryPtrs<EventRemoveAll>(event, this));
654 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
656 if (data->getOnSuccess() != NULL) {
657 ret = contacts_db_delete_records_async(_contacts_phone_log._uri, list, cnt, removeAllCB, eventData.Get());
658 if (ret == CONTACTS_ERROR_NONE) {
659 m_removeAllEvent.push_back(eventData);
660 event->switchToManualAnswer();
662 LogDebug("callhistory remove All error [" << ret << "]");
663 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
666 ret = contacts_db_delete_records(_contacts_phone_log._uri, list, cnt);
667 if (ret != CONTACTS_ERROR_NONE) {
668 LogDebug("callhistory remove All error [" << ret << "]");
669 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
675 contacts_list_destroy(record_list, true);
676 } catch (const Commons::PlatformException& ex) {
677 LogError("Exception: " << ex.GetMessage());
678 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
687 void CallHistory::callRemoveBatchEvent(int error, const EventRemoveBatchPtr &event)
689 if (error != CONTACTS_ERROR_NONE) {
690 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
693 EventRequestReceiver<EventRemoveBatch>::ManualAnswer(event);
695 std::vector<EventRemoveBatchDataPtr>::iterator it;
696 for (it = m_removeBatchEvent.begin(); it != m_removeBatchEvent.end(); ++it) {
697 if ((*it)->getEventPtrs() == event) {
698 m_removeBatchEvent.erase(it);
699 LogDebug("event is removed. (" << m_removeBatchEvent.size() << ")");
705 void CallHistory::callRemoveAllEvent(int error, const EventRemoveAllPtr &event)
707 if (error != CONTACTS_ERROR_NONE) {
708 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
711 EventRequestReceiver<EventRemoveAll>::ManualAnswer(event);
713 std::vector<EventRemoveAllDataPtr>::iterator it;
714 for (it = m_removeAllEvent.begin(); it != m_removeAllEvent.end(); ++it) {
715 if ((*it)->getEventPtrs() == event) {
716 m_removeAllEvent.erase(it);
717 LogDebug("event is removed. (" << m_removeAllEvent.size() << ")");
723 CallHistory::Watcher::Watcher(long handle, const EventCallHistoryListenerEmitterPtr& emitter) :
728 m_missedCallList = MissedCallListPtr(new MissedCallList());
729 if (contacts_connect2() == CONTACTS_ERROR_NONE) {
730 LogDebug("Successful to connect Call history DB ");
732 LogDebug("Failed to connect Call history DB ");
736 CallHistory::Watcher::~Watcher()
738 if (contacts_disconnect2() == CONTACTS_ERROR_NONE) {
739 LogDebug("Successful to disconnect Call history DB ");
741 LogDebug("Failed to disconnect Call history DB ");
745 void CallHistory::Watcher::stateHasChanged(CallHistoryEntryListPtr &entryList, EventCallHistoryListener::ResultStates state)
747 if (entryList == NULL)
750 EventCallHistoryListenerPtr event(new EventCallHistoryListener());
751 event->setResultState(state);
752 event->setResult(entryList);
753 m_emitter->emit(event);
756 bool CallHistory::Watcher::parseRecord(contacts_list_h *recordList, CallHistoryEntryListPtr &entryList)
758 CallHistory* callHistory = (CallHistory *) this;
759 if (callHistory != NULL) {
760 return callHistory->parseRecord(recordList, entryList);
765 bool CallHistory::Watcher::addMissedCall(CallHistoryEntryListPtr &entryList)
767 if (entryList != NULL) {
768 CallHistoryEntryList::iterator it = entryList->begin();
769 for (; it != entryList->end(); it++) {
770 if ((*it)->getDirection().compare(STR_MISSED_NEW) == 0) {
771 m_missedCallList->push_back((*it)->getEntryId());
779 bool CallHistory::Watcher::updateCurrentMissedCall()
781 CallHistory* callHistory = (CallHistory *) this;
782 if (callHistory != NULL) {
783 setMissedCallList(callHistory->updateCurrentMissedCall());
789 void CallHistory::Watcher::addedListenerCB()
791 CallHistoryEntryListPtr callHistoryListPtr(new CallHistoryEntryList());
792 int ret = CONTACTS_ERROR_NONE;
794 contacts_query_h query = NULL;
795 contacts_filter_h filter = NULL;
796 contacts_list_h recordList = NULL;
798 contacts_query_create(_contacts_phone_log._uri, &query);
799 contacts_filter_create(_contacts_phone_log._uri, &filter);
800 contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_LESS_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VIDEO_BLOCKED);
801 contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
802 contacts_filter_add_int(filter, _contacts_phone_log.id, CONTACTS_MATCH_GREATER_THAN, static_cast<int>(getCurrentLogId()));
804 contacts_query_set_filter(query, filter);
805 contacts_query_set_sort(query, _contacts_phone_log.id, false);
806 ret = contacts_db_get_records_with_query(query, 0, 0, &recordList);
808 if (ret != CONTACTS_ERROR_NONE) {
809 LogDebug("callhistory query error [" << ret << "]");
812 if (parseRecord(&recordList, callHistoryListPtr)) {
813 if (callHistoryListPtr->size() > 0) {
814 setCurrentLogId((*callHistoryListPtr)[0]->getEntryId());
815 stateHasChanged(callHistoryListPtr, EventCallHistoryListener::ADDED);
816 addMissedCall(callHistoryListPtr);
820 contacts_list_destroy(recordList, true);
821 contacts_query_destroy(query);
822 contacts_filter_destroy(filter);
825 void CallHistory::Watcher::changedListenerCB()
827 MissedCallListPtr missedCallListPtr(getMissedCallList());
828 CallHistoryEntryListPtr callHistoryListPtr(new CallHistoryEntryList());
829 int ret = CONTACTS_ERROR_NONE;
831 contacts_query_h query = NULL;
832 contacts_filter_h filter = NULL;
833 contacts_list_h recordList = NULL;
835 if (missedCallListPtr->size() > 0) {
836 contacts_query_create(_contacts_phone_log._uri, &query);
837 contacts_filter_create(_contacts_phone_log._uri, &filter);
838 contacts_filter_add_int(filter, _contacts_phone_log.log_type, CONTACTS_MATCH_LESS_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VIDEO_BLOCKED);
839 contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
841 MissedCallList::iterator itM = missedCallListPtr->begin();
844 if (itM != missedCallListPtr->begin()) {
845 contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
847 contacts_filter_add_int(filter, _contacts_phone_log.id, CONTACTS_MATCH_EQUAL, static_cast<int>(*itM));
849 } while(itM < missedCallListPtr->end());
851 contacts_query_set_filter(query, filter);
852 contacts_query_set_sort(query, _contacts_phone_log.id, false);
853 ret = contacts_db_get_records_with_query(query, 0, 0, &recordList);
855 if (ret != CONTACTS_ERROR_NONE) {
856 LogDebug("callhistory query error [" << ret << "]");
859 if (parseRecord(&recordList, callHistoryListPtr)) {
860 CallHistoryEntryList::iterator itC = callHistoryListPtr->begin();
862 for (;itC != callHistoryListPtr->end();) {
863 if ((*itC)->getDirection().compare(STR_MISSED) != 0) {
864 itC = callHistoryListPtr->erase(itC);
870 if (callHistoryListPtr->size() > 0) {
871 updateCurrentMissedCall();
872 stateHasChanged(callHistoryListPtr, EventCallHistoryListener::CHANGED);
876 contacts_list_destroy(recordList, true);
877 contacts_query_destroy(query);
878 contacts_filter_destroy(filter);