Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / API / Messaging / StorageChangesConversationGenerator.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  * StorageChangesConversationGenerator.cpp
18  *
19  *  Created on: 2011. 10. 28.
20  *      Author: sangtai
21  */
22
23 #include "StorageChangesConversationGenerator.h"
24 #include "StorageChangesConversationFilterValidatorFactory.h"
25 #include "API/Messaging/ConversationFilterValidatorFactory.h"
26
27 #include "IMessagingTypes.h"
28
29 #include <emf-types.h>
30
31 #include <dpl/log/log.h>
32
33 using namespace std;
34 using namespace TizenApis::Api::Tizen;
35
36 namespace TizenApis {
37         namespace Platform {
38                 namespace Messaging {
39
40
41                 const int StorageChangesConversationGenerator::MESSAGE_TYPE_NOT_INITIALIZED = -1;
42
43                 StorageChangesConversationGenerator::StorageChangesConversationGenerator(Api::Messaging::IConversationPtr conv):m_messageType(MESSAGE_TYPE_NOT_INITIALIZED) {
44
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();
53                         m_to = conv->getTo();
54                         m_cc = conv->getCC();
55                         m_bcc = conv->getBCC();
56                         m_lastMessageId = conv->getLastMessageId();
57
58                         struct tm * timeinfo;
59                         timeinfo = localtime( &m_time );
60                         
61                 }
62
63                 StorageChangesConversationGenerator::~StorageChangesConversationGenerator() {
64                 }
65
66
67                 void StorageChangesConversationGenerator::visitPreComposite(Api::Tizen::FilterType& type, int depth){
68                         LogDebug("<<<");
69                         m_operand.push_back(leftblank);
70                         LogDebug("Left_blank");                 
71                         LogDebug(">>>");
72                 }
73
74                 void StorageChangesConversationGenerator::visitInComposite(Api::Tizen::FilterType& type, int depth){
75                         LogDebug("<<<");
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);                               
82                         }else{
83                                 LogError("[ERROR] invalid Filter type:" << type);
84                         }
85
86                         LogDebug(">>>");
87                 }
88
89                 void StorageChangesConversationGenerator::visitPostComposite(Api::Tizen::FilterType& type, int depth){
90                         LogDebug("<<<");
91                         bool data1, data2;
92                         do
93                         {
94                                 LogDebug("m_result size =" << m_result.size());
95                                 data1 = m_result.back();
96                                 m_result.pop_back();
97                                 data2 = m_result.back();
98                                 m_result.pop_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)
103                                 {
104                                         LogDebug("Union");                              
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());
112                                 }
113                                 else if(m_operand.back() == operandIntersection)
114                                 {
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());                                 
123                                         
124                                 }
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());                                                                                       
129                         LogDebug(">>>");
130                 }
131
132                 struct tm StorageChangesConversationGenerator::convertToTimeFormat(const std::string& timeString){
133                         LogDebug("<<< timeString:[" << timeString << "]");
134
135                         struct tm tm_Time;
136
137                         int nextStart = 0;
138
139                         int yearLength = 4;
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 << "]");
143
144                         int monthLength = 2;
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 << "]");
148
149                         int dateLength = 2;
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 << "]");
153
154                         int hourLength = 2;
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 << "]");
158
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 << "]");
163
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 << "]");
167
168                         return tm_Time;
169                         }
170
171                 bool StorageChangesConversationGenerator::getMatchExactlyClause(std::string& attrName, Api::Tizen::AnyPtr& value)
172                 {
173                         std::string valueString = value->toString();
174                         LogDebug("<<< attrName : " << attrName);
175                         LogDebug("<<< valueString : " << valueString);
176                         LogDebug("<<< attrName.compare(valueString) : " << attrName.compare(valueString));
177
178                         if(attrName.compare(valueString) == 0)
179                         {
180                                 LogDebug("<<< getMatchExactlyClause SAME >>>");
181                                 return TRUE;                            
182                         }
183                         else{
184                                 LogDebug("<<< getMatchExactlyClause DIFF >>>");
185                                 return FALSE;                                                           
186                         }
187                 }
188
189                 bool StorageChangesConversationGenerator::getMatchContainsClause(std::string& attrName, Api::Tizen::AnyPtr& value)
190                 {
191                         std::string valueString = value->toString();
192                         LogDebug("<<< attrName : " << attrName);
193                         LogDebug("<<< valueString : " << valueString);
194                         LogDebug("<<< attrName.find(valueString) : " << attrName.find(valueString));
195
196                         if(attrName.find(valueString) != std::string::npos)
197                         {
198                                 LogDebug("<<< getMatchContainsClause CONTAINS >>>");
199                                 return TRUE;                            
200                         }else{
201                                 LogDebug("<<< getMatchContainsClause NOT CONTAINS >>>");
202                                 return FALSE;                                                           
203                         }
204                         }
205
206                 bool StorageChangesConversationGenerator::getMatchStartwithClause(std::string& attrName, Api::Tizen::AnyPtr& value)
207                 {
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));
212
213                         
214                         if(attrName.size()-valueString.size() < 0)
215                                 return FALSE;
216
217                         if(attrName.compare(0,valueString.size(), valueString) == 0)
218                         {
219                                 LogDebug("<<< getMatchStartwithClause START WITH >>>");
220                         return TRUE;
221                 }
222                         else{
223                                 LogDebug("<<< getMatchStartwithClause NOT START WITH >>>");
224                                 return FALSE;                                                           
225                         }
226                 }
227
228                 bool StorageChangesConversationGenerator::getMatchEndwithClause(std::string& attrName, Api::Tizen::AnyPtr& value)
229                 {
230                         std::string valueString = value->toString();
231
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));
235
236
237                         if(attrName.size()-valueString.size() < 0)
238                                 return FALSE;
239
240                         if(attrName.compare(attrName.size()-valueString.size(),valueString.size(), valueString) == 0 )
241                         {
242                                 LogDebug("<<< getMatchEndwithClause END WITH >>>");
243                                 return TRUE;                            
244                         }
245                         else{
246                                 LogDebug("<<< getMatchEndwithClause NOT END WITH >>>");
247                                 return FALSE;                                                           
248                         }
249                         }
250
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());
255
256                         Api::Tizen::FilterType filterType = UNION_FILTER;
257
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);
263                         }else{
264                                 visitPreComposite(filterType, depth);
265                         
266                                 AnyArray::iterator iter;
267                                 for(iter=values->begin(); iter!= values->end(); iter++){
268                         
269                                         if(iter != values->begin()){
270                         
271                                                 visitInComposite(filterType, depth);
272                                         }
273                         
274                                         AnyPtr matchValue = *iter;
275                                         visitAttributeEach(attrName, matchFlag, matchValue, depth);
276                                 }
277                         
278                                 visitPostComposite(filterType, depth);
279                         }
280                         LogDebug(">>>");
281                 }
282
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;
286
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)
293                                 {
294                                         m_result.push_back(TRUE);
295                                         LogDebug("<<< type is same");
296                                 }
297                                 else
298                                 {
299                                         m_result.push_back(FALSE);
300                                         LogDebug("<<< type is different");
301                                 }
302                                 return;
303                         }
304                         if(attrName.compare("messageCount")==0){
305                                 LogDebug("<<< value:[" << value << "]");
306                                 int nType = atoi(value->toString().c_str());
307                                 if((int)m_messageCount == nType)
308                                 {
309                                         m_result.push_back(TRUE);
310                                         LogDebug("<<< messageCount is same");
311                                 }
312                                 else
313                                 {
314                                         m_result.push_back(FALSE);
315                                         LogDebug("<<< messageCount is different");
316                                 }
317                                 return;
318                         }
319                         if(attrName.compare("unreadMessages")==0){
320                                 LogDebug("<<< value:[" << value << "]");
321                                 int nType = atoi(value->toString().c_str());
322                                 if((int)m_unreadMessages == nType)
323                                 {
324                                         m_result.push_back(TRUE);
325                                         LogDebug("<<< unreadMessages is same");
326                                 }
327                                 else
328                                 {
329                                         m_result.push_back(FALSE);
330                                         LogDebug("<<< unreadMessages is different");
331                                 }
332                                 return;
333                         }
334                         if(attrName.compare("isRead")==0){
335                                 LogDebug("<<< value:[" << value << "]");
336                                 int nType = value->toString().compare("true");
337                                 if((int)m_read == nType)
338                                 {
339                                         m_result.push_back(TRUE);
340                                         LogDebug("<<< read is same");
341                                 }
342                                 else
343                                 {
344                                         m_result.push_back(FALSE);
345                                         LogDebug("<<< read is different");
346                                 }
347                                 return;
348                         }
349                         if(attrName.compare("lastMessageId")==0){
350                                 LogDebug("<<< value:[" << value << "]");
351                                 int nType = atoi(value->toString().c_str());
352                                 if((int)m_lastMessageId == nType)
353                                 {
354                                         m_result.push_back(TRUE);
355                                         LogDebug("<<< lastMessageId is same");
356                                 }
357                                 else
358                                 {
359                                         m_result.push_back(FALSE);
360                                         LogDebug("<<< lastMessageId is different");
361                                 }
362                                 return;
363                         }
364   
365                         if(attrName.compare("preview")==0){
366                                 LogDebug("<<< value:[" << value << "]");
367                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
368                                 LogDebug("<<< m_body:[" << m_preview << "]");
369   
370                                 valueString = m_preview;
371                                 if(getMatchFlagResult(value, valueString, matchFlag))
372                                 {
373                                         m_result.push_back(TRUE);
374                                         LogDebug("<<< preview is same");
375                         }
376                                 else
377                                 {
378                                         m_result.push_back(FALSE);
379                                         LogDebug("<<< preview is different");
380                                 }
381                                 return;
382                         }
383                         if(attrName.compare("from")==0){
384                                 LogDebug("<<< value:[" << value << "]");
385                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
386                                 LogDebug("<<< m_from:[" << m_from << "]");
387    
388                                 valueString = m_from;
389                                 if(getMatchFlagResult(value, valueString, matchFlag))
390                                 {
391                                         m_result.push_back(TRUE);
392                                         LogDebug("<<< from is same");
393                                 }
394                                 else
395                         {
396                                         m_result.push_back(FALSE);
397                                         LogDebug("<<< from is different");
398                                 }
399                                 return;
400                         }
401                         if(attrName.compare("subject")==0){
402                         LogDebug("<<< value:[" << value << "]");
403                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
404                                 LogDebug("<<< m_subject:[" << m_subject << "]");
405   
406                                 valueString = m_subject;
407                                 if(getMatchFlagResult(value, valueString, matchFlag))
408                                 {
409                                         m_result.push_back(TRUE);
410                                         LogDebug("<<< subject is same");
411                                 }
412                                 else
413                                 {
414                                         m_result.push_back(FALSE);
415                                         LogDebug("<<< subject is different");
416                                 }
417                                 return;
418                         }
419   
420                         if(attrName.compare("to")==0){
421                                 LogDebug("<<< value:[" << value << "]");
422                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
423                                 LogDebug("<<< m_to.size :[" << m_to.size() << "]");
424   
425                                 bool result = FALSE;
426                                 for(unsigned int i=0; i < m_to.size(); i++)
427                                 {
428                                         valueString = m_to[i];
429                                         result = result || getMatchFlagResult(value, valueString, matchFlag);
430                                 }
431                                 LogDebug("<<< to compare result is :" << result);
432                                 m_result.push_back(result);
433                                 return;
434                         }
435   
436                         if(attrName.compare("cc")==0){
437                                 LogDebug("<<< value:[" << value << "]");
438                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
439                                 LogDebug("<<< m_cc.size :[" << m_cc.size() << "]");
440   
441                                 bool result = FALSE;
442                                 for(unsigned int i=0; i < m_cc.size(); i++)
443                                 {
444                                         valueString = m_cc[i];
445                                         result = result || getMatchFlagResult(value, valueString, matchFlag);
446                                 }
447                                 LogDebug("<<< cc compare result is : " << result);
448                                 m_result.push_back(result);
449                                 return;
450                         }
451   
452                         if(attrName.compare("bcc")==0){
453                                 LogDebug("<<< value:[" << value << "]");
454                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
455                                 LogDebug("<<< m_cc.size :[" << m_bcc.size() << "]");
456   
457                                 bool result = FALSE;
458                                 for(unsigned int i=0; i < m_bcc.size(); i++)
459                                 {
460                                         valueString = m_bcc[i];
461                                         result = result || getMatchFlagResult(value, valueString, matchFlag);
462                                 }
463                                 LogDebug("<<< bcc compare result is : " << result);
464                                 m_result.push_back(result);
465                                 return;
466                         }
467   
468                         LogDebug(">>>");
469                         }
470
471                 int StorageChangesConversationGenerator::convertMessageType(std::string& stringType){
472                         LogDebug("<<< stringType:[" << stringType << "]");
473
474                         int retMessageType = -1;
475
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;
482                         }else{
483                                 LogError("invalid type:[" << stringType << "]");
484                                 return -1;
485                         }
486
487                         LogDebug(">>> retMessageType:" << retMessageType);
488                         return retMessageType;
489                 }
490
491                 std::string StorageChangesConversationGenerator::convertBooleanStringToIntegerString(std::string& booleanString){
492                         std::string retString;
493
494                         if(booleanString.compare("true") == 0){
495                                 retString = "1";
496                         }else if(booleanString.compare("false") == 0){
497                                 retString = "0";
498                         }else{
499                                 LogError("invalid booleanString:[" << booleanString << "]");
500                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid booleanString :[" + booleanString + "]");
501                         }
502                         return retString;
503                 }
504
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;
507                         struct tm retValue;
508
509                         if(attrName.compare("timestamp")==0){
510                                 retValue = convertToTimeFormat(initialValue->toString());
511                                 init_time = mktime(&retValue);
512
513                                 mid_time = m_time;
514
515                                 retValue = convertToTimeFormat(endValue->toString());                           
516                                 end_time = mktime(&retValue);
517
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 << "]");
523
524                                 if(((mid_time - init_time) >= 0 ) && ((end_time - mid_time) >= 0 ))
525                                 {
526                                         LogDebug("<<< timestamp is in range");
527                                         m_result.push_back(TRUE);
528                                 }
529                                 else
530                                 {
531                                         LogDebug("<<< timestamp is out of range");
532                                         m_result.push_back(FALSE);
533                                 }
534                         }
535                         }
536                         
537                 bool StorageChangesConversationGenerator::getMatchFlagResult(Api::Tizen::AnyPtr& value, std::string& valueString, Api::Tizen::MatchFlag& matchFlag)
538                 {
539                         std::vector<std::string>::iterator iter;
540                         bool result = FALSE;
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);                           
553                         }else{
554                                 LogDebug("[ERROR]invalid match flag : iter:" << *iter);
555                         }
556                         return result;
557                 }
558
559                 bool StorageChangesConversationGenerator::getCompareResult()
560                 {       
561                         bool result;
562                         LogDebug("m_result.size() " << m_result.size());
563                         result = m_result.back();
564                         m_result.pop_back();                    
565                         LogDebug("result = " << result);        
566                         return result;
567                 }
568 }
569         }               //namespace Platform
570 }               //namespace WrtPlugins