mmappcomponents/harvester/server/src/mpxmediaremovalmonitor.cpp
changeset 0 a2952bb97e68
child 19 51035f0751c2
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 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 "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:  Monitors for Media removal
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <f32file.h>
       
    21 #include <mpxlog.h>
       
    22 #include "mpxmediaremovalmonitor.h"
       
    23 
       
    24 // ---------------------------------------------------------------------------
       
    25 // C++ Constructor
       
    26 // ---------------------------------------------------------------------------
       
    27 //
       
    28 CMPXMediaRemovalMonitor::CMPXMediaRemovalMonitor
       
    29                   ( TInt aDrive, RFs& aFs, MMPXSystemEventObserver& aObserver ) 
       
    30                                                        : CActive(EPriorityHigh),
       
    31                                                          iDrive( aDrive ),
       
    32                                                          iFs( aFs ),
       
    33                                                          iDiskRemoved( EFalse ),
       
    34                                                          iObserver( aObserver )
       
    35                                                          
       
    36     {
       
    37     CActiveScheduler::Add(this);
       
    38     }
       
    39 
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // 2nd Phase Constructor
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 void CMPXMediaRemovalMonitor::ConstructL()
       
    46     {
       
    47     MPX_DEBUG1(_L("CMPXMediaRemovalMonitor::ConstructL <---"));
       
    48     
       
    49     // Initial state
       
    50     TDriveInfo drive;
       
    51 	User::LeaveIfError(iFs.Drive(drive, TInt(iDrive)));
       
    52    	iDiskRemoved = (drive.iType == EMediaNotPresent);
       
    53 
       
    54     // Start listening
       
    55     TNotifyType notType(ENotifyDisk);
       
    56     iFs.NotifyChange( notType, iStatus );
       
    57     SetActive();
       
    58     
       
    59     MPX_DEBUG1(_L("CMPXMediaRemovalMonitor::ConstructL --->"));
       
    60     }
       
    61 
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Two-Phased Constructor
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CMPXMediaRemovalMonitor* CMPXMediaRemovalMonitor::NewL
       
    68                 ( TInt aDrive, RFs& aFs, MMPXSystemEventObserver& aObserver )
       
    69     {
       
    70     CMPXMediaRemovalMonitor* self
       
    71                                = CMPXMediaRemovalMonitor::NewLC( aDrive,
       
    72                                                                  aFs,
       
    73                                                                  aObserver );
       
    74     CleanupStack::Pop( self );
       
    75     return self;
       
    76     }
       
    77 
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Two-Phased Constructor
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 CMPXMediaRemovalMonitor* CMPXMediaRemovalMonitor::NewLC
       
    84                ( TInt aDrive, RFs& aFs, MMPXSystemEventObserver& aObserver )
       
    85     {
       
    86     CMPXMediaRemovalMonitor* self = 
       
    87                           new( ELeave ) CMPXMediaRemovalMonitor( aDrive,
       
    88                                                                  aFs,
       
    89                                                                  aObserver );
       
    90     CleanupStack::PushL( self );
       
    91     self->ConstructL();
       
    92     return self;
       
    93     }
       
    94 
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // Destructor
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 CMPXMediaRemovalMonitor::~CMPXMediaRemovalMonitor()
       
   101     {
       
   102     Cancel();
       
   103     }
       
   104 
       
   105     
       
   106 // ---------------------------------------------------------------------------
       
   107 // Service the request
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 void CMPXMediaRemovalMonitor::RunL()
       
   111     {
       
   112     MPX_DEBUG1(_L("CMPXMediaRemovalMonitor::RunL <---"));
       
   113     
       
   114     // Re-subscribe to event.
       
   115     TNotifyType notType(ENotifyDisk);
       
   116     iFs.NotifyChange( notType, iStatus );
       
   117     SetActive();
       
   118     
       
   119     // Check state
       
   120     TDriveInfo drive;
       
   121 	User::LeaveIfError(iFs.Drive(drive, TInt(iDrive)));
       
   122 	
       
   123 	// Notify Observer
       
   124     switch(drive.iType)
       
   125         {
       
   126         case EMediaNotPresent:
       
   127             {
       
   128             if (!iDiskRemoved)
       
   129                 {
       
   130                 iObserver.HandleSystemEventL( EDiskRemovedEvent, iDrive );
       
   131                 }
       
   132             iDiskRemoved = ETrue;
       
   133             break;
       
   134             }
       
   135         default:
       
   136             {
       
   137             if ( iDiskRemoved &&
       
   138         		 ( drive.iMediaAtt & ( KMediaAttLockable|KMediaAttLocked|KMediaAttHasPassword ) ) != 
       
   139  				 ( KMediaAttLockable|KMediaAttLocked|KMediaAttHasPassword ) ) 
       
   140                 {
       
   141                 iObserver.HandleSystemEventL( EDiskInsertedEvent, iDrive );
       
   142                 iDiskRemoved = EFalse;
       
   143                 }
       
   144             break;
       
   145             }
       
   146         }
       
   147     
       
   148 
       
   149     MPX_DEBUG1(_L("CMPXMediaRemovalMonitor::RunL --->"));
       
   150     }
       
   151     
       
   152 // ---------------------------------------------------------------------------
       
   153 // Cancel NotifyChange request from file system
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 void CMPXMediaRemovalMonitor::DoCancel()
       
   157     {
       
   158     iFs.NotifyChangeCancel();
       
   159     }
       
   160     
       
   161 // ----------------------------------------------------------------------------
       
   162 // Handles a leave occurring in the request completion event handler RunL()
       
   163 // Don't care if client has a User::Leave() in RunL(), keep monitoring for events
       
   164 // ----------------------------------------------------------------------------
       
   165 //
       
   166 TInt CMPXMediaRemovalMonitor::RunError(TInt aError)
       
   167     {
       
   168     MPX_DEBUG2("CMPXMediaRemovalMonitor::RunError(%d)", aError );
       
   169     (void) aError;  // avoid compile warning in urel
       
   170     
       
   171     return KErrNone;
       
   172     }