merge wrt-plugins-tizen_0.2.0-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, AnyPtr& matchValue, int depth){
253                         LogDebug("<<< attrName:[" << attrName << "], matchFlag:[" << matchFlag << "]");
254                         if(matchValue == NULL)
255                                 return;
256
257                         LogDebug("matchValue:" << matchValue->toString());
258
259                         visitAttributeEach(attrName, matchFlag, matchValue, depth);
260                         LogDebug(">>>");
261                 }
262
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;
266
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)
273                                 {
274                                         m_result.push_back(TRUE);
275                                         LogDebug("<<< type is same");
276                                 }
277                                 else
278                                 {
279                                         m_result.push_back(FALSE);
280                                         LogDebug("<<< type is different");
281                                 }
282                                 return;
283                         }
284                         if(attrName.compare("messageCount")==0){
285                                 LogDebug("<<< value:[" << value << "]");
286                                 int nType = atoi(value->toString().c_str());
287                                 if((int)m_messageCount == nType)
288                                 {
289                                         m_result.push_back(TRUE);
290                                         LogDebug("<<< messageCount is same");
291                                 }
292                                 else
293                                 {
294                                         m_result.push_back(FALSE);
295                                         LogDebug("<<< messageCount is different");
296                                 }
297                                 return;
298                         }
299                         if(attrName.compare("unreadMessages")==0){
300                                 LogDebug("<<< value:[" << value << "]");
301                                 int nType = atoi(value->toString().c_str());
302                                 if((int)m_unreadMessages == nType)
303                                 {
304                                         m_result.push_back(TRUE);
305                                         LogDebug("<<< unreadMessages is same");
306                                 }
307                                 else
308                                 {
309                                         m_result.push_back(FALSE);
310                                         LogDebug("<<< unreadMessages is different");
311                                 }
312                                 return;
313                         }
314                         if(attrName.compare("isRead")==0){
315                                 LogDebug("<<< value:[" << value << "]");
316                                 int nType = value->toString().compare("true");
317                                 if((int)m_read == nType)
318                                 {
319                                         m_result.push_back(TRUE);
320                                         LogDebug("<<< read is same");
321                                 }
322                                 else
323                                 {
324                                         m_result.push_back(FALSE);
325                                         LogDebug("<<< read is different");
326                                 }
327                                 return;
328                         }
329                         if(attrName.compare("lastMessageId")==0){
330                                 LogDebug("<<< value:[" << value << "]");
331                                 int nType = atoi(value->toString().c_str());
332                                 if((int)m_lastMessageId == nType)
333                                 {
334                                         m_result.push_back(TRUE);
335                                         LogDebug("<<< lastMessageId is same");
336                                 }
337                                 else
338                                 {
339                                         m_result.push_back(FALSE);
340                                         LogDebug("<<< lastMessageId is different");
341                                 }
342                                 return;
343                         }
344   
345                         if(attrName.compare("preview")==0){
346                                 LogDebug("<<< value:[" << value << "]");
347                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
348                                 LogDebug("<<< m_body:[" << m_preview << "]");
349   
350                                 valueString = m_preview;
351                                 if(getMatchFlagResult(value, valueString, matchFlag))
352                                 {
353                                         m_result.push_back(TRUE);
354                                         LogDebug("<<< preview is same");
355                         }
356                                 else
357                                 {
358                                         m_result.push_back(FALSE);
359                                         LogDebug("<<< preview is different");
360                                 }
361                                 return;
362                         }
363                         if(attrName.compare("from")==0){
364                                 LogDebug("<<< value:[" << value << "]");
365                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
366                                 LogDebug("<<< m_from:[" << m_from << "]");
367    
368                                 valueString = m_from;
369                                 if(getMatchFlagResult(value, valueString, matchFlag))
370                                 {
371                                         m_result.push_back(TRUE);
372                                         LogDebug("<<< from is same");
373                                 }
374                                 else
375                         {
376                                         m_result.push_back(FALSE);
377                                         LogDebug("<<< from is different");
378                                 }
379                                 return;
380                         }
381                         if(attrName.compare("subject")==0){
382                         LogDebug("<<< value:[" << value << "]");
383                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
384                                 LogDebug("<<< m_subject:[" << m_subject << "]");
385   
386                                 valueString = m_subject;
387                                 if(getMatchFlagResult(value, valueString, matchFlag))
388                                 {
389                                         m_result.push_back(TRUE);
390                                         LogDebug("<<< subject is same");
391                                 }
392                                 else
393                                 {
394                                         m_result.push_back(FALSE);
395                                         LogDebug("<<< subject is different");
396                                 }
397                                 return;
398                         }
399   
400                         if(attrName.compare("to")==0){
401                                 LogDebug("<<< value:[" << value << "]");
402                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
403                                 LogDebug("<<< m_to.size :[" << m_to.size() << "]");
404   
405                                 bool result = FALSE;
406                                 for(unsigned int i=0; i < m_to.size(); i++)
407                                 {
408                                         valueString = m_to[i];
409                                         result = result || getMatchFlagResult(value, valueString, matchFlag);
410                                 }
411                                 LogDebug("<<< to compare result is :" << result);
412                                 m_result.push_back(result);
413                                 return;
414                         }
415   
416                         if(attrName.compare("cc")==0){
417                                 LogDebug("<<< value:[" << value << "]");
418                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
419                                 LogDebug("<<< m_cc.size :[" << m_cc.size() << "]");
420   
421                                 bool result = FALSE;
422                                 for(unsigned int i=0; i < m_cc.size(); i++)
423                                 {
424                                         valueString = m_cc[i];
425                                         result = result || getMatchFlagResult(value, valueString, matchFlag);
426                                 }
427                                 LogDebug("<<< cc compare result is : " << result);
428                                 m_result.push_back(result);
429                                 return;
430                         }
431   
432                         if(attrName.compare("bcc")==0){
433                                 LogDebug("<<< value:[" << value << "]");
434                                 LogDebug("<<< value->toString():[" << value->toString() << "]");
435                                 LogDebug("<<< m_cc.size :[" << m_bcc.size() << "]");
436   
437                                 bool result = FALSE;
438                                 for(unsigned int i=0; i < m_bcc.size(); i++)
439                                 {
440                                         valueString = m_bcc[i];
441                                         result = result || getMatchFlagResult(value, valueString, matchFlag);
442                                 }
443                                 LogDebug("<<< bcc compare result is : " << result);
444                                 m_result.push_back(result);
445                                 return;
446                         }
447   
448                         LogDebug(">>>");
449                         }
450
451                 int StorageChangesConversationGenerator::convertMessageType(std::string& stringType){
452                         LogDebug("<<< stringType:[" << stringType << "]");
453
454                         int retMessageType = -1;
455
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;
462                         }else{
463                                 LogError("invalid type:[" << stringType << "]");
464                                 return -1;
465                         }
466
467                         LogDebug(">>> retMessageType:" << retMessageType);
468                         return retMessageType;
469                 }
470
471                 std::string StorageChangesConversationGenerator::convertBooleanStringToIntegerString(std::string& booleanString){
472                         std::string retString;
473
474                         if(booleanString.compare("true") == 0){
475                                 retString = "1";
476                         }else if(booleanString.compare("false") == 0){
477                                 retString = "0";
478                         }else{
479                                 LogError("invalid booleanString:[" << booleanString << "]");
480                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "invalid booleanString :[" + booleanString + "]");
481                         }
482                         return retString;
483                 }
484
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;
487                         struct tm retValue;
488
489                         if(initialValue == NULL || endValue == NULL)
490                                 return;
491
492                         if(attrName.compare("timestamp")==0){
493                                 retValue = convertToTimeFormat(initialValue->toString());
494                                 init_time = mktime(&retValue);
495
496                                 mid_time = m_time;
497
498                                 retValue = convertToTimeFormat(endValue->toString());                           
499                                 end_time = mktime(&retValue);
500
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 << "]");
506
507                                 if(((mid_time - init_time) >= 0 ) && ((end_time - mid_time) >= 0 ))
508                                 {
509                                         LogDebug("<<< timestamp is in range");
510                                         m_result.push_back(TRUE);
511                                 }
512                                 else
513                                 {
514                                         LogDebug("<<< timestamp is out of range");
515                                         m_result.push_back(FALSE);
516                                 }
517                         }
518                         }
519                         
520                 bool StorageChangesConversationGenerator::getMatchFlagResult(Api::Tizen::AnyPtr& value, std::string& valueString, Api::Tizen::MatchFlag& matchFlag)
521                 {
522                         std::vector<std::string>::iterator iter;
523                         bool result = FALSE;
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);                           
536                         }else{
537                                 LogDebug("[ERROR]invalid match flag : iter:" << *iter);
538                         }
539                         return result;
540                 }
541
542                 bool StorageChangesConversationGenerator::getCompareResult()
543                 {       
544                         bool result;
545                         LogDebug("m_result.size() " << m_result.size());
546                         result = m_result.back();
547                         m_result.pop_back();                    
548                         LogDebug("result = " << result);        
549                         return result;
550                 }
551 }
552         }               //namespace Platform
553 }               //namespace WrtPlugins