filemanager/schbkup/src/filemanagerschbackuptask.cpp
changeset 14 1957042d8c7e
parent 1 d1daf54a55b5
child 16 ada7962b4308
equal deleted inserted replaced
1:d1daf54a55b5 14:1957042d8c7e
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  Handles file manager scheduled backup task start
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32base.h>
       
    22 #include <schtask.h>
       
    23 #include <w32std.h>
       
    24 #include <apgtask.h>
       
    25 #include <apgcli.h>
       
    26 #include <apgwgnam.h>
       
    27 #include <apacmdln.h>
       
    28 #include <centralrepository.h>
       
    29 #include <ctsydomainpskeys.h>
       
    30 #include <UsbWatcherInternalPSKeys.h>
       
    31 #include <usbpersonalityids.h>
       
    32 #include <DataSyncInternalPSKeys.h>
       
    33 #include <connect/sbdefs.h>
       
    34 #include "filemanagerschbackuptask.h"
       
    35 #include "filemanagerschsubscriber.h"
       
    36 #include "FileManagerUID.h"
       
    37 #include "FileManagerDebug.h"
       
    38 #include "FileManagerSchDefinitions.h"
       
    39 #include "FileManagerPrivateCRKeys.h"
       
    40 
       
    41 using namespace conn;
       
    42 
       
    43 // CONSTANTS
       
    44 const TUid KUidFileManager = { KFileManagerUID3 };
       
    45 _LIT_SECURE_ID( KFileManagerSID, KFileManagerUID3 );
       
    46 const TInt KStartStateChangeTimeoutSecs = 30;
       
    47 const TInt KPhoneStateChangeTimeoutSecs = 120;
       
    48 const TInt KNumAttempts = -1; // Try forever
       
    49 
       
    50 
       
    51 // ======== LOCAL FUNCTIONS ========
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // FindStandaloneAppL
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 static TBool FindStandaloneAppL(
       
    58         RWsSession& aWs, const TUid& aUid, TInt& aWgId )
       
    59     {
       
    60     FUNC_LOG
       
    61 
       
    62     aWgId = 0; // Used window group id is always greater than zero
       
    63     RArray< RWsSession::TWindowGroupChainInfo > windowChain;
       
    64     User::LeaveIfError( aWs.WindowGroupList( &windowChain ) );
       
    65     CleanupClosePushL( windowChain );
       
    66     TInt count( windowChain.Count() );
       
    67     for( TInt i( 0 ); i < count; ++i )
       
    68         {
       
    69         const RWsSession::TWindowGroupChainInfo& entry( windowChain[ i ] );
       
    70         CApaWindowGroupName* app = CApaWindowGroupName::NewLC(
       
    71             aWs, entry.iId );
       
    72         TUid appUid( app->AppUid() );
       
    73         CleanupStack::PopAndDestroy( app );
       
    74         // Match the app's UID and the embedded status.
       
    75         // The app is standalone when there is no parent window group.
       
    76         if ( appUid == aUid && entry.iParentId <= 0 )
       
    77             {
       
    78             // Standalone application found
       
    79             aWgId = entry.iId;
       
    80             break;
       
    81             }
       
    82         }
       
    83     CleanupStack::PopAndDestroy( &windowChain );
       
    84     return aWgId > 0;
       
    85     }
       
    86 
       
    87 
       
    88 // ======== MEMBER FUNCTIONS ========
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // CFileManagerSchBackupTask::CFileManagerSchBackupTask
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CFileManagerSchBackupTask::CFileManagerSchBackupTask() :
       
    95         CActive( EPriorityStandard ),
       
    96         iDay( KErrNotFound ),
       
    97         iAttemptsLeft( KNumAttempts )
       
    98     {
       
    99     FUNC_LOG
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // CFileManagerSchBackupTask::NewL
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 CFileManagerSchBackupTask* CFileManagerSchBackupTask::NewL(
       
   107         const CScheduledTask& aTask )
       
   108     {
       
   109     FUNC_LOG
       
   110 
       
   111     CFileManagerSchBackupTask* self =
       
   112         new ( ELeave ) CFileManagerSchBackupTask();
       
   113     CleanupStack::PushL( self );
       
   114     self->ConstructL( aTask );
       
   115     CleanupStack::Pop( self );
       
   116     return self;
       
   117     }
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // CFileManagerSchBackupTask::~CFileManagerSchBackupTask
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 CFileManagerSchBackupTask::~CFileManagerSchBackupTask()
       
   124     {
       
   125     FUNC_LOG
       
   126 
       
   127     delete iSubscriber;
       
   128     delete iCenRep;
       
   129     Cancel();
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // CFileManagerSchBackupTask::RunL
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 void CFileManagerSchBackupTask::RunL()
       
   137     {
       
   138     StartL();
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // CFileManagerSchBackupTask::DoCancel
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 void CFileManagerSchBackupTask::DoCancel()
       
   146     {
       
   147     delete iSubscriber;
       
   148     iSubscriber = NULL;
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // CFileManagerSchBackupTask::RunError()
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 TInt CFileManagerSchBackupTask::RunError(
       
   156 #ifdef FILE_MANAGER_ERROR_LOG_ENABLED
       
   157     TInt aError
       
   158 #else // FILE_MANAGER_ERROR_LOG_ENABLED
       
   159     TInt /*aError*/
       
   160 #endif // FILE_MANAGER_ERROR_LOG_ENABLED
       
   161      )
       
   162     {    
       
   163     ERROR_LOG1( "CFileManagerSchBackupTask::RunError()-Error=%d", aError )
       
   164 
       
   165     // Try again
       
   166     Retry();
       
   167 
       
   168     return KErrNone;
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CFileManagerSchBackupTask::ConstructL
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CFileManagerSchBackupTask::ConstructL( const CScheduledTask& aTask )
       
   176     {
       
   177     FUNC_LOG
       
   178 
       
   179     INFO_LOG1( "CFileManagerTaskScheduler::ConstructL()-Repeat=%d",
       
   180         aTask.Info().iRepeat )
       
   181 
       
   182     CActiveScheduler::Add( this );
       
   183 
       
   184     // Check task creator
       
   185     if( aTask.SecurityInfo().iSecureId.iId != KFileManagerSID )
       
   186         {
       
   187         User::Leave( KErrPermissionDenied );
       
   188         }
       
   189 
       
   190     // Get pointer to file manager settings
       
   191     iCenRep = CRepository::NewL( KCRUidFileManagerSettings );
       
   192 
       
   193     // Get backup weekday if given from originator
       
   194     const HBufC& data( aTask.Data() );
       
   195     if ( data.Length() )
       
   196         {
       
   197         TLex parse;
       
   198         parse.Assign( data );
       
   199         User::LeaveIfError( parse.Val( iDay ) );
       
   200         }
       
   201 
       
   202     Retry();
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // CFileManagerSchBackupTask::CheckBackupRequired
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 TBool CFileManagerSchBackupTask::CheckBackupRequired()
       
   210     {
       
   211     TBool ret( ETrue );
       
   212 
       
   213     // Check schedule type
       
   214     TInt scheduling( 0 );
       
   215     iCenRep->Get( KFileManagerBackupScheduling, scheduling );
       
   216     if ( scheduling == EFileManagerBackupScheduleNone )
       
   217         {
       
   218         // Backup is not required
       
   219         ret = EFalse;
       
   220         }
       
   221 
       
   222     // Check weekday
       
   223     if ( ret && iDay != KErrNotFound )
       
   224         {
       
   225         TTime time;
       
   226         time.HomeTime();
       
   227         if ( time.DayNoInWeek() != iDay )
       
   228             {
       
   229             // Backup is not required today
       
   230             ret = EFalse;
       
   231             }
       
   232         }
       
   233 
       
   234     if ( !ret )
       
   235         {
       
   236         INFO_LOG( "CFileManagerSchBackupTask::CheckBackupRequired()-No" )
       
   237         Exit();
       
   238         }
       
   239 
       
   240     return ret;
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // CFileManagerSchBackupTask::CheckPhoneStateL
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 TBool CFileManagerSchBackupTask::CheckPhoneStateL()
       
   248     {
       
   249     // Check call state
       
   250     TInt callState( 0 );
       
   251     RProperty::Get(
       
   252         KPSUidCtsyCallInformation, KCTsyCallState, callState ); 
       
   253     if ( callState != EPSCTsyCallStateNone )
       
   254         {
       
   255         INFO_LOG1( "CFileManagerSchBackupTask::CheckPhoneStateL()-callState=%d",
       
   256             callState )
       
   257 
       
   258         // Set subscriber to wait call to finish
       
   259         iSubscriber = CFileManagerSchSubscriber::NewL(
       
   260             *this,
       
   261             KPSUidCtsyCallInformation,
       
   262             KCTsyCallState,
       
   263             CFileManagerSchSubscriber::ESubscribePS,
       
   264             KPhoneStateChangeTimeoutSecs );
       
   265         return EFalse;
       
   266         }
       
   267 
       
   268     // Check usb transfer state
       
   269     TInt usbState( 0 );
       
   270     RProperty::Get(
       
   271         KPSUidUsbWatcher, KUsbWatcherSelectedPersonality, usbState );
       
   272     if ( usbState == KUsbPersonalityIdMS )
       
   273         {
       
   274         INFO_LOG( "CFileManagerSchBackupTask::CheckPhoneStateL()-Usb transfer on" )
       
   275 
       
   276         // Set subscriber to wait usb transfer to finish
       
   277         iSubscriber = CFileManagerSchSubscriber::NewL(
       
   278             *this,
       
   279             KPSUidUsbWatcher,
       
   280             KUsbWatcherSelectedPersonality,
       
   281             CFileManagerSchSubscriber::ESubscribePS,
       
   282             KPhoneStateChangeTimeoutSecs );
       
   283         return EFalse;
       
   284         }
       
   285 
       
   286     // Check synchronization state
       
   287     TInt syncState( 0 );
       
   288     RProperty::Get(
       
   289         KPSUidDataSynchronizationInternalKeys, KDataSyncStatus, syncState );
       
   290     if ( syncState > 0 )
       
   291         {
       
   292         INFO_LOG( "CFileManagerSchBackupTask::CheckPhoneStateL()-Synchronization on" )
       
   293 
       
   294         // Set subscriber to wait usb transfer to finish
       
   295         iSubscriber = CFileManagerSchSubscriber::NewL(
       
   296             *this,
       
   297             KPSUidDataSynchronizationInternalKeys,
       
   298             KDataSyncStatus,
       
   299             CFileManagerSchSubscriber::ESubscribePS,
       
   300             KPhoneStateChangeTimeoutSecs );
       
   301         return EFalse;
       
   302         }
       
   303 
       
   304     // Check backup/restore (e.g. PC Suite initiated) state
       
   305     TInt burState( 0 );
       
   306     RProperty::Get(
       
   307         KUidSystemCategory, KUidBackupRestoreKey, burState );
       
   308 
       
   309     const TBURPartType partType = static_cast< TBURPartType >
       
   310         ( burState & KBURPartTypeMask );
       
   311         
       
   312     if ( partType != EBURUnset && partType != EBURNormal )
       
   313         {
       
   314         INFO_LOG( "CFileManagerSchBackupTask::CheckPhoneStateL()-Backup/restore on" )
       
   315 
       
   316         // Set subscriber to wait backup or restore to finish
       
   317         iSubscriber = CFileManagerSchSubscriber::NewL(
       
   318             *this,
       
   319             KUidSystemCategory,
       
   320             KUidBackupRestoreKey,
       
   321             CFileManagerSchSubscriber::ESubscribePS,
       
   322             KPhoneStateChangeTimeoutSecs );
       
   323         return EFalse;
       
   324         }
       
   325 
       
   326     return ETrue;
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // CFileManagerSchBackupTask::StartL
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 void CFileManagerSchBackupTask::StartL()
       
   334     {
       
   335     FUNC_LOG
       
   336 
       
   337     delete iSubscriber;
       
   338     iSubscriber = NULL;
       
   339 
       
   340     if ( !CheckBackupRequired() )
       
   341         {
       
   342         return;
       
   343         }
       
   344     if ( !CheckPhoneStateL() )
       
   345         {
       
   346         return;
       
   347         }
       
   348 
       
   349     // Set backup starting state
       
   350     iCenRep->Set(
       
   351         KFileManagerLastSchBackupStatus,
       
   352         EFileManagerSchBackupStatusStarting );
       
   353 
       
   354     // Set subscriber to watch backup start success
       
   355     iSubscriber = CFileManagerSchSubscriber::NewL(
       
   356         *this,
       
   357         KCRUidFileManagerSettings,
       
   358         KFileManagerLastSchBackupStatus,
       
   359         CFileManagerSchSubscriber::ESubscribeCR,
       
   360         KStartStateChangeTimeoutSecs );
       
   361 
       
   362     // Start file manager for backup and ensure that it starts properly
       
   363     StartFileManagerL();
       
   364     }
       
   365 
       
   366 // ---------------------------------------------------------------------------
       
   367 // CFileManagerSchBackupTask::Exit
       
   368 // ---------------------------------------------------------------------------
       
   369 //
       
   370 void CFileManagerSchBackupTask::Exit()
       
   371     {
       
   372     FUNC_LOG
       
   373 
       
   374     Cancel();
       
   375     CActiveScheduler::Stop();
       
   376     }
       
   377 
       
   378 // ---------------------------------------------------------------------------
       
   379 // CFileManagerSchBackupTask::Retry
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 void CFileManagerSchBackupTask::Retry()
       
   383     {
       
   384     if ( iAttemptsLeft != 0 )
       
   385         {
       
   386         TRequestStatus* status = &iStatus;
       
   387         User::RequestComplete( status, KErrNone );
       
   388         SetActive();
       
   389 
       
   390         if ( iAttemptsLeft > 0 )
       
   391             {
       
   392             --iAttemptsLeft;
       
   393             }
       
   394         }
       
   395     else
       
   396         {
       
   397         Exit();
       
   398         }
       
   399     }
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // CFileManagerSchBackupTask::StartFileManagerL
       
   403 // ---------------------------------------------------------------------------
       
   404 //
       
   405 void CFileManagerSchBackupTask::StartFileManagerL()
       
   406     {
       
   407     FUNC_LOG
       
   408 
       
   409     RWsSession wsSession;
       
   410     User::LeaveIfError( wsSession.Connect() );
       
   411     CleanupClosePushL( wsSession );
       
   412     // Backup cannot be done from embedded file manager.
       
   413     // Backup operation closes the app where file manager is started and 
       
   414     // the backup operation stops because file manager gets closed as well.
       
   415     TInt wgId( 0 );
       
   416     if ( FindStandaloneAppL( wsSession, KUidFileManager, wgId ) )
       
   417         {
       
   418         // Notify running standalone file manager to start backup
       
   419         HBufC8* param8 = HBufC8::NewLC( KSchBackupTaskName().Length() );
       
   420         TPtr8 paramPtr( param8->Des() );
       
   421         paramPtr.Copy( KSchBackupTaskName );
       
   422         wsSession.SendMessageToWindowGroup(
       
   423             wgId,
       
   424             TUid::Uid( KUidApaMessageSwitchOpenFileValue ),
       
   425             *param8 );
       
   426         CleanupStack::PopAndDestroy( param8 );
       
   427         }
       
   428     else
       
   429         {
       
   430         // Start standalone file manager for backup
       
   431         TApaAppInfo appInfo;
       
   432         RApaLsSession apaLsSession;
       
   433         User::LeaveIfError( apaLsSession.Connect() );
       
   434         CleanupClosePushL( apaLsSession );
       
   435         User::LeaveIfError( apaLsSession.GetAppInfo(
       
   436             appInfo, KUidFileManager ) );
       
   437         CApaCommandLine* apaCmdLine = CApaCommandLine::NewLC();
       
   438         apaCmdLine->SetExecutableNameL( appInfo.iFullName );
       
   439         apaCmdLine->SetCommandL( EApaCommandBackground );
       
   440         TThreadId fmThreadId;
       
   441         User::LeaveIfError( apaLsSession.StartApp(
       
   442             *apaCmdLine, fmThreadId ) );
       
   443         CleanupStack::PopAndDestroy( apaCmdLine );
       
   444         CleanupStack::PopAndDestroy( &apaLsSession );
       
   445         }
       
   446     CleanupStack::PopAndDestroy( &wsSession );
       
   447     }
       
   448 
       
   449 // ---------------------------------------------------------------------------
       
   450 // CFileManagerSchBackupTask::NotifyKeyChangeOrTimeoutL
       
   451 // ---------------------------------------------------------------------------
       
   452 //
       
   453 void CFileManagerSchBackupTask::NotifyKeyChangeOrTimeoutL(
       
   454         const TUid& aCategory,
       
   455         const TUint aKey,
       
   456         const TBool aTimeout )
       
   457     {
       
   458     FUNC_LOG
       
   459 
       
   460     if ( aCategory == KCRUidFileManagerSettings &&
       
   461         aKey == KFileManagerLastSchBackupStatus &&
       
   462         !aTimeout )
       
   463         {
       
   464         // Backup was started, the task starter can be finished
       
   465         Exit();
       
   466         }
       
   467     else
       
   468         {
       
   469         // Backup was not started, try starting it again
       
   470         Retry();
       
   471         }
       
   472     }
       
   473 
       
   474 // End of file