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.
19 * @file MessageFilter.cpp
20 * @author Pawel Misiak (p.misiak@samsung.com)
24 #include "MessageFilter.h"
25 #include <dpl/log/log.h>
26 #include <Commons/RegexUtils.h>
30 const time_t MKTIME_ERROR = static_cast<time_t>(-1);
31 const int YEAR_OFFSET = 1900;
36 namespace WrtDeviceApis {
39 MessageFilter::MessageFilter() :
41 m_startTimeCheck(false),
42 m_endTimeCheck(false),
46 m_subjectCheck(false),
50 m_messagePriorityCheck(false)
52 LogDebug("message filter creation");
55 MessageFilter::~MessageFilter()
59 void MessageFilter::setMsgId(const std::string& value)
65 void MessageFilter::setTo(const Recipients& value)
71 void MessageFilter::setCc(const Recipients& value)
77 void MessageFilter::setBcc(const Recipients& value)
83 void MessageFilter::setSubject(const std::string& value)
86 m_subjectCheck = true;
89 void MessageFilter::setBody(const std::string& value)
95 void MessageFilter::setFrom(const string& value)
101 void MessageFilter::setStartTime(const time_t& value)
104 m_startTimeCheck = true;
107 void MessageFilter::setEndTime(const time_t& value)
110 m_endTimeCheck = true;
113 void MessageFilter::setIsRead(bool value)
116 m_isReadCheck = true;
119 void MessageFilter::setMessagePriority(const MessagePriority::Priority& value)
121 m_messagePriority = value;
122 m_messagePriorityCheck = true;
125 pcrecpp::RE MessageFilter::reCreate(const std::string& value) const
127 // conversion example:
128 // ^$ <- (empty string)
136 if (!value.empty()) {
137 tmpRe = Commons::preparePercent(value);
139 return pcrecpp::RE(tmpRe);
142 bool MessageFilter::idCheck(const std::string& value) const
145 pcrecpp::RE l_re = reCreate(m_msgId);
146 if (!l_re.FullMatch(value)) {
153 bool MessageFilter::timeCheck(tm value) const
155 if (value.tm_year > YEAR_OFFSET) {
156 value.tm_year -= YEAR_OFFSET;
158 time_t time = mktime(&value);
159 if (MKTIME_ERROR == time) {
163 return (!m_startTimeCheck ||
164 time >= m_startTime) && (!m_endTimeCheck || time <= m_endTime);
167 bool MessageFilter::bodyCheck(const std::string& value) const
170 pcrecpp::RE l_re = reCreate(m_body);
171 if (!l_re.FullMatch(value)) {
178 bool MessageFilter::toCheck(const Recipients& value) const
181 return recipientCheck(m_to, value);
186 bool MessageFilter::ccCheck(const Recipients& value) const
189 return recipientCheck(m_cc, value);
194 bool MessageFilter::bccCheck(const Recipients& value) const
197 return recipientCheck(m_bcc, value);
202 bool MessageFilter::recipientCheck(const Recipients& filter,
203 const Recipients& value) const
205 size_t vsize = value.getRecipientSize();
207 // every filter element must have at least one match
208 for (size_t i = 0; i < filter.getRecipientSize(); i++) {
209 pcrecpp::RE l_re = reCreate(filter.getRecipient(i));
213 // go through all message recipients to find matching one
214 for (size_t v = 0; v < vsize; ++v) {
215 if (l_re.PartialMatch(value.getRecipient(v))) {
222 // none of supplied message recipients was matching this filter element
229 bool MessageFilter::fromCheck(const std::string& value) const
232 pcrecpp::RE l_re = reCreate(m_from);
233 if (!l_re.FullMatch(value)) {
240 bool MessageFilter::subjectCheck(const std::string & value) const
242 if (m_subjectCheck) {
243 pcrecpp::RE l_re = reCreate(m_subject);
244 if (!l_re.FullMatch(value)) {
251 bool MessageFilter::isReadCheck(bool value) const
254 return (m_isRead == value);
259 bool MessageFilter::priorityCheck(MessagePriority::Priority value) const
261 if (m_messagePriorityCheck) {
262 return (m_messagePriority == value);
267 bool MessageFilter::isValid() const
269 if (m_msgIdCheck || m_startTimeCheck || m_endTimeCheck || m_toCheck ||
270 m_ccCheck || m_bccCheck || m_subjectCheck || m_bodyCheck || m_isRead ||
271 m_isReadCheck || m_fromCheck || m_messagePriorityCheck) {
278 bool MessageFilter::compare(const ISmsPtr & msg) const
283 m_messagePriorityCheck) {
284 // sms do not support this properties, so find has no effect
289 idCheck(msg->getId()) &&
290 bodyCheck(msg->getBody()) &&
291 toCheck(msg->getToRecipients()) &&
292 isReadCheck(msg->isRead()) &&
293 timeCheck(msg->getDateTime()) &&
294 fromCheck(msg->getFrom())
298 bool MessageFilter::compare(const IMmsPtr & msg) const
302 m_messagePriorityCheck) {
303 // mms do not support this properties, so find has no effect
308 idCheck(msg->getId()) &&
309 subjectCheck(msg->getSubject()) &&
310 bodyCheck(msg->getBody()) &&
311 toCheck(msg->getToRecipients()) &&
312 isReadCheck(msg->isRead()) &&
313 timeCheck(msg->getDateTime()) &&
314 fromCheck(msg->getFrom())
318 bool MessageFilter::compare(const IEmailPtr & msg) const
321 idCheck(msg->getId()) &&
322 subjectCheck(msg->getSubject()) &&
323 bodyCheck(msg->getBody()) &&
324 fromCheck(msg->getFrom()) &&
325 toCheck(msg->getToRecipients()) &&
326 ccCheck(msg->getCcRecipients()) &&
327 bccCheck(msg->getBccRecipients()) &&
328 isReadCheck(msg->isRead()) &&
329 priorityCheck(msg->getPriority()) &&
330 timeCheck(msg->getDateTime())