harvester/harvesterplugins/AudioPlaylistPlugin/src/harvesteraudioplaylistplugin.cpp
changeset 20 6dfc5f825351
child 21 50bf9db68373
equal deleted inserted replaced
3:b73a2e62868f 20:6dfc5f825351
       
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <harvesterdata.h>
       
    21 
       
    22 #include <mderelation.h>
       
    23 #include <mdeobjectdef.h>
       
    24 
       
    25 #include "harvesteraudioplaylistplugin.h"
       
    26 #include "mdeobjectwrapper.h"
       
    27 #include "mdsutils.h"
       
    28 
       
    29 #include "harvesterlog.h"
       
    30 
       
    31 // for CleanupResetAndDestroyPushL
       
    32 #include <mmf/common/mmfcontrollerpluginresolver.h>
       
    33 
       
    34 CHarvesterAudioPlaylistPluginPropertyDefs::CHarvesterAudioPlaylistPluginPropertyDefs() : CBase()
       
    35     {
       
    36     }
       
    37 
       
    38 void CHarvesterAudioPlaylistPluginPropertyDefs::ConstructL(CMdEObjectDef& aObjectDef)
       
    39     {
       
    40     CMdENamespaceDef& nsDef = aObjectDef.NamespaceDef();
       
    41     
       
    42     // Property definitions
       
    43     CMdEObjectDef& objectDef = nsDef.GetObjectDefL( MdeConstants::Object::KBaseObject );
       
    44     iCreationDatePropertyDef = &objectDef.GetPropertyDefL( MdeConstants::Object::KCreationDateProperty );
       
    45     iLastModifiedDatePropertyDef = &objectDef.GetPropertyDefL( MdeConstants::Object::KLastModifiedDateProperty );
       
    46     iSizePropertyDef = &objectDef.GetPropertyDefL( MdeConstants::Object::KSizeProperty );
       
    47     iItemTypePropertyDef = &objectDef.GetPropertyDefL( MdeConstants::Object::KItemTypeProperty );
       
    48     iTitlePropertyDef = &objectDef.GetPropertyDefL( MdeConstants::Object::KTitleProperty );
       
    49     
       
    50     CMdEObjectDef& albumObjectDef = nsDef.GetObjectDefL( MdeConstants::Album::KAlbumObject );
       
    51     iAlbumTypeDef = &albumObjectDef.GetPropertyDefL( MdeConstants::Album::KTypeProperty );
       
    52     
       
    53     CMdEObjectDef& playlistObjectDef = nsDef.GetObjectDefL( MdeConstants::AudioPlaylistItem::KAudioPlaylistItem );
       
    54     iAlbumIDDef = &playlistObjectDef.GetPropertyDefL( MdeConstants::AudioPlaylistItem::KPlaylistIDProperty );
       
    55     iAudioObjectIDDef = &playlistObjectDef.GetPropertyDefL( MdeConstants::AudioPlaylistItem::KAudioObjectIDProperty );
       
    56     iPositionDef = &playlistObjectDef.GetPropertyDefL( MdeConstants::AudioPlaylistItem::KPositionProperty );
       
    57     }
       
    58 
       
    59 CHarvesterAudioPlaylistPluginPropertyDefs* CHarvesterAudioPlaylistPluginPropertyDefs::NewL(CMdEObjectDef& aObjectDef)
       
    60     {
       
    61     CHarvesterAudioPlaylistPluginPropertyDefs* self = 
       
    62         new (ELeave) CHarvesterAudioPlaylistPluginPropertyDefs();
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL( aObjectDef );
       
    65     CleanupStack::Pop( self );
       
    66     return self;
       
    67     }
       
    68 
       
    69 using namespace MdeConstants;
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CHarvesterAudioPlugin::CHarvesterAudioPlugin
       
    73 // ---------------------------------------------------------------------------
       
    74 //    
       
    75 CHarvesterAudioPlaylistPlugin::CHarvesterAudioPlaylistPlugin() : 
       
    76 	CHarvesterPlugin()
       
    77 	{
       
    78 	}
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // CHarvesterAudioPlaylistPlugin::NewL
       
    82 // ---------------------------------------------------------------------------
       
    83 //    
       
    84 CHarvesterAudioPlaylistPlugin* CHarvesterAudioPlaylistPlugin::NewL()
       
    85 	{
       
    86 	WRITELOG( "CHarvesterAudioPlaylistPlugin::NewL()" );
       
    87 	CHarvesterAudioPlaylistPlugin* self = new (ELeave) CHarvesterAudioPlaylistPlugin();
       
    88 	CleanupStack::PushL( self );
       
    89 	self->ConstructL();
       
    90 	CleanupStack::Pop( self );
       
    91 	
       
    92 	return self;
       
    93 	}
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CHarvesterAudioPlaylistPlugin::~CHarvesterAudioPlaylistPlugin
       
    97 // ---------------------------------------------------------------------------
       
    98 //    
       
    99 CHarvesterAudioPlaylistPlugin::~CHarvesterAudioPlaylistPlugin()
       
   100 	{
       
   101 	WRITELOG( "CHarvesterAudioPlaylistPlugin::~CHarvesterAudioPlaylistPlugin()" );
       
   102 	
       
   103 	delete iPlaylistParser;
       
   104 	delete iPropDefs;
       
   105 	delete iMime;
       
   106 	}
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CHarvesterAudioPlaylistPlugin::ConstructL
       
   110 // ---------------------------------------------------------------------------
       
   111 //    
       
   112 void CHarvesterAudioPlaylistPlugin::ConstructL()
       
   113 	{
       
   114 	WRITELOG( "CHarvesterAudioPlaylistPlugin::ConstructL()" );
       
   115 	
       
   116 	iPlaylistParser =  CHarvesterAudioPlaylistParser::NewL();
       
   117 	}
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // CHarvesterAudioPlaylistPlugin::HarvestL (from CHarvesterPlugin)
       
   121 // ---------------------------------------------------------------------------
       
   122 //    
       
   123 void CHarvesterAudioPlaylistPlugin::HarvestL( CHarvesterData* aHD )
       
   124 	{
       
   125 	WRITELOG( "CHarvesterAudioPlaylistPlugin::HarvestL()" );
       
   126 	
       
   127 	TRAPD( err, DoHarvestL( aHD ) );
       
   128 	
       
   129 	if ( err != KErrNone )
       
   130 	    {
       
   131 	    aHD->SetErrorCode( err );
       
   132 	    }
       
   133 	}
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // CHarvesterAudioPlaylistPlugin::DoHarvestL
       
   137 // ---------------------------------------------------------------------------
       
   138 //    
       
   139 void CHarvesterAudioPlaylistPlugin::DoHarvestL( CHarvesterData* aHD )
       
   140 	{
       
   141 	WRITELOG( "CHarvesterAudioPlaylistPlugin::DoHarvestL()" );
       
   142     CMdEObject& mdeObject = aHD->MdeObject();
       
   143         
       
   144     TBool isAdd = EFalse;
       
   145     if ( mdeObject.Placeholder() || mdeObject.Id() == KNoId ) // is a new object or placeholder
       
   146         {
       
   147         isAdd = ETrue;
       
   148         }
       
   149     else
       
   150         {
       
   151         return; // Once playlist has been imported, changes made to it by Music Player
       
   152                      // will not get written back to original playlist file. Thus changes to playlist
       
   153                      // should be done within Music Player, and MDS will prevent playlist that
       
   154                      // have already been imported to be changes so that there will not be 
       
   155                      // several versions of same playlist imported by accident.
       
   156         }
       
   157 
       
   158     GetPropertiesL( aHD, isAdd );
       
   159 	}
       
   160 
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // CHarvesterAudioPlaylistPlugin::GetPropertiesL
       
   164 // ---------------------------------------------------------------------------
       
   165 //    
       
   166 void CHarvesterAudioPlaylistPlugin::GetPropertiesL( CHarvesterData* aHD,
       
   167                                             TBool aIsAdd )
       
   168     {
       
   169     CMdEObject& mdeObject = aHD->MdeObject();
       
   170     
       
   171     // get creation time, modified time and file size
       
   172     if( !mdeObject.Placeholder() )
       
   173         {
       
   174         GetPlaceHolderPropertiesL( aHD, aIsAdd );
       
   175         }
       
   176     
       
   177     const TDesC& uri = mdeObject.Uri();
       
   178     
       
   179     TBool isSupported( GetMimeTypePropertyL( aHD, aIsAdd ) );
       
   180 
       
   181     if( isSupported )
       
   182     	{
       
   183     	GetMusicPropertiesL( aHD, aIsAdd );
       
   184     	}
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // CHarvesterAudioPlaylistPlugin::GetPlaceHolderPropertiesL
       
   189 // Get placeholder properties (creation time, modify time and file size).
       
   190 // ---------------------------------------------------------------------------
       
   191 //    
       
   192 void CHarvesterAudioPlaylistPlugin::GetPlaceHolderPropertiesL( CHarvesterData* aHD,
       
   193                                TBool aIsAdd )
       
   194     {
       
   195     CMdEObject& mdeObject = aHD->MdeObject();
       
   196     
       
   197     const TDesC& uri = mdeObject.Uri();
       
   198     
       
   199     TEntry entry;
       
   200     TInt err = iFs.Entry( uri, entry );
       
   201     
       
   202     if ( err!= KErrNone )
       
   203         {
       
   204         User::Leave( err ); // metadata cannot be gathered!
       
   205         }
       
   206     
       
   207 	TTime now;
       
   208 	now.HomeTime();
       
   209 	
       
   210     if( !iPropDefs )
       
   211         {
       
   212         CMdEObjectDef& objectDef = mdeObject.Def();
       
   213         iPropDefs = CHarvesterAudioPlaylistPluginPropertyDefs::NewL( objectDef );
       
   214         }
       
   215     
       
   216 	CMdeObjectWrapper::HandleObjectPropertyL(
       
   217                  mdeObject, *iPropDefs->iCreationDatePropertyDef, &now, aIsAdd );
       
   218 
       
   219 	CMdeObjectWrapper::HandleObjectPropertyL(
       
   220              mdeObject, *iPropDefs->iLastModifiedDatePropertyDef, &entry.iModified, aIsAdd );
       
   221 
       
   222 	CMdeObjectWrapper::HandleObjectPropertyL(
       
   223               mdeObject, *iPropDefs->iSizePropertyDef, &entry.iSize, aIsAdd );
       
   224     }
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 // CHarvesterAudioPlaylistPlugin::GetMimeTypePropertyL
       
   228 // Get mime type.
       
   229 // ---------------------------------------------------------------------------
       
   230 //    
       
   231 TBool CHarvesterAudioPlaylistPlugin::GetMimeTypePropertyL( 
       
   232 		CHarvesterData* aHD, TBool aIsAdd )
       
   233     {
       
   234     CMdEObject& mdeObject = aHD->MdeObject();
       
   235  
       
   236     TBuf<40> mimeType( KNullDesC );
       
   237     TBool supported( EFalse );
       
   238     supported = iPlaylistParser->ParseMimeType( mdeObject.Uri(), mimeType );
       
   239     delete iMime;
       
   240     iMime = NULL;
       
   241     iMime = mimeType.AllocL();
       
   242         
       
   243     if( !iPropDefs )
       
   244         {
       
   245         CMdEObjectDef& objectDef = mdeObject.Def();
       
   246         iPropDefs = CHarvesterAudioPlaylistPluginPropertyDefs::NewL( objectDef );
       
   247         }
       
   248     
       
   249     CMdeObjectWrapper::HandleObjectPropertyL( mdeObject, 
       
   250             *iPropDefs->iItemTypePropertyDef, (TAny*)&(mimeType), 
       
   251         	aIsAdd );
       
   252     
       
   253     return supported;
       
   254     }
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CHarvesterAudioPlaylistPlugin::GetMusicPropertiesL
       
   258 // ---------------------------------------------------------------------------
       
   259 //    
       
   260 void CHarvesterAudioPlaylistPlugin::GetMusicPropertiesL( CHarvesterData* aHD,
       
   261                                       TBool aIsAdd )
       
   262     {
       
   263     CMdEObject& mdeObject = aHD->MdeObject();
       
   264     
       
   265     if( !iPropDefs )
       
   266         {
       
   267         CMdEObjectDef& objectDef = mdeObject.Def();
       
   268         iPropDefs = CHarvesterAudioPlaylistPluginPropertyDefs::NewL( objectDef );
       
   269         }
       
   270     
       
   271     CMdESession& session = mdeObject.Session();
       
   272 
       
   273 #ifdef MDS_PLAYLIST_HARVESTING_ENABLED
       
   274 	TInt playlistType( MdeConstants::Album::EAlbumMusicPlaylist );
       
   275 	CMdeObjectWrapper::HandleObjectPropertyL( 
       
   276               mdeObject, *iPropDefs->iAlbumTypeDef, &playlistType, aIsAdd );
       
   277 #endif
       
   278     
       
   279     RPointerArray<HBufC> playlistItems;
       
   280     TCleanupItem cleanupItem( MdsUtils::CleanupPtrArray<HBufC>, &playlistItems );
       
   281     CleanupStack::PushL( cleanupItem );
       
   282     
       
   283     const TBool parsed( iPlaylistParser->ParseL( mdeObject.Uri(), playlistItems, iMime->Des() ) ); 
       
   284 
       
   285     if( parsed )
       
   286         {
       
   287         TTime curtime( 0 );
       
   288         curtime.UniversalTime();
       
   289 
       
   290         const TTimeIntervalSeconds timeOffsetSeconds = User::UTCOffset();
       
   291         const TTime localTime = curtime + timeOffsetSeconds;
       
   292         
       
   293         RPointerArray<CMdEObject> objectArray;
       
   294         
       
   295         CMdENamespaceDef& nsDef = session.GetDefaultNamespaceDefL();
       
   296         CMdEObjectDef& playlistObjectDef = nsDef.GetObjectDefL( MdeConstants::AudioPlaylistItem::KAudioPlaylistItem );
       
   297         
       
   298         const TInt count( playlistItems.Count() );
       
   299         for( TInt i = 0; i < count; i++)
       
   300             {
       
   301             CMdEObject* mdeAudioObject = session.GetObjectL( playlistItems[i]->Des() );
       
   302             if( mdeAudioObject )
       
   303                 {
       
   304                 CMdEObject* object = session.NewObjectLC( playlistObjectDef, Object::KAutomaticUri );
       
   305                 
       
   306                 object->AddTimePropertyL( *iPropDefs->iCreationDatePropertyDef, localTime );
       
   307                 object->AddTimePropertyL( *iPropDefs->iLastModifiedDatePropertyDef, curtime );
       
   308                 object->AddUint32PropertyL( *iPropDefs->iSizePropertyDef, 0 );
       
   309                 object->AddTextPropertyL( *iPropDefs->iItemTypePropertyDef, AudioPlaylistItem::KAudioPlaylistItemType );
       
   310                 
       
   311                 object->AddInt32PropertyL( *iPropDefs->iAlbumIDDef, mdeObject.Id() );
       
   312                 object->AddInt32PropertyL( *iPropDefs->iAudioObjectIDDef, mdeAudioObject->Id() );
       
   313                 
       
   314                 // position is considered to represent items relative position in the list
       
   315                 // if item is removed from the middle of the list, position is still valid even though
       
   316                 // the actual position value is not updated
       
   317                 object->AddInt32PropertyL( *iPropDefs->iPositionDef, (i+1) ); // playlist position starts from 1
       
   318                 objectArray.AppendL( object );
       
   319                 CleanupStack::Pop(); // object
       
   320                 }
       
   321             delete mdeAudioObject;
       
   322             }
       
   323         CleanupResetAndDestroyPushL( objectArray );
       
   324         TRAPD( addError, session.AddObjectsL( objectArray ) );
       
   325         if ( addError != KErrNone )
       
   326             {
       
   327             WRITELOG1( "CHarvesterAudioPlaylistPlugin::GetMusicPropertiesL - Add items to MDE error: %d", addError );
       
   328             }
       
   329         CleanupStack::PopAndDestroy( &objectArray );
       
   330         }
       
   331 
       
   332     CleanupStack::PopAndDestroy( &playlistItems );
       
   333     }
       
   334 
       
   335 // End of file
       
   336