contentstorage/casrv/carunningappmonitor/src/castoragetask.cpp
changeset 116 305818acdca4
parent 112 dbfb5e38438b
child 119 50e220be30d1
equal deleted inserted replaced
112:dbfb5e38438b 116:305818acdca4
     1 /*
       
     2 * Copyright (c) 2009 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: castoragetask.cpp
       
    15 *
       
    16 */
       
    17 #include <apgwgnam.h>
       
    18 #include <s32strm.h>
       
    19 
       
    20 #include "castoragetask.h"
       
    21 #include "cadef.h"
       
    22 #include "cainnerquery.h"
       
    23 #include "cainnerentry.h"
       
    24 #include "castorageproxy.h"
       
    25 #include "carunningtaskhandler.h"
       
    26 #include "caarraycleanup.inl"
       
    27 #include "cadef.h"
       
    28 
       
    29 const TInt KDelayTaskRetry(10000);
       
    30 const TInt KMaxTaskRetry(1);
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // ExecuteL
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 void CCaStorageTask::ExecuteL( MCaRunningTaskHandler& aTaskHandler,
       
    37                                CCaStorageProxy& aStorage,
       
    38                                TUid aUid,
       
    39                                TInt aWgi )
       
    40     {
       
    41     CCaStorageTask* self = 
       
    42         new (ELeave)CCaStorageTask( aTaskHandler, aStorage, aUid, aWgi );
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL();
       
    45     //task instance is registered on task handler. shouldn't be deleted here
       
    46     CleanupStack::Pop( self );
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // ~CCaStorageTask
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CCaStorageTask::~CCaStorageTask()
       
    54     {
       
    55     Cancel();
       
    56     iTimer.Close();
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CCaStorageTask
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CCaStorageTask::CCaStorageTask( MCaRunningTaskHandler& aTaskHandler,
       
    64                                 CCaStorageProxy& aStorage,
       
    65                                 TUid aUid,
       
    66                                 TInt aWgId )
       
    67 :
       
    68 CActive( EPriorityLow ),
       
    69 iTaskHandler( aTaskHandler ),
       
    70 iStorage( aStorage ),
       
    71 iUid( aUid ),
       
    72 iWgId( aWgId )
       
    73     {
       
    74     CActiveScheduler::Add( this );
       
    75     }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // ConstructL
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CCaStorageTask::ConstructL()
       
    82     {
       
    83     User::LeaveIfError( iTimer.CreateLocal() );
       
    84     iStatus = KRequestPending;
       
    85     TRequestStatus* status = &iStatus;
       
    86     User::RequestComplete( status, KErrNone );
       
    87     SetActive();
       
    88     iTaskHandler.RegisterTaskL( this );
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // UnregisterD
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CCaStorageTask::UnregisterD()
       
    96     {
       
    97     iTaskHandler.UnregisterTask( this );
       
    98     delete this;
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // GetEntriesL
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CCaStorageTask::GetEntriesL( RPointerArray<CCaInnerEntry>& aArray )
       
   106     {
       
   107     CCaInnerQuery* allAppQuery = CCaInnerQuery::NewLC();
       
   108     allAppQuery->SetUid( static_cast<TUint>( iUid.iUid ) );
       
   109     iStorage.GetEntriesL( allAppQuery, aArray );
       
   110     CleanupStack::PopAndDestroy( allAppQuery );
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // UpdateEntryL
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 TInt CCaStorageTask::UpdateEntryL()
       
   118     {
       
   119     TInt retVal( KErrNone );
       
   120     RPointerArray<CCaInnerEntry> entries;
       
   121     CleanupResetAndDestroyPushL( entries );
       
   122     GetEntriesL( entries );
       
   123     //update only first found entry. othere are just ignored
       
   124     retVal = (0 < entries.Count()) ? UpdateEntryL( *entries[0] ) : KErrNotFound;
       
   125     CleanupStack::PopAndDestroy( &entries );
       
   126     return retVal;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // UpdateEntryL
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 TInt CCaStorageTask::UpdateEntryL( CCaInnerEntry& aEntry )
       
   134     {
       
   135     if(KErrNotFound == iWgId)
       
   136         {
       
   137         aEntry.SetFlags( aEntry.GetFlags() & ~ERunning );
       
   138         RBuf currValue;//temporary variable required to verify if attr exists
       
   139         CleanupClosePushL( currValue );
       
   140         currValue.CreateL( KCaMaxAttrValueLen );
       
   141         if( aEntry.FindAttribute( KCaAttrWindowGroupId, currValue ) )
       
   142             {
       
   143             aEntry.RemoveAttributeL( KCaAttrWindowGroupId );
       
   144             }
       
   145         CleanupStack::PopAndDestroy( &currValue );
       
   146         }
       
   147     else
       
   148         {
       
   149         aEntry.SetFlags(aEntry.GetFlags() | ERunning);
       
   150         RBuf newValue;
       
   151         CleanupClosePushL(newValue);
       
   152         newValue.CreateL(KCaMaxAttrValueLen);
       
   153         newValue.Num(iWgId);
       
   154         aEntry.AddAttributeL( KCaAttrWindowGroupId, newValue );
       
   155         CleanupStack::PopAndDestroy( &newValue );
       
   156         }
       
   157     iStorage.AddL( &aEntry );
       
   158     return KErrNone;
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // RunL
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void CCaStorageTask::RunL()
       
   166     {
       
   167     User::LeaveIfError( iStatus.Int() );
       
   168     if( KErrNone == UpdateEntryL() ||  KMaxTaskRetry <= iCount++ )
       
   169         {
       
   170         UnregisterD();
       
   171         }
       
   172     else
       
   173         {
       
   174         iTimer.After( iStatus, TTimeIntervalMicroSeconds32( KDelayTaskRetry ) );
       
   175         SetActive();
       
   176         }
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // RunError
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 TInt CCaStorageTask::RunError( TInt /*aError*/ )
       
   184     {
       
   185     UnregisterD();
       
   186     return KErrNone;
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // DoCancel
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CCaStorageTask::DoCancel()
       
   194     {
       
   195     iTimer.Cancel();
       
   196     }