2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file LogManager.cpp
18 * @author Lukasz Marek (l.marek@samsung.com)
22 #include <dpl/log/log.h>
23 #include <contacts-svc.h>
25 #include "LogManager.h"
26 #include "LogEntryWrapper.h"
28 using namespace WrtPlugins::Api;
30 namespace WrtPlugins {
32 int LogManager::m_instanceCount = 0;
34 LogManager::LogManager()
36 DPL::Mutex::ScopedLock mx(&m_constructorMutex);
37 if (m_instanceCount == 0) {
38 LogDebug("opening calllog DB");
39 if (CTS_SUCCESS != contacts_svc_connect()) {
40 LogError("database not opened");
46 LogManager::~LogManager()
49 DPL::Mutex::ScopedLock mx(&m_constructorMutex);
51 if (m_instanceCount == 0) {
52 LogDebug("closing calllog DB");
53 contacts_svc_disconnect();
57 void LogManager::OnRequestReceived(const Api::IEventGetNumberOfLogsPtr &event)
62 int result = getNumberOfLogsInternal(event->getFilter());
63 event->setNumberOfLogs(result);
65 Catch(Commons::Exception)
67 LogError("Error during searching logs");
70 event->setResult(true);
73 void LogManager::OnRequestReceived(const Api::IEventFindLogEntriesPtr &event)
78 std::vector<LogEntryPtr> result = findLogEntriesInternal(
79 event->getFilter(), event->getFirstCall(), event->getLastCall());
80 std::vector<LogEntryPtr>::const_iterator it;
81 for (it = result.begin(); it != result.end(); it++) {
85 Catch(Commons::Exception)
87 LogError("Error during searching logs");
90 event->setResult(true);
93 void LogManager::OnRequestReceived(const Api::IEventDeleteLogEntryPtr &event)
98 if (!event->getFilter() || !event->getFilter()->getIdIsSet()) {
101 deleteLogEntryInternal(event->getFilter()->getIdFilter());
103 Catch(Commons::Exception)
105 LogError("Error during deleting log");
108 event->setResult(true);
111 void LogManager::OnRequestReceived(const Api::IEventClearLogPtr &event)
116 clearLogInternal(event->getFilter());
118 Catch(Commons::Exception)
120 LogError("Error during deleting logs");
123 event->setResult(true);
126 int LogManager::getNumberOfLogsInternal(const LogFilterPtr &filter)
130 LogWarning("filter is NULL");
134 CTSiter *iter = NULL;
135 contacts_svc_get_list(CTS_LIST_GROUPING_PLOG, &iter);
136 while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
137 CTSvalue *log = contacts_svc_iter_get_info(iter);
139 LogError("funtion returned NULL value");
142 LogEntryWrapperPtr wrapper(new LogEntryWrapper(log));
143 if (wrapper->matchFilters(filter)) {
146 contacts_svc_value_free(log);
148 contacts_svc_iter_remove(iter);
152 std::vector<Api::LogEntryPtr> LogManager::findLogEntriesInternal(
153 const LogFilterPtr &filter,
158 std::vector<LogEntryPtr> result;
159 CTSiter *iter = NULL;
160 contacts_svc_get_list(CTS_LIST_GROUPING_PLOG, &iter);
161 while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
162 CTSvalue *log = contacts_svc_iter_get_info(iter);
164 LogError("funtion returned NULL value");
167 LogEntryWrapperPtr wrapper(new LogEntryWrapper(log));
168 if (wrapper->matchFilters(filter)) {
169 result.push_back(wrapper->getAbstractCall());
171 contacts_svc_value_free(log);
173 contacts_svc_iter_remove(iter);
177 void LogManager::deleteLogEntryInternal(int id) const
180 if (CTS_SUCCESS != contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_ID, id)) {
181 ThrowMsg(Commons::PlatformException, "Cannot delete log");
186 void LogManager::clearLogInternal(const LogFilterPtr &filter)
189 CTSiter *iter = NULL;
190 contacts_svc_get_list(CTS_LIST_GROUPING_PLOG, &iter);
191 while (CTS_SUCCESS == contacts_svc_iter_next(iter)) {
192 CTSvalue *log = contacts_svc_iter_get_info(iter);
194 LogError("funtion returned NULL value");
197 LogEntryWrapperPtr wrapper(new LogEntryWrapper(log));
198 if (wrapper->matchFilters(filter)) {
199 deleteLogEntryInternal(wrapper->getAbstractCall()->getId());
201 contacts_svc_value_free(log);
203 contacts_svc_iter_remove(iter);