messagingapp/msgappfw/plugin/src/ccsmsgpluginutility.cpp
changeset 37 518b245aa84c
parent 25 84d9eb65b26f
child 38 4e4b6adb1024
equal deleted inserted replaced
25:84d9eb65b26f 37:518b245aa84c
     1 /*
       
     2 * Copyright (c) 2007 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:  CS Msg Plugin Utility class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // USER INCLUDES
       
    20 #include "ccsmsgpluginutility.h"
       
    21 #include "ccsdebug.h"
       
    22 
       
    23 //SYSTEM INCLUDES
       
    24 #include <csmsemailfields.h>
       
    25 #include <e32cmn.h>
       
    26 #include <SendUiConsts.h>
       
    27 #include <pushentry.h>
       
    28 #include <msvids.h>
       
    29 
       
    30 // ----------------------------------------------------------------------------
       
    31 // CCsMsgPluginUtility::NewL
       
    32 // Two Phase Construction
       
    33 // ----------------------------------------------------------------------------
       
    34 CCsMsgPluginUtility* CCsMsgPluginUtility::NewL( )
       
    35     {
       
    36     CCsMsgPluginUtility* self = new ( ELeave ) CCsMsgPluginUtility();
       
    37     return self;
       
    38     }
       
    39 
       
    40 // ----------------------------------------------------------------------------
       
    41 // CCsMsgPluginUtility::~CCsMsgPluginUtility
       
    42 // Destructor
       
    43 // ----------------------------------------------------------------------------
       
    44 CCsMsgPluginUtility::~CCsMsgPluginUtility()
       
    45     {
       
    46     }
       
    47 
       
    48 // ----------------------------------------------------------------------------
       
    49 // Constructor
       
    50 // ----------------------------------------------------------------------------
       
    51 CCsMsgPluginUtility::CCsMsgPluginUtility()
       
    52     {
       
    53     }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // CCsMsgPluginUtility::CreateConversationEntryL
       
    57 // Creates CCsConversationEntry
       
    58 // ----------------------------------------------------------------------------
       
    59 //
       
    60 CCsConversationEntry* CCsMsgPluginUtility::CreateConversationEntryL(
       
    61         const HBufC* aContact,
       
    62         TMsvId aEnryId,
       
    63         TInt64 aTimeStamp,
       
    64         TMsvId aDir,
       
    65         const HBufC* aDescription,
       
    66         TCsSendState aSendState,
       
    67         TCsAttribute aMsgAttribs,
       
    68         TCsType aCsType) const 
       
    69     {
       
    70     // create CCsConversationEntry
       
    71     CCsConversationEntry *conversationEntry = CCsConversationEntry::NewL();
       
    72     CleanupStack::PushL(conversationEntry);
       
    73         
       
    74     //set the values
       
    75     conversationEntry->SetContactL( *aContact );
       
    76     conversationEntry->SetEntryId( aEnryId );
       
    77     conversationEntry->SetTimeStampL( aTimeStamp );
       
    78     conversationEntry->SetDescriptionL( *aDescription );
       
    79     conversationEntry->SetConversationDir( MapDirection( aDir ) );
       
    80     conversationEntry->SetSendState( aSendState );
       
    81     conversationEntry->ChangeAttributes( aMsgAttribs, ECsAttributeNone );
       
    82     conversationEntry->SetType(aCsType);
       
    83         
       
    84     //pop and return
       
    85     CleanupStack::Pop(conversationEntry);
       
    86     return conversationEntry;
       
    87     }
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // CCsMsgPluginUtility::MapDirection
       
    91 // Map the msg direction to TCsDirection
       
    92 // ----------------------------------------------------------------------------
       
    93 //
       
    94 TCsDirection CCsMsgPluginUtility::MapDirection( TMsvId aDir ) const
       
    95     {
       
    96     TCsDirection direction = ECsDirectionUnknown;    
       
    97     switch(aDir)
       
    98         {
       
    99         case KMsvSentEntryIdValue:
       
   100         case KMsvGlobalOutBoxIndexEntryIdValue:  // Fall-through
       
   101         case KMsvDraftEntryIdValue:
       
   102             {
       
   103             direction = ECsDirectionOutgoing;
       
   104             break;
       
   105             }
       
   106         case KMsvGlobalInBoxIndexEntryIdValue:
       
   107             {
       
   108             direction = ECsDirectionIncoming;
       
   109             break;
       
   110             }
       
   111         }
       
   112     return direction;
       
   113     }
       
   114 
       
   115 // ----------------------------------------------------------------------------
       
   116 // CCsMsgPluginUtility::GetMsgAttributes
       
   117 // Returns the message attributes as a bitmask of TCsAttribute values.
       
   118 // ----------------------------------------------------------------------------
       
   119 //
       
   120 TUint16 CCsMsgPluginUtility::GetMsgAttributes( const TMsvEntry& aEntry )
       
   121     {
       
   122     TUint16 msgAttributes = ECsAttributeNone;
       
   123     TMsvId parent = aEntry.Parent();
       
   124 
       
   125     // New
       
   126     if( aEntry.New() )
       
   127         {
       
   128         msgAttributes |= ECsAttributeNew;
       
   129         }
       
   130 
       
   131     // Drafts
       
   132     if( KMsvDraftEntryIdValue == parent )
       
   133         {
       
   134         msgAttributes |= ECsAttributeDraft;
       
   135         }
       
   136 
       
   137     // Attachments
       
   138     if( aEntry.Attachment() )
       
   139         {
       
   140         msgAttributes |= ECsAttributeAttachment;
       
   141         }
       
   142 
       
   143     // Priority
       
   144     TMsvPriority priority = aEntry.Priority();
       
   145     if( EMsvHighPriority == priority )
       
   146         {
       
   147         msgAttributes |= ECsAttributeHighPriority;
       
   148         }
       
   149     else if( EMsvLowPriority == priority )
       
   150         {
       
   151         msgAttributes |= ECsAttributeLowPriority;
       
   152         }
       
   153 
       
   154     // Read/Unread
       
   155     if( KMsvGlobalInBoxIndexEntryIdValue == parent )
       
   156         {
       
   157         if( EFalse == aEntry.Unread() )
       
   158             {
       
   159             // Read
       
   160             msgAttributes &= ~ECsAttributeUnread;
       
   161             }
       
   162         else
       
   163             {
       
   164             // Unread
       
   165             msgAttributes |= ECsAttributeUnread;
       
   166             }
       
   167         }
       
   168 
       
   169     // Sent
       
   170     if( KMsvSentEntryIdValue == parent )
       
   171         {
       
   172         msgAttributes |= ECsAttributeSent;
       
   173         }
       
   174 
       
   175     return msgAttributes;
       
   176     }
       
   177 
       
   178 // ----------------------------------------------------------------------------
       
   179 // CCsMsgPluginUtility::GetSendState
       
   180 // Returns the send status.
       
   181 // ----------------------------------------------------------------------------
       
   182 //
       
   183 TCsSendState CCsMsgPluginUtility::GetSendState( const TMsvEntry& aContext )
       
   184     {
       
   185     TUint sendingState = aContext.SendingState();
       
   186     TCsSendState convSendState = ECsSendStateUnknown;
       
   187 
       
   188     switch( sendingState )
       
   189         {
       
   190         case KMsvSendStateUponRequest:
       
   191             {
       
   192             convSendState = ECsSendStateUponRequest;
       
   193             break;
       
   194             }
       
   195         case KMsvSendStateWaiting:
       
   196             {
       
   197             convSendState = ECsSendStateWaiting;
       
   198             break;
       
   199             }
       
   200         case KMsvSendStateSending:
       
   201             {
       
   202             convSendState = ECsSendStateSending;
       
   203             break;
       
   204             }
       
   205         case KMsvSendStateScheduled:
       
   206             {
       
   207             convSendState = ECsSendStateScheduled;
       
   208             break;
       
   209             }
       
   210         case KMsvSendStateResend:
       
   211             {
       
   212             convSendState = ECsSendStateResend;
       
   213             break;
       
   214             }
       
   215         case KMsvSendStateSuspended:
       
   216             {
       
   217             convSendState = ECsSendStateSuspended;
       
   218             break;
       
   219             }
       
   220         case KMsvSendStateFailed:
       
   221             {
       
   222             convSendState = ECsSendStateFailed;
       
   223             break;
       
   224             }
       
   225         case KMsvSendStateSent:
       
   226             {
       
   227             convSendState = ECsSendStateSent;
       
   228             break;
       
   229             }
       
   230         case KMsvSendStateNotApplicable:
       
   231             {
       
   232             convSendState = ECsSendStateNotApplicable;
       
   233             break;
       
   234             }
       
   235         }
       
   236     return convSendState;
       
   237     }
       
   238 
       
   239 // ----------------------------------------------------------------------------
       
   240 // CCsMsgPluginUtility::GetContact
       
   241 // Get the Contact (From/To) of the message
       
   242 // ----------------------------------------------------------------------------
       
   243 //
       
   244 void CCsMsgPluginUtility::CreateContactL(CMsvSession* aSession,
       
   245 					const TMsvEntry& aEntry,
       
   246 					RPointerArray<HBufC>& addressList)
       
   247 	{
       
   248     if ( aEntry.iMtm.iUid == KSenduiMtmSmsUidValue )      
       
   249         {
       
   250         CPlainText* nullString = CPlainText::NewL();
       
   251         CleanupStack::PushL( nullString );
       
   252     
       
   253         CSmsHeader* smsHeader = NULL;
       
   254         if ( aEntry.Parent() == KMsvGlobalInBoxIndexEntryIdValue ) 
       
   255             {
       
   256             smsHeader = CSmsHeader::NewL( CSmsPDU::ESmsDeliver, *nullString );
       
   257             }
       
   258         else if ( aEntry.Parent() == KMsvSentEntryIdValue || 
       
   259                   aEntry.Parent() == KMsvGlobalOutBoxIndexEntryIdValue )
       
   260             {
       
   261             smsHeader = CSmsHeader::NewL( CSmsPDU::ESmsSubmit, *nullString );
       
   262             }
       
   263         else
       
   264             {
       
   265             CleanupStack::PopAndDestroy(nullString);
       
   266             return;
       
   267             }
       
   268         CleanupStack::PushL( smsHeader );
       
   269     
       
   270         CMsvEntry *cEntry = CMsvEntry::NewL(*aSession,aEntry.Id(),
       
   271             TMsvSelectionOrdering() );
       
   272         CleanupStack::PushL( cEntry );
       
   273     
       
   274         CMsvStore* store = cEntry->ReadStoreL();
       
   275         CleanupStack::PushL(store);
       
   276     
       
   277         TRAPD(err, smsHeader->RestoreL( *store ));    
       
   278         if ( err == KErrNone )
       
   279             {
       
   280             if ( aEntry.Parent() == KMsvGlobalOutBoxIndexEntryIdValue ||
       
   281                  aEntry.Parent() == KMsvSentEntryIdValue )
       
   282                 {
       
   283                 const CArrayPtrFlat<CSmsNumber>& rcpts = smsHeader->Recipients();
       
   284                 const CSmsNumber& rcpt = *rcpts.At(0);
       
   285 
       
   286                 HBufC* fromAddress = (rcpt.Address()).AllocL();
       
   287                 addressList.Append(fromAddress);
       
   288                 }
       
   289             else
       
   290                 {
       
   291                 // Check and add if any email addresses                
       
   292                 const CSmsEmailFields& emailFields = smsHeader->EmailFields();
       
   293                 if( emailFields.HasAddress() )
       
   294                     {                  
       
   295                     TPtrC name;
       
   296                     TPtrC address;
       
   297 
       
   298                     const MDesCArray& emailRecipients = emailFields.Addresses();
       
   299                     for( TInt id = 0; id < emailRecipients.MdcaCount(); id++ )
       
   300                         {
       
   301                         NameAndAddress( emailRecipients.MdcaPoint( id ), name, address);
       
   302                         
       
   303                         // Add address to list
       
   304                         HBufC* addressBuf = HBufC::NewL( address.Length() );
       
   305                         TPtr addressPtr( 0, 0 );
       
   306                         addressPtr.Set( addressBuf->Des() );
       
   307                         addressPtr.Copy( address );
       
   308                         addressList.Append(addressBuf);
       
   309                         }
       
   310                     }
       
   311                 else
       
   312                     { 
       
   313                     HBufC* fromAddress = (smsHeader->FromAddress()).AllocL();   
       
   314                     
       
   315                     // Try iDetails if empty
       
   316                     if ( fromAddress->Length() == 0 )
       
   317                         fromAddress = aEntry.iDetails.AllocL();
       
   318                     
       
   319                     addressList.Append(fromAddress);
       
   320                     }
       
   321                 }
       
   322             }
       
   323         
       
   324         // Cleanup 
       
   325         CleanupStack::PopAndDestroy(store);
       
   326         CleanupStack::PopAndDestroy(cEntry);
       
   327         CleanupStack::PopAndDestroy(smsHeader);
       
   328         CleanupStack::PopAndDestroy(nullString);
       
   329         }
       
   330     else if ( aEntry.iMtm.iUid == KSenduiMtmBtUidValue  ||  
       
   331 			  aEntry.iMtm.iUid == KSenduiMtmBioUidValue ||
       
   332 			  aEntry.iMtm == KUidMtmWapPush )
       
   333         {
       
   334         HBufC* fromAddress = aEntry.iDetails.AllocL();
       
   335         addressList.Append(fromAddress);
       
   336         }
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------
       
   340 // CCsMsgPluginUtility::CompareEntry
       
   341 //
       
   342 // ---------------------------------------------------------
       
   343 TBool CCsMsgPluginUtility::CompareEntry( const TMsvEntry& aOldContext,  
       
   344 	const TMsvEntry& aNewContext, TMsvId aDir ) const
       
   345     {
       
   346     if(	(aOldContext.Id() == aNewContext.Id()) 
       
   347         &&   (aOldContext.Parent() == aNewContext.Parent())
       
   348         &&   (0 == aOldContext.iDescription.Compare(aNewContext.iDescription)) )
       
   349     {
       
   350     // incase of outbox, check sending state also
       
   351     if(aDir == KMsvGlobalOutBoxIndexEntryIdValue)
       
   352         {
       
   353         if(aOldContext.SendingState() == aNewContext.SendingState())
       
   354             return ETrue;
       
   355         else
       
   356             return EFalse;
       
   357         }
       
   358     return ETrue;
       
   359     }
       
   360 
       
   361     return EFalse;
       
   362     }
       
   363 
       
   364 // ----------------------------------------------------------------------------
       
   365 // CCsMsgPluginUtility::NameAndAddress
       
   366 // Extracts name and address from the aMsvAddress
       
   367 // which is of the form name<address>
       
   368 // ----------------------------------------------------------------------------
       
   369 void CCsMsgPluginUtility::NameAndAddress( const TPtrC& aMsvAddress, 
       
   370 				TPtrC& aName, 
       
   371 				TPtrC& aAddress )
       
   372     {
       
   373     // For address information separation (start)
       
   374     const TUint KMsgSmsAddressStartChar         ('<');
       
   375 
       
   376     // For address information separation (end)
       
   377     const TUint KMsgSmsAddressEndChar           ('>');
       
   378 
       
   379     TInt addressStart = aMsvAddress.LocateReverse( KMsgSmsAddressStartChar );
       
   380     TInt addressEnd = aMsvAddress.LocateReverse( KMsgSmsAddressEndChar );
       
   381 
       
   382     if ( addressStart != KErrNotFound && addressEnd != KErrNotFound
       
   383             && addressEnd > addressStart )
       
   384         {
       
   385         // verified address, will be used as selected from contacts manager
       
   386         aName.Set( aMsvAddress.Ptr(), addressStart );
       
   387         aAddress.Set(
       
   388                 aMsvAddress.Mid( addressStart + 1 ).Ptr(),
       
   389                 ( addressEnd - addressStart ) -1 );
       
   390         if ( !aAddress.Length())
       
   391             {
       
   392             aAddress.Set( aName );
       
   393             aName.Set( KNullDesC ); // empty string
       
   394             }
       
   395         }
       
   396     else
       
   397         {
       
   398         // unverified string, will be used as entered in the header field
       
   399         aName.Set( KNullDesC ); // empty string
       
   400         aAddress.Set( aMsvAddress.Ptr(), aMsvAddress.Length() ); // a whole string to address
       
   401         }
       
   402 
       
   403     if ( aName.CompareF( aAddress ) == 0 )
       
   404         {
       
   405         aName.Set( KNullDesC ); // empty string
       
   406         }
       
   407     }
       
   408 
       
   409 // ---------------------------------------------------------
       
   410 // CCsMsgPluginUtility::PureAddress
       
   411 // Extract the phone number for MMS
       
   412 // ---------------------------------------------------------
       
   413 //
       
   414 TPtrC CCsMsgPluginUtility::PureAddress( const TDesC& aAddress )
       
   415     {
       
   416     _LIT( KOpen, "<" );
       
   417     _LIT( KClose, ">" );
       
   418 	
       
   419     // syntax is :
       
   420     // <alias><separator1><pure_address><separator2> |
       
   421     // <pure_address>
       
   422     TInt firstPos = 0;
       
   423     TInt lastPos = 0;
       
   424     TInt length = aAddress.Length();
       
   425     TInt sepaLen1 = KOpen().Length();
       
   426     TInt sepaLen2 = KClose().Length();
       
   427     TInt firstSeparatorPosition = 0;
       
   428 
       
   429     while( firstSeparatorPosition >= 0 )
       
   430         {
       
   431         firstSeparatorPosition = aAddress.Mid( firstPos ).Find( KOpen );
       
   432         if ( firstSeparatorPosition >= 0 )
       
   433             {
       
   434             firstPos += firstSeparatorPosition + sepaLen1;
       
   435             }
       
   436         }
       
   437     if ( firstPos <= 0 )
       
   438         {
       
   439         // No alias
       
   440         return aAddress;
       
   441         }
       
   442 
       
   443     // Check if the second separator ends the address
       
   444     TPtrC last = aAddress.Right( sepaLen2 );
       
   445     lastPos = length - sepaLen2;
       
   446 
       
   447     if ( !last.Compare( KClose ) )
       
   448         {
       
   449         // Alias part found
       
   450         if ( lastPos > firstPos )
       
   451             {
       
   452             return aAddress.Mid( firstPos, lastPos - firstPos );
       
   453             }
       
   454         }
       
   455     // No alias defined - return the original string as pure address
       
   456     // If syntax is weird, namely 
       
   457     // alias <>
       
   458     // with nothing between the separators, we return the original string as is
       
   459     return aAddress;
       
   460     }
       
   461 
       
   462 //End of File