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