simpleengine/xmlutils/src/simplepresencelist.cpp
changeset 0 c8caa15ef882
equal deleted inserted replaced
-1:000000000000 0:c8caa15ef882
       
     1 /*
       
     2 * Copyright (c) 2006 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:    Simple Engine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <s32strm.h>
       
    25 #include <e32base.h>
       
    26 #include <SenBaseElement.h>
       
    27 #include <SenBaseAttribute.h>
       
    28 #include <SenXmlUtils.h>
       
    29 #include <SenXmlReader.h>
       
    30 #include <SenDomFragment.h>
       
    31 
       
    32 #include <multipartparser.h>
       
    33 #include <bodypart.h>
       
    34 
       
    35 // own simple
       
    36 #include "simplecommon.h"
       
    37 #include "simpleelement.h"
       
    38 #include "simplenamespace.h"
       
    39 #include "simpledocument.h"
       
    40 #include "simplemeta.h"
       
    41 #include "simpleutils.h"
       
    42 #include "simplepresencelist.h"
       
    43 
       
    44 #include "simplecommon.h"
       
    45 
       
    46 #ifdef _DEBUG
       
    47 #include "simpledebugutils.h"
       
    48 #endif
       
    49 
       
    50 
       
    51 // ================= MEMBER FUNCTIONS =======================
       
    52 //
       
    53 
       
    54 // ----------------------------------------------------------
       
    55 // CSimplePresenceList::CSimplePresenceList
       
    56 // ----------------------------------------------------------
       
    57 //
       
    58 CSimplePresenceList::CSimplePresenceList( )
       
    59 : iMeta( NULL )
       
    60     {
       
    61     }
       
    62 
       
    63 // ----------------------------------------------------------
       
    64 // CSimplePresenceList::~CSimplePresenceList
       
    65 // ----------------------------------------------------------
       
    66 //
       
    67 CSimplePresenceList::~CSimplePresenceList()
       
    68     {
       
    69     delete iMeta;
       
    70     iPresList.ResetAndDestroy();
       
    71     iDocuments.ResetAndDestroy();         
       
    72     iPresList.Close();
       
    73     iDocuments.Close();
       
    74     }
       
    75 
       
    76 // ----------------------------------------------------------
       
    77 // CSimplePresenceList::ConstructL
       
    78 // ----------------------------------------------------------
       
    79 //
       
    80 void CSimplePresenceList::ConstructL(
       
    81   const TDesC8& aData, const TDesC8& aBoundary, const TDesC8& aStart )
       
    82     {       
       
    83     _LIT(KUrl, "http://dummy.com/d1/d.html");
       
    84            
       
    85     // body part array
       
    86     RPointerArray<CBodyPart> bodyPartArray;
       
    87     // Cleanup-routine
       
    88     TCleanupItem clItem( ResetAndDestroy, &bodyPartArray  );
       
    89     CleanupStack::PushL( clItem );
       
    90     
       
    91     // remove "..." characters from boundary if needed
       
    92     TPtrC8 pUnQuoted = aBoundary;
       
    93     TInt quoted = aBoundary.Locate('"');
       
    94     if (!quoted)
       
    95         {
       
    96         pUnQuoted.Set( aBoundary.Mid( 1, aBoundary.Length() - 2 ));
       
    97         } 
       
    98         
       
    99             
       
   100     // parse
       
   101     MultipartParser::ParseL( aData, KSimpleMultipartType, pUnQuoted, KUrl, bodyPartArray  );    
       
   102     DoConstructL( bodyPartArray, aStart );
       
   103    
       
   104     CleanupStack::PopAndDestroy( ); // bodyPartArray 
       
   105     }
       
   106         
       
   107 // ----------------------------------------------------------
       
   108 // CSimplePresenceList::NewL
       
   109 // ----------------------------------------------------------
       
   110 //
       
   111 CSimplePresenceList* CSimplePresenceList::NewL( const TDesC8& aXml,
       
   112     const TDesC8& aBoundary, const TDesC8& aStart  )
       
   113     {
       
   114     CSimplePresenceList* self = new (ELeave) CSimplePresenceList( );
       
   115     CleanupStack::PushL( self );
       
   116     self->ConstructL( aXml, aBoundary, aStart );
       
   117     CleanupStack::Pop( self );
       
   118     return self;
       
   119     }
       
   120 
       
   121 // ----------------------------------------------------------
       
   122 // CSimplePresenceList::Close
       
   123 // ----------------------------------------------------------
       
   124 //
       
   125 void CSimplePresenceList::Close()
       
   126     {
       
   127     delete this;
       
   128     }
       
   129     
       
   130 // ----------------------------------------------------------
       
   131 // CSimplePresenceList::ResetAndDestroy
       
   132 // ----------------------------------------------------------
       
   133 //
       
   134 void CSimplePresenceList::ResetAndDestroy( TAny* aPtrArray )
       
   135     {
       
   136     RPointerArray<CBodyPart>* array =
       
   137     	static_cast<RPointerArray<CBodyPart>*>( aPtrArray );
       
   138     array->ResetAndDestroy();
       
   139     array->Close();	
       
   140     }    
       
   141 
       
   142 // ----------------------------------------------------------
       
   143 // CSimplePresenceList::GetDocuments
       
   144 // ----------------------------------------------------------
       
   145 //
       
   146 void CSimplePresenceList::GetDocuments( RPointerArray<MSimpleDocument>& aDocs )
       
   147     {
       
   148     aDocs.Reset();
       
   149     TInt size = iDocuments.Count();
       
   150     for ( TInt i = 0; i<size ; i++ )
       
   151         {
       
   152         (void)aDocs.Append( iDocuments[i] );
       
   153         }    
       
   154     }
       
   155 
       
   156 // ----------------------------------------------------------
       
   157 // CSimplePresenceList::MetaData
       
   158 // ----------------------------------------------------------
       
   159 //
       
   160 MSimpleMeta* CSimplePresenceList::MetaData()
       
   161     {
       
   162     return iMeta;
       
   163     }
       
   164 
       
   165 // ----------------------------------------------------------
       
   166 // CSimplePresenceList::GetSubLists
       
   167 // ----------------------------------------------------------
       
   168 //
       
   169 void CSimplePresenceList::GetSubLists( RPointerArray<MSimplePresenceList>& aLists )
       
   170     {
       
   171     aLists.Reset();
       
   172     TInt size = iPresList.Count();
       
   173     for ( TInt i = 0; i<size ; i++ )
       
   174         {
       
   175         (void)aLists.Append( iPresList[i] );
       
   176         }     
       
   177     }
       
   178     
       
   179 // ----------------------------------------------------------
       
   180 // CSimplePresenceList::DoConstructL
       
   181 // ----------------------------------------------------------
       
   182 //    
       
   183 void CSimplePresenceList::DoConstructL( 
       
   184     RPointerArray<CBodyPart>& aParts, const TDesC8& aStart )
       
   185     {
       
   186     
       
   187     _LIT8( KMyContentType, "Content-Type: multipart/related;");  
       
   188     _LIT8( KMyBoundary, "boundary="); 
       
   189     
       
   190     const TInt myBoundaryLen = 9;   // boundary=  9 characters         
       
   191        
       
   192     // Handle body parts one by one
       
   193     TInt size = aParts.Count();
       
   194     TInt i;
       
   195     CBodyPart* cp = NULL;
       
   196     TPtrC8 boundary;
       
   197     TPtrC8 start;
       
   198     
       
   199     // remove "..." quoted marks when needed
       
   200     TPtrC8 pStartUnquoted( KNullDesC8 );
       
   201     if ( aStart.Locate( '"') == 0 )
       
   202         {
       
   203         pStartUnquoted.Set( aStart.Mid( 1, aStart.Length() - 2 ));
       
   204         }
       
   205     else
       
   206         {
       
   207         pStartUnquoted.Set( aStart );        
       
   208         } 
       
   209 
       
   210     // remove <...> marks when needed
       
   211     if ( pStartUnquoted.Locate( '<') == 0 )
       
   212         {
       
   213         pStartUnquoted.Set( pStartUnquoted.Mid( 1, pStartUnquoted.Length() - 2 ));
       
   214         } 
       
   215     
       
   216     TPtrC8 p8;
       
   217     p8.Set( KSimpleDocumentType );
       
   218     TInt mySize = p8.Length();
       
   219     p8.Set( KSimpleMultipartType );
       
   220     TInt mySize2 = p8.Length();    
       
   221           
       
   222 #ifdef _DEBUG                
       
   223     TPtrC p16b; // notice: for debug
       
   224     TPtrC8 p8b;
       
   225 #endif 
       
   226 
       
   227 // Make this handle direct content too in the RLMI list, i.e. 
       
   228 // content-type: multipart/related; type="application/pidf+xml"; boundary=...;
       
   229     for (i = 0; i < size; i++)
       
   230         {
       
   231         cp = aParts[i];
       
   232         // inline const TDesC8& Headers() { return iHeaders; }
       
   233         TPtrC8 allHeaders = cp->Headers();
       
   234         HBufC8* headersBuff = allHeaders.AllocL();
       
   235         CleanupStack::PushL( headersBuff );
       
   236                
       
   237 #ifdef _DEBUG        
       
   238         p16b.Set( cp->Url() ); // debug only
       
   239         p8b.Set( cp->ContentType() ); // debug only
       
   240         p8b.Set( cp->ContentID() ); // debug only
       
   241 #endif                         
       
   242         if ( !pStartUnquoted.Compare( cp->ContentID()) &&
       
   243              !cp->ContentType().Left(sizeof(KSimpleListType)).CompareF( KSimpleListType ))
       
   244             {
       
   245             // Meta data is the root         
       
   246             iMeta = CSimpleMeta::NewL( cp->Body() );                               
       
   247             }
       
   248         else if ( !cp->ContentType().CompareF( KSimpleDocumentType ))
       
   249             {
       
   250             // Ordinary presence element                    
       
   251             CSimpleDocument* cd = CSimpleDocument::NewL( cp->Body() );             
       
   252             CleanupStack::PushL( cd );           
       
   253             User::LeaveIfError( iDocuments.Append( cd ) );
       
   254             CleanupStack::Pop( cd );
       
   255             }             
       
   256         else if ( (!cp->ContentType().Left(mySize2).CompareF( KSimpleMultipartType ))) 
       
   257             {
       
   258             // multipart for user's direct content data
       
   259             // get boundary from headers
       
   260             TPtrC8 pStart(KNullDesC8);
       
   261             TPtrC8 pBoundary(KNullDesC8);          
       
   262             
       
   263             TPtrC8 pHeaders = headersBuff->Des();
       
   264             TPtrC8 pContentType;
       
   265             TInt pos1 = pHeaders.Find( KMyContentType );
       
   266             if ( pos1 >= 0 )
       
   267                 {
       
   268                 TPtrC8 h2 = pHeaders.Mid( pos1 );
       
   269                 TInt pos2 = h2.Locate( '\r');
       
   270                 if ( pos2 < 0 )
       
   271                     {
       
   272                     pContentType.Set( h2 );
       
   273                     }
       
   274                 else
       
   275                     {
       
   276                     pContentType.Set( h2.Left( h2.Length() - pos2 ));                    
       
   277                     }    
       
   278                                                                 
       
   279                 // search boundary 
       
   280                 TInt posx = pContentType.Find( KMyBoundary );
       
   281                 if ( posx >= 0 )
       
   282                     {                    
       
   283                     TPtrC8 h5 = pContentType.Mid( posx );
       
   284                     TInt pos5 = h5.Locate( ';');
       
   285                     if ( pos5 < 0 )
       
   286                         {
       
   287                         // There are no more parameters
       
   288                         pBoundary.Set( h5.Mid( myBoundaryLen )); 
       
   289                         }
       
   290                     else
       
   291                         {
       
   292                         // There are more in the line, has to cut off                        
       
   293                         pBoundary.Set( h5.Mid( myBoundaryLen, pos5 - myBoundaryLen ));                        
       
   294                         }    
       
   295                     }                 
       
   296                 }
       
   297             
       
   298             // "..." characters are removed later from boundary in NewInMultiPartL(
       
   299             CSimpleDocument* cd = CSimpleDocument::NewInMultiPartL( cp->Body(), pBoundary, pStart );
       
   300             CleanupStack::PushL( cd );           
       
   301             User::LeaveIfError( iDocuments.Append( cd ) );
       
   302             CleanupStack::Pop( cd );            
       
   303                         
       
   304             }        
       
   305         /* notice: later nested multiparts if type = rlmi   */
       
   306         else
       
   307             {
       
   308             // This is something that should not be in the document.
       
   309             // Ignore it.
       
   310             } 
       
   311         CleanupStack::PopAndDestroy( headersBuff );                             
       
   312         }    
       
   313     }
       
   314