creator/engine/src/creator_messageelement.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "creator_messageelement.h"
       
    21 #include "creator_traces.h"
       
    22 #include "creator_message.h"
       
    23 
       
    24 using namespace creatormsg;
       
    25 
       
    26 /*
       
    27  * 
       
    28  */
       
    29 CCreatorMessageElement* CCreatorMessageElement::NewL(CCreatorEngine* aEngine, const TDesC& aName, const TDesC& aContext )
       
    30     {
       
    31     CCreatorMessageElement* self = new (ELeave) CCreatorMessageElement(aEngine);
       
    32     CleanupStack::PushL(self);
       
    33     self->ConstructL(aName, aContext);
       
    34     CleanupStack::Pop(self);
       
    35     return self;
       
    36     }
       
    37 /*
       
    38  * 
       
    39  */
       
    40 CCreatorMessageElement::CCreatorMessageElement(CCreatorEngine* aEngine) 
       
    41 : 
       
    42 CCreatorScriptElement(aEngine)
       
    43     {
       
    44     iIsCommandElement = ETrue;
       
    45     }
       
    46 /*
       
    47  * Sets message type
       
    48  */
       
    49 void CCreatorMessageElement::SetMessageTypeL(CMessagesParameters& aParameters, const TDesC& aMsgTypeStr ) const
       
    50     {
       
    51     if( CompareIgnoreCase(aMsgTypeStr, KSms) == 0 )
       
    52         {
       
    53         aParameters.iMessageType = ESMS;
       
    54         }
       
    55     else if( CompareIgnoreCase(aMsgTypeStr, KMms) == 0 )
       
    56         {
       
    57         aParameters.iMessageType = EMMS;
       
    58         }
       
    59     else if( CompareIgnoreCase(aMsgTypeStr, KAms) == 0 )
       
    60         {
       
    61         aParameters.iMessageType = EAMS;
       
    62         }
       
    63     else if( CompareIgnoreCase(aMsgTypeStr, KEmail) == 0 )
       
    64         {
       
    65         aParameters.iMessageType = EEmail;
       
    66         }
       
    67     else if( CompareIgnoreCase(aMsgTypeStr, KSmart) == 0 )
       
    68         {
       
    69         aParameters.iMessageType = ESmartMessage;
       
    70         }
       
    71     else if( CompareIgnoreCase(aMsgTypeStr, KIr) == 0 )
       
    72         {
       
    73         aParameters.iMessageType = EIrMessage;
       
    74         }
       
    75     else if( CompareIgnoreCase(aMsgTypeStr, KBt) == 0 )
       
    76         {
       
    77         aParameters.iMessageType = EBTMessage;
       
    78         }
       
    79     else
       
    80         {
       
    81         LOGSTRING2("ERROR in CCreatorMessageElement::SetMessageTypeL: Unknown message type: %S", &aMsgTypeStr);
       
    82         User::Leave(KErrGeneral);
       
    83         }
       
    84     }
       
    85 /*
       
    86  * Returns maximum length of the body text, when randomlength=max is used.
       
    87  */
       
    88 TInt CCreatorMessageElement::GetMaxBodyLength( const TDesC& aMsgType ) const
       
    89     {
       
    90     TInt len = 0;
       
    91     if( CompareIgnoreCase(aMsgType, KSms) == 0 )
       
    92         {
       
    93         len = 160;
       
    94         }
       
    95     else if( CompareIgnoreCase(aMsgType, KMms) == 0 )
       
    96         {
       
    97         len = 160;
       
    98         }    
       
    99     else if( CompareIgnoreCase(aMsgType, KEmail) == 0 )
       
   100         {
       
   101         len = 1024;
       
   102         }
       
   103     else
       
   104         {
       
   105         len = KUndef;
       
   106         }
       
   107     return len;
       
   108     }
       
   109 /*
       
   110  * Returns random body text length
       
   111  */
       
   112 TInt CCreatorMessageElement::GetRandomBodyLengthL(const TDesC& aRandomLenStr, const TDesC& aMsgType ) const
       
   113     {
       
   114     TInt len = 0;
       
   115     if( aRandomLenStr == KMax )
       
   116         {
       
   117         len = GetMaxBodyLength(aMsgType);
       
   118         }
       
   119     else if( aRandomLenStr == KDefault )
       
   120         {
       
   121         len = KUndef;        
       
   122         }
       
   123     else
       
   124         {
       
   125         len = ConvertStrToIntL(aRandomLenStr);
       
   126         }
       
   127     return len;
       
   128     }
       
   129 
       
   130 /*
       
   131  * Creates random message address (To/From)
       
   132  * Returns phone number for SMS, MMS, AMS and Smart messages.
       
   133  * Email address for others
       
   134  */
       
   135 HBufC* CCreatorMessageElement::CreateMessageAddressLC(const TDesC& msgType)
       
   136     {
       
   137     HBufC* toAddr;
       
   138     if( CompareIgnoreCase(msgType, KSms) == 0 || 
       
   139         CompareIgnoreCase(msgType, KMms) == 0 || 
       
   140         CompareIgnoreCase(msgType, KAms) == 0 || 
       
   141         CompareIgnoreCase(msgType, KSmart) == 0)
       
   142         {
       
   143         TPtrC temp = iEngine->RandomString(CCreatorEngine::EPhoneNumber);
       
   144         toAddr = HBufC::NewL(temp.Length());
       
   145         CleanupStack::PushL(toAddr);
       
   146         toAddr->Des().Copy(temp);
       
   147         }
       
   148     else
       
   149         {
       
   150         toAddr = iEngine->CreateEmailAddressLC();
       
   151         }
       
   152     return toAddr;
       
   153     }
       
   154 
       
   155 /*
       
   156  *
       
   157  */
       
   158 void CCreatorMessageElement::ExecuteCommandL()
       
   159     {
       
   160     LOGSTRING("Creator: CCreatorMessageElement::ExecuteCommandL");
       
   161     
       
   162     // Find out the message type:
       
   163     const CCreatorScriptAttribute* msgTypeAttr = this->FindAttributeByName(KType);
       
   164     TPtrC msgType;
       
   165     if( msgTypeAttr )
       
   166         {
       
   167         msgType.Set(msgTypeAttr->Value());
       
   168         }
       
   169     else
       
   170         {
       
   171         LOGSTRING("ERROR in CCreatorMessageElement::ExecuteCommandL: Type attribute is missing.");
       
   172         User::Leave(KErrGeneral); // type is required attribute
       
   173         }
       
   174     
       
   175     // Find out the amount of calendar entries:
       
   176     const CCreatorScriptAttribute* msgAmountAttr = this->FindAttributeByName(KAmount);
       
   177     TInt msgAmount = 1;    
       
   178     if( msgAmountAttr )
       
   179         {
       
   180         msgAmount = ConvertStrToIntL(msgAmountAttr->Value());
       
   181         }    
       
   182     
       
   183     // Get 'fields' element 
       
   184     CCreatorScriptElement* fieldsElement = FindSubElement(KFields);
       
   185     if( fieldsElement && fieldsElement->SubElements().Count() > 0 )
       
   186         {
       
   187         // Get sub-elements (i.e the message field elements)
       
   188         const RPointerArray<CCreatorScriptElement>& fields = fieldsElement->SubElements();
       
   189         
       
   190         // Create message entries, the amount of entries is defined by msgAmount:
       
   191         for( TInt cI = 0; cI < msgAmount; ++cI )
       
   192             {
       
   193             CMessagesParameters* param = new (ELeave) CMessagesParameters;
       
   194             CleanupStack::PushL(param);
       
   195             
       
   196             // Message type:
       
   197             if( msgTypeAttr )
       
   198                 {
       
   199                 SetMessageTypeL(*param, msgTypeAttr->Value());
       
   200                 }           
       
   201             
       
   202             // Loop all the message field elements:
       
   203             for( TInt i = 0; i < fields.Count(); ++i )
       
   204                 {
       
   205                 CCreatorScriptElement* field = fields[i];
       
   206                 TPtrC elemName = field->Name();
       
   207                 TPtrC elemContent = field->Content();
       
   208                 const RPointerArray<CCreatorScriptElement>& contactReferences = field->SubElements();
       
   209                 const CCreatorScriptAttribute* randomAttr = field->FindAttributeByName(KRandomLength);
       
   210                 const CCreatorScriptAttribute* amountAttr = field->FindAttributeByName(KAmount);
       
   211                 const CCreatorScriptAttribute* increaseAttr = field->FindAttributeByName(KIncrease);
       
   212                 TBool increase( EFalse );
       
   213                 if ( increaseAttr )
       
   214                     {
       
   215                     increase = ConvertStrToBooleanL( increaseAttr->Value() );
       
   216                     }
       
   217                 TInt fieldAmount = 1;
       
   218                 if( amountAttr )
       
   219                     {
       
   220                     fieldAmount = ConvertStrToIntL(amountAttr->Value());
       
   221                     }               
       
   222                 
       
   223                 if( elemName == KTo )
       
   224                     {
       
   225                     // Recipient ('to'-field)
       
   226                     for( TInt amountIndex = 0; amountIndex < fieldAmount; ++amountIndex )
       
   227                         {
       
   228                         if( (randomAttr || elemContent.Length() == 0 ) && contactReferences.Count() == 0 )
       
   229                             {                        
       
   230                             // Random content
       
   231                             HBufC* toAddr = CreateMessageAddressLC(msgType);
       
   232                             if( toAddr )
       
   233                                 {
       
   234                                 param->iRecipientAddressArray.AppendL(toAddr);
       
   235                                 CleanupStack::Pop(); // toAddr
       
   236                                 }
       
   237                             }
       
   238                         else
       
   239                             {                            
       
   240                             if( elemContent.Length() > 0 && contactReferences.Count() == 0)
       
   241                                 {
       
   242                                 // Explicit recipient given
       
   243                                 HBufC* recipient = HBufC::NewL( elemContent.Length() + 3 );
       
   244                                 CleanupStack::PushL(recipient);
       
   245                                 if ( increase )
       
   246                                     {
       
   247                                     IncreasePhoneNumL( elemContent, cI, recipient );
       
   248                                     }
       
   249                                 else
       
   250                                     {
       
   251                                     recipient->Des().Copy(elemContent);
       
   252                                     }
       
   253                                 param->iRecipientAddressArray.AppendL(recipient);
       
   254                                 CleanupStack::Pop(); // recipient
       
   255                                 }
       
   256                             else
       
   257                                 {
       
   258                                 // Recipients specified with contact-set-references (if any)
       
   259                                 for( TInt csI = 0; csI < contactReferences.Count(); ++csI )
       
   260                                     {                            
       
   261                                     CCreatorScriptElement* contactSetRef = contactReferences[csI];
       
   262                                     AppendContactSetReferenceL(*contactSetRef, param->iRecipientLinkIds);                                
       
   263                                     }
       
   264                                 }
       
   265                             }
       
   266                         }
       
   267                     }
       
   268                 else if( elemName == KFrom )
       
   269                     {
       
   270                     // Sender ('from'-field)
       
   271                     // Amount attribute for sender is ignored, because there can be only one sender                 
       
   272                     delete param->iSenderAddress;
       
   273                     param->iSenderAddress = 0;
       
   274                     if( (randomAttr || elemContent.Length() == 0 ) && contactReferences.Count() == 0 )
       
   275                         {
       
   276                         // Get random address
       
   277                         param->iSenderAddress = CreateMessageAddressLC(msgType);
       
   278                         CleanupStack::Pop(); // param->iSenderAddress
       
   279                         }
       
   280                     else
       
   281                         {                        
       
   282                         if( elemContent.Length() > 0 && contactReferences.Count() == 0)
       
   283                             {
       
   284                             // Explicit sender address given
       
   285                             param->iSenderAddress = HBufC::NewL(elemContent.Length());
       
   286                             if ( increase )
       
   287                                 {
       
   288                                 IncreasePhoneNumL( elemContent, cI, param->iSenderAddress );
       
   289                                 }
       
   290                             else
       
   291                                 {
       
   292                                 param->iSenderAddress->Des().Copy(elemContent);
       
   293                                 }
       
   294                             }
       
   295                         else
       
   296                             {
       
   297                             // Senders specified with contact-set-references (if any)
       
   298                             for( TInt csI = 0; csI < contactReferences.Count(); ++csI )
       
   299                                 {                            
       
   300                                 CCreatorScriptElement* contactSetRef = contactReferences[csI];
       
   301                                 AppendContactSetReferenceL(*contactSetRef, param->iSenderLinkIds);                                
       
   302                                 }
       
   303                             }                           
       
   304                         }
       
   305                     }
       
   306                 else if( elemName == KFolder )              
       
   307                     {
       
   308                     // Folder type
       
   309                     if( CompareIgnoreCase(elemContent, KSent) == 0 )
       
   310                         {
       
   311                         param->iFolderType = ESent;
       
   312                         }
       
   313                     else if( CompareIgnoreCase(elemContent, KInbox) == 0 )
       
   314                         {
       
   315                         param->iFolderType = EInbox;
       
   316                         }
       
   317                     else if( CompareIgnoreCase(elemContent, KOutbox) == 0 )
       
   318                         {
       
   319                         param->iFolderType = EOutbox;
       
   320                         }
       
   321                     else if( CompareIgnoreCase(elemContent, KDraft) == 0 )
       
   322                         {
       
   323                         param->iFolderType = EDrafts;
       
   324                         }                        
       
   325                     }
       
   326                 else if( elemName == KSubject )
       
   327                     {
       
   328                     // Message subject
       
   329                     delete param->iMessageSubject;
       
   330                     param->iMessageSubject = 0;
       
   331                     
       
   332                     if( randomAttr || elemContent.Length() == 0 )
       
   333                         {
       
   334                         // Random data should be used
       
   335                         TPtrC temp = iEngine->RandomString(CCreatorEngine::EMessageSubject);
       
   336                         param->iMessageSubject = HBufC::NewL(temp.Length());
       
   337                         param->iMessageSubject->Des().Copy(temp);
       
   338                         }
       
   339                     else
       
   340                         {                        
       
   341                         param->iMessageSubject = HBufC::NewL(elemContent.Length());
       
   342                         param->iMessageSubject->Des().Copy(elemContent);
       
   343                         }
       
   344                     }
       
   345                 else if( elemName == KText )
       
   346                     {
       
   347                     // Body text
       
   348                     delete param->iMessageBodyText;
       
   349                     param->iMessageBodyText = 0;
       
   350                     
       
   351                     if( randomAttr || elemContent.Length() == 0 )
       
   352                         {
       
   353                         // Put random text:
       
   354                         if( randomAttr && randomAttr->Value() != KDefault )
       
   355                             {
       
   356                             // Get the random length
       
   357                             TInt len = GetRandomBodyLengthL(randomAttr->Value(), msgTypeAttr->Value());
       
   358                             if( len != KUndef )
       
   359                                 {
       
   360                                 param->iMessageBodyText = iEngine->CreateRandomStringLC(len);
       
   361                                 CleanupStack::Pop(); // param->iMessageBodyText
       
   362                                 }
       
   363                             }
       
   364                         else
       
   365                             {
       
   366                             // Use default random data
       
   367                             TPtrC temp = iEngine->RandomString(CCreatorEngine::EMessageText);
       
   368                             param->iMessageBodyText = HBufC::NewL(temp.Length());
       
   369                             param->iMessageBodyText->Des().Copy(temp);
       
   370                             }
       
   371                         }
       
   372                     else
       
   373                         {
       
   374                         param->iMessageBodyText = HBufC::NewL(elemContent.Length());
       
   375                         param->iMessageBodyText->Des().Copy(elemContent);
       
   376                         }
       
   377                     }
       
   378                 else if( elemName == KAttachmentId )
       
   379                     {
       
   380                     // Attachment file id
       
   381                     for( TInt amountIndex = 0; amountIndex < fieldAmount; ++amountIndex )
       
   382                         {                        
       
   383                         if( randomAttr  || elemContent.Length() == 0)
       
   384                             {
       
   385                             //When type is AMS, attachement will be audio
       
   386                             if(param->iMessageType == EAMS)
       
   387                                 {
       
   388                                 param->iAttachments->AppendL(CCreatorEngine::EMP3_250kB);
       
   389                                 }
       
   390                             //Otherwise attachement can be any file
       
   391                             else
       
   392                                 {
       
   393                                 //EJPEG_25kB is first (0) in the enum and LAST_FILE_ID is last in the enum, so real last item id is one before LAST_FILE_ID
       
   394                                 param->iAttachments->AppendL( iEngine->RandomNumber(CCreatorEngine::EJPEG_25kB, CCreatorEngine::LAST_FILE_ID-1) );                                
       
   395                                 }
       
   396                             }
       
   397                         else
       
   398                             {
       
   399                             //When user has been set attechment by it self, we trust user selection (not validating value, e.g. if message is SMS and there is attachement)
       
   400                             TInt id = iEngine->GetAttachmentIdL(elemContent);
       
   401                             if( id != KUndef )
       
   402                                 {
       
   403                                 param->iAttachments->AppendL( id );
       
   404                                 }
       
   405                             }
       
   406                         }
       
   407                     }
       
   408                 // Attachment file path handling 
       
   409                 //E.g. C:\data\others\DOC-20kB.doc
       
   410                 else if( elemName == KAttachmentPath )
       
   411                     {
       
   412                     // Attachment file id
       
   413                     for( TInt amountIndex = 0; amountIndex < fieldAmount; ++amountIndex )
       
   414                         {
       
   415                         //Path is random, getting one of the files (not even using path attribute, but id with random)
       
   416                         if( randomAttr  || elemContent.Length() == 0)
       
   417                             {
       
   418                             //EJPEG_25kB is first (0) in the enum and LAST_FILE_ID is last in the enum, so real last item id is one before LAST_FILE_ID
       
   419                             param->iAttachments->AppendL( iEngine->RandomNumber(CCreatorEngine::EJPEG_25kB, CCreatorEngine::LAST_FILE_ID -1) );
       
   420                             }
       
   421                         //Otherwise adding attachement path as it is to paths.
       
   422                         else
       
   423                             {
       
   424                             //Adding Attachement file path
       
   425                             HBufC* elemData = elemContent.AllocLC();
       
   426                             param->iAttachmentPaths.AppendL( elemData );     
       
   427                             CleanupStack::Pop(elemData);
       
   428                             }
       
   429                         }
       
   430                     }
       
   431                 else if ( elemName == KStatus )
       
   432                     {
       
   433                     if( CompareIgnoreCase( elemContent, KNew ) == 0 )
       
   434                         {
       
   435                         param->iCreateAsUnread = ETrue;
       
   436                         }
       
   437                     else if( CompareIgnoreCase( elemContent, KRead ) == 0 )
       
   438                         {
       
   439                         param->iCreateAsUnread = EFalse;
       
   440                         }
       
   441                     }
       
   442                 }
       
   443             iEngine->AppendToCommandArrayL(ECmdCreateMessagingEntryMessagesViaScript, param);
       
   444             CleanupStack::Pop(); // param
       
   445             }
       
   446         }
       
   447     else
       
   448     	{
       
   449     	for( TInt i = 0; i < msgAmount; ++i )
       
   450     		{
       
   451     		TInt randMsg = 0;
       
   452     		if( msgType == KSms )
       
   453     			randMsg = iEngine->RandomNumber(ECmdCreateRandomEntrySMSInbox, ECmdCreateRandomEntrySMSSent);
       
   454     		else if( msgType == KMms )
       
   455     			randMsg = iEngine->RandomNumber(ECmdCreateRandomEntryMMSInbox, ECmdCreateRandomEntryMMSSent);
       
   456     		else if( msgType == KAms )
       
   457     			randMsg = iEngine->RandomNumber(ECmdCreateRandomEntryAMSInbox, ECmdCreateRandomEntryAMSSent);
       
   458     		else if( msgType == KEmail )
       
   459     			randMsg = iEngine->RandomNumber(ECmdCreateRandomEntryEmailInbox, ECmdCreateRandomEntryEmailSent);
       
   460     		else if( msgType == KSmart )
       
   461     			randMsg = iEngine->RandomNumber(ECmdCreateRandomEntryBIOInbox, ECmdCreateRandomEntryBIOSent);
       
   462     		else if( msgType == KBt )
       
   463     			randMsg = iEngine->RandomNumber(ECmdCreateRandomEntryBTInbox, ECmdCreateRandomEntryBTSent);
       
   464     		else if( msgType == KIr )
       
   465     			randMsg = iEngine->RandomNumber(ECmdCreateRandomEntryIRInbox, ECmdCreateRandomEntryIRSent);
       
   466     			
       
   467     		if( randMsg > 0 )
       
   468     			{
       
   469     			iEngine->AppendToCommandArrayL(randMsg, 0, 1);
       
   470     			}
       
   471     		}
       
   472     	}
       
   473     }
       
   474 
       
   475 // End of file