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 int StorageChangesMessageGenerator::MESSAGE_TYPE_NOT_INITIALIZED = -1;
42 StorageChangesMessageGenerator::StorageChangesMessageGenerator(Api::Messaging::IMessagePtr msg):m_messageType(MESSAGE_TYPE_NOT_INITIALIZED) {
44 m_type = msg->getMessageType();
45 m_folder = msg->getCurrentFolder();
46 m_dateTime = msg->getDateTime();
47 m_from = msg->getFrom();
49 Recipents = msg->getToRecipientsPtr();
51 if(m_type == Api::Messaging::SMS)
53 LogDebug("size :" << Recipents->getRecipientSize());
55 if(Recipents->getRecipientSize()>0)
57 m_to = Recipents->getRecipientsRef();
58 LogDebug("m_to :" << m_to[0]);
61 else if(m_type == Api::Messaging::MMS)
63 LogDebug("to size :" << Recipents->getRecipientSize());
65 Api::Messaging::IMmsPtr mms = DPL::DynamicPointerCast<Api::Messaging::IMms >(msg);
66 if(Recipents->getRecipientSize() > 0)
68 m_to = Recipents->getRecipientsRef();
69 LogDebug("m_to :" << m_to[0]);
72 Recipents = mms->getCcRecipientsPtr();
73 LogDebug("cc size :" << Recipents->getRecipientSize());
75 if(Recipents->getRecipientSize() > 0)
77 m_cc = Recipents->getRecipientsRef();
80 Recipents = mms->getBccRecipientsPtr();
81 LogDebug("bcc size :" << Recipents->getRecipientSize());
83 if(Recipents->getRecipientSize() > 0)
85 m_bcc = Recipents->getRecipientsRef();
88 else if(m_type == Api::Messaging::EMAIL)
90 LogDebug("to size :" << Recipents->getRecipientSize());
92 Api::Messaging::IEmailPtr email = DPL::DynamicPointerCast<Api::Messaging::IEmail >(msg);
93 if(Recipents->getRecipientSize() > 0)
95 m_to = Recipents->getRecipientsRef();
96 LogDebug("m_to :" << m_to[0]);
99 Recipents = email->getCcRecipientsPtr();
100 LogDebug("cc size :" << Recipents->getRecipientSize());
102 if(Recipents->getRecipientSize() > 0)
104 m_cc = Recipents->getRecipientsRef();
107 Recipents = email->getBccRecipientsPtr();
108 LogDebug("bcc size :" << Recipents->getRecipientSize());
110 if(Recipents->getRecipientSize() > 0)
112 m_bcc = Recipents->getRecipientsRef();
119 m_body = msg->getBody();
120 m_isRead = msg->isRead();
121 m_priority = msg->getPriority();
122 m_subject = msg->getSubject();
124 LogDebug("m_type:" << m_type );
125 LogDebug("m_folder:" << m_folder );
126 LogDebug("m_dateTime year:" << m_dateTime.tm_year + 1900 );
127 LogDebug("m_dateTime mon:" << m_dateTime.tm_mon );
128 LogDebug("m_dateTime day:" << m_dateTime.tm_mday );
129 LogDebug("m_dateTime hour:" << m_dateTime.tm_hour );
130 LogDebug("m_dateTime min:" << m_dateTime.tm_min );
131 LogDebug("m_dateTime sec:" << m_dateTime.tm_sec );
132 LogDebug("m_from:" << m_from );
133 LogDebug("m_body:" << m_body );
134 LogDebug("m_isRead:" << m_isRead );
135 LogDebug("m_priority:" << m_priority );
136 LogDebug("m_subject:" << m_subject );
140 StorageChangesMessageGenerator::~StorageChangesMessageGenerator() {
143 void StorageChangesMessageGenerator::visitPreComposite(Api::Tizen::FilterType& type, int depth){
145 m_operand.push_back(leftblank);
146 LogDebug("Left_blank");
150 void StorageChangesMessageGenerator::visitInComposite(Api::Tizen::FilterType& type, int depth){
152 if(type == UNION_FILTER ){
153 LogDebug("UNION_FILTER");
154 m_operand.push_back(operandUnion);
155 }else if(type == INTERSECTION_FILTER){
156 LogDebug("INTERSECTION_FILTER");
157 m_operand.push_back(operandIntersection);
159 LogError("[ERROR] invalid Filter type:" << type);
165 void StorageChangesMessageGenerator::visitPostComposite(Api::Tizen::FilterType& type, int depth){
170 LogDebug("m_result size =" << m_result.size());
171 data1 = m_result.back();
173 data2 = m_result.back();
175 LogDebug("m_result size =" << m_result.size());
176 LogDebug("m_operand.back() =" << m_operand.back());
177 LogDebug("m_operand size =" << m_operand.size());
178 if(m_operand.back() == operandUnion)
181 LogDebug("data1 = " << data1);
182 LogDebug("data2 = " << data2);
183 LogDebug("data1 || data2 = " << (data1 || data2));
184 m_operand.pop_back();
185 m_result.push_back(data1 || data2);
186 LogDebug("result" << m_result.back());
187 LogDebug("m_result size =" << m_result.size());
189 else if(m_operand.back() == operandIntersection)
191 LogDebug("Intersection");
192 LogDebug("data1 = " << data1);
193 LogDebug("data2 = " << data2);
194 LogDebug("data1 && data2 = " << (data1 && data2));
195 m_operand.pop_back();
196 m_result.push_back(data1 && data2);
197 LogDebug("result" << m_result.back());
198 LogDebug("m_result size =" << m_result.size());
201 }while(m_operand.back() != leftblank);
202 m_operand.pop_back();
203 LogDebug("elase leftblank from m_operand");
204 LogDebug("m_operand size =" << m_operand.size());
208 struct tm StorageChangesMessageGenerator::convertToTimeFormat(const std::string& timeString){
209 LogDebug("<<< timeString:[" << timeString << "]");
216 tm_Time.tm_year = atoi(timeString.substr(0, yearLength).c_str())-1900;
217 nextStart = nextStart + yearLength;
218 LogDebug("<<< tm_Time.tm_year:[" << tm_Time.tm_year << "]");
221 tm_Time.tm_mon = atoi(timeString.substr(nextStart, monthLength).c_str());
222 nextStart = nextStart + monthLength;
223 LogDebug("<<< initTime.tm_mon:[" << tm_Time.tm_mon << "]");
226 tm_Time.tm_mday = atoi(timeString.substr(nextStart, dateLength).c_str());
227 nextStart = nextStart + dateLength;
228 LogDebug("<<< initTime.tm_mday:[" << tm_Time.tm_mday << "]");
231 tm_Time.tm_hour = atoi(timeString.substr(nextStart, hourLength).c_str());
232 nextStart = nextStart + hourLength;
233 LogDebug("<<< initTime.tm_hour:[" << tm_Time.tm_hour << "]");
235 int minuteLength = 2;
236 tm_Time.tm_min = atoi(timeString.substr(nextStart, minuteLength).c_str());
237 nextStart = nextStart + minuteLength;
238 LogDebug("<<< initTime.tm_min:[" << tm_Time.tm_min << "]");
240 int secondLength = 2;
241 tm_Time.tm_sec = atoi(timeString.substr(nextStart, secondLength).c_str());
242 LogDebug("<<< initTime.tm_sec:[" << tm_Time.tm_sec << "]");
247 bool StorageChangesMessageGenerator::getMatchExactlyClause(std::string& attrName, Api::Tizen::AnyPtr& value)
249 std::string valueString = value->toString();
250 LogDebug("<<< attrName : " << attrName);
251 LogDebug("<<< valueString : " << valueString);
252 LogDebug("<<< attrName.compare(valueString) : " << attrName.compare(valueString));
254 if(attrName.compare(valueString) == 0)
256 LogDebug("<<< getMatchExactlyClause SAME >>>");
260 LogDebug("<<< getMatchExactlyClause DIFF >>>");
265 bool StorageChangesMessageGenerator::getMatchContainsClause(std::string& attrName, Api::Tizen::AnyPtr& value)
267 std::string valueString = value->toString();
268 LogDebug("<<< attrName : " << attrName);
269 LogDebug("<<< valueString : " << valueString);
270 LogDebug("<<< attrName.find(valueString) : " << attrName.find(valueString));
272 if(attrName.find(valueString) != std::string::npos)
274 LogDebug("<<< getMatchContainsClause CONTAINS >>>");
277 LogDebug("<<< getMatchContainsClause NOT CONTAINS >>>");
282 bool StorageChangesMessageGenerator::getMatchStartwithClause(std::string& attrName, Api::Tizen::AnyPtr& value)
284 std::string valueString = value->toString();
285 LogDebug("<<< attrName : " << attrName);
286 LogDebug("<<< valueString : " << valueString);
287 LogDebug("<<< attrName.compare(0,valueString.size(), valueString) : " << attrName.compare(0,valueString.size(), valueString));
290 if(attrName.size()-valueString.size() < 0)
293 if(attrName.compare(0,valueString.size(), valueString) == 0)
295 LogDebug("<<< getMatchStartwithClause START WITH >>>");
299 LogDebug("<<< getMatchStartwithClause NOT START WITH >>>");
304 bool StorageChangesMessageGenerator::getMatchEndwithClause(std::string& attrName, Api::Tizen::AnyPtr& value)
306 std::string valueString = value->toString();
308 LogDebug("<<< attrName : " << attrName);
309 LogDebug("<<< valueString : " << valueString);
310 LogDebug("<<< attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString) : " << attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString));
313 if(attrName.size()-valueString.size() < 0)
316 if(attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString) == 0 )
318 LogDebug("<<< getMatchEndwithClause END WITH >>>");
322 LogDebug("<<< getMatchEndwithClause NOT END WITH >>>");
327 bool StorageChangesMessageGenerator::getBetweenRangeClause(struct tm initialValue, struct tm endValue)
329 time_t init_time, mid_time, end_time;
330 init_time = mktime(&initialValue);
331 mid_time = mktime(&m_dateTime);
332 end_time = mktime(&endValue);
333 LogDebug("<<< mktime(initialValue):[" << init_time << "]");
334 LogDebug("<<< mktime(m_dateTime):[" << mid_time << "]");
335 LogDebug("<<< mktime(endValue):[" << end_time << "]");
336 LogDebug("<<< mid_time - init_time:[" << mid_time - init_time << "]");
337 LogDebug("<<< end_time - mid_time:[" << end_time - mid_time << "]");
339 if(((mid_time - init_time) >= 0 ) && ((end_time - mid_time) >= 0 ))
346 void StorageChangesMessageGenerator::visitAttribute(std::string& attrName,
347 MatchFlag& matchFlag, Api::Tizen::AnyArrayPtr& values, int depth){
348 LogDebug("<<< attrName:[" << attrName << "], matchFlag:[" << matchFlag << "]");
349 LogDebug("values->size():" << values->size());
351 Api::Tizen::FilterType filterType = UNION_FILTER;
353 if(matchFlag == Api::Tizen::MATCH_EXISTS){
354 //TODO implement for EXIST
355 }else if(values->size() == 1){
356 AnyPtr matchValue = values->at(0);
357 visitAttributeEach(attrName, matchFlag, matchValue, depth);
359 visitPreComposite(filterType, depth);
361 AnyArray::iterator iter;
362 for(iter=values->begin(); iter!= values->end(); iter++){
364 if(iter != values->begin()){
366 visitInComposite(filterType, depth);
369 AnyPtr matchValue = *iter;
370 visitAttributeEach(attrName, matchFlag, matchValue, depth);
373 visitPostComposite(filterType, depth);
378 void StorageChangesMessageGenerator::visitAttributeEach(std::string& attrName, MatchFlag& matchFlag, Api::Tizen::AnyPtr& value, int depth){
379 LogDebug("<<< attrName:[" << attrName << "], value:[" << value->toString() << "]");
380 std::string valueString;
382 // Check msg_type and filter_type
383 if(attrName.compare("type")==0){
384 LogDebug("<<< value:[" << value << "]");
385 m_currentType = value->toString();
386 m_messageType = convertMessageType(m_currentType);
387 if((int)m_type == m_messageType)
389 m_result.push_back(TRUE);
390 LogDebug("<<< Type is same");
394 m_result.push_back(FALSE);
395 LogDebug("<<< Type is different");
399 if(attrName.compare("folder")==0){
400 LogDebug("<<< value:[" << value << "]");
401 std::string convertType = value->toString();
402 int nType = convertFolderType(convertType);
403 if((int)m_folder == nType)
405 m_result.push_back(TRUE);
406 LogDebug("<<< folder is same");
410 m_result.push_back(FALSE);
411 LogDebug("<<< folder is different");
415 if(attrName.compare("isRead")==0){
416 LogDebug("<<< value:[" << value << "]");
417 int nType = value->toString().compare("true");
418 if((int)m_isRead == nType)
420 m_result.push_back(TRUE);
421 LogDebug("<<< isRead is same");
425 m_result.push_back(FALSE);
426 LogDebug("<<< isRead is different");
430 if(attrName.compare("priority")==0){
431 LogDebug("<<< value:[" << value->toString() << "]");
432 if(value->toString().compare("true") == 0)
434 if((int)m_priority == Api::Messaging::MessagePriority::Priority::HIGH)
436 m_result.push_back(TRUE);
437 LogDebug("<<< priority is HIGH");
441 m_result.push_back(FALSE);
442 LogDebug("<<< priority is LOW OR NORMAL");
447 if((int)m_priority == Api::Messaging::MessagePriority::Priority::HIGH)
449 m_result.push_back(FALSE);
450 LogDebug("<<< priority is HIGH");
454 m_result.push_back(TRUE);
455 LogDebug("<<< priority is LOW OR NORMAL");
460 if(attrName.compare("body")==0){
461 LogDebug("<<< value:[" << value << "]");
462 LogDebug("<<< value->toString():[" << value->toString() << "]");
463 LogDebug("<<< m_body:[" << m_body << "]");
465 valueString = m_body;
466 if(getMatchFlagResult(value, valueString, matchFlag))
468 m_result.push_back(TRUE);
469 LogDebug("<<< body is same");
473 m_result.push_back(FALSE);
474 LogDebug("<<< body is different");
479 if(attrName.compare("subject")==0){
480 LogDebug("<<< value:[" << value << "]");
481 LogDebug("<<< value->toString():[" << value->toString() << "]");
482 LogDebug("<<< m_subject:[" << m_subject << "]");
484 valueString = m_subject;
485 if(getMatchFlagResult(value, valueString, matchFlag))
487 m_result.push_back(TRUE);
488 LogDebug("<<< subject is same");
492 m_result.push_back(FALSE);
493 LogDebug("<<< subject is different");
498 if(attrName.compare("from")==0){
499 LogDebug("<<< value:[" << value << "]");
500 LogDebug("<<< value->toString():[" << value->toString() << "]");
501 LogDebug("<<< m_from:[" << m_from << "]");
503 valueString = m_from;
504 if(getMatchFlagResult(value, valueString, matchFlag))
506 m_result.push_back(TRUE);
507 LogDebug("<<< from is same");
511 m_result.push_back(FALSE);
512 LogDebug("<<< from is different");
517 if(attrName.compare("to")==0){
518 LogDebug("<<< value:[" << value << "]");
519 LogDebug("<<< value->toString():[" << value->toString() << "]");
520 LogDebug("<<< m_to.size :[" << m_to.size() << "]");
523 for(unsigned int i=0; i < m_to.size(); i++)
525 valueString = m_to[i];
526 result = result || getMatchFlagResult(value, valueString, matchFlag);
528 LogDebug("<<< to compare result is :" << result);
529 m_result.push_back(result);
533 if(attrName.compare("cc")==0){
534 LogDebug("<<< value:[" << value << "]");
535 LogDebug("<<< value->toString():[" << value->toString() << "]");
536 LogDebug("<<< m_cc.size :[" << m_cc.size() << "]");
539 for(unsigned int i=0; i < m_cc.size(); i++)
541 valueString = m_cc[i];
542 result = result || getMatchFlagResult(value, valueString, matchFlag);
544 LogDebug("<<< cc compare result is : " << result);
545 m_result.push_back(result);
549 if(attrName.compare("bcc")==0){
550 LogDebug("<<< value:[" << value << "]");
551 LogDebug("<<< value->toString():[" << value->toString() << "]");
552 LogDebug("<<< m_cc.size :[" << m_bcc.size() << "]");
555 for(unsigned int i=0; i < m_bcc.size(); i++)
557 valueString = m_bcc[i];
558 result = result || getMatchFlagResult(value, valueString, matchFlag);
560 LogDebug("<<< bcc compare result is : " << result);
561 m_result.push_back(result);
568 int StorageChangesMessageGenerator::convertMessageType(std::string& stringType){
569 LogDebug("<<< stringType:[" << stringType << "]");
571 int retMessageType = -1;
573 if(stringType.compare("messaging.sms") ==0){
574 retMessageType = Api::Messaging::SMS;
575 }else if(stringType.compare("messaging.mms") ==0){
576 retMessageType = Api::Messaging::MMS;
577 }else if(stringType.compare("messaging.email") ==0){
578 retMessageType = Api::Messaging::EMAIL;
580 LogError("invalid type:[" << stringType << "]");
584 LogDebug(">>> retMessageType:" << retMessageType);
585 return retMessageType;
588 int StorageChangesMessageGenerator::convertFolderType(std::string& stringType){
589 LogDebug("<<< stringType:[" << stringType << "]");
591 int retMessageType = -1;
593 if(stringType.compare("INBOX") ==0){
594 retMessageType = Api::Messaging::INBOX;
595 }else if(stringType.compare("OUTBOX") ==0){
596 retMessageType = Api::Messaging::OUTBOX;
597 }else if(stringType.compare("DRAFTBOX") ==0){
598 retMessageType = Api::Messaging::DRAFTBOX;
599 }else if(stringType.compare("SENTBOX") ==0){
600 retMessageType = Api::Messaging::SENTBOX;
602 LogError("invalid type:[" << stringType << "]");
606 LogDebug(">>> retMessageType:" << retMessageType);
607 return retMessageType;
610 void StorageChangesMessageGenerator::visitAttributeRange(std::string& attrName, Api::Tizen::AnyPtr& initialValue, Api::Tizen::AnyPtr& endValue, int depth) {
615 if(attrName.compare("timestamp")==0){
616 startTime = convertToTimeFormat(initialValue->toString());
617 endTime = convertToTimeFormat(endValue->toString());
618 result = getBetweenRangeClause(startTime, endTime);
621 LogDebug("<<< timestamp is in range");
622 m_result.push_back(TRUE);
624 LogDebug("<<< timestamp is out of range");
625 m_result.push_back(FALSE);
630 bool StorageChangesMessageGenerator::getMatchFlagResult(Api::Tizen::AnyPtr& value, std::string& valueString, MatchFlag& matchFlag)
632 std::vector<std::string>::iterator iter;
634 if(matchFlag == Api::Tizen::MATCH_EXACTLY){
635 LogDebug("STRING_MATCH_EXCACTLY");
636 result = result || getMatchExactlyClause(valueString, value);
637 }else if(matchFlag == Api::Tizen::MATCH_CONTAINS){
638 LogDebug("STRING_MATCH_CONTAINS");
639 result = result || getMatchContainsClause(valueString, value);
640 }else if(matchFlag == Api::Tizen::MATCH_STARTSWITH){
641 LogDebug("STRING_MATCH_STARTSWITH");
642 result = result || getMatchStartwithClause(valueString, value);
643 }else if(matchFlag == Api::Tizen::MATCH_ENDSWITH){
644 LogDebug("STRING_MATCH_ENDSWITH");
645 result = result || getMatchEndwithClause(valueString, value);
647 LogDebug("[ERROR]invalid match flag : iter:" << *iter);
652 bool StorageChangesMessageGenerator::getCompareResult()
655 LogDebug("m_result.size() " << m_result.size());
656 result = m_result.back();
658 LogDebug("result = " << result);
662 } //namespace Platform
663 } //namespace WrtPlugins