mpxplugins/viewplugins/views/waitnotedialog/src/mpxscanningdialog.cpp
branchRCL_3
changeset 15 a1247965635c
parent 14 c54d95799c80
child 21 cb96c29156b2
--- a/mpxplugins/viewplugins/views/waitnotedialog/src/mpxscanningdialog.cpp	Wed Apr 14 15:54:18 2010 +0300
+++ b/mpxplugins/viewplugins/views/waitnotedialog/src/mpxscanningdialog.cpp	Tue Apr 27 16:34:37 2010 +0300
@@ -54,6 +54,8 @@
     iTotalNewTracks = 0;
     iInitialCount = 0;
     isCollectionDBChanged = EFalse;
+    iAsyncEvent = ECmdIdle;
+
     }
 
 
@@ -63,8 +65,13 @@
 //
 void CMPXScanningWaitDialog::ConstructL()
     {
-    iHarvesterUtil = CMPXHarvesterFactory::NewL();
+	 MPX_DEBUG1("CMPXScanningWaitDialog::ConstructL <---");
     BaseConstructL();
+
+	TCallBack callback( CMPXScanningWaitDialog::AsyncCallHarvesterEventL, this );
+	iAsyncCallBack = new (ELeave) CAsyncCallBack( CActive::EPriorityHigh );
+	iAsyncCallBack->Set( callback );
+    MPX_DEBUG1("CMPXScanningWaitDialog::ConstructL --->");
     }
 
 
@@ -93,6 +100,9 @@
         {
         iHarvesterUtil->Close();
         }
+    iHarvesterUtil = NULL;
+    delete iAsyncCallBack;
+    iAsyncCallBack = NULL;
     }
 
 // ---------------------------------------------------------------------------
@@ -136,8 +146,14 @@
         }
         
     // Start the scanning in harvester
-    iHarvesterUtil->ScanL();
-    iScanningError = KErrNone;
+    // Just to be faster, lets just create a small async callback and then
+    // call ScanL   
+    if( !iAsyncCallBack->IsActive() )
+    	{
+		iAsyncEvent = ECmdScan;
+		iCancelScan = EFalse;
+		iAsyncCallBack->CallBack();
+    	}
     }
 
 // ---------------------------------------------------------------------------
@@ -154,16 +170,19 @@
     HBufC* text = NULL;
     if( aButtonId == EAknSoftkeyCancel )
         {
-        iScanningError = KErrCancel;
-        // If harvester crashed,..... restart it.
-        MPX_TRAPD( err, iHarvesterUtil->CancelScanL() );
-        if( err != KErrNone )
-            {
-            iHarvesterUtil->Close();
-            iHarvesterUtil = NULL;
-            iHarvesterUtil = CMPXHarvesterFactory::NewL(); 
-            }
-        
+		// cancel scan in harvester
+		// Just to be faster, lets just create a small async callback and then
+		// call CancelScanL   
+		if( !iAsyncCallBack->IsActive() )
+			{
+			iAsyncEvent = ECmdCancleScan;
+			iAsyncCallBack->CallBack();
+			}      
+		else
+			{
+		    iCancelScan = ETrue;
+			}
+
         // When stop refreshing library, prompt a process waiting dialog.
         // Inform user to wait for the updating library.
         HBufC* waitText = StringLoader::LoadLC( R_MPX_UPDATING_LIBRARY_TXT );  
@@ -272,7 +291,10 @@
 void CMPXScanningWaitDialog::HandleDatabaseCorruptionL()
     {
     // Cleanup Harvester
-    iHarvesterUtil->RecreateDatabasesL();
+	if( iHarvesterUtil != NULL )
+		{
+	    iHarvesterUtil->RecreateDatabasesL();
+		}
     }
 
 // ---------------------------------------------------------------------------
@@ -283,6 +305,7 @@
     const CMPXMessage& aMessage )
     {
     MPX_FUNC( "CMPXScanningWaitDialog::DoHandleCollectionMessageL" );
+    
     TMPXMessageId id( aMessage.ValueTObjectL<TMPXMessageId>( KMPXMessageGeneralId ) );
     if ( KMPXMessageGeneral == id )
         {
@@ -488,5 +511,46 @@
 
     return count;
     }
+// ---------------------------------------------------------------------------
+// async callback 
+// ---------------------------------------------------------------------------
+//
+TInt CMPXScanningWaitDialog::AsyncCallHarvesterEventL( TAny* aSelf )
+    {
+	MPX_DEBUG1("CMPXScanningWaitDialog::CallHarvesterScanL <---");
+	CMPXScanningWaitDialog* self = static_cast<CMPXScanningWaitDialog*>( aSelf );
+	if( self->iHarvesterUtil == NULL )
+		{
+	    self->iHarvesterUtil = CMPXHarvesterFactory::NewL();
+		}
+	if( self->iAsyncEvent == ECmdScan )
+		{
+	    if( !self->iCancelScan )
+	    	{
+		    self->iHarvesterUtil->ScanL();
+		    self->iScanningError = KErrNone;
+	    	}
+	    else
+	    	{
+	        self->iCancelScan = EFalse;
+	    	}
+		}
+	
+	if( self->iAsyncEvent == ECmdCancleScan || self->iCancelScan )
+		{
+	    self->iScanningError = KErrCancel; 
+        // If harvester crashed,..... restart it.
+		MPX_TRAPD( err, self->iHarvesterUtil->CancelScanL() );
+		if( err != KErrNone )
+			{
+		    self->iHarvesterUtil->Close();
+		    self->iHarvesterUtil = NULL;
+		    self->iHarvesterUtil = CMPXHarvesterFactory::NewL(); 
+			}
+		}
+	self->iAsyncEvent = ECmdIdle;
+    MPX_DEBUG1("CMPXScanningWaitDialog::CallHarvesterScanL --->");
+    return KErrNone;
+    }
 // END OF FILE