mpx/commonframework/common/src/mpxmediaarray.cpp
changeset 0 a2952bb97e68
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Implementation of array of media objects
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "mpxmediaarray.h"
       
    20 #include "mpxmedia.h"
       
    21 #include "mpxcmn.h"
       
    22 
       
    23 // CONSTANTS
       
    24 //
       
    25 // Media attribute for the media object added to array
       
    26 //
       
    27 const TMPXAttributeData KMPXMediaArrayItem= {0x101FFC12, 0X01};
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CMPXMediaArray::NewL
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 EXPORT_C CMPXMediaArray* CMPXMediaArray::NewL()
       
    34     {
       
    35     CMPXMediaArray* a=new(ELeave)CMPXMediaArray();
       
    36     CleanupStack::PushL(a);
       
    37     a->ConstructL();
       
    38     CleanupStack::Pop(a);
       
    39     return a;
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CMPXMediaArray::NewL
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C CMPXMediaArray* CMPXMediaArray::NewL(const CMPXMediaArray& aArray)
       
    47     {
       
    48     CMPXMediaArray* a=new(ELeave)CMPXMediaArray();
       
    49     CleanupStack::PushL(a);
       
    50     a->ConstructL(aArray);
       
    51     CleanupStack::Pop(a);
       
    52     return a;
       
    53     }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // Destructor
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C CMPXMediaArray::~CMPXMediaArray()
       
    60     {
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // Externalize
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 EXPORT_C void CMPXMediaArray::ExternalizeL(RWriteStream& aStream) const
       
    68     {
       
    69     DoExternalizeL(aStream,MMPXData::EMediaArray);
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // Internalize. ConstructL() will have been called prior to this so there
       
    74 // will definitely be a iClientHandle, iDataHandle and iData, though there may or
       
    75 // may not be any data added. Either way, the data should be reset.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 EXPORT_C void CMPXMediaArray::InternalizeL(RReadStream& aStream)
       
    79     {
       
    80     DoInternalizeL(aStream,MMPXData::EMediaArray);
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // Total number of objects contained in array
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 EXPORT_C TInt CMPXMediaArray::Count() const
       
    88     {
       
    89     return CMPXMediaBase::Count();
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // Array assessor
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 EXPORT_C CMPXMedia* CMPXMediaArray::operator[](TInt aIndex) const
       
    97     {
       
    98     return Value(aIndex);
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // Get a pointer to the media object at the specified position within the array
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 EXPORT_C CMPXMedia* CMPXMediaArray::AtL(TInt aIndex) const
       
   106     {
       
   107     return ValueL(aIndex);
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // Appends media object to array
       
   112 // Takes the ownship of aMedia
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 EXPORT_C void CMPXMediaArray::AppendL(const CMPXMedia* aMedia)
       
   116     {
       
   117     AppendL(*aMedia);
       
   118     delete const_cast<CMPXMedia*>(aMedia);
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // Appends media object to array
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 EXPORT_C void CMPXMediaArray::AppendL(const CMPXMedia& aMedia)
       
   126     {
       
   127     User::LeaveIfError(AddToArray(aMedia,CMPXMediaBase::Count()));
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // Resets the media array
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 EXPORT_C void CMPXMediaArray::Reset()
       
   135     {
       
   136     MPX_ASSERT(iClientHandle&&iData);
       
   137     //
       
   138     ResetLocal(); // Local data
       
   139     iData->Reset(iClientHandle); // Clears heap data for this object
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // Removes the specified media from the array
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 EXPORT_C void CMPXMediaArray::Remove(TInt aIndex)
       
   147     {
       
   148     MPX_ASSERT(iData&&iClientHandle&&aIndex>=0&&aIndex<Count());
       
   149     DeleteLocal(aIndex);
       
   150     iData->Delete(iClientHandle,aIndex); // And data for this object
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // Inserts the specified media into the array at the specified position
       
   155 // Takes the ownership of aMedia
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 EXPORT_C TInt CMPXMediaArray::Insert(const CMPXMedia *aMedia, TInt aPos)
       
   159     {
       
   160     TInt r=AddToArray(*aMedia,aPos);
       
   161     if (r==KErrNone)
       
   162         {
       
   163         delete const_cast<CMPXMedia*>(aMedia);
       
   164         }
       
   165     return r;
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // Inserts the specified media into the array at the specified position
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 EXPORT_C TInt CMPXMediaArray::Insert(const CMPXMedia& aMedia, TInt aPos)
       
   173     {
       
   174     return AddToArray(aMedia,aPos);
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // Sets the specified media into the array at the specified position
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 EXPORT_C void CMPXMediaArray::Set(const CMPXMedia& aMedia, TInt aPos)
       
   182     {
       
   183     MMPXData::LockHeap( iClientHandle );
       
   184     DeleteLocal(aPos);
       
   185     (void)AddToArray(aMedia,aPos,ETrue);
       
   186     MMPXData::UnlockHeap( iClientHandle );
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // Inserts the specified media into the array at the specified position and
       
   191 // leave if an error is encountered
       
   192 // Takes ownership of the aMedia
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 EXPORT_C void CMPXMediaArray::InsertL(const CMPXMedia *aMedia, TInt aPos)
       
   196     {
       
   197     User::LeaveIfError(Insert(aMedia,aPos));
       
   198     }
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // Inserts the specified media into the array at the specified position and
       
   202 // leave if an error is encountered
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 EXPORT_C void CMPXMediaArray::InsertL(const CMPXMedia& aMedia, TInt aPos)
       
   206     {
       
   207     User::LeaveIfError(AddToArray(aMedia,aPos));
       
   208     }
       
   209 
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // The value for a specific index
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 CMPXMedia* CMPXMediaArray::Value(TInt aIndex) const
       
   216     {
       
   217     CMPXMedia* m=NULL;
       
   218     TRAP_IGNORE(m=ValueL(aIndex));
       
   219     return m;
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // The value for a specific index
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 CMPXMedia* CMPXMediaArray::ValueL(TInt aIndex) const
       
   227     {
       
   228     MPX_ASSERT(iData&&iClientHandle&&aIndex>=0&&aIndex<Count());
       
   229     //
       
   230     CMPXMedia* obj=NULL;
       
   231     //
       
   232     // Check local values to see if there and current
       
   233     //
       
   234     LockHeapLC();
       
   235     TInt uid=iData->Uid(iClientHandle,aIndex);
       
   236     const TValue* v=LocalValue(uid);
       
   237     if (!v)
       
   238         {
       
   239         TPtrC8 data=iData->Value(iClientHandle,aIndex);
       
   240         TUint* ptr=(TUint*)data.Ptr();
       
   241         //
       
   242         // Check that it's really a media object
       
   243         //
       
   244         MPX_ASSERT(*(MMPXData::TMPXObjectType*)ptr==MMPXData::EMedia);
       
   245         MPX_ASSERT(sizeof(TUint)+sizeof(TInt)==data.Size());
       
   246         //
       
   247         ++ptr; // Actual data offset
       
   248         obj=CMPXMedia::NewL(*ptr); // new media object from the data
       
   249         CleanupStack::PushL(obj);
       
   250         CMPXMediaArray& theArray=*const_cast<CMPXMediaArray*>(this);
       
   251         User::LeaveIfError(theArray.SetLocal(TValue(obj,uid,EMPXTypeCObject)));
       
   252         CleanupStack::Pop(obj);
       
   253         }
       
   254     else
       
   255         {
       
   256         obj=static_cast<CMPXMedia*>(v->iValue);
       
   257         }
       
   258     CleanupStack::PopAndDestroy(); // unlock the heap
       
   259     return obj;
       
   260     }
       
   261 
       
   262 // -----------------------------------------------------------------------------
       
   263 // Inserts the specified media into the array at the specified position
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 TInt CMPXMediaArray::AddToArray(const CMPXMedia& aMedia,TInt aPos,TBool aReplace)
       
   267     {
       
   268     MPX_ASSERT(iData&&iDataHandle&&iClientHandle);
       
   269     const TInt KMediaStreamLen=sizeof(MMPXData::TMPXObjectType)+sizeof(TUint);
       
   270     TBuf8<KMediaStreamLen> buf;
       
   271     TUint* p=(TUint*)buf.Ptr();
       
   272     *p=MMPXData::EMedia;
       
   273     *++p=aMedia.Data();
       
   274     buf.SetLength(KMediaStreamLen);
       
   275     TInt r(KErrNone);
       
   276     if (aReplace)
       
   277         {
       
   278         iData->Set(iClientHandle,KMPXMediaArrayItem,EMPXTypeCObject,buf,aPos);
       
   279         }
       
   280     else if (aPos==CMPXMediaBase::Count())
       
   281         {
       
   282         r=iData->Append(iClientHandle,KMPXMediaArrayItem,EMPXTypeCObject,buf);
       
   283         }
       
   284     else
       
   285         {
       
   286         r=iData->Insert(iClientHandle,KMPXMediaArrayItem,EMPXTypeCObject,buf,aPos);
       
   287         }
       
   288     return r;
       
   289     }
       
   290 
       
   291 // END OF FILE