harvester/monitorplugins/mmcplugin/src/mmcmonitorao.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2006-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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <driveinfo.h>
       
    19 
       
    20 #ifdef __WINSCW__
       
    21 #include <pathinfo.h>
       
    22 #endif
       
    23 
       
    24 #include "harvesterlog.h"
       
    25 #include "harvestercommon.h"
       
    26 #include "mmcmonitorao.h"
       
    27 #include "mmcmonitorplugin.h"
       
    28 #include "fsutil.h"
       
    29 
       
    30 CMMCMonitorAO* CMMCMonitorAO::NewL()
       
    31 	{
       
    32 	WRITELOG( "CMMCMonitorAO::NewL" ); // DEBUG INFO
       
    33 	
       
    34 	CMMCMonitorAO* self = new (ELeave) CMMCMonitorAO();
       
    35 	CleanupStack::PushL( self );
       
    36 	self->ConstructL();
       
    37 	CleanupStack::Pop( self );
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 void CMMCMonitorAO::ConstructL()
       
    42 	{
       
    43 	WRITELOG( "CMMCMonitorAO::ConstructL" ); // DEBUG INFO
       
    44 	
       
    45 	User::LeaveIfError( iFs.Connect() );
       
    46 	CActiveScheduler::Add( this );
       
    47 	for( TInt i=0; i<KMaxDrives; i++ )
       
    48 	    {
       
    49 	    iMediaIdList[i] = 0;
       
    50 	    }
       
    51 	}
       
    52 
       
    53 CMMCMonitorAO::~CMMCMonitorAO()
       
    54 	{
       
    55 	WRITELOG( "CMMCMonitorAO::~CMMCMonitorAO" ); // DEBUG INFO
       
    56 	
       
    57 	StopMonitoring();
       
    58     iFs.Close();
       
    59 	}
       
    60 
       
    61 TBool CMMCMonitorAO::StartMonitoring( MMMCMonitorObserver& aObserver, RArray<TMdEMediaInfo>& aMedias )
       
    62 	{
       
    63 	WRITELOG( "CMMCMonitorAO::StartMonitoring" ); // DEBUG INFO
       
    64 
       
    65 	iObserver = &aObserver;
       
    66 	
       
    67     BuildDriveList();
       
    68     
       
    69     for ( TInt i = 0; i < aMedias.Count(); i++ )
       
    70     	{
       
    71     	TInt drive(0);
       
    72     	TInt err = iFs.CharToDrive( aMedias[i].iDrive, drive );
       
    73     	if ( err == KErrNone )
       
    74     		{
       
    75     		WRITELOG2( "CMMCMonitorAO::StartMonitoring - set drive %d mediaid to %d ", drive, aMedias[i].iMediaId );
       
    76     		iDriveList[drive] = 1; // fake value for startup (not KDriveAbsent)
       
    77         	iMediaIdList[drive] = aMedias[i].iMediaId;
       
    78     		}
       
    79     	}
       
    80     
       
    81 	iPreviousDriveList.Zero();
       
    82 	iPreviousDriveList.Copy( iDriveList );
       
    83 
       
    84 #ifdef _DEBUG
       
    85 	PrintAllLists();
       
    86 #endif
       
    87 
       
    88 	CompareDriveLists();
       
    89 
       
    90 	StartNotify();
       
    91 	
       
    92 	return ETrue;
       
    93 	}
       
    94 
       
    95 void CMMCMonitorAO::StartNotify()
       
    96 	{
       
    97 	WRITELOG( "CMMCMonitorAO::StartNotify with ENotifyEntry" ); // DEBUG INFO
       
    98 	
       
    99 	if( !IsActive() )
       
   100 		{
       
   101 		iFs.NotifyChange( ENotifyEntry, iStatus ); 
       
   102 		SetActive();                               
       
   103 		}
       
   104 	}
       
   105 
       
   106 TBool CMMCMonitorAO::StopMonitoring()
       
   107 	{
       
   108 	WRITELOG( "CMMCMonitorAO::StopNotify" ); // DEBUG INFO
       
   109 	
       
   110 	Cancel();
       
   111 	return ETrue;
       
   112 	}
       
   113 
       
   114 TBool CMMCMonitorAO::Resume()
       
   115 	{
       
   116 	WRITELOG( "CMMCMonitorAO::Resume" ); // DEBUG INFO
       
   117 
       
   118 	iPreviousDriveList.Zero();
       
   119 	iPreviousDriveList.Copy( iDriveList );
       
   120 
       
   121 	BuildDriveList();
       
   122 	CompareDriveLists();
       
   123 	StartNotify();
       
   124 	
       
   125 	return ETrue;
       
   126 	}
       
   127 
       
   128 TBool CMMCMonitorAO::Pause()
       
   129 	{
       
   130 	WRITELOG( "CMMCMonitorAO::Pause" ); // DEBUG INFO
       
   131 	
       
   132 	return StopMonitoring();
       
   133 	}
       
   134 
       
   135 void CMMCMonitorAO::RunL()
       
   136 	{
       
   137 #ifdef _DEBUG
       
   138 	WRITELOG1( "CMMCMonitorAO::RunL iStatus: %d", iStatus.Int() ); // DEBUG INFO
       
   139 #endif
       
   140 
       
   141 	if ( iStatus.Int() != KErrNone )
       
   142 		{
       
   143 		StartNotify();
       
   144 		return;
       
   145 		}
       
   146 
       
   147 	iPreviousDriveList.Zero();
       
   148 	iPreviousDriveList.Copy( iDriveList );
       
   149 
       
   150 	BuildDriveList();
       
   151 
       
   152 #ifdef _DEBUG
       
   153 	PrintAllLists();
       
   154 #endif
       
   155 
       
   156 	CompareDriveLists();
       
   157 
       
   158 #ifdef _DEBUG
       
   159 	PrintAllLists();
       
   160 #endif
       
   161 
       
   162 	StartNotify();
       
   163 	}
       
   164 
       
   165 #ifdef _DEBUG
       
   166 TInt CMMCMonitorAO::RunError( TInt aError )
       
   167 #else
       
   168 TInt CMMCMonitorAO::RunError( TInt )
       
   169 #endif
       
   170 	{
       
   171 	WRITELOG1( "CMMCMonitorAO::RunError with error code: %d", aError ); // DEBUG INFO
       
   172 	
       
   173 	StartNotify();
       
   174 	return KErrNone;
       
   175 	}
       
   176 
       
   177 void CMMCMonitorAO::DoCancel()
       
   178 	{
       
   179 	WRITELOG( "CMMCMonitorAO::DoCancel" ); // DEBUG INFO
       
   180 	
       
   181 	iFs.NotifyChangeCancel( iStatus );
       
   182 	}
       
   183 
       
   184 CMMCMonitorAO::CMMCMonitorAO() : CActive( KHarvesterPriorityMonitorPlugin ),
       
   185     iObserver( NULL )
       
   186 	{
       
   187 	}
       
   188 
       
   189 void CMMCMonitorAO::BuildDriveList()
       
   190 	{
       
   191 	WRITELOG( "CMMCMonitorAO::BuildDriveList" ); // DEBUG INFO
       
   192 
       
   193 	iDriveList.Zero();
       
   194     TInt numOfElements( 0 );
       
   195     TInt err = DriveInfo::GetUserVisibleDrives( 
       
   196     		iFs, iDriveList, numOfElements, 
       
   197 			KDriveAttExclude | KDriveAttRemote | KDriveAttRom );
       
   198 
       
   199 #ifdef __WINSCW__
       
   200     TFileName systemPath = PathInfo::GetPath( PathInfo::EPhoneMemoryRootPath );
       
   201     TInt systemDriveNum( -1 );
       
   202     iFs.CharToDrive( systemPath[0], systemDriveNum );
       
   203 #endif
       
   204     
       
   205 	if ( err != KErrNone )
       
   206 		{
       
   207 		WRITELOG1( "CMMCMonitorAO::BuildDriveList with error code: %d", err ); // DEBUG INFO
       
   208 		
       
   209 		return;
       
   210 		}
       
   211 
       
   212 	TDriveInfo driveInfo;
       
   213 	for ( TInt i=0; i < KMaxDrives; i++ )
       
   214 		{
       
   215 	
       
   216 #ifdef __WINSCW__
       
   217         if ( i == systemDriveNum )
       
   218             {
       
   219             iDriveList[i] = 0;
       
   220             continue;
       
   221             }
       
   222 #endif
       
   223 	
       
   224 		if ( iDriveList[i] == KDriveAbsent )
       
   225 			{
       
   226 			continue;
       
   227 			}
       
   228 		err = iFs.Drive( driveInfo, i );
       
   229 		if ( err != KErrNone )
       
   230 			{
       
   231 			WRITELOG1( "CMMCMonitorAO::BuildDriveList with error code: %d", err ); // DEBUG INFO
       
   232 			}
       
   233 
       
   234 		TUint driveStatus;
       
   235 		err = DriveInfo::GetDriveStatus( iFs, i, driveStatus );
       
   236 		if ( err != KErrNone )
       
   237 			{
       
   238 			WRITELOG1( "CMMCMonitorAO::BuildDriveList with error code2: %d", err ); // DEBUG INFO
       
   239 			}
       
   240 
       
   241 #ifdef _DEBUG
       
   242 		if ( driveStatus & DriveInfo::EDriveInternal )
       
   243 			{
       
   244 			WRITELOG1("Drive %d is internal",i);
       
   245 			}		
       
   246 		
       
   247 		WRITELOG2("Drive %d type %d",i,driveInfo.iType);
       
   248 #endif
       
   249 		
       
   250 		if ( !( driveStatus & DriveInfo::EDriveRemovable ) )
       
   251 			{
       
   252 			WRITELOG1("Drive %d is not removable",i);
       
   253 			// check if driver is internal hard disk
       
   254 			if ( driveInfo.iType != EMediaHardDisk )
       
   255 				{
       
   256 				iDriveList[i] = 0;
       
   257 				continue;
       
   258 				}
       
   259 			else
       
   260 				{
       
   261 				WRITELOG1("Drive %d is hard disk",i);
       
   262 				}
       
   263 			}
       
   264 		
       
   265 		if( driveStatus & DriveInfo::EDriveUsbMemory )
       
   266 		    {
       
   267             iDriveList[i] = 0;
       
   268             continue;
       
   269 		    }
       
   270 
       
   271 		if ( driveInfo.iType == EMediaNotPresent )
       
   272 			{
       
   273 			WRITELOG1("Drive %d is not present",i);
       
   274 			iDriveList[i] = KDriveAbsent;
       
   275 			continue;
       
   276 			}
       
   277 
       
   278 		const TUint32 mediaId = FSUtil::MediaID(iFs, i);
       
   279 		if ( mediaId == 0 )
       
   280 			{
       
   281 			WRITELOG1("Drive %d: MediaId is 0",i);
       
   282 			iDriveList[i] = KDriveAbsent;
       
   283 			}
       
   284 		}
       
   285 	}
       
   286 
       
   287 void CMMCMonitorAO::CompareDriveLists()
       
   288 	{
       
   289 	WRITELOG( "CMMCMonitorAO::CompareDriveLists" ); // DEBUG INFO
       
   290 
       
   291 	for ( TInt i = 0; i < KMaxDrives; i++ )
       
   292 		{
       
   293 		if ( iDriveList[i] == KDriveAbsent && iPreviousDriveList[i] == KDriveAbsent )
       
   294 			{	
       
   295 			continue;
       
   296 			}
       
   297 
       
   298 		TChar chr;
       
   299 		iFs.DriveToChar( i, chr );
       
   300 		const TUint32 mediaId = FSUtil::MediaID(iFs, i);
       
   301 
       
   302 		if ( iDriveList[i] > KDriveAbsent && iPreviousDriveList[i] > KDriveAbsent )
       
   303 			{
       
   304 			if ( iMediaIdList[i] != mediaId )
       
   305 				{
       
   306 				WRITELOG3( "CMMCMonitorAO::CompareDriveLists media changed %d, old=%d, new=%d", i, iMediaIdList[i], mediaId ); // DEBUG INFO
       
   307 				// skip mount events if mediaId is 0
       
   308 				if ( iMediaIdList[i] != 0 )
       
   309 					{
       
   310 					iObserver->MountEvent( chr, iMediaIdList[i], EDismounted );
       
   311 					}
       
   312 				if ( mediaId != 0 )
       
   313 					{
       
   314 					iObserver->MountEvent( chr, mediaId, EMounted );
       
   315 					}
       
   316 				iMediaIdList[i] = mediaId;
       
   317 
       
   318 				}
       
   319 			continue;
       
   320 			}
       
   321 
       
   322 		if ( iDriveList[i] == KDriveAbsent )
       
   323 			{
       
   324 			WRITELOG1( "CMMCMonitorAO::CompareDriveLists Dismounted %d", i ); // DEBUG INFO
       
   325 			if ( mediaId )
       
   326 				{
       
   327 				iObserver->MountEvent( chr, mediaId, EDismounted );
       
   328 				}
       
   329 			else if ( iMediaIdList[i] )
       
   330 				{
       
   331 				iObserver->MountEvent( chr, iMediaIdList[i], EDismounted );
       
   332 				}
       
   333 			iMediaIdList[i] = 0;
       
   334 			}
       
   335 
       
   336 		if ( iPreviousDriveList[i] == KDriveAbsent )
       
   337 			{
       
   338 			WRITELOG1( "CMMCMonitorAO::CompareDriveLists Mounted %d", i ); // DEBUG INFO
       
   339 			iObserver->MountEvent( chr, mediaId, EMounted );
       
   340 			iMediaIdList[i] = mediaId;
       
   341 			}
       
   342 		}
       
   343 	}
       
   344 	
       
   345 #ifdef _DEBUG
       
   346 void CMMCMonitorAO::PrintAllLists()
       
   347 	{
       
   348     WRITELOG( "CMMCMonitorAO::PrintAllLists()" );
       
   349 
       
   350 	for ( TInt i=0; i < KMaxDrives; i++ )
       
   351 		{
       
   352 		if ( !(iPreviousDriveList[i] == 0 && iDriveList[i] == 0 && iMediaIdList[i] == 0) )
       
   353 			{
       
   354 		    TInt32 drive;
       
   355 			drive = iPreviousDriveList[i];
       
   356 		    WRITELOG1( "CMMCMonitorAO::PrintAllLists() - Previous drive element: %d", drive );
       
   357 			drive = iDriveList[i];
       
   358 		    WRITELOG1( "CMMCMonitorAO::PrintAllLists() - Current drive element : %d", drive );
       
   359 			drive = iMediaIdList[i];
       
   360 		    WRITELOG1( "CMMCMonitorAO::PrintAllLists() - Current MediaId       : %d", drive );
       
   361 			}
       
   362 		}
       
   363 	}
       
   364 #endif