|         |      1 /* | 
|         |      2 * Copyright (c) 2006 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:  Wraps task scheduler functionality | 
|         |     15 * | 
|         |     16 */ | 
|         |     17  | 
|         |     18  | 
|         |     19  | 
|         |     20 // INCLUDE FILES | 
|         |     21 #include <apacmdln.h> | 
|         |     22 #include <FileManagerSchDefinitions.h> | 
|         |     23 #include <FileManagerDebug.h> | 
|         |     24 #include <CFileManagerEngine.h> | 
|         |     25 #include <CFileManagerBackupSettings.h> | 
|         |     26 #include <FileManagerPrivateCRKeys.h> | 
|         |     27 #include "CFileManagerTaskScheduler.h" | 
|         |     28  | 
|         |     29  | 
|         |     30 // CONSTANTS | 
|         |     31 const TInt KSchedulerPriority = 32; | 
|         |     32 const TInt KSchedulerGranularity = 1; | 
|         |     33 const TInt KSchedulerRepeat = -1; // Repeat until deleted | 
|         |     34 const TInt KSchedulerTaskId = 0; | 
|         |     35 const TInt KDayNumStringLen = 4; | 
|         |     36 const TInt KTaskInterval = 1; | 
|         |     37 const TInt KTaskValidity = 1; | 
|         |     38 _LIT( KDayNumStr, "%d" ); | 
|         |     39  | 
|         |     40  | 
|         |     41 // ======== MEMBER FUNCTIONS ======== | 
|         |     42  | 
|         |     43 // ---------------------------------------------------------------------------- | 
|         |     44 // CFileManagerTaskScheduler::CFileManagerTaskScheduler | 
|         |     45 // ---------------------------------------------------------------------------- | 
|         |     46 // | 
|         |     47 CFileManagerTaskScheduler::CFileManagerTaskScheduler( | 
|         |     48         CFileManagerEngine& aEngine ) : | 
|         |     49     iEngine( aEngine ), | 
|         |     50     iScheduleHandle( KErrNotFound ) | 
|         |     51     { | 
|         |     52     } | 
|         |     53  | 
|         |     54 // ---------------------------------------------------------------------------- | 
|         |     55 // CFileManagerTaskScheduler::NewL | 
|         |     56 // ---------------------------------------------------------------------------- | 
|         |     57 // | 
|         |     58 CFileManagerTaskScheduler* CFileManagerTaskScheduler::NewL( | 
|         |     59         CFileManagerEngine& aEngine ) | 
|         |     60     { | 
|         |     61     CFileManagerTaskScheduler* self = | 
|         |     62         new( ELeave ) CFileManagerTaskScheduler( aEngine ); | 
|         |     63     CleanupStack::PushL( self ); | 
|         |     64     self->ConstructL(); | 
|         |     65     CleanupStack::Pop( self ); | 
|         |     66     return self; | 
|         |     67     } | 
|         |     68  | 
|         |     69 // ---------------------------------------------------------------------------- | 
|         |     70 // CFileManagerTaskScheduler::ConstructL | 
|         |     71 // ---------------------------------------------------------------------------- | 
|         |     72 //  | 
|         |     73 void CFileManagerTaskScheduler::ConstructL() | 
|         |     74     { | 
|         |     75     FUNC_LOG | 
|         |     76  | 
|         |     77     User::LeaveIfError( iScheduler.Connect() ); | 
|         |     78     TFileName name( KSchBackupStarterExe ); | 
|         |     79     User::LeaveIfError( iScheduler.Register( name, KSchedulerPriority ) ); | 
|         |     80  | 
|         |     81     // Try to get schedule handle of previously made schedule | 
|         |     82     CArrayFixFlat< TSchedulerItemRef >* array =  | 
|         |     83         new( ELeave ) CArrayFixFlat< TSchedulerItemRef >( | 
|         |     84             KSchedulerGranularity ); | 
|         |     85     CleanupStack::PushL( array ); | 
|         |     86     if ( iScheduler.GetScheduleRefsL( *array, EAllSchedules ) == KErrNone ) | 
|         |     87         { | 
|         |     88         if ( array->Count() ) | 
|         |     89             { | 
|         |     90             iScheduleHandle = array->At( 0 ).iHandle; | 
|         |     91             } | 
|         |     92         } | 
|         |     93     CleanupStack::PopAndDestroy( array ); | 
|         |     94     } | 
|         |     95  | 
|         |     96 // ---------------------------------------------------------------------------- | 
|         |     97 // CFileManagerTaskScheduler::~CFileManagerTaskScheduler | 
|         |     98 // ---------------------------------------------------------------------------- | 
|         |     99 //  | 
|         |    100 CFileManagerTaskScheduler::~CFileManagerTaskScheduler() | 
|         |    101     { | 
|         |    102     FUNC_LOG | 
|         |    103      | 
|         |    104     iScheduler.Close(); | 
|         |    105     } | 
|         |    106  | 
|         |    107 // ---------------------------------------------------------------------------- | 
|         |    108 // CFileManagerTaskScheduler::CreateScheduleL | 
|         |    109 // ---------------------------------------------------------------------------- | 
|         |    110 //  | 
|         |    111 void CFileManagerTaskScheduler::CreateScheduleL() | 
|         |    112     { | 
|         |    113     FUNC_LOG | 
|         |    114  | 
|         |    115     CFileManagerBackupSettings& settings( iEngine.BackupSettingsL() ); | 
|         |    116  | 
|         |    117     // Create schedule entry and store schedule handle for later use | 
|         |    118     CArrayFixFlat< TScheduleEntryInfo2 >* array = | 
|         |    119         new ( ELeave ) CArrayFixFlat< | 
|         |    120             TScheduleEntryInfo2 >( KSchedulerGranularity ); | 
|         |    121     CleanupStack::PushL( array ); | 
|         |    122  | 
|         |    123     TTsTime tsTime( settings.Time(), EFalse ); | 
|         |    124     TScheduleEntryInfo2 entry( | 
|         |    125         tsTime, EDaily, KTaskInterval, KTaskValidity ); | 
|         |    126     array->AppendL( entry ); | 
|         |    127  | 
|         |    128     TSchedulerItemRef ref; | 
|         |    129     User::LeaveIfError( iScheduler.CreatePersistentSchedule( ref, *array ) ); | 
|         |    130     CleanupStack::PopAndDestroy( array ); | 
|         |    131     iScheduleHandle = ref.iHandle; | 
|         |    132  | 
|         |    133     // Add weekday to task data if weekly backup | 
|         |    134     HBufC* data = HBufC::NewLC( KDayNumStringLen ); | 
|         |    135     if ( settings.Scheduling() == EFileManagerBackupScheduleWeekly ) | 
|         |    136         { | 
|         |    137         TPtr ptr( data->Des() ); | 
|         |    138         ptr.Format( KDayNumStr, settings.Day() ); | 
|         |    139         } | 
|         |    140  | 
|         |    141     // Create scheduled task entry and enable it | 
|         |    142     TTaskInfo taskInfo; | 
|         |    143     taskInfo.iName = KSchBackupTaskName; | 
|         |    144     taskInfo.iPriority = KSchedulerPriority; | 
|         |    145     taskInfo.iTaskId = KSchedulerTaskId; | 
|         |    146     taskInfo.iRepeat = KSchedulerRepeat; | 
|         |    147     User::LeaveIfError( iScheduler.ScheduleTask( | 
|         |    148         taskInfo, *data, iScheduleHandle ) ); | 
|         |    149     CleanupStack::PopAndDestroy( data ); | 
|         |    150     User::LeaveIfError( iScheduler.EnableSchedule( iScheduleHandle ) ); | 
|         |    151     } | 
|         |    152  | 
|         |    153 // ---------------------------------------------------------------------------- | 
|         |    154 // CFileManagerTaskScheduler::DeleteScheduleL | 
|         |    155 // ---------------------------------------------------------------------------- | 
|         |    156 //  | 
|         |    157 void CFileManagerTaskScheduler::DeleteScheduleL() | 
|         |    158     { | 
|         |    159     if ( iScheduleHandle != KErrNotFound ) | 
|         |    160         { | 
|         |    161         INFO_LOG( "CFileManagerTaskScheduler::DeleteScheduleL-Delete" ) | 
|         |    162         iScheduler.DisableSchedule( iScheduleHandle ); | 
|         |    163         iScheduler.DeleteTask( KSchedulerTaskId ); | 
|         |    164         iScheduler.DeleteSchedule( iScheduleHandle ); | 
|         |    165         iScheduleHandle = KErrNotFound; | 
|         |    166         } | 
|         |    167     } | 
|         |    168  | 
|         |    169 // ---------------------------------------------------------------------------- | 
|         |    170 // CFileManagerTaskScheduler::EnableBackupScheduleL | 
|         |    171 // ---------------------------------------------------------------------------- | 
|         |    172 //  | 
|         |    173 void CFileManagerTaskScheduler::EnableBackupScheduleL( const TBool aEnable ) | 
|         |    174     { | 
|         |    175     // Delete old schedule first | 
|         |    176     DeleteScheduleL(); | 
|         |    177  | 
|         |    178     if ( aEnable ) | 
|         |    179         { | 
|         |    180         CreateScheduleL(); | 
|         |    181         } | 
|         |    182     } | 
|         |    183  | 
|         |    184 //  End of File   |