contextframework/cfw/src/cfscriptengine/cfpersistentdata.cpp
changeset 0 2e3d3ce01487
child 18 0818dd463d41
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2007-2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  CCFPersistentData class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include <s32file.h>
       
    22 #include <bautils.h>
       
    23 #include <connect/sbdefs.h>
       
    24 
       
    25 #include "cfpersistentdata.h"
       
    26 #include "cfoperationnode.h"
       
    27 #include "cftrace.h"
       
    28 #include "cfpendingpersistencytask.h"
       
    29 #include "cfcommon.h"
       
    30 
       
    31 // ======== MEMBER FUNCTIONS ========
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // CCFPersistentData::CCFPersistentData
       
    35 // C++ default constructor can NOT contain any code, that might leave.
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CCFPersistentData::CCFPersistentData( RFs& aFs,
       
    39     const TUid& aOwner,
       
    40     const TDesC& aName ):
       
    41     CActive( EPriorityStandard ),
       
    42 	iFs( aFs ),
       
    43 	iOwner( aOwner ),
       
    44     iName( aName )
       
    45     {
       
    46     FUNC_LOG;
       
    47 
       
    48     CActiveScheduler::Add( this ); // Add to scheduler
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // CCFPersistentData::ConstructL
       
    53 // Symbian 2nd phase constructor can leave.
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 void CCFPersistentData::ConstructL()
       
    57     {
       
    58     FUNC_LOG;
       
    59     
       
    60     StartListeningL();
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // CCFPersistentData::NewL
       
    65 // Two-phased constructor.
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CCFPersistentData* CCFPersistentData::NewL( RFs& aFs,
       
    69     const TUid& aOwner,
       
    70     const TDesC& aName )
       
    71     {
       
    72     FUNC_LOG;
       
    73 
       
    74     CCFPersistentData* self = NewLC( aFs, aOwner, aName );
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CCFPersistentData::NewLC
       
    81 // Two-phased constructor.
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 CCFPersistentData* CCFPersistentData::NewLC( RFs& aFs,
       
    85     const TUid& aOwner,
       
    86     const TDesC& aName )
       
    87     {
       
    88     FUNC_LOG;
       
    89 
       
    90     CCFPersistentData* self = new( ELeave ) CCFPersistentData( aFs,
       
    91         aOwner, aName );
       
    92     CleanupStack::PushL( self );
       
    93     self->ConstructL();
       
    94     return self;
       
    95     }
       
    96 
       
    97 
       
    98 // Destructor
       
    99 CCFPersistentData::~CCFPersistentData()
       
   100     {
       
   101     FUNC_LOG;
       
   102     
       
   103     Cancel(); // Cancel any request, if outstanding
       
   104     iProperty.Close();
       
   105     iPendingTasks.ResetAndDestroy();
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CCFPersistentData::DoCancel
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CCFPersistentData::DoCancel()
       
   113 	{
       
   114     iProperty.Cancel();
       
   115 	}
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // CCFPersistentData::StartListeningL
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 void CCFPersistentData::StartListeningL()
       
   122 	{
       
   123 	Cancel();							// Cancel any request, just to be sure
       
   124 
       
   125     // Check the key current value
       
   126 	iProperty.Attach(KUidSystemCategory, conn::KUidBackupRestoreKey);
       
   127 	TInt backupStateValue = 0;
       
   128 	iProperty.Get(backupStateValue);
       
   129 	TUint backupState = backupStateValue & conn::KBURPartTypeMask;
       
   130 	if( backupState == conn::EBURNormal || backupState == conn::EBURUnset )
       
   131 		{
       
   132 		iStoreRestoreDenied = EFalse;
       
   133 		}
       
   134 	else
       
   135 		{
       
   136 		iStoreRestoreDenied = ETrue;
       
   137 		}
       
   138 	
       
   139 	iProperty.Subscribe(iStatus);
       
   140 	SetActive();						// Tell scheduler a request is active
       
   141 	}
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CCFPersistentData::RunL
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CCFPersistentData::RunL()
       
   148 	{
       
   149 	TInt backupStateValue = 0;
       
   150 	iProperty.Get(backupStateValue);
       
   151 	TUint backupState = backupStateValue & conn::KBURPartTypeMask;
       
   152 	if( backupState == conn::EBURNormal || backupState == conn::EBURUnset )
       
   153 		{
       
   154 		if( iStoreRestoreDenied )
       
   155 			{
       
   156 			ProcessPendingTasksL();
       
   157 			}
       
   158 		iStoreRestoreDenied = EFalse;
       
   159 		}
       
   160 	else
       
   161 		{
       
   162 		iStoreRestoreDenied = ETrue;
       
   163 		}
       
   164 	
       
   165 	iProperty.Subscribe(iStatus);
       
   166 	SetActive();						// Tell scheduler a request is active
       
   167 	}
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // CCFPersistentData::RunError
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 TInt CCFPersistentData::RunError( TInt aError )
       
   174     {
       
   175     return aError;
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // CCFPersistentData::ProcessPendingTasksL
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 void CCFPersistentData::ProcessPendingTasksL()
       
   183     {
       
   184     FUNC_LOG;
       
   185     
       
   186     for (TInt i = iPendingTasks.Count(); i > 0; i--)
       
   187     	{
       
   188     	CCFPendingPersistencyTask* iTask = iPendingTasks[i];
       
   189     	if ( iTask->Mode() == CCFPendingPersistencyTask::EStore )
       
   190     		{
       
   191     		DoStoreL( iTask->FileName(), iTask->Operation()  );
       
   192     		}
       
   193     	else if ( iTask->Mode() == CCFPendingPersistencyTask::ERestore )
       
   194     		{
       
   195     		DoRestoreL( iTask->FileName(), iTask->Operation()  );
       
   196     		}
       
   197     	}
       
   198     iPendingTasks.ResetAndDestroy();
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CCFPersistentData::RestoreL
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CCFPersistentData::RestoreL( 
       
   206     const TDesC& aFile,
       
   207     CCFOperationNode& aOperation )
       
   208     {
       
   209     FUNC_LOG;
       
   210 
       
   211     if ( !iStoreRestoreDenied )
       
   212     	{
       
   213     	DoRestoreL( aFile, aOperation );
       
   214     	}
       
   215     else
       
   216     	{
       
   217     	// make store task pending
       
   218     	CCFPendingPersistencyTask* task = CCFPendingPersistencyTask::NewLC(
       
   219     			CCFPendingPersistencyTask::ERestore, aFile, aOperation );
       
   220     	User::LeaveIfError( iPendingTasks.Append( task ) );
       
   221     	CleanupStack::Pop( task );
       
   222     	}
       
   223     }
       
   224     
       
   225 // ---------------------------------------------------------------------------
       
   226 // CCFPersistentData::DoRestoreL
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 void CCFPersistentData::DoRestoreL( 
       
   230     const TDesC& aFile,
       
   231     CCFOperationNode& aOperation )
       
   232     {
       
   233     FUNC_LOG;
       
   234 
       
   235     HBufC* streamName = CreateStreamName( aFile );
       
   236     User::LeaveIfNull( streamName );
       
   237     CleanupStack::PushL( streamName );
       
   238     
       
   239     RFileReadStream stream;
       
   240     TInt err = stream.Open( iFs, *streamName, EFileShareReadersOnly | EFileRead );
       
   241     if ( err == KErrNone )
       
   242         {
       
   243         stream.PushL();                             // CLEANUP<< stream
       
   244         aOperation.InternalizeL( stream );
       
   245         stream.Pop();                               // CLEANUP>> stream
       
   246         }
       
   247     else if ( err != KErrNotFound )
       
   248         {
       
   249         ERROR(err, "CCFPersistentData::RestoreL() error");
       
   250         }
       
   251     stream.Close();
       
   252     CleanupStack::PopAndDestroy( streamName );      // CLEANUP>> streamName
       
   253     }
       
   254 
       
   255 // ---------------------------------------------------------------------------
       
   256 // CCFPersistentData::StoreL
       
   257 // ---------------------------------------------------------------------------
       
   258 //
       
   259 void CCFPersistentData::StoreL( 
       
   260     const TDesC& aFile,
       
   261     CCFOperationNode& aOperation )
       
   262     {
       
   263     FUNC_LOG;
       
   264     
       
   265     if ( !iStoreRestoreDenied )
       
   266     	{
       
   267     	DoStoreL( aFile, aOperation );
       
   268     	}
       
   269     else
       
   270     	{
       
   271     	// make store task pending
       
   272     	CCFPendingPersistencyTask* task = CCFPendingPersistencyTask::NewLC(
       
   273     			CCFPendingPersistencyTask::EStore, aFile, aOperation );
       
   274     	User::LeaveIfError( iPendingTasks.Append( task ) );
       
   275     	CleanupStack::Pop( task );
       
   276     	}
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // CCFPersistentData::StoreL
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 void CCFPersistentData::DoStoreL( 
       
   284     const TDesC& aFile,
       
   285     CCFOperationNode& aOperation )
       
   286     {
       
   287     FUNC_LOG;
       
   288 
       
   289     HBufC* streamName = CreateStreamName( aFile );
       
   290     User::LeaveIfNull( streamName );
       
   291     CleanupStack::PushL( streamName );
       
   292     
       
   293     BaflUtils::EnsurePathExistsL( iFs, *streamName );
       
   294     RFileWriteStream stream;
       
   295     TInt err = stream.Replace( iFs,
       
   296             *streamName,
       
   297             EFileShareExclusive | EFileWrite );
       
   298     if ( err == KErrNone )
       
   299         {
       
   300         stream.PushL();
       
   301         aOperation.ExternalizeL( stream );
       
   302         stream.Pop();
       
   303         }
       
   304     else
       
   305         {
       
   306         ERROR(err, "CCFPersistentData::StoreL() error");
       
   307         }
       
   308     stream.Close();
       
   309     CleanupStack::PopAndDestroy( streamName );
       
   310     }
       
   311 
       
   312 // ---------------------------------------------------------------------------
       
   313 // CCFPersistentData::Delete
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 void CCFPersistentData::Delete( const TDesC& aFile )
       
   317     {
       
   318     FUNC_LOG;
       
   319     
       
   320     if ( !iStoreRestoreDenied )
       
   321     	{
       
   322     	DoDelete( aFile );
       
   323     	}
       
   324     else
       
   325     	{
       
   326     	ERROR( KErrInUse, "Cannot delete file because of backup or restore!")
       
   327     	}
       
   328     }
       
   329 
       
   330 // ---------------------------------------------------------------------------
       
   331 // CCFPersistentData::DoDelete
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 void CCFPersistentData::DoDelete( const TDesC& aFile )
       
   335     {
       
   336     FUNC_LOG;
       
   337 
       
   338     HBufC* streamName = CreateStreamName( aFile );
       
   339     if( streamName )
       
   340         {
       
   341         TInt err = BaflUtils::DeleteFile( iFs, *streamName );
       
   342         if( err != KErrNone )
       
   343             {
       
   344             ERROR_1( err, "Failed to delete persistent data [%S]", &aFile );
       
   345             }
       
   346         delete streamName;
       
   347         streamName = NULL;
       
   348         }
       
   349     else
       
   350         {
       
   351         ERROR_1( KErrNoMemory, "Failed to delete persistent data [%S]", &aFile );
       
   352         }    
       
   353     }
       
   354 
       
   355 // ---------------------------------------------------------------------------
       
   356 // CCFPersistentData::CreateStreamName
       
   357 // ---------------------------------------------------------------------------
       
   358 //
       
   359 HBufC* CCFPersistentData::CreateStreamName( const TDesC& aFile )
       
   360     {
       
   361     HBufC* name = HBufC::New( KMaxFileName );
       
   362     if( name )
       
   363         {
       
   364         // Double check that we do not exceed KMaxFileName
       
   365         if( aFile.Length() + KPersistenDataFormat().Length() < KMaxFileName )
       
   366             {
       
   367             // Format name
       
   368             TParsePtrC parse( aFile );
       
   369             TPtrC persistencyFileName( parse.Name() );
       
   370             TPtr namePtr( name->Des() );
       
   371             namePtr.Format( KPersistenDataFormat,
       
   372                 iOwner.iUid, &iName, &persistencyFileName );
       
   373             }
       
   374         }
       
   375     return name;
       
   376     }