appinstaller/AppMngr2/src/appmngr2scanner.cpp
changeset 80 9dcba1ee99f7
parent 77 d1838696558c
equal deleted inserted replaced
77:d1838696558c 80:9dcba1ee99f7
     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:   Directory scanner
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "appmngr2scanner.h"            // CAppMngr2Scanner
       
    20 #include "appmngr2scannerdir.h"         // CAppMngr2ScannerDir
       
    21 #include "appmngr2scannerobserver.h"    // MAppMngr2ScannerObserver
       
    22 #include "appmngr2filerecognizer.h"     // CAppMngr2FileRecognizer
       
    23 #include <appmngr2debugutils.h>         // FLOG macros
       
    24 #include <driveinfo.h>                  // DriveInfo
       
    25 
       
    26 
       
    27 // ======== LOCAL FUNCTIONS =========
       
    28 
       
    29 static TBool EqualF( const CAppMngr2ScannerDir& p, const CAppMngr2ScannerDir& q )
       
    30     {
       
    31     return ( p.DirName().CompareF( q.DirName() ) == 0 );
       
    32     }
       
    33 
       
    34 
       
    35 // ======== MEMBER FUNCTIONS ========
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // CAppMngr2Scanner::NewL()
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CAppMngr2Scanner* CAppMngr2Scanner::NewL( MAppMngr2ScannerObserver& aObs )
       
    42     {
       
    43     CAppMngr2Scanner* self = new (ELeave) CAppMngr2Scanner( aObs );
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CAppMngr2Scanner::~CAppMngr2Scanner()
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CAppMngr2Scanner::~CAppMngr2Scanner()
       
    55     {
       
    56     Cancel();
       
    57     iDirs.ResetAndDestroy();
       
    58     delete iRecognizer;
       
    59     iFs.Close();
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CAppMngr2Scanner::DoCancel()
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 void CAppMngr2Scanner::DoCancel()
       
    67     {
       
    68     switch( iState )
       
    69         {
       
    70         case EScanning:
       
    71             iRecognizer->CancelRecognizeFiles();
       
    72             iState = EIdle;
       
    73             break;
       
    74             
       
    75         default:
       
    76             break;
       
    77         }
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // CAppMngr2Scanner::RunL()
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 void CAppMngr2Scanner::RunL()
       
    85     {
       
    86     switch( iState )
       
    87         {
       
    88         case EScanning:
       
    89             HandleScanningResultsL();
       
    90             break;
       
    91 
       
    92         default:
       
    93             break;
       
    94         }
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // CAppMngr2Scanner::RunError()
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 TInt CAppMngr2Scanner::RunError( TInt aError )
       
   102     {
       
   103     FLOG( "Scanning error %d", aError );
       
   104     
       
   105     // RunL() leaved, scanner is not active any more, inform observer
       
   106     iObserver.ScanningComplete();
       
   107     iState = EIdle;
       
   108     return aError;
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CAppMngr2Scanner::DirectoryChangedL()
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CAppMngr2Scanner::DirectoryChangedL( const TDesC& aDirName )
       
   116     {
       
   117     iObserver.DirectoryChangedL( aDirName );
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CAppMngr2Scanner::AddDirectoryL()
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 void CAppMngr2Scanner::AddDirectoryL( const TDesC& aPath )
       
   125     {
       
   126     __ASSERT_ALWAYS( iState == EIdle, User::Leave( KErrInUse ) );
       
   127 
       
   128     // ignore invalid directory names, CAppMngr2ScannerDir checks it
       
   129     CAppMngr2ScannerDir* dir = NULL;
       
   130     TRAPD( err, dir = CAppMngr2ScannerDir::NewL( iFs, aPath, *this ) );
       
   131     if( err == KErrNone )
       
   132         {
       
   133         CleanupStack::PushL( dir );
       
   134         
       
   135         // add to scanning directories, ignore duplicates
       
   136         TIdentityRelation<CAppMngr2ScannerDir> match( EqualF );
       
   137         TInt findResult = iDirs.Find( dir, match ); 
       
   138         if( findResult == KErrNotFound )
       
   139             {
       
   140             FLOG( "Scanner: dir %S added", &( dir->DirName() ) );
       
   141             iDirs.AppendL( dir );
       
   142             CleanupStack::Pop( dir );
       
   143             }
       
   144         else
       
   145             {
       
   146             CleanupStack::PopAndDestroy( dir );
       
   147             }
       
   148         }
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // CAppMngr2Scanner::StartScanningL()
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 void CAppMngr2Scanner::StartScanningL()
       
   156     {
       
   157     FLOG( "CAppMngr2Scanner::StartScanningL()" );
       
   158     
       
   159     // cancel possible previous, still on-going scanning
       
   160     if( IsActive() )
       
   161         {
       
   162         FLOG( "CAppMngr2Scanner::StartScanningL(): previous scan cancelled" );
       
   163         Cancel();
       
   164         iObserver.ScanningComplete();
       
   165         }
       
   166     
       
   167     // find first valid directory name to scan
       
   168     iScanningIndex = -1;
       
   169     NextValidScanningIndex();
       
   170     
       
   171     // start scanning
       
   172     FLOG( "Scanning %d: %S", iScanningIndex, &( iDirs[ iScanningIndex ]->DirName() ) );
       
   173     iDirs[ iScanningIndex ]->StopWatchingChanges();
       
   174     iRecognizer->RecognizeFilesL( iDirs[ iScanningIndex ]->DirName(), iStatus );
       
   175     iState = EScanning;
       
   176     SetActive();
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // CAppMngr2Scanner::CAppMngr2Scanner()
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 CAppMngr2Scanner::CAppMngr2Scanner( MAppMngr2ScannerObserver& aObs ) :
       
   184         CActive( CActive::EPriorityStandard ), iObserver( aObs )
       
   185     {
       
   186     CActiveScheduler::Add( this );
       
   187     }
       
   188 
       
   189 // ---------------------------------------------------------------------------
       
   190 // CAppMngr2Scanner::ConstructL()
       
   191 // ---------------------------------------------------------------------------
       
   192 //
       
   193 void CAppMngr2Scanner::ConstructL()
       
   194     {
       
   195     User::LeaveIfError( iFs.Connect() );
       
   196     iRecognizer = CAppMngr2FileRecognizer::NewL( iFs );
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------------------------
       
   200 // CAppMngr2Scanner::NextValidScanningIndex()
       
   201 // ---------------------------------------------------------------------------
       
   202 //
       
   203 void CAppMngr2Scanner::NextValidScanningIndex()
       
   204     {
       
   205     TInt err;
       
   206     TEntry entry;
       
   207     TInt dirCount = iDirs.Count();
       
   208     
       
   209     iScanningIndex++;
       
   210     while( iScanningIndex < dirCount )
       
   211         {
       
   212         err = iFs.Entry( iDirs[ iScanningIndex ]->DirName(), entry );
       
   213         if( err == KErrNone )
       
   214             {
       
   215             break;
       
   216             }
       
   217         FLOG( "Scanner skips %d: %S, err = %d", iScanningIndex,
       
   218                 &( iDirs[ iScanningIndex ]->DirName() ), err );
       
   219         iScanningIndex++;
       
   220         }
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // CAppMngr2Scanner::HandleScanningResultsL()
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 void CAppMngr2Scanner::HandleScanningResultsL()
       
   228     {
       
   229     FLOG( "Scanning result: %d, for %S, %d files", iStatus.Int(),
       
   230             &( iDirs[ iScanningIndex ]->DirName() ), iRecognizer->Results().Count() );
       
   231 
       
   232     // start monitoring changes in this directory
       
   233     iDirs[ iScanningIndex ]->StartWatchingChanges();
       
   234     
       
   235     // report results if there were no errors
       
   236     if( iStatus.Int() == KErrNone )
       
   237         {
       
   238         TRAP_IGNORE( iObserver.ScanningResultL( iRecognizer->Results() ) );
       
   239         }
       
   240     
       
   241     // get next valid directory name
       
   242     NextValidScanningIndex();
       
   243 
       
   244     // start file recognizer, or notify that all done
       
   245     if( iScanningIndex < iDirs.Count() )
       
   246         {
       
   247         FLOG( "Scanning %d: %S", iScanningIndex,
       
   248                 &( iDirs[ iScanningIndex ]->DirName() ) );
       
   249         iDirs[ iScanningIndex ]->StopWatchingChanges();
       
   250         iRecognizer->RecognizeFilesL( iDirs[ iScanningIndex ]->DirName(), iStatus );
       
   251         SetActive();
       
   252         }
       
   253     else
       
   254         {
       
   255         FLOG( "Scanning complete" );
       
   256         iObserver.ScanningComplete();
       
   257         iState = EIdle;
       
   258         }
       
   259     }
       
   260