harvester/monitorplugins/mmcplugin/src/mmcmonitorplugin.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2006-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:  Monitors MMC insertions and removals.*
       
    15 */
       
    16 
       
    17 
       
    18 #include "mmcmonitorplugin.h"
       
    19 #include "harvesterlog.h"
       
    20 #include "mdsfileserverpluginclient.h"
       
    21 #include "fsutil.h"
       
    22 #include "harvestercenreputil.h"
       
    23 #include <driveinfo.h>
       
    24 
       
    25 #include <e32cmn.h>
       
    26 
       
    27 _LIT( KColon, ":" );
       
    28 
       
    29 // construct/destruct
       
    30 CMMCMonitorPlugin* CMMCMonitorPlugin::NewL()
       
    31     {
       
    32     CMMCMonitorPlugin* self = new (ELeave) CMMCMonitorPlugin();
       
    33     CleanupStack::PushL(self);
       
    34     self->ConstructL();
       
    35     CleanupStack::Pop(self);
       
    36     return self;
       
    37     }
       
    38 
       
    39 void CMMCMonitorPlugin::ConstructL() // second-phase constructor
       
    40     {
       
    41     WRITELOG( "CMMCMonitorPlugin::ConstructL" );
       
    42 
       
    43     iMMCMonitor = CMMCMonitorAO::NewL();
       
    44     iMountTask = CMMCMountTaskAO::NewL();
       
    45     iUsbMonitor = CMMCUsbAO::NewL();
       
    46     iMdEClient = NULL;
       
    47     }
       
    48     
       
    49 CMMCMonitorPlugin::~CMMCMonitorPlugin() // destruct
       
    50     {
       
    51     WRITELOG( "CMMCMonitorPlugin::~CMMCMonitorPlugin" );
       
    52     
       
    53     if (iMMCMonitor)
       
    54     	{
       
    55     	iMMCMonitor->StopMonitoring();
       
    56     	delete iMMCMonitor;
       
    57     	}
       
    58     
       
    59 	if (iUsbMonitor)
       
    60     	{
       
    61     	iUsbMonitor->StopMonitoring();
       
    62     	delete iUsbMonitor;
       
    63     	}
       
    64     
       
    65 	if (iMountTask)
       
    66     	{
       
    67     	delete iMountTask;
       
    68     	}
       
    69 	
       
    70 	delete iMmcScanner;
       
    71 	delete iHddScanner;
       
    72     }
       
    73 
       
    74 TBool CMMCMonitorPlugin::StartMonitoring( MMonitorPluginObserver& aObserver,
       
    75     CMdESession* aMdEClient, CContextEngine* /*aCtxEngine*/, 
       
    76     CHarvesterPluginFactory* aHarvesterPluginFactory )
       
    77     {
       
    78     WRITELOG( "CMMCMonitorPlugin::StartMonitoring" );
       
    79 
       
    80     iObserver = &aObserver;
       
    81     TRAPD( err, iMdEClient = CMdEHarvesterSession::NewL( *aMdEClient ) );
       
    82     if( err != KErrNone )
       
    83     	{
       
    84     	return EFalse;
       
    85     	}
       
    86     
       
    87     iMountTask->SetMonitorObserver( aObserver );
       
    88     iMountTask->SetMdeSession( iMdEClient );
       
    89     iMountTask->SetHarvesterPluginFactory( aHarvesterPluginFactory );
       
    90     
       
    91     // get present media IDs before last shutdown
       
    92     RArray<TMdEMediaInfo> medias;
       
    93     TRAP_IGNORE( iMdEClient->GetPresentMediasL( medias ) );
       
    94 	
       
    95     // update db present status 
       
    96     TRAP( err, StartMonitoringAllMMCsL( medias ) );
       
    97     if ( err != KErrNone )
       
    98         {
       
    99         return EFalse;
       
   100         }
       
   101     
       
   102     TChar driveLetter( 0 );
       
   103 	TBool presentState( EFalse );
       
   104     
       
   105 	TUint32 hdMediaId( 0 );
       
   106     hdMediaId = iMountTask->GetInternalDriveMediaId();
       
   107     TBool alreadyWaited( EFalse );
       
   108     
       
   109     
       
   110     for ( TInt i = 0; i < medias.Count(); i++ )
       
   111     	{
       
   112     	TRAP_IGNORE( iMdEClient->GetMediaL( medias[i].iMediaId, driveLetter, presentState ) );
       
   113     	
       
   114     	if ( presentState && medias[i].iMediaId != hdMediaId )
       
   115     		{
       
   116     		// scan MMC if card was in phone
       
   117     		TRAP_IGNORE( iMmcScanner = CMmcScannerAO::NewL( medias[i].iMediaId, iMdEClient, iObserver,
       
   118     		    				aHarvesterPluginFactory, CActive::EPriorityHigh, alreadyWaited ) );
       
   119     		alreadyWaited = ETrue;
       
   120     		}
       
   121     	}
       
   122  
       
   123     // scan mass storage to catch all chances even if battery dies during operation that should  be catched
       
   124     if( hdMediaId )
       
   125 		{
       
   126 		TBool exists( EFalse );
       
   127 		TRAP_IGNORE( exists= iMdEClient->GetMediaL( hdMediaId, driveLetter, presentState ) );
       
   128 		
       
   129 		if ( exists )
       
   130 			{
       
   131 			WRITELOG("CMMCMonitorPlugin::StartMonitoring - start mass storage scan");
       
   132 			
       
   133 			TMdEMediaInfo hdInfo;
       
   134 			hdInfo.iMediaId = hdMediaId;
       
   135 			hdInfo.iDrive = driveLetter;
       
   136 			medias.Append( hdInfo );
       
   137 			
       
   138 			TRAP_IGNORE( iHddScanner = CMmcScannerAO::NewL( hdMediaId, iMdEClient, iObserver,
       
   139 			    				aHarvesterPluginFactory, CActive::EPriorityUserInput, alreadyWaited ));
       
   140 			}
       
   141 		}
       
   142 
       
   143     iMMCMonitor->StartMonitoring( *this, medias );
       
   144     
       
   145     medias.Close();
       
   146  
       
   147    	return iUsbMonitor->StartMonitoring( *this );
       
   148     }
       
   149 
       
   150 TBool CMMCMonitorPlugin::StopMonitoring()
       
   151     {
       
   152     WRITELOG( "CMMCMonitorPlugin::StopMonitoring" );
       
   153     
       
   154     iMMCMonitor->StopMonitoring();
       
   155     return iUsbMonitor->StopMonitoring();
       
   156     }
       
   157 
       
   158 TBool CMMCMonitorPlugin::ResumeMonitoring( MMonitorPluginObserver& /*aObserver*/,
       
   159     CMdESession* /*aMdEClient*/, CContextEngine* /*aCtxEngine*/,
       
   160     CHarvesterPluginFactory* /*aHarvesterPluginFactory*/ )
       
   161     {
       
   162     WRITELOG( "CMMCMonitorPlugin::ResumeMonitoring" );
       
   163     iMountTask->SetCachingStatus( EFalse );
       
   164     return ETrue;
       
   165     }
       
   166 
       
   167 TBool CMMCMonitorPlugin::PauseMonitoring()
       
   168     {
       
   169     WRITELOG( "CMMCMonitorPlugin::PauseMonitoring" ); // DEBUG INFO
       
   170     iMountTask->SetCachingStatus( ETrue );
       
   171     return ETrue;
       
   172     }
       
   173 
       
   174 // constructor support
       
   175 // don't export these, because used only by functions in this DLL, eg our NewLC()
       
   176 CMMCMonitorPlugin::CMMCMonitorPlugin() // first-phase C++ constructor
       
   177     {
       
   178     // No implementation required
       
   179     }
       
   180 
       
   181 void CMMCMonitorPlugin::AddNotificationPathL( TChar aDrive )
       
   182     {
       
   183     WRITELOG( "CMMCMonitorPlugin::AddNotificationPath" );
       
   184 
       
   185     // 1 in length is for aDrive
       
   186     HBufC* path = HBufC::NewLC( 1 + KColon.iTypeLength );
       
   187     TPtr pathPtr = path->Des();
       
   188     pathPtr.Append( aDrive );
       
   189     pathPtr.Append( KColon );    
       
   190     
       
   191 	CHarvesterCenRepUtil* cenRepoUtil = CHarvesterCenRepUtil::NewLC();
       
   192 	cenRepoUtil->AddIgnorePathsToFspL( pathPtr );
       
   193 	cenRepoUtil->FspEngine().AddNotificationPath( pathPtr );
       
   194 	CleanupStack::PopAndDestroy( cenRepoUtil );    
       
   195     CleanupStack::PopAndDestroy( path );
       
   196     }
       
   197 
       
   198 void CMMCMonitorPlugin::MountEvent( TChar aDriveChar, TUint32 aMediaID, TMMCEventType aEventType )
       
   199     {
       
   200     WRITELOG( "CMMCMonitorPlugin::MountEvent" );
       
   201 
       
   202     TMountData* mountData = NULL;
       
   203     mountData = new TMountData;
       
   204     if ( !mountData )
       
   205         {
       
   206         return;
       
   207         }
       
   208     if( aMediaID != 0 && aEventType == EMounted)
       
   209     	{
       
   210 	    RFs fs;
       
   211 	    const TInt err = fs.Connect();
       
   212 	    if ( err != KErrNone )
       
   213 	    	{
       
   214 	        delete mountData;
       
   215 	    	return;
       
   216 	    	}
       
   217 
       
   218 	    TUint status;
       
   219 	    TInt drive;
       
   220 	    fs.CharToDrive( aDriveChar, drive );
       
   221 		if( DriveInfo::GetDriveStatus( fs, drive, status ) == KErrNone )
       
   222 			{
       
   223 			//The "Out of disk space" mde query uses the MdE_Preferences table
       
   224 			if( !(status & DriveInfo::EDriveInternal) )
       
   225 				{
       
   226 				iMdEClient->AddMemoryCard( aMediaID );
       
   227 				}
       
   228 			}
       
   229 		
       
   230 		fs.Close();
       
   231     	}
       
   232 
       
   233     mountData->iDrivePath.Append( aDriveChar );
       
   234     mountData->iDrivePath.Append( KColon );
       
   235     mountData->iMediaID = aMediaID;
       
   236     
       
   237     switch ( aEventType )
       
   238         {
       
   239         case EMounted:
       
   240             {
       
   241             WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EMounted" );
       
   242             mountData->iMountType = TMountData::EMount;
       
   243             iMountTask->StartMount( *mountData );
       
   244             }
       
   245         break;
       
   246         
       
   247         case EDismounted:
       
   248             {
       
   249             if( aMediaID == 0 )
       
   250             	{
       
   251             	TRAP_IGNORE( mountData->iMediaID = FSUtil::GetPreviousMediaIDL( iMdEClient, aDriveChar ) );
       
   252             	}
       
   253             if( mountData->iMediaID )
       
   254             	{
       
   255 	            WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EDismounted" );
       
   256 	            mountData->iMountType = TMountData::EUnmount;
       
   257 	            iMountTask->StartUnmount( *mountData );
       
   258             	}
       
   259             }
       
   260         break;
       
   261         
       
   262         case EFormatted:
       
   263             {
       
   264             WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EFormatted" );
       
   265             mountData->iMountType = TMountData::EFormat;
       
   266             iMountTask->StartUnmount( *mountData );
       
   267             }
       
   268         break;
       
   269         
       
   270         default:
       
   271             {
       
   272             _LIT( KLogPanic, "unknown state" );
       
   273             User::Panic( KLogPanic, KErrArgument );
       
   274             }
       
   275         break;
       
   276         }
       
   277     }
       
   278 
       
   279 void CMMCMonitorPlugin::StartMonitoringAllMMCsL( RArray<TMdEMediaInfo>& aMedias )
       
   280     {
       
   281     WRITELOG( "CMMCMonitorPlugin::StartMonitoringAllMMCs" );
       
   282     TInt count( 0 );
       
   283     
       
   284     RFs fs;
       
   285     User::LeaveIfError( fs.Connect() );
       
   286     CleanupClosePushL( fs );
       
   287         
       
   288     TDriveInfo driveInfo;
       
   289     TDriveList driveList;
       
   290     TInt numOfElements( 0 );
       
   291     DriveInfo::GetUserVisibleDrives( fs, 
       
   292                                                         driveList, 
       
   293                                                         numOfElements, 
       
   294                                                         KDriveAttExclude | KDriveAttRemote | KDriveAttRom );
       
   295     
       
   296     TInt i( 0 );
       
   297     TChar drive;
       
   298     const TInt acount = driveList.Length();
       
   299     const TInt mediaCount = aMedias.Count();
       
   300     
       
   301     // set removed medias to not present
       
   302     for ( i = 0; i < mediaCount; i++ )
       
   303     	{
       
   304     	TInt driveNum(0);
       
   305     	fs.CharToDrive( aMedias[i].iDrive, driveNum );
       
   306     	TUint32 mediaId = FSUtil::MediaID( fs, driveNum );
       
   307     	if ( mediaId != aMedias[i].iMediaId ) 
       
   308     		{
       
   309     		iMdEClient->SetMediaL( aMedias[i].iMediaId, aMedias[i].iDrive, EFalse );
       
   310     		}
       
   311     	}
       
   312     
       
   313     for ( i = 0; i < acount; i++ )
       
   314         {
       
   315         if ( driveList[i] > 0 )
       
   316             {
       
   317             TUint driveStatus( 0 );
       
   318             DriveInfo::GetDriveStatus( fs, i, driveStatus ); 
       
   319 
       
   320             if ( driveStatus & DriveInfo::EDriveUsbMemory )
       
   321                 {
       
   322                 driveList[i] = 0;
       
   323                 continue;
       
   324                 }
       
   325             
       
   326             fs.Drive( driveInfo, i );
       
   327             if ( driveInfo.iDriveAtt & KDriveAttRemovable && driveInfo.iType != EMediaNotPresent )
       
   328                 {
       
   329                 count++; // DEBUG INFO
       
   330                 
       
   331                 fs.DriveToChar( i, drive );
       
   332                 
       
   333                 // set media id to MdE
       
   334                 TUint32 mediaId = FSUtil::MediaID( fs, i );
       
   335                 if ( mediaId != 0 )
       
   336                     {
       
   337                     iMdEClient->SetMediaL( mediaId, drive, ETrue );
       
   338 
       
   339                     AddNotificationPathL( drive );
       
   340                     }
       
   341                 }
       
   342             }
       
   343         }
       
   344     
       
   345     CleanupStack::PopAndDestroy( &fs ); 
       
   346     
       
   347     WRITELOG1( "CMMCMonitorPlugin::StartMonitoringAllMMCs found %d MMCs", count );
       
   348     }