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, AnyPtr& matchValue, int depth){
253 LogDebug("<<< attrName:[" << attrName << "], matchFlag:[" << matchFlag << "]");
254 if(matchValue == NULL)
257 LogDebug("matchValue:" << matchValue->toString());
259 visitAttributeEach(attrName, matchFlag, matchValue, depth);
263 void StorageChangesConversationGenerator::visitAttributeEach(std::string& attrName, Api::Tizen::MatchFlag& matchFlag, Api::Tizen::AnyPtr& value, int depth){
264 LogDebug("<<< attrName:[" << attrName << "], value:[" << value->toString() << "]");
265 std::string valueString;
267 // Check msg_type and filter_type
268 if(attrName.compare("type")==0){
269 LogDebug("<<< value:[" << value << "]");
270 std::string convertType = value->toString();
271 int nType = convertMessageType(convertType);
272 if((int)m_type == nType)
274 m_result.push_back(TRUE);
275 LogDebug("<<< type is same");
279 m_result.push_back(FALSE);
280 LogDebug("<<< type is different");
284 if(attrName.compare("messageCount")==0){
285 LogDebug("<<< value:[" << value << "]");
286 int nType = atoi(value->toString().c_str());
287 if((int)m_messageCount == nType)
289 m_result.push_back(TRUE);
290 LogDebug("<<< messageCount is same");
294 m_result.push_back(FALSE);
295 LogDebug("<<< messageCount is different");
299 if(attrName.compare("unreadMessages")==0){
300 LogDebug("<<< value:[" << value << "]");
301 int nType = atoi(value->toString().c_str());
302 if((int)m_unreadMessages == nType)
304 m_result.push_back(TRUE);
305 LogDebug("<<< unreadMessages is same");
309 m_result.push_back(FALSE);
310 LogDebug("<<< unreadMessages is different");
314 if(attrName.compare("isRead")==0){
315 LogDebug("<<< value:[" << value << "]");
316 int nType = value->toString().compare("true");
317 if((int)m_read == nType)
319 m_result.push_back(TRUE);
320 LogDebug("<<< read is same");
324 m_result.push_back(FALSE);
325 LogDebug("<<< read is different");
329 if(attrName.compare("lastMessageId")==0){
330 LogDebug("<<< value:[" << value << "]");
331 int nType = atoi(value->toString().c_str());
332 if((int)m_lastMessageId == nType)
334 m_result.push_back(TRUE);
335 LogDebug("<<< lastMessageId is same");
339 m_result.push_back(FALSE);
340 LogDebug("<<< lastMessageId is different");
345 if(attrName.compare("preview")==0){
346 LogDebug("<<< value:[" << value << "]");
347 LogDebug("<<< value->toString():[" << value->toString() << "]");
348 LogDebug("<<< m_body:[" << m_preview << "]");
350 valueString = m_preview;
351 if(getMatchFlagResult(value, valueString, matchFlag))
353 m_result.push_back(TRUE);
354 LogDebug("<<< preview is same");
358 m_result.push_back(FALSE);
359 LogDebug("<<< preview is different");
363 if(attrName.compare("from")==0){
364 LogDebug("<<< value:[" << value << "]");
365 LogDebug("<<< value->toString():[" << value->toString() << "]");
366 LogDebug("<<< m_from:[" << m_from << "]");
368 valueString = m_from;
369 if(getMatchFlagResult(value, valueString, matchFlag))
371 m_result.push_back(TRUE);
372 LogDebug("<<< from is same");
376 m_result.push_back(FALSE);
377 LogDebug("<<< from is different");
381 if(attrName.compare("subject")==0){
382 LogDebug("<<< value:[" << value << "]");
383 LogDebug("<<< value->toString():[" << value->toString() << "]");
384 LogDebug("<<< m_subject:[" << m_subject << "]");
386 valueString = m_subject;
387 if(getMatchFlagResult(value, valueString, matchFlag))
389 m_result.push_back(TRUE);
390 LogDebug("<<< subject is same");
394 m_result.push_back(FALSE);
395 LogDebug("<<< subject is different");
400 if(attrName.compare("to")==0){
401 LogDebug("<<< value:[" << value << "]");
402 LogDebug("<<< value->toString():[" << value->toString() << "]");
403 LogDebug("<<< m_to.size :[" << m_to.size() << "]");
406 for(unsigned int i=0; i < m_to.size(); i++)
408 valueString = m_to[i];
409 result = result || getMatchFlagResult(value, valueString, matchFlag);
411 LogDebug("<<< to compare result is :" << result);
412 m_result.push_back(result);
416 if(attrName.compare("cc")==0){
417 LogDebug("<<< value:[" << value << "]");
418 LogDebug("<<< value->toString():[" << value->toString() << "]");
419 LogDebug("<<< m_cc.size :[" << m_cc.size() << "]");
422 for(unsigned int i=0; i < m_cc.size(); i++)
424 valueString = m_cc[i];
425 result = result || getMatchFlagResult(value, valueString, matchFlag);
427 LogDebug("<<< cc compare result is : " << result);
428 m_result.push_back(result);
432 if(attrName.compare("bcc")==0){
433 LogDebug("<<< value:[" << value << "]");
434 LogDebug("<<< value->toString():[" << value->toString() << "]");
435 LogDebug("<<< m_cc.size :[" << m_bcc.size() << "]");
438 for(unsigned int i=0; i < m_bcc.size(); i++)
440 valueString = m_bcc[i];
441 result = result || getMatchFlagResult(value, valueString, matchFlag);
443 LogDebug("<<< bcc compare result is : " << result);
444 m_result.push_back(result);
451 int StorageChangesConversationGenerator::convertMessageType(std::string& stringType){
452 LogDebug("<<< stringType:[" << stringType << "]");
454 int retMessageType = -1;
456 if(stringType.compare("messaging.sms") ==0){
457 retMessageType = Api::Messaging::SMS;
458 }else if(stringType.compare("messaging.mms") ==0){
459 retMessageType = Api::Messaging::MMS;
460 }else if(stringType.compare("messaging.email") ==0){
461 retMessageType = Api::Messaging::EMAIL;
463 LogError("invalid type:[" << stringType << "]");
467 LogDebug(">>> retMessageType:" << retMessageType);
468 return retMessageType;
471 std::string StorageChangesConversationGenerator::convertBooleanStringToIntegerString(std::string& booleanString){
472 std::string retString;
474 if(booleanString.compare("true") == 0){
476 }else if(booleanString.compare("false") == 0){
479 LogError("invalid booleanString:[" << booleanString << "]");
480 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid booleanString :[" + booleanString + "]");
485 void StorageChangesConversationGenerator::visitAttributeRange(std::string& attrName, Api::Tizen::AnyPtr& initialValue, Api::Tizen::AnyPtr& endValue, int depth) {
486 time_t init_time, mid_time, end_time;
489 if(initialValue == NULL || endValue == NULL)
492 if(attrName.compare("timestamp")==0){
493 retValue = convertToTimeFormat(initialValue->toString());
494 init_time = mktime(&retValue);
498 retValue = convertToTimeFormat(endValue->toString());
499 end_time = mktime(&retValue);
501 LogDebug("<<< mktime(initialValue):[" << init_time << "]");
502 LogDebug("<<< mktime(m_dateTime):[" << mid_time << "]");
503 LogDebug("<<< mktime(endValue):[" << end_time << "]");
504 LogDebug("<<< mid_time - init_time:[" << mid_time - init_time << "]");
505 LogDebug("<<< end_time - mid_time:[" << end_time - mid_time << "]");
507 if(((mid_time - init_time) >= 0 ) && ((end_time - mid_time) >= 0 ))
509 LogDebug("<<< timestamp is in range");
510 m_result.push_back(TRUE);
514 LogDebug("<<< timestamp is out of range");
515 m_result.push_back(FALSE);
520 bool StorageChangesConversationGenerator::getMatchFlagResult(Api::Tizen::AnyPtr& value, std::string& valueString, Api::Tizen::MatchFlag& matchFlag)
522 std::vector<std::string>::iterator iter;
524 if(matchFlag == Api::Tizen::MATCH_EXACTLY){
525 LogDebug("STRING_MATCH_EXACTLY");
526 result = result || getMatchExactlyClause(valueString, value);
527 }else if(matchFlag == Api::Tizen::MATCH_CONTAINS){
528 LogDebug("STRING_MATCH_CONTAINS");
529 result = result || getMatchContainsClause(valueString, value);
530 }else if(matchFlag == Api::Tizen::MATCH_STARTSWITH){
531 LogDebug("STRING_MATCH_STARTSWITH");
532 result = result || getMatchStartwithClause(valueString, value);
533 }else if(matchFlag == Api::Tizen::MATCH_ENDSWITH){
534 LogDebug("STRING_MATCH_ENDSWITH");
535 result = result || getMatchEndwithClause(valueString, value);
537 LogDebug("[ERROR]invalid match flag : iter:" << *iter);
542 bool StorageChangesConversationGenerator::getCompareResult()
545 LogDebug("m_result.size() " << m_result.size());
546 result = m_result.back();
548 LogDebug("result = " << result);
552 } //namespace Platform
553 } //namespace WrtPlugins