omads/omadsextensions/adapters/mediads/src/songitem.cpp
branchRCL_3
changeset 25 4f0867e42d62
parent 24 8e7494275d3a
child 26 3e6957da2ff8
equal deleted inserted replaced
24:8e7494275d3a 25:4f0867e42d62
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  CSongItem class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "songitem.h"
       
    21 
       
    22 #include <metadatautility.h>
       
    23 #include <metadatafieldcontainer.h>
       
    24 #include <metadatafield.hrh>
       
    25 
       
    26 #include "logger.h"
       
    27 #include "sconmetadatafielddefs.h"
       
    28 
       
    29 
       
    30 CSongItem::CSongItem()
       
    31     {
       
    32     }
       
    33 
       
    34 CSongItem* CSongItem::NewLC()
       
    35     {
       
    36     CSongItem* self = new (ELeave) CSongItem();
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     return self;
       
    40     }
       
    41 
       
    42 void CSongItem::ConstructL()
       
    43     {
       
    44     iUri = KNullDesC().AllocL();
       
    45     iTitle = KNullDesC().AllocL();
       
    46     iMimeType = KNullDesC().AllocL();
       
    47     iArtist = KNullDesC().AllocL();
       
    48     iAlbum = KNullDesC().AllocL();
       
    49     iGenre = KNullDesC().AllocL();
       
    50     iComment = KNullDesC().AllocL();
       
    51     iComposer = KNullDesC().AllocL();
       
    52     
       
    53     iDuration = -1;
       
    54     iYear = -1;
       
    55     iAlbumTrack = -1;
       
    56     iRating = -1;
       
    57     iSize = 0;
       
    58     }
       
    59 
       
    60 CSongItem::~CSongItem()
       
    61     {
       
    62     delete iUri;
       
    63     delete iTitle;
       
    64     delete iMimeType;
       
    65     delete iArtist;
       
    66     delete iAlbum;
       
    67     delete iGenre;
       
    68     delete iComment;
       
    69     delete iComposer;
       
    70     }
       
    71 
       
    72 void CSongItem::SetId( TInt aId )
       
    73     {
       
    74     iId = aId;
       
    75     }
       
    76 
       
    77 TInt CSongItem::Id() const
       
    78     {
       
    79     return iId;
       
    80     }
       
    81 
       
    82 void CSongItem::SetUriL( const TDesC& aUri )
       
    83     {
       
    84     delete iUri;
       
    85     iUri = NULL;
       
    86     iUri = aUri.AllocL();
       
    87     }
       
    88 
       
    89 const TDesC& CSongItem::Uri() const
       
    90     {
       
    91     return *iUri;
       
    92     }
       
    93 
       
    94 void CSongItem::SetTitleL( const TDesC& aTitle )
       
    95     {
       
    96     delete iTitle;
       
    97     iTitle = NULL;
       
    98     iTitle = aTitle.AllocL();
       
    99     }
       
   100 
       
   101 const TDesC& CSongItem::Title() const
       
   102     {
       
   103     return *iTitle;
       
   104     }
       
   105 
       
   106 void CSongItem::SetMimeTypeL( const TDesC& aMimeType )
       
   107     {
       
   108     delete iMimeType;
       
   109     iMimeType = NULL;
       
   110     iMimeType = aMimeType.AllocL();
       
   111     }
       
   112 
       
   113 const TDesC& CSongItem::MimeType() const
       
   114     {
       
   115     return *iMimeType;
       
   116     }
       
   117 
       
   118 void CSongItem::SetDuration( TInt aDuration )
       
   119     {
       
   120     iDuration = aDuration;
       
   121     }
       
   122 
       
   123 TInt CSongItem::Duration() const
       
   124     {
       
   125     return iDuration;
       
   126     }
       
   127 
       
   128 void CSongItem::SetArtistL( const TDesC& aArtist )
       
   129     {
       
   130     delete iArtist;
       
   131     iArtist = NULL;
       
   132     iArtist = aArtist.AllocL();
       
   133     }
       
   134 
       
   135 const TDesC& CSongItem::Artist() const
       
   136     {
       
   137     return *iArtist;
       
   138     }
       
   139 
       
   140 void CSongItem::SetAlbumL( const TDesC& aAlbum )
       
   141     {
       
   142     delete iAlbum;
       
   143     iAlbum = NULL;
       
   144     iAlbum = aAlbum.AllocL();
       
   145     }
       
   146 
       
   147 const TDesC& CSongItem::Album() const
       
   148     {
       
   149     return *iAlbum;
       
   150     }
       
   151 
       
   152 void CSongItem::SetYear( TInt aYear )
       
   153     {
       
   154     iYear = aYear;
       
   155     }
       
   156 
       
   157 TInt CSongItem::Year() const
       
   158     {
       
   159     return iYear;
       
   160     }
       
   161 
       
   162 void CSongItem::SetAlbumTrack( TInt aAlbumTrack )
       
   163     {
       
   164     iAlbumTrack = aAlbumTrack;
       
   165     }
       
   166 
       
   167 TInt CSongItem::AlbumTrack() const
       
   168     {
       
   169     return iAlbumTrack;
       
   170     }
       
   171 
       
   172 void CSongItem::SetGenreL( const TDesC& aGenre )
       
   173     {
       
   174     delete iGenre;
       
   175     iGenre = NULL;
       
   176     iGenre = aGenre.AllocL();
       
   177     }
       
   178 
       
   179 const TDesC& CSongItem::Genre() const
       
   180     {
       
   181     return *iGenre;
       
   182     }
       
   183 
       
   184 void CSongItem::SetCommentL( const TDesC& aComment )
       
   185     {
       
   186     delete iComment;
       
   187     iComment = NULL;
       
   188     iComment = aComment.AllocL();
       
   189     }
       
   190 
       
   191 const TDesC& CSongItem::Comment() const
       
   192     {
       
   193     return *iComment;
       
   194     }
       
   195     
       
   196 void CSongItem::SetComposerL( const TDesC& aComposer )
       
   197     {
       
   198     delete iComposer;
       
   199     iComposer = NULL;
       
   200     iComposer = aComposer.AllocL();
       
   201     }
       
   202 
       
   203 const TDesC& CSongItem::Composer() const
       
   204     {
       
   205     return *iComposer;
       
   206     }
       
   207 
       
   208 void CSongItem::SetRating( TInt aRating )
       
   209     {
       
   210     iRating = aRating;
       
   211     }
       
   212 
       
   213 TInt CSongItem::Rating() const
       
   214     {
       
   215     return iRating;
       
   216     }
       
   217 
       
   218 void CSongItem::SetModifiedTime( TTime& aTime )
       
   219     {
       
   220     iModifiedTime = aTime;
       
   221     }
       
   222 
       
   223 const TTime& CSongItem::ModifiedTime() const
       
   224     {
       
   225     return iModifiedTime;
       
   226     }
       
   227 
       
   228 void CSongItem::SetFileSize( TInt32 aSize )
       
   229     {
       
   230     iSize = aSize;
       
   231     }
       
   232 
       
   233 TInt32 CSongItem::FileSize() const
       
   234     {
       
   235     return iSize;
       
   236     }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CSongItem::ExportL
       
   240 // Exports item as specified in "Metadata format description v3.doc" document.
       
   241 // -----------------------------------------------------------------------------
       
   242 void CSongItem::ExportL( RWriteStream& aStream )
       
   243     {
       
   244     TRACE_FUNC_ENTRY;
       
   245     
       
   246     // write object header
       
   247 
       
   248     // header id
       
   249     aStream.WriteUint8L( KSconMetadataHeaderAudio );
       
   250     
       
   251     // header version
       
   252     aStream.WriteUint8L( KSconMetadataHeaderVersion );
       
   253     
       
   254     WriteUnicodeL( aStream, iTitle->Des(), KSconAudioTitle );
       
   255     WriteUnicodeL( aStream, iUri->Des(), KSconAudioFilename );
       
   256     
       
   257     if ( iDuration > -1 )
       
   258         {
       
   259         WriteUInt32FieldL( aStream, iDuration, KSconAudioDuration );
       
   260         }
       
   261     
       
   262     WriteUnicodeL( aStream, iArtist->Des(), KSconAudioArtist );
       
   263     if ( iYear > -1 )
       
   264         {
       
   265         WriteUInt16FieldL( aStream, iYear, KSconAudioYear);
       
   266         }
       
   267     if ( iAlbumTrack > -1 )
       
   268         {
       
   269         WriteUInt16FieldL( aStream, iAlbumTrack, KSconAudioAlbumTrack );
       
   270         }
       
   271     WriteUnicodeL( aStream, iAlbum->Des(), KSconAudioAlbum );
       
   272     WriteUnicodeL( aStream, iGenre->Des(), KSconAudioGenre );
       
   273     WriteUnicodeL( aStream, iComposer->Des(), KSconAudioComposer );
       
   274     WriteUnicodeL( aStream, iComment->Des(), KSconAudioComment );
       
   275 
       
   276     if ( iRating > -1 )
       
   277         {
       
   278         WriteUInt16FieldL( aStream, iRating, KSconAudioRating );
       
   279         }
       
   280     WriteUnicodeL( aStream, iMimeType->Des(), KSconAudioMimeType );
       
   281     
       
   282     aStream.WriteUint8L( KSconAudioDate );
       
   283     const TUint KDateTimeSize = 7; // Int16 + 5*Int8 = 2 + 5*1 = 7 bytes
       
   284     aStream.WriteUint32L( KDateTimeSize ); //size
       
   285     
       
   286     TDateTime time = iModifiedTime.DateTime();
       
   287     aStream.WriteInt16L( time.Year() );
       
   288     aStream.WriteInt8L( time.Month()+1 ); // range 1...12
       
   289     aStream.WriteInt8L( time.Day()+1 );
       
   290     aStream.WriteInt8L( time.Hour() );
       
   291     aStream.WriteInt8L( time.Minute() );
       
   292     aStream.WriteInt8L( time.Second() );
       
   293     
       
   294     // filesize
       
   295     WriteUInt32FieldL( aStream, iSize, KSconAudioFileSize );
       
   296     
       
   297     // read album art
       
   298     CMetaDataUtility* metaDataUtil = CMetaDataUtility::NewL();
       
   299     CleanupStack::PushL( metaDataUtil );
       
   300     
       
   301     RArray<TMetaDataFieldId> wantedFields;
       
   302     CleanupClosePushL(wantedFields);
       
   303     wantedFields.AppendL(EMetaDataJpeg);
       
   304     
       
   305     metaDataUtil->OpenFileL(iUri->Des(), wantedFields);
       
   306     
       
   307     const CMetaDataFieldContainer& metaCont = 
       
   308                                     metaDataUtil->MetaDataFieldsL();
       
   309     TPtrC8 data = metaCont.Field8( EMetaDataJpeg );
       
   310     if ( data.Length() > 0 )
       
   311         {
       
   312         WriteDataFieldL( aStream, data, KSconAudioJpeg );
       
   313         }
       
   314     
       
   315     CleanupStack::PopAndDestroy(&wantedFields);
       
   316     CleanupStack::PopAndDestroy(metaDataUtil);
       
   317     
       
   318     aStream.CommitL();
       
   319     TRACE_FUNC_EXIT;
       
   320     }
       
   321 
       
   322 
       
   323 void CSongItem::WriteUnicodeL( RWriteStream& aStream, const TDesC& aData, const TUint8 aFieldId )
       
   324     {
       
   325     aStream.WriteUint8L( aFieldId );
       
   326     aStream.WriteUint32L( aData.Size() );
       
   327     aStream.WriteL( aData, aData.Length() );
       
   328     }
       
   329 
       
   330 void CSongItem::WriteDataFieldL( RWriteStream& aStream, const TDesC8& aData, const TUint8 aFieldId )
       
   331     {
       
   332     aStream.WriteUint8L( aFieldId );
       
   333     aStream.WriteUint32L( aData.Size() );
       
   334     aStream.WriteL( aData, aData.Length() );
       
   335     }
       
   336 
       
   337 void CSongItem::WriteUInt16FieldL( RWriteStream& aStream, TUint16 aData, const TUint8 aFieldId )
       
   338     {
       
   339     aStream.WriteUint8L( aFieldId );
       
   340     const TUint KSizeOfUint16 = 2; // =sizeof(TUint16), hardcoded for performance reasons.
       
   341     aStream.WriteUint32L( KSizeOfUint16 );
       
   342     aStream.WriteUint16L( aData );
       
   343     }
       
   344 
       
   345 void CSongItem::WriteUInt32FieldL( RWriteStream& aStream, TUint32 aData, const TUint8 aFieldId )
       
   346     {
       
   347     aStream.WriteUint8L( aFieldId );
       
   348     const TUint KSizeOfUint32 = 4; // =sizeof(TUint32), hardcoded for performance reasons.
       
   349     aStream.WriteUint32L( KSizeOfUint32 );
       
   350     aStream.WriteUint32L( aData );
       
   351     }