xdmprotocols/XcapProtocol/XcapCache/Server/src/XcapCacheIndexEntry.cpp
changeset 0 c8caa15ef882
child 12 e6a66db4e9d0
equal deleted inserted replaced
-1:000000000000 0:c8caa15ef882
       
     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:   CXcapCacheIndexEntry
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "ServerDefines.h"
       
    23 #include "XcapCacheServer.h"
       
    24 #include "XcapCacheIndexEntry.h"
       
    25 #include "XcapCacheEntryProperty.h"
       
    26 
       
    27 // ----------------------------------------------------------
       
    28 // CXcapCacheIndexEntry::CXcapCacheIndexEntry
       
    29 // 
       
    30 // ----------------------------------------------------------
       
    31 //
       
    32 CXcapCacheIndexEntry::CXcapCacheIndexEntry()
       
    33     {
       
    34     }
       
    35 
       
    36 // ----------------------------------------------------------
       
    37 // CXcapCacheIndexEntry::NewL
       
    38 // 
       
    39 // ----------------------------------------------------------
       
    40 //
       
    41 CXcapCacheIndexEntry* CXcapCacheIndexEntry::NewL( const TCacheEntryInfo* aHeader )
       
    42     {
       
    43     CXcapCacheIndexEntry* self = new( ELeave ) CXcapCacheIndexEntry();
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL( aHeader );
       
    46     CleanupStack::Pop();
       
    47     return self;
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------
       
    51 // CXcapCacheIndexEntry::NewL
       
    52 // 
       
    53 // ----------------------------------------------------------
       
    54 //
       
    55 CXcapCacheIndexEntry* CXcapCacheIndexEntry::NewL( TPtr8& aEntryData )
       
    56     {
       
    57     CXcapCacheIndexEntry* self = new( ELeave ) CXcapCacheIndexEntry();
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL( aEntryData );
       
    60     CleanupStack::Pop();
       
    61     return self;
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------
       
    65 // CXcapCacheIndexEntry::~CXcapCacheIndexEntry
       
    66 // 
       
    67 // ----------------------------------------------------------
       
    68 //
       
    69 CXcapCacheIndexEntry::~CXcapCacheIndexEntry()
       
    70     {
       
    71     iPropertyList.ResetAndDestroy();
       
    72     iPropertyList.Close();
       
    73     }
       
    74 
       
    75 // ----------------------------------------------------------
       
    76 // CXcapCacheIndexEntry::ConstructL
       
    77 // 
       
    78 // ----------------------------------------------------------
       
    79 //
       
    80 void CXcapCacheIndexEntry::ConstructL( TPtr8& aEntryData )
       
    81     {
       
    82     while( aEntryData.Length() > 0 )
       
    83         {
       
    84         CXcapCacheEntryProperty* prop = NULL;
       
    85         TInt index = aEntryData.FindF( KValueFieldSeparator );
       
    86         if( index > 0 )
       
    87             {
       
    88             TPtrC8 propData = aEntryData.Left( index );
       
    89             TInt separator = propData.FindF( KValuePropertySeparator );
       
    90             if( separator > 0 )
       
    91                 {
       
    92                 TPtrC8 name = propData.Left( separator );
       
    93                 TPtrC8 value = propData.Right( propData.Length() - separator - 1 );
       
    94                 const TInt nameId = FindPropNameId( name );
       
    95                 __ASSERT_DEBUG( nameId != KErrNotFound, User::Leave( KErrCorrupt ) );
       
    96                 prop = CreatePropertyL( nameId, value );
       
    97                 CleanupStack::PushL( prop );
       
    98                 User::LeaveIfError( iPropertyList.Append( prop ) );
       
    99                 CleanupStack::Pop();  //prop
       
   100                 }
       
   101             aEntryData.Delete( 0, index + 1 );
       
   102             }
       
   103         }
       
   104     }
       
   105 
       
   106 // ----------------------------------------------------------
       
   107 // CXcapCacheIndexEntry::ConstructL
       
   108 // 
       
   109 // ----------------------------------------------------------
       
   110 //
       
   111 void CXcapCacheIndexEntry::ConstructL( const TCacheEntryInfo* aHeader )
       
   112     {
       
   113     CreateFromHeaderL( aHeader );
       
   114     }
       
   115     
       
   116 // ----------------------------------------------------------
       
   117 // CXcapCacheIndexEntry::FindPropNameId
       
   118 // 
       
   119 // ----------------------------------------------------------
       
   120 //
       
   121 TInt CXcapCacheIndexEntry::FindPropNameId( const TDesC8& aNameString ) const
       
   122     {
       
   123     TBool found = EFalse;
       
   124     TInt ret = KErrNotFound;
       
   125     TInt arrayLength = sizeof( KCacheEntryArray ) /
       
   126                        sizeof( KCacheEntryArray[0] );
       
   127     for( TInt i = 0;!found && i < arrayLength;i++ )
       
   128         {
       
   129         if( aNameString.CompareF( TPtrC8( KCacheEntryArray[i] ) ) == 0 )
       
   130             {
       
   131             ret = i;
       
   132             found = ETrue;
       
   133             }
       
   134         }
       
   135     return ret;
       
   136     }
       
   137 
       
   138 // ----------------------------------------------------------
       
   139 // CXcapCacheIndexEntry::UpdateEntryL
       
   140 // 
       
   141 // ----------------------------------------------------------
       
   142 //
       
   143 void CXcapCacheIndexEntry::UpdateEntryL( const TCacheEntryInfo* aHeader )
       
   144     {
       
   145     TTime access = LastAccess();
       
   146     DeleteFileL( FileNameL() );
       
   147     iPropertyList.ResetAndDestroy();
       
   148     CreateFromHeaderL( aHeader );
       
   149     }
       
   150 
       
   151 // ----------------------------------------------------------
       
   152 // CXcapCacheIndexEntry::DeleteFileL
       
   153 // 
       
   154 // ----------------------------------------------------------
       
   155 //
       
   156 void CXcapCacheIndexEntry::DeleteFileL( const TDesC8& aFileName )
       
   157     {
       
   158     HBufC* buf = HBufC::NewLC( aFileName.Length() );
       
   159     buf->Des().Copy( aFileName );
       
   160     CFileMan* manager = CFileMan::NewL( CXcapCacheServer::FileSession() );
       
   161     CleanupStack::PushL( manager );
       
   162     TInt error = manager->Delete( buf->Des() );
       
   163     #ifdef _DEBUG
       
   164         CXcapCacheServer::WriteToLog( _L8( "CXcapCacheServer::DeleteFileL() - Error: %d" ), error );
       
   165     #endif
       
   166     CleanupStack::PopAndDestroy( 2 );  //manager, buf
       
   167     }
       
   168 
       
   169 // ----------------------------------------------------------
       
   170 // CXcapCacheIndexEntry::CreatePropertyL
       
   171 // 
       
   172 // ----------------------------------------------------------
       
   173 //
       
   174 CXcapCacheEntryProperty* CXcapCacheIndexEntry::CreatePropertyL( const TInt aPropertyName,
       
   175                                                                 const TDesC8& aPropertyValue )
       
   176     {
       
   177     return CXcapCacheEntryProperty::NewL( aPropertyName, aPropertyValue );
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------
       
   181 // CXcapCacheIndexTableEntry::FindProperty
       
   182 // 
       
   183 // ---------------------------------------------------------
       
   184 //  
       
   185 const CXcapCacheEntryProperty& CXcapCacheIndexEntry::FindProperty( const TInt aPropId ) const
       
   186     {
       
   187     TBool found = EFalse;
       
   188     TInt count = iPropertyList.Count();
       
   189     CXcapCacheEntryProperty* property = NULL;
       
   190     for( TInt i = 0;!found && i < count;i++ )
       
   191         {
       
   192         property = iPropertyList[i];
       
   193         if( property->Name() == aPropId )
       
   194             found = ETrue;
       
   195         }
       
   196     return *property;
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------
       
   200 // CXcapCacheIndexTableEntry::LastAccess
       
   201 // 
       
   202 // ---------------------------------------------------------
       
   203 //  
       
   204 TTime CXcapCacheIndexEntry::LastAccess() const
       
   205     {
       
   206     const CXcapCacheEntryProperty& property = FindProperty( KCacheLastAccess );
       
   207     return property.TimeValue();
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------
       
   211 // CXcapCacheIndexTableEntry::LastModified
       
   212 // 
       
   213 // ---------------------------------------------------------
       
   214 //  
       
   215 TTime CXcapCacheIndexEntry::LastModified() const
       
   216     {
       
   217     const CXcapCacheEntryProperty& property = FindProperty( KCacheLastModified );
       
   218     return property.TimeValue();
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------
       
   222 // CXcapCacheIndexTableEntry::XmlSize
       
   223 // 
       
   224 // ---------------------------------------------------------
       
   225 //  
       
   226 TInt CXcapCacheIndexEntry::XmlSize() const
       
   227     {
       
   228     const CXcapCacheEntryProperty& property = FindProperty( KCacheXmlSize );
       
   229     return property.IntValue();
       
   230     }
       
   231 
       
   232 // ---------------------------------------------------------
       
   233 // CXcapCacheIndexTableEntry::FileNameL
       
   234 // 
       
   235 // ---------------------------------------------------------
       
   236 //  
       
   237 TPtrC8 CXcapCacheIndexEntry::FileNameL() const
       
   238     {
       
   239     const CXcapCacheEntryProperty& property = FindProperty( KCacheXmlFile );
       
   240     return property.DesValue8();
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------
       
   244 // CXcapCacheIndexTableEntry::FileName16L
       
   245 // 
       
   246 // ---------------------------------------------------------
       
   247 //
       
   248 TPtrC CXcapCacheIndexEntry::FileName16L() const
       
   249 	{
       
   250 	const CXcapCacheEntryProperty& property = FindProperty( KCacheXmlFile );
       
   251 	return property.DesValue16();
       
   252 	}
       
   253 
       
   254 // ---------------------------------------------------------
       
   255 // CXcapCacheIndexTableEntry::ETag
       
   256 // 
       
   257 // ---------------------------------------------------------
       
   258 //  
       
   259 TPtrC8 CXcapCacheIndexEntry::ETag() const
       
   260     {
       
   261     const CXcapCacheEntryProperty& property = FindProperty( KCacheEntryETag );
       
   262     return property.DesValue8();
       
   263     }
       
   264 
       
   265 // ----------------------------------------------------------
       
   266 // CXcapCacheIndexEntry::CreateFromHeaderL
       
   267 // 
       
   268 // ----------------------------------------------------------
       
   269 //
       
   270 void CXcapCacheIndexEntry::CreateFromHeaderL( const TCacheEntryInfo* aHeader )
       
   271     {
       
   272     TInt arrayLength = sizeof( KCacheEntryArray ) /
       
   273                        sizeof( KCacheEntryArray[0] );
       
   274     HBufC8* hour = HourStringLC();
       
   275     for( TInt i = 0;i < arrayLength;i++ )
       
   276         {
       
   277         CXcapCacheEntryProperty* prop = NULL;
       
   278         switch( i )
       
   279             {
       
   280             case KCacheEntryETag:
       
   281                 prop = CreatePropertyL( KCacheEntryETag, *aHeader->iEtag );
       
   282                 break;
       
   283             case KCacheXmlSize:
       
   284                 {
       
   285                 TBuf8<10> sizeBuf;
       
   286                 sizeBuf.AppendNum( aHeader->iDataLength );
       
   287                 prop = CreatePropertyL( KCacheXmlSize, sizeBuf );
       
   288                 }
       
   289                 break;
       
   290             case KCacheXmlFile:
       
   291                 {
       
   292                 HBufC8* fileName = StoreXmlFileLC( *aHeader->iRespData );
       
   293                 prop = CreatePropertyL( KCacheXmlFile, fileName->Des() );
       
   294                 CleanupStack::PopAndDestroy();  //fileName
       
   295                 }
       
   296                 break;
       
   297             case KCacheLastAccess:
       
   298                 prop = CreatePropertyL( KCacheLastAccess, hour->Des() );
       
   299                 break;
       
   300             case KCacheLastModified:
       
   301                 prop = CreatePropertyL( KCacheLastModified, hour->Des() );
       
   302                 break;
       
   303             default:
       
   304                 break;
       
   305             }
       
   306         if( prop != NULL )
       
   307             User::LeaveIfError( iPropertyList.Append( prop ) );
       
   308         }
       
   309     CleanupStack::PopAndDestroy();  //hour
       
   310     }
       
   311 
       
   312 // ----------------------------------------------------------
       
   313 // CXcapCacheIndexEntry::HourStringLC
       
   314 // 
       
   315 // ----------------------------------------------------------
       
   316 //
       
   317 HBufC8* CXcapCacheIndexEntry::HourStringLC()
       
   318     {
       
   319     HBufC8* hourString = NULL;
       
   320     HBufC* date = CXcapCacheServer::DateL();
       
   321     HBufC* time = CXcapCacheServer::TimeL();
       
   322     hourString = HBufC8::NewLC( date->Length() + time->Length() + 1 );
       
   323     TPtr8 pointer( hourString->Des() );
       
   324     pointer.Copy( *date );
       
   325     pointer.Append( KPageFileSeparator );
       
   326     pointer.Append( *time );
       
   327     delete date;
       
   328     date = NULL;
       
   329     delete time;
       
   330     time = NULL;
       
   331     return hourString;
       
   332     }
       
   333 
       
   334 // ----------------------------------------------------------
       
   335 // CXcapCacheIndexEntry::StoreXmlFileLC
       
   336 // 
       
   337 // ----------------------------------------------------------
       
   338 //
       
   339 HBufC8* CXcapCacheIndexEntry::StoreXmlFileLC( const TDesC8& aXmlData )
       
   340     {
       
   341     RFile file;
       
   342     HBufC* nameBuf = NULL;
       
   343     HBufC* randomString = NULL;
       
   344     TPtrC randomName( _L( "" ) );
       
   345     TInt error = KErrAlreadyExists;
       
   346     //It is extremely unlikely that the random string generator
       
   347     //creates two exact same patterns, but let's check for it, anyway.
       
   348     while( error == KErrAlreadyExists )
       
   349         {
       
   350         randomString = CXcapCacheServer::RandomStringL();
       
   351         randomName.Set( *randomString );
       
   352         nameBuf = HBufC::NewLC( KCacheServerRoot().Length() + randomName.Length() );
       
   353         nameBuf->Des().Copy( KCacheServerRoot );
       
   354         nameBuf->Des().Append( randomName );
       
   355         error = file.Create( CXcapCacheServer::FileSession(),
       
   356                              nameBuf->Des(), EFileWrite );
       
   357         if( error != KErrNone )
       
   358             CleanupStack::PopAndDestroy();  //nameBuf
       
   359         }
       
   360     file.Write( aXmlData );
       
   361     file.Close();
       
   362     __ASSERT_ALWAYS( nameBuf != NULL, User::Leave( KErrCorrupt ) );
       
   363     HBufC8* ret = HBufC8::NewLC( nameBuf->Des().Length() );
       
   364     ret->Des().Copy( nameBuf->Des() );
       
   365     CleanupStack::Pop();  //ret
       
   366     CleanupStack::PopAndDestroy();  //nameBuf
       
   367     CleanupStack::PushL( ret );
       
   368     return ret;
       
   369     }
       
   370 
       
   371 // ----------------------------------------------------------
       
   372 // CXcapCacheIndexEntry::PrintLC
       
   373 // 
       
   374 // ----------------------------------------------------------
       
   375 //
       
   376 HBufC8* CXcapCacheIndexEntry::PrintLC() const
       
   377     {
       
   378     TInt position = 0;
       
   379     CBufFlat* buffer = NULL;
       
   380     HBufC8* entryString = NULL;
       
   381     TInt count = iPropertyList.Count();
       
   382     if( count > 0 )
       
   383         {
       
   384         buffer = CBufFlat::NewL( 50 );
       
   385         CleanupStack::PushL( buffer );
       
   386         for( TInt i = 0;i < count;i++ )
       
   387             {
       
   388             TPtrC8 name = TPtrC8( KCacheEntryArray[iPropertyList[i]->Name()] );
       
   389             TPtrC8 value = iPropertyList[i]->DesValue8();
       
   390             buffer->InsertL( position, name );
       
   391             position = position + name.Length();
       
   392             buffer->InsertL( position, KValuePropertySeparator );
       
   393             position = position + KValuePropertySeparator().Length();
       
   394             buffer->InsertL( position, value );
       
   395             position = position + value.Length();
       
   396             buffer->InsertL( position, KValueFieldSeparator );
       
   397             position = position + KValueFieldSeparator().Length();
       
   398             }
       
   399         buffer->InsertL( position, KIndexFileEndOfLine );
       
   400         TPtr8 pointer( buffer->Ptr( 0 ) );
       
   401         entryString = HBufC8::NewL( pointer.Length() );
       
   402         entryString->Des().Copy( pointer );
       
   403         CleanupStack::PopAndDestroy();  //buffer
       
   404         CleanupStack::PushL( entryString );
       
   405         }
       
   406     return entryString;
       
   407     }
       
   408 
       
   409 
       
   410