harvester/monitorplugins/mmcplugin/src/mmcmonitorplugin.cpp
changeset 20 6dfc5f825351
parent 3 b73a2e62868f
child 21 50bf9db68373
equal deleted inserted replaced
3:b73a2e62868f 20:6dfc5f825351
    20 #include "mdsfileserverpluginclient.h"
    20 #include "mdsfileserverpluginclient.h"
    21 #include "fsutil.h"
    21 #include "fsutil.h"
    22 #include "harvestercenreputil.h"
    22 #include "harvestercenreputil.h"
    23 #include <driveinfo.h>
    23 #include <driveinfo.h>
    24 
    24 
       
    25 #ifdef __WINSCW__
       
    26 #include <pathinfo.h>
       
    27 #endif
       
    28 
    25 #include <e32cmn.h>
    29 #include <e32cmn.h>
    26 
    30 
    27 _LIT( KColon, ":" );
    31 _LIT( KColon, ":" );
    28 
    32 
    29 // construct/destruct
    33 // construct/destruct
    38 
    42 
    39 void CMMCMonitorPlugin::ConstructL() // second-phase constructor
    43 void CMMCMonitorPlugin::ConstructL() // second-phase constructor
    40     {
    44     {
    41     WRITELOG( "CMMCMonitorPlugin::ConstructL" );
    45     WRITELOG( "CMMCMonitorPlugin::ConstructL" );
    42 
    46 
       
    47     User::LeaveIfError( iFs.Connect() );
       
    48     
    43     iMMCMonitor = CMMCMonitorAO::NewL();
    49     iMMCMonitor = CMMCMonitorAO::NewL();
    44     iMountTask = CMMCMountTaskAO::NewL();
    50     iMountTask = CMMCMountTaskAO::NewL();
    45     iUsbMonitor = CMMCUsbAO::NewL();
    51     iUsbMonitor = CMMCUsbAO::NewL();
    46     iMdEClient = NULL;
    52     iMdEClient = NULL;
    47     }
    53     }
    67     	delete iMountTask;
    73     	delete iMountTask;
    68     	}
    74     	}
    69 	
    75 	
    70 	delete iMmcScanner;
    76 	delete iMmcScanner;
    71 	delete iHddScanner;
    77 	delete iHddScanner;
       
    78 	
       
    79 	iFs.Close();
    72     }
    80     }
    73 
    81 
    74 TBool CMMCMonitorPlugin::StartMonitoring( MMonitorPluginObserver& aObserver,
    82 TBool CMMCMonitorPlugin::StartMonitoring( MMonitorPluginObserver& aObserver,
    75     CMdESession* aMdEClient, CContextEngine* /*aCtxEngine*/, 
    83     CMdESession* aMdEClient, CContextEngine* /*aCtxEngine*/, 
    76     CHarvesterPluginFactory* aHarvesterPluginFactory )
    84     CHarvesterPluginFactory* aHarvesterPluginFactory )
   112     	TRAP_IGNORE( iMdEClient->GetMediaL( medias[i].iMediaId, driveLetter, presentState ) );
   120     	TRAP_IGNORE( iMdEClient->GetMediaL( medias[i].iMediaId, driveLetter, presentState ) );
   113     	
   121     	
   114     	if ( presentState && medias[i].iMediaId != hdMediaId )
   122     	if ( presentState && medias[i].iMediaId != hdMediaId )
   115     		{
   123     		{
   116     		// scan MMC if card was in phone
   124     		// scan MMC if card was in phone
       
   125     	
       
   126     	    if( iMmcScanner )
       
   127     	        {
       
   128     	        iMmcScanner->Cancel();
       
   129     	        delete iMmcScanner;
       
   130     	        iMmcScanner = NULL;
       
   131     	        }
   117     		TRAP_IGNORE( iMmcScanner = CMmcScannerAO::NewL( medias[i].iMediaId, iMdEClient, iObserver,
   132     		TRAP_IGNORE( iMmcScanner = CMmcScannerAO::NewL( medias[i].iMediaId, iMdEClient, iObserver,
   118     		    				aHarvesterPluginFactory, CActive::EPriorityHigh, alreadyWaited ) );
   133     		    				aHarvesterPluginFactory, CActive::EPriorityHigh, alreadyWaited ) );
   119     		alreadyWaited = ETrue;
   134     		alreadyWaited = ETrue;
   120     		}
   135     		}
   121     	}
   136     	}
   122  
   137  
       
   138     if( hdMediaId == 0 )
       
   139         {
       
   140         // Try to fetch internall mass storage media id again if it was not mounted
       
   141         hdMediaId = iMountTask->GetInternalDriveMediaId();
       
   142         }
       
   143     
   123     // scan mass storage to catch all chances even if battery dies during operation that should  be catched
   144     // scan mass storage to catch all chances even if battery dies during operation that should  be catched
   124     if( hdMediaId )
   145     if( hdMediaId )
   125 		{
   146 		{
   126 		TBool exists( EFalse );
   147 		TBool exists( EFalse );
   127 		TRAP_IGNORE( exists= iMdEClient->GetMediaL( hdMediaId, driveLetter, presentState ) );
   148 		TRAP_IGNORE( exists= iMdEClient->GetMediaL( hdMediaId, driveLetter, presentState ) );
   205         {
   226         {
   206         return;
   227         return;
   207         }
   228         }
   208     if( aMediaID != 0 && aEventType == EMounted)
   229     if( aMediaID != 0 && aEventType == EMounted)
   209     	{
   230     	{
   210 	    RFs fs;
       
   211 	    const TInt err = fs.Connect();
       
   212 	    if ( err != KErrNone )
       
   213 	    	{
       
   214 	        delete mountData;
       
   215 	    	return;
       
   216 	    	}
       
   217 
       
   218 	    TUint status;
   231 	    TUint status;
   219 	    TInt drive;
   232 	    TInt drive;
   220 	    fs.CharToDrive( aDriveChar, drive );
   233 	    iFs.CharToDrive( aDriveChar, drive );
   221 		if( DriveInfo::GetDriveStatus( fs, drive, status ) == KErrNone )
   234 		if( DriveInfo::GetDriveStatus( iFs, drive, status ) == KErrNone )
   222 			{
   235 			{
   223 			//The "Out of disk space" mde query uses the MdE_Preferences table
   236 			//The "Out of disk space" mde query uses the MdE_Preferences table
   224 			if( !(status & DriveInfo::EDriveInternal) )
   237 			if( !(status & DriveInfo::EDriveInternal) )
   225 				{
   238 				{
   226 				iMdEClient->AddMemoryCard( aMediaID );
   239 				iMdEClient->AddMemoryCard( aMediaID );
   227 				}
   240 				}
   228 			}
   241 			}
   229 		
       
   230 		fs.Close();
       
   231     	}
   242     	}
   232 
   243 
   233     mountData->iDrivePath.Append( aDriveChar );
   244     mountData->iDrivePath.Append( aDriveChar );
   234     mountData->iDrivePath.Append( KColon );
   245     mountData->iDrivePath.Append( KColon );
   235     mountData->iMediaID = aMediaID;
   246     mountData->iMediaID = aMediaID;
   238         {
   249         {
   239         case EMounted:
   250         case EMounted:
   240             {
   251             {
   241             WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EMounted" );
   252             WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EMounted" );
   242             mountData->iMountType = TMountData::EMount;
   253             mountData->iMountType = TMountData::EMount;
       
   254             if( !iMountTask->IsActive() )
       
   255                 {
       
   256                 iMountTask->SetPriority( KHarvesterPriorityMonitorPlugin );
       
   257                 }
   243             iMountTask->StartMount( *mountData );
   258             iMountTask->StartMount( *mountData );
   244             }
   259             }
   245         break;
   260         break;
   246         
   261         
   247         case EDismounted:
   262         case EDismounted:
   252             	}
   267             	}
   253             if( mountData->iMediaID )
   268             if( mountData->iMediaID )
   254             	{
   269             	{
   255 	            WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EDismounted" );
   270 	            WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EDismounted" );
   256 	            mountData->iMountType = TMountData::EUnmount;
   271 	            mountData->iMountType = TMountData::EUnmount;
       
   272 	            if( !iMountTask->IsActive() )
       
   273 	                {
       
   274 	                iMountTask->SetPriority( KHarvesterPriorityMonitorPlugin );
       
   275 	                }
   257 	            iMountTask->StartUnmount( *mountData );
   276 	            iMountTask->StartUnmount( *mountData );
   258             	}
   277             	}
   259             }
   278             }
   260         break;
   279         break;
   261         
   280         
   262         case EFormatted:
   281         case EFormatted:
   263             {
   282             {
   264             WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EFormatted" );
   283             WRITELOG( "CMMCMonitorPlugin::MountEvent with parameter EFormatted" );
   265             mountData->iMountType = TMountData::EFormat;
   284             mountData->iMountType = TMountData::EFormat;
       
   285             if( !iMountTask->IsActive() )
       
   286                 {
       
   287                 iMountTask->SetPriority( KHarvesterPriorityMonitorPlugin );
       
   288                 }
   266             iMountTask->StartUnmount( *mountData );
   289             iMountTask->StartUnmount( *mountData );
   267             }
   290             }
   268         break;
   291         break;
   269         
   292         
   270         default:
   293         default:
   277     }
   300     }
   278 
   301 
   279 void CMMCMonitorPlugin::StartMonitoringAllMMCsL( RArray<TMdEMediaInfo>& aMedias )
   302 void CMMCMonitorPlugin::StartMonitoringAllMMCsL( RArray<TMdEMediaInfo>& aMedias )
   280     {
   303     {
   281     WRITELOG( "CMMCMonitorPlugin::StartMonitoringAllMMCs" );
   304     WRITELOG( "CMMCMonitorPlugin::StartMonitoringAllMMCs" );
   282     
   305 
   283     RFs fs;
       
   284     User::LeaveIfError( fs.Connect() );
       
   285     CleanupClosePushL( fs );
       
   286         
       
   287     TDriveInfo driveInfo;
   306     TDriveInfo driveInfo;
   288     TDriveList driveList;
   307     TDriveList driveList;
   289     TInt numOfElements( 0 );
   308     TInt numOfElements( 0 );
   290     DriveInfo::GetUserVisibleDrives( fs, 
   309     DriveInfo::GetUserVisibleDrives( iFs, 
   291                                                         driveList, 
   310                                                         driveList, 
   292                                                         numOfElements, 
   311                                                         numOfElements, 
   293                                                         KDriveAttExclude | KDriveAttRemote | KDriveAttRom );
   312                                                         KDriveAttExclude | KDriveAttRemote | KDriveAttRom );
       
   313 
       
   314 #ifdef __WINSCW__
       
   315     TFileName systemPath = PathInfo::GetPath( PathInfo::EPhoneMemoryRootPath );
       
   316     TInt systemDriveNum( -1 );
       
   317     iFs.CharToDrive( systemPath[0], systemDriveNum );
       
   318 #endif
   294     
   319     
   295     TInt i( 0 );
   320     TInt i( 0 );
   296     TChar drive;
   321     TChar drive;
   297     const TInt acount = driveList.Length();
   322     const TInt acount = driveList.Length();
   298     const TInt mediaCount = aMedias.Count();
   323     const TInt mediaCount = aMedias.Count();
   300     
   325     
   301     // set removed medias to not present
   326     // set removed medias to not present
   302     for ( i = 0; i < mediaCount; i++ )
   327     for ( i = 0; i < mediaCount; i++ )
   303     	{
   328     	{
   304     	TInt driveNum(0);
   329     	TInt driveNum(0);
   305     	fs.CharToDrive( aMedias[i].iDrive, driveNum );
   330     	iFs.CharToDrive( aMedias[i].iDrive, driveNum );
   306     	TUint32 mediaId = FSUtil::MediaID( fs, driveNum );
   331     	TUint32 mediaId = FSUtil::MediaID( iFs, driveNum );
   307     	if ( mediaId != aMedias[i].iMediaId ) 
   332     	if ( mediaId != aMedias[i].iMediaId ) 
   308     		{
   333     		{
   309     		iMdEClient->SetMediaL( aMedias[i].iMediaId, aMedias[i].iDrive, EFalse );
   334     		iMdEClient->SetMediaL( aMedias[i].iMediaId, aMedias[i].iDrive, EFalse );
   310     		}
   335     		}
   311     	}
   336     	}
   312     
   337     
   313     for ( i = 0; i < acount; i++ )
   338     for ( i = 0; i < acount; i++ )
   314         {
   339         {
       
   340 #ifdef __WINSCW__
       
   341         if ( i == systemDriveNum )
       
   342             {
       
   343             continue;
       
   344             }
       
   345 #endif
       
   346     
   315         if ( driveList[i] > 0 )
   347         if ( driveList[i] > 0 )
   316             {
   348             {
   317             TUint driveStatus( 0 );
   349             TUint driveStatus( 0 );
   318             DriveInfo::GetDriveStatus( fs, i, driveStatus ); 
   350             DriveInfo::GetDriveStatus( iFs, i, driveStatus ); 
   319 
   351 
   320             if ( driveStatus & DriveInfo::EDriveUsbMemory )
   352             if ( driveStatus & DriveInfo::EDriveUsbMemory )
   321                 {
   353                 {
   322                 driveList[i] = 0;
   354                 driveList[i] = 0;
   323                 continue;
   355                 continue;
   324                 }
   356                 }
   325             
   357             
   326             fs.Drive( driveInfo, i );
   358             iFs.Drive( driveInfo, i );
   327             if ( driveInfo.iDriveAtt & KDriveAttRemovable && driveInfo.iType != EMediaNotPresent )
   359             if ( ((driveInfo.iDriveAtt & KDriveAttRemovable) || (driveInfo.iDriveAtt & KDriveAttLogicallyRemovable) ||
       
   360                    (driveInfo.iType == EMediaHardDisk && driveStatus & DriveInfo::EDriveInternal) ) &&
       
   361                    (driveInfo.iType != EMediaNotPresent) )
   328                 {
   362                 {
   329                 count++; // DEBUG INFO
   363                 count++; // DEBUG INFO
   330                 
   364                 
   331                 fs.DriveToChar( i, drive );
   365                 iFs.DriveToChar( i, drive );
   332                 
   366                 
   333                 // set media id to MdE
   367                 // set media id to MdE
   334                 TUint32 mediaId = FSUtil::MediaID( fs, i );
   368                 TUint32 mediaId = FSUtil::MediaID( iFs, i );
   335                 if ( mediaId != 0 )
   369                 if ( mediaId != 0 )
   336                     {
   370                     {
   337                     iMdEClient->SetMediaL( mediaId, drive, ETrue );
   371                     iMdEClient->SetMediaL( mediaId, drive, ETrue );
   338 
   372 
   339                     AddNotificationPathL( drive );
   373                     AddNotificationPathL( drive );
   340                     }
   374                     }
   341                 }
   375                 }
   342             }
   376             }
   343         }
   377         }
   344     
   378     
   345     CleanupStack::PopAndDestroy( &fs ); 
       
   346     
       
   347     WRITELOG1( "CMMCMonitorPlugin::StartMonitoringAllMMCs found %d MMCs", count );
   379     WRITELOG1( "CMMCMonitorPlugin::StartMonitoringAllMMCs found %d MMCs", count );
   348     }
   380     }
       
   381