appinstaller/AppinstUi/Daemon/Src/drivewatcher.cpp
branchRCL_3
changeset 25 7333d7932ef7
parent 0 ba25891c3a9e
child 26 8b7f4e561641
equal deleted inserted replaced
24:5cc91383ab1e 25:7333d7932ef7
    17 
    17 
    18 
    18 
    19 #include "drivewatcher.h"
    19 #include "drivewatcher.h"
    20 #include "SWInstDebug.h"
    20 #include "SWInstDebug.h"
    21 
    21 
    22 namespace Swi
    22 using namespace Swi;
    23 {
    23 
    24 // CDriveWatcher
    24 // CDriveWatcher
    25 _LIT(KNotificationDirectory,"mediachange\\");
    25 _LIT(KNotificationDirectory,"mediachange\\");
    26 
    26 
    27 /*static*/ CDriveWatcher* CDriveWatcher::NewL(RFs& aFs, TInt aDrive, 
    27 
    28 												MDriveObserver& aObserver,
    28 // -----------------------------------------------------------------------
    29 							 					TInt aPriority)
    29 // CDriveWatcher::NewL
       
    30 // -----------------------------------------------------------------------
       
    31 //    
       
    32 /*static*/ 
       
    33 CDriveWatcher* CDriveWatcher::NewL( RFs& aFs, 
       
    34                                     TInt aDrive, 
       
    35                                     MDriveObserver& aObserver,
       
    36                                     TInt aPriority )
    30 	{
    37 	{
    31 	CDriveWatcher* self=NewLC(aFs, aDrive, aObserver, aPriority);
    38 	CDriveWatcher* self = NewLC( aFs, aDrive, aObserver, aPriority );
    32 	CleanupStack::Pop(self);
    39 	CleanupStack::Pop(self);
    33 	return self;
    40 	return self;
    34 	}
    41 	}
    35 	
    42 
    36 /*static*/ CDriveWatcher* CDriveWatcher::NewLC(RFs& aFs, TInt aDrive, 
    43 // -----------------------------------------------------------------------
    37 												MDriveObserver& aObserver,
    44 // 
    38 							 					TInt aPriority)
    45 // -----------------------------------------------------------------------
       
    46 // 
       
    47 /*static*/ 
       
    48 CDriveWatcher* CDriveWatcher::NewLC( RFs& aFs, 
       
    49                                      TInt aDrive, 
       
    50                                      MDriveObserver& aObserver,
       
    51                                      TInt aPriority )
    39 	{
    52 	{
    40 	CDriveWatcher* self=new(ELeave) CDriveWatcher(aFs, aDrive, aObserver, aPriority);
    53 	CDriveWatcher* self = new(ELeave) CDriveWatcher( aFs, 
    41 	CleanupStack::PushL(self);
    54 	                                                 aDrive, 
       
    55 	                                                 aObserver, 
       
    56 	                                                 aPriority );
       
    57 	CleanupStack::PushL( self );
    42 	self->ConstructL();
    58 	self->ConstructL();
    43 	return self;	
    59 	return self;	
    44 	}
    60 	}
    45 	
    61 
       
    62 // -----------------------------------------------------------------------
       
    63 // 
       
    64 // -----------------------------------------------------------------------
       
    65 //
    46 CDriveWatcher::~CDriveWatcher()
    66 CDriveWatcher::~CDriveWatcher()
    47 	{
    67 	{
    48 	Cancel();
    68 	Cancel();
    49 	}
    69 	}
    50 
    70 
    51 CDriveWatcher::CDriveWatcher(RFs& aFs, TInt aDrive, MDriveObserver& aObserver,
    71 // -----------------------------------------------------------------------
    52 							 TInt aPriority)
    72 // 
    53 	: CActive(aPriority), iFs(aFs), iDrive(aDrive), iObserver(aObserver)
    73 // -----------------------------------------------------------------------
       
    74 //
       
    75 CDriveWatcher::CDriveWatcher( RFs& aFs, 
       
    76                               TInt aDrive, 
       
    77                               MDriveObserver& aObserver,
       
    78                               TInt aPriority )
       
    79                               : CActive(aPriority), 
       
    80                                 iFs(aFs), 
       
    81                                 iDrive(aDrive), 
       
    82                                 iObserver(aObserver)
    54 	{
    83 	{
    55 	CActiveScheduler::Add(this);
    84 	CActiveScheduler::Add(this);
    56 	}
    85 	}
    57 
    86 
       
    87 // -----------------------------------------------------------------------
       
    88 // 
       
    89 // -----------------------------------------------------------------------
       
    90 //
    58 void CDriveWatcher::ConstructL()
    91 void CDriveWatcher::ConstructL()
    59 	{
    92 	{
    60 	// Notify observer of media change since we're beginning from an unknown state
    93 	// Notify observer of media change since we're beginning 
       
    94     // from an unknown state
    61 	NotifyMediaChange();
    95 	NotifyMediaChange();
    62 	
    96 	
    63 	// Start watching for changes
    97 	// Start watching for changes
    64 	WaitForChangeL();
    98 	WaitForChangeL();
    65 	}
    99 	}
    66 
   100 
       
   101 // -----------------------------------------------------------------------
       
   102 // 
       
   103 // -----------------------------------------------------------------------
       
   104 //
    67 void CDriveWatcher::DoCancel()
   105 void CDriveWatcher::DoCancel()
    68 	{
   106 	{
    69 	iFs.NotifyChangeCancel(iStatus);
   107 	iFs.NotifyChangeCancel(iStatus);
    70 	}
   108 	}
    71 
   109 
       
   110 // -----------------------------------------------------------------------
       
   111 // 
       
   112 // -----------------------------------------------------------------------
       
   113 //
    72 TBool CDriveWatcher::IsMediaPresentL()
   114 TBool CDriveWatcher::IsMediaPresentL()
    73 	{
   115 	{
    74         FLOG_1( _L("Daemon: Checking media presence for drive %d"), iDrive );
   116     FLOG_1( _L("Daemon: Checking media presence for drive %d"), iDrive );
    75         
   117         
    76 	TVolumeInfo volumeInfo;
   118 	TVolumeInfo volumeInfo;
    77 	TInt err=iFs.Volume(volumeInfo, iDrive);
   119 	TInt err = iFs.Volume( volumeInfo, iDrive );
    78 	
   120 	
    79 	switch (err)
   121 	switch ( err )
    80 		{
   122 		{
    81 		case KErrNotReady: // No Media present
   123 		case KErrNotReady: // No Media present
    82 			{
   124 			{
    83 			return EFalse;	
   125 			return EFalse;	
    84 			}
   126 			}
    87 			{
   129 			{
    88 			return ETrue;
   130 			return ETrue;
    89 			}
   131 			}
    90 		}
   132 		}
    91 
   133 
    92 	User::Leave(err);	
   134 	User::Leave( err );	
    93 	return ETrue;	// Will never get here.
   135 	return ETrue;	// Will never get here.
    94 	}
   136 	}
    95 
   137 
       
   138 // -----------------------------------------------------------------------
       
   139 // 
       
   140 // -----------------------------------------------------------------------
       
   141 //
    96 void CDriveWatcher::NotifyMediaChange()
   142 void CDriveWatcher::NotifyMediaChange()
    97 	{
   143 	{
    98         FLOG( _L("Daemon: NotifyMediaChange") );        
   144     FLOG( _L("Daemon: NotifyMediaChange") );        
    99 	// Unsuccessful media change is not fatal, so handle here
   145 	// Unsuccessful media change is not fatal, so handle here
   100 	TRAPD(err,iObserver.MediaChangeL(iDrive, IsMediaPresentL() ? MDriveObserver::EMediaInserted : MDriveObserver::EMediaRemoved));
   146 	TRAPD( err, iObserver.MediaChangeL( iDrive, 
       
   147 	                                    IsMediaPresentL() 
       
   148 	                                    ? MDriveObserver::EMediaInserted : 
       
   149                                         MDriveObserver::EMediaRemoved));
   101 
   150 
   102 	if (err != KErrNone)
   151 	if (err != KErrNone)
   103 		{
   152 		{
   104                 FLOG_1(_L("Daemon: MDriveObserver::MediaChangeL left while processing media notification %d"),err);
   153         FLOG_1(_L("Daemon: MediaChangeL TRAP err = %d"), err );
   105 		}              
   154 		}              
   106 	}
   155 	}
   107 	
   156 
       
   157 // -----------------------------------------------------------------------
       
   158 // 
       
   159 // -----------------------------------------------------------------------
       
   160 //	
   108 void CDriveWatcher::RunL()
   161 void CDriveWatcher::RunL()
   109 	{
   162 	{
   110 	NotifyMediaChange();
   163 	NotifyMediaChange();
   111 			
   164 			
   112 	WaitForChangeL();
   165 	WaitForChangeL();
   113 	}
   166 	}
   114 
   167 
       
   168 // -----------------------------------------------------------------------
       
   169 // 
       
   170 // -----------------------------------------------------------------------
       
   171 //
   115 void CDriveWatcher::WaitForChangeL()
   172 void CDriveWatcher::WaitForChangeL()
   116 	{
   173 	{
   117 	TChar drive;
   174 	TChar drive;
   118 	User::LeaveIfError(iFs.DriveToChar(iDrive, drive));
   175 	User::LeaveIfError( iFs.DriveToChar( iDrive, drive ) );
   119 	TUint driveChar(drive); // Can't pass TChar to Format().
   176 	TUint driveChar(drive); // Can't pass TChar to Format().
   120 	
   177 	
   121 	TPath notificationPath;
   178 	TPath notificationPath;
   122 	TPath privatePath;
   179 	TPath privatePath;
   123 	_LIT(KNotificationPathFormat,"%c:%S%S");
   180 	_LIT(KNotificationPathFormat,"%c:%S%S");
   124 	User::LeaveIfError(iFs.PrivatePath(privatePath));
   181 	User::LeaveIfError( iFs.PrivatePath( privatePath ) );
   125 
   182 
   126 	notificationPath.Format(KNotificationPathFormat, driveChar, &privatePath, &KNotificationDirectory);	
   183 	notificationPath.Format( KNotificationPathFormat, 
       
   184 	                         driveChar, 
       
   185 	                         &privatePath, 
       
   186 	                         &KNotificationDirectory );	
   127 	
   187 	
   128 	iFs.NotifyChange(ENotifyEntry, iStatus, notificationPath);
   188 	iFs.NotifyChange( ENotifyEntry, iStatus, notificationPath );
   129 
   189 
   130 	SetActive();
   190 	SetActive();
   131 	}
   191 	}
   132 	
   192 	
   133 }
   193 //EOF