mmappcomponents/mmmtpdataprovider/mmmtpdprequestprocessor/src/crenameobject.cpp
changeset 0 a2952bb97e68
child 9 bee149131e4b
child 25 d881023c13eb
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     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:  Rename Object
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <f32file.h>
       
    20 
       
    21 #include <mtp/mmtpdataproviderframework.h>
       
    22 #include <mtp/mmtpobjectmgr.h>
       
    23 #include <mtp/cmtpobjectmetadata.h>
       
    24 #include <mtp/tmtptypeuint32.h>
       
    25 
       
    26 #include "crenameobject.h"
       
    27 #include "mmmtpdplogger.h"
       
    28 #include "cmmmtpdpmetadataaccesswrapper.h"
       
    29 #include "crequestprocessor.h" // refer to KMmMtpRArrayGranularity
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CRenameObject::NewL
       
    33 // Two phase constructor
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CRenameObject* CRenameObject::NewL( MMTPDataProviderFramework& aFramework,
       
    37         CMmMtpDpMetadataAccessWrapper& aWrapper )
       
    38     {
       
    39     PRINT( _L( "MM MTP => CRenameObject::NewL" ) );
       
    40 
       
    41     CRenameObject* self = new ( ELeave ) CRenameObject( aFramework, aWrapper );
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop( self );
       
    45 
       
    46     PRINT( _L( "MM MTP <= CRenameObject::NewL" ) );
       
    47     return self;
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CRenameObject::CRenameObject
       
    52 // Standard C++ Constructor
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 EXPORT_C CRenameObject::CRenameObject( MMTPDataProviderFramework& aFramework,
       
    56          CMmMtpDpMetadataAccessWrapper& aWrapper ) :
       
    57     CActive( EPriorityStandard ),
       
    58     iFramework( aFramework ),
       
    59     iObjectHandles( KMmMtpRArrayGranularity ),
       
    60     iWrapper ( aWrapper )
       
    61     {
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CRenameObject::~CRenameObject
       
    66 // destructor
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 EXPORT_C CRenameObject::~CRenameObject()
       
    70     {
       
    71     Cancel();
       
    72     iObjectHandles.Close();
       
    73     delete iObjectInfo;
       
    74     iOldFileName.Close();
       
    75     iRightPartName.Close();
       
    76     iFileName.Close();
       
    77     iOldFolderFullName.Close();
       
    78     iNewFolderName.Close();
       
    79 
       
    80     delete iRenameWaiter;
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CRenameObject::StartL
       
    85 //
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 EXPORT_C void CRenameObject::StartL( const TUint32 aParentHandle,
       
    89         const TDesC& aOldFolderName )
       
    90     {
       
    91     PRINT2( _L( "MM MTP => CRenameObject::StartL aParentHandle(0x%x), aOldFolderName(%S)" ),
       
    92         aParentHandle, &aOldFolderName);
       
    93 
       
    94     iObjectHandles.Reset();
       
    95 
       
    96     GenerateObjectHandleListL( aParentHandle );
       
    97 
       
    98     iCount = iObjectHandles.Count();
       
    99     PRINT1( _L( "MM MTP <> CRenameObject::StartL, handle count = %d" ), iCount );
       
   100     if ( iCount > 0 )
       
   101         {
       
   102         // get the old/new folder full file name
       
   103         GetParentSuidL( aParentHandle, aOldFolderName );
       
   104 
       
   105         iIndex = 0;
       
   106 
       
   107         TRequestStatus* status = &iStatus;
       
   108         User::RequestComplete( status, iStatus.Int() );
       
   109         SetActive();
       
   110 
       
   111         iRenameWaiter->Start();
       
   112         }
       
   113 
       
   114     PRINT( _L( "MM MTP <= CRenameObject::StartL" ) );
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CRenameObject::DoCancel()
       
   119 // Cancel the rename object process
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 EXPORT_C void CRenameObject::DoCancel()
       
   123     {
       
   124 
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CRenameObject::RunL
       
   129 //
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 EXPORT_C void CRenameObject::RunL()
       
   133     {
       
   134     PRINT1( _L( "MM MTP => CRenameObject::RunL iIndex = %d" ), iIndex );
       
   135     if ( iIndex < iCount )
       
   136         {
       
   137         if ( iFramework.ObjectMgr().ObjectL( iObjectHandles[iIndex], *iObjectInfo ) )
       
   138             {
       
   139             iOldFileName.Zero();
       
   140             iOldFileName = iObjectInfo->DesC( CMTPObjectMetaData::ESuid );
       
   141             PRINT1( _L( "MM MTP <> CRenameObject::RunL old file name(%S)" ), &iOldFileName );
       
   142 
       
   143             iRightPartName.Zero();
       
   144             iRightPartName = iOldFileName.Right( iOldFileName.Length() - iOldFolderFullName.Length() );
       
   145             PRINT1( _L( "MM MTP <> CRenameObject::RunL right part name(%S)" ), &iRightPartName );
       
   146 
       
   147             if ( iNewFolderName.Length() + iRightPartName.Length() <= KMaxFileName )
       
   148                 {
       
   149                 iFileName.Zero();
       
   150                 iFileName.Append( iNewFolderName );
       
   151                 iFileName.Append( iRightPartName );
       
   152                 PRINT1( _L( "MM MTP <> CRenameObject::RunL new file name(%S)" ), &iFileName );
       
   153                 // update framework metadata DB
       
   154                 iObjectInfo->SetDesCL( CMTPObjectMetaData::ESuid, iFileName );
       
   155                 iObjectInfo->SetUint( CMTPObjectMetaData::EObjectMetaDataUpdate, 1 );
       
   156                 iFramework.ObjectMgr().ModifyObjectL( *iObjectInfo );
       
   157 
       
   158                 PerformAdditionalActionL();
       
   159                 }
       
   160             }
       
   161 
       
   162         iIndex++;
       
   163 
       
   164         TRequestStatus* status = &iStatus;
       
   165         User::RequestComplete( status, iStatus.Int() );
       
   166         SetActive();
       
   167         }
       
   168     else
       
   169         {
       
   170         if( iRenameWaiter->IsStarted() )
       
   171             iRenameWaiter->AsyncStop();
       
   172         }
       
   173 
       
   174     PRINT( _L( "MM MTP <= CRenameObject::RunL" ) );
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CRenameObject::RunError
       
   179 //
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 EXPORT_C TInt CRenameObject::RunError( TInt aError )
       
   183     {
       
   184     if ( aError != KErrNone )
       
   185         PRINT1( _L( "MM MTP <> CRenameObject::RunError with error %d" ), aError );
       
   186 
       
   187     return KErrNone;
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CRenameObject::ConstructL
       
   192 //
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 EXPORT_C void CRenameObject::ConstructL()
       
   196     {
       
   197     CActiveScheduler::Add( this );
       
   198 
       
   199     iObjectInfo = CMTPObjectMetaData::NewL();
       
   200 
       
   201     iNewFolderName.CreateL( KMaxFileName );
       
   202     iOldFolderFullName.CreateL( KMaxFileName );
       
   203     iRightPartName.CreateL( KMaxFileName );
       
   204     iFileName.CreateL( KMaxFileName );
       
   205     iOldFileName.CreateL( KMaxFileName );
       
   206 
       
   207     iRenameWaiter = new( ELeave ) CActiveSchedulerWait;
       
   208     }
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CRenameObject::GenerateObjectHandleListL
       
   212 //
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void CRenameObject::GenerateObjectHandleListL( TUint32 aParentHandle )
       
   216     {
       
   217     PRINT1( _L( "MM MTP => CRenameObject::GenerateObjectHandleListL aParentHandle(0x%x)" ), aParentHandle );
       
   218     RMTPObjectMgrQueryContext context;
       
   219     RArray<TUint> handles;
       
   220     CleanupClosePushL( context ); // + context
       
   221     CleanupClosePushL( handles ); // + handles
       
   222 
       
   223     TMTPObjectMgrQueryParams params( KMTPStorageAll, KMTPFormatsAll,
       
   224             aParentHandle );
       
   225     do
       
   226         {
       
   227         iFramework.ObjectMgr().GetObjectHandlesL( params, context, handles );
       
   228 
       
   229         TInt numberOfObjects = handles.Count();
       
   230         for ( TInt i = 0; i < numberOfObjects; i++ )
       
   231             {
       
   232             if ( iFramework.ObjectMgr().ObjectOwnerId( handles[i] ) == iFramework.DataProviderId() )
       
   233                 {
       
   234                 iObjectHandles.AppendL( handles[i] );
       
   235                 continue;
       
   236                 }
       
   237 
       
   238             // Folder
       
   239             // TODO: need to modify, should not know device dp id
       
   240             if ( iFramework.ObjectMgr().ObjectOwnerId( handles[i] ) == 0 ) // We know that the device dp id is always 0, otherwise the whole MTP won't work.
       
   241                 {
       
   242                 GenerateObjectHandleListL( handles[i] );
       
   243                 }
       
   244             }
       
   245         }
       
   246     while ( !context.QueryComplete() );
       
   247 
       
   248     CleanupStack::PopAndDestroy( &handles ); // - handles
       
   249     CleanupStack::PopAndDestroy( &context ); // - context
       
   250 
       
   251     PRINT( _L( "MM MTP <= CRenameObject::GenerateObjectHandleListL" ) );
       
   252     }
       
   253 
       
   254 // -----------------------------------------------------------------------------
       
   255 // CRenameObject::GetParentSuidL
       
   256 // Get the full file name of old/new folder
       
   257 // -----------------------------------------------------------------------------
       
   258 //
       
   259 void CRenameObject::GetParentSuidL( TUint32 aHandle,
       
   260     const TDesC& aFolderName )
       
   261     {
       
   262     PRINT2( _L( "MM MTP => CRenameObject::GetParentSuidL aHandle(0x%x), aFolderName(%S)" ),
       
   263         aHandle, &aFolderName );
       
   264     CMTPObjectMetaData* objectInfo( CMTPObjectMetaData::NewLC() ); // + objectInfo
       
   265     // get the old folder suid
       
   266     if ( iFramework .ObjectMgr().ObjectL( aHandle, *objectInfo ) )
       
   267         {
       
   268         iNewFolderName.Zero();
       
   269         iNewFolderName = objectInfo->DesC( CMTPObjectMetaData::ESuid );
       
   270         PRINT1( _L( "MM MTP <> CRenameObject::GetParentSuidL new folder full file name(%S)" ), &iNewFolderName );
       
   271         const TInt length = iNewFolderName.Length();
       
   272 
       
   273         TParsePtrC parentSuid( iNewFolderName.Left( length - 1 ) );
       
   274 
       
   275         iOldFolderFullName.Zero();
       
   276         iOldFolderFullName.Append( parentSuid.DriveAndPath() );
       
   277         iOldFolderFullName.Append( aFolderName ); // just name not suid
       
   278         _LIT( KBackSlash, "\\" );
       
   279         iOldFolderFullName.Append( KBackSlash );
       
   280         PRINT1( _L( "MM MTP <> CRenameObject::GetParentSuidL = %S" ), &iOldFolderFullName );
       
   281         }
       
   282     else
       
   283         User::Leave( KErrCorrupt );
       
   284 
       
   285     CleanupStack::PopAndDestroy( objectInfo ); // - objectInfo
       
   286 
       
   287     PRINT( _L( "MM MTP <= CRenameObject::GetParentSuidL" ) );
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CRenameObject::PerformAdditionalActionL
       
   292 //
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CRenameObject::PerformAdditionalActionL()
       
   296     {
       
   297     PRINT( _L( "MM MTP => CRenameObject::PerformAdditionalActionL" ) );
       
   298 
       
   299     // update MPX DB
       
   300     TRAPD( err, iWrapper.RenameObjectL( iOldFileName, iFileName ) );
       
   301 
       
   302     // should not fail for 1 file, keep it going, as folder already renamed
       
   303     if ( err != KErrNone )
       
   304         PRINT1( _L( "MM MTP <> CRenameObject::PerformAdditionalActionL err = %d" ), err );
       
   305 
       
   306     PRINT( _L( "MM MTP <= CRenameObject::PerformAdditionalActionL" ) );
       
   307     }
       
   308 
       
   309 //end of file