photosgallery/viewframework/medialists/src/glxmedia.cpp
changeset 0 4e91876724a2
child 18 bcb43dc84c44
child 20 d1bdfdf534bd
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2008-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:    Media item 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "glxmedia.h"
       
    22 
       
    23 #include <glxassert.h>
       
    24 #include <glxthumbnailattributeinfo.h>
       
    25 #include <mpxmediacollectiondetaildefs.h>
       
    26 #include <mpxmediadrmdefs.h>
       
    27 #include <e32math.h>
       
    28 #include <lbsposition.h>                    // for TCoordinate
       
    29 
       
    30 #include "glxcollectiongeneraldefs.h"
       
    31 #include "glxmediastaticitemdefs.h"
       
    32 #include "glxthumbnailutility.h"
       
    33 #include <glxtracer.h>                 //For logging
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // Constructor
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 TGlxMedia::TGlxMedia( CGlxMedia& aMedia )
       
    40     {
       
    41     TRACER("TGlxMedia::Default Constructor");
       
    42     
       
    43     __ASSERT_DEBUG ( aMedia.Id() != KGlxIdNone, Panic( EGlxPanicIllegalArgument ) ); // Must have an id defined,
       
    44     iId = aMedia.Id();
       
    45     iItem = &aMedia;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // Compare two media items by id
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 EXPORT_C TBool TGlxMedia::MatchById(const TGlxMedia& aMedia1, const TGlxMedia& aMedia2)
       
    53 	{
       
    54 	TRACER("TGlxMedia::MatchById");
       
    55 	
       
    56 	return aMedia1.iId == aMedia2.iId;
       
    57 	}
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // Tests to see if the Media is a static
       
    61 // -----------------------------------------------------------------------------
       
    62 
       
    63 EXPORT_C TBool TGlxMedia::IsStatic() const
       
    64     {
       
    65     TRACER("TGlxMedia::IsStatic");
       
    66     
       
    67     if ( iItem )
       
    68         {
       
    69         return iItem->IsSupported( KGlxMediaStaticItem );
       
    70         }
       
    71         
       
    72     return EFalse;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // Gets the command associated with a static item
       
    77 // -----------------------------------------------------------------------------
       
    78 EXPORT_C TBool TGlxMedia::GetStaticItemCommand(TInt& aCmd) const
       
    79     {
       
    80     TRACER("TGlxMedia::GetStaticItemCommand");
       
    81     
       
    82     if ( iItem )
       
    83         {
       
    84         TMPXAttribute attrib(KGlxMediaIdStaticItem,
       
    85                             KGlxMediaStaticItemCommand);
       
    86         return iItem->GetValueTObject( aCmd, attrib );
       
    87         }
       
    88     // by default the cmd value is not changed    
       
    89     return EFalse;
       
    90     }
       
    91     
       
    92 // -----------------------------------------------------------------------------
       
    93 // Gets the date
       
    94 // -----------------------------------------------------------------------------
       
    95 EXPORT_C TBool TGlxMedia::GetDate(TTime& aDate) const
       
    96     {
       
    97     TRACER("TGlxMedia::GetDate");
       
    98     
       
    99     if ( iItem )
       
   100         {
       
   101         return iItem->GetValueTObject( aDate, KMPXMediaGeneralDate );
       
   102         }
       
   103     // by default the date is not changed    
       
   104     return EFalse;
       
   105     }
       
   106     
       
   107 // -----------------------------------------------------------------------------
       
   108 // Gets the last modified date
       
   109 // -----------------------------------------------------------------------------
       
   110 EXPORT_C TBool TGlxMedia::GetLastModifiedDate(TTime& aDate) const
       
   111     {
       
   112     TRACER("TGlxMedia::GetLastModifiedDate");
       
   113     
       
   114     if ( iItem )
       
   115         {
       
   116         return iItem->GetValueTObject( aDate, KGlxMediaGeneralLastModifiedDate );
       
   117         }
       
   118     // by default the date is not changed    
       
   119     return EFalse;
       
   120     }
       
   121     
       
   122 // -----------------------------------------------------------------------------
       
   123 // Gets the general category
       
   124 // -----------------------------------------------------------------------------
       
   125 EXPORT_C TMPXGeneralCategory TGlxMedia::Category() const
       
   126     {
       
   127     TRACER("TGlxMedia::Category");
       
   128     
       
   129     TMPXGeneralCategory cat =  EMPXNoCategory;
       
   130     if ( iItem )
       
   131         {
       
   132         // if this fails cat will remain as EMPXNoCategory
       
   133         iItem->GetValueTObject( cat, KMPXMediaGeneralCategory );
       
   134         }
       
   135     return cat;
       
   136     }
       
   137     
       
   138 // -----------------------------------------------------------------------------
       
   139 // Gets the dimensions
       
   140 // -----------------------------------------------------------------------------
       
   141 EXPORT_C TBool TGlxMedia::GetDimensions(TSize& aSize) const
       
   142     {
       
   143     TRACER("TGlxMedia::GetDimensions");
       
   144     
       
   145     if ( iItem )
       
   146         {
       
   147         return iItem->GetValueTObject( aSize, KGlxMediaGeneralDimensions );
       
   148         }
       
   149     // by default the size is not changed    
       
   150     return EFalse;
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // Gets the (file) size
       
   155 // -----------------------------------------------------------------------------
       
   156 EXPORT_C TBool TGlxMedia::GetSize(TInt& aSize) const
       
   157     {
       
   158     TRACER("TGlxMedia::GetSize");
       
   159     
       
   160     if ( iItem )
       
   161         {
       
   162         return iItem->GetValueTObject( aSize, KMPXMediaGeneralSize );
       
   163         }
       
   164     // by default the size is not changed    
       
   165     return EFalse;
       
   166     }
       
   167     
       
   168 // -----------------------------------------------------------------------------
       
   169 // Gets the duration 
       
   170 // -----------------------------------------------------------------------------
       
   171 EXPORT_C TBool TGlxMedia::GetDuration(TReal32& aDuration) const
       
   172     {
       
   173     TRACER("TGlxMedia::GetDuration");
       
   174     
       
   175     if ( iItem )
       
   176         {
       
   177         return iItem->GetValueTObject( aDuration, KMPXMediaGeneralDuration );
       
   178         }
       
   179     // by default the duration is not changed    
       
   180     return EFalse;
       
   181     }
       
   182     
       
   183 // -----------------------------------------------------------------------------
       
   184 // Gets the frame count
       
   185 // -----------------------------------------------------------------------------
       
   186 EXPORT_C TBool TGlxMedia::GetFrameCount(TInt& aFrameCount) const
       
   187     {
       
   188     TRACER("TGlxMedia::GetFrameCount");
       
   189     
       
   190     if ( iItem )
       
   191         {
       
   192         return iItem->GetValueTObject( aFrameCount, KGlxMediaGeneralFramecount );
       
   193         }
       
   194     // by default the frame count is not changed    
       
   195     return EFalse;
       
   196     }
       
   197     
       
   198 // -----------------------------------------------------------------------------
       
   199 // Gets the general count
       
   200 // -----------------------------------------------------------------------------
       
   201 EXPORT_C TBool TGlxMedia::GetContainedItemCount(TInt& aGeneralCount) const
       
   202     {
       
   203     TRACER("TGlxMedia::GetContainedItemCount");
       
   204     
       
   205     if ( iItem )
       
   206         {
       
   207         return iItem->GetValueTObject( aGeneralCount, KMPXMediaGeneralCount );
       
   208         }
       
   209     // by default the frame count is not changed    
       
   210     return EFalse;
       
   211     }
       
   212     
       
   213 // -----------------------------------------------------------------------------
       
   214 // Gets the general count
       
   215 // -----------------------------------------------------------------------------
       
   216 EXPORT_C TBool TGlxMedia::GetSlideshowPlayableContainedItemCount(TInt& aGeneralCount) const
       
   217     {
       
   218     TRACER("TGlxMedia::GetSlideshowPlayableContainedItemCount");
       
   219     
       
   220     if ( iItem )
       
   221         {
       
   222         return iItem->GetValueTObject( aGeneralCount, KGlxMediaGeneralSlideshowableContent );
       
   223         }
       
   224     // by default the frame count is not changed    
       
   225     return EFalse;
       
   226     }
       
   227     
       
   228 // -----------------------------------------------------------------------------
       
   229 // Gets the coordinate value
       
   230 // -----------------------------------------------------------------------------
       
   231 EXPORT_C TBool TGlxMedia::GetCoordinate(TCoordinate& aCoordinate) const
       
   232     {
       
   233     TRACER("TGlxMedia::GetCoordinate");
       
   234     
       
   235     TBool isSupported = EFalse;
       
   236     
       
   237     if ( iItem )
       
   238         {
       
   239         if (iItem->GetValueTObject( aCoordinate, KGlxMediaGeneralLocation ))
       
   240             {
       
   241             isSupported = ETrue;
       
   242             if ( Math::IsNaN( aCoordinate.Latitude() ) ||
       
   243                  Math::IsNaN( aCoordinate.Longitude() )  ||
       
   244                  aCoordinate.Datum() != KPositionDatumWgs84 )
       
   245                 {
       
   246                 isSupported = EFalse;
       
   247                 }
       
   248             }
       
   249         }
       
   250     // by default the coodinate is not changed    
       
   251     return isSupported;
       
   252     }
       
   253     
       
   254 // -----------------------------------------------------------------------------
       
   255 // Gets the IconInfo
       
   256 // -----------------------------------------------------------------------------
       
   257 EXPORT_C TBool TGlxMedia::GetIconInfo(TIconInfo& aInfo) const
       
   258     {
       
   259     TRACER("TGlxMedia::GetIconInfo");
       
   260     
       
   261     if ( iItem )
       
   262         {
       
   263         return iItem->GetValueTObject( aInfo, KMPXMediaGeneralIcon );
       
   264         }
       
   265     // by default aInfo is not changed    
       
   266     return EFalse;
       
   267     }
       
   268     
       
   269 // -----------------------------------------------------------------------------
       
   270 // Returns the DRM protection indication - defaults to ETrue if not found
       
   271 // -----------------------------------------------------------------------------
       
   272 EXPORT_C TBool TGlxMedia::IsDrmProtected() const
       
   273     {
       
   274     TRACER("TGlxMedia::IsDrmProtected");
       
   275     
       
   276     TBool isDrmProtected = ETrue;
       
   277     if ( iItem )
       
   278         {
       
   279         // ignore return value. Failure leaves isDrmProtected unchanged as ETrue
       
   280         iItem->GetValueTObject( isDrmProtected, KMPXMediaDrmProtected );
       
   281         }
       
   282     // by default is protected
       
   283     return isDrmProtected;
       
   284     }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // Gets the DRM protection
       
   288 // -----------------------------------------------------------------------------
       
   289 EXPORT_C TBool TGlxMedia::GetDrmProtected(TBool& aIsProtected) const
       
   290     {
       
   291     TRACER("TGlxMedia::GetDrmProtected");
       
   292     
       
   293     if ( iItem )
       
   294         {
       
   295         return iItem->GetValueTObject( aIsProtected, 
       
   296                                        KMPXMediaDrmProtected );
       
   297         }
       
   298     // by default aIsProtected is not changed
       
   299     return EFalse;
       
   300     }
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // Gets the DRM protection
       
   304 // -----------------------------------------------------------------------------
       
   305 EXPORT_C TBool TGlxMedia::GetDrmValidity(TGlxMediaGeneralRightsValidity& aIsValid) const
       
   306     {
       
   307     if ( iItem )
       
   308         {
       
   309         return iItem->GetValueTObject( aIsValid, 
       
   310                                        KGlxMediaGeneralDRMRightsValid );
       
   311         }
       
   312     // by default aIsValid is not changed
       
   313     return EFalse;
       
   314     }
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // Gets the System Item indicator
       
   318 // -----------------------------------------------------------------------------
       
   319 EXPORT_C TBool TGlxMedia::GetSystemItem(TBool& aIsSystemItem) const
       
   320     {
       
   321     TRACER("TGlxMedia::GetSystemItem");
       
   322     
       
   323     if ( iItem )
       
   324         {
       
   325         return iItem->GetValueTObject( aIsSystemItem, 
       
   326                                        KGlxMediaGeneralSystemItem );
       
   327         }
       
   328     // by default aIsSystemItem is not changed
       
   329     return EFalse;
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // Gets the title
       
   334 // -----------------------------------------------------------------------------
       
   335 EXPORT_C const TDesC& TGlxMedia::Title() const
       
   336     {
       
   337     TRACER("TGlxMedia::Title");
       
   338     
       
   339     if ( iItem )
       
   340         {
       
   341         return iItem->ValueText( KMPXMediaGeneralTitle );
       
   342         }
       
   343     return KNullDesC;    
       
   344     }
       
   345     
       
   346 // -----------------------------------------------------------------------------
       
   347 // Gets the subtitle
       
   348 // -----------------------------------------------------------------------------
       
   349 EXPORT_C const TDesC& TGlxMedia::SubTitle() const
       
   350     {
       
   351     TRACER("TGlxMedia::SubTitle");
       
   352     
       
   353     if ( iItem )
       
   354         {
       
   355         return iItem->ValueText( KGlxMediaCollectionPluginSpecificSubTitle );
       
   356         }
       
   357     return KNullDesC;    
       
   358     }
       
   359     
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // Gets the comment
       
   363 // -----------------------------------------------------------------------------
       
   364 EXPORT_C const TDesC& TGlxMedia::Comment() const
       
   365     {
       
   366     TRACER("TGlxMedia::Comment");
       
   367     
       
   368     if ( iItem )
       
   369         {
       
   370         return iItem->ValueText( KMPXMediaGeneralComment );
       
   371         }
       
   372     return KNullDesC;    
       
   373     }
       
   374     
       
   375 // -----------------------------------------------------------------------------
       
   376 // Gets the URI indicating the success in the return value
       
   377 // -----------------------------------------------------------------------------
       
   378 EXPORT_C const TDesC&  TGlxMedia::Uri() const
       
   379     {
       
   380     TRACER("TGlxMedia::Uri");
       
   381     
       
   382     if( iItem )
       
   383         {
       
   384         return iItem->ValueText( KMPXMediaGeneralUri);
       
   385         }
       
   386     // by default descriptor pointer is unchanged
       
   387     return KNullDesC;
       
   388     }
       
   389     
       
   390 // -----------------------------------------------------------------------------
       
   391 // Gets the MimeType indicating the success in the return value
       
   392 // -----------------------------------------------------------------------------
       
   393 EXPORT_C const TDesC&  TGlxMedia::MimeType() const
       
   394     {
       
   395     TRACER("TGlxMedia::MimeType");
       
   396     
       
   397     if( iItem )
       
   398         {
       
   399         return iItem->ValueText( KMPXMediaGeneralMimeType);
       
   400         }
       
   401     // by default descriptor pointer is unchanged
       
   402     return KNullDesC;
       
   403     }
       
   404     
       
   405     
       
   406 // -----------------------------------------------------------------------------
       
   407 // Gets a thumbnail attribute
       
   408 // -----------------------------------------------------------------------------
       
   409 EXPORT_C const CGlxThumbnailAttribute* TGlxMedia::ThumbnailAttribute(
       
   410                                                 TMPXAttribute& aAttribute) const
       
   411     {
       
   412     TRACER("TGlxMedia::ThumbnailAttribute");
       
   413     
       
   414     if( iItem && KGlxMediaIdThumbnail == aAttribute.ContentId() )
       
   415         {
       
   416         return static_cast<const CGlxThumbnailAttribute*>
       
   417                                   ( iItem->ValueCObject(aAttribute) );
       
   418         }
       
   419     else
       
   420         {
       
   421         return NULL;
       
   422         }
       
   423     }
       
   424     
       
   425 // ---------------------------------------------------------------------------
       
   426 // GetClosestThumbnail
       
   427 // ---------------------------------------------------------------------------
       
   428 //
       
   429 EXPORT_C TBool TGlxMedia::GetClosestThumbnail( TMPXAttribute& aAttribute,
       
   430                             const TSize& aDesiredSize, TBool aDrmValid ) const
       
   431     {
       
   432     TRACER("TGlxMedia::GetClosestThumbnail");
       
   433     
       
   434     if ( iItem )
       
   435         {
       
   436         TInt index = GlxThumbnailUtility::ClosestThumbnail( aDesiredSize,
       
   437                                                         *iItem, aDrmValid );
       
   438 
       
   439         if ( index >= 0 )
       
   440             {
       
   441             aAttribute = iItem->Attribute(index);
       
   442 
       
   443             return ETrue;
       
   444             }
       
   445         }
       
   446 
       
   447     return EFalse;
       
   448     }
       
   449 
       
   450 // -----------------------------------------------------------------------------
       
   451 // Is the media playable in slideshow
       
   452 // -----------------------------------------------------------------------------
       
   453 //
       
   454 EXPORT_C TBool TGlxMedia::IsSlideShowPlayableContent() const
       
   455     {
       
   456     TRACER("TGlxMedia::IsSlideShowPlayableContent");
       
   457     
       
   458     if (iItem)
       
   459         {
       
   460     	// Only DRM protected images are allowed
       
   461         if (iItem->IsSupported(KMPXMediaGeneralCategory) && 
       
   462             iItem->IsSupported(KMPXMediaDrmProtected) && 
       
   463             iItem->IsSupported(KGlxMediaGeneralFramecount))
       
   464             {
       
   465             if (iItem->ValueTObject<TInt>(KMPXMediaGeneralCategory) == EMPXImage &&
       
   466                 !iItem->ValueTObject<TInt>(KMPXMediaDrmProtected) && 
       
   467                 iItem->ValueTObject<TInt>(KGlxMediaGeneralFramecount) == 1)
       
   468             	{
       
   469                 return ETrue;                	
       
   470             	}
       
   471             }
       
   472         // The media item is either not DRM protected, a video or an animated gif    
       
   473         }
       
   474     return EFalse;    			
       
   475     }
       
   476 
       
   477 // -----------------------------------------------------------------------------
       
   478 // Returns the IdSpaceId associated with the media
       
   479 // -----------------------------------------------------------------------------
       
   480 //
       
   481 EXPORT_C TGlxIdSpaceId TGlxMedia::IdSpaceId() const
       
   482     {
       
   483     TRACER("TGlxMedia::IdSpaceId");
       
   484     
       
   485     TGlxIdSpaceId idSpaceId( KGlxIdNone );
       
   486 
       
   487     if ( iItem )
       
   488         {
       
   489         iItem->GetValueTObject( idSpaceId, KMPXMediaColDetailSpaceId );
       
   490         }
       
   491 
       
   492     return idSpaceId;
       
   493     }
       
   494 
       
   495 // -----------------------------------------------------------------------------
       
   496 // Set media pointer
       
   497 // -----------------------------------------------------------------------------
       
   498 //
       
   499 void TGlxMedia::SetMedia( CGlxMedia* aMedia, MGlxMediaUser& aMediaUser, TInt aIndex )
       
   500 	{
       
   501 	TRACER("TGlxMedia::SetMedia");
       
   502 	
       
   503     __ASSERT_DEBUG( aMedia == NULL || !iItem , Panic(EGlxPanicAlreadyInitialised ) ); // Already has media
       
   504     if ( aMedia )
       
   505         {
       
   506         // Media object being added
       
   507     	aMedia->AddUser( aMediaUser, aIndex );
       
   508         }
       
   509     else if ( iItem )
       
   510         {
       
   511         // Media object being set to null, remove since old media object exists
       
   512     	iItem->RemoveUser( aMediaUser );
       
   513         }
       
   514 	iItem = aMedia;
       
   515 	}
       
   516 
       
   517 // -----------------------------------------------------------------------------
       
   518 // Update media
       
   519 // -----------------------------------------------------------------------------
       
   520 //
       
   521 void TGlxMedia::UpdateMedia( MGlxMediaUser& aMediaUser, TInt aIndex )
       
   522     {
       
   523     TRACER("TGlxMedia::UpdateMedia");
       
   524     
       
   525     if ( iItem )
       
   526         {
       
   527         iItem->UpdateUser( aMediaUser, aIndex );
       
   528         }
       
   529 	}
       
   530    
       
   531 // -----------------------------------------------------------------------------
       
   532 // Constructor
       
   533 // -----------------------------------------------------------------------------
       
   534 //
       
   535 EXPORT_C CGlxMedia::CGlxMedia(const TGlxMediaId& aId)
       
   536 		: iId(aId), iUsers(2) // On average, two list as users assumed
       
   537     {
       
   538     TRACER("CGlxMedia::Default Constructor");
       
   539     }
       
   540 
       
   541 // -----------------------------------------------------------------------------
       
   542 // Destructor
       
   543 // -----------------------------------------------------------------------------
       
   544 //
       
   545 EXPORT_C CGlxMedia::~CGlxMedia()
       
   546     {
       
   547     TRACER("CGlxMedia::Destructor");
       
   548     
       
   549     iUsers.Close();
       
   550 
       
   551     Reset();
       
   552     iValues.Close(); 
       
   553     iAttributes.Close();
       
   554     }
       
   555 
       
   556 // -----------------------------------------------------------------------------
       
   557 // Make space in the array of users, so can safely append later
       
   558 // -----------------------------------------------------------------------------
       
   559 //
       
   560 void CGlxMedia::ReserveUsersL(TInt aReservationCount)
       
   561 	{
       
   562 	TRACER("CGlxMedia::ReserveUsersL");
       
   563 	
       
   564 	iUsers.ReserveL( aReservationCount );
       
   565     __DEBUG_ONLY( _iUserReservationCount = aReservationCount );
       
   566 	}
       
   567 
       
   568 // -----------------------------------------------------------------------------
       
   569 // Adds a user to the list
       
   570 // -----------------------------------------------------------------------------
       
   571 //
       
   572 void CGlxMedia::AddUser( MGlxMediaUser& aUser, TInt aIndex ) 
       
   573 	{
       
   574 	TRACER("CGlxMedia::AddUser");
       
   575 	
       
   576 	// Must not exist already
       
   577 	__ASSERT_DEBUG( !IsUser( aUser ), Panic( EGlxPanicAlreadyAdded ) ); 
       
   578 	// Client must have made a reservation before calling
       
   579 	__ASSERT_DEBUG( _iUserReservationCount > 0, Panic( EGlxPanicLogicError ) );
       
   580 
       
   581     __DEBUG_ONLY( _iUserReservationCount-- );
       
   582 
       
   583 	iUsers.Append( TMediaUser( &aUser, aIndex ) ); // Ignore error, cannot fail since reservation made 
       
   584 	}
       
   585 
       
   586 // -----------------------------------------------------------------------------
       
   587 // RemoveUser
       
   588 // -----------------------------------------------------------------------------
       
   589 //
       
   590 void CGlxMedia::RemoveUser( const MGlxMediaUser& aUser ) 
       
   591 	{
       
   592 	TRACER("CGlxMedia::RemoveUser");
       
   593 	
       
   594     TMediaUser mediaUser( const_cast<MGlxMediaUser*>( &aUser ) );
       
   595 	TInt index = iUsers.Find( mediaUser, &TMediaUser::MatchUser );	
       
   596     if ( KErrNotFound != index )
       
   597         {
       
   598     	iUsers.Remove( index );
       
   599         }
       
   600 	}
       
   601 
       
   602 // -----------------------------------------------------------------------------
       
   603 // IsUser
       
   604 // -----------------------------------------------------------------------------
       
   605 //
       
   606 TBool CGlxMedia::IsUser( const MGlxMediaUser& aUser ) const
       
   607 	{
       
   608 	TRACER("CGlxMedia::IsUser");
       
   609 	
       
   610     TMediaUser mediaUser( const_cast<MGlxMediaUser*>( &aUser ) );
       
   611 	return ( KErrNotFound != iUsers.Find( mediaUser, &TMediaUser::MatchUser ) );
       
   612 	}
       
   613 
       
   614 // -----------------------------------------------------------------------------
       
   615 // Return user by index
       
   616 // -----------------------------------------------------------------------------
       
   617 //
       
   618 MGlxMediaUser& CGlxMedia::User( TInt aIndex ) const
       
   619 	{
       
   620 	TRACER("CGlxMedia::User");
       
   621 	
       
   622 	return *( iUsers[ aIndex ].iMediaUser );
       
   623 	}
       
   624 
       
   625 // -----------------------------------------------------------------------------
       
   626 // Return index in the media list user
       
   627 // -----------------------------------------------------------------------------
       
   628 //
       
   629 TInt CGlxMedia::IndexInUser(TInt aIndex) const
       
   630     {
       
   631     TRACER("CGlxMedia::IndexInUser");
       
   632     
       
   633 	  return iUsers[ aIndex ].iIndex;
       
   634     }
       
   635 
       
   636 // -----------------------------------------------------------------------------
       
   637 // Update index in the media list user
       
   638 // -----------------------------------------------------------------------------
       
   639 //
       
   640 void CGlxMedia::UpdateUser( MGlxMediaUser& aUser, TInt aIndex )
       
   641     {
       
   642     TRACER("CGlxMedia::UpdateUser");
       
   643     
       
   644     TMediaUser mediaUser( &aUser );
       
   645     TInt index = iUsers.Find( mediaUser, &TMediaUser::MatchUser );	
       
   646     if ( KErrNotFound != index )
       
   647         {
       
   648         iUsers[ index ].iIndex = aIndex;
       
   649         }
       
   650 	}
       
   651 
       
   652 // -----------------------------------------------------------------------------
       
   653 // Return the number of users
       
   654 // -----------------------------------------------------------------------------
       
   655 //
       
   656 TInt CGlxMedia::UserCount() const
       
   657 	{
       
   658 	TRACER("CGlxMedia::UserCount");
       
   659 	
       
   660 	return iUsers.Count();
       
   661 	}
       
   662 
       
   663 // -----------------------------------------------------------------------------
       
   664 // The value for a specific attribute
       
   665 // -----------------------------------------------------------------------------
       
   666 //
       
   667 EXPORT_C const TDesC& CGlxMedia::ValueText(const TMPXAttribute& aAttribute) const
       
   668     {
       
   669     TRACER("CGlxMedia::ValueText");
       
   670     
       
   671     TInt i = Index(aAttribute);
       
   672     HBufC* ptr = NULL;
       
   673     if (i == KErrNotFound)
       
   674         {
       
   675         return KNullDesC;
       
   676         }
       
   677 
       
   678    // __ASSERT_DEBUG(EMPXTypeText == iValues[i].iType,
       
   679    //                                     Panic(EGlxPanicWrongAttributeType));
       
   680    if(EMPXTypeText == iValues[i].iType)
       
   681     {
       
   682     ptr = reinterpret_cast<HBufC*>(iValues[i].iValue);
       
   683     __ASSERT_DEBUG(ptr != NULL, Panic(EGlxPanicNullPointer));
       
   684     }
       
   685     
       
   686     return *ptr;
       
   687     }   
       
   688 
       
   689 // -----------------------------------------------------------------------------
       
   690 // The text from a specific attribute, indicating success
       
   691 // -----------------------------------------------------------------------------
       
   692 //
       
   693 EXPORT_C TBool CGlxMedia::GetValueText(TPtrC& aText, 
       
   694                                        const TMPXAttribute& aAttribute) const
       
   695     {
       
   696     TRACER("CGlxMedia::GetValueText");
       
   697     
       
   698     TInt i = Index(aAttribute);
       
   699     if (i == KErrNotFound)
       
   700         {
       
   701         // leave aText unchanged
       
   702         return EFalse;
       
   703         }
       
   704 
       
   705     __ASSERT_DEBUG(EMPXTypeText == iValues[i].iType,
       
   706                                         Panic(EGlxPanicWrongAttributeType));
       
   707     HBufC* ptr = reinterpret_cast<HBufC*>(iValues[i].iValue);
       
   708     __ASSERT_DEBUG(ptr != NULL, Panic(EGlxPanicNullPointer));
       
   709     aText.Set(*ptr);
       
   710     return ETrue;
       
   711     }   
       
   712 
       
   713 // -----------------------------------------------------------------------------
       
   714 // The value for a specific attribute
       
   715 // -----------------------------------------------------------------------------
       
   716 //
       
   717 EXPORT_C const CBase* CGlxMedia::ValueCObject(const TMPXAttribute& aAttribute) const
       
   718     {
       
   719     TRACER("CGlxMedia::ValueCObject");
       
   720     
       
   721     TInt i = Index(aAttribute);
       
   722 	if (i == KErrNotFound)
       
   723         {
       
   724         return NULL;
       
   725         }
       
   726 
       
   727     __ASSERT_DEBUG(EMPXTypeCObject == iValues[i].iType,
       
   728                                         Panic(EGlxPanicWrongAttributeType));
       
   729 	CBase* ptr = reinterpret_cast<CBase*>(iValues[i].iValue);
       
   730 	__ASSERT_DEBUG(ptr != NULL, Panic(EGlxPanicNullPointer));
       
   731    	return ptr;
       
   732     }   
       
   733 
       
   734 // -----------------------------------------------------------------------------
       
   735 // Clears all the attributes and values
       
   736 // -----------------------------------------------------------------------------
       
   737 //
       
   738 EXPORT_C void CGlxMedia::Reset() 
       
   739 	{
       
   740 	TRACER("CGlxMedia::Reset");
       
   741 	
       
   742 	// Delete attribute values
       
   743 	TInt count = iValues.Count();
       
   744 	__ASSERT_DEBUG(iAttributes.Count() == count, Panic(EGlxPanicLogicError));
       
   745 	for (TInt i = 0; i < count; i++) 
       
   746 		{
       
   747 		Delete(iValues[i]);
       
   748 		}
       
   749 	
       
   750 	// Remove also attributes and pointers to values
       
   751     iValues.Reset(); 
       
   752     iAttributes.Reset();
       
   753 	}
       
   754 
       
   755 // -----------------------------------------------------------------------------
       
   756 // Deletes the value
       
   757 // -----------------------------------------------------------------------------
       
   758 //
       
   759 void CGlxMedia::Delete(TValue& aValue) 
       
   760 	{
       
   761 	TRACER("CGlxMedia::Delete");
       
   762 	
       
   763 	TMPXAttributeType type = aValue.iType;
       
   764 	if (type == EMPXTypeText)
       
   765 		{
       
   766 		delete reinterpret_cast<HBufC*>(aValue.iValue);
       
   767 		}
       
   768 	else if (type == EMPXTypeCObject)
       
   769 		{
       
   770 		delete reinterpret_cast<CBase*>(aValue.iValue);
       
   771 		}
       
   772 	else if (type == EMPXTypeTObject) 
       
   773 		{
       
   774 		User::Free(aValue.iValue);
       
   775 		}
       
   776 	}
       
   777 
       
   778 // -----------------------------------------------------------------------------
       
   779 // Sets or modifies the value
       
   780 // -----------------------------------------------------------------------------
       
   781 //
       
   782 EXPORT_C void CGlxMedia::SetTextValueL(const TMPXAttribute& aAttribute,
       
   783                             		   const TDesC& aValue) 
       
   784 	{
       
   785 	TRACER("CGlxMedia::SetTextValueL");
       
   786 	
       
   787 	// Allocate first for leave safely
       
   788 	HBufC* buf = aValue.AllocLC();
       
   789 	
       
   790     TInt i = Index(aAttribute);
       
   791     if (i != KErrNotFound)
       
   792         {
       
   793        // Replace the existing value
       
   794         TValue& value = iValues[i];
       
   795         Delete(value);
       
   796         
       
   797         value.iValue = buf;
       
   798         value.iType = EMPXTypeText;
       
   799         }
       
   800     else
       
   801         {
       
   802         iValues.AppendL(TValue(buf, EMPXTypeText));
       
   803         TInt err = iAttributes.Append(aAttribute);
       
   804         if (err != KErrNone)
       
   805         	{
       
   806         	// Failed, undo the append
       
   807         	iValues.Remove(iValues.Count() - 1);
       
   808       		User::Leave(err);
       
   809         	}
       
   810 		}
       
   811         
       
   812     CleanupStack::Pop(buf);
       
   813 	}
       
   814 	
       
   815 // -----------------------------------------------------------------------------
       
   816 // Sets or modifies the value
       
   817 // -----------------------------------------------------------------------------
       
   818 //
       
   819 EXPORT_C void CGlxMedia::SetCObjectValueL(const TMPXAttribute& aAttribute,
       
   820                             		   	  CBase* aValue_TakesOwnership) 
       
   821 	{
       
   822 	TRACER("CGlxMedia::SetCObjectValueL");
       
   823 	
       
   824     TInt i = Index(aAttribute);
       
   825     if (i != KErrNotFound)
       
   826         {
       
   827  	    // Replace the existing value
       
   828         TValue& value = iValues[i];
       
   829         Delete(value);
       
   830         
       
   831         value.iValue = aValue_TakesOwnership;
       
   832         value.iType = EMPXTypeCObject;
       
   833         }
       
   834     else
       
   835         {
       
   836         iValues.AppendL(TValue(aValue_TakesOwnership, EMPXTypeCObject));
       
   837         TInt err = iAttributes.Append(aAttribute);
       
   838         if (err != KErrNone)
       
   839         	{
       
   840         	// Failed, undo the append
       
   841         	iValues.Remove(iValues.Count() - 1);
       
   842         	User::Leave(err);
       
   843         	}
       
   844         }
       
   845 	}
       
   846 
       
   847 // -----------------------------------------------------------------------------
       
   848 // DeleteAttribute
       
   849 // -----------------------------------------------------------------------------
       
   850 //
       
   851 EXPORT_C void CGlxMedia::DeleteAttribute(const TMPXAttribute& aAttribute)
       
   852     {
       
   853     TRACER("CGlxMedia::DeleteAttribute");
       
   854     
       
   855     TInt index = Index( aAttribute );
       
   856     if ( KErrNotFound != index )
       
   857         {
       
   858         DeleteAttribute( index );
       
   859         }
       
   860     }
       
   861 
       
   862 // -----------------------------------------------------------------------------
       
   863 // AttributeTypeId
       
   864 // -----------------------------------------------------------------------------
       
   865 //
       
   866 TMPXAttributeType CGlxMedia::AttributeTypeId(const TMPXAttribute& aAttribute) const
       
   867 	{
       
   868 	TRACER("CGlxMedia::AttributeTypeId");
       
   869 	
       
   870 	TMPXAttributeType type(EMPXTypeUnknown);
       
   871     TInt index(Index(aAttribute));
       
   872 	if( index != KErrNotFound)
       
   873 		{
       
   874 		return iValues[index].iType;
       
   875 		}
       
   876 	return type;
       
   877 	
       
   878 	}
       
   879 // -----------------------------------------------------------------------------
       
   880 // DeleteAttribute
       
   881 // -----------------------------------------------------------------------------
       
   882 //
       
   883 void CGlxMedia::DeleteAttribute( TInt aAttributeIndex )
       
   884     {
       
   885     TRACER("CGlxMedia::DeleteAttribute");
       
   886     
       
   887     GLX_ASSERT_DEBUG( 0 <= aAttributeIndex && aAttributeIndex < iValues.Count(), 
       
   888         Panic(EGlxPanicIllegalArgument), "deleting attribute out of bounds");
       
   889 
       
   890     // Delete the existing value
       
   891     TValue& value = iValues[aAttributeIndex];
       
   892     Delete( value );
       
   893     iValues.Remove( aAttributeIndex );
       
   894     iAttributes.Remove( aAttributeIndex );
       
   895     }
       
   896     
       
   897 // -----------------------------------------------------------------------------
       
   898 // Handles modification of item
       
   899 // -----------------------------------------------------------------------------
       
   900 EXPORT_C void CGlxMedia::HandleModified(const RArray<TMPXAttribute>& aAttributes)
       
   901 	{
       
   902 	TRACER("CGlxMedia::HandleModified");
       
   903 	
       
   904 	TInt attributeCount = aAttributes.Count();
       
   905 	if (attributeCount > 0)
       
   906 		{
       
   907 		// Remove specified attributes
       
   908 		for (TInt count = 0; count < attributeCount; ++count)
       
   909 			{
       
   910 			DeleteAttribute(aAttributes[count]);
       
   911 			}
       
   912 		}
       
   913 	else
       
   914 		{
       
   915 		// Remove all attributes
       
   916 		Reset();
       
   917 		}
       
   918 	}
       
   919 
       
   920 // -----------------------------------------------------------------------------
       
   921 // Sets generic TAny* value
       
   922 // -----------------------------------------------------------------------------
       
   923 EXPORT_C void CGlxMedia::SetValueL(const TMPXAttribute& aAttribute, TAny* aValue, TMPXAttributeType aType)
       
   924     {
       
   925     TRACER("CGlxMedia::SetValueL");
       
   926     
       
   927     TInt i = Index(aAttribute);
       
   928     if (KErrNotFound != i)
       
   929         {
       
   930         // Replace the existing value
       
   931         TValue& value = iValues[i];
       
   932         Delete(value);
       
   933 
       
   934         value.iValue = aValue; // Takes ownership
       
   935         value.iType = aType;
       
   936         }
       
   937     else
       
   938         {
       
   939         TInt err = iValues.Append(TValue(aValue, aType));
       
   940 
       
   941         // If appended value ok, try to append the attribute.
       
   942         if (KErrNone == err)
       
   943             {
       
   944             err = iAttributes.Append(aAttribute);
       
   945             if (err != KErrNone)
       
   946                 {
       
   947                 // Failed, undo the append
       
   948                 iValues.Remove(iValues.Count() - 1);
       
   949                 }
       
   950             }
       
   951 
       
   952         if (KErrNone != err)
       
   953             {
       
   954             User::Free(aValue);
       
   955             User::Leave(err);
       
   956             }
       
   957         }
       
   958     }
       
   959     
       
   960 
       
   961 // ---------------------------------------------------------------------------
       
   962 // Find match by MGlxMediaUser
       
   963 // ---------------------------------------------------------------------------
       
   964 //
       
   965 TBool CGlxMedia::TMediaUser::MatchUser(const TMediaUser& aUser1, const TMediaUser& aUser2)
       
   966     {
       
   967     TRACER("CGlxMedia::TMediaUser::MatchUser");
       
   968     
       
   969     return aUser1.iMediaUser == aUser2.iMediaUser;
       
   970     }
       
   971 
       
   972 // -----------------------------------------------------------------------------
       
   973 // Constructor
       
   974 // -----------------------------------------------------------------------------
       
   975 //
       
   976 CGlxMedia::TMediaUser::TMediaUser(MGlxMediaUser* aMediaUser, TInt aIndex) :
       
   977         iMediaUser( aMediaUser ),
       
   978         iIndex( aIndex )
       
   979     {
       
   980     TRACER("CGlxMedia::TMediaUser::Default Constructor");
       
   981     
       
   982     }
       
   983 
       
   984 // ---------------------------------------------------------------------------
       
   985 // Test invariant
       
   986 // ---------------------------------------------------------------------------
       
   987 void CGlxMedia::__DbgTestInvariant() const
       
   988     {
       
   989     TRACER("CGlxMedia::__DbgTestInvariant");
       
   990     
       
   991     #ifdef _DEBUG
       
   992     
       
   993         __ASSERT_DEBUG( Id() != KGlxIdNone, Panic(EGlxPanicIllegalState));
       
   994 	    // Make sure each list is a user only once
       
   995 	    TInt userCount = iUsers.Count();
       
   996 	    for (TInt i = 0; i < userCount; i++) 
       
   997 	    	{
       
   998 		    for (TInt j = i + 1; j < userCount; j++) 
       
   999 		    	{
       
  1000 			    __ASSERT_DEBUG( !TMediaUser::MatchUser( iUsers[i], iUsers[j] ), Panic(EGlxPanicIllegalState));
       
  1001 		    	}
       
  1002 	    	}
       
  1003 
       
  1004     #endif // _DEBUG
       
  1005     }
       
  1006 
       
  1007 // -----------------------------------------------------------------------------
       
  1008 // Delete location attribute
       
  1009 // -----------------------------------------------------------------------------
       
  1010 EXPORT_C void TGlxMedia::DeleteLocationAttribute()
       
  1011     {
       
  1012     TRACER("TGlxMedia::DeleteLocationAttribute");
       
  1013     if ( iItem )
       
  1014         {
       
  1015         iItem->DeleteAttribute( KGlxMediaGeneralLocation );
       
  1016         }
       
  1017  
       
  1018      }