harvester/monitorplugins/fileplugin/src/FolderRenamer.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32std.h>
       
    19 
       
    20 #include "FolderRenamer.h"
       
    21 #include "harvesterlog.h"
       
    22 #include "mdeharvestersession.h"
       
    23 #include "mdeharvestersession.h"
       
    24 #include "harvesterdata.h"
       
    25 #include "fileeventhandlerao.h"
       
    26 #include "harvestercommon.h"
       
    27 
       
    28 CRenameItem * CRenameItem::NewL(
       
    29 		const TDesC &aOldName, const TDesC &aNewName)
       
    30 	{	
       
    31 	CRenameItem *self = new (ELeave) CRenameItem();
       
    32 	CleanupStack::PushL(self);
       
    33 	self->ConstructL(aOldName,aNewName);
       
    34 	CleanupStack::Pop(self);
       
    35 	return self;
       
    36 	}
       
    37 	
       
    38 CRenameItem::~CRenameItem()
       
    39 	{
       
    40 	delete iOldName;
       
    41 	delete iNewName;
       
    42 	iFileEvents.ResetAndDestroy();
       
    43 	}
       
    44 	
       
    45 
       
    46 void CRenameItem::ConstructL(const TDesC &aOldName, const TDesC &aNewName)
       
    47 	{
       
    48 	iOldName = HBufC::NewL( aOldName.Length() );
       
    49 	iNewName = HBufC::NewL( aNewName.Length() );
       
    50 
       
    51 	// convert paths to lower case
       
    52 	// Note: CopyLC doesn't push anything to cleanup stack
       
    53 	iOldName->Des().CopyLC( aOldName );
       
    54 	iNewName->Des().CopyLC( aNewName );
       
    55 	}
       
    56 
       
    57 CRenameItem::CRenameItem() : iOldName( NULL ), iNewName( NULL )
       
    58 	{
       
    59 	//No implementation required
       
    60 	}
       
    61 
       
    62 void CRenameItem::AddFileEvent(TMdsFSPStatus &aEvent)
       
    63 	{
       
    64 	TMdsFSPStatus* event = NULL;
       
    65 	event = new TMdsFSPStatus(aEvent);
       
    66 	if (event)
       
    67 		{
       
    68 		iFileEvents.Append(event);
       
    69 		}
       
    70 	}
       
    71 
       
    72 void CRenameItem::HandleFileEventsL(CFileEventHandlerAO &aCFileEventHandlerAO)
       
    73 	{
       
    74 	TInt count = iFileEvents.Count();
       
    75 	for (TInt i = 0; i < count; i++)
       
    76 		{
       
    77 		aCFileEventHandlerAO.HandleNotificationL(* (iFileEvents[i]));
       
    78 		}
       
    79 	iFileEvents.ResetAndDestroy();
       
    80 	}
       
    81 
       
    82 TPtrC CRenameItem::OldName()
       
    83 	{
       
    84 	return iOldName->Des();
       
    85 	}
       
    86 
       
    87 
       
    88 TPtrC CRenameItem::NewName()
       
    89 	{
       
    90 	return iNewName->Des();
       
    91 	}
       
    92 
       
    93 
       
    94 CFolderRenamer * CFolderRenamer::NewL(CFileEventHandlerAO &aCFileEventHandlerAO)
       
    95 	{	
       
    96 	CFolderRenamer *self = new (ELeave) CFolderRenamer(aCFileEventHandlerAO);
       
    97 	CleanupStack::PushL(self);
       
    98 	self->ConstructL();
       
    99 	CleanupStack::Pop(self);
       
   100 	return self;
       
   101 	}
       
   102 
       
   103 
       
   104 CFolderRenamer::CFolderRenamer(CFileEventHandlerAO &aCFileEventHandlerAO) : 
       
   105 	CActive(KHarvesterPriorityMonitorPlugin),
       
   106 	iState(ERenameStateIdle),
       
   107 	iCFileEventHandlerAO(aCFileEventHandlerAO),
       
   108 	iIsRunning( EFalse )
       
   109 	{
       
   110 	}
       
   111 
       
   112 CFolderRenamer::~CFolderRenamer()
       
   113 	{
       
   114 	Cancel();
       
   115 	}
       
   116 
       
   117 void CFolderRenamer::ConstructL()
       
   118 	{
       
   119 	CActiveScheduler::Add(this);
       
   120 	}
       
   121 	
       
   122 void CFolderRenamer::DoCancel()
       
   123 	{	
       
   124 	}
       
   125 	
       
   126 
       
   127 void CFolderRenamer::RunL()
       
   128 	{
       
   129 	switch (iState)
       
   130 		{
       
   131 		case ERenameStateIdle:
       
   132 			{
       
   133 			if (iRenamedFolders.Count() == 0)
       
   134 				{
       
   135 				iIsRunning = EFalse;
       
   136 				return;
       
   137 				}
       
   138 			} //Flow to Renaming case -> no break
       
   139 			
       
   140 		case ERenameStateRenaming:
       
   141 			{
       
   142 			if (iRenamedFolders.Count())
       
   143 				{
       
   144 				iState = ERenameStateWaitingMdeRename;			
       
   145 				CRenameItem &currItem = *iRenamedFolders[0];				
       
   146 				CMdEHarvesterSession* session = iCFileEventHandlerAO.MdeHarvesterSession();
       
   147 				session->ChangePath( currItem.OldName(), currItem.NewName(), iStatus );
       
   148 				SetActive();
       
   149 				}
       
   150 			}
       
   151 			break;
       
   152 			
       
   153 		case ERenameStateWaitingMdeRename:
       
   154 			{
       
   155 			CRenameItem *currItem = iRenamedFolders[0];
       
   156 			iRenamedFolders.Remove(0);
       
   157 			TRAP_IGNORE(currItem->HandleFileEventsL(iCFileEventHandlerAO));
       
   158 			delete currItem;
       
   159 			SetNextRequest(ERenameStateIdle);
       
   160 			if (iRenamedFolders.Count() == 0)
       
   161 				{
       
   162 				iIsRunning = EFalse;
       
   163 				iRenamedFolders.Compress();
       
   164 				}
       
   165 			}
       
   166 			break;
       
   167 			
       
   168 		default:
       
   169 			{
       
   170 			WRITELOG("Unexpected state in FolderRenamer");
       
   171 			SetNextRequest(ERenameStateIdle);
       
   172 			}			
       
   173 			break;
       
   174 		}
       
   175 	}
       
   176 
       
   177 TInt CFolderRenamer::RunError( TInt aError )
       
   178     {
       
   179     WRITELOG("Unexpected error in FolderRenamer");
       
   180     SetNextRequest(ERenameStateIdle);
       
   181     return aError;
       
   182     }
       
   183 
       
   184 void CFolderRenamer::RenameL(const TDesC &aOldName, const TDesC &aNewName)
       
   185 	{
       
   186     //There comes multiple events for single rename, drop these 
       
   187 	TInt count = iRenamedFolders.Count();
       
   188 	for (TInt i = 0; i < count; i++)
       
   189 		{
       
   190 		if ( iRenamedFolders[i]->OldName().CompareF(aOldName) == 0 && 
       
   191 				iRenamedFolders[i]->NewName().CompareF(aNewName) == 0 )
       
   192 			{
       
   193 			return;
       
   194 			}
       
   195 		}
       
   196 	
       
   197 	CRenameItem *renameItem = CRenameItem::NewL(aOldName,aNewName);
       
   198 	iRenamedFolders.AppendL(renameItem);
       
   199 	iIsRunning = ETrue;
       
   200 	//First item --> Kickstart
       
   201 		
       
   202 	if (iState == ERenameStateIdle)
       
   203 		{
       
   204 		SetNextRequest(ERenameStateRenaming);
       
   205 		}
       
   206 	}
       
   207 
       
   208 
       
   209 void CFolderRenamer::SetNextRequest(TFolderRenameState aState)
       
   210 	{
       
   211 	iState = aState;	
       
   212 	if (!IsActive())
       
   213 		{
       
   214 		SetActive();				
       
   215 		TRequestStatus * status = &iStatus;
       
   216 		User::RequestComplete(status,KErrNone);	
       
   217 		}
       
   218 	}
       
   219 
       
   220 
       
   221 void CFolderRenamer::HandleFileEventL(TMdsFSPStatus &aEvent)
       
   222 	{
       
   223 	if (aEvent.iFileEventType == EMdsDirRenamed)
       
   224 		{
       
   225 		RenameL(aEvent.iFileName,aEvent.iNewFileName);
       
   226 		return;
       
   227 		}
       
   228 
       
   229 	//Check whether new folder name of the rename events matches to received path. If it matches
       
   230     //queue events until folder is renamed, otherwise send notification to filemonitor immediatedly
       
   231     //has to be started from last event!
       
   232 	if (iIsRunning)
       
   233 		{
       
   234 		const TInt count = iRenamedFolders.Count();	
       
   235 		for (TInt i = count-1; i >= 0; i--)
       
   236 			{
       
   237 			if (aEvent.iFileName.FindF(iRenamedFolders[i]->NewName()) != KErrNotFound)
       
   238 				{
       
   239 				iRenamedFolders[i]->AddFileEvent(aEvent);
       
   240 				return;
       
   241 				}
       
   242 			}		
       
   243 		}
       
   244 			
       
   245 	iCFileEventHandlerAO.HandleNotificationL(aEvent);
       
   246 	}
       
   247 
       
   248 void CFolderRenamer::HandleFileEventsL( CArrayFixSeg< TMdsFSPStatus >* aEvents )
       
   249     {
       
   250     const TInt count( aEvents->Count() );  // Can hold only delete events
       
   251     
       
   252     if( iIsRunning )
       
   253         {
       
   254         for( TInt i( 0 ); i < count; i++ )
       
   255             {
       
   256             //Check whether new folder name of the rename events matches to received path. If it matches
       
   257             //queue events until folder is renamed, otherwise send notification to filemonitor immediatedly
       
   258             //has to be started from last event!
       
   259             const TInt count = iRenamedFolders.Count();   
       
   260             for (TInt i = count-1; i >= 0; i--)
       
   261                 {
       
   262                 if ((*aEvents)[i].iFileName.FindF(iRenamedFolders[i]->NewName()) != KErrNotFound)
       
   263                     {
       
   264                     iRenamedFolders[i]->AddFileEvent((*aEvents)[i]);
       
   265                     aEvents->Delete( i );
       
   266                     break;
       
   267                     } 
       
   268                 }
       
   269             }
       
   270         aEvents->Compress();
       
   271         }
       
   272     
       
   273     if( aEvents->Count() )
       
   274         {
       
   275         iCFileEventHandlerAO.HandleMultideletionL(aEvents);
       
   276         }
       
   277     }
       
   278 	
       
   279 
       
   280