tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / DEPRACATED / Telephony / LogManager.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
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  * @file        LogManager.cpp
18  * @author      Lukasz Marek (l.marek@samsung.com)
19  * @version     0.2
20  */
21
22 #include <dpl/log/log.h>
23 #include <contacts-svc.h>
24 #include <vector>
25 #include "LogManager.h"
26 #include "LogEntryWrapper.h"
27
28 using namespace WrtPlugins::Api;
29
30 namespace WrtPlugins {
31 namespace Platform {
32 int LogManager::m_instanceCount = 0;
33
34 LogManager::LogManager()
35 {
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");
41         }
42     }
43     m_instanceCount++;
44 }
45
46 LogManager::~LogManager()
47 {
48     LogDebug("entered");
49     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
50     m_instanceCount--;
51     if (m_instanceCount == 0) {
52         LogDebug("closing calllog DB");
53         contacts_svc_disconnect();
54     }
55 }
56
57 void LogManager::OnRequestReceived(const Api::IEventGetNumberOfLogsPtr &event)
58 {
59     LogDebug("entered");
60     Try
61     {
62         int result = getNumberOfLogsInternal(event->getFilter());
63         event->setNumberOfLogs(result);
64     }
65     Catch(Commons::Exception)
66     {
67         LogError("Error during searching logs");
68         return;
69     }
70     event->setResult(true);
71 }
72
73 void LogManager::OnRequestReceived(const Api::IEventFindLogEntriesPtr &event)
74 {
75     LogDebug("entered");
76     Try
77     {
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++) {
82             event->addLog(*it);
83         }
84     }
85     Catch(Commons::Exception)
86     {
87         LogError("Error during searching logs");
88         return;
89     }
90     event->setResult(true);
91 }
92
93 void LogManager::OnRequestReceived(const Api::IEventDeleteLogEntryPtr &event)
94 {
95     LogDebug("entered");
96     Try
97     {
98         if (!event->getFilter() || !event->getFilter()->getIdIsSet()) {
99             return;
100         }
101         deleteLogEntryInternal(event->getFilter()->getIdFilter());
102     }
103     Catch(Commons::Exception)
104     {
105         LogError("Error during deleting log");
106         return;
107     }
108     event->setResult(true);
109 }
110
111 void LogManager::OnRequestReceived(const Api::IEventClearLogPtr &event)
112 {
113     LogDebug("entered");
114     Try
115     {
116         clearLogInternal(event->getFilter());
117     }
118     Catch(Commons::Exception)
119     {
120         LogError("Error during deleting logs");
121         return;
122     }
123     event->setResult(true);
124 }
125
126 int LogManager::getNumberOfLogsInternal(const LogFilterPtr &filter)
127 {
128     LogDebug("entered");
129     if (!filter) {
130         LogWarning("filter is NULL");
131         return 0;
132     }
133     int result = 0;
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);
138         if (NULL == log) {
139             LogError("funtion returned NULL value");
140             continue;
141         }
142         LogEntryWrapperPtr wrapper(new LogEntryWrapper(log));
143         if (wrapper->matchFilters(filter)) {
144             result++;
145         }
146         contacts_svc_value_free(log);
147     }
148     contacts_svc_iter_remove(iter);
149     return result;
150 }
151
152 std::vector<Api::LogEntryPtr> LogManager::findLogEntriesInternal(
153         const LogFilterPtr &filter,
154         const int minIndex,
155         const int maxIndex)
156 {
157     LogDebug("entered");
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);
163         if (NULL == log) {
164             LogError("funtion returned NULL value");
165             continue;
166         }
167         LogEntryWrapperPtr wrapper(new LogEntryWrapper(log));
168         if (wrapper->matchFilters(filter)) {
169             result.push_back(wrapper->getAbstractCall());
170         }
171         contacts_svc_value_free(log);
172     }
173     contacts_svc_iter_remove(iter);
174     return result;
175 }
176
177 void LogManager::deleteLogEntryInternal(int id) const
178 {
179     LogDebug("entered");
180     if (CTS_SUCCESS != contacts_svc_delete_phonelog(CTS_PLOG_DEL_BY_ID, id)) {
181         ThrowMsg(Commons::PlatformException, "Cannot delete log");
182     }
183     return;
184 }
185
186 void LogManager::clearLogInternal(const LogFilterPtr &filter)
187 {
188     LogDebug("entered");
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);
193         if (NULL == log) {
194             LogError("funtion returned NULL value");
195             continue;
196         }
197         LogEntryWrapperPtr wrapper(new LogEntryWrapper(log));
198         if (wrapper->matchFilters(filter)) {
199             deleteLogEntryInternal(wrapper->getAbstractCall()->getId());
200         }
201         contacts_svc_value_free(log);
202     }
203     contacts_svc_iter_remove(iter);
204     return;
205 }
206 }
207 }