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