harvester/monitorplugins/mmcplugin/src/mmcmounttaskao.cpp
changeset 54 a3cc46f37772
parent 21 50bf9db68373
--- a/harvester/monitorplugins/mmcplugin/src/mmcmounttaskao.cpp	Tue Jul 06 14:44:37 2010 +0300
+++ b/harvester/monitorplugins/mmcplugin/src/mmcmounttaskao.cpp	Fri Sep 17 16:54:49 2010 +0300
@@ -79,6 +79,7 @@
 	iHarvestEntryArray.Close();
 	
 	delete iMdeSession;
+	iMdeSession = NULL;
   
 	Deinitialize();
 	
@@ -88,6 +89,7 @@
 		}
 	
 	delete iMmcFileList;
+	iMmcFileList = NULL;
 	iHdArray.ResetAndDestroy();
 	iHdArray.Close();
 	}
@@ -108,17 +110,38 @@
 	iHarvesterPluginFactory = aPluginFactory;
 	}
 	
-void CMMCMountTaskAO::StartMount( TMountData& aMountData )
+void CMMCMountTaskAO::StartMountL( TMountData& aMountData )
 	{
 	WRITELOG("CMMCMountTaskAO::StartMount");
-	iMountDataQueue.Append( &aMountData );
+	
+	// Remove pending mount request for the same drive
+	// if for example USB cable is pluged and unpluged
+	// several times in a row
+	for( TInt i = iMountDataQueue.Count() - 1; i >=0; i-- )
+	    {
+	    WRITELOG( "CMMCMountTaskAO::StartUnmountL - checking for pending mount notifications" );
+	    TMountData* tempData = iMountDataQueue[i];
+	    if( tempData->iMediaID == aMountData.iMediaID &&
+	        tempData->iMountType == TMountData::EMount &&
+	        aMountData.iMountType == TMountData::EMount &&
+	        tempData->iDrivePath == aMountData.iDrivePath )
+	        {
+	        WRITELOG( "CMMCMountTaskAO::StartUnmountL - removing obsolite mount notifications" );
+	        iMountDataQueue.Remove(i);
+	        delete tempData;
+	        tempData = NULL;
+	        }
+	    }
+
+	User::LeaveIfError( iMountDataQueue.Append( &aMountData ));
+	
 	if ( iNextRequest == ERequestIdle )
 		{
 		SetNextRequest( ERequestStartTask );
 		}
 	}
 	
-void CMMCMountTaskAO::StartUnmount(TMountData& aMountData)
+void CMMCMountTaskAO::StartUnmountL(TMountData& aMountData)
 	{
 	WRITELOG("CMMCMountTaskAO::StartUnmount");
 	
@@ -131,8 +154,28 @@
 			Deinitialize();
 			}
 		}
+
+    // Remove pending unmount request for the same drive
+    // if for example USB cable is pluged and unpluged
+    // several times in a row
+    for( TInt i = iMountDataQueue.Count() - 1; i >=0; i-- )
+        {
+        WRITELOG( "CMMCMountTaskAO::StartUnmountL - checking for pending unmount notifications" );
+        TMountData* tempData = iMountDataQueue[i];
+        if( tempData->iMediaID == aMountData.iMediaID &&
+            tempData->iMountType == TMountData::EUnmount &&
+            aMountData.iMountType == TMountData::EUnmount &&
+            tempData->iDrivePath == aMountData.iDrivePath )
+            {
+            WRITELOG( "CMMCMountTaskAO::StartUnmountL - removing obsolite unmount notifications" );
+            iMountDataQueue.Remove(i);
+            delete tempData;
+            tempData = NULL;
+            }
+        }
+	
+	User::LeaveIfError( iMountDataQueue.Append( &aMountData ));
 		
-	iMountDataQueue.Append( &aMountData );
 	SetNextRequest( ERequestStartTask );
 	}
 	
@@ -146,7 +189,10 @@
 		{
 		if ( iMountData )
 			{
-			iMountDataQueue.Insert( iMountData, 0 );
+			if( iMountDataQueue.Insert( iMountData, 0 ) != KErrNone)
+			    {
+                delete iMountData;
+			    }
 			iMountData = NULL;
 			}
 		Deinitialize();
@@ -173,7 +219,7 @@
 
 				WRITELOG1( "iMountData.iMountType: %d", iMountData->iMountType );
 				WRITELOG1( "iMountData.iDrivePath: %S", &iMountData->iDrivePath );
-				WRITELOG1( "iMountData.iMediaID: %d", iMountData->iMediaID );
+				WRITELOG1( "iMountData.iMediaID: %u", iMountData->iMediaID );
 
 				if ( iMountData->iMountType == TMountData::EMount )
 					{
@@ -205,7 +251,11 @@
 			TRAPD( err, iMmcFileList->BuildFileListL( iFs, iMountData->iDrivePath, iEntryArray ));
 			if ( err == KErrNoMemory )
 				{
-				iMountDataQueue.Insert( iMountData, 0 );
+				if( iMountDataQueue.Insert( iMountData, 0 ) != KErrNone)
+				    {
+                    delete iMountData;
+				    }
+					
 				iMountData = NULL;
 				Deinitialize();
 				SetNextRequest( ERequestStartTask );
@@ -284,9 +334,14 @@
 					{
 					if( err == KErrNoMemory )
 						{
-						iMountDataQueue.Insert( iMountData, 0 );
+						if(iMountDataQueue.Insert( iMountData, 0 ) != KErrNone)
+						    {
+                            delete iMountData;
+						    }
+							
 						iMountData = NULL;
 						}
+						
 					Deinitialize();
 					SetNextRequest( ERequestStartTask );
 					break;
@@ -376,6 +431,7 @@
 	{
 	WRITELOG1( "CMMCMountTaskAO::RunError with error code: %d", aError );
 	Deinitialize();
+	SetNextRequest( ERequestStartTask );
 	return KErrNone;
 	}
 
@@ -398,7 +454,7 @@
 
 void CMMCMountTaskAO::SetNotPresentToMDE()
 	{
-	WRITELOG1("CMMCMountTaskAO::SetNotPresentToMDE - MediaID %d", iMountData->iMediaID);
+	WRITELOG1("CMMCMountTaskAO::SetNotPresentToMDE - MediaID %u", iMountData->iMediaID);
 	if ( iMountData->iMediaID )
 		{
 		iMdeSession->SetFilesToNotPresent( iMountData->iMediaID );
@@ -437,6 +493,7 @@
 			hd->SetEventType( EHarvesterEdit );
 			hd->SetObjectType( ENormal );
 			delete ei;
+			ei = NULL;
 			}
 		else
 			{
@@ -445,7 +502,12 @@
 			hd->SetClientData( ei );
 			}
 		
-		iHdArray.Append( hd );
+		if(iHdArray.Append( hd ) != KErrNone )
+		    {
+            delete hd;
+            hd = NULL;
+		    }
+			
 		aArray.Remove( i );
 		}
 	
@@ -515,7 +577,7 @@
 		}
 	}
 
-TUint32 CMMCMountTaskAO::GetInternalDriveMediaId()
+TUint32 CMMCMountTaskAO::GetInternalDriveMediaId( TBool& aPresent )
 	{
     WRITELOG( "CMMCMountTaskAO::GetInternalDriveMediaId" );
 	    
@@ -558,10 +620,15 @@
 	        	// check if disk is internal
 	        	TUint driveStatus;
 	        	const TInt err = DriveInfo::GetDriveStatus( iFs, i, driveStatus );
-	        	if ( (err == KErrNone ) && ( driveStatus & DriveInfo::EDriveInternal ) )
+	        	if ( ( err == KErrNone ) && 
+	        	     ( driveStatus & DriveInfo::EDriveInternal ))
 	        		{
 	        		// get media id
 	        		hdMediaId = FSUtil::MediaID( iFs, i );
+	        		if( driveStatus & DriveInfo::EDrivePresent )
+	        		    {
+	        		    aPresent = ETrue;
+	        		    }
 	        		break;
 	        		}
 	        	}