mtpfws/mtpfw/datatypes/src/cmtptypelist.cpp
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2006-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 @publishedPartner
       
    19 */
       
    20 
       
    21 #include <mtp/cmtptypelist.h>
       
    22 
       
    23 #include "mtpdatatypespanic.h"
       
    24 #include <mtp/tmtptypedatapair.h>
       
    25 
       
    26 const TUint CMTPTypeList::KChunkNumberofElements(0);
       
    27 
       
    28 
       
    29 const TUint CMTPTypeList::KChunckNumberOfHeader(1);
       
    30 
       
    31 EXPORT_C CMTPTypeList* CMTPTypeList::NewL(TMTPTypeIds aListType, TMTPTypeIds aElementType)
       
    32     {
       
    33     CMTPTypeList* self = CMTPTypeList::NewLC(aListType, aElementType); 
       
    34     CleanupStack::Pop(self);
       
    35     return self;  
       
    36     }
       
    37 
       
    38 EXPORT_C CMTPTypeList* CMTPTypeList::NewLC(TMTPTypeIds aListType, TMTPTypeIds aElementType)
       
    39     {
       
    40     CMTPTypeList* self = new(ELeave) CMTPTypeList(aListType, aElementType);
       
    41     CleanupStack::PushL(self);
       
    42     self->ConstructL();
       
    43     return self;
       
    44     }
       
    45 
       
    46 EXPORT_C  CMTPTypeList::~CMTPTypeList()
       
    47     {
       
    48     ResetAndDestroy();
       
    49     }
       
    50 
       
    51 CMTPTypeList::CMTPTypeList(TInt aListType, TInt aElementType):
       
    52     CMTPTypeCompoundBase(KJustInTimeConstruction, KVariableChunkCount),
       
    53     iListType(aListType),
       
    54     iElementType(aElementType),
       
    55     iChunkNumberOfElements(KChunkNumberofElements)
       
    56     {
       
    57     const CMTPTypeCompoundBase::TElementInfo KDefaultInfo = {0, EMTPTypeUndefined, {EMTPTypeUndefined, KMTPNotApplicable,  KMTPNotApplicable}};
       
    58     iInfoNumberOfElements       = KDefaultInfo; 
       
    59     iInfoNumberOfElements.iType = EMTPTypeUINT32;
       
    60     iInfoElement                = KDefaultInfo; 
       
    61     iInfoElement.iType          = aElementType;
       
    62     }
       
    63 
       
    64 void CMTPTypeList::InitListL()
       
    65     {
       
    66     ChunkAppendL(iChunkNumberOfElements);
       
    67     }
       
    68 
       
    69 void CMTPTypeList::ConstructL()
       
    70     {
       
    71     InitListL();
       
    72     }
       
    73 
       
    74 
       
    75 EXPORT_C void CMTPTypeList::AppendL(const MMTPType* aElement)
       
    76     {
       
    77     if (aElement == NULL)
       
    78         {
       
    79         User::Leave(KMTPDataTypeInvalid);
       
    80         }
       
    81              
       
    82     // Append the element.
       
    83     AppendElementChunkL(aElement);
       
    84     
       
    85     // Increment NumberOfElements.
       
    86     const TUint index(iChunkNumberOfElements.Value());
       
    87     iChunkNumberOfElements.Set(index + 1);
       
    88     }
       
    89 
       
    90 EXPORT_C void CMTPTypeList::Remove(const TInt aIndex)
       
    91     {
       
    92         
       
    93     MMTPType* tmp = iChunksElement[aIndex];
       
    94     iChunksElement.Remove(aIndex);
       
    95     ChunkRemove(aIndex + 1);
       
    96     RMTPType::Destroy(iElementType,tmp );
       
    97     
       
    98     const TUint index(iChunkNumberOfElements.Value());
       
    99     iChunkNumberOfElements.Set(index - 1);
       
   100     }
       
   101 
       
   102 EXPORT_C TInt CMTPTypeList::ElementType() const
       
   103     {
       
   104     return iElementType;
       
   105     }
       
   106 
       
   107 EXPORT_C TUint32 CMTPTypeList::NumberOfElements() const
       
   108     {
       
   109     return iChunkNumberOfElements.Value();
       
   110     }
       
   111 
       
   112 EXPORT_C MMTPType& CMTPTypeList::ElementL(const TInt aIndex) const
       
   113 	{
       
   114 	__ASSERT_ALWAYS(( aIndex < NumberOfElements() ), User::Leave(EMTPTypeBoundsError));
       
   115 
       
   116 	return *iChunksElement[aIndex];
       
   117 	}
       
   118 
       
   119 EXPORT_C TInt CMTPTypeList::FirstWriteChunk(TPtr8& aChunk)
       
   120     {
       
   121     ResetAndDestroy();
       
   122     
       
   123     // Setup the write chunk pointer.
       
   124     TInt err(UpdateWriteSequenceErr(CMTPTypeCompoundBase::FirstWriteChunk(aChunk)));
       
   125     switch (err)
       
   126         {
       
   127     case KMTPChunkSequenceCompletion:
       
   128         err = KErrNone;
       
   129         // Don't break, fall through to set the write sequence state.
       
   130         
       
   131     case KErrNone:
       
   132         // Set the write sequence state.
       
   133         iWriteSequenceState = EElementChunks;
       
   134         break;
       
   135         
       
   136     default:
       
   137         break;
       
   138         }
       
   139         
       
   140     return  err;
       
   141     }
       
   142 
       
   143 EXPORT_C TInt CMTPTypeList::NextWriteChunk(TPtr8& aChunk)
       
   144     {
       
   145     TInt err(KMTPChunkSequenceCompletion);
       
   146     if (iWriteSequenceState != EIdle)
       
   147         {
       
   148         err = UpdateWriteSequenceErr(CMTPTypeCompoundBase::NextWriteChunk(aChunk));
       
   149         if ((iWriteSequenceErr == KMTPChunkSequenceCompletion) && (iWriteSequenceState != EIdle))
       
   150             {
       
   151             err = KErrNone;
       
   152             }   
       
   153         }    
       
   154     return err;
       
   155     }
       
   156 
       
   157 EXPORT_C TInt CMTPTypeList::NextWriteChunk(TPtr8& aChunk, TUint /*aDataLength*/)
       
   158 	{
       
   159 	return NextWriteChunk(aChunk);
       
   160 	}
       
   161 
       
   162 EXPORT_C TUint CMTPTypeList::Type() const
       
   163     {
       
   164     return iListType;
       
   165     }
       
   166 
       
   167 EXPORT_C TBool CMTPTypeList::CommitRequired() const
       
   168     {
       
   169     return ETrue;
       
   170     }
       
   171 
       
   172 EXPORT_C MMTPType* CMTPTypeList::CommitChunkL(TPtr8& aChunk)
       
   173     {
       
   174     if (iWriteSequenceErr == KMTPChunkSequenceCompletion)
       
   175         {
       
   176         switch (iWriteSequenceState)
       
   177             {
       
   178         case EElementChunks:
       
   179             if ((iChunkNumberOfElements.Value()) && 
       
   180                 (iChunksElement.Count() < iChunkNumberOfElements.Value()))
       
   181                 {
       
   182                 MMTPType* element = RMTPType::AllocL(iElementType);
       
   183                 CleanupStack::PushL(element);
       
   184                 AppendElementChunkL(element);
       
   185                 CleanupStack::Pop(element); 
       
   186                 }
       
   187             else
       
   188                 {
       
   189                 iWriteSequenceState = EIdle;
       
   190                 }
       
   191             break;
       
   192             
       
   193         case EIdle:
       
   194             // Completing the last chunk in the write sequence.
       
   195             break;
       
   196             
       
   197         default:
       
   198             Panic(EMTPTypeBadStorage);
       
   199             break;
       
   200             }
       
   201         }
       
   202         
       
   203     if (CMTPTypeCompoundBase::CommitRequired())
       
   204         {
       
   205         CMTPTypeCompoundBase::CommitChunkL(aChunk);
       
   206         }     
       
   207     return NULL;
       
   208     }
       
   209 
       
   210 
       
   211 const CMTPTypeCompoundBase::TElementInfo& CMTPTypeList::ElementInfo(TInt aElementId) const
       
   212     {
       
   213     if (aElementId == KChunkNumberofElements)
       
   214         {
       
   215         return iInfoNumberOfElements;
       
   216         }
       
   217     else
       
   218         {
       
   219         __ASSERT_ALWAYS(((aElementId - KChunckNumberOfHeader) < iChunkNumberOfElements.Value()), Panic(EMTPTypeBoundsError));
       
   220         iInfoElement.iChunkId = aElementId;
       
   221         return iInfoElement; 
       
   222         }
       
   223     }
       
   224 
       
   225 
       
   226 TInt CMTPTypeList::ValidateChunkCount() const
       
   227     {
       
   228     if (NumberOfElements() != (ChunkCount() - 1))
       
   229         {
       
   230         return KMTPDataTypeInvalid;
       
   231         }
       
   232     
       
   233     return KErrNone;
       
   234     }
       
   235 
       
   236 TInt CMTPTypeList::UpdateWriteSequenceErr(TInt aErr)
       
   237     {
       
   238     iWriteSequenceErr = aErr;
       
   239     return iWriteSequenceErr;        
       
   240     }
       
   241 
       
   242 
       
   243 void CMTPTypeList::AppendElementChunkL(const MMTPType* aElement)
       
   244     {
       
   245     iChunksElement.AppendL(aElement);
       
   246     ChunkAppendL(*aElement);
       
   247     }
       
   248 
       
   249 EXPORT_C void CMTPTypeList::ResetAndDestroy()
       
   250     {
       
   251     const TUint num = iChunksElement.Count();
       
   252     for(TInt i = num -1  ; i  >= 0 ; i-- )
       
   253         {
       
   254         Remove(i);
       
   255         }
       
   256     
       
   257     iChunksElement.Close();
       
   258     }
       
   259