filemanager/schbkup/src/filemanagerschbackuptask.cpp
branchRCL_3
changeset 21 65326cf895ed
equal deleted inserted replaced
20:491b3ed49290 21:65326cf895ed
       
     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     delete iSystemStateMonitor;
       
   130     Cancel();
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // CFileManagerSchBackupTask::RunL
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 void CFileManagerSchBackupTask::RunL()
       
   138     {
       
   139     StartL();
       
   140     }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CFileManagerSchBackupTask::DoCancel
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void CFileManagerSchBackupTask::DoCancel()
       
   147     {
       
   148     delete iSubscriber;
       
   149     iSubscriber = NULL;
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // CFileManagerSchBackupTask::RunError()
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 TInt CFileManagerSchBackupTask::RunError(
       
   157 #ifdef FILE_MANAGER_ERROR_LOG_ENABLED
       
   158     TInt aError
       
   159 #else // FILE_MANAGER_ERROR_LOG_ENABLED
       
   160     TInt /*aError*/
       
   161 #endif // FILE_MANAGER_ERROR_LOG_ENABLED
       
   162      )
       
   163     {    
       
   164     ERROR_LOG1( "CFileManagerSchBackupTask::RunError()-Error=%d", aError )
       
   165 
       
   166     // Try again
       
   167     Retry();
       
   168 
       
   169     return KErrNone;
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // CFileManagerSchBackupTask::ConstructL
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void CFileManagerSchBackupTask::ConstructL( const CScheduledTask& aTask )
       
   177     {
       
   178     FUNC_LOG
       
   179 
       
   180     INFO_LOG1( "CFileManagerTaskScheduler::ConstructL()-Repeat=%d",
       
   181         aTask.Info().iRepeat )
       
   182 
       
   183     CActiveScheduler::Add( this );
       
   184 
       
   185     // Check task creator
       
   186     if( aTask.SecurityInfo().iSecureId.iId != KFileManagerSID )
       
   187         {
       
   188         User::Leave( KErrPermissionDenied );
       
   189         }
       
   190 
       
   191     // Get pointer to file manager settings
       
   192     iCenRep = CRepository::NewL( KCRUidFileManagerSettings );
       
   193 
       
   194     // Get backup weekday if given from originator
       
   195     const HBufC& data( aTask.Data() );
       
   196     if ( data.Length() )
       
   197         {
       
   198         TLex parse;
       
   199         parse.Assign( data );
       
   200         User::LeaveIfError( parse.Val( iDay ) );
       
   201         }
       
   202     
       
   203     // Get pointer to system state monitor
       
   204     iSystemStateMonitor = CFmSystemStateMonitor::NewL( *this );
       
   205 
       
   206     Retry();
       
   207     }
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // CFileManagerSchBackupTask::CheckBackupRequired
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 TBool CFileManagerSchBackupTask::CheckBackupRequired()
       
   214     {
       
   215     TBool ret( ETrue );
       
   216 
       
   217     // Check schedule type
       
   218     TInt scheduling( 0 );
       
   219     iCenRep->Get( KFileManagerBackupScheduling, scheduling );
       
   220     if ( scheduling == EFileManagerBackupScheduleNone )
       
   221         {
       
   222         // Backup is not required
       
   223         ret = EFalse;
       
   224         }
       
   225 
       
   226     // Check weekday
       
   227     if ( ret && iDay != KErrNotFound )
       
   228         {
       
   229         TTime time;
       
   230         time.HomeTime();
       
   231         if ( time.DayNoInWeek() != iDay )
       
   232             {
       
   233             // Backup is not required today
       
   234             ret = EFalse;
       
   235             }
       
   236         }
       
   237 
       
   238     if ( !ret )
       
   239         {
       
   240         INFO_LOG( "CFileManagerSchBackupTask::CheckBackupRequired()-No" )
       
   241         Exit();
       
   242         }
       
   243 
       
   244     return ret;
       
   245     }
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // CFileManagerSchBackupTask::CheckPhoneStateL
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 TBool CFileManagerSchBackupTask::CheckPhoneStateL()
       
   252     {
       
   253     
       
   254     // Check phone is normal state or not
       
   255     if ( !iSystemStateMonitor->IsSystemStateNormal() )
       
   256        {
       
   257         INFO_LOG( "CFileManagerSchBackupTask::CheckPhoneStateL()-System State is not normal" )
       
   258         iSystemStateMonitor->StartMonitor( ESsmNormal );
       
   259         return EFalse;
       
   260        }
       
   261     
       
   262     // Check call state
       
   263     TInt callState( 0 );
       
   264     RProperty::Get(
       
   265         KPSUidCtsyCallInformation, KCTsyCallState, callState ); 
       
   266     if ( callState != EPSCTsyCallStateNone )
       
   267         {
       
   268         INFO_LOG1( "CFileManagerSchBackupTask::CheckPhoneStateL()-callState=%d",
       
   269             callState )
       
   270 
       
   271         // Set subscriber to wait call to finish
       
   272         iSubscriber = CFileManagerSchSubscriber::NewL(
       
   273             *this,
       
   274             KPSUidCtsyCallInformation,
       
   275             KCTsyCallState,
       
   276             CFileManagerSchSubscriber::ESubscribePS,
       
   277             KPhoneStateChangeTimeoutSecs );
       
   278         return EFalse;
       
   279         }
       
   280 
       
   281     // Check usb transfer state
       
   282     TInt usbState( 0 );
       
   283     RProperty::Get(
       
   284         KPSUidUsbWatcher, KUsbWatcherSelectedPersonality, usbState );
       
   285     if ( usbState == KUsbPersonalityIdMS )
       
   286         {
       
   287         INFO_LOG( "CFileManagerSchBackupTask::CheckPhoneStateL()-Usb transfer on" )
       
   288 
       
   289         // Set subscriber to wait usb transfer to finish
       
   290         iSubscriber = CFileManagerSchSubscriber::NewL(
       
   291             *this,
       
   292             KPSUidUsbWatcher,
       
   293             KUsbWatcherSelectedPersonality,
       
   294             CFileManagerSchSubscriber::ESubscribePS,
       
   295             KPhoneStateChangeTimeoutSecs );
       
   296         return EFalse;
       
   297         }
       
   298 
       
   299     // Check synchronization state
       
   300     TInt syncState( 0 );
       
   301     RProperty::Get(
       
   302         KPSUidDataSynchronizationInternalKeys, KDataSyncStatus, syncState );
       
   303     if ( syncState > 0 )
       
   304         {
       
   305         INFO_LOG( "CFileManagerSchBackupTask::CheckPhoneStateL()-Synchronization on" )
       
   306 
       
   307         // Set subscriber to wait usb transfer to finish
       
   308         iSubscriber = CFileManagerSchSubscriber::NewL(
       
   309             *this,
       
   310             KPSUidDataSynchronizationInternalKeys,
       
   311             KDataSyncStatus,
       
   312             CFileManagerSchSubscriber::ESubscribePS,
       
   313             KPhoneStateChangeTimeoutSecs );
       
   314         return EFalse;
       
   315         }
       
   316 
       
   317     // Check backup/restore (e.g. PC Suite initiated) state
       
   318     TInt burState( 0 );
       
   319     RProperty::Get(
       
   320         KUidSystemCategory, KUidBackupRestoreKey, burState );
       
   321 
       
   322     const TBURPartType partType = static_cast< TBURPartType >
       
   323         ( burState & KBURPartTypeMask );
       
   324         
       
   325     if ( partType != EBURUnset && partType != EBURNormal )
       
   326         {
       
   327         INFO_LOG( "CFileManagerSchBackupTask::CheckPhoneStateL()-Backup/restore on" )
       
   328 
       
   329         // Set subscriber to wait backup or restore to finish
       
   330         iSubscriber = CFileManagerSchSubscriber::NewL(
       
   331             *this,
       
   332             KUidSystemCategory,
       
   333             KUidBackupRestoreKey,
       
   334             CFileManagerSchSubscriber::ESubscribePS,
       
   335             KPhoneStateChangeTimeoutSecs );
       
   336         return EFalse;
       
   337         }
       
   338 
       
   339     return ETrue;
       
   340     }
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 // CFileManagerSchBackupTask::StartL
       
   344 // ---------------------------------------------------------------------------
       
   345 //
       
   346 void CFileManagerSchBackupTask::StartL()
       
   347     {
       
   348     FUNC_LOG
       
   349 
       
   350     delete iSubscriber;
       
   351     iSubscriber = NULL;
       
   352 
       
   353     if ( !CheckBackupRequired() )
       
   354         {
       
   355         return;
       
   356         }
       
   357     if ( !CheckPhoneStateL() )
       
   358         {
       
   359         return;
       
   360         }
       
   361 
       
   362     // Set backup starting state
       
   363     iCenRep->Set(
       
   364         KFileManagerLastSchBackupStatus,
       
   365         EFileManagerSchBackupStatusStarting );
       
   366 
       
   367     // Set subscriber to watch backup start success
       
   368     iSubscriber = CFileManagerSchSubscriber::NewL(
       
   369         *this,
       
   370         KCRUidFileManagerSettings,
       
   371         KFileManagerLastSchBackupStatus,
       
   372         CFileManagerSchSubscriber::ESubscribeCR,
       
   373         KStartStateChangeTimeoutSecs );
       
   374 
       
   375     // Start file manager for backup and ensure that it starts properly
       
   376     StartFileManagerL();
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 // CFileManagerSchBackupTask::Exit
       
   381 // ---------------------------------------------------------------------------
       
   382 //
       
   383 void CFileManagerSchBackupTask::Exit()
       
   384     {
       
   385     FUNC_LOG
       
   386 
       
   387     Cancel();
       
   388     CActiveScheduler::Stop();
       
   389     }
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // CFileManagerSchBackupTask::Retry
       
   393 // ---------------------------------------------------------------------------
       
   394 //
       
   395 void CFileManagerSchBackupTask::Retry()
       
   396     {
       
   397     if ( iAttemptsLeft != 0 )
       
   398         {
       
   399         TRequestStatus* status = &iStatus;
       
   400         User::RequestComplete( status, KErrNone );
       
   401         SetActive();
       
   402 
       
   403         if ( iAttemptsLeft > 0 )
       
   404             {
       
   405             --iAttemptsLeft;
       
   406             }
       
   407         }
       
   408     else
       
   409         {
       
   410         Exit();
       
   411         }
       
   412     }
       
   413 
       
   414 // ---------------------------------------------------------------------------
       
   415 // CFileManagerSchBackupTask::StartFileManagerL
       
   416 // ---------------------------------------------------------------------------
       
   417 //
       
   418 void CFileManagerSchBackupTask::StartFileManagerL()
       
   419     {
       
   420     FUNC_LOG
       
   421 
       
   422     RWsSession wsSession;
       
   423     User::LeaveIfError( wsSession.Connect() );
       
   424     CleanupClosePushL( wsSession );
       
   425     // Backup cannot be done from embedded file manager.
       
   426     // Backup operation closes the app where file manager is started and 
       
   427     // the backup operation stops because file manager gets closed as well.
       
   428     TInt wgId( 0 );
       
   429     if ( FindStandaloneAppL( wsSession, KUidFileManager, wgId ) )
       
   430         {
       
   431         // Notify running standalone file manager to start backup
       
   432         HBufC8* param8 = HBufC8::NewLC( KSchBackupTaskName().Length() );
       
   433         TPtr8 paramPtr( param8->Des() );
       
   434         paramPtr.Copy( KSchBackupTaskName );
       
   435         wsSession.SendMessageToWindowGroup(
       
   436             wgId,
       
   437             TUid::Uid( KUidApaMessageSwitchOpenFileValue ),
       
   438             *param8 );
       
   439         CleanupStack::PopAndDestroy( param8 );
       
   440         }
       
   441     else
       
   442         {
       
   443         // Start standalone file manager for backup
       
   444         TApaAppInfo appInfo;
       
   445         RApaLsSession apaLsSession;
       
   446         User::LeaveIfError( apaLsSession.Connect() );
       
   447         CleanupClosePushL( apaLsSession );
       
   448         User::LeaveIfError( apaLsSession.GetAppInfo(
       
   449             appInfo, KUidFileManager ) );
       
   450         CApaCommandLine* apaCmdLine = CApaCommandLine::NewLC();
       
   451         apaCmdLine->SetExecutableNameL( appInfo.iFullName );
       
   452         apaCmdLine->SetCommandL( EApaCommandBackground );
       
   453         TThreadId fmThreadId;
       
   454         User::LeaveIfError( apaLsSession.StartApp(
       
   455             *apaCmdLine, fmThreadId ) );
       
   456         CleanupStack::PopAndDestroy( apaCmdLine );
       
   457         CleanupStack::PopAndDestroy( &apaLsSession );
       
   458         }
       
   459     CleanupStack::PopAndDestroy( &wsSession );
       
   460     }
       
   461 
       
   462 // ---------------------------------------------------------------------------
       
   463 // CFileManagerSchBackupTask::NotifyKeyChangeOrTimeoutL
       
   464 // ---------------------------------------------------------------------------
       
   465 //
       
   466 void CFileManagerSchBackupTask::NotifyKeyChangeOrTimeoutL(
       
   467         const TUid& aCategory,
       
   468         const TUint aKey,
       
   469         const TBool aTimeout )
       
   470     {
       
   471     FUNC_LOG
       
   472 
       
   473     if ( aCategory == KCRUidFileManagerSettings &&
       
   474         aKey == KFileManagerLastSchBackupStatus &&
       
   475         !aTimeout )
       
   476         {
       
   477         // Backup was started, the task starter can be finished
       
   478         Exit();
       
   479         }
       
   480     else
       
   481         {
       
   482         // Backup was not started, try starting it again
       
   483         Retry();
       
   484         }
       
   485     }
       
   486 
       
   487 // ---------------------------------------------------------------------------
       
   488 // CFileManagerSchBackupTask::SystemStateChangedEvent
       
   489 // ---------------------------------------------------------------------------
       
   490 //
       
   491 void CFileManagerSchBackupTask::SystemStateChangedEvent()
       
   492     {
       
   493     FUNC_LOG
       
   494     
       
   495     // Backup was not started, try starting it again
       
   496     Retry();
       
   497     }
       
   498 
       
   499 // End of file