installationservices/swi/source/daemon/drivewatcher.cpp
branchRCL_3
changeset 26 8b7f4e561641
parent 0 ba25891c3a9e
equal deleted inserted replaced
25:7333d7932ef7 26:8b7f4e561641
       
     1 /*
       
     2 * Copyright (c) 2004-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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "drivewatcher.h"
       
    20 #include "log.h"
       
    21 
       
    22 namespace Swi
       
    23 {
       
    24 // CDriveWatcher
       
    25 _LIT(KNotificationDirectory,"mediachange\\");
       
    26 
       
    27 /*static*/ CDriveWatcher* CDriveWatcher::NewL(RFs& aFs, TInt aDrive, 
       
    28 												MDriveObserver& aObserver,
       
    29 							 					TInt aPriority)
       
    30 	{
       
    31 	CDriveWatcher* self=NewLC(aFs, aDrive, aObserver, aPriority);
       
    32 	CleanupStack::Pop(self);
       
    33 	return self;
       
    34 	}
       
    35 	
       
    36 /*static*/ CDriveWatcher* CDriveWatcher::NewLC(RFs& aFs, TInt aDrive, 
       
    37 												MDriveObserver& aObserver,
       
    38 							 					TInt aPriority)
       
    39 	{
       
    40 	CDriveWatcher* self=new(ELeave) CDriveWatcher(aFs, aDrive, aObserver, aPriority);
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	return self;	
       
    44 	}
       
    45 	
       
    46 CDriveWatcher::~CDriveWatcher()
       
    47 	{
       
    48 	Cancel();
       
    49 	}
       
    50 
       
    51 CDriveWatcher::CDriveWatcher(RFs& aFs, TInt aDrive, MDriveObserver& aObserver,
       
    52 							 TInt aPriority)
       
    53 	: CActive(aPriority), iFs(aFs), iDrive(aDrive), iObserver(aObserver)
       
    54 	{
       
    55 	CActiveScheduler::Add(this);
       
    56 	}
       
    57 
       
    58 void CDriveWatcher::ConstructL()
       
    59 	{
       
    60 	// Notify observer of media change since we're beginning from an unknown state
       
    61 	NotifyMediaChange();
       
    62 	
       
    63 	// Start watching for changes
       
    64 	WaitForChangeL();
       
    65 	}
       
    66 
       
    67 void CDriveWatcher::DoCancel()
       
    68 	{
       
    69 	iFs.NotifyChangeCancel(iStatus);
       
    70 	}
       
    71 
       
    72 TBool CDriveWatcher::IsMediaPresentL()
       
    73 	{
       
    74 	TVolumeInfo volumeInfo;
       
    75 	TInt err=iFs.Volume(volumeInfo, iDrive);
       
    76 	
       
    77 	switch (err)
       
    78 		{
       
    79 		case KErrNotReady: // No Media present
       
    80 			{
       
    81 			return EFalse;	
       
    82 			}
       
    83 			
       
    84 		case KErrNone: // Media Present
       
    85 			{
       
    86 			return ETrue;
       
    87 			}
       
    88 		}
       
    89 
       
    90 	User::Leave(err);	
       
    91 	return ETrue;	// Will never get here.
       
    92 	}
       
    93 
       
    94 void CDriveWatcher::NotifyMediaChange()
       
    95 	{
       
    96 	// Unsuccessful media change is not fatal, so handle here
       
    97 	TRAPD(err,iObserver.MediaChangeL(iDrive, IsMediaPresentL() ? MDriveObserver::EMediaInserted : MDriveObserver::EMediaRemoved));
       
    98 
       
    99 	if (err != KErrNone)
       
   100 		{
       
   101 	    DEBUG_PRINTF2(_L8("SWI Daemon - Media change notification failed with code %d"), err);
       
   102 		}
       
   103 	}
       
   104 	
       
   105 void CDriveWatcher::RunL()
       
   106 	{
       
   107 	NotifyMediaChange();
       
   108 			
       
   109 	WaitForChangeL();
       
   110 	}
       
   111 
       
   112 void CDriveWatcher::WaitForChangeL()
       
   113 	{
       
   114 	TChar drive;
       
   115 	User::LeaveIfError(iFs.DriveToChar(iDrive, drive));
       
   116 	TUint driveChar(drive); // Can't pass TChar to Format().
       
   117 	
       
   118 	TPath notificationPath;
       
   119 	TPath privatePath;
       
   120 	_LIT(KNotificationPathFormat,"%c:%S%S");
       
   121 	User::LeaveIfError(iFs.PrivatePath(privatePath));
       
   122 
       
   123 	notificationPath.Format(KNotificationPathFormat, driveChar, &privatePath, &KNotificationDirectory);	
       
   124 	
       
   125 	iFs.NotifyChange(ENotifyEntry, iStatus, notificationPath);
       
   126 
       
   127 	SetActive();
       
   128 	}
       
   129 	
       
   130 }