xdmprotocols/XcapProtocol/src/XcapDocument.cpp
branchGCC_SURGE
changeset 28 d9861ae9169c
parent 23 77cb48a03620
parent 26 04ca1926b01c
equal deleted inserted replaced
23:77cb48a03620 28:d9861ae9169c
     1 /*
       
     2 * Copyright (c) 2005 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: CXcapDocument
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <f32file.h>
       
    23 #include <XdmErrors.h>
       
    24 #include "XdmOperationFactory.h"
       
    25 #include "XcapProtocol.h"
       
    26 #include "XcapDocument.h"
       
    27 #include "XcapAppUsage.h"
       
    28 #include "XdmXmlParser.h"
       
    29 #include "XdmNamespace.h"
       
    30 #include "CommonDefines.h"
       
    31 #include "XcapDocumentNode.h"
       
    32 #include "XcapHttpRequest.h"
       
    33 #include "XcapHttpOperation.h"
       
    34 #include "XcapHttpTransport.h"
       
    35 
       
    36 // ================= MEMBER FUNCTIONS =======================
       
    37 //
       
    38 
       
    39 
       
    40 // ----------------------------------------------------------
       
    41 // CXcapDocument::CXcapDocument
       
    42 // 
       
    43 // ----------------------------------------------------------
       
    44 //
       
    45 CXcapDocument::CXcapDocument( TXdmDocType aDocumentType,
       
    46                               const CXdmEngine& aXdmEngine,
       
    47                               const CXcapProtocol& aXcapProtocol ) :
       
    48                               CXdmDocument( CONST_CAST( CXdmEngine&, aXdmEngine ) ),
       
    49                               iDocumentType( aDocumentType ),
       
    50                               iXcapProtocol( CONST_CAST( CXcapProtocol&, aXcapProtocol ) )
       
    51                                                 
       
    52     { 
       
    53     }
       
    54 
       
    55 // ----------------------------------------------------------
       
    56 // CXcapDocument::NewL
       
    57 // 
       
    58 // ----------------------------------------------------------
       
    59 //
       
    60 CXcapDocument* CXcapDocument::NewL( TXdmDocType aDocumentType,
       
    61                                     const TDesC& aDocumentName,
       
    62                                     const CXdmEngine& aXdmEngine,
       
    63                                     const CXcapProtocol& aXcapProtocol )
       
    64     {
       
    65     CXcapDocument* self = new ( ELeave ) CXcapDocument( aDocumentType, aXdmEngine, aXcapProtocol );
       
    66     CleanupStack::PushL( self );
       
    67     self->BaseConstructL( KXcapOperationFactory, aDocumentName );
       
    68     self->ConstructL();
       
    69     CleanupStack::Pop();
       
    70     return self;
       
    71     }
       
    72 
       
    73 // ----------------------------------------------------------
       
    74 // CXcapDocument::NewL
       
    75 // 
       
    76 // ----------------------------------------------------------
       
    77 //
       
    78 CXcapDocument* CXcapDocument::NewL( TXdmDocType aDocumentType,
       
    79                                     const TDesC8& aDocumentName,
       
    80                                     const CXdmEngine& aXdmEngine,
       
    81                                     const CXcapProtocol& aXcapProtocol )
       
    82     {
       
    83     CXcapDocument* self = new ( ELeave ) CXcapDocument( aDocumentType, aXdmEngine, aXcapProtocol );
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL( aDocumentName );
       
    86     CleanupStack::Pop();
       
    87     return self;
       
    88     }
       
    89 
       
    90 // ----------------------------------------------------------
       
    91 // CXcapDocument::ConstructL
       
    92 // 
       
    93 // ----------------------------------------------------------
       
    94 //
       
    95 void CXcapDocument::ConstructL( const TDesC8& aDocumentName )
       
    96     {
       
    97     BaseConstructL( KXcapOperationFactory, aDocumentName );
       
    98     ConstructL();
       
    99     }
       
   100     
       
   101 // ----------------------------------------------------------
       
   102 // CXcapDocument::ConstructL
       
   103 // 
       
   104 // ----------------------------------------------------------
       
   105 //
       
   106 void CXcapDocument::ConstructL()
       
   107     {
       
   108     InstallAppUsageL();
       
   109     iErrorRoot = CXcapDocumentNode::NewL( iXdmEngine, iXcapProtocol );
       
   110     TPtrC8 root( iXcapProtocol.Transport().RootUri() );
       
   111     #ifdef _DEBUG
       
   112         HBufC8* buf = HBufC8::NewLC( iDocumentName->Des().Length() );
       
   113         buf->Des().Copy( iDocumentName->Des() );
       
   114         TPtr8 nameDesc( buf->Des() );
       
   115         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::ConstructL()" ) );
       
   116         iXcapProtocol.WriteToLog( _L8( "  Root URI:   %S" ), &root );
       
   117         iXcapProtocol.WriteToLog( _L8( "  Document:   %S" ), &nameDesc );
       
   118         CleanupStack::PopAndDestroy();  //buf
       
   119     #endif
       
   120     RXcapCache* cache = iXcapProtocol.Cache();
       
   121     if( cache )
       
   122         UpdateDocumentInfoL( cache );
       
   123     else
       
   124         {
       
   125         #ifdef _DEBUG
       
   126             iXcapProtocol.WriteToLog( _L8( "  Cache disabled, no info for the document" ) );
       
   127         #endif
       
   128         }
       
   129     CActiveScheduler::Add( this );
       
   130     }
       
   131 
       
   132 // ----------------------------------------------------
       
   133 // CXcapDocument::~CXcapDocument
       
   134 // 
       
   135 // ----------------------------------------------------
       
   136 //
       
   137 CXcapDocument::~CXcapDocument()
       
   138     {
       
   139     #ifdef _DEBUG
       
   140         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::~CXcapDocument()" ) );
       
   141     #endif
       
   142     Cancel();
       
   143     delete iTempCopy;
       
   144     delete iEightBitName;
       
   145     delete iDocumentRoot;
       
   146     delete iErrorRoot;
       
   147     delete iAppUsage;
       
   148     iNamespaces.ResetAndDestroy();
       
   149     iNamespaces.Close();
       
   150     }
       
   151 
       
   152 // ----------------------------------------------------------
       
   153 // CXcapDocument::ResetContents
       
   154 // 
       
   155 // ----------------------------------------------------------
       
   156 //
       
   157 void CXcapDocument::ResetContents()
       
   158     {
       
   159     iOptions = 0;
       
   160     delete iDocumentRoot;
       
   161     iDocumentRoot = NULL;
       
   162     delete iErrorRoot;
       
   163     iErrorRoot = NULL;
       
   164     delete iTempCopy;
       
   165     iTempCopy = NULL;
       
   166     iNamespaces.ResetAndDestroy();
       
   167     }
       
   168     
       
   169 // ----------------------------------------------------------
       
   170 // CXcapDocument::UpdateDocumentInfoL
       
   171 // 
       
   172 // ----------------------------------------------------------
       
   173 //
       
   174 void CXcapDocument::UpdateDocumentInfoL( RXcapCache* aCacheClient )
       
   175     {
       
   176     #ifdef _DEBUG
       
   177         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::UpdateDocumentInfo()" ) );
       
   178     #endif
       
   179     iETagBuffer.Zero();
       
   180     TCacheEntryInfo cacheEntryInfo;
       
   181     TPtrC8 root( iXcapProtocol.Transport().RootUri() );
       
   182     TInt error = aCacheClient->FetchDocumentInfo( 
       
   183                  iETagBuffer, iDocumentName->Des(), root, cacheEntryInfo );    
       
   184     if( error == KErrNone )
       
   185         {
       
   186         iLastAccess = cacheEntryInfo.iLastAccess;
       
   187         iLastModification = cacheEntryInfo.iLastUpdate;
       
   188         iDataLength = cacheEntryInfo.iDataLength;
       
   189         #ifdef _DEBUG
       
   190             const TInt KDateTimeMaxSize = 100;
       
   191             TBuf8<KDateTimeMaxSize> printBuffer;
       
   192             TBuf<KDateTimeMaxSize> dateTimeBuffer;
       
   193             _LIT( KDateTimeFormat, "%D%M%Y%/0%1%/1%2%/2%3%/3 %:0%H%:1%T%:2%S");
       
   194             iLastModification.FormatL( dateTimeBuffer, KDateTimeFormat );
       
   195             printBuffer.Copy( dateTimeBuffer );
       
   196             iXcapProtocol.WriteToLog( _L8( "  ETag:        %S" ), &iETagBuffer );
       
   197             iXcapProtocol.WriteToLog( _L8( "  Size:        %d bytes" ), iDataLength );
       
   198             iXcapProtocol.WriteToLog( _L8( "  Time stamp:  %S" ), &printBuffer );
       
   199         #endif
       
   200         }
       
   201     else
       
   202         {
       
   203         #ifdef _DEBUG
       
   204             iXcapProtocol.WriteToLog( _L8( "  Cache read complete - Error: %d" ), error );
       
   205         #endif
       
   206         }
       
   207     }
       
   208     
       
   209 // ----------------------------------------------------
       
   210 // CXcapDocument::InstallAppUsageL
       
   211 // 
       
   212 // ----------------------------------------------------
       
   213 //
       
   214 void CXcapDocument::InstallAppUsageL()
       
   215     {
       
   216     #ifdef _DEBUG
       
   217         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::InstallAppUsageL()" ) );
       
   218     #endif  
       
   219     HBufC8* docName = NULL;
       
   220     iAppUsage = CXcapAppUsage::NewL( iXdmEngine, iDocumentType );
       
   221     TPtrC8 auid = iAppUsage->AUID();
       
   222     if( IsGlobalTree( auid ) )
       
   223         {
       
   224         docName = HBufC8::NewLC( auid.Length() + KGlobalDirName().Length() + 
       
   225                                  KXcapGlobalDocName().Length() );
       
   226         docName->Des().Copy( auid );
       
   227         docName->Des().Append( KGlobalDirName );
       
   228         docName->Des().Append( KXcapGlobalDocName );
       
   229         delete iDocumentName;
       
   230         iDocumentName = NULL;
       
   231         iDocumentName = HBufC::NewL( docName->Des().Length() );
       
   232         iDocumentName->Des().Copy( docName->Des() );
       
   233         CleanupStack::PopAndDestroy();  //docName
       
   234         }
       
   235     else
       
   236         {
       
   237         TPtrC8 user = iXcapProtocol.UserName();
       
   238         docName = HBufC8::NewLC( auid.Length() + user.Length() + 
       
   239                                  KUserDirName().Length() + 1 /*'/'*/ + 
       
   240                                  iDocumentName->Des().Length() );
       
   241         docName->Des().Copy( auid );
       
   242         docName->Des().Append( KUserDirName );
       
   243         docName->Des().Append( user );
       
   244         docName->Des().Append( KSlash );
       
   245         docName->Des().Append( iDocumentName->Des() );
       
   246         delete iDocumentName;
       
   247         iDocumentName = NULL;
       
   248         iDocumentName = HBufC::NewL( docName->Des().Length() );
       
   249         iDocumentName->Des().Copy( docName->Des() );
       
   250         CleanupStack::PopAndDestroy();  //docName
       
   251         }
       
   252     
       
   253     }
       
   254 
       
   255 // ----------------------------------------------------
       
   256 // CXcapDocument::IsGlobalTree
       
   257 // 
       
   258 // ----------------------------------------------------
       
   259 //
       
   260 TBool CXcapDocument::IsGlobalTree( const TDesC8& aAuid )
       
   261     {
       
   262     #ifdef _DEBUG
       
   263         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::IsGlobalTree()" ) );
       
   264     #endif
       
   265     TBool found = EFalse;
       
   266     //Update this array when necessary
       
   267     const TPtrC8 KGlobalTreeAuids[] = { TPtrC8( KXdmCapabilityUsageAUID ) };
       
   268     const TInt count = sizeof( KGlobalTreeAuids ) / sizeof( KGlobalTreeAuids[0] );
       
   269     for( TInt i = 0;!found && i < count;i++ )
       
   270         found = aAuid.Compare( KGlobalTreeAuids[i] ) == 0;
       
   271     return found;
       
   272     }
       
   273     
       
   274 // ----------------------------------------------------
       
   275 // CXcapDocument::CreateTempCopyL
       
   276 // 
       
   277 // ----------------------------------------------------
       
   278 //
       
   279 void CXcapDocument::CreateTempCopyL()
       
   280     {
       
   281     #ifdef _DEBUG
       
   282         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::CreateTempCopyL()" ) );
       
   283     #endif
       
   284     CXdmDocument* copy = iXdmEngine.CreateDocumentModelL( Name(), iDocumentType );
       
   285     iTempCopy = ( CXcapDocument* )copy;
       
   286     }
       
   287 
       
   288 // ----------------------------------------------------
       
   289 // CXcapDocument::TempCopyL
       
   290 // 
       
   291 // ----------------------------------------------------
       
   292 //
       
   293 EXPORT_C CXcapDocument* CXcapDocument::TempCopyL()
       
   294     {
       
   295     return CXcapDocument::NewL( iDocumentType, Name(), iXdmEngine, iXcapProtocol );
       
   296     }
       
   297 
       
   298 // ----------------------------------------------------
       
   299 // CXcapDocument::CreateRootL
       
   300 // 
       
   301 // ----------------------------------------------------
       
   302 //
       
   303 CXdmDocumentNode* CXcapDocument::CreateRootL()
       
   304     {
       
   305     #ifdef _DEBUG
       
   306         iXcapProtocol.WriteToLog( _L8( "CXdmDocument::CreateRootL()" ) );
       
   307     #endif
       
   308     delete iDocumentRoot;
       
   309     iDocumentRoot = NULL;
       
   310     iDocumentRoot = CXcapDocumentNode::NewL( iXdmEngine, iXcapProtocol );
       
   311     return iDocumentRoot;
       
   312     }
       
   313 
       
   314 // ----------------------------------------------------
       
   315 // CXcapDocument::AppendPathPartL
       
   316 // 
       
   317 // ----------------------------------------------------
       
   318 //
       
   319 void CXcapDocument::AppendPathPartL( const TDesC& aString )
       
   320     {
       
   321     #ifdef _DEBUG
       
   322         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::PathPartL()" ) );
       
   323     #endif
       
   324     if( iDocumentRoot != NULL )
       
   325         {
       
   326         CXdmDocumentNode* node = NULL;
       
   327         CXdmDocumentNode* parent = iDocumentRoot;
       
   328         while( parent->NextNode() != NULL )
       
   329             parent = parent->NextNode();
       
   330         node = CXcapDocumentNode::NewL( iXdmEngine, aString, iXcapProtocol, parent );
       
   331         parent->SetNextNode( node );
       
   332         }
       
   333     else iDocumentRoot = CXcapDocumentNode::NewL( iXdmEngine, aString, iXcapProtocol );
       
   334     }
       
   335 
       
   336 // ----------------------------------------------------
       
   337 // CXcapDocument::CurrentExtent
       
   338 // 
       
   339 // ----------------------------------------------------
       
   340 //
       
   341 CXdmDocumentNode* CXcapDocument::CurrentExtent() const
       
   342     {
       
   343     #ifdef _DEBUG
       
   344         iXcapProtocol.WriteToLog( _L8( "CXdmDocument::CurrentExtent()" ) );
       
   345     #endif
       
   346     CXdmDocumentNode* ret = NULL;
       
   347     if( iDocumentRoot != NULL )
       
   348         {
       
   349         CXdmDocumentNode* node = iDocumentRoot;
       
   350         while( node->NextNode() != NULL )
       
   351             node = node->NextNode();
       
   352         ret = node;
       
   353         }
       
   354     else
       
   355         {
       
   356         #ifdef _DEBUG
       
   357             iXcapProtocol.WriteToLog( _L8( " This document does not yet have a root, leave with KErrGeneral" ) );
       
   358         #endif
       
   359         User::Leave( KErrGeneral );
       
   360         }
       
   361     return ret;
       
   362     }
       
   363     
       
   364 // ----------------------------------------------------
       
   365 // CXcapDocument::DocumentRoot
       
   366 // 
       
   367 // ----------------------------------------------------
       
   368 //
       
   369 CXdmDocumentNode* CXcapDocument::DocumentRoot() const
       
   370     {
       
   371     return iDocumentRoot;
       
   372     }     
       
   373 
       
   374 // ----------------------------------------------------
       
   375 // CXcapDocument::ErrorRoot
       
   376 // 
       
   377 // ----------------------------------------------------
       
   378 //
       
   379 CXdmDocumentNode* CXcapDocument::ErrorRoot()
       
   380     {
       
   381     if( !iErrorRoot )
       
   382         {
       
   383         iErrorRoot = CXcapDocumentNode::NewL( iXdmEngine, iXcapProtocol );
       
   384         }
       
   385     return iErrorRoot;
       
   386     }
       
   387 
       
   388 // ----------------------------------------------------
       
   389 // CXcapDocument::ResetSubset
       
   390 // 
       
   391 // ----------------------------------------------------
       
   392 //
       
   393 EXPORT_C void CXcapDocument::ResetSubset()
       
   394     {
       
   395     CXdmDocument::ResetSubset();
       
   396     delete iDocumentRoot;
       
   397     iDocumentRoot = NULL;
       
   398     iOptions = 0;
       
   399     }
       
   400            
       
   401 // ----------------------------------------------------
       
   402 // CXcapDocument::ETag
       
   403 // 
       
   404 // ----------------------------------------------------
       
   405 //
       
   406 EXPORT_C TPtrC8 CXcapDocument::ETag() const
       
   407     {
       
   408     return iETagBuffer;
       
   409     }
       
   410         
       
   411 // ----------------------------------------------------
       
   412 // CXcapDocument::SetETag
       
   413 // 
       
   414 // ----------------------------------------------------
       
   415 //     
       
   416 EXPORT_C void CXcapDocument::SetETag( const TDesC8& aETagDesc )
       
   417     {
       
   418     iETagBuffer.Zero();
       
   419     iETagBuffer.Copy( aETagDesc );
       
   420     #ifdef _DEBUG
       
   421         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::SetETag()" ) );
       
   422         iXcapProtocol.WriteToLog( _L8( "  New ETag: %S" ), &iETagBuffer );
       
   423     #endif
       
   424     }
       
   425 
       
   426 // ----------------------------------------------------
       
   427 // CXcapDocument::DataLength
       
   428 // 
       
   429 // ----------------------------------------------------
       
   430 //
       
   431 EXPORT_C TInt CXcapDocument::DataLength() const
       
   432     {
       
   433     return iDataLength;
       
   434     }
       
   435 
       
   436 // ----------------------------------------------------
       
   437 // CXcapDocument::ApplicationUsage
       
   438 // 
       
   439 // ----------------------------------------------------
       
   440 //
       
   441 EXPORT_C CXcapAppUsage& CXcapDocument::ApplicationUsage() const
       
   442     {
       
   443     return *iAppUsage;
       
   444     }
       
   445 
       
   446 // ----------------------------------------------------
       
   447 // CXcapDocument::Protocol
       
   448 // 
       
   449 // ----------------------------------------------------
       
   450 //
       
   451 EXPORT_C CXcapProtocol& CXcapDocument::Protocol() const
       
   452     {
       
   453     return iXcapProtocol;
       
   454     }
       
   455     
       
   456 // ----------------------------------------------------
       
   457 // CXcapDocument::RemoveData
       
   458 // 
       
   459 // ----------------------------------------------------
       
   460 //
       
   461 EXPORT_C void CXcapDocument::RemoveData( CXcapDocumentNode* aDocumentNode )
       
   462     {
       
   463     #ifdef _DEBUG
       
   464         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::RemoveData()" ) );
       
   465     #endif
       
   466     CXcapDocumentNode* parent = ( CXcapDocumentNode* )aDocumentNode->Parent();
       
   467     if( parent != NULL )
       
   468         parent->RemoveNode( aDocumentNode );
       
   469     }
       
   470     
       
   471 // ----------------------------------------------------
       
   472 // CXcapDocument::StartUpdateL
       
   473 // 
       
   474 // ----------------------------------------------------
       
   475 //
       
   476 void CXcapDocument::StartUpdateL()
       
   477     {
       
   478     #ifdef _DEBUG
       
   479         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::StartUpdateL()" ) );
       
   480     #endif
       
   481     TInt resolverError = 0;
       
   482     if( iXcapProtocol.IsSimRequestPending( resolverError ) )
       
   483         {
       
   484         #ifdef _DEBUG
       
   485             iXcapProtocol.WriteToLog( _L8( "  IMS Resolver pending => add to queue" ) );
       
   486         #endif
       
   487         iXcapProtocol.AppendNotifyeeL( this );
       
   488         }
       
   489     else
       
   490         {
       
   491         if( resolverError == KErrNone && !IsActive() && iChangeRequests.Count() > 0 )
       
   492             {
       
   493             iUpdateIndex = 0;
       
   494             iDocumentState = EXdmDocUpdating;
       
   495             iOperationCount = iChangeRequests.Count();
       
   496             ActivateOperationL();
       
   497             }
       
   498         else
       
   499             {
       
   500             #ifdef _DEBUG
       
   501                 iXcapProtocol.WriteToLog( _L8(
       
   502                  "  Already active, no operations or IMS resolver failed => complete with error" ) );
       
   503             #endif
       
   504             TInt error = resolverError == KErrNone ? KErrNotReady : resolverError; 
       
   505             User::RequestComplete( iClientStatus, error );
       
   506             }
       
   507         }
       
   508     }
       
   509 
       
   510 // ----------------------------------------------------
       
   511 // CXcapDocument::StartInternalL
       
   512 // 
       
   513 // ----------------------------------------------------
       
   514 //
       
   515 void CXcapDocument::StartInternalL( TRequestStatus& aStatus )
       
   516     {
       
   517     #ifdef _DEBUG
       
   518         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::StartInternalL()" ) );
       
   519     #endif
       
   520     aStatus = KRequestPending;
       
   521     iClientStatus = &aStatus;
       
   522     StartUpdateL();
       
   523     }
       
   524 
       
   525 // ----------------------------------------------------
       
   526 // CXcapDocument::NotifyResolverCompleteL
       
   527 // 
       
   528 // ----------------------------------------------------
       
   529 //
       
   530 void CXcapDocument::NotifyResolverCompleteL( TInt aError )
       
   531     {
       
   532     #ifdef _DEBUG
       
   533         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::NotifyResolverCompleteL()" ) );
       
   534     #endif
       
   535     if( aError == KErrNone )
       
   536         {
       
   537         #ifdef _DEBUG
       
   538             iXcapProtocol.WriteToLog( _L8( "  IMS Resolver ready" ) );
       
   539         #endif
       
   540         StartUpdateL();
       
   541         }
       
   542     else
       
   543         {
       
   544         #ifdef _DEBUG
       
   545             iXcapProtocol.WriteToLog( _L8( "  IMS Resolver failed => complete with %d" ), aError );
       
   546         #endif
       
   547         DequeueAll();
       
   548         User::RequestComplete( iClientStatus, aError );
       
   549         }
       
   550     }
       
   551     
       
   552 // ----------------------------------------------------
       
   553 // CXcapDocument::ActivateOperationL
       
   554 // 
       
   555 // ----------------------------------------------------
       
   556 //
       
   557 void CXcapDocument::ActivateOperationL()
       
   558     {
       
   559     MXdmOperation* request = iChangeRequests[iUpdateIndex];
       
   560     #ifdef _DEBUG
       
   561         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::ActivateOperation() - Operation: %x" ), request );
       
   562     #endif
       
   563     if( iDocSubset )
       
   564         {
       
   565         #ifdef _DEBUG
       
   566             iXcapProtocol.WriteToLog( _L8( " Document subset => set KXdmOption1 and KXdmOption2" ) );
       
   567         #endif
       
   568         SetOption( KXdmOption1 );
       
   569         SetOption( KXdmOption2 );
       
   570         }
       
   571     if( iXcapProtocol.AuthType() == EXcapAuthEarlyIms )
       
   572         {
       
   573         #ifdef _DEBUG
       
   574             iXcapProtocol.WriteToLog( _L8( " EarlyIMS is in use => set KXdmOption4" ) );
       
   575         #endif
       
   576         SetOption( KXdmOption4 );
       
   577         }
       
   578     request->ExecuteL( iStatus, iOptions );
       
   579     SetActive();
       
   580     }
       
   581 
       
   582 // ----------------------------------------------------
       
   583 // CXcapDocument::SetOption
       
   584 // 
       
   585 // ----------------------------------------------------
       
   586 //
       
   587 void CXcapDocument::SetOption( TInt aOption )
       
   588     {
       
   589     #ifdef _DEBUG
       
   590         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::SetOption()" ) );
       
   591     #endif
       
   592     iOptions |= aOption;
       
   593     }
       
   594        
       
   595 // ----------------------------------------------------
       
   596 // CXcapDocument::CancelUpdate
       
   597 // 
       
   598 // ----------------------------------------------------
       
   599 //
       
   600 void CXcapDocument::CancelUpdate()
       
   601     {
       
   602     #ifdef _DEBUG
       
   603         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::CancelUpdate()" ) );
       
   604     #endif
       
   605     TInt resolverError = 0;
       
   606     if( iXcapProtocol.IsSimRequestPending( resolverError ) )
       
   607         {
       
   608         iXcapProtocol.CancelImsResolver();
       
   609         }
       
   610     else
       
   611         {
       
   612         switch( iDocumentState )
       
   613             {
       
   614             case EResolvingAsyncConflict:
       
   615                 iTempCopy->CancelUpdate();
       
   616                 break;
       
   617             default:
       
   618                 {
       
   619                 TInt count = iChangeRequests.Count();
       
   620                 if( count > 0 )
       
   621                     {
       
   622                     MXdmOperation* request = iChangeRequests[iUpdateIndex];
       
   623                     request->CancelOperation();
       
   624                     #ifdef _DEBUG
       
   625                         iXcapProtocol.WriteToLog( _L8( "  Operation %x cancelled" ), request );
       
   626                     #endif
       
   627                     }
       
   628                 }
       
   629             }
       
   630         }
       
   631     User::RequestComplete( iClientStatus, KErrCancel );
       
   632     }
       
   633 
       
   634 // ---------------------------------------------------------
       
   635 // CXcapDocument::RunL()
       
   636 // 
       
   637 // ---------------------------------------------------------
       
   638 //
       
   639 void CXcapDocument::RunL()
       
   640     {
       
   641     #ifdef _DEBUG
       
   642         HBufC8* buf = HBufC8::NewLC( iDocumentName->Des().Length() );
       
   643         buf->Des().Copy( iDocumentName->Des() );
       
   644         TPtr8 nameDesc( buf->Des() );
       
   645         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::RunL()" ) );
       
   646         iXcapProtocol.WriteToLog( _L8( "  Name:         %S" ), &nameDesc );
       
   647         iXcapProtocol.WriteToLog( _L8( "  Error:        %d" ), iStatus.Int() );
       
   648         iXcapProtocol.WriteToLog( _L8( "  Update index: %d" ), iUpdateIndex );
       
   649         CleanupStack::PopAndDestroy();  //buf
       
   650     #endif
       
   651     if( iStatus == KErrNone )
       
   652         {
       
   653         switch( iDocumentState )
       
   654             {
       
   655             case EXdmDocUpdating:
       
   656                 #ifdef _DEBUG
       
   657                     iXcapProtocol.WriteToLog( _L8( "  Document state: EXdmDocUpdating" ) );
       
   658                 #endif
       
   659                 HandleRequestCompletionL();
       
   660                 break;
       
   661             case EResolvingAsyncConflict:
       
   662                 #ifdef _DEBUG
       
   663                     iXcapProtocol.WriteToLog( _L8( "  Document state: EResolvingAsyncConflict" ) );
       
   664                 #endif
       
   665                 ResolveAsyncConflictL();
       
   666                 break;
       
   667             case ERetryingFailedOperation:
       
   668                 #ifdef _DEBUG
       
   669                     iXcapProtocol.WriteToLog( _L8( "  Document state: ERetryingFailedOperation" ) );
       
   670                 #endif
       
   671                 ResolveAsyncConflictL();
       
   672                 break;
       
   673             default:
       
   674                 break;
       
   675             }
       
   676         }
       
   677     else HandleErrorL();
       
   678     #ifdef _DEBUG
       
   679         TInt biggestBlock( 0 );
       
   680         TInt memory( User::Available( biggestBlock ) );
       
   681         iXcapProtocol.WriteToLog( _L8( "** RunL() completes - Available memory: %d" ), memory );
       
   682     #endif
       
   683     }
       
   684 
       
   685 // ---------------------------------------------------------
       
   686 // CXcapDocument::HandleRequestCompletionL
       
   687 // 
       
   688 // ---------------------------------------------------------
       
   689 //
       
   690 void CXcapDocument::HandleRequestCompletionL()
       
   691     {
       
   692     #ifdef _DEBUG
       
   693         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::HandleRequestCompletionL()" ) );
       
   694     #endif
       
   695     MXdmOperation* request = iChangeRequests[iUpdateIndex];
       
   696     const TXdmCompletionData& data = request->CompletionData();
       
   697     switch( data.iCompletion )
       
   698         {
       
   699         case KInsertConflict:
       
   700             ResolveAsyncConflictL();
       
   701             break;
       
   702         default:
       
   703             {
       
   704             RXcapCache* cache = iXcapProtocol.Cache();
       
   705             if( cache )
       
   706                 UpdateDocumentInfoL( cache );
       
   707             DequeueOperation( request );
       
   708             CheckOperationQueueL();
       
   709             break;
       
   710             }
       
   711         }
       
   712     }
       
   713 
       
   714 // ---------------------------------------------------------
       
   715 // CXcapDocument::ResolveAsyncConflictL
       
   716 // 
       
   717 // ---------------------------------------------------------
       
   718 //
       
   719 void CXcapDocument::ResolveAsyncConflictL()
       
   720     {
       
   721     #ifdef _DEBUG
       
   722         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::ResolveAsyncConflictL()" ) );
       
   723     #endif
       
   724     switch( iDocumentState )
       
   725         {
       
   726         //This means the cached version of this document
       
   727         //is out-of-date and that it needs to be updated.
       
   728         case EXdmDocUpdating:
       
   729             {
       
   730             CreateTempCopyL();
       
   731             iTempCopy->SetETag( iETagBuffer );
       
   732             iTempCopy->FetchDataL();
       
   733             iTempCopy->SaveClientStatus( iStatus );
       
   734             iTempCopy->StartUpdateL();
       
   735             iDocumentState = EResolvingAsyncConflict;
       
   736             SetActive();
       
   737             }
       
   738             break;
       
   739         case EResolvingAsyncConflict:
       
   740             #ifdef _DEBUG
       
   741                 iXcapProtocol.WriteToLog( _L8( "  Async conflict resolved, retry insert" ) );
       
   742             #endif
       
   743             //CheckOperationQueueL() will increment the index
       
   744             iUpdateIndex--;
       
   745             SetETag( iTempCopy->ETag() );
       
   746             iDocumentState = ERetryingFailedOperation;
       
   747             CheckOperationQueueL();
       
   748             break;
       
   749         case ERetryingFailedOperation:
       
   750             #ifdef _DEBUG
       
   751                 iXcapProtocol.WriteToLog( _L8( "  Operation retry ready" ) );
       
   752             #endif
       
   753             //Nothing can be done now but instruct the
       
   754             //client to re-fetch the whole document and
       
   755             //retry the failed operation.
       
   756             User::RequestComplete( iClientStatus, KErrPathNotFound );
       
   757             break;
       
   758         default:
       
   759             break;
       
   760         }
       
   761     }
       
   762     
       
   763 // ---------------------------------------------------------
       
   764 // CXcapDocument::HandleErrorL
       
   765 // 
       
   766 // ---------------------------------------------------------
       
   767 //
       
   768 void CXcapDocument::HandleErrorL()
       
   769     {
       
   770     #ifdef _DEBUG
       
   771         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::HandleErrorL()" ) );
       
   772     #endif
       
   773     TInt clientCode = KErrGeneral;
       
   774     MXdmOperation* request = request = iChangeRequests[iUpdateIndex];
       
   775     //The TXdmCompletionData is guaranteed to point at something 
       
   776     const TXdmCompletionData& data = request->CompletionData();
       
   777     switch( data.iCompletion )
       
   778         {
       
   779         case KErrCancel:
       
   780             #ifdef _DEBUG
       
   781                 iXcapProtocol.WriteToLog( _L8( " Update cancelled" ) );
       
   782             #endif
       
   783             clientCode = KErrCancel;
       
   784             break;
       
   785         case ERetryingFailedOperation:
       
   786             #ifdef _DEBUG
       
   787                 iXcapProtocol.WriteToLog( _L8( "  Operation retry failed" ) );
       
   788             #endif
       
   789             //Nothing can be done now but instruct the
       
   790             //client to re-fetch the whole document and
       
   791             //retry the failed operation.
       
   792             User::RequestComplete( iClientStatus, KXcapErrorUnrecoverableConflict );
       
   793             break;
       
   794         case KXcapErrorHttpConflict:
       
   795             #ifdef _DEBUG
       
   796                 iXcapProtocol.WriteToLog( _L8( "  Conflict, parse error document if available" ) );
       
   797             #endif
       
   798             if( data.iResponseData != NULL )
       
   799                 {
       
   800                 TInt error = KErrNone;
       
   801                 TPtrC8 errorData( data.iResponseData->Des() );
       
   802                 TRAP( error, iXcapProtocol.Parser().ParseDocumentL( errorData, ErrorRoot() ) );
       
   803                 #ifdef _DEBUG
       
   804                     iXcapProtocol.WriteToLog( _L8( "  Parsing completed: %d" ), error );
       
   805                     ErrorRoot()->Print();
       
   806                 #endif
       
   807                 //Suppress build warning
       
   808                 error = KErrNone;
       
   809                 }
       
   810 		        User::RequestComplete( iClientStatus, KXcapErrorHttpConflict );
       
   811             break;
       
   812         case KXcapErrorNetworkNotAvailabe:
       
   813             {
       
   814             #ifdef _DEBUG
       
   815                 iXcapProtocol.WriteToLog( _L8( "  Network not available, check cache" ) );
       
   816             #endif
       
   817             if( iDataLength > 0 )
       
   818                 {
       
   819                 RXcapCache* cache = iXcapProtocol.Cache();
       
   820                 if( cache != NULL )
       
   821                     {
       
   822                     TPtrC name( Name() );
       
   823                     TInt error = KErrNone;
       
   824                     TPtrC8 root = iXcapProtocol.Transport().RootUri();
       
   825                     HBufC8* data = HBufC8::NewLC( iDataLength );
       
   826                     TPtr8 dataPtr( data->Des() );
       
   827                     cache->FetchDocumentContent( dataPtr, name, root );
       
   828                     TRAP( error, iXcapProtocol.Parser().ParseDocumentL( this, dataPtr ) );
       
   829                     #ifdef _DEBUG
       
   830                         iXcapProtocol.WriteToLog( _L8( "  Parsing completed: %d" ), error );
       
   831                     #endif
       
   832                     //Suppress build warning
       
   833                     error = KErrNone;
       
   834                     CleanupStack::PopAndDestroy();  //data
       
   835                     }
       
   836                 }
       
   837             else
       
   838                 {
       
   839                 #ifdef _DEBUG
       
   840                     iXcapProtocol.WriteToLog( _L8( "  No data in cache, nothing to do" ) );
       
   841                 #endif
       
   842                 }
       
   843             User::RequestComplete( iClientStatus, KXcapErrorNetworkNotAvailabe );
       
   844             }
       
   845             break;
       
   846         default:
       
   847             #ifdef _DEBUG
       
   848                 iXcapProtocol.WriteToLog( _L8( " Default case - Result: %d" ), data.iCompletion );
       
   849             #endif
       
   850             clientCode = iStatus.Int();
       
   851             User::RequestComplete( iClientStatus, clientCode );
       
   852         }
       
   853     DequeueOperation( request );
       
   854     }
       
   855      
       
   856 // ---------------------------------------------------------
       
   857 // CXcapDocument::CheckOperationQueueL
       
   858 // 
       
   859 // ---------------------------------------------------------
       
   860 //
       
   861 void CXcapDocument::CheckOperationQueueL()
       
   862     {
       
   863     #ifdef _DEBUG
       
   864         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::CheckOperationQueueL()" ) );
       
   865     #endif
       
   866     iUpdateIndex++;
       
   867     if( iUpdateIndex < iOperationCount )
       
   868         ActivateOperationL();
       
   869     else
       
   870         {
       
   871         DequeueAll();
       
   872         iUpdateIndex = 0;
       
   873         iXcapProtocol.CheckActivity();
       
   874         User::RequestComplete( iClientStatus, KErrNone );   
       
   875         }
       
   876     }
       
   877 
       
   878 // ---------------------------------------------------------
       
   879 // CXcapDocument::DequeueOperation
       
   880 // 
       
   881 // ---------------------------------------------------------
       
   882 //
       
   883 void CXcapDocument::DequeueOperation( MXdmOperation* aOperation )
       
   884     {
       
   885     #ifdef _DEBUG
       
   886         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::DequeueOperation()" ) );
       
   887     #endif
       
   888     TInt index = iChangeRequests.Find( aOperation );
       
   889     if( index >= 0 )
       
   890     	{	
       
   891 		iChangeRequests.Remove( index );    
       
   892         aOperation->Destroy();
       
   893         aOperation = NULL;
       
   894     	}  
       
   895    	iOperationCount = iChangeRequests.Count();
       
   896     }
       
   897 
       
   898 // ---------------------------------------------------------
       
   899 // CXcapDocument::DequeueAll
       
   900 // 
       
   901 // ---------------------------------------------------------
       
   902 //
       
   903 void CXcapDocument::DequeueAll()
       
   904     {
       
   905     #ifdef _DEBUG
       
   906         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::DequeueAll()" ) );
       
   907     #endif
       
   908     TInt count = iChangeRequests.Count();
       
   909     for( TInt i = 0;i < count;i++ )
       
   910     	{
       
   911     	MXdmOperation* request = iChangeRequests[i];
       
   912     	iChangeRequests.Remove( i );
       
   913     	request->Destroy();
       
   914     	request = NULL;
       
   915     	} 
       
   916     }
       
   917       
       
   918 // ---------------------------------------------------------
       
   919 // CXcapDocument::EightBitNameLC
       
   920 // 
       
   921 // ---------------------------------------------------------
       
   922 //
       
   923 HBufC8* CXcapDocument::EightBitNameLC()
       
   924     {
       
   925     /*#ifdef _DEBUG
       
   926         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::EightBitNameLC()" ) );
       
   927     #endif*/
       
   928     __ASSERT_DEBUG( Name().Length() > 0, User::Panic( _L( "CXcapDocument" ), 1 ) );
       
   929     HBufC8* buf = HBufC8::NewLC( Name().Length() );
       
   930     buf->Des().Copy( Name() );
       
   931     return buf;
       
   932     }
       
   933 
       
   934 // ---------------------------------------------------------
       
   935 // CXcapDocument::DocumentType
       
   936 // 
       
   937 // ---------------------------------------------------------
       
   938 //
       
   939 TXdmDocType CXcapDocument::DocumentType() const
       
   940     {
       
   941     return iDocumentType;
       
   942     }
       
   943 
       
   944 // ---------------------------------------------------------
       
   945 // CXcapDocument::AppendNamespaceL
       
   946 // 
       
   947 // ---------------------------------------------------------
       
   948 //
       
   949 void CXcapDocument::AppendNamespaceL( const TDesC8& aUri, const TDesC8& aPrefix )
       
   950     {
       
   951     CXdmNamespace* ns = CXdmNamespace::NewL( aUri, aPrefix );
       
   952     CleanupStack::PushL( ns );
       
   953     User::LeaveIfError( iNamespaces.Append( ns ) );
       
   954     CleanupStack::Pop();  //ns
       
   955     }
       
   956 
       
   957 // ---------------------------------------------------------
       
   958 // CXcapDocument::RemoveNamespace
       
   959 // 
       
   960 // ---------------------------------------------------------
       
   961 //
       
   962 void CXcapDocument::RemoveNamespace( const TDesC8& aUri )
       
   963     {
       
   964     TBool found = EFalse;
       
   965     CXdmNamespace* ns = NULL;
       
   966     TInt count = iNamespaces.Count();
       
   967     for( TInt i = 0;!found && i < count;i++ )
       
   968         {
       
   969         ns = iNamespaces[i];
       
   970         if( ns->Uri().Compare( aUri ) == 0 )
       
   971             {
       
   972             found = ETrue;
       
   973             iNamespaces.Remove( i );
       
   974             delete ns;
       
   975             ns = NULL;
       
   976             }
       
   977         }
       
   978     }
       
   979                     
       
   980 // ---------------------------------------------------------
       
   981 // CXcapDocument::Uri
       
   982 // 
       
   983 // ---------------------------------------------------------
       
   984 //
       
   985 TPtrC8 CXcapDocument::Uri( const TDesC8& aPrefix ) const
       
   986     {
       
   987     TPtrC8 uri( _L8( "" ) );
       
   988     TBool found = EFalse;
       
   989     TInt count = iNamespaces.Count();
       
   990     for( TInt i = 0;!found && i < count ;i++ )
       
   991         {
       
   992         if( iNamespaces[i]->Prefix().Compare( aPrefix ) == 0 )
       
   993             {
       
   994             uri.Set( iNamespaces[i]->Uri() );
       
   995             found = ETrue;
       
   996             }
       
   997         }
       
   998     return uri;
       
   999     }
       
  1000 
       
  1001 // ---------------------------------------------------------
       
  1002 // CXcapDocument::Count
       
  1003 // 
       
  1004 // ---------------------------------------------------------
       
  1005 //
       
  1006 TInt CXcapDocument::Count() const
       
  1007     {
       
  1008     return iNamespaces.Count();
       
  1009     }
       
  1010 
       
  1011 // ---------------------------------------------------------
       
  1012 // CXcapDocument::Prefix
       
  1013 // 
       
  1014 // ---------------------------------------------------------
       
  1015 //
       
  1016 TPtrC8 CXcapDocument::Prefix( TInt aIndex ) const
       
  1017     {
       
  1018     TInt count = iNamespaces.Count();
       
  1019     if( count > 0 && ( aIndex >= 0 && aIndex < count ) )
       
  1020         return iNamespaces[aIndex]->Prefix();
       
  1021     else return TPtrC8();
       
  1022     }
       
  1023     
       
  1024 // ---------------------------------------------------------
       
  1025 // CXcapDocument::Uri
       
  1026 // 
       
  1027 // ---------------------------------------------------------
       
  1028 //
       
  1029 TPtrC8 CXcapDocument::Uri( TInt aIndex ) const
       
  1030     {
       
  1031     TInt count = iNamespaces.Count();
       
  1032     if( count > 0 && ( aIndex >= 0 && aIndex < count ) )
       
  1033         return iNamespaces[aIndex]->Uri();
       
  1034     else return TPtrC8();
       
  1035     }
       
  1036     
       
  1037 // ---------------------------------------------------------
       
  1038 // CXcapDocument::ResetNamespaces
       
  1039 // 
       
  1040 // ---------------------------------------------------------
       
  1041 //
       
  1042 void CXcapDocument::ResetNamespaces( ) 
       
  1043     {   
       
  1044     iNamespaces.ResetAndDestroy();
       
  1045     } 
       
  1046     
       
  1047 // ---------------------------------------------------------
       
  1048 // CXcapDocument::TimeStamp
       
  1049 // 
       
  1050 // ---------------------------------------------------------
       
  1051 //
       
  1052 TTime CXcapDocument::TimeStamp() const
       
  1053     {
       
  1054     return iLastModification;
       
  1055     }
       
  1056 
       
  1057 // ----------------------------------------------------
       
  1058 // CXdmDocument::SaveClientStatus
       
  1059 // 
       
  1060 // ----------------------------------------------------
       
  1061 //
       
  1062 void CXcapDocument::SaveClientStatus( TRequestStatus& aClientStatus )
       
  1063     {
       
  1064     iClientStatus = &aClientStatus;        
       
  1065     }
       
  1066             
       
  1067 // ---------------------------------------------------------
       
  1068 // CXcapDocument::DoCancel
       
  1069 // 
       
  1070 // ---------------------------------------------------------
       
  1071 //
       
  1072 void CXcapDocument::DoCancel()
       
  1073     {
       
  1074     #ifdef _DEBUG
       
  1075         iXcapProtocol.WriteToLog( _L8( "CXcapDocument::DoCancel()" ) );
       
  1076     #endif
       
  1077     if( iClientStatus )
       
  1078         {
       
  1079         User::RequestComplete( iClientStatus, KErrCancel );
       
  1080         #ifdef _DEBUG
       
  1081             iXcapProtocol.WriteToLog( _L8( " Request completed" ) );
       
  1082         #endif
       
  1083         }
       
  1084     else
       
  1085         {
       
  1086         #ifdef _DEBUG
       
  1087             iXcapProtocol.WriteToLog( _L8( " iClientStatus == NULL => do nothing" ) );
       
  1088         #endif
       
  1089         }
       
  1090     }
       
  1091 
       
  1092 
       
  1093 
       
  1094