tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / API / Messaging / MessageFilter.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  *
18  *
19  * @file       MessageFilter.cpp
20  * @author     Pawel Misiak (p.misiak@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24 #include "MessageFilter.h"
25 #include <dpl/log/log.h>
26 #include <Commons/RegexUtils.h>
27 #include <ctime>
28
29 namespace {
30 const time_t MKTIME_ERROR = static_cast<time_t>(-1);
31 const int YEAR_OFFSET = 1900;
32 }
33
34 using namespace std;
35
36 namespace WrtDeviceApis {
37 namespace Messaging {
38 namespace Api {
39 MessageFilter::MessageFilter() :
40     m_msgIdCheck(false),
41     m_startTimeCheck(false),
42     m_endTimeCheck(false),
43     m_toCheck(false),
44     m_ccCheck(false),
45     m_bccCheck(false),
46     m_subjectCheck(false),
47     m_bodyCheck(false),
48     m_isReadCheck(false),
49     m_fromCheck(false),
50     m_messagePriorityCheck(false)
51 {
52     LogDebug("message filter creation");
53 }
54
55 MessageFilter::~MessageFilter()
56 {
57 }
58
59 void MessageFilter::setMsgId(const std::string& value)
60 {
61     m_msgId = value;
62     m_msgIdCheck = true;
63 }
64
65 void MessageFilter::setTo(const Recipients& value)
66 {
67     m_to = value;
68     m_toCheck = true;
69 }
70
71 void MessageFilter::setCc(const Recipients& value)
72 {
73     m_cc = value;
74     m_ccCheck = true;
75 }
76
77 void MessageFilter::setBcc(const Recipients& value)
78 {
79     m_bcc = value;
80     m_bccCheck = true;
81 }
82
83 void MessageFilter::setSubject(const std::string& value)
84 {
85     m_subject = value;
86     m_subjectCheck = true;
87 }
88
89 void MessageFilter::setBody(const std::string& value)
90 {
91     m_body = value;
92     m_bodyCheck = true;
93 }
94
95 void MessageFilter::setFrom(const string& value)
96 {
97     m_from = value;
98     m_fromCheck = true;
99 }
100
101 void MessageFilter::setStartTime(const time_t& value)
102 {
103     m_startTime = value;
104     m_startTimeCheck = true;
105 }
106
107 void MessageFilter::setEndTime(const time_t& value)
108 {
109     m_endTime = value;
110     m_endTimeCheck = true;
111 }
112
113 void MessageFilter::setIsRead(bool value)
114 {
115     m_isRead = value;
116     m_isReadCheck = true;
117 }
118
119 void MessageFilter::setMessagePriority(const MessagePriority::Priority& value)
120 {
121     m_messagePriority = value;
122     m_messagePriorityCheck = true;
123 }
124
125 pcrecpp::RE MessageFilter::reCreate(const std::string& value) const
126 {
127     // conversion example:
128     // ^$ <- (empty string)
129     // ^a$ <- a
130     // ^a.* <- a%
131     // .*a$ <- %a
132     // .*a.* <- %a%
133     // ^\..*a.* <- .%a%
134
135     string tmpRe;
136     if (!value.empty()) {
137         tmpRe = Commons::preparePercent(value);
138     }
139     return pcrecpp::RE(tmpRe);
140 }
141
142 bool MessageFilter::idCheck(const std::string& value) const
143 {
144     if (m_msgIdCheck) {
145         pcrecpp::RE l_re = reCreate(m_msgId);
146         if (!l_re.FullMatch(value)) {
147             return false;
148         }
149     }
150     return true;
151 }
152
153 bool MessageFilter::timeCheck(tm value) const
154 {
155     if (value.tm_year > YEAR_OFFSET) {
156         value.tm_year -= YEAR_OFFSET;
157     }
158     time_t time = mktime(&value);
159     if (MKTIME_ERROR == time) {
160         return false;
161     }
162
163     return (!m_startTimeCheck ||
164             time >= m_startTime) && (!m_endTimeCheck || time <= m_endTime);
165 }
166
167 bool MessageFilter::bodyCheck(const std::string& value) const
168 {
169     if (m_bodyCheck) {
170         pcrecpp::RE l_re = reCreate(m_body);
171         if (!l_re.FullMatch(value)) {
172             return false;
173         }
174     }
175     return true;
176 }
177
178 bool MessageFilter::toCheck(const Recipients& value) const
179 {
180     if (m_toCheck) {
181         return recipientCheck(m_to, value);
182     }
183     return true;
184 }
185
186 bool MessageFilter::ccCheck(const Recipients& value) const
187 {
188     if (m_ccCheck) {
189         return recipientCheck(m_cc, value);
190     }
191     return true;
192 }
193
194 bool MessageFilter::bccCheck(const Recipients& value) const
195 {
196     if (m_bccCheck) {
197         return recipientCheck(m_bcc, value);
198     }
199     return true;
200 }
201
202 bool MessageFilter::recipientCheck(const Recipients& filter,
203         const Recipients& value) const
204 {
205     size_t vsize = value.getRecipientSize();
206
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));
210
211         bool match = false;
212
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))) {
216                 match = true;
217                 break;
218             }
219         }
220
221         if (!match) {
222             // none of supplied message recipients was matching this filter element
223             return false;
224         }
225     }
226     return true;
227 }
228
229 bool MessageFilter::fromCheck(const std::string& value) const
230 {
231     if (m_fromCheck) {
232         pcrecpp::RE l_re = reCreate(m_from);
233         if (!l_re.FullMatch(value)) {
234             return false;
235         }
236     }
237     return true;
238 }
239
240 bool MessageFilter::subjectCheck(const std::string & value) const
241 {
242     if (m_subjectCheck) {
243         pcrecpp::RE l_re = reCreate(m_subject);
244         if (!l_re.FullMatch(value)) {
245             return false;
246         }
247     }
248     return true;
249 }
250
251 bool MessageFilter::isReadCheck(bool value) const
252 {
253     if (m_isReadCheck) {
254         return (m_isRead == value);
255     }
256     return true;
257 }
258
259 bool MessageFilter::priorityCheck(MessagePriority::Priority value) const
260 {
261     if (m_messagePriorityCheck) {
262         return (m_messagePriority == value);
263     }
264     return true;
265 }
266
267 bool MessageFilter::isValid() const
268 {
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) {
272         return true;
273     }
274
275     return false;
276 }
277
278 bool MessageFilter::compare(const ISmsPtr & msg) const
279 {
280     if (m_ccCheck ||
281         m_bccCheck ||
282         m_subjectCheck ||
283         m_messagePriorityCheck) {
284         // sms do not support this properties, so find has no effect
285         return false;
286     }
287
288     return (
289                idCheck(msg->getId()) &&
290                bodyCheck(msg->getBody()) &&
291                toCheck(msg->getToRecipients()) &&
292                isReadCheck(msg->isRead()) &&
293                timeCheck(msg->getDateTime()) &&
294                fromCheck(msg->getFrom())
295                );
296 }
297
298 bool MessageFilter::compare(const IMmsPtr & msg) const
299 {
300     if (m_ccCheck ||
301         m_bccCheck ||
302         m_messagePriorityCheck) {
303         // mms do not support this properties, so find has no effect
304         return false;
305     }
306
307     return (
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())
315                );
316 }
317
318 bool MessageFilter::compare(const IEmailPtr & msg) const
319 {
320     return (
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())
331                );
332 }
333 }}
334 }