mtpdataproviders/mtpimagedp/src/cmtpimagedpobjectpropertymgr.cpp
changeset 0 d0791faffa3f
child 2 4843bb5893b6
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <bautils.h>
       
    22 #include <f32file.h>
       
    23 #include <bautils.h>
       
    24 #include <s32file.h>
       
    25 #include <e32const.h>
       
    26 #include <e32cmn.h>
       
    27 #include <imageconversion.h> 
       
    28 #include <mdeconstants.h>
       
    29 
       
    30 #include <mtp/mmtpobjectmgr.h>
       
    31 #include <mtp/cmtpobjectmetadata.h>
       
    32 #include <mtp/tmtptypeuint32.h>
       
    33 #include <mtp/cmtptypestring.h>
       
    34 #include <mtp/mmtpdataproviderframework.h>
       
    35 #include <mtp/cmtptypearray.h>
       
    36 #include <mtp/mmtpdataproviderframework.h>
       
    37 #include <mtp/mmtpobjectmgr.h>
       
    38 #include <mtp/cmtptypestring.h>
       
    39 #include <mtp/mtpprotocolconstants.h>
       
    40 
       
    41 #include "cmtpimagedpobjectpropertymgr.h"
       
    42 #include "mtpimagedppanic.h"
       
    43 #include "mtpimagedputilits.h"
       
    44 #include "mtpimagedpconst.h"
       
    45 #include "mtpdebug.h"
       
    46 
       
    47 // Class constants.
       
    48 __FLOG_STMT(_LIT8(KComponent,"MTPImageDpPropertyMgr");)
       
    49 
       
    50 /**
       
    51 The properties cache table content.
       
    52 */
       
    53 const CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::TElementMetaData CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::KElements[CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::ENumProperties] = 
       
    54     {
       
    55         {0, CMTPImagePropertiesCache::EUint}, // EImagePixWidth
       
    56         {1, CMTPImagePropertiesCache::EUint}, // EImagePixHeight
       
    57         {2, CMTPImagePropertiesCache::EUint}, // EImageBitDepth
       
    58         {0, CMTPImagePropertiesCache::EDesC}, // EDateCreated
       
    59     };
       
    60 
       
    61 CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache* CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::NewL()
       
    62     {
       
    63     CMTPImagePropertiesCache* self = new(ELeave) CMTPImagePropertiesCache(KElements, ENumProperties);
       
    64     self->ConstructL();
       
    65     return self;
       
    66     }
       
    67 
       
    68 /**
       
    69 Destructor.
       
    70 */
       
    71 CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::~CMTPImagePropertiesCache()
       
    72     {
       
    73     iElementsDesC.ResetAndDestroy();
       
    74     iElementsUint.Reset();
       
    75     } 
       
    76 
       
    77 /**
       
    78 Constructor.
       
    79 */
       
    80 CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::CMTPImagePropertiesCache(const TElementMetaData* aElements, TUint aCount) :
       
    81     iElements(sizeof(TElementMetaData), const_cast<TElementMetaData*>(aElements), aCount)
       
    82     {
       
    83     
       
    84     }
       
    85 
       
    86 void CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::ConstructL()
       
    87     {
       
    88     const TUint KCount(iElements.Count());
       
    89     for (TUint i(0); (i < KCount); i++)
       
    90         {
       
    91         const TElementMetaData& KElement(iElements[i]);
       
    92         switch (KElement.iType)
       
    93             {
       
    94         case EDesC:
       
    95             __ASSERT_DEBUG((iElementsDesC.Count() == KElement.iOffset), Panic(EMTPImageDpBadLayout));
       
    96             iElementsDesC.AppendL(KNullDesC().AllocLC());
       
    97             CleanupStack::Pop();
       
    98             break;
       
    99 
       
   100         case EUint:
       
   101             __ASSERT_DEBUG((iElementsUint.Count() == KElement.iOffset), Panic(EMTPImageDpBadLayout));
       
   102             iElementsUint.AppendL(0);
       
   103             break;
       
   104 
       
   105         default:
       
   106             //nothing to do
       
   107             __DEBUG_ONLY(User::Invariant());
       
   108             break;
       
   109             }         
       
   110         }
       
   111     
       
   112     iObjectHandle = KMTPHandleNone;   
       
   113     }
       
   114 
       
   115 void CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::ResetL()
       
   116     {
       
   117     SetUint(EImagePixWidth, 0);
       
   118     SetUint(EImagePixHeight, 0);
       
   119     SetUint(EImageBitDepth, 0);
       
   120     SetDesCL(EDateCreated, KNullDesC);
       
   121     }
       
   122 
       
   123 const TDesC& CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::DesC(TUint aId) const
       
   124     {
       
   125     __ASSERT_DEBUG((iElements[aId].iType == EDesC), Panic(EMTPImageDpTypeMismatch));
       
   126     return *iElementsDesC[iElements[aId].iOffset];
       
   127     }  
       
   128 
       
   129 TUint CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::Uint(TUint aId) const
       
   130     {
       
   131     __ASSERT_DEBUG((iElements[aId].iType == EUint), Panic(EMTPImageDpTypeMismatch));
       
   132     return iElementsUint[iElements[aId].iOffset];
       
   133     }
       
   134 
       
   135 TUint CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::ObjectHandle() const
       
   136     {
       
   137     return iObjectHandle;
       
   138     }
       
   139 
       
   140 void CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::SetDesCL(TUint aId, const TDesC& aValue)
       
   141     {
       
   142     const TElementMetaData& KElement(iElements[aId]);
       
   143     __ASSERT_DEBUG((KElement.iType == EDesC), Panic(EMTPImageDpTypeMismatch));
       
   144     delete iElementsDesC[KElement.iOffset];
       
   145     iElementsDesC[KElement.iOffset] = NULL;
       
   146     iElementsDesC[KElement.iOffset] = aValue.AllocL();  
       
   147     }
       
   148 
       
   149 void CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::SetUint(TUint aId, TUint aValue)
       
   150     {
       
   151     __ASSERT_DEBUG((iElements[aId].iType == EUint), Panic(EMTPImageDpTypeMismatch));
       
   152     iElementsUint[iElements[aId].iOffset] = aValue;
       
   153     }
       
   154 
       
   155 void CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::SetObjectHandle(TUint aObjectHandle)
       
   156     {
       
   157     iObjectHandle = aObjectHandle;
       
   158     }
       
   159 
       
   160 CMTPImageDpObjectPropertyMgr* CMTPImageDpObjectPropertyMgr::NewL(MMTPDataProviderFramework& aFramework)
       
   161     {
       
   162     CMTPImageDpObjectPropertyMgr* self = new (ELeave) CMTPImageDpObjectPropertyMgr(aFramework);
       
   163     CleanupStack::PushL(self);
       
   164     self->ConstructL(aFramework);
       
   165     CleanupStack::Pop(self);
       
   166     return self;
       
   167     }
       
   168 
       
   169 CMTPImageDpObjectPropertyMgr::CMTPImageDpObjectPropertyMgr(MMTPDataProviderFramework& aFramework) :
       
   170     iFs(aFramework.Fs()),
       
   171     iObjectMgr(aFramework.ObjectMgr())
       
   172     {
       
   173     __FLOG_OPEN(KMTPSubsystem, KComponent);
       
   174     }
       
   175 
       
   176 void CMTPImageDpObjectPropertyMgr::ConstructL(MMTPDataProviderFramework& /*aFramework*/)
       
   177     {
       
   178     __FLOG(_L8("CMTPImageDpObjectPropertyMgr::ConstructL - Entry"));
       
   179     iPropertiesCache = CMTPImagePropertiesCache::NewL();    
       
   180     iMetaDataSession = CMdESession::NewL(*this);
       
   181     __FLOG(_L8("CMTPImageDpObjectPropertyMgr::ConstructL - Open Mde session"));
       
   182     
       
   183     iActiveSchedulerWait = new (ELeave) CActiveSchedulerWait();
       
   184     iActiveSchedulerWait->Start(); // wait for mdesession to start
       
   185     if (iMdeSessionError != KErrNone) 
       
   186         {
       
   187         User::Leave(iMdeSessionError);
       
   188         }
       
   189     __FLOG(_L8("CMTPImageDpObjectPropertyMgr::ConstructL - Exit"));
       
   190     }
       
   191     
       
   192 CMTPImageDpObjectPropertyMgr::~CMTPImageDpObjectPropertyMgr()
       
   193     {
       
   194     __FLOG(_L8("CMTPImageDpObjectPropertyMgr::~CMTPImageDpObjectPropertyMgr - Entry"));
       
   195     delete iPropertiesCache;
       
   196     delete iObject;
       
   197     delete iMetaDataSession;
       
   198     delete iActiveSchedulerWait; 
       
   199     __FLOG(_L8("CMTPImageDpObjectPropertyMgr::~CMTPImageDpObjectPropertyMgr - Exit"));
       
   200     __FLOG_CLOSE;
       
   201     }
       
   202 
       
   203 void CMTPImageDpObjectPropertyMgr::SetCurrentObjectL(CMTPObjectMetaData& aObjectInfo, TBool aRequireForModify, TBool aSaveToCache)
       
   204     {
       
   205     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::SetCurrentObjectL"));
       
   206     iObjectInfo = &aObjectInfo;
       
   207 
       
   208     /**
       
   209      * Image DP property manager will not directly modify properties which stored in the MdS
       
   210      */
       
   211     if(!aRequireForModify)
       
   212         {
       
   213         /**
       
   214          * determine whether the cache hit is occured
       
   215          */
       
   216         if (iPropertiesCache->ObjectHandle() == iObjectInfo->Uint(CMTPObjectMetaData::EHandle))
       
   217             {
       
   218             iCacheHit = ETrue;
       
   219             }
       
   220         else
       
   221             {
       
   222             iCacheHit = EFalse;
       
   223             
       
   224             CMdENamespaceDef& defaultNamespace = iMetaDataSession->GetDefaultNamespaceDefL();
       
   225             CMdEObjectDef& imageObjDef = defaultNamespace.GetObjectDefL( MdeConstants::Image::KImageObject );
       
   226             delete iObject;
       
   227             iObject = NULL;
       
   228             
       
   229             //if we can not open MdS object for getting properties, we will not get properites which stored in MdS
       
   230             TFileName uri;
       
   231             uri.CopyLC(iObjectInfo->DesC(CMTPObjectMetaData::ESuid));
       
   232             TRAP_IGNORE((iObject = iMetaDataSession->GetObjectL(uri, imageObjDef)));            
       
   233             }        
       
   234         }    
       
   235     else
       
   236         {        
       
   237         /**
       
   238          * Set image object properties, because the cached properties are all readonly,
       
   239          * so only sendobjectproplist/sendobjectinfo operations can use cache mechanism, 
       
   240          * other operations will not use cache, such as setobjectvalue/setobjectproplist
       
   241          */
       
   242         if (aSaveToCache)
       
   243             {
       
   244             TUint objectHandle = iObjectInfo->Uint(CMTPObjectMetaData::EHandle);
       
   245             if (iPropertiesCache->ObjectHandle() != objectHandle)
       
   246                 {
       
   247                 iPropertiesCache->ResetL();
       
   248                 }
       
   249             iPropertiesCache->SetObjectHandle(objectHandle);            
       
   250             }
       
   251         }
       
   252     
       
   253     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::SetCurrentObjectL"));
       
   254     }
       
   255 
       
   256 void CMTPImageDpObjectPropertyMgr::SetPropertyL(TMTPObjectPropertyCode aProperty, const TUint8 aValue)
       
   257     {
       
   258     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::SetPropertyL"));
       
   259     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   260     
       
   261     if (aProperty == EMTPObjectPropCodeNonConsumable) 
       
   262         {
       
   263         iObjectInfo->SetUint(CMTPObjectMetaData::ENonConsumable, aValue);
       
   264         }
       
   265     else
       
   266         {
       
   267         User::Leave(EMTPRespCodeObjectPropNotSupported);
       
   268         }
       
   269     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::SetPropertyL"));
       
   270     }
       
   271 
       
   272 void CMTPImageDpObjectPropertyMgr::SetPropertyL(TMTPObjectPropertyCode aProperty, const TUint16 aValue)
       
   273     {
       
   274     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::SetPropertyL"));
       
   275     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   276     
       
   277     switch(aProperty)
       
   278         {
       
   279     case EMTPObjectPropCodeObjectFormat:
       
   280         iObjectInfo->SetUint(CMTPObjectMetaData::EFormatCode, aValue);
       
   281         break;        
       
   282     case EMTPObjectPropCodeProtectionStatus://this property does not supported by image dp
       
   283         //nothing to do
       
   284         break;
       
   285     default:
       
   286         //nothing to do
       
   287         break;
       
   288         }
       
   289     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::SetPropertyL"));
       
   290     }
       
   291 
       
   292 void CMTPImageDpObjectPropertyMgr::SetPropertyL(TMTPObjectPropertyCode aProperty, const TUint32 aValue)
       
   293     {
       
   294     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::SetPropertyL"));
       
   295     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   296     
       
   297     switch(aProperty)
       
   298         {
       
   299     case EMTPObjectPropCodeStorageID:
       
   300         iObjectInfo->SetUint(CMTPObjectMetaData::EStorageId, aValue);
       
   301         break;
       
   302     case EMTPObjectPropCodeParentObject:
       
   303         iObjectInfo->SetUint(CMTPObjectMetaData::EParentHandle, aValue);
       
   304         break;
       
   305     case EMTPObjectPropCodeWidth:
       
   306         iPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixWidth, aValue);
       
   307         break;
       
   308     case EMTPObjectPropCodeHeight:
       
   309         iPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixHeight, aValue);
       
   310         break; 
       
   311     case EMTPObjectPropCodeImageBitDepth:
       
   312         iPropertiesCache->SetUint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImageBitDepth, aValue);
       
   313         break;          
       
   314     default:
       
   315         //nothing to do
       
   316         break;
       
   317         }
       
   318     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::SetPropertyL"));
       
   319     }
       
   320 
       
   321 void CMTPImageDpObjectPropertyMgr::SetPropertyL(TMTPObjectPropertyCode aProperty, const TDesC& aValue)
       
   322     {
       
   323     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::SetPropertyL"));
       
   324     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   325     
       
   326     switch(aProperty)
       
   327         {
       
   328     case EMTPObjectPropCodeObjectFileName:
       
   329         {
       
   330         TParsePtrC oldUri = TParsePtrC(iObjectInfo->DesC(CMTPObjectMetaData::ESuid));
       
   331         
       
   332         //calculate new file name length
       
   333         TInt len = oldUri.DriveAndPath().Length() + aValue.Length();
       
   334         
       
   335         //allocate memory for the new uri
       
   336         RBuf  newUri;
       
   337         newUri.CleanupClosePushL();
       
   338         newUri.CreateL(len);
       
   339         
       
   340         //create the new uri
       
   341         newUri.Append(oldUri.DriveAndPath());
       
   342         newUri.Append(aValue);
       
   343         newUri.Trim();
       
   344         
       
   345         //ask fs to rename file, leave if err returned from fs
       
   346         User::LeaveIfError(iFs.Rename(oldUri.FullName(), newUri));
       
   347         iObjectInfo->SetDesCL(CMTPObjectMetaData::ESuid, newUri);
       
   348         CleanupStack::PopAndDestroy(&newUri);        
       
   349         }
       
   350         break;
       
   351         
       
   352     case EMTPObjectPropCodeName:
       
   353         iObjectInfo->SetDesCL(CMTPObjectMetaData::EName, aValue);
       
   354         break;
       
   355               
       
   356     case EMTPObjectPropCodeDateModified:
       
   357         {
       
   358         TTime modifiedTime;
       
   359         ConvertMTPTimeStr2TTimeL(aValue, modifiedTime);
       
   360         iFs.SetModified(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), modifiedTime);     
       
   361         }
       
   362         break;
       
   363       
       
   364     case EMTPObjectPropCodeDateCreated://MdS property
       
   365         iPropertiesCache->SetDesCL(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EDateCreated, aValue);
       
   366         break;
       
   367         
       
   368     default:
       
   369         //nothing to do
       
   370         break;
       
   371         }
       
   372     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::SetPropertyL"));
       
   373     }
       
   374 
       
   375 void CMTPImageDpObjectPropertyMgr::GetPropertyL(TMTPObjectPropertyCode aProperty, TUint8 &aValue)
       
   376     {
       
   377     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   378     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   379     
       
   380     if (aProperty == EMTPObjectPropCodeNonConsumable) 
       
   381         {
       
   382         aValue = iObjectInfo->Uint(CMTPObjectMetaData::ENonConsumable);       
       
   383         }
       
   384     else
       
   385         {
       
   386         User::Leave(EMTPRespCodeObjectPropNotSupported);
       
   387         }     
       
   388     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   389     }
       
   390 
       
   391 void CMTPImageDpObjectPropertyMgr::GetPropertyL(TMTPObjectPropertyCode aProperty, TUint16 &aValue)
       
   392     {
       
   393     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   394     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   395     TEntry entry;
       
   396     switch(aProperty)
       
   397         {
       
   398     case EMTPObjectPropCodeObjectFormat:
       
   399         aValue = iObjectInfo->Uint(CMTPObjectMetaData::EFormatCode);
       
   400         break;
       
   401     case EMTPObjectPropCodeRepresentativeSampleFormat:
       
   402         aValue = KThumbFormatCode;
       
   403        break;        
       
   404     case EMTPObjectPropCodeProtectionStatus:
       
   405         iFs.Entry(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), entry);
       
   406         if (entry.IsReadOnly())
       
   407             {
       
   408             aValue = EMTPProtectionReadOnly;
       
   409             }
       
   410         else
       
   411             {
       
   412             aValue = EMTPProtectionNoProtection;
       
   413             }
       
   414         break;        
       
   415     default:
       
   416         GetPropertyFromMdsL(aProperty, &aValue);
       
   417         break;
       
   418         }
       
   419     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   420     }
       
   421 
       
   422 void CMTPImageDpObjectPropertyMgr::GetPropertyL(TMTPObjectPropertyCode aProperty, TUint32 &aValue)
       
   423     {
       
   424     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   425     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   426     
       
   427     switch(aProperty)
       
   428         {
       
   429     case EMTPObjectPropCodeStorageID:
       
   430         aValue = iObjectInfo->Uint(CMTPObjectMetaData::EStorageId);
       
   431         break;
       
   432         
       
   433     case EMTPObjectPropCodeParentObject:
       
   434         aValue = iObjectInfo->Uint(CMTPObjectMetaData::EParentHandle);
       
   435         break;        
       
   436        
       
   437     case EMTPObjectPropCodeRepresentativeSampleSize:
       
   438         aValue = KThumbCompressedSize;
       
   439        break;       
       
   440        
       
   441     case EMTPObjectPropCodeRepresentativeSampleHeight:
       
   442         aValue = KThumbHeigth;
       
   443        break;
       
   444        
       
   445     case EMTPObjectPropCodeRepresentativeSampleWidth:
       
   446         aValue = KThumbWidht;
       
   447        break;
       
   448               
       
   449     default:
       
   450         GetPropertyFromMdsL(aProperty, &aValue);
       
   451         break;  
       
   452         }
       
   453     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   454     }
       
   455     
       
   456 void CMTPImageDpObjectPropertyMgr::GetPropertyL(TMTPObjectPropertyCode aProperty, TUint64& aValue)
       
   457     {
       
   458     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   459     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   460 
       
   461     if (aProperty == EMTPObjectPropCodeObjectSize) 
       
   462         {
       
   463         TEntry entry;
       
   464         iFs.Entry(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), entry);
       
   465         aValue = entry.FileSize();            
       
   466         }
       
   467     else
       
   468         {
       
   469         User::Leave(EMTPRespCodeObjectPropNotSupported);
       
   470         }    
       
   471     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   472     }
       
   473 
       
   474 void CMTPImageDpObjectPropertyMgr::GetPropertyL(TMTPObjectPropertyCode aProperty, TMTPTypeUint128& aValue)
       
   475     {
       
   476     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   477     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   478     
       
   479     if (aProperty == EMTPObjectPropCodePersistentUniqueObjectIdentifier) 
       
   480         {
       
   481         TUint32 handle = iObjectInfo->Uint(CMTPObjectMetaData::EHandle);
       
   482         aValue = iObjectMgr.PuidL(handle);
       
   483         }
       
   484     else
       
   485         {
       
   486         User::Leave(EMTPRespCodeObjectPropNotSupported);
       
   487         }
       
   488     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   489     }
       
   490     
       
   491 void CMTPImageDpObjectPropertyMgr::GetPropertyL(TMTPObjectPropertyCode aProperty, CMTPTypeString& aValue)
       
   492     {
       
   493     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   494     __ASSERT_DEBUG(iObjectInfo, Panic(EMTPImageDpObjectNull));
       
   495 
       
   496     switch(aProperty)
       
   497         {    
       
   498     case EMTPObjectPropCodeObjectFileName:
       
   499         {
       
   500         TFileName name;
       
   501         User::LeaveIfError(BaflUtils::MostSignificantPartOfFullName(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), name));     
       
   502         aValue.SetL(name);
       
   503         }
       
   504         break;
       
   505         
       
   506     case EMTPObjectPropCodeName:
       
   507         {
       
   508         aValue.SetL(iObjectInfo->DesC(CMTPObjectMetaData::EName));
       
   509         }
       
   510         break;
       
   511         
       
   512     case EMTPObjectPropCodeDateModified:
       
   513         {
       
   514         TBuf<64> dateString;
       
   515         TEntry entry;
       
   516         iFs.Entry(iObjectInfo->DesC(CMTPObjectMetaData::ESuid), entry);
       
   517         
       
   518         _LIT(KTimeFormat, "%F%Y%M%DT%H%T%S"); 
       
   519         entry.iModified.FormatL(dateString, KTimeFormat);        
       
   520         aValue.SetL(dateString);
       
   521         }
       
   522         break;                 
       
   523         
       
   524     default:
       
   525         GetPropertyFromMdsL(aProperty, &aValue);
       
   526         break;
       
   527         }
       
   528     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetPropertyL"));
       
   529     }
       
   530 
       
   531 void CMTPImageDpObjectPropertyMgr::GetPropertyFromMdsL(TMTPObjectPropertyCode aProperty, TAny* aValue)
       
   532     {
       
   533     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetPropertyFromMdsL"));
       
   534     
       
   535     TInt err = KErrNone;
       
   536     CMdENamespaceDef& defaultNamespace = iMetaDataSession->GetDefaultNamespaceDefL();
       
   537     CMdEObjectDef& imageObjDef = defaultNamespace.GetObjectDefL( MdeConstants::Image::KImageObject );
       
   538     CMdEProperty* mdeProperty = NULL;
       
   539     switch (aProperty)
       
   540         {        
       
   541     case EMTPObjectPropCodeDateCreated:
       
   542         {
       
   543         if (iCacheHit)
       
   544             {
       
   545             (*(static_cast<CMTPTypeString*>(aValue))).SetL(iPropertiesCache->DesC(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EDateCreated));
       
   546             }
       
   547         else if (iObject)
       
   548             {        
       
   549             CMdEPropertyDef& creationDatePropDef = imageObjDef.GetPropertyDefL(MdeConstants::Object::KCreationDateProperty);
       
   550             TInt err = iObject->Property( creationDatePropDef, mdeProperty );  
       
   551             if (err >= KErrNone) 
       
   552                 {
       
   553                 TBuf<KMaxTimeFormatSpec*2> timeValue;
       
   554                 // locale independent YYYYMMSSThhmmss, as required by the MTP spec
       
   555                 _LIT(KTimeFormat, "%F%Y%M%DT%H%T%S");
       
   556                 mdeProperty->TimeValueL().FormatL(timeValue, KTimeFormat);
       
   557                 (*(static_cast<CMTPTypeString*>(aValue))).SetL(timeValue);
       
   558                 }
       
   559             }
       
   560         }
       
   561        break;  
       
   562        
       
   563     case EMTPObjectPropCodeWidth:
       
   564         {
       
   565         if (iCacheHit)
       
   566             {
       
   567             *static_cast<TUint32*>(aValue) = iPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixWidth);
       
   568             }
       
   569         else if (iObject)
       
   570             {
       
   571             CMdEPropertyDef& imageWidthPropDef = imageObjDef.GetPropertyDefL(MdeConstants::MediaObject::KWidthProperty);
       
   572             err = iObject->Property( imageWidthPropDef, mdeProperty );  
       
   573             if (err >= KErrNone) 
       
   574                 {
       
   575                 TUint16 width = mdeProperty->Uint16ValueL();
       
   576                 if (width > 0)
       
   577                     {
       
   578                     *static_cast<TUint32*>(aValue) = width;
       
   579                     }
       
   580                 else
       
   581                     {
       
   582                     *static_cast<TUint32*>(aValue) = ParseImageFileL(iObject->Uri(), EMTPObjectPropCodeWidth);
       
   583                     }
       
   584                 }
       
   585             else
       
   586                 {
       
   587                 *static_cast<TUint32*>(aValue) = ParseImageFileL(iObject->Uri(), EMTPObjectPropCodeWidth);
       
   588                 }
       
   589             }
       
   590         else
       
   591             {
       
   592             *static_cast<TUint32*>(aValue) = 0;
       
   593             }
       
   594         }
       
   595        break; 
       
   596        
       
   597     case EMTPObjectPropCodeHeight:
       
   598         {
       
   599         if (iCacheHit)
       
   600             {
       
   601             *static_cast<TUint32*>(aValue) = iPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImagePixHeight);
       
   602             }
       
   603         else if (iObject)
       
   604             {
       
   605             CMdEPropertyDef& imageHeightPropDef = imageObjDef.GetPropertyDefL(MdeConstants::MediaObject::KHeightProperty);
       
   606             err = iObject->Property( imageHeightPropDef, mdeProperty );  
       
   607             if (err >= KErrNone) 
       
   608                 {
       
   609                 TUint16 height = mdeProperty->Uint16ValueL();
       
   610                 if (height > 0)
       
   611                     {
       
   612                     *static_cast<TUint32*>(aValue) = height;
       
   613                     }
       
   614                 else
       
   615                     {
       
   616                     *static_cast<TUint32*>(aValue) = ParseImageFileL(iObject->Uri(), EMTPObjectPropCodeHeight);
       
   617                     }
       
   618                 }
       
   619             else
       
   620                 {
       
   621                 *static_cast<TUint32*>(aValue) = ParseImageFileL(iObject->Uri(), EMTPObjectPropCodeHeight);
       
   622                 }
       
   623             }
       
   624         else
       
   625             {
       
   626             *static_cast<TUint32*>(aValue) = 0;
       
   627             }
       
   628         }
       
   629        break; 
       
   630        
       
   631     case EMTPObjectPropCodeImageBitDepth:
       
   632         {
       
   633         if (iCacheHit)
       
   634             {
       
   635             *static_cast<TUint32*>(aValue) = iPropertiesCache->Uint(CMTPImageDpObjectPropertyMgr::CMTPImagePropertiesCache::EImageBitDepth);
       
   636             }
       
   637         else if (iObject)
       
   638             {
       
   639             CMdEPropertyDef& imageBitDepth = imageObjDef.GetPropertyDefL(MdeConstants::Image::KBitsPerSampleProperty);
       
   640             err = iObject->Property( imageBitDepth, mdeProperty );  
       
   641             if (err >= KErrNone) 
       
   642                 {
       
   643                 TUint16 bitDepth = mdeProperty->Uint16ValueL();
       
   644                 if (bitDepth > 0)
       
   645                     {
       
   646                     *static_cast<TUint32*>(aValue) = bitDepth;
       
   647                     }
       
   648                 else
       
   649                     {
       
   650                     *static_cast<TUint32*>(aValue) = ParseImageFileL(iObject->Uri(), EMTPObjectPropCodeImageBitDepth);
       
   651                     }                
       
   652                 }
       
   653             else
       
   654                 {
       
   655                 *static_cast<TUint32*>(aValue) = ParseImageFileL(iObject->Uri(), EMTPObjectPropCodeImageBitDepth);
       
   656                 }  
       
   657             }
       
   658         else
       
   659             {
       
   660             *static_cast<TUint32*>(aValue) = 0;
       
   661             }        
       
   662         }     
       
   663        break; 
       
   664        
       
   665     default:
       
   666         //nothing to do
       
   667         break;
       
   668         }
       
   669     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetPropertyFromMdsL"));
       
   670     }
       
   671 
       
   672 TBool CMTPImageDpObjectPropertyMgr::GetYear(const TDesC& aDateString, TInt& aYear) const
       
   673     {
       
   674   __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetYear"));
       
   675     aYear = 0;
       
   676     TLex dateBuf(aDateString.Left(4));
       
   677   __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetYear"));
       
   678     return dateBuf.Val(aYear) == KErrNone;
       
   679     }
       
   680 
       
   681 TBool CMTPImageDpObjectPropertyMgr::GetMonth(const TDesC& aDateString, TMonth& aMonth) const
       
   682     {
       
   683       __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetMonth"));
       
   684     TBool result = EFalse;
       
   685     aMonth = EJanuary;
       
   686     TInt month = 0;
       
   687     TLex dateBuf(aDateString.Mid(4, 2));
       
   688     if(dateBuf.Val(month) == KErrNone && month > 0 && month < 13)
       
   689         {
       
   690         month--;
       
   691         aMonth = (TMonth)month;
       
   692         result = ETrue;
       
   693         }
       
   694     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetMonth"));
       
   695     return result;
       
   696     }
       
   697 
       
   698 TBool CMTPImageDpObjectPropertyMgr::GetDay(const TDesC& aDateString, TInt& aDay) const
       
   699     {
       
   700     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetDay"));
       
   701     TBool result = EFalse;
       
   702     aDay = 0;
       
   703     TLex dateBuf(aDateString.Mid(6, 2));
       
   704     if(dateBuf.Val(aDay) == KErrNone && aDay > 0 && aDay < 32)
       
   705         {
       
   706         aDay--;
       
   707         result = ETrue;
       
   708         }
       
   709     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetDay"));
       
   710     return result;	
       
   711     }
       
   712 
       
   713 TBool CMTPImageDpObjectPropertyMgr::GetHour(const TDesC& aDateString, TInt& aHour) const
       
   714     {
       
   715     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetHour"));
       
   716     aHour = 0;
       
   717     TLex dateBuf(aDateString.Mid(9, 2));
       
   718     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetHour"));
       
   719     return (dateBuf.Val(aHour) == KErrNone && aHour >=0 && aHour < 60);
       
   720     }
       
   721                 
       
   722 TBool CMTPImageDpObjectPropertyMgr::GetMinute(const TDesC& aDateString, TInt& aMinute) const
       
   723     {
       
   724     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetMinute"));
       
   725     aMinute = 0;
       
   726     TLex dateBuf(aDateString.Mid(11, 2));
       
   727     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetMinute"));
       
   728     return (dateBuf.Val(aMinute) == KErrNone && aMinute >=0 && aMinute < 60);
       
   729     }
       
   730 
       
   731 TBool CMTPImageDpObjectPropertyMgr::GetSecond(const TDesC& aDateString, TInt& aSecond) const
       
   732     {
       
   733     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetSecond"));
       
   734     aSecond = 0;
       
   735     TLex dateBuf(aDateString.Mid(13, 2));
       
   736     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetSecond"));
       
   737     return (dateBuf.Val(aSecond) == KErrNone && aSecond >= 0 && aSecond < 60);
       
   738     }
       
   739 
       
   740 TBool CMTPImageDpObjectPropertyMgr::GetTenthSecond(const TDesC& aDateString, TInt& aTenthSecond) const
       
   741     {
       
   742     __FLOG(_L8(">> CMTPImageDpObjectPropertyMgr::GetTenthSecond"));
       
   743     TBool result = EFalse;
       
   744     aTenthSecond = 0;
       
   745     TInt dotPos = aDateString.Find(_L("."));
       
   746     if(dotPos != KErrNotFound && dotPos < aDateString.Length() - 1)
       
   747         {
       
   748         TLex dateBuf(aDateString.Mid(dotPos + 1, 1));
       
   749         result = (dateBuf.Val(aTenthSecond) == KErrNone && aTenthSecond >=0 && aTenthSecond < 10);
       
   750         }
       
   751     else
       
   752         {
       
   753         result = ETrue;
       
   754         }
       
   755     __FLOG(_L8("<< CMTPImageDpObjectPropertyMgr::GetTenthSecond"));
       
   756     return result;	
       
   757     }
       
   758 
       
   759 /*
       
   760  * Convert the MTP datatime string to TTime:
       
   761  * 
       
   762  *  MTP datatime string format: YYYYMMDDThhmmss.s  Optional(.s)
       
   763  *  TTime string format       : YYYYMMDD:HHMMSS.MMMMMM
       
   764  *  
       
   765  */
       
   766 void CMTPImageDpObjectPropertyMgr::ConvertMTPTimeStr2TTimeL(const TDesC& aTimeString, TTime& aModifiedTime) const
       
   767     {
       
   768     //Convert the Time String to TDateTime
       
   769     TInt year = 0;
       
   770     TMonth month;
       
   771     TInt day = 0;
       
   772     TInt hour = 0;
       
   773     TInt minute = 0;
       
   774     TInt second = 0;
       
   775     TInt tenthSecond = 0;
       
   776     
       
   777     if(!GetYear(aTimeString,year)
       
   778            ||!GetMonth(aTimeString,month)
       
   779            ||!GetDay(aTimeString,day)
       
   780            ||!GetHour(aTimeString,hour)
       
   781            ||!GetMinute(aTimeString,minute)
       
   782            ||!GetSecond(aTimeString,second)
       
   783            ||!GetTenthSecond(aTimeString,tenthSecond))
       
   784         {
       
   785         User::Leave(KErrArgument);
       
   786         }
       
   787     else
       
   788         {     
       
   789         TDateTime dateTime(year, month, day, hour, minute, second, tenthSecond);
       
   790         aModifiedTime = dateTime;
       
   791         } 
       
   792     }
       
   793 
       
   794 void CMTPImageDpObjectPropertyMgr::RemoveProperty(CMdEObject& aObject, CMdEPropertyDef& aPropDef)
       
   795     {
       
   796     __FLOG(_L8("CMTPImageDpObjectPropertyMgr::RemoveProperty"));
       
   797     TInt index;
       
   798     CMdEProperty* property;
       
   799     index = aObject.Property(aPropDef, property);
       
   800     if (index != KErrNotFound)
       
   801         {
       
   802         aObject.RemoveProperty(index);
       
   803         }
       
   804     }
       
   805 
       
   806 TUint32 CMTPImageDpObjectPropertyMgr::ParseImageFileL(const TDesC& aUri, TMTPObjectPropertyCode aPropCode)
       
   807     {
       
   808     TUint32 result = 0;
       
   809     TInt err = KErrNone;
       
   810     const TInt K64Kb = 65536;
       
   811     
       
   812     HBufC8* buffer = HBufC8::NewLC(K64Kb);
       
   813     TPtr8 myImagePtr = buffer->Des();
       
   814     err = iFs.ReadFileSection(aUri, 0, myImagePtr, K64Kb);
       
   815     if (err != KErrNone)
       
   816         {
       
   817         CleanupStack::PopAndDestroy(buffer);
       
   818         return result;
       
   819         }
       
   820     
       
   821     CBufferedImageDecoder *decoder = CBufferedImageDecoder::NewL(iFs);
       
   822     CleanupStack::PushL(decoder);
       
   823         
       
   824     decoder->OpenL(myImagePtr, CImageDecoder::TOptions( CImageDecoder::EPreferFastDecode | CImageDecoder::EOptionIgnoreExifMetaData));
       
   825     if (decoder->ValidDecoder())
       
   826         {
       
   827         const TFrameInfo& info = decoder->FrameInfo();
       
   828         
       
   829         switch (aPropCode)
       
   830             {
       
   831         case EMTPObjectPropCodeWidth:
       
   832             result = info.iOverallSizeInPixels.iWidth;
       
   833             break;
       
   834             
       
   835         case EMTPObjectPropCodeHeight:
       
   836             result = info.iOverallSizeInPixels.iHeight;
       
   837             break;
       
   838             
       
   839         case EMTPObjectPropCodeImageBitDepth:
       
   840             result = info.iBitsPerPixel;
       
   841             break;
       
   842             
       
   843         default:
       
   844             // nothing to do
       
   845             break;
       
   846             }
       
   847         
       
   848         /**
       
   849          * Open MdE object for update image object properties after parsing
       
   850          */
       
   851         CMdENamespaceDef& defaultNamespace = iMetaDataSession->GetDefaultNamespaceDefL();
       
   852         CMdEObjectDef& imageObjDef = defaultNamespace.GetObjectDefL(MdeConstants::Image::KImageObject);
       
   853         CMdEPropertyDef& imageWidthPropDef = imageObjDef.GetPropertyDefL(MdeConstants::MediaObject::KWidthProperty);
       
   854         CMdEPropertyDef& imageHeightPropDef = imageObjDef.GetPropertyDefL(MdeConstants::MediaObject::KHeightProperty);
       
   855         CMdEPropertyDef& imageBitDepth = imageObjDef.GetPropertyDefL(MdeConstants::Image::KBitsPerSampleProperty);
       
   856         
       
   857         //update current object properties
       
   858         TUint16 width = info.iOverallSizeInPixels.iWidth;
       
   859         RemoveProperty(*iObject, imageWidthPropDef);
       
   860         iObject->AddUint16PropertyL(imageWidthPropDef, width);
       
   861         
       
   862         TUint16 height = info.iOverallSizeInPixels.iHeight;
       
   863         RemoveProperty(*iObject, imageHeightPropDef);
       
   864         iObject->AddUint16PropertyL(imageHeightPropDef, height);
       
   865         
       
   866         TUint16 bitsPerPixel = info.iBitsPerPixel;
       
   867         RemoveProperty(*iObject, imageBitDepth);
       
   868         iObject->AddUint16PropertyL(imageBitDepth, bitsPerPixel);
       
   869         
       
   870         CMdEObject* updateObject = NULL;
       
   871         TRAP(err, updateObject = iMetaDataSession->OpenObjectL(iObject->Id(), imageObjDef));
       
   872         if (err == KErrNone && updateObject != NULL)
       
   873             {
       
   874             CleanupStack::PushL(updateObject);
       
   875             
       
   876             RemoveProperty(*updateObject, imageWidthPropDef);                   
       
   877             updateObject->AddUint16PropertyL(imageWidthPropDef, width);    
       
   878             
       
   879             RemoveProperty(*updateObject, imageHeightPropDef);        
       
   880             updateObject->AddUint16PropertyL(imageHeightPropDef, height);
       
   881             
       
   882             RemoveProperty(*updateObject, imageBitDepth);        
       
   883             updateObject->AddUint16PropertyL(imageBitDepth, bitsPerPixel);    
       
   884                     
       
   885             iMetaDataSession->CommitObjectL(*updateObject);
       
   886             CleanupStack::PopAndDestroy(updateObject);
       
   887             }
       
   888         }
       
   889     
       
   890     CleanupStack::PopAndDestroy(2);// decoder, buffer
       
   891     
       
   892     return result;
       
   893     }
       
   894 
       
   895 /**
       
   896  * Return the mdesession instance
       
   897  */
       
   898 CMdESession& CMTPImageDpObjectPropertyMgr::MdeSession()
       
   899     {
       
   900     return *iMetaDataSession;
       
   901     }
       
   902 
       
   903 /**
       
   904  *  From MMdESessionObserver
       
   905  */
       
   906 void CMTPImageDpObjectPropertyMgr::HandleSessionOpened(CMdESession& /*aSession*/, TInt aError)
       
   907     {   
       
   908     SetMdeSessionError(aError);
       
   909     if (iActiveSchedulerWait && iActiveSchedulerWait->IsStarted())
       
   910         {
       
   911         iActiveSchedulerWait->AsyncStop();  
       
   912         }
       
   913     }
       
   914 
       
   915 /**
       
   916  *  From MMdESessionObserver
       
   917  */
       
   918 void CMTPImageDpObjectPropertyMgr::HandleSessionError(CMdESession& /*aSession*/, TInt aError)
       
   919     {
       
   920     SetMdeSessionError(aError);
       
   921     if (iActiveSchedulerWait && iActiveSchedulerWait->IsStarted())
       
   922         {
       
   923         iActiveSchedulerWait->AsyncStop();  
       
   924         }  
       
   925     }
       
   926 
       
   927 void CMTPImageDpObjectPropertyMgr::SetMdeSessionError(TInt aError)
       
   928     {
       
   929     iMdeSessionError = aError;
       
   930     }
       
   931 
       
   932 void CMTPImageDpObjectPropertyMgr::ClearCacheL()
       
   933     {
       
   934     iPropertiesCache->ResetL();
       
   935     }