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 * StorageChangesMessageGenerator.cpp
19 * Created on: 2011. 10. 28.
23 #include "StorageChangesMessageGenerator.h"
24 #include "StorageChangesMessageFilterValidatorFactory.h"
25 #include "API/Messaging/ConversationFilterValidatorFactory.h"
27 #include "IMessagingTypes.h"
29 #include <emf-types.h>
31 #include <dpl/log/log.h>
34 using namespace TizenApis::Api::Tizen;
40 const std::string StorageChangesMessageGenerator::STRING_MATCH_EXCACTLY = "EXACTLY";
41 const std::string StorageChangesMessageGenerator::STRING_MATCH_CONTAINS = "CONTAINS";
42 const std::string StorageChangesMessageGenerator::STRING_MATCH_STARTSWITH = "STARTSWITH";
43 const std::string StorageChangesMessageGenerator::STRING_MATCH_ENDSWITH = "ENDSWITH";
44 // const std::string StorageChangesMessageGenerator::STRING_MATCH_CASESENSITIVE = "CASESENSITIVE";
46 const int StorageChangesMessageGenerator::MESSAGE_TYPE_NOT_INITIALIZED = -1;
48 StorageChangesMessageGenerator::StorageChangesMessageGenerator(Api::Messaging::IMessagePtr msg):m_messageType(MESSAGE_TYPE_NOT_INITIALIZED) {
50 m_type = msg->getMessageType();
51 m_folder = msg->getCurrentFolder();
52 m_dateTime = msg->getDateTime();
53 m_from = msg->getFrom();
55 Recipents = msg->getToRecipientsPtr();
57 if(m_type == Api::Messaging::SMS)
59 LogDebug("size :" << Recipents->getRecipientSize());
61 if(Recipents->getRecipientSize()>0)
63 m_to = Recipents->getRecipientsRef();
64 LogDebug("m_to :" << m_to[0]);
67 else if(m_type == Api::Messaging::MMS)
69 LogDebug("to size :" << Recipents->getRecipientSize());
71 Api::Messaging::IMmsPtr mms = DPL::DynamicPointerCast<Api::Messaging::IMms >(msg);
72 if(Recipents->getRecipientSize() > 0)
74 m_to = Recipents->getRecipientsRef();
75 LogDebug("m_to :" << m_to[0]);
78 Recipents = mms->getCcRecipientsPtr();
79 LogDebug("cc size :" << Recipents->getRecipientSize());
81 if(Recipents->getRecipientSize() > 0)
83 m_cc = Recipents->getRecipientsRef();
86 Recipents = mms->getBccRecipientsPtr();
87 LogDebug("bcc size :" << Recipents->getRecipientSize());
89 if(Recipents->getRecipientSize() > 0)
91 m_bcc = Recipents->getRecipientsRef();
94 else if(m_type == Api::Messaging::EMAIL)
96 LogDebug("to size :" << Recipents->getRecipientSize());
98 Api::Messaging::IEmailPtr email = DPL::DynamicPointerCast<Api::Messaging::IEmail >(msg);
99 if(Recipents->getRecipientSize() > 0)
101 m_to = Recipents->getRecipientsRef();
102 LogDebug("m_to :" << m_to[0]);
105 Recipents = email->getCcRecipientsPtr();
106 LogDebug("cc size :" << Recipents->getRecipientSize());
108 if(Recipents->getRecipientSize() > 0)
110 m_cc = Recipents->getRecipientsRef();
113 Recipents = email->getBccRecipientsPtr();
114 LogDebug("bcc size :" << Recipents->getRecipientSize());
116 if(Recipents->getRecipientSize() > 0)
118 m_bcc = Recipents->getRecipientsRef();
125 m_body = msg->getBody();
126 m_isRead = msg->isRead();
127 m_priority = msg->getPriority();
128 m_subject = msg->getSubject();
130 LogDebug("m_type:" << m_type );
131 LogDebug("m_folder:" << m_folder );
132 LogDebug("m_dateTime year:" << m_dateTime.tm_year + 1900 );
133 LogDebug("m_dateTime mon:" << m_dateTime.tm_mon );
134 LogDebug("m_dateTime day:" << m_dateTime.tm_mday );
135 LogDebug("m_dateTime hour:" << m_dateTime.tm_hour );
136 LogDebug("m_dateTime min:" << m_dateTime.tm_min );
137 LogDebug("m_dateTime sec:" << m_dateTime.tm_sec );
138 LogDebug("m_from:" << m_from );
139 LogDebug("m_body:" << m_body );
140 LogDebug("m_isRead:" << m_isRead );
141 LogDebug("m_priority:" << m_priority );
142 LogDebug("m_subject:" << m_subject );
146 StorageChangesMessageGenerator::~StorageChangesMessageGenerator() {
149 void StorageChangesMessageGenerator::visitPreComposite(Api::Tizen::FilterType& type, int depth){
151 m_operand.push_back(leftblank);
152 LogDebug("Left_blank");
156 void StorageChangesMessageGenerator::visitInComposite(Api::Tizen::FilterType& type, int depth){
158 if(type == UNION_FILTER ){
159 LogDebug("UNION_FILTER");
160 m_operand.push_back(operandUnion);
161 }else if(type == INTERSECTION_FILTER){
162 LogDebug("INTERSECTION_FILTER");
163 m_operand.push_back(operandIntersection);
165 LogError("[ERROR] invalid Filter type:" << type);
171 void StorageChangesMessageGenerator::visitPostComposite(Api::Tizen::FilterType& type, int depth){
176 LogDebug("m_result size =" << m_result.size());
177 data1 = m_result.back();
179 data2 = m_result.back();
181 LogDebug("m_result size =" << m_result.size());
182 LogDebug("m_operand.back() =" << m_operand.back());
183 LogDebug("m_operand size =" << m_operand.size());
184 if(m_operand.back() == operandUnion)
187 LogDebug("data1 = " << data1);
188 LogDebug("data2 = " << data2);
189 LogDebug("data1 || data2 = " << (data1 || data2));
190 m_operand.pop_back();
191 m_result.push_back(data1 || data2);
192 LogDebug("result" << m_result.back());
193 LogDebug("m_result size =" << m_result.size());
195 else if(m_operand.back() == operandIntersection)
197 LogDebug("Intersection");
198 LogDebug("data1 = " << data1);
199 LogDebug("data2 = " << data2);
200 LogDebug("data1 && data2 = " << (data1 && data2));
201 m_operand.pop_back();
202 m_result.push_back(data1 && data2);
203 LogDebug("result" << m_result.back());
204 LogDebug("m_result size =" << m_result.size());
207 }while(m_operand.back() != leftblank);
208 m_operand.pop_back();
209 LogDebug("elase leftblank from m_operand");
210 LogDebug("m_operand size =" << m_operand.size());
214 struct tm StorageChangesMessageGenerator::convertToTimeFormat(const std::string& timeString){
215 LogDebug("<<< timeString:[" << timeString << "]");
222 tm_Time.tm_year = atoi(timeString.substr(0, yearLength).c_str())-1900;
223 nextStart = nextStart + yearLength;
224 LogDebug("<<< tm_Time.tm_year:[" << tm_Time.tm_year << "]");
227 tm_Time.tm_mon = atoi(timeString.substr(nextStart, monthLength).c_str());
228 nextStart = nextStart + monthLength;
229 LogDebug("<<< initTime.tm_mon:[" << tm_Time.tm_mon << "]");
232 tm_Time.tm_mday = atoi(timeString.substr(nextStart, dateLength).c_str());
233 nextStart = nextStart + dateLength;
234 LogDebug("<<< initTime.tm_mday:[" << tm_Time.tm_mday << "]");
237 tm_Time.tm_hour = atoi(timeString.substr(nextStart, hourLength).c_str());
238 nextStart = nextStart + hourLength;
239 LogDebug("<<< initTime.tm_hour:[" << tm_Time.tm_hour << "]");
241 int minuteLength = 2;
242 tm_Time.tm_min = atoi(timeString.substr(nextStart, minuteLength).c_str());
243 nextStart = nextStart + minuteLength;
244 LogDebug("<<< initTime.tm_min:[" << tm_Time.tm_min << "]");
246 int secondLength = 2;
247 tm_Time.tm_sec = atoi(timeString.substr(nextStart, secondLength).c_str());
248 LogDebug("<<< initTime.tm_sec:[" << tm_Time.tm_sec << "]");
253 bool StorageChangesMessageGenerator::getMatchExactlyClause(std::string& attrName, Api::Tizen::AnyPtr& value)
255 std::string valueString = value->toString();
256 LogDebug("<<< attrName : " << attrName);
257 LogDebug("<<< valueString : " << valueString);
258 LogDebug("<<< attrName.compare(valueString) : " << attrName.compare(valueString));
260 if(attrName.compare(valueString) == 0)
262 LogDebug("<<< getMatchExactlyClause SAME >>>");
266 LogDebug("<<< getMatchExactlyClause DIFF >>>");
271 bool StorageChangesMessageGenerator::getMatchContainsClause(std::string& attrName, Api::Tizen::AnyPtr& value)
273 std::string valueString = value->toString();
274 LogDebug("<<< attrName : " << attrName);
275 LogDebug("<<< valueString : " << valueString);
276 LogDebug("<<< attrName.find(valueString) : " << attrName.find(valueString));
278 if(attrName.find(valueString) != std::string::npos)
280 LogDebug("<<< getMatchContainsClause CONTAINS >>>");
283 LogDebug("<<< getMatchContainsClause NOT CONTAINS >>>");
288 bool StorageChangesMessageGenerator::getMatchStartwithClause(std::string& attrName, Api::Tizen::AnyPtr& value)
290 std::string valueString = value->toString();
291 LogDebug("<<< attrName : " << attrName);
292 LogDebug("<<< valueString : " << valueString);
293 LogDebug("<<< attrName.compare(0,valueString.size(), valueString) : " << attrName.compare(0,valueString.size(), valueString));
296 if(attrName.size()-valueString.size() < 0)
299 if(attrName.compare(0,valueString.size(), valueString) == 0)
301 LogDebug("<<< getMatchStartwithClause START WITH >>>");
305 LogDebug("<<< getMatchStartwithClause NOT START WITH >>>");
310 bool StorageChangesMessageGenerator::getMatchEndwithClause(std::string& attrName, Api::Tizen::AnyPtr& value)
312 std::string valueString = value->toString();
314 LogDebug("<<< attrName : " << attrName);
315 LogDebug("<<< valueString : " << valueString);
316 LogDebug("<<< attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString) : " << attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString));
319 if(attrName.size()-valueString.size() < 0)
322 if(attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString) == 0 )
324 LogDebug("<<< getMatchEndwithClause END WITH >>>");
328 LogDebug("<<< getMatchEndwithClause NOT END WITH >>>");
333 bool StorageChangesMessageGenerator::getBetweenRangeClause(struct tm initialValue, struct tm endValue)
335 time_t init_time, mid_time, end_time;
336 init_time = mktime(&initialValue);
337 mid_time = mktime(&m_dateTime);
338 end_time = mktime(&endValue);
339 LogDebug("<<< mktime(initialValue):[" << init_time << "]");
340 LogDebug("<<< mktime(m_dateTime):[" << mid_time << "]");
341 LogDebug("<<< mktime(endValue):[" << end_time << "]");
342 LogDebug("<<< mid_time - init_time:[" << mid_time - init_time << "]");
343 LogDebug("<<< end_time - mid_time:[" << end_time - mid_time << "]");
345 if(((mid_time - init_time) >= 0 ) && ((end_time - mid_time) >= 0 ))
352 void StorageChangesMessageGenerator::visitAttribute(std::string& attrName,
353 Api::Tizen::AnyArrayPtr& values, std::string& matchFlag, bool caseSensitive, int depth){
354 LogDebug("<<< attrName:[" << attrName << "], matchFlag:[" << matchFlag << "]");
355 LogDebug("values->size():" << values->size());
357 Api::Tizen::FilterType filterType = UNION_FILTER;
359 if(matchFlag.compare("EXIST")==0){
360 //TODO implement for EXIST
361 }else if(values->size() == 1){
362 AnyPtr matchValue = values->at(0);
363 visitAttributeEach(attrName, matchValue, matchFlag, depth);
365 visitPreComposite(filterType, depth);
367 AnyArray::iterator iter;
368 for(iter=values->begin(); iter!= values->end(); iter++){
370 if(iter != values->begin()){
372 visitInComposite(filterType, depth);
375 AnyPtr matchValue = *iter;
376 visitAttributeEach(attrName, matchValue, matchFlag, depth);
379 visitPostComposite(filterType, depth);
384 void StorageChangesMessageGenerator::visitAttributeEach(std::string& attrName, Api::Tizen::AnyPtr& value, std::string& matchFlag, int depth){
385 LogDebug("<<< attrName:[" << attrName << "], value:[" << value->toString() << "]");
386 std::string valueString;
388 // Check msg_type and filter_type
389 if(attrName.compare("type")==0){
390 LogDebug("<<< value:[" << value << "]");
391 m_currentType = value->toString();
392 m_messageType = convertMessageType(m_currentType);
393 if((int)m_type == m_messageType)
395 m_result.push_back(TRUE);
396 LogDebug("<<< Type is same");
400 m_result.push_back(FALSE);
401 LogDebug("<<< Type is different");
405 if(attrName.compare("folder")==0){
406 LogDebug("<<< value:[" << value << "]");
407 std::string convertType = value->toString();
408 int nType = convertFolderType(convertType);
409 if((int)m_folder == nType)
411 m_result.push_back(TRUE);
412 LogDebug("<<< folder is same");
416 m_result.push_back(FALSE);
417 LogDebug("<<< folder is different");
421 if(attrName.compare("isRead")==0){
422 LogDebug("<<< value:[" << value << "]");
423 int nType = value->toString().compare("true");
424 if((int)m_isRead == nType)
426 m_result.push_back(TRUE);
427 LogDebug("<<< isRead is same");
431 m_result.push_back(FALSE);
432 LogDebug("<<< isRead is different");
436 if(attrName.compare("priority")==0){
437 LogDebug("<<< value:[" << value->toString() << "]");
438 if(value->toString().compare("true") == 0)
440 if((int)m_priority == Api::Messaging::MessagePriority::Priority::HIGH)
442 m_result.push_back(TRUE);
443 LogDebug("<<< priority is HIGH");
447 m_result.push_back(FALSE);
448 LogDebug("<<< priority is LOW OR NORMAL");
453 if((int)m_priority == Api::Messaging::MessagePriority::Priority::HIGH)
455 m_result.push_back(FALSE);
456 LogDebug("<<< priority is HIGH");
460 m_result.push_back(TRUE);
461 LogDebug("<<< priority is LOW OR NORMAL");
466 if(attrName.compare("body")==0){
467 LogDebug("<<< value:[" << value << "]");
468 LogDebug("<<< value->toString():[" << value->toString() << "]");
469 LogDebug("<<< m_body:[" << m_body << "]");
471 valueString = m_body;
472 if(getMatchFlagResult(value, valueString, matchFlag))
474 m_result.push_back(TRUE);
475 LogDebug("<<< body is same");
479 m_result.push_back(FALSE);
480 LogDebug("<<< body is different");
485 if(attrName.compare("subject")==0){
486 LogDebug("<<< value:[" << value << "]");
487 LogDebug("<<< value->toString():[" << value->toString() << "]");
488 LogDebug("<<< m_subject:[" << m_subject << "]");
490 valueString = m_subject;
491 if(getMatchFlagResult(value, valueString, matchFlag))
493 m_result.push_back(TRUE);
494 LogDebug("<<< subject is same");
498 m_result.push_back(FALSE);
499 LogDebug("<<< subject is different");
504 if(attrName.compare("from")==0){
505 LogDebug("<<< value:[" << value << "]");
506 LogDebug("<<< value->toString():[" << value->toString() << "]");
507 LogDebug("<<< m_from:[" << m_from << "]");
509 valueString = m_from;
510 if(getMatchFlagResult(value, valueString, matchFlag))
512 m_result.push_back(TRUE);
513 LogDebug("<<< from is same");
517 m_result.push_back(FALSE);
518 LogDebug("<<< from is different");
523 if(attrName.compare("to")==0){
524 LogDebug("<<< value:[" << value << "]");
525 LogDebug("<<< value->toString():[" << value->toString() << "]");
526 LogDebug("<<< m_to.size :[" << m_to.size() << "]");
529 for(unsigned int i=0; i < m_to.size(); i++)
531 valueString = m_to[i];
532 result = result || getMatchFlagResult(value, valueString, matchFlag);
534 LogDebug("<<< to compare result is :" << result);
535 m_result.push_back(result);
539 if(attrName.compare("cc")==0){
540 LogDebug("<<< value:[" << value << "]");
541 LogDebug("<<< value->toString():[" << value->toString() << "]");
542 LogDebug("<<< m_cc.size :[" << m_cc.size() << "]");
545 for(unsigned int i=0; i < m_cc.size(); i++)
547 valueString = m_cc[i];
548 result = result || getMatchFlagResult(value, valueString, matchFlag);
550 LogDebug("<<< cc compare result is : " << result);
551 m_result.push_back(result);
555 if(attrName.compare("bcc")==0){
556 LogDebug("<<< value:[" << value << "]");
557 LogDebug("<<< value->toString():[" << value->toString() << "]");
558 LogDebug("<<< m_cc.size :[" << m_bcc.size() << "]");
561 for(unsigned int i=0; i < m_bcc.size(); i++)
563 valueString = m_bcc[i];
564 result = result || getMatchFlagResult(value, valueString, matchFlag);
566 LogDebug("<<< bcc compare result is : " << result);
567 m_result.push_back(result);
574 int StorageChangesMessageGenerator::convertMessageType(std::string& stringType){
575 LogDebug("<<< stringType:[" << stringType << "]");
577 int retMessageType = -1;
579 if(stringType.compare("tizen.sms") ==0){
580 retMessageType = Api::Messaging::SMS;
581 }else if(stringType.compare("tizen.mms") ==0){
582 retMessageType = Api::Messaging::MMS;
583 }else if(stringType.compare("tizen.email") ==0){
584 retMessageType = Api::Messaging::EMAIL;
586 LogError("invalid type:[" << stringType << "]");
590 LogDebug(">>> retMessageType:" << retMessageType);
591 return retMessageType;
594 int StorageChangesMessageGenerator::convertFolderType(std::string& stringType){
595 LogDebug("<<< stringType:[" << stringType << "]");
597 int retMessageType = -1;
599 if(stringType.compare("INBOX") ==0){
600 retMessageType = Api::Messaging::INBOX;
601 }else if(stringType.compare("OUTBOX") ==0){
602 retMessageType = Api::Messaging::OUTBOX;
603 }else if(stringType.compare("DRAFTBOX") ==0){
604 retMessageType = Api::Messaging::DRAFTBOX;
605 }else if(stringType.compare("SENTBOX") ==0){
606 retMessageType = Api::Messaging::SENTBOX;
608 LogError("invalid type:[" << stringType << "]");
612 LogDebug(">>> retMessageType:" << retMessageType);
613 return retMessageType;
616 void StorageChangesMessageGenerator::visitAttributeRange(std::string& attrName, Api::Tizen::AnyPtr& initialValue, Api::Tizen::AnyPtr& endValue, int depth) {
621 if(attrName.compare("timestamp")==0){
622 startTime = convertToTimeFormat(initialValue->toString());
623 endTime = convertToTimeFormat(endValue->toString());
624 result = getBetweenRangeClause(startTime, endTime);
627 LogDebug("<<< timestamp is in range");
628 m_result.push_back(TRUE);
630 LogDebug("<<< timestamp is out of range");
631 m_result.push_back(FALSE);
636 bool StorageChangesMessageGenerator::getMatchFlagResult(Api::Tizen::AnyPtr& value, std::string& valueString, std::string& matchFlag)
638 std::vector<std::string>::iterator iter;
640 if(matchFlag.compare(STRING_MATCH_EXCACTLY) == 0){
641 LogDebug("STRING_MATCH_EXCACTLY");
642 result = result || getMatchExactlyClause(valueString, value);
643 }else if(matchFlag.compare(STRING_MATCH_CONTAINS) == 0){
644 LogDebug("STRING_MATCH_CONTAINS");
645 result = result || getMatchContainsClause(valueString, value);
646 }else if(matchFlag.compare(STRING_MATCH_STARTSWITH) == 0){
647 LogDebug("STRING_MATCH_STARTSWITH");
648 result = result || getMatchStartwithClause(valueString, value);
649 }else if(matchFlag.compare(STRING_MATCH_ENDSWITH) == 0){
650 LogDebug("STRING_MATCH_ENDSWITH");
651 result = result || getMatchEndwithClause(valueString, value);
653 LogDebug("[ERROR]invalid match flag : iter:" << *iter);
658 bool StorageChangesMessageGenerator::getCompareResult()
661 LogDebug("m_result.size() " << m_result.size());
662 result = m_result.back();
664 LogDebug("result = " << result);
668 } //namespace Platform
669 } //namespace WrtPlugins