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 * StorageChangesConversationGenerator.cpp
19 * Created on: 2011. 10. 28.
23 #include "StorageChangesConversationGenerator.h"
24 #include "StorageChangesConversationFilterValidatorFactory.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;
41 const int StorageChangesConversationGenerator::MESSAGE_TYPE_NOT_INITIALIZED = -1;
43 StorageChangesConversationGenerator::StorageChangesConversationGenerator(Api::Messaging::IConversationPtr conv):m_messageType(MESSAGE_TYPE_NOT_INITIALIZED) {
45 m_type = conv->getType();
46 m_time = conv->getTime();
47 m_messageCount = conv->getMessageCount();
48 m_unreadMessages = conv->getUnreadMessages();
49 m_preview = conv->getPreview();
50 m_read = conv->getRead();
51 m_from = conv->getFrom();
52 m_subject = conv->getSubject();
55 m_bcc = conv->getBCC();
56 m_lastMessageId = conv->getLastMessageId();
59 timeinfo = localtime( &m_time );
63 StorageChangesConversationGenerator::~StorageChangesConversationGenerator() {
67 void StorageChangesConversationGenerator::visitPreComposite(Api::Tizen::FilterType& type, int depth){
69 m_operand.push_back(leftblank);
70 LogDebug("Left_blank");
74 void StorageChangesConversationGenerator::visitInComposite(Api::Tizen::FilterType& type, int depth){
76 if(type == UNION_FILTER ){
77 LogDebug("UNION_FILTER");
78 m_operand.push_back(operandUnion);
79 }else if(type == INTERSECTION_FILTER){
80 LogDebug("INTERSECTION_FILTER");
81 m_operand.push_back(operandIntersection);
83 LogError("[ERROR] invalid Filter type:" << type);
89 void StorageChangesConversationGenerator::visitPostComposite(Api::Tizen::FilterType& type, int depth){
94 LogDebug("m_result size =" << m_result.size());
95 data1 = m_result.back();
97 data2 = m_result.back();
99 LogDebug("m_result size =" << m_result.size());
100 LogDebug("m_operand.back() =" << m_operand.back());
101 LogDebug("m_operand size =" << m_operand.size());
102 if(m_operand.back() == operandUnion)
105 LogDebug("data1 = " << data1);
106 LogDebug("data2 = " << data2);
107 LogDebug("data1 || data2 = " << (data1 || data2));
108 m_operand.pop_back();
109 m_result.push_back(data1 || data2);
110 LogDebug("result" << m_result.back());
111 LogDebug("m_result size =" << m_result.size());
113 else if(m_operand.back() == operandIntersection)
115 LogDebug("Intersection");
116 LogDebug("data1 = " << data1);
117 LogDebug("data2 = " << data2);
118 LogDebug("data1 && data2 = " << (data1 && data2));
119 m_operand.pop_back();
120 m_result.push_back(data1 && data2);
121 LogDebug("result " << m_result.back());
122 LogDebug("m_result size =" << m_result.size());
125 }while(m_operand.back() != leftblank);
126 m_operand.pop_back();
127 LogDebug("elase leftblank from m_operand");
128 LogDebug("m_operand size =" << m_operand.size());
132 struct tm StorageChangesConversationGenerator::convertToTimeFormat(const std::string& timeString){
133 LogDebug("<<< timeString:[" << timeString << "]");
140 tm_Time.tm_year = atoi(timeString.substr(0, yearLength).c_str())-1900;
141 nextStart = nextStart + yearLength;
142 LogDebug("<<< tm_Time.tm_year:[" << tm_Time.tm_year << "]");
145 tm_Time.tm_mon = atoi(timeString.substr(nextStart, monthLength).c_str());
146 nextStart = nextStart + monthLength;
147 LogDebug("<<< initTime.tm_mon:[" << tm_Time.tm_mon << "]");
150 tm_Time.tm_mday = atoi(timeString.substr(nextStart, dateLength).c_str());
151 nextStart = nextStart + dateLength;
152 LogDebug("<<< initTime.tm_mday:[" << tm_Time.tm_mday << "]");
155 tm_Time.tm_hour = atoi(timeString.substr(nextStart, hourLength).c_str());
156 nextStart = nextStart + hourLength;
157 LogDebug("<<< initTime.tm_hour:[" << tm_Time.tm_hour << "]");
159 int minuteLength = 2;
160 tm_Time.tm_min = atoi(timeString.substr(nextStart, minuteLength).c_str());
161 nextStart = nextStart + minuteLength;
162 LogDebug("<<< initTime.tm_min:[" << tm_Time.tm_min << "]");
164 int secondLength = 2;
165 tm_Time.tm_sec = atoi(timeString.substr(nextStart, secondLength).c_str());
166 LogDebug("<<< initTime.tm_sec:[" << tm_Time.tm_sec << "]");
171 bool StorageChangesConversationGenerator::getMatchExactlyClause(std::string& attrName, Api::Tizen::AnyPtr& value)
173 std::string valueString = value->toString();
174 LogDebug("<<< attrName : " << attrName);
175 LogDebug("<<< valueString : " << valueString);
176 LogDebug("<<< attrName.compare(valueString) : " << attrName.compare(valueString));
178 if(attrName.compare(valueString) == 0)
180 LogDebug("<<< getMatchExactlyClause SAME >>>");
184 LogDebug("<<< getMatchExactlyClause DIFF >>>");
189 bool StorageChangesConversationGenerator::getMatchContainsClause(std::string& attrName, Api::Tizen::AnyPtr& value)
191 std::string valueString = value->toString();
192 LogDebug("<<< attrName : " << attrName);
193 LogDebug("<<< valueString : " << valueString);
194 LogDebug("<<< attrName.find(valueString) : " << attrName.find(valueString));
196 if(attrName.find(valueString) != std::string::npos)
198 LogDebug("<<< getMatchContainsClause CONTAINS >>>");
201 LogDebug("<<< getMatchContainsClause NOT CONTAINS >>>");
206 bool StorageChangesConversationGenerator::getMatchStartwithClause(std::string& attrName, Api::Tizen::AnyPtr& value)
208 std::string valueString = value->toString();
209 LogDebug("<<< attrName : " << attrName);
210 LogDebug("<<< valueString : " << valueString);
211 LogDebug("<<< attrName.compare(0,valueString.size(), valueString) : " << attrName.compare(0,valueString.size(), valueString));
214 if(attrName.size()-valueString.size() < 0)
217 if(attrName.compare(0,valueString.size(), valueString) == 0)
219 LogDebug("<<< getMatchStartwithClause START WITH >>>");
223 LogDebug("<<< getMatchStartwithClause NOT START WITH >>>");
228 bool StorageChangesConversationGenerator::getMatchEndwithClause(std::string& attrName, Api::Tizen::AnyPtr& value)
230 std::string valueString = value->toString();
232 LogDebug("<<< attrName : " << attrName);
233 LogDebug("<<< valueString : " << valueString);
234 LogDebug("<<< attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString) : " << attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString));
237 if(attrName.size()-valueString.size() < 0)
240 if(attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString) == 0 )
242 LogDebug("<<< getMatchEndwithClause END WITH >>>");
246 LogDebug("<<< getMatchEndwithClause NOT END WITH >>>");
251 void StorageChangesConversationGenerator::visitAttribute(std::string& attrName,
252 MatchFlag& matchFlag, AnyArrayPtr& values, int depth){
253 LogDebug("<<< attrName:[" << attrName << "], matchFlag:[" << matchFlag << "]");
254 LogDebug("values->size():" << values->size());
256 Api::Tizen::FilterType filterType = UNION_FILTER;
258 if(matchFlag==Api::Tizen::MATCH_EXISTS){
259 //TODO implement for EXIST
260 }else if(values->size() == 1){
261 AnyPtr matchValue = values->at(0);
262 visitAttributeEach(attrName, matchFlag, matchValue, depth);
264 visitPreComposite(filterType, depth);
266 AnyArray::iterator iter;
267 for(iter=values->begin(); iter!= values->end(); iter++){
269 if(iter != values->begin()){
271 visitInComposite(filterType, depth);
274 AnyPtr matchValue = *iter;
275 visitAttributeEach(attrName, matchFlag, matchValue, depth);
278 visitPostComposite(filterType, depth);
283 void StorageChangesConversationGenerator::visitAttributeEach(std::string& attrName, Api::Tizen::MatchFlag& matchFlag, Api::Tizen::AnyPtr& value, int depth){
284 LogDebug("<<< attrName:[" << attrName << "], value:[" << value->toString() << "]");
285 std::string valueString;
287 // Check msg_type and filter_type
288 if(attrName.compare("type")==0){
289 LogDebug("<<< value:[" << value << "]");
290 std::string convertType = value->toString();
291 int nType = convertMessageType(convertType);
292 if((int)m_type == nType)
294 m_result.push_back(TRUE);
295 LogDebug("<<< type is same");
299 m_result.push_back(FALSE);
300 LogDebug("<<< type is different");
304 if(attrName.compare("messageCount")==0){
305 LogDebug("<<< value:[" << value << "]");
306 int nType = atoi(value->toString().c_str());
307 if((int)m_messageCount == nType)
309 m_result.push_back(TRUE);
310 LogDebug("<<< messageCount is same");
314 m_result.push_back(FALSE);
315 LogDebug("<<< messageCount is different");
319 if(attrName.compare("unreadMessages")==0){
320 LogDebug("<<< value:[" << value << "]");
321 int nType = atoi(value->toString().c_str());
322 if((int)m_unreadMessages == nType)
324 m_result.push_back(TRUE);
325 LogDebug("<<< unreadMessages is same");
329 m_result.push_back(FALSE);
330 LogDebug("<<< unreadMessages is different");
334 if(attrName.compare("isRead")==0){
335 LogDebug("<<< value:[" << value << "]");
336 int nType = value->toString().compare("true");
337 if((int)m_read == nType)
339 m_result.push_back(TRUE);
340 LogDebug("<<< read is same");
344 m_result.push_back(FALSE);
345 LogDebug("<<< read is different");
349 if(attrName.compare("lastMessageId")==0){
350 LogDebug("<<< value:[" << value << "]");
351 int nType = atoi(value->toString().c_str());
352 if((int)m_lastMessageId == nType)
354 m_result.push_back(TRUE);
355 LogDebug("<<< lastMessageId is same");
359 m_result.push_back(FALSE);
360 LogDebug("<<< lastMessageId is different");
365 if(attrName.compare("preview")==0){
366 LogDebug("<<< value:[" << value << "]");
367 LogDebug("<<< value->toString():[" << value->toString() << "]");
368 LogDebug("<<< m_body:[" << m_preview << "]");
370 valueString = m_preview;
371 if(getMatchFlagResult(value, valueString, matchFlag))
373 m_result.push_back(TRUE);
374 LogDebug("<<< preview is same");
378 m_result.push_back(FALSE);
379 LogDebug("<<< preview is different");
383 if(attrName.compare("from")==0){
384 LogDebug("<<< value:[" << value << "]");
385 LogDebug("<<< value->toString():[" << value->toString() << "]");
386 LogDebug("<<< m_from:[" << m_from << "]");
388 valueString = m_from;
389 if(getMatchFlagResult(value, valueString, matchFlag))
391 m_result.push_back(TRUE);
392 LogDebug("<<< from is same");
396 m_result.push_back(FALSE);
397 LogDebug("<<< from is different");
401 if(attrName.compare("subject")==0){
402 LogDebug("<<< value:[" << value << "]");
403 LogDebug("<<< value->toString():[" << value->toString() << "]");
404 LogDebug("<<< m_subject:[" << m_subject << "]");
406 valueString = m_subject;
407 if(getMatchFlagResult(value, valueString, matchFlag))
409 m_result.push_back(TRUE);
410 LogDebug("<<< subject is same");
414 m_result.push_back(FALSE);
415 LogDebug("<<< subject is different");
420 if(attrName.compare("to")==0){
421 LogDebug("<<< value:[" << value << "]");
422 LogDebug("<<< value->toString():[" << value->toString() << "]");
423 LogDebug("<<< m_to.size :[" << m_to.size() << "]");
426 for(unsigned int i=0; i < m_to.size(); i++)
428 valueString = m_to[i];
429 result = result || getMatchFlagResult(value, valueString, matchFlag);
431 LogDebug("<<< to compare result is :" << result);
432 m_result.push_back(result);
436 if(attrName.compare("cc")==0){
437 LogDebug("<<< value:[" << value << "]");
438 LogDebug("<<< value->toString():[" << value->toString() << "]");
439 LogDebug("<<< m_cc.size :[" << m_cc.size() << "]");
442 for(unsigned int i=0; i < m_cc.size(); i++)
444 valueString = m_cc[i];
445 result = result || getMatchFlagResult(value, valueString, matchFlag);
447 LogDebug("<<< cc compare result is : " << result);
448 m_result.push_back(result);
452 if(attrName.compare("bcc")==0){
453 LogDebug("<<< value:[" << value << "]");
454 LogDebug("<<< value->toString():[" << value->toString() << "]");
455 LogDebug("<<< m_cc.size :[" << m_bcc.size() << "]");
458 for(unsigned int i=0; i < m_bcc.size(); i++)
460 valueString = m_bcc[i];
461 result = result || getMatchFlagResult(value, valueString, matchFlag);
463 LogDebug("<<< bcc compare result is : " << result);
464 m_result.push_back(result);
471 int StorageChangesConversationGenerator::convertMessageType(std::string& stringType){
472 LogDebug("<<< stringType:[" << stringType << "]");
474 int retMessageType = -1;
476 if(stringType.compare("messaging.sms") ==0){
477 retMessageType = Api::Messaging::SMS;
478 }else if(stringType.compare("messaging.mms") ==0){
479 retMessageType = Api::Messaging::MMS;
480 }else if(stringType.compare("messaging.email") ==0){
481 retMessageType = Api::Messaging::EMAIL;
483 LogError("invalid type:[" << stringType << "]");
487 LogDebug(">>> retMessageType:" << retMessageType);
488 return retMessageType;
491 std::string StorageChangesConversationGenerator::convertBooleanStringToIntegerString(std::string& booleanString){
492 std::string retString;
494 if(booleanString.compare("true") == 0){
496 }else if(booleanString.compare("false") == 0){
499 LogError("invalid booleanString:[" << booleanString << "]");
500 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid booleanString :[" + booleanString + "]");
505 void StorageChangesConversationGenerator::visitAttributeRange(std::string& attrName, Api::Tizen::AnyPtr& initialValue, Api::Tizen::AnyPtr& endValue, int depth) {
506 time_t init_time, mid_time, end_time;
509 if(attrName.compare("timestamp")==0){
510 retValue = convertToTimeFormat(initialValue->toString());
511 init_time = mktime(&retValue);
515 retValue = convertToTimeFormat(endValue->toString());
516 end_time = mktime(&retValue);
518 LogDebug("<<< mktime(initialValue):[" << init_time << "]");
519 LogDebug("<<< mktime(m_dateTime):[" << mid_time << "]");
520 LogDebug("<<< mktime(endValue):[" << end_time << "]");
521 LogDebug("<<< mid_time - init_time:[" << mid_time - init_time << "]");
522 LogDebug("<<< end_time - mid_time:[" << end_time - mid_time << "]");
524 if(((mid_time - init_time) >= 0 ) && ((end_time - mid_time) >= 0 ))
526 LogDebug("<<< timestamp is in range");
527 m_result.push_back(TRUE);
531 LogDebug("<<< timestamp is out of range");
532 m_result.push_back(FALSE);
537 bool StorageChangesConversationGenerator::getMatchFlagResult(Api::Tizen::AnyPtr& value, std::string& valueString, Api::Tizen::MatchFlag& matchFlag)
539 std::vector<std::string>::iterator iter;
541 if(matchFlag == Api::Tizen::MATCH_EXACTLY){
542 LogDebug("STRING_MATCH_EXCACTLY");
543 result = result || getMatchExactlyClause(valueString, value);
544 }else if(matchFlag == Api::Tizen::MATCH_CONTAINS){
545 LogDebug("STRING_MATCH_CONTAINS");
546 result = result || getMatchContainsClause(valueString, value);
547 }else if(matchFlag == Api::Tizen::MATCH_STARTSWITH){
548 LogDebug("STRING_MATCH_STARTSWITH");
549 result = result || getMatchStartwithClause(valueString, value);
550 }else if(matchFlag == Api::Tizen::MATCH_ENDSWITH){
551 LogDebug("STRING_MATCH_ENDSWITH");
552 result = result || getMatchEndwithClause(valueString, value);
554 LogDebug("[ERROR]invalid match flag : iter:" << *iter);
559 bool StorageChangesConversationGenerator::getCompareResult()
562 LogDebug("m_result.size() " << m_result.size());
563 result = m_result.back();
565 LogDebug("result = " << result);
569 } //namespace Platform
570 } //namespace WrtPlugins