internetradio2.0/uisrc/irdiskspacewatcher.cpp
changeset 3 ee64f059b8e1
parent 2 2e1adbfc62af
child 4 3f2d53f144fe
child 5 0930554dc389
equal deleted inserted replaced
2:2e1adbfc62af 3:ee64f059b8e1
     1 /*
       
     2 * Copyright (c) 2004 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:  Performs disk space watching.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include    <f32file.h>
       
    20 
       
    21 #include    "irdebug.h"
       
    22 #include    "irdiskspacewatcher.h"
       
    23 #include    "irdiskspacesession.h"
       
    24 
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // C++ default constructor can NOT contain any code, that might leave.
       
    28 // ---------------------------------------------------------------------------
       
    29 //
       
    30 CIRDiskSpaceWatcher::CIRDiskSpaceWatcher( const TInt64& aCriticalLevel, 
       
    31         MIRDiskSpaceWatcherObserver& aDiskSpaceWatcherObserver ) :
       
    32         iCriticalLevel( aCriticalLevel ), 
       
    33         iObserver ( aDiskSpaceWatcherObserver )
       
    34     {
       
    35     }
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // EPOC default constructor can leave.
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 void CIRDiskSpaceWatcher::ConstructL()
       
    42     {
       
    43     User::LeaveIfError(iFs.Connect());
       
    44     }
       
    45 
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // Two-phased constructor.
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CIRDiskSpaceWatcher* CIRDiskSpaceWatcher::NewL( const TInt64& aCriticalLevel, 
       
    52         MIRDiskSpaceWatcherObserver& aDiskSpaceWatcherObserver )
       
    53     
       
    54     {
       
    55     CIRDiskSpaceWatcher* self = new( ELeave ) CIRDiskSpaceWatcher( aCriticalLevel, 
       
    56         aDiskSpaceWatcherObserver );
       
    57     
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL();
       
    60     CleanupStack::Pop(self);
       
    61 
       
    62     return self;
       
    63     }
       
    64 
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // Destructor
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CIRDiskSpaceWatcher::~CIRDiskSpaceWatcher()
       
    71     {   
       
    72     iDiskSpaceSessions.ResetAndDestroy();
       
    73     iDiskSpaceSessions.Close();
       
    74     iFs.Close();
       
    75     }
       
    76 
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // Returns ETrue if below the critical level in defined disk, otherwise EFalse
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 TBool CIRDiskSpaceWatcher::IsBelowCriticalLevel( 
       
    83         const TDriveNumber& aDriveNumber ) const
       
    84     {
       
    85     TBool returnValue = EFalse; 
       
    86  
       
    87     if ( iDiskSpaceSessions.Count() > 0 )
       
    88         {        
       
    89         CIRDiskSpaceSession* session = iDiskSpaceSessions[0];
       
    90         returnValue = session->IsBelowCriticalLevel( aDriveNumber );
       
    91         }
       
    92 
       
    93     return returnValue;
       
    94     }
       
    95 
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // CIRDiskSpaceWatcher::StartObservingDriveL
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 void CIRDiskSpaceWatcher::StartObservingDriveL( 
       
   102         const TDriveNumber& aDriveNumber )
       
   103     {
       
   104     TBool found = EFalse;
       
   105 
       
   106     for ( TInt i = 0 ; i < iDiskSpaceSessions.Count() ; i++ )
       
   107         {
       
   108         CIRDiskSpaceSession* session = iDiskSpaceSessions[i];
       
   109         if ( session->DriveNumber() == aDriveNumber )
       
   110             {
       
   111             session->SetObserverCount( session->ObserverCount() + 1 );
       
   112             found = ETrue;
       
   113             /*lint -save -e960 (Note -- Violates MISRA Required Rule 58, non-switch break used)*/
       
   114             break;
       
   115             /*lint -restore*/
       
   116             }
       
   117         }
       
   118     if ( !found )
       
   119         {
       
   120         CIRDiskSpaceSession* session = CIRDiskSpaceSession::NewL( *this, 
       
   121             aDriveNumber, iCriticalLevel, iFs );
       
   122         session->SetObserverCount( 1 );
       
   123         iDiskSpaceSessions.Append( session );
       
   124         }
       
   125     }
       
   126 
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CIRDiskSpaceWatcher::StopObservingDrive
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 void CIRDiskSpaceWatcher::StopObservingDrive( 
       
   133         const TDriveNumber& aDriveNumber )     
       
   134     {
       
   135     for ( TInt i = 0 ; i < iDiskSpaceSessions.Count() ; i++ )
       
   136         {
       
   137         CIRDiskSpaceSession* session = iDiskSpaceSessions[i];
       
   138         if ( session->DriveNumber() == aDriveNumber )
       
   139             {
       
   140             session->SetObserverCount( session->ObserverCount() - 1 );
       
   141             if ( session->ObserverCount() == 0 )
       
   142                 {
       
   143                 iDiskSpaceSessions.Remove( i );
       
   144                 delete session;
       
   145                 session = NULL;
       
   146                 }
       
   147             /*lint -save -e960 (Note -- Violates MISRA Required Rule 58, non-switch break used)*/
       
   148             break;
       
   149             /*lint -restore*/
       
   150             }
       
   151         }
       
   152     
       
   153     }
       
   154 
       
   155     
       
   156 // ---------------------------------------------------------------------------
       
   157 // From class MIRDiskSpaceWatcherObserver.
       
   158 // CIRDiskSpaceWatcher::NotifyLowDiskSpaceL
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CIRDiskSpaceWatcher::NotifyLowDiskSpaceL()
       
   162     {
       
   163     IRRDEBUG2("CIRDiskSpaceWatcher::NotifyLowDiskSpaceL - Phone LOW MEMORY detected", KNullDesC);
       
   164     iObserver.NotifyLowDiskSpaceL();
       
   165     }