metadataengine/server/src/mdsdiskspacenotifier.cpp
changeset 0 c53acadfccc6
child 6 646a02f170b9
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2007-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:  This is disk space notifier for Metadata server.*
       
    15 */
       
    16 
       
    17 // INCLUDE FILES
       
    18 #include "mdsdiskspacenotifier.h"
       
    19 
       
    20 
       
    21 CMdSDiskSpaceNotifierAO* CMdSDiskSpaceNotifierAO::NewL(
       
    22 	MMdSDiskSpaceNotifierObserver& aObserver, TInt64 aThreshold, const TDesC& aFilename)
       
    23 	{
       
    24 	CMdSDiskSpaceNotifierAO* self = 
       
    25 		CMdSDiskSpaceNotifierAO::NewLC( aObserver, aThreshold, aFilename );
       
    26 	CleanupStack::Pop( self );
       
    27 	return self;
       
    28 	}
       
    29 
       
    30 CMdSDiskSpaceNotifierAO* CMdSDiskSpaceNotifierAO::NewLC(
       
    31 	MMdSDiskSpaceNotifierObserver& aObserver, TInt64 aThreshold, const TDesC& aFilename)
       
    32 	{
       
    33 	TDriveNumber driveNumber = GetDriveNumberL( aFilename );
       
    34 
       
    35     CMdSDiskSpaceNotifierAO* self = 
       
    36     	new ( ELeave ) CMdSDiskSpaceNotifierAO( aObserver, aThreshold, driveNumber );
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     return self;
       
    40 	}
       
    41 
       
    42 CMdSDiskSpaceNotifierAO::~CMdSDiskSpaceNotifierAO()
       
    43 	{
       
    44 	Cancel();
       
    45 
       
    46 	iFileServerSession.Close();
       
    47 	}
       
    48 
       
    49 void CMdSDiskSpaceNotifierAO::RunL()
       
    50 	{	
       
    51 	TVolumeInfo volumeInfo;
       
    52 
       
    53 	if ( iState == CMdSDiskSpaceNotifierAO::ENormal )
       
    54 		{
       
    55 		TInt status = iStatus.Int();
       
    56 		
       
    57 		switch( status )
       
    58 			{
       
    59 			case KErrNone:
       
    60 				iFileServerSession.Volume( volumeInfo, iDrive );
       
    61 				
       
    62 				// Check if free space is less than threshold level
       
    63 				if( volumeInfo.iFree < iThreshold )
       
    64 					{
       
    65 					iObserver.HandleDiskSpaceNotificationL( MMdSDiskSpaceNotifierObserver::ELess );
       
    66 					iDiskFull = ETrue;
       
    67 					iState = EIterate;
       
    68 					iIterationCount = 0;
       
    69 					SetActive();
       
    70 					TRequestStatus* status = &iStatus;
       
    71 					User::RequestComplete( status, KErrNone );
       
    72 					return;
       
    73 					}
       
    74 				else
       
    75 					{
       
    76 					iObserver.HandleDiskSpaceNotificationL( MMdSDiskSpaceNotifierObserver::EMore );
       
    77 					iDiskFull = EFalse;
       
    78 					}
       
    79 				StartNotifier();
       
    80 				break;
       
    81 
       
    82 			case KErrArgument:
       
    83 				User::Leave( status );
       
    84 				break;
       
    85 			default:
       
    86 				break;
       
    87 			}
       
    88 		}
       
    89 	else if ( iState == CMdSDiskSpaceNotifierAO::EIterate )
       
    90 		{
       
    91 		const TInt KMaxIterations = 10;
       
    92 		
       
    93 		iFileServerSession.Volume( volumeInfo, iDrive );
       
    94 		if ( volumeInfo.iFree < iThreshold )
       
    95 			{
       
    96 			iObserver.HandleDiskSpaceNotificationL( MMdSDiskSpaceNotifierObserver::ELess );
       
    97 			++iIterationCount;
       
    98 			if ( iIterationCount < KMaxIterations )
       
    99 				{
       
   100 				SetActive();
       
   101 				TRequestStatus* status = &iStatus;
       
   102 				User::RequestComplete( status, KErrNone );
       
   103 				return;
       
   104 				}
       
   105 			else
       
   106 				{
       
   107 				iFileServerSession.Volume( volumeInfo, iDrive );
       
   108 				if ( volumeInfo.iFree >= iThreshold )
       
   109 					{
       
   110 					iDiskFull = EFalse;
       
   111 					}
       
   112 				}
       
   113 			}
       
   114 		else
       
   115 			{
       
   116 			iDiskFull = EFalse;
       
   117 			}
       
   118 		iState = ENormal;
       
   119 		iIterationCount = 0;
       
   120 		StartNotifier();			
       
   121 		}
       
   122 	else
       
   123 		{
       
   124 		User::Leave( KErrGeneral );
       
   125 		}
       
   126 	}
       
   127 
       
   128 TInt CMdSDiskSpaceNotifierAO::RunError(TInt aError)
       
   129 	{
       
   130 	iObserver.HandleDiskSpaceError( aError );
       
   131 	
       
   132 	return KErrNone;
       
   133 	}
       
   134 
       
   135 void CMdSDiskSpaceNotifierAO::DoCancel()
       
   136 	{
       
   137 	if( IsActive() )
       
   138 		{	
       
   139 		iFileServerSession.NotifyDiskSpaceCancel();
       
   140 		}
       
   141 	}
       
   142 
       
   143 CMdSDiskSpaceNotifierAO::CMdSDiskSpaceNotifierAO(
       
   144 	MMdSDiskSpaceNotifierObserver& aObserver, TInt64 aThreshold, TDriveNumber aDrive)
       
   145 	: CActive( CActive::EPriorityStandard ), 
       
   146 	iObserver( aObserver ), iThreshold( aThreshold ), iDrive( aDrive ), iState( CMdSDiskSpaceNotifierAO::ENormal ), iDiskFull( EFalse )
       
   147 	{
       
   148 	CActiveScheduler::Add( this );
       
   149 	}
       
   150 
       
   151 void CMdSDiskSpaceNotifierAO::ConstructL()
       
   152 	{	
       
   153 	TInt KMessageSlotCount = 2; // slots for NotifyDiskSpace and NotifyDiskSpaceCancel
       
   154 
       
   155 	User::LeaveIfError( iFileServerSession.Connect( KMessageSlotCount ) );
       
   156 	
       
   157 	TVolumeInfo volumeInfo;
       
   158 	iFileServerSession.Volume( volumeInfo, iDrive );	
       
   159 	if ( volumeInfo.iFree < iThreshold )
       
   160 		{
       
   161 		iDiskFull = ETrue;
       
   162 		}
       
   163 
       
   164 	StartNotifier();
       
   165 	}
       
   166 
       
   167 void CMdSDiskSpaceNotifierAO::StartNotifier()
       
   168 	{	
       
   169 	iFileServerSession.NotifyDiskSpace( iThreshold, iDrive, iStatus );
       
   170 	
       
   171 	SetActive();
       
   172 	}
       
   173 
       
   174 TDriveNumber CMdSDiskSpaceNotifierAO::GetDriveNumberL( const TDesC& aFilename )
       
   175 	{
       
   176 	TLex driveParser( aFilename );
       
   177 	
       
   178 	TChar driveChar = driveParser.Get();
       
   179 
       
   180 	if( 0 == driveChar || TChar( ':' ) != driveParser.Peek() )
       
   181 		{
       
   182 		User::Leave( KErrArgument );
       
   183 		}
       
   184 		
       
   185 	TInt driveNumber;
       
   186 	
       
   187 	RFs::CharToDrive( driveChar, driveNumber );
       
   188 	
       
   189 	return (TDriveNumber)driveNumber;
       
   190 	}
       
   191 
       
   192 TBool CMdSDiskSpaceNotifierAO::DiskFull() const
       
   193 	{
       
   194 	return iDiskFull;
       
   195 	}