filesystemuis/memscaneng/serversrc/mseng.cpp
changeset 0 6a9f87576119
child 13 5181328fad28
equal deleted inserted replaced
-1:000000000000 0:6a9f87576119
       
     1 /*
       
     2 * Copyright (c) 2006-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: 
       
    15 *     The actual "engine". 
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 
       
    24 // SYSTEM INCLUDES
       
    25 #include    <mseng.rsg>
       
    26 #include    <bautils.h>
       
    27 
       
    28 // USER INCLUDES
       
    29 #include    "mseng.h"
       
    30 #include    "mseng.hrh"
       
    31 #include    "msengscanner.h"
       
    32 
       
    33 // ========================= MEMBER FUNCTIONS ================================
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // CMseng::CMseng()
       
    37 //
       
    38 // C++ default constructor. Can NOT contain any code, that might leave.
       
    39 // ---------------------------------------------------------------------------
       
    40 
       
    41 
       
    42     
       
    43 CMseng::CMseng( MMsengUIHandler& aUIHandler ) :
       
    44     iUIHandler(aUIHandler),
       
    45     iFreeMemory(0)
       
    46     {
       
    47     }    
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // CMseng::NewL()
       
    51 //
       
    52 // Two-phased constructor.
       
    53 // ---------------------------------------------------------------------------
       
    54 
       
    55 EXPORT_C CMseng* CMseng::NewL(MMsengUIHandler& aUIHandler)
       
    56     {
       
    57     CMseng* self = new (ELeave) CMseng(aUIHandler);
       
    58        
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61     CleanupStack::Pop( self );
       
    62 
       
    63     return self;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CMseng::ConstructL()
       
    68 //
       
    69 // Symbian OS default constructor can leave.
       
    70 // ---------------------------------------------------------------------------
       
    71 
       
    72 void CMseng::ConstructL()
       
    73     {
       
    74     #ifdef __SHOW_RDEBUG_PRINT_
       
    75     RDebug::Print(_L("** CMseng::ConstructL()... starting **"));
       
    76     #endif // __SHOW_RDEBUG_PRINT_
       
    77 
       
    78     // Connect to File Server
       
    79     User::LeaveIfError(iFsSession.Connect());
       
    80 
       
    81     // Open the resource file
       
    82     TParse* fp = new(ELeave) TParse(); 
       
    83     fp->Set(KMsengRscFilePath, &KDC_RESOURCE_FILES_DIR, NULL); 
       
    84     TFileName fileName( fp->FullName() );
       
    85     delete fp;
       
    86 
       
    87 
       
    88     BaflUtils::NearestLanguageFile( iFsSession, fileName );
       
    89     //
       
    90     TEntry entry;
       
    91     User::LeaveIfError( iFsSession.Entry( fileName, entry ) );
       
    92     // if file does not exist, leaves with KErrNotFound
       
    93 
       
    94     iResFile = CResourceFile::NewL( iFsSession, fileName, 0, entry.iSize );
       
    95     
       
    96     iResFile->ConfirmSignatureL();
       
    97     
       
    98 
       
    99     /////////////////////////////////////////////////////////
       
   100     //create data structures and initialize them from resource file
       
   101 
       
   102     TInt index = -1; // index used in for-loops
       
   103     TInt subindex = -1; // index used in for-loops inside another for-loop
       
   104     TInt length = -1; // length of resource array being read
       
   105     TInt sublength = -1; // length of sub-array inside array resource
       
   106 
       
   107     RResourceReader theReader;
       
   108     theReader.OpenLC( iResFile, DATAGROUPNAMEARRAY );
       
   109     
       
   110 
       
   111     //the first WORD contains the number of elements in the resource
       
   112     iNumberOfDataGroups = theReader.ReadInt16L();
       
   113     
       
   114     CleanupStack::PopAndDestroy( &theReader );
       
   115     
       
   116     
       
   117 
       
   118     /////////////////////////////////////////////////////////
       
   119     // Read the resource containing the data needed to create
       
   120     // mapping between data groups and UIDs
       
   121     //
       
   122     theReader.OpenLC( iResFile, DATAGROUPUIDARRAY );  
       
   123    
       
   124     //the first WORD contains the number of elements in the resource
       
   125     length = theReader.ReadInt16L();
       
   126 
       
   127    
       
   128     // Create array with such granularity that reallocation is unnecessary
       
   129     // initialize array to contain null pointers 
       
   130     iDataGroupUidArray = new (ELeave) CArrayPtrFlat<CIntArray>(iNumberOfDataGroups);
       
   131     for(index=0; index<iNumberOfDataGroups; index++)
       
   132         {
       
   133         iDataGroupUidArray->AppendL(NULL);
       
   134         }
       
   135     TInt groupindex; // value from enum TDataGroups
       
   136     // Read the array resource
       
   137     for(index=0; index<length; index++)
       
   138         {
       
   139         // Read one enum TDataGroups value
       
   140         groupindex = theReader.ReadInt8L();
       
   141         
       
   142         // Read the sub-array. First WORD contains array length.
       
   143         sublength = theReader.ReadInt16L();
       
   144         
       
   145         // Create new CUidArray with appropriate granularity
       
   146         // and insert it into the main array
       
   147         CIntArray* subarray = new (ELeave) CIntArray(iNumberOfDataGroups);
       
   148         CleanupStack::PushL(subarray);
       
   149         if( groupindex < iDataGroupUidArray->Count() )
       
   150             {
       
   151             iDataGroupUidArray->At(groupindex) = subarray;
       
   152             }
       
   153         
       
   154         // Read the subarray resource
       
   155         for(subindex=0; subindex<sublength; subindex++)
       
   156             {
       
   157             // uidtype matches one value fron enum TUidTypes
       
   158             TInt uidtype = theReader.ReadInt8L(); 
       
   159             if( groupindex < iDataGroupUidArray->Count() )
       
   160                 {
       
   161                 iDataGroupUidArray->At(groupindex)->InsertL(subindex,uidtype);
       
   162                 }
       
   163             }
       
   164         CleanupStack::Pop( subarray );
       
   165         }
       
   166     CleanupStack::PopAndDestroy( &theReader );
       
   167 
       
   168     /////////////////////////////////////////////////////////
       
   169     // Read the resource containing the data needed to create
       
   170     // mapping between data groups and extensions 
       
   171     //
       
   172     theReader.OpenLC( iResFile, DATAGROUPEXTARRAY );
       
   173 
       
   174     //the first WORD contains the number of elements in the resource
       
   175     length = theReader.ReadInt16L();
       
   176     // Create array with such granularity that reallocation is unnecessary
       
   177     // Initialize it to contain null pointers, since some cells can leave empty
       
   178     iDataGroupExtArray = new (ELeave) CArrayPtrFlat<CIntArray>(iNumberOfDataGroups);
       
   179     for(index=0; index<iNumberOfDataGroups; index++)
       
   180         {
       
   181         iDataGroupExtArray->AppendL(NULL);
       
   182         }
       
   183     // Read the array resource
       
   184     for(index=0; index<length; index++)
       
   185         {
       
   186         // Read one enum TDataGroups value
       
   187         groupindex = theReader.ReadInt8L();
       
   188         // Read the sub-array. First WORD contains array length.
       
   189         sublength = theReader.ReadInt16L();
       
   190         // Create new CIntArray with appropriate granularity
       
   191         // and insert it into the main array
       
   192         CIntArray* subarray = new (ELeave) CIntArray(sublength);
       
   193         CleanupStack::PushL(subarray);
       
   194         if( groupindex < iDataGroupExtArray->Count() )
       
   195             {
       
   196             iDataGroupExtArray->At(groupindex) = subarray;
       
   197             }
       
   198         
       
   199         // Read the subarray resource
       
   200         for(subindex=0; subindex<sublength; subindex++)
       
   201             {
       
   202             // exttype matches one value fron enum TUidTypes
       
   203             TInt exttype = theReader.ReadInt8L();
       
   204             if( groupindex < iDataGroupExtArray->Count() )
       
   205                 {
       
   206                 iDataGroupExtArray->At(groupindex)->InsertL(subindex,exttype);
       
   207                 }
       
   208             }
       
   209         CleanupStack::Pop( subarray );
       
   210         }
       
   211 
       
   212     CleanupStack::PopAndDestroy( &theReader );
       
   213     
       
   214     //instantiate scanner
       
   215     iScanner = new (ELeave) CMsengScanner(iUIHandler, *iResFile);
       
   216     }
       
   217     
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // CMseng::~CMseng()
       
   221 // 
       
   222 // Destructor.
       
   223 // ---------------------------------------------------------------------------
       
   224 
       
   225 EXPORT_C CMseng::~CMseng()
       
   226     {
       
   227 #ifdef __SHOW_RDEBUG_PRINT_
       
   228     RDebug::Print(_L("** CMseng::~CMseng(). Finished. **"));
       
   229 #endif // __SHOW_RDEBUG_PRINT_
       
   230 
       
   231     delete iScanner;
       
   232     
       
   233     // Pointer arrays: elements must be deleted before deleting array
       
   234     if(iDataGroupUidArray)
       
   235         {
       
   236         iDataGroupUidArray->ResetAndDestroy();
       
   237         }
       
   238     delete iDataGroupUidArray;
       
   239     
       
   240     
       
   241     if(iDataGroupExtArray)
       
   242         {
       
   243         iDataGroupExtArray->ResetAndDestroy();
       
   244         }
       
   245     delete iDataGroupExtArray;
       
   246   
       
   247         
       
   248     delete iResFile;
       
   249     
       
   250     iFsSession.Close();       
       
   251     }
       
   252 
       
   253 
       
   254 
       
   255 // ---------------------------------------------------------------------------
       
   256 // CMseng::DataGroupsL()
       
   257 //
       
   258 // Get a descriptor array containing the names of the data groups.
       
   259 // ---------------------------------------------------------------------------
       
   260 
       
   261 EXPORT_C CDesCArray* CMseng::DataGroupsL() const
       
   262     {
       
   263 #ifdef __SHOW_RDEBUG_PRINT_
       
   264     RDebug::Print(_L("CMseng::GetDataGroupsL() called."));
       
   265 #endif // __SHOW_RDEBUG_PRINT_
       
   266 
       
   267 
       
   268     // Create the array for the data group names with appropriate granularity
       
   269     CDesCArray* dataGroupNameArray = new (ELeave) CDesCArrayFlat(iNumberOfDataGroups);
       
   270     CleanupStack::PushL(dataGroupNameArray);
       
   271 
       
   272     // Read the resource containing data group names 
       
   273     // and put them to resultArray
       
   274     
       
   275     RResourceReader theReader;
       
   276     theReader.OpenLC( iResFile, DATAGROUPNAMEARRAY );
       
   277     
       
   278     
       
   279     // The first WORD contains the number of elements in the resource
       
   280     // (actually this is already in iNumberOfDataGroups)
       
   281 
       
   282     TInt length = theReader.ReadInt16L();
       
   283     __ASSERT_DEBUG(iNumberOfDataGroups == length, User::Panic(_L("CMseng::DataGroupsL"), KErrGeneral));
       
   284     
       
   285     // Read the data group names from resource file and insert to array
       
   286     TInt groupindex; // value from enum TDataGroups
       
   287     for(TInt index=0; index<length; index++)
       
   288         {
       
   289         groupindex = theReader.ReadInt8L();
       
   290         TPtrC name = theReader.ReadTPtrCL();
       
   291         // Copy the name to the right place in the array
       
   292         dataGroupNameArray->InsertL(groupindex, name);
       
   293         }
       
   294     CleanupStack::PopAndDestroy( &theReader );
       
   295 
       
   296     // Return the array of data groups
       
   297     CleanupStack::Pop( dataGroupNameArray );
       
   298 
       
   299 #ifdef __SHOW_RDEBUG_PRINT_
       
   300 // print the data group array
       
   301     RDebug::Print(_L("Printing the Data Groups:"));
       
   302     for(TInt k = 0; k < dataGroupNameArray->Count(); k++)
       
   303         {
       
   304         HBufC* groupName = dataGroupNameArray->MdcaPoint(k).AllocL();
       
   305         RDebug::Print( _L("    %d: %S"), k, groupName);
       
   306         delete groupName;
       
   307         }
       
   308 #endif // __SHOW_RDEBUG_PRINT_
       
   309 
       
   310     return dataGroupNameArray;
       
   311     
       
   312     }
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 // CMseng::ScanResultL()
       
   316 //
       
   317 // Returns an array of scan results
       
   318 // ---------------------------------------------------------------------------
       
   319 
       
   320 EXPORT_C CArrayFix<TInt64>* CMseng::ScanResultL() const
       
   321     {
       
   322 #ifdef __SHOW_RDEBUG_PRINT_
       
   323     RDebug::Print(_L("CMseng::ScanResultL() called. Starting to calculate result..."));
       
   324 #endif // __SHOW_RDEBUG_PRINT_
       
   325 
       
   326     // Create the result array (with such granularity that reallocations do not happen)
       
   327     CArrayFix<TInt64>* resultArray = new (ELeave) CArrayFixFlat<TInt64>(iNumberOfDataGroups);
       
   328     CleanupStack::PushL(resultArray);
       
   329 
       
   330     // Get result arrays from scanner 
       
   331     const CArrayFix<TInt64>* extResultArray = iScanner->ExtResults();
       
   332     const CArrayFix<TInt64>* uidResultArray = iScanner->UidResults();
       
   333     const CArrayFix<TInt64>* groupResultArray = iScanner->GroupResults();
       
   334 
       
   335     // Initialize the result array from the array of initial result
       
   336     for (TInt i = 0; i < iNumberOfDataGroups; i++)
       
   337         {
       
   338         if( i < groupResultArray->Count() )
       
   339             {
       
   340             resultArray->AppendL(groupResultArray->At(i));
       
   341             }
       
   342         }
       
   343 
       
   344     //Calculate the results and put them to the array
       
   345     
       
   346     // Find results for each data group
       
   347     for(TInt groupindex = 0; groupindex < iNumberOfDataGroups; groupindex++)
       
   348         {
       
   349         // For one data group, the UIDs belonging to this group are listed in 
       
   350         // iDataGroupExtArray. For each of these UIDs, add the result to the total result.
       
   351 
       
   352         // If the examined data group does not have associated UIDs, 
       
   353         // iDataGroupUidArray->At(groupindex) is a NULL pointer.
       
   354         if(iDataGroupUidArray->At(groupindex))
       
   355             {
       
   356             TInt count = iDataGroupUidArray->At(groupindex)->Count();
       
   357             for(TInt uidindex = 0; uidindex < count; uidindex++)
       
   358                 {
       
   359                 resultArray->At(groupindex) += 
       
   360                     uidResultArray->At( iDataGroupUidArray->At(groupindex)->At(uidindex) );
       
   361                 }
       
   362             }
       
   363 
       
   364         // The extension results are collected in a similar manner
       
   365 
       
   366         // If the examined data group does not have associated UIDs, 
       
   367         // iDataGroupUidArray->At(groupindex) is a NULL pointer
       
   368         if(iDataGroupExtArray->At(groupindex))
       
   369             {
       
   370             TInt count = iDataGroupExtArray->At(groupindex)->Count();
       
   371             for(TInt extindex = 0; extindex < count; extindex++)
       
   372                 {
       
   373                 resultArray->At(groupindex) += 
       
   374                     extResultArray->At( iDataGroupExtArray->At(groupindex)->At(extindex) );
       
   375                 }
       
   376             }
       
   377         }
       
   378     // Calculate "Free memory" and "All device data"
       
   379     TInt64 totalMemory;
       
   380     TInt64 freeMemory;
       
   381     DiskInfoL(totalMemory, freeMemory, iScanner->CurrentDrive());
       
   382 #ifdef __SHOW_RDEBUG_PRINT_
       
   383     RDebug::Print(_L("CMseng::ScanresultL(): iFreeMemory %d, freeMemory %d"), (TUint32)iFreeMemory, (TUint32)freeMemory);
       
   384 #endif
       
   385     // For some reason there is sometimes 16 kB difference in free memory when scanning started
       
   386     // vs. scanning ended (16 kB more at the end of scanning) and latter one is incorrect.
       
   387     // That is why free memory detected in the beginning of scanning taken into account. 
       
   388     if(iFreeMemory)
       
   389         {
       
   390         freeMemory = iFreeMemory;
       
   391         }
       
   392     else
       
   393         {
       
   394         iFreeMemory = freeMemory;
       
   395         }
       
   396     
       
   397     // "Free memory" is the memory currently available
       
   398     resultArray->At(EGroupFreeMemory) = freeMemory;
       
   399     // "All Device Data" is all memory used
       
   400     resultArray->At(EGroupAllDeviceData) = (totalMemory - freeMemory);
       
   401     
       
   402     // Calculate how much files not falling to any predefined category consume
       
   403     TInt64 others( 0 );
       
   404     for( TInt i = EGroupCalendar; i < iNumberOfDataGroups; i++ )
       
   405         {
       
   406         others += resultArray->At( i );
       
   407         }
       
   408     
       
   409     // This should never happen, but just in case check that negative count is not established.
       
   410     if( resultArray->At(EGroupAllDeviceData) - others < 0 )
       
   411         {
       
   412         resultArray->At( EGroupOthers ) = 0;
       
   413         }
       
   414     else
       
   415         {
       
   416         resultArray->At( EGroupOthers ) = resultArray->At(EGroupAllDeviceData) - others;
       
   417         }
       
   418 
       
   419 // write the result array to log file
       
   420 #ifdef __SHOW_RDEBUG_PRINT_
       
   421     RDebug::Print(_L("CMseng::ScanresultL(): current result array -"));
       
   422     // note that the log macros cannot handle TInt64
       
   423     for(TInt k = 0; k < resultArray->Count(); k++)
       
   424         {
       
   425         const TInt KMaxChars = 32;
       
   426         TBuf<KMaxChars> num;
       
   427         num.Num(resultArray->At(k));
       
   428         RDebug::Print(num);
       
   429         }
       
   430 #endif // __SHOW_RDEBUG_PRINT_
       
   431 
       
   432     CleanupStack::Pop( resultArray );
       
   433     return resultArray;
       
   434     }
       
   435 
       
   436 // ---------------------------------------------------------------------------
       
   437 // CMseng::ScanInProgress()
       
   438 //
       
   439 // Return ETrue if there is scanning going on, otherwise EFalse.
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 EXPORT_C TBool CMseng::ScanInProgress() const
       
   443     {
       
   444     if(iScanner) 
       
   445         {
       
   446         return iScanner->HaveActiveScanners();
       
   447         }
       
   448     else
       
   449         {
       
   450         return EFalse;
       
   451         }
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // CMseng::DiskInfoL
       
   456 //
       
   457 // Retrieves information about disk usage.
       
   458 // ---------------------------------------------------------------------------
       
   459 //
       
   460 EXPORT_C void CMseng::DiskInfoL(TInt64& aTotal, TInt64& aFree, const TDriveNumber aVolume) const
       
   461     {
       
   462 
       
   463     TVolumeInfo vinfo;
       
   464     User::LeaveIfError(iFsSession.Volume(vinfo, aVolume));
       
   465     aTotal = TInt64(vinfo.iSize);
       
   466     aFree = TInt64(vinfo.iFree);
       
   467 
       
   468 	}
       
   469 
       
   470 // ---------------------------------------------------------------------------
       
   471 // CMseng::MemInfoL
       
   472 //
       
   473 // Retrieves information about RAM usage.
       
   474 // ---------------------------------------------------------------------------
       
   475 //
       
   476 EXPORT_C void CMseng::MemInfoL(TInt64& aTotal, TInt64& aFree)
       
   477     {
       
   478     TMemoryInfoV1Buf membuf;
       
   479     User::LeaveIfError(UserHal::MemoryInfo(membuf));
       
   480     TMemoryInfoV1 minfo = membuf();
       
   481     aTotal = minfo.iTotalRamInBytes;
       
   482     aFree = minfo.iFreeRamInBytes;
       
   483     }
       
   484 
       
   485 // ---------------------------------------------------------------------------
       
   486 // CMseng::ScanL()
       
   487 //
       
   488 // First scan the specific data files.
       
   489 // Then scan directories that are scanned for the 
       
   490 // size of all files. Then call scanner's ScanL.
       
   491 // ---------------------------------------------------------------------------
       
   492 //
       
   493 EXPORT_C void CMseng::ScanL(TDriveNumber aDrive)
       
   494     {
       
   495     __ASSERT_ALWAYS( (CMseng::IsInternalDrive(iFsSession, aDrive)
       
   496         || CMseng::IsRemovableDrive(iFsSession, aDrive)), User::Leave(KErrNotSupported) );
       
   497 
       
   498     // Scanning started.
       
   499     iUIHandler.StartL();
       
   500     
       
   501     // Start scanning memory, check that not already doing it
       
   502     TInt err = iScanner->ScanL(aDrive, iNumberOfDataGroups, iFsSession);
       
   503     if(err != KErrNone) // can be only KErrNone or KErrInUse
       
   504         {
       
   505         iUIHandler.ErrorL(KErrInUse);
       
   506         }        
       
   507     }
       
   508 
       
   509 // ---------------------------------------------------------------------------
       
   510 // CMseng::Cancel()
       
   511 // 
       
   512 // ---------------------------------------------------------------------------
       
   513 //
       
   514 EXPORT_C void CMseng::Cancel()
       
   515     {
       
   516 #ifdef __SHOW_RDEBUG_PRINT_
       
   517     RDebug::Print(_L("CMseng::Cancel() called. canceling scanning..."));
       
   518 #endif // __SHOW_RDEBUG_PRINT_
       
   519     
       
   520     iScanner->Cancel();
       
   521     }
       
   522 
       
   523 // -----------------------------------------------------------------------------
       
   524 // CMseng::IsInternalDrive
       
   525 // -----------------------------------------------------------------------------
       
   526 //
       
   527 TBool CMseng::IsInternalDrive( RFs& aFs, TInt aDrv )
       
   528     {
       
   529     TDriveInfo drvInfo;
       
   530     if ( aFs.Drive( drvInfo, aDrv ) == KErrNone )
       
   531         {
       
   532          if ( !( drvInfo.iDriveAtt & KDriveAttInternal ) &&
       
   533               drvInfo.iDriveAtt & ( KDriveAttRemovable | KDriveAttRemote ) )
       
   534             {
       
   535             return EFalse;
       
   536             }
       
   537         }
       
   538     else
       
   539         {
       
   540         return EFalse;
       
   541         }
       
   542     return ETrue;
       
   543     }
       
   544 
       
   545 // -----------------------------------------------------------------------------
       
   546 // CMseng::IsRemovableDrive
       
   547 // -----------------------------------------------------------------------------
       
   548 //
       
   549 TBool CMseng::IsRemovableDrive( RFs& aFs, TInt aDrv )
       
   550     {
       
   551     TDriveInfo drvInfo;
       
   552     if ( aFs.Drive( drvInfo, aDrv ) == KErrNone )
       
   553         {
       
   554          if ( !( drvInfo.iDriveAtt & KDriveAttRemovable ) )
       
   555             {
       
   556             return EFalse;
       
   557             }
       
   558         }
       
   559     else
       
   560         {
       
   561         return EFalse;
       
   562         }
       
   563     return ETrue;
       
   564     }
       
   565 
       
   566 
       
   567 //  End of File