profilesservices/ProfileEngine/EngSrc/CProfileTiming.cpp
changeset 0 8c5d936e5675
child 8 f62c3a3d66b8
equal deleted inserted replaced
-1:000000000000 0:8c5d936e5675
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Implementation of CProfileTiming.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "CProfileTiming.h"
       
    22 #include <ProfilesVariant.hrh>
       
    23 #include <f32file.h> // TParse
       
    24 #include <data_caging_path_literals.hrh>
       
    25 #include "MProfilesLocalFeatures.h"
       
    26 #include "ProfileEngineConstants.h"
       
    27 #include "ProfileEnginePrivateCRKeys.h"
       
    28 #include "ProfilesDebug.h"
       
    29 
       
    30 // LOCAL CONSTANTS AND MACROS
       
    31 namespace
       
    32     {
       
    33     _LIT( KProfileScheduledTask, "z:PROFILESCHEDULEEVENT.exe" );
       
    34     }
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CProfileTiming::CProfileTiming
       
    40 // C++ default constructor can NOT contain any code, that
       
    41 // might leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CProfileTiming::CProfileTiming( CRepository& aProfileRepository,
       
    45                                 MProfilesLocalFeatures& aFeatures )
       
    46     : iRepository( aProfileRepository ), iFeatures( aFeatures )
       
    47     {
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CProfileTiming::ConstructL
       
    52 // Symbian 2nd phase constructor can leave.
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 void CProfileTiming::ConstructL()
       
    56     {
       
    57     if( iFeatures.IsFeatureSupported( KProEngFeatureIdTimedProfiles ) )
       
    58         {
       
    59         iEntryList = new ( ELeave ) CArrayFixFlat<TScheduleEntryInfo2>( 1 );
       
    60 
       
    61         iTaskData = KNullDesC().AllocL();
       
    62 
       
    63         TParse* tp = new ( ELeave ) TParse();
       
    64         tp->Set( KProfileScheduledTask, &KDC_SHARED_LIB_DIR, NULL );
       
    65         iTaskDllPath.Copy( tp->FullName() );
       
    66         delete tp;
       
    67         }
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CProfileTiming::NewL
       
    72 // Two-phased constructor.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CProfileTiming* CProfileTiming::NewL( CRepository& aProfileRepository,
       
    76                                       MProfilesLocalFeatures& aFeatures )
       
    77     {
       
    78     CProfileTiming* self = 
       
    79         new( ELeave ) CProfileTiming( aProfileRepository, aFeatures );
       
    80 
       
    81     CleanupStack::PushL( self );
       
    82     self->ConstructL();
       
    83     CleanupStack::Pop();
       
    84 
       
    85     return self;
       
    86     }
       
    87 
       
    88 
       
    89 // Destructor
       
    90 CProfileTiming::~CProfileTiming()
       
    91     {
       
    92     if( iFeatures.IsFeatureSupported( KProEngFeatureIdTimedProfiles ) )
       
    93         {
       
    94         iScheduler.Close();
       
    95 
       
    96         if( iEntryList )
       
    97             {
       
    98             iEntryList->Reset();
       
    99             }
       
   100         delete iEntryList;
       
   101         delete iTaskData;
       
   102         }
       
   103     }
       
   104 
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CProfileTiming::SetTimedProfileL
       
   108 // (other items were commented in a header).
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 void CProfileTiming::SetTimedProfileL( TInt aPreviousId, TTime aTime )
       
   112     {
       
   113     PRODEBUG1( "CProfileTiming:SetTimedProfile( %d )", aPreviousId );
       
   114     if( !iFeatures.IsFeatureSupported( KProEngFeatureIdTimedProfiles ) )
       
   115         {
       
   116         return;
       
   117         }
       
   118 
       
   119     ConnectToSchedulerL();
       
   120 
       
   121     const TTimeIntervalMinutes KProfileMaxTiming( 1440 );
       
   122     const TInt KProfileOnce( 1 );
       
   123     const TInt KProfileDontCare( 0 );
       
   124     _LIT( KProfileTaskName, "TimedProfileTask" );
       
   125     _LIT( KProfileSchedulerItemRefName, "ProfileItemRef" );
       
   126     iEntryList->Reset();
       
   127 
       
   128     // iStartTime contains the time when the timed profile deactivates and the
       
   129     // validity period makes sure that the timing will go off in the nearest
       
   130     // 24 hours.
       
   131     TTsTime ttsTime;
       
   132     ttsTime.SetLocalTime( aTime );
       
   133     TScheduleEntryInfo2 entryInfo( ttsTime, EHourly, KProfileOnce, KProfileMaxTiming );
       
   134     iEntryList->AppendL( entryInfo );
       
   135 
       
   136     TName name( KProfileTaskName() );
       
   137     TTaskInfo taskInfo( KProfileDontCare, name, CActive::EPriorityStandard,
       
   138                         KProfileOnce ) ;
       
   139 
       
   140     TName name2( KProfileSchedulerItemRefName() );
       
   141     iSchedulerItemRef.iName = name2;
       
   142 
       
   143     PRODEBUG( "CProfileTiming:SetTimedProfile before CreatePers" );
       
   144     TInt error( iScheduler.CreatePersistentSchedule( iSchedulerItemRef,
       
   145                                                      *iEntryList ) );
       
   146     PRODEBUG1( "CProfileTiming:SetTimedProfile after CreatePers, err=%d )", error );
       
   147     if( !error )
       
   148         {
       
   149         error = iScheduler.ScheduleTask( taskInfo, *iTaskData,
       
   150                                          iSchedulerItemRef.iHandle );
       
   151         PRODEBUG1( "CProfileTiming:SetTimedProfile after ScheduleTask, err=%d )", error );
       
   152         if( error )
       
   153             {
       
   154             iScheduler.DeleteSchedule( iSchedulerItemRef.iHandle );
       
   155             }
       
   156         else
       
   157             {
       
   158             iTaskId = taskInfo.iTaskId;
       
   159             PRODEBUG( "CProfileTiming:SetTimedProfile before SaveSchedulingData" );
       
   160             SaveSchedulingData( aPreviousId, iSchedulerItemRef.iHandle,
       
   161                                 iTaskId );
       
   162             PRODEBUG( "CProfileTiming:SetTimedProfile after SaveSchedulingData" );
       
   163             }
       
   164         }
       
   165 
       
   166     PRODEBUG1( "CProfileTiming:SetTimedProfile before return:%d", error );
       
   167     User::LeaveIfError( error );
       
   168     }
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CProfileTiming::RollbackTimedProfile
       
   172 // (other items were commented in a header).
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 void CProfileTiming::RollbackTimedProfile()
       
   176     {
       
   177     PRODEBUG( "CProfileTiming:RollbackTimedProfile" );
       
   178     if( !iFeatures.IsFeatureSupported( KProEngFeatureIdTimedProfiles ) )
       
   179         {
       
   180         return;
       
   181         }
       
   182 
       
   183     // Ignore errors here, there's already an error occurred because  this
       
   184     // method is called
       
   185     iScheduler.DeleteTask( iTaskId );
       
   186     iScheduler.DeleteSchedule( iSchedulerItemRef.iHandle );
       
   187     PRODEBUG( "CProfileTiming:RollbackTimedProfile end" );
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CProfileTiming::CancelTimedProfile
       
   192 // (other items were commented in a header).
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 TInt CProfileTiming::CancelTimedProfileL()
       
   196     {
       
   197     PRODEBUG( "CProfileTiming:CancelTimedProfileL" );
       
   198     if( !iFeatures.IsFeatureSupported( KProEngFeatureIdTimedProfiles ) )
       
   199         {
       
   200         return KErrNone;
       
   201         }
       
   202 
       
   203     TInt previousId( KErrNotFound );
       
   204     TInt taskId( KErrNotFound );
       
   205     GetSchedulingDataL( previousId, iSchedulerItemRef.iHandle, taskId );
       
   206     PRODEBUG1( "CProfileTiming:CancelTimedProfileL previousid=%d", previousId );
       
   207     if( previousId != KErrNotFound )
       
   208         {
       
   209         ConnectToSchedulerL();
       
   210         TInt error( iScheduler.DeleteTask( taskId ) );
       
   211         PRODEBUG1( "CProfileTiming:CancelTimedProfileL after deletetask err=%d", error );
       
   212         if( ( error != KErrNone ) && ( error != KErrNotFound ) )
       
   213             {
       
   214             User::Leave( error );
       
   215             }
       
   216 
       
   217         error = iScheduler.DeleteSchedule( iSchedulerItemRef.iHandle );
       
   218         PRODEBUG1( "CProfileTiming:CancelTimedProfileL after deletesched err=%d", error );
       
   219         if( ( error != KErrNone ) && ( error != KErrNotFound ) )
       
   220             {
       
   221             User::Leave( error );
       
   222             }
       
   223                             
       
   224         SaveSchedulingData(); // resets timing data in cenrep
       
   225         PRODEBUG( "CProfileTiming:CancelTimedProfileL after SaveSchedulingData" );
       
   226         }
       
   227     PRODEBUG( "CProfileTiming:CancelTimedProfileL end" );
       
   228     return previousId;
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CProfileTiming::IsTimingActiveL
       
   233 // (other items were commented in a header).
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 TBool CProfileTiming::IsTimingActiveL()
       
   237     {
       
   238     PRODEBUG( "CProfileTiming:IsTimingActiveL" );
       
   239     TBool result( EFalse );
       
   240 
       
   241     if( iFeatures.IsFeatureSupported( KProEngFeatureIdTimedProfiles ) )
       
   242         {
       
   243         User::LeaveIfError( iRepository.Get( KProEngSchedulerHandleId,
       
   244                             iSchedulerItemRef.iHandle ) );
       
   245         result = ( iSchedulerItemRef.iHandle != KErrNotFound );
       
   246         }
       
   247 
       
   248     PRODEBUG1( "CProfileTiming:IsTimingActiveL returning:%d", result );
       
   249     return result;
       
   250     }
       
   251 
       
   252 // -----------------------------------------------------------------------------
       
   253 // CProfileTiming::SaveSchedulingData
       
   254 // The errors are ignored inside this method, because this class is used only
       
   255 // inside CentralRepository transactions.
       
   256 // (other items were commented in a header).
       
   257 // -----------------------------------------------------------------------------
       
   258 //
       
   259 void CProfileTiming::SaveSchedulingData( TInt aPreviousId, TInt aHandleId,
       
   260                                          TInt aTaskId )
       
   261     {
       
   262     iRepository.Set( KProEngSchedulerHandleId, aHandleId );
       
   263     iRepository.Set( KProEngPreviousActiveId, aPreviousId );
       
   264     iRepository.Set( KProEngSchedulerTaskId, aTaskId );
       
   265     }
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // CProfileTiming::GetSchedulingData
       
   269 // (other items were commented in a header).
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void CProfileTiming::GetSchedulingDataL( TInt& aPreviousId, TInt& aHandleId,
       
   273                                          TInt& aTaskId )
       
   274     {
       
   275     PRODEBUG( "CProfileTiming:GetSchedulingDataL" );
       
   276     User::LeaveIfError(
       
   277             iRepository.Get( KProEngPreviousActiveId, aPreviousId ) );
       
   278     User::LeaveIfError(
       
   279             iRepository.Get( KProEngSchedulerHandleId, aHandleId ) );
       
   280     User::LeaveIfError( iRepository.Get( KProEngSchedulerTaskId, aTaskId ) );
       
   281     PRODEBUG( "CProfileTiming:GetSchedulingDataL end" );
       
   282     }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CProfileTiming::ConnectToSchedulerL
       
   286 // (other items were commented in a header).
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CProfileTiming::ConnectToSchedulerL()
       
   290     {
       
   291     // Feature support is not tested here because it must have been done already
       
   292     // in the calling method.
       
   293     if( !iConnected )
       
   294         {
       
   295         User::LeaveIfError( iScheduler.Connect() );
       
   296 
       
   297         User::LeaveIfError( iScheduler.Register( iTaskDllPath,
       
   298                                                  CActive::EPriorityStandard ) );
       
   299         iConnected = ETrue;
       
   300         }
       
   301     }
       
   302 
       
   303 //  End of File
       
   304