qtinternetradio/irqsystemeventhandler/src/irdiskspaceobserver.cpp
changeset 3 ee64f059b8e1
child 5 0930554dc389
equal deleted inserted replaced
2:2e1adbfc62af 3:ee64f059b8e1
       
     1 /*
       
     2 * Copyright (c) 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 "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:  Contains logic for watching certain disk's space
       
    15 *
       
    16 */
       
    17 #include <pathinfo.h>
       
    18 #include "irdiskspaceobserver.h"
       
    19 #include "irdiskspaceobserverinterface.h"
       
    20 
       
    21 const TDriveNumber KQtIRDefaultDrive = EDriveC;
       
    22 
       
    23 const TUint KDriveAValue = 'A';
       
    24 const TUint KDriveZValue = 'Z';
       
    25 const TUint KDriveaValue = 'a';
       
    26 const TUint KDrivezValue = 'z';
       
    27 
       
    28 static TDriveNumber phoneMemRootDrive();
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // Constructor
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 CIRDiskSpaceObserver* CIRDiskSpaceObserver::NewL(MIRDiskSpaceObserverInterface* aObserver)
       
    35 {
       
    36     CIRDiskSpaceObserver* self = new (ELeave) CIRDiskSpaceObserver(aObserver);
       
    37 
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop(self);
       
    41     return self;
       
    42     
       
    43 }
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // Second stage constructor
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 void CIRDiskSpaceObserver::ConstructL()
       
    50 {
       
    51     CActiveScheduler::Add(this);
       
    52     User::LeaveIfError(iFs.Connect());
       
    53 }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // C++ Constructor
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CIRDiskSpaceObserver::CIRDiskSpaceObserver( MIRDiskSpaceObserverInterface* aObserver )
       
    60                               : CActive( CActive::EPriorityStandard ),       
       
    61                                 iObserver( aObserver ),
       
    62                                 iCriticalLevel(0)
       
    63 {
       
    64 }
       
    65             
       
    66 // ---------------------------------------------------------------------------
       
    67 // Destructor
       
    68 // ---------------------------------------------------------------------------
       
    69 //            
       
    70 CIRDiskSpaceObserver::~CIRDiskSpaceObserver()
       
    71 {
       
    72     Cancel();
       
    73     iFs.Close();    
       
    74 }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // Critical Level
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 TInt64 CIRDiskSpaceObserver::CriticalLevel() const
       
    81 {
       
    82     if(IsActive())
       
    83     {
       
    84         return iCriticalLevel;
       
    85     }
       
    86     else
       
    87     {
       
    88         return -1;
       
    89     }    
       
    90 }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // Start Monitoring
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void CIRDiskSpaceObserver::Start(const TInt64 aCriticalLevel)
       
    97 {
       
    98     Cancel();
       
    99     iCriticalLevel = aCriticalLevel;
       
   100     Run();
       
   101 }
       
   102 
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // From class CActive. 
       
   106 // Cancels the pending request.
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 void CIRDiskSpaceObserver::DoCancel() 
       
   110 {    
       
   111     iFs.NotifyDiskSpaceCancel(iStatus);
       
   112 }
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // From class CActive. 
       
   117 // Invoked when the observed disk's free disk space has run below
       
   118 // the supplied critical level.
       
   119 // Notifies the observer and regenerates the request to RFs' NotifyDiskSpace.
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 void CIRDiskSpaceObserver::RunL()
       
   123 {       
       
   124     Run();   
       
   125 }
       
   126     
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // Returns true if below the critical level in defined disk, otherwise false
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 TBool CIRDiskSpaceObserver::IsBelowCriticalLevel(const TInt64 aCriticalLevel) const
       
   133 {
       
   134     TVolumeInfo volInfo;
       
   135     if(KErrNone == iFs.Volume(volInfo, KQtIRDefaultDrive))
       
   136     {
       
   137         if(volInfo.iFree < aCriticalLevel)
       
   138         {
       
   139             return ETrue;
       
   140         }
       
   141     }
       
   142     
       
   143     return EFalse;
       
   144 }
       
   145 
       
   146 void CIRDiskSpaceObserver::Run()
       
   147 {
       
   148     if( iObserver && IsBelowCriticalLevel(iCriticalLevel))
       
   149     {
       
   150         iObserver->notifyLowDiskSpace((qint64)iCriticalLevel);
       
   151     }
       
   152         
       
   153     iFs.NotifyDiskSpace(iCriticalLevel, phoneMemRootDrive(), iStatus);
       
   154     SetActive(); 
       
   155 }
       
   156 
       
   157 TDriveNumber phoneMemRootDrive()
       
   158 {
       
   159     TParsePtrC phoneMemoryRootPath( PathInfo::PhoneMemoryRootPath() );
       
   160     TPtrC drive( phoneMemoryRootPath.Drive() );
       
   161     TUint driveValue = drive[0];
       
   162     TUint delta = 0;
       
   163     if( driveValue >= KDriveAValue &&
       
   164         driveValue <= KDriveZValue)
       
   165     {
       
   166         delta = driveValue - KDriveAValue;
       
   167     }
       
   168     else if( driveValue >= KDriveaValue &&
       
   169             driveValue <= KDrivezValue)
       
   170     {
       
   171         delta = driveValue - KDriveaValue;
       
   172     }
       
   173     else
       
   174     {
       
   175         return KQtIRDefaultDrive;
       
   176     }
       
   177     
       
   178     return static_cast<TDriveNumber>(EDriveA + delta);
       
   179 }