webengine/widgetbackuprestore/Src/WidgetActiveCallback.cpp
changeset 0 dd21522fd290
child 16 a359256acfc6
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 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 the License "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:  Widget's active callback in WidgetBackupRestore.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <abclient.h>
       
    21 #include <bautils.h>
       
    22 
       
    23 #include "WidgetActiveCallback.h"
       
    24 #include "WidgetInstaller.h"
       
    25 
       
    26 //  CONSTANTS
       
    27 _LIT(KWidgetUiPath, "\\private\\10282822\\");
       
    28 _LIT(KWidgetBURTempPath, "WidgetBURTemp\\");
       
    29 _LIT(KWidgetBURDummy, "dummy data for backup");
       
    30 
       
    31 // ======== MEMBER FUNCTIONS ========
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // 
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CWidgetActiveCallback* CWidgetActiveCallback::NewL()
       
    38     {
       
    39     CWidgetActiveCallback* self =
       
    40         new( ELeave ) CWidgetActiveCallback();
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop( self );
       
    44 
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Destructor
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CWidgetActiveCallback::~CWidgetActiveCallback()
       
    53     {
       
    54     delete iPathBUR;
       
    55     iFs.Close();
       
    56 #ifdef _DEBUG
       
    57     iFileLogger.Close();
       
    58 #endif
       
    59    }
       
    60 
       
    61 // ---------------------------------------------------------
       
    62 // CWidgetActiveCallback::CleanupBackupDataL
       
    63 //
       
    64 // Cleaning for passive backup: move private area from widgetBackupRestore back to widgetUI.
       
    65 // ---------------------------------------------------------
       
    66 // 
       
    67 void CWidgetActiveCallback::CleanupBackupDataL()
       
    68     {
       
    69     CFileMan* fileManager = CFileMan::NewL( iFs );
       
    70     CleanupStack::PushL( fileManager );
       
    71 
       
    72     // Move widget data from BUR path back to widgetUI
       
    73     User::LeaveIfError( fileManager->Move( *iPathBUR, KWidgetUiPath, CFileMan::ERecurse ) );
       
    74     // Remove the temp folder for BUR
       
    75     User::LeaveIfError( fileManager->RmDir( *iPathBUR ) );
       
    76 
       
    77     CleanupStack::PopAndDestroy();  // fileMananger
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // Inform that all data has been backed up or restored.
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 void CWidgetActiveCallback::AllSnapshotsSuppliedL()
       
    85     {
       
    86     // Finalize and cleanup.
       
    87     return;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Not supported.
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 void CWidgetActiveCallback::ReceiveSnapshotDataL(
       
    95     TDriveNumber /*aDrive*/, TDesC8& /*aBuffer*/, TBool /*aLastSection*/)
       
    96     {
       
    97     User::Leave( KErrNotSupported );
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // Make a guess about data size.
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 TUint CWidgetActiveCallback::GetExpectedDataSize(
       
   105     TDriveNumber /*aDrive*/)
       
   106     {
       
   107     // we have no idea at this point - we even don't know what is to be
       
   108     // backed up yet
       
   109     return 0;
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // Not supported.
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CWidgetActiveCallback::GetSnapshotDataL(
       
   117     TDriveNumber /*aDrive*/, TPtr8& /*aBuffer*/, TBool& /*aFinished*/)
       
   118     {
       
   119     User::Leave( KErrNotSupported );
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // Initialize for backup.
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CWidgetActiveCallback::InitialiseGetBackupDataL(
       
   127     TDriveNumber /*aDrive*/)
       
   128     {
       
   129     // create pathBUR and put data for passive backup in it
       
   130     PrepareBackupDataL();
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 void CWidgetActiveCallback::GetBackupDataSectionL(
       
   137     TPtr8& aBuffer, TBool& aFinished)
       
   138     {
       
   139     // Make sure that the buffer is empty and starts from the beginning
       
   140     aBuffer.SetLength(0);
       
   141     // Set dummy data
       
   142     aBuffer.Append( KWidgetBURDummy );
       
   143 
       
   144     aFinished = ETrue;
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // Initialize restore.
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void CWidgetActiveCallback::InitialiseRestoreBaseDataL(
       
   152     TDriveNumber /*aDrive*/ )
       
   153     {
       
   154     return;
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // Run state machine for restore. Receive stream from BUR engine and turn it
       
   159 // to file(s).
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 void CWidgetActiveCallback::RestoreBaseDataSectionL(
       
   163     TDesC8& /*aBuffer*/, TBool aFinished )
       
   164     {
       
   165     if( aFinished )
       
   166         {
       
   167         // If the temp folder for BUR exists, install widgets
       
   168         if ( BaflUtils::FolderExists( iFs, *iPathBUR ) )
       
   169             {
       
   170             InstallL();
       
   171             }
       
   172         }
       
   173     }
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // Incremental restoration is not supported.
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CWidgetActiveCallback::InitialiseRestoreIncrementDataL(
       
   180     TDriveNumber /*aDrive*/)
       
   181     {
       
   182     User::Leave( KErrNotSupported );
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // Incremental restoration is not supported.
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 void CWidgetActiveCallback::RestoreIncrementDataSectionL(
       
   190     TDesC8& /*aBuffer*/, TBool /*aFinished*/)
       
   191     {
       
   192     User::Leave( KErrNotSupported );
       
   193     }
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // Called when restore is complete - sets data back to initial state.
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 void CWidgetActiveCallback::RestoreComplete(TDriveNumber /*aDrive*/)
       
   200     {
       
   201     return;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // Tidy up when operation is over.
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CWidgetActiveCallback::TerminateMultiStageOperation()
       
   209     {
       
   210     return;
       
   211     }
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 TUint CWidgetActiveCallback::GetDataChecksum(TDriveNumber /*aDrive*/)
       
   218     {
       
   219     // not required - not implemented
       
   220     return 0;
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // C++ constructor.
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 CWidgetActiveCallback::CWidgetActiveCallback()
       
   228     {
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // 2nd phase constructor.
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 void CWidgetActiveCallback::ConstructL()
       
   236     {
       
   237 #ifdef _DEBUG
       
   238     _LIT(KDir, "WidgetBUR");
       
   239     _LIT(KFile, "WidgetBURCallback.log");
       
   240     TInt err( 0 );
       
   241 
       
   242     err = iFileLogger.Connect();
       
   243     if ( err == KErrNone )
       
   244         {
       
   245         iFileLogger.CreateLog( KDir(), KFile(), EFileLoggingModeOverwrite );
       
   246         iCanLog = ETrue;
       
   247         }
       
   248 #endif
       
   249     // Get the path to the default folder.
       
   250     TFileName  path;
       
   251     User::LeaveIfError( iFs.Connect() );
       
   252     iFs.PrivatePath( path );
       
   253 
       
   254     // Test whether or not the private area is present on C:.
       
   255     // If it is missing, create it.
       
   256     if ( !BaflUtils::FolderExists( iFs, path ) )
       
   257         {
       
   258         User::LeaveIfError( iFs.CreatePrivatePath(EDriveC) );
       
   259         }
       
   260 
       
   261     // The temp path for BUR
       
   262     iPathBUR = HBufC16::NewL( path.Length() + KWidgetBURTempPath().Length() );
       
   263     iPathBUR->Des().Copy( path );
       
   264     iPathBUR->Des().Append( KWidgetBURTempPath );
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // CWidgetActiveCallback::PrepareBackupDataL()
       
   269 //
       
   270 // Preparing for passive backup: move private area from widgetUI to widgetBackupRestore.
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 void CWidgetActiveCallback::PrepareBackupDataL()
       
   274     {
       
   275     User::LeaveIfError( iFs.MkDir( *iPathBUR ) );
       
   276 
       
   277     // Move widget data to BUR path
       
   278     CFileMan* fileManager = CFileMan::NewL( iFs );
       
   279     CleanupStack::PushL( fileManager );
       
   280     User::LeaveIfError( fileManager->Move( KWidgetUiPath, *iPathBUR, CFileMan::ERecurse ) );
       
   281     CleanupStack::PopAndDestroy();  // fileMananger
       
   282     }
       
   283 
       
   284 // ============================================================================
       
   285 // CWidgetActiveCallback::InstallL
       
   286 // Perform installation.
       
   287 // (other items were commented in a header).
       
   288 //
       
   289 // @since 3.2
       
   290 // ============================================================================
       
   291 //
       
   292 void CWidgetActiveCallback::InstallL()
       
   293     {
       
   294     CDir* dir = NULL;
       
   295 
       
   296     if ( iFs.GetDir( *iPathBUR, KEntryAttMatchMask, EDirsFirst, dir ) == KErrNone )
       
   297         {
       
   298         CleanupStack::PushL(dir);
       
   299         
       
   300         CWidgetInstaller* installer = NULL;
       
   301         TRAPD( err , installer = CWidgetInstaller::NewL() );
       
   302         User::LeaveIfError( err );
       
   303         if ( !installer )
       
   304             {
       
   305             User::Leave( KErrGeneral );
       
   306             }
       
   307 
       
   308         for (TInt i=0; i<dir->Count(); i++)
       
   309             {
       
   310             const TEntry& fileEntry = (*dir)[i];
       
   311             
       
   312             // check for directory entries, i.e. "com.yahoo.local.widget.traffic"
       
   313             if (fileEntry.IsDir())
       
   314                 {
       
   315                 // e.g.  restorePath = "\private\[WidgetBackupRestoreUid]\WidgetBURTemp\com.yahoo.local.widget.traffic"
       
   316                 TFileName restorePath( *iPathBUR );
       
   317                 restorePath.Append(fileEntry.iName);
       
   318 
       
   319                 // installer will move the restorePath to widgetUI private area
       
   320                 TRAPD( err, installer->InstallL( restorePath ) );
       
   321                 if ( err )
       
   322                     {
       
   323                     installer->RunError( err );
       
   324                     if ( err == KErrNoMemory || err == KErrDiskFull )
       
   325                         {
       
   326                         User::Leave( err );
       
   327                         }               
       
   328                     }
       
   329                 } // end of if (fileEntry
       
   330             else
       
   331                 {
       
   332                 // only care about dir
       
   333                 break;
       
   334                 }
       
   335             }  // end of for (
       
   336 
       
   337         delete installer;
       
   338         
       
   339         CleanupStack::PopAndDestroy();  // dir
       
   340 
       
   341         // Remove the temp folder for BUR
       
   342         CFileMan* fileManager = CFileMan::NewL( iFs );
       
   343         CleanupStack::PushL( fileManager );
       
   344         User::LeaveIfError( fileManager->RmDir( *iPathBUR ) );
       
   345         CleanupStack::PopAndDestroy();  // fileManager
       
   346         }  // end of if ( iFs.GetDir
       
   347     }
       
   348     
       
   349 // End of file