upnpharvester/cdssync/cdssynclib/src/cdssyncsqlao.cpp
branchIOP_Improvements
changeset 40 08b5eae9f9ff
parent 39 6369bfd1b60d
child 41 b4d83ea1d6e2
equal deleted inserted replaced
39:6369bfd1b60d 40:08b5eae9f9ff
     1 /*
       
     2 * Copyright (c) 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:      Cds Sync Active Object
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 #include "mcmsqlmain.h"
       
    24 #include "cmsqlbaseitem.h"
       
    25 #include "cmsqlgenericitem.h"
       
    26 #include "cdssyncsqlao.h"
       
    27 #include "cdssyncimplsql.h"
       
    28 #include "msdebug.h"
       
    29 
       
    30 // CONSTANTS
       
    31 const TInt KCdsSyncMaxImageItemCount = 15000;
       
    32 const TInt KCdsSyncMaxVideoItemCount = 5000;
       
    33 const TInt KCdsSyncMaxItemCount      = 40000;
       
    34 
       
    35 // --------------------------------------------------------------------------
       
    36 // CCdsSyncSqlAo::NewL
       
    37 // --------------------------------------------------------------------------
       
    38 //
       
    39 CCdsSyncSqlAo* CCdsSyncSqlAo::NewL( 
       
    40     MCmSqlMain& aSqlDb,
       
    41     CCdsSyncImpl& aSync,
       
    42     RPointerArray<CCmSqlGenericItem>& aItemsToAdd,
       
    43     RPointerArray<CCmSqlBaseItem>& aItemsToDelete,
       
    44     TInt aAddGranularity )
       
    45     {    
       
    46     CCdsSyncSqlAo* self = CCdsSyncSqlAo::NewLC(
       
    47         aSqlDb,
       
    48         aSync,
       
    49         aItemsToAdd,
       
    50         aItemsToDelete,
       
    51         aAddGranularity );
       
    52     CleanupStack::Pop( self );
       
    53     return self;
       
    54     }
       
    55  
       
    56 // --------------------------------------------------------------------------
       
    57 // CCdsSyncSqlAo::NewLC
       
    58 // --------------------------------------------------------------------------
       
    59 //    
       
    60 CCdsSyncSqlAo* CCdsSyncSqlAo::NewLC( 
       
    61     MCmSqlMain& aSqlDb,
       
    62     CCdsSyncImpl& aSync,
       
    63     RPointerArray<CCmSqlGenericItem>& aItemsToAdd,
       
    64     RPointerArray<CCmSqlBaseItem>& aItemsToDelete,
       
    65     TInt aAddGranularity )
       
    66     {    
       
    67     LOG(_L("[Cds Sync]\t CCdsSyncSqlAo::NewLC"));    
       
    68     CCdsSyncSqlAo* self = new (ELeave) CCdsSyncSqlAo( 
       
    69         aSqlDb, aSync, aItemsToAdd, aItemsToDelete, aAddGranularity  
       
    70         ); 
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL(); 
       
    73     return self;
       
    74     }    
       
    75 
       
    76 // --------------------------------------------------------------------------
       
    77 // CCdsSyncSqlAo::~CCdsSyncSqlAo
       
    78 // --------------------------------------------------------------------------
       
    79 CCdsSyncSqlAo::~CCdsSyncSqlAo()
       
    80     {
       
    81     LOG(_L("[Cds Sync]\t CCdsSyncSqlAo::~CCdsSyncSqlAo"));    
       
    82     
       
    83     if ( IsActive() )
       
    84         {
       
    85         Cancel();
       
    86         }
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // CCdsSyncSqlAo::CCdsSyncSqlAo
       
    91 // --------------------------------------------------------------------------
       
    92 CCdsSyncSqlAo::CCdsSyncSqlAo( MCmSqlMain& aSqlDb,
       
    93                               CCdsSyncImpl& aSync,
       
    94                               RPointerArray<CCmSqlGenericItem>& aItemsToAdd,
       
    95                               RPointerArray<CCmSqlBaseItem>& aItemsToDelete,
       
    96                               TInt aAddGranularity ) :
       
    97     CActive( CActive::EPriorityIdle ),
       
    98     iState( ECdsSyncSqlAoIdle ),
       
    99     iSqlDb( aSqlDb ),
       
   100     iSync( aSync ),
       
   101     iItemsToAdd( aItemsToAdd ),
       
   102     iItemsToDelete( aItemsToDelete ),
       
   103     iAddGranularity( aAddGranularity ),    
       
   104     iSourceDataComplete( EFalse )
       
   105     {
       
   106     }
       
   107 
       
   108 // --------------------------------------------------------------------------
       
   109 // CCdsSyncSqlAo::ConstructL
       
   110 // --------------------------------------------------------------------------
       
   111 void CCdsSyncSqlAo::ConstructL() 
       
   112     {
       
   113     LOG(_L("[Cds Sync]\t CCdsSyncSqlAo::ConstructL"));    
       
   114     CActiveScheduler::Add( this );
       
   115     }
       
   116 
       
   117 // --------------------------------------------------------------------------
       
   118 // CCdsSyncSqlAo::NotifyItemsAddedL
       
   119 // --------------------------------------------------------------------------
       
   120 void CCdsSyncSqlAo::NotifyItemsAddedL( TBool aSourceDataComplete )
       
   121     {
       
   122     LOG(_L("[Cds Sync]\t CCdsSyncSqlAo::NotifyItemsAddedL"));
       
   123 
       
   124     // Source data is complete
       
   125     if ( aSourceDataComplete )
       
   126         {
       
   127         iSourceDataComplete = ETrue;
       
   128         }
       
   129 
       
   130     TRACE( Print( _L("[Cds Sync]\t addarray has %d items, srccomplete %d"), 
       
   131         iItemsToAdd.Count(), iSourceDataComplete ));
       
   132     
       
   133     if ( iState == ECdsSyncSqlAoIdle )
       
   134         {
       
   135         if ( iItemsToAdd.Count() >= iAddGranularity || 
       
   136            ( iSourceDataComplete && iItemsToAdd.Count() ) )
       
   137         
       
   138             {
       
   139             // add more items
       
   140             iStatus = KRequestPending;
       
   141             iItemsInAddition = iItemsToAdd.Count();
       
   142 
       
   143         #ifdef _DEBUG
       
   144             iOperationTime.HomeTime();
       
   145         #endif
       
   146 
       
   147             TRACE( Print( _L("[Cds Sync]\t starting to add %d items"),
       
   148                  iItemsInAddition ));
       
   149             iSqlDb.AsyncBatchAdd( iItemsToAdd, iStatus );
       
   150             SetActive();    
       
   151             iState = ECdsSyncSqlAoAdding;
       
   152             }
       
   153         else if ( iSourceDataComplete )
       
   154             {
       
   155             // all done, start deleting
       
   156        #ifdef _DEBUG
       
   157             iOperationTime.HomeTime();
       
   158        #endif
       
   159             iSqlDb.AsyncBatchDelete( iItemsToDelete, iStatus );
       
   160             SetActive();
       
   161             iState = ECdsSyncSqlAoDeleting;
       
   162            }
       
   163         }
       
   164     }
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // CCdsSyncSqlAo::Activate()
       
   168 // ---------------------------------------------------------------------------
       
   169 void CCdsSyncSqlAo::Activate()
       
   170     {
       
   171     SetActive();
       
   172     }
       
   173 
       
   174 // --------------------------------------------------------------------------
       
   175 // CCdsSyncSqlAo::IfOverLimitDeleteMediaItems() 
       
   176 // --------------------------------------------------------------------------
       
   177 void CCdsSyncSqlAo::IfOverLimitDeleteMediaItems() 
       
   178     {
       
   179     LOG(_L("[Cds Sync]\t CCdsSyncSqlAo::IfOverLimitDeleteMediaItems START"));
       
   180     TInt64 musicCount( KErrNone );
       
   181     TInt64 videoCount( KErrNone );
       
   182     TInt64 imageCount( KErrNone );
       
   183     TRAP_IGNORE( iSqlDb.GetMediaCountL( musicCount, ECmAudio ) );
       
   184     TRAP_IGNORE( iSqlDb.GetMediaCountL( videoCount, ECmVideo ) );
       
   185     TRAP_IGNORE( iSqlDb.GetMediaCountL( imageCount, ECmImage ) );
       
   186     TRACE( Print( _L("[Cds Sync]\t Amout of music items %ld"), musicCount ));
       
   187     TRACE( Print( _L("[Cds Sync]\t Amout of image items %ld"), imageCount ));
       
   188     TRACE( Print( _L("[Cds Sync]\t Amout of video items %ld"), videoCount ));
       
   189     if( KCdsSyncMaxImageItemCount < imageCount )
       
   190         {
       
   191         TRAP_IGNORE( iSqlDb.DeleteOldestMediaItemsL( ECmImage, 
       
   192             imageCount - KCdsSyncMaxImageItemCount ) );
       
   193         TRACE( Print( _L("[Cds Sync]\t deleted %ld image items"), 
       
   194             ( imageCount - KCdsSyncMaxImageItemCount )));           
       
   195         TRAP_IGNORE( iSqlDb.GetMediaCountL( imageCount, ECmImage ) );
       
   196         }
       
   197     if( KCdsSyncMaxVideoItemCount < videoCount )
       
   198         {
       
   199         TRAP_IGNORE( iSqlDb.DeleteOldestMediaItemsL( ECmVideo, 
       
   200             videoCount - KCdsSyncMaxVideoItemCount ) );
       
   201         TRACE( Print( _L("[Cds Sync]\t deleted %ld video items"), 
       
   202             ( videoCount - KCdsSyncMaxVideoItemCount )));              
       
   203         TRAP_IGNORE( iSqlDb.GetMediaCountL( videoCount, ECmVideo ) );
       
   204         }
       
   205     if( KCdsSyncMaxItemCount < ( imageCount + videoCount + musicCount ) )
       
   206         {
       
   207         TRAP_IGNORE( iSqlDb.DeleteOldestMediaItemsL( ECmAudio, 
       
   208             ( imageCount + videoCount + musicCount ) - 
       
   209             KCdsSyncMaxItemCount ) );
       
   210         TRACE( Print( _L("[Cds Sync]\t deleted %ld music items"), 
       
   211             ( ( imageCount + videoCount + musicCount ) - 
       
   212             KCdsSyncMaxItemCount )));                     
       
   213         }
       
   214     LOG(_L("[Cds Sync]\t CCdsSyncSqlAo::IfOverLimitDeleteMediaItems END"));
       
   215     }
       
   216     
       
   217 // --------------------------------------------------------------------------
       
   218 // CCdsSyncSqlAo::RunL() 
       
   219 // --------------------------------------------------------------------------
       
   220 void CCdsSyncSqlAo::RunL() 
       
   221     {
       
   222     TRACE( Print( _L("[Cds Sync]\t CCdsSyncSqlAo::RunL, iStatus %d"), 
       
   223         iStatus.Int() ));
       
   224     if ( iState == ECdsSyncSqlAoAdding )
       
   225         {
       
   226         iState = ECdsSyncSqlAoIdle;
       
   227         
       
   228     #ifdef _DEBUG
       
   229             
       
   230         TTime timeAfter;
       
   231         timeAfter.HomeTime();
       
   232         TRACE( Print( 
       
   233           _L("[Cds Sync]\t added %d, took %ld microsec, err %d"),
       
   234           iItemsToAdd.Count(), 
       
   235           timeAfter.MicroSecondsFrom( iOperationTime ).Int64(),
       
   236           iStatus.Int() ));
       
   237         TRACE( Print( _L("[Cds Sync]\t array has %d items"), 
       
   238            iItemsToAdd.Count() ));
       
   239         
       
   240     #endif
       
   241         
       
   242         TInt itemCount( iItemsToAdd.Count() );
       
   243         
       
   244         for ( TInt i = 0; i < itemCount; i++ )
       
   245             {
       
   246             delete iItemsToAdd[ 0 ];
       
   247             iItemsToAdd.Remove( 0 );
       
   248             }
       
   249         
       
   250         // check if there's more to add
       
   251         NotifyItemsAddedL();
       
   252         
       
   253         }
       
   254     else if ( iState == ECdsSyncSqlAoInitializing )
       
   255         {
       
   256         iSync.OperationsCompleteL( KErrNone );
       
   257         }
       
   258     else // ECdsSyncSqlAoDeleting
       
   259         {
       
   260     #ifdef _DEBUG
       
   261     
       
   262         TTime timeAfter;
       
   263         timeAfter.HomeTime();
       
   264         TRACE( Print( 
       
   265           _L("[Cds Sync]\t removed %d, took %ld microsec, err %d"),
       
   266           iItemsToDelete.Count(), 
       
   267           timeAfter.MicroSecondsFrom( iOperationTime ).Int64(),
       
   268           iStatus.Int() ));
       
   269     #endif
       
   270             
       
   271         iItemsToDelete.ResetAndDestroy();
       
   272         LOG(_L("[Cds Sync]\t Checking if too much media..."));
       
   273         IfOverLimitDeleteMediaItems();
       
   274         LOG(_L("[Cds Sync]\t Checked!"));
       
   275         LOG(_L("[Cds Sync]\t Deleting unused propertys"));
       
   276         iSqlDb.DeleteUnusedPropertys( );
       
   277         iState = ECdsSyncSqlAoIdle;
       
   278         iSync.OperationsCompleteL( KErrNone );
       
   279         }
       
   280     }
       
   281 
       
   282 // --------------------------------------------------------------------------
       
   283 // CCdsSyncSqlAo::DoCancel()
       
   284 // --------------------------------------------------------------------------
       
   285 void CCdsSyncSqlAo::DoCancel()
       
   286     {
       
   287     LOG(_L("[Cds Sync]\t CCdsSyncSqlAo::DoCancel"));    
       
   288     }