xdmprotocols/XcapProtocol/XcapCache/Server/src/XcapCacheIndex.cpp
branchRCL_3
changeset 18 fbd2e7cec7ef
parent 0 c8caa15ef882
equal deleted inserted replaced
17:2669f8761a99 18:fbd2e7cec7ef
       
     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:   CXcapCacheIndex
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "ServerDefines.h"
       
    23 #include "XcapCacheIndex.h"
       
    24 #include "XcapCacheServer.h"
       
    25 #include "XcapCacheIndexEntry.h"
       
    26 
       
    27 // ----------------------------------------------------------
       
    28 // CXcapCacheIndex::CXcapCacheIndex
       
    29 // 
       
    30 // ----------------------------------------------------------
       
    31 //
       
    32 CXcapCacheIndex::CXcapCacheIndex()
       
    33     {
       
    34     }
       
    35 
       
    36 // ----------------------------------------------------------
       
    37 // CXcapCacheIndex::NewL
       
    38 // 
       
    39 // ----------------------------------------------------------
       
    40 //
       
    41 CXcapCacheIndex* CXcapCacheIndex::NewL()
       
    42     {
       
    43     CXcapCacheIndex* self = new( ELeave ) CXcapCacheIndex();
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop();
       
    47     return self;
       
    48     }
       
    49 
       
    50 // ----------------------------------------------------------
       
    51 // CXcapCacheIndex::ConstructL
       
    52 // 
       
    53 // ----------------------------------------------------------
       
    54 //
       
    55 void CXcapCacheIndex::ConstructL()
       
    56     {
       
    57     TInt length = 0;   
       
    58     RFile index = InitialiseIndexL();                                    
       
    59     CleanupClosePushL( index );
       
    60     User::LeaveIfError( index.Size( length ) );
       
    61     if( length > 0 )
       
    62         {
       
    63         HBufC8* data = HBufC8::NewLC( length );
       
    64         TPtr8 pointer( data->Des() );
       
    65         User::LeaveIfError( index.Read( pointer ) );
       
    66         ReadEntriesL( pointer );
       
    67         CleanupStack::PopAndDestroy(); //data
       
    68         }
       
    69     CleanupStack::PopAndDestroy(); //index
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------
       
    73 // CXcapCacheIndex::InitialiseIndexL
       
    74 // 
       
    75 // ----------------------------------------------------------
       
    76 //
       
    77 RFile CXcapCacheIndex::InitialiseIndexL()
       
    78     {
       
    79     RFile ret;
       
    80     TUid process = RProcess().Identity();
       
    81     #ifdef _DEBUG
       
    82         CXcapCacheServer::WriteToLog( _L8( "CXcapCacheIndex::InitialiseIndexL() - Process: %x" ), process ) ;
       
    83     #endif
       
    84     TInt error = ret.Open( CXcapCacheServer::FileSession(), KCacheServerIndex,
       
    85                            EFileShareExclusive | EFileRead | EFileWrite );
       
    86     if( error != KErrNone )
       
    87         {
       
    88         if( error == KErrPathNotFound )
       
    89             User::LeaveIfError( CXcapCacheServer::FileSession().MkDirAll( KCacheServerRoot ) );
       
    90         User::LeaveIfError( ret.Create( CXcapCacheServer::FileSession(), KCacheServerIndex,
       
    91                             EFileShareExclusive | EFileRead | EFileWrite ) );
       
    92         }
       
    93     return ret;
       
    94     }
       
    95     
       
    96 // ----------------------------------------------------------
       
    97 // CXcapCacheIndex::~CXcapCacheIndex
       
    98 // 
       
    99 // ----------------------------------------------------------
       
   100 //
       
   101 CXcapCacheIndex::~CXcapCacheIndex()
       
   102     {
       
   103     iEntryList.ResetAndDestroy();
       
   104     iEntryList.Close();
       
   105     }
       
   106 
       
   107 // ----------------------------------------------------------
       
   108 // CXcapCacheIndex::ConstructL
       
   109 // 
       
   110 // ----------------------------------------------------------
       
   111 //
       
   112 TInt CXcapCacheIndex::UpdateIndexL( const CXcapCacheIndexEntry* aEntry )
       
   113     {
       
   114     if( aEntry != NULL )
       
   115         {
       
   116         User::LeaveIfError( iEntryList.Append( aEntry ) );
       
   117         return iEntryList.Find( aEntry );
       
   118         }
       
   119     else return KErrNotFound;
       
   120     }
       
   121 
       
   122 // ----------------------------------------------------------
       
   123 // CXcapCacheIndex::Entry
       
   124 // 
       
   125 // ----------------------------------------------------------
       
   126 //
       
   127 CXcapCacheIndexEntry* CXcapCacheIndex::Entry( TInt aIndex ) const
       
   128     {
       
   129     TInt count = iEntryList.Count();
       
   130     CXcapCacheIndexEntry* entry = NULL;
       
   131     if( count > 0 && aIndex >= 0 && aIndex < count )
       
   132         entry = iEntryList[aIndex];
       
   133     return entry;
       
   134     }
       
   135 
       
   136 // ----------------------------------------------------------
       
   137 // CXcapCacheIndex::EntryCount
       
   138 // 
       
   139 // ----------------------------------------------------------
       
   140 //
       
   141 TInt CXcapCacheIndex::EntryCount() const
       
   142 	{
       
   143 	return iEntryList.Count();
       
   144 	}
       
   145 
       
   146 // ----------------------------------------------------------
       
   147 // CXcapCacheIndex::Compare
       
   148 // 
       
   149 // ----------------------------------------------------------
       
   150 //
       
   151 TInt CXcapCacheIndex::Compare( const CXcapCacheIndexEntry& aFirst,
       
   152                                const CXcapCacheIndexEntry& aSecond )
       
   153     {
       
   154     if( aFirst.LastModified() == aSecond.LastModified() )
       
   155         return 0;
       
   156     else if( aFirst.LastModified() > aSecond.LastModified() )
       
   157         return 1;
       
   158     else return -1;
       
   159     }
       
   160 
       
   161 // ----------------------------------------------------------
       
   162 // CXcapCacheIndex::SortEntries
       
   163 // 
       
   164 // ----------------------------------------------------------
       
   165 //
       
   166 void CXcapCacheIndex::SortEntriesL()
       
   167     {
       
   168     TLinearOrder<CXcapCacheIndexEntry> order( CXcapCacheIndex::Compare );
       
   169     iEntryList.Sort( order );
       
   170     #ifdef _DEBUG
       
   171         TPtrC8 entryDesc;
       
   172         TInt count = iEntryList.Count();
       
   173         CXcapCacheServer::WriteToLog( _L8( "CXcapCacheIndex::SortEntriesL()" ) );
       
   174         CXcapCacheServer::WriteToLog( _L8( "------------------------------------------" ) );
       
   175         CXcapCacheServer::WriteToLog( _L8( " Sorted list:" ) );
       
   176         for( TInt i = 0;i < count;i++ )
       
   177             {
       
   178             HBufC* buffer = NULL;
       
   179             buffer = CXcapCacheServer::DateTimeL( iEntryList[i]->LastModified() );
       
   180             TBuf8<KDateTimeMaxSize> printBuffer( _L8( "  " ) );
       
   181             printBuffer.AppendFormat( _L8( "%d: " ), i );
       
   182             printBuffer.Append( *buffer );
       
   183             CXcapCacheServer::WriteToLog( printBuffer );
       
   184             }
       
   185         CXcapCacheServer::WriteToLog( _L8( "------------------------------------------" ) );
       
   186     #endif
       
   187     }
       
   188 
       
   189 // ----------------------------------------------------------
       
   190 // CXcapCacheIndex::RemoveEntry
       
   191 // 
       
   192 // ----------------------------------------------------------
       
   193 //
       
   194 void CXcapCacheIndex::RemoveEntry( TInt aIndex )
       
   195     {
       
   196     TInt count = iEntryList.Count();
       
   197     if( count > 0 && aIndex >= 0 && aIndex < count )
       
   198         {
       
   199         CXcapCacheIndexEntry* entry = iEntryList[aIndex];
       
   200         iEntryList.Remove( aIndex );
       
   201         delete entry;
       
   202         entry = NULL;
       
   203         }
       
   204     }
       
   205 
       
   206 // ----------------------------------------------------------
       
   207 // CXcapCacheIndex::ReadEntriesL
       
   208 // 
       
   209 // ----------------------------------------------------------
       
   210 //
       
   211 void CXcapCacheIndex::ReadEntriesL( TPtr8& aIndexData )
       
   212     {
       
   213     TBool finished = EFalse;
       
   214     while( !finished && aIndexData.Length() > 0 )
       
   215         {
       
   216         //If we find that there is still more data, but no \r\n
       
   217         //to denote the end of an entry, simply quit. The data
       
   218         //format has most probably been corrupted at some point.
       
   219         TInt entryIndex = aIndexData.FindF( KIndexFileEndOfLine );
       
   220         if( entryIndex > 0 )
       
   221             {
       
   222             TPtr8 entryData = CXcapCacheServer::DescriptorCast( 
       
   223                               aIndexData.Left( entryIndex ) );
       
   224             CXcapCacheIndexEntry* entry = CXcapCacheIndexEntry::NewL( entryData );
       
   225             CleanupStack::PushL( entry );
       
   226             User::LeaveIfError( iEntryList.Append( entry ) );
       
   227             CleanupStack::Pop();
       
   228             aIndexData.Delete( 0, entryIndex + 2 );
       
   229             }
       
   230         else finished = ETrue;
       
   231         }
       
   232     }
       
   233 
       
   234 // ----------------------------------------------------------
       
   235 // CXcapCacheIndex::ConstructL
       
   236 // 
       
   237 // ----------------------------------------------------------
       
   238 //
       
   239 void CXcapCacheIndex::StoreCacheDataL()
       
   240     {
       
   241     RFile index;
       
   242     TInt count = iEntryList.Count();
       
   243     if( count > 0 )
       
   244         {
       
   245         User::LeaveIfError( index.Replace( CXcapCacheServer::FileSession(),
       
   246                             KCacheServerIndex, EFileWrite ) );
       
   247         CleanupClosePushL( index );
       
   248         for( TInt i = 0;i < count;i++ )
       
   249             {
       
   250             CXcapCacheIndexEntry* entry = iEntryList[i];
       
   251             HBufC8* entryData = entry->PrintLC();
       
   252             if( entryData )
       
   253                 {
       
   254                 User::LeaveIfError( index.Write( entryData->Des() ) );
       
   255                 CleanupStack::PopAndDestroy();  //entryData
       
   256                 }
       
   257             }
       
   258         CleanupStack::PopAndDestroy();  //index
       
   259         }
       
   260     else
       
   261         {
       
   262         CFileMan* manager = CFileMan::NewL( CXcapCacheServer::FileSession() );
       
   263         manager->Delete( KCacheServerIndex );
       
   264         delete manager;
       
   265         manager = NULL;
       
   266         }
       
   267     }
       
   268