mobilemessaging/unieditor/mtm/src/UniHeaders.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2005-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: UniHeaders implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <msvstore.h>
       
    22 #include <badesca.h>
       
    23 #include <msvids.h>
       
    24 #include <mtmdef.h>
       
    25 #include <e32def.h>
       
    26 #include <e32math.h>
       
    27 #include <mmsvattachmentmanager.h>
       
    28 #include <cmsvmimeheaders.h>
       
    29 #include <mmsgenutils.h>
       
    30 #include "UniHeaders.h"
       
    31 
       
    32 // EXTERNAL DATA STRUCTURES
       
    33 
       
    34 // EXTERNAL FUNCTION PROTOTYPES  
       
    35 
       
    36 // CONSTANTS
       
    37 const TInt KArrayGranularity = 4;
       
    38 const TUid KUidUniHeaderStream={0x2000B00A}; 
       
    39 
       
    40 // MACROS
       
    41 
       
    42 // LOCAL CONSTANTS AND MACROS
       
    43 
       
    44 // MODULE DATA STRUCTURES
       
    45 
       
    46 // LOCAL FUNCTION PROTOTYPES
       
    47 
       
    48 // ==================== LOCAL FUNCTIONS ====================
       
    49 
       
    50 
       
    51 // ================= MEMBER FUNCTIONS =======================
       
    52 
       
    53 // C++ default constructor can NOT contain any code that
       
    54 // might leave.
       
    55 //
       
    56 CUniHeaders::CUniHeaders()
       
    57 // in a class derived from CBase all member variables are set initially to 0
       
    58     {
       
    59     }
       
    60 
       
    61 // Symbian OS default constructor can leave.
       
    62 void CUniHeaders::ConstructL()
       
    63     {
       
    64     iToArray = new ( ELeave )CDesCArrayFlat( KArrayGranularity );
       
    65     iCcArray = new ( ELeave )CDesCArrayFlat( KArrayGranularity );
       
    66     iBccArray = new ( ELeave )CDesCArrayFlat( KArrayGranularity );
       
    67     }
       
    68 
       
    69 // Two-phased constructor.
       
    70 CUniHeaders* CUniHeaders::NewL()
       
    71     {
       
    72     CUniHeaders* self = new ( ELeave ) CUniHeaders;
       
    73     
       
    74     CleanupStack::PushL( self );
       
    75     self->ConstructL();
       
    76     CleanupStack::Pop( self );
       
    77     return self;
       
    78     }
       
    79 
       
    80     
       
    81 // Destructor
       
    82 CUniHeaders::~CUniHeaders()
       
    83     {
       
    84     delete iToArray;
       
    85     delete iCcArray;
       
    86     delete iBccArray;
       
    87     delete iSubject;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------
       
    91 // CUniHeaders::RestoreL
       
    92 //
       
    93 // ---------------------------------------------------------
       
    94 //
       
    95 
       
    96 void CUniHeaders::RestoreL(
       
    97     CMsvStore& aStore )
       
    98     {
       
    99 
       
   100     RMsvReadStream stream;
       
   101     Reset(); // all old pointers are deleted here
       
   102     if ( aStore.IsPresentL( KUidUniHeaderStream ) )
       
   103         {
       
   104         stream.OpenLC( aStore, KUidUniHeaderStream ); // pushes 'stream' to the stack
       
   105         InternalizeL( stream );
       
   106         CleanupStack::PopAndDestroy( &stream );     
       
   107         }
       
   108     } 
       
   109 
       
   110 // ---------------------------------------------------------
       
   111 // CUniHeaders::StoreL
       
   112 //
       
   113 // ---------------------------------------------------------
       
   114 //
       
   115 
       
   116 void CUniHeaders::StoreL(
       
   117     CMsvStore& aStore )
       
   118     {    
       
   119     // caller must commit the store
       
   120     RMsvWriteStream stream;
       
   121     stream.AssignLC( aStore, KUidUniHeaderStream ); // pushes 'stream' to the stack
       
   122     ExternalizeL( stream );
       
   123     stream.CommitL();
       
   124     stream.Close();
       
   125     CleanupStack::PopAndDestroy( &stream );
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------
       
   129 // CUniHeaders::TypedAddresseeList
       
   130 //
       
   131 // ---------------------------------------------------------
       
   132 //
       
   133 const CDesCArray& CUniHeaders::TypedAddresseeList( 
       
   134     TMsvRecipientType aType ) 
       
   135     {
       
   136     if ( aType == EMsvRecipientTo )
       
   137         {
       
   138         return *iToArray;
       
   139         }
       
   140     else if ( aType == EMsvRecipientCc )
       
   141         {
       
   142         return *iCcArray;
       
   143         }
       
   144     else 
       
   145         {
       
   146         return *iBccArray;
       
   147         }
       
   148     }
       
   149    
       
   150 // ---------------------------------------------------------
       
   151 // CUniHeaders::AddTypedAddresseeL
       
   152 //
       
   153 // ---------------------------------------------------------
       
   154 //
       
   155 void CUniHeaders::AddTypedAddresseeL( 
       
   156     const TDesC&  aRealAddress, 
       
   157     TMsvRecipientType aType  )
       
   158     {
       
   159     if ( aType == EMsvRecipientTo )
       
   160         {
       
   161         iToArray->AppendL( aRealAddress );
       
   162         }
       
   163     if ( aType == EMsvRecipientCc )
       
   164         {
       
   165         iCcArray->AppendL( aRealAddress );
       
   166         }
       
   167     if ( aType == EMsvRecipientBcc ) 
       
   168         {
       
   169         iBccArray->AppendL( aRealAddress );
       
   170         }
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------
       
   174 // CUniHeaders::RemoveAddresseeL
       
   175 //
       
   176 // ---------------------------------------------------------
       
   177 //
       
   178 TBool CUniHeaders::RemoveAddressee( const TDesC& aRealAddress )
       
   179     {
       
   180 
       
   181     if ( RemoveAddressee( *iToArray, aRealAddress) )
       
   182         {
       
   183         return ETrue;
       
   184         }
       
   185     if ( RemoveAddressee( *iCcArray, aRealAddress) )
       
   186         {
       
   187         return ETrue;
       
   188         }
       
   189     if ( RemoveAddressee( *iBccArray, aRealAddress) )
       
   190         {
       
   191         return ETrue;
       
   192         }
       
   193       
       
   194     return EFalse;
       
   195 
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------
       
   199 // CUniHeaders::SetSubjectL
       
   200 //
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 void CUniHeaders::SetSubjectL( const TDesC& aSubject )
       
   204     {
       
   205     HBufC* newAttrib = aSubject.AllocL();
       
   206     delete iSubject;
       
   207     iSubject = newAttrib;
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------
       
   211 // CUniHeaders::Subject
       
   212 //
       
   213 // ---------------------------------------------------------
       
   214 //
       
   215 TPtrC CUniHeaders::Subject() const
       
   216     {
       
   217     return iSubject ? TPtrC( *iSubject ) : TPtrC(); 
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------
       
   221 // CUniHeaders::InternalizeL
       
   222 //
       
   223 // ---------------------------------------------------------
       
   224 //
       
   225 void CUniHeaders::InternalizeL( RMsvReadStream& aStream )
       
   226     {
       
   227 
       
   228     iSubject = HBufC::NewL( aStream, KMaxHeaderStringLength );
       
   229     iMessageTypeSetting = aStream.ReadInt32L();
       
   230     iMessageTypeLocking = aStream.ReadInt32L();
       
   231     iMessageRoot = aStream.ReadInt32L();
       
   232     
       
   233     // Internalize fields which may have multiple values.
       
   234     InternalizeArrayL( *iToArray, aStream ); //lint !e64 !e1514
       
   235     InternalizeArrayL( *iCcArray, aStream ); //lint !e64 !e1514
       
   236     InternalizeArrayL( *iBccArray, aStream ); //lint !e64 !e1514
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------
       
   240 // CUniHeaders::ExternalizeL
       
   241 //
       
   242 // ---------------------------------------------------------
       
   243 //
       
   244 void CUniHeaders::ExternalizeL( RMsvWriteStream& aStream ) const
       
   245     {
       
   246 
       
   247     aStream << Subject(); //lint !e1023 !e1703 not ambiguous
       
   248     aStream.WriteInt32L( iMessageTypeSetting );
       
   249     aStream.WriteInt32L( iMessageTypeLocking );
       
   250     aStream.WriteInt32L( iMessageRoot );
       
   251 
       
   252     // Externalize fields which may have multiple values.
       
   253     ExternalizeArrayL( *iToArray, aStream ); //lint !e64 !e1514
       
   254     ExternalizeArrayL( *iCcArray, aStream ); //lint !e64 !e1514
       
   255     ExternalizeArrayL( *iBccArray, aStream ); //lint !e64 !e1514
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------
       
   259 // CUniHeaders::RemoveAddressee
       
   260 //
       
   261 // ---------------------------------------------------------
       
   262 //
       
   263 TBool CUniHeaders::RemoveAddressee(
       
   264     CDesCArray& aList,
       
   265     const TDesC& aAddress )
       
   266     {
       
   267     
       
   268     TInt pos = 0; 
       
   269 
       
   270     // Find the match index from a given addressee list
       
   271     aList.Find( aAddress, pos );
       
   272     if (pos < aList.Count())
       
   273         {
       
   274         // remove entry from the heap
       
   275         aList.Delete( pos );
       
   276         aList.Compress();
       
   277         return ETrue;
       
   278         }
       
   279     return EFalse;
       
   280     }
       
   281 
       
   282 // ---------------------------------------------------------
       
   283 // CUniHeaders::Reset
       
   284 //
       
   285 // ---------------------------------------------------------
       
   286 //
       
   287 void CUniHeaders::Reset()
       
   288     {
       
   289     
       
   290     iToArray->Reset();
       
   291     iCcArray->Reset(); 
       
   292     iBccArray->Reset(); 
       
   293 
       
   294     delete iSubject;   
       
   295     iSubject = NULL;
       
   296     }
       
   297 
       
   298 // ---------------------------------------------------------
       
   299 // CUniHeaders::Size
       
   300 //
       
   301 // ---------------------------------------------------------
       
   302 //
       
   303 TInt CUniHeaders::Size() const
       
   304     {
       
   305 
       
   306     TInt size = 0;
       
   307 
       
   308     TInt i = 0;
       
   309 
       
   310     TInt nbr = iToArray->MdcaCount();
       
   311     for ( i=0; i < nbr; ++i )
       
   312         {
       
   313         size += iToArray->MdcaPoint(i).Size();
       
   314         }
       
   315 
       
   316     nbr = iCcArray->MdcaCount();
       
   317     for ( i=0; i < nbr; ++i )
       
   318         {
       
   319         size+=iCcArray->MdcaPoint(i).Size();
       
   320         }
       
   321 
       
   322     nbr = iBccArray->MdcaCount();
       
   323     for ( i=0; i < nbr; ++i )
       
   324         {
       
   325         size+=iBccArray->MdcaPoint(i).Size();
       
   326         }
       
   327 
       
   328     size+= Subject().Size();
       
   329     size+= 2 * sizeof( TInt32 ); // iMessageTypeSetting, iMessageTypeLocking
       
   330     
       
   331     return size;
       
   332     }
       
   333 
       
   334 // ---------------------------------------------------------
       
   335 // CUniHeaders::ExternalizeArrayL
       
   336 //
       
   337 // ---------------------------------------------------------
       
   338 //
       
   339 void CUniHeaders::ExternalizeArrayL(
       
   340     CDesC16Array& anArray,
       
   341     RWriteStream& aStream ) const
       
   342     {
       
   343 
       
   344     TInt count=anArray.Count();
       
   345     aStream << TCardinality( count );       // compressed value
       
   346     for ( TInt ii = 0; ii < count; ++ii )
       
   347         {
       
   348         aStream << anArray[ii].Left( Min( anArray[ii].Length(), KMaxHeaderStringLength ) ) ;
       
   349         }
       
   350     }
       
   351 
       
   352 // ---------------------------------------------------------
       
   353 // CUniHeaders::InternalizeArrayL
       
   354 //
       
   355 // ---------------------------------------------------------
       
   356 //
       
   357 void CUniHeaders::InternalizeArrayL(
       
   358     CDesC16Array& anArray,
       
   359     RReadStream& aStream )
       
   360     {
       
   361     TCardinality card;
       
   362     aStream >> card;
       
   363     TInt count=card;
       
   364     anArray.Reset();
       
   365     for ( TInt ii = 0; ii < count; ++ii )
       
   366         {
       
   367         HBufC16* buf=HBufC16::NewLC( aStream,KMaxHeaderStringLength );
       
   368         anArray.CArrayFixBase::InsertL( ii,&buf );
       
   369         CleanupStack::Pop( buf );
       
   370         }
       
   371     }
       
   372 
       
   373 // ================= OTHER EXPORTED FUNCTIONS ==============
       
   374 
       
   375 
       
   376 //  End of File  
       
   377