mmappcomponents/harvester/server/src/mpxharvesterengine.cpp
changeset 0 a2952bb97e68
child 19 51035f0751c2
child 27 cbb1bfb7ebfb
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation file for the Harvester Engine
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #ifdef RD_MULTIPLE_DRIVE
       
    21 #include <driveinfo.h>
       
    22 #endif //RD_MULTIPLE_DRIVE
       
    23 #include <mpxlog.h>
       
    24 #include <mpxmedia.h>
       
    25 #include <mpxcollectionutility.h>
       
    26 #include <mpxcollectionmessage.h>
       
    27 #include <mpxtaskqueue.h>
       
    28 #include <mpxplaylistengine.h>
       
    29 #include <mpxplaylistpluginhandler.h>
       
    30 #include <mpxcmn.h>
       
    31 #include <mpxmediageneraldefs.h>
       
    32 #include <mpxplaybackutility.h>
       
    33 #include <mpxcommandgeneraldefs.h>
       
    34 
       
    35 // Harvester includes
       
    36 #include "mpxharvestercommon.h"
       
    37 #include "mpxharvesterengine.h"
       
    38 #include "mpxfsformatmonitor.h"
       
    39 #include "mpxmediaremovalmonitor.h"
       
    40 #include "mpxusbeventhandler.h"
       
    41 #include "mpxmmcejectmonitor.h"
       
    42 #include "mpxharvesterfilehandler.h"
       
    43 #include "mpxharvesterengineobserver.h"
       
    44 #include "mpxhvsmsg.h"
       
    45 
       
    46 // ======== MEMBER FUNCTIONS ========
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Constructor
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CMPXHarvesterEngine::CMPXHarvesterEngine() : iDiskOpActive( EFalse )
       
    53     {
       
    54     }
       
    55 
       
    56 // ----------------------------------------------------------------------------
       
    57 // Destructor
       
    58 // ----------------------------------------------------------------------------
       
    59 //
       
    60 CMPXHarvesterEngine::~CMPXHarvesterEngine()
       
    61     {
       
    62     MPX_DEBUG1("CMPXHarvesterEngine::~CMPXHarvesterEngine <---");
       
    63 
       
    64     delete iFormatMonitor;
       
    65     delete iMediaRemovalMonitor;
       
    66     delete iUSBMonitor;
       
    67     delete iMMCMonitor;
       
    68 
       
    69     delete iFileHandler;
       
    70     iFsSession.Close();
       
    71 
       
    72     if( iTaskQueue )
       
    73         {
       
    74         iTaskQueue->Reset();
       
    75         }
       
    76     delete iTaskQueue;
       
    77     delete iPlaylistEngine;
       
    78 
       
    79     if( iTempCollectionUtil )
       
    80         {
       
    81         iTempCollectionUtil->Close();
       
    82         }
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // 2nd Phase Constructor`
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 void CMPXHarvesterEngine::ConstructL()
       
    90     {
       
    91     MPX_DEBUG1("CMPXHarvesterEngine::ConstructL <---");
       
    92 
       
    93     User::LeaveIfError(iFsSession.Connect());
       
    94 
       
    95     iFsSession.ShareProtected();
       
    96 
       
    97     // Format handler for Removable Drive
       
    98     iFormatMonitor = CMPXFsFormatMonitor::NewL( *this );
       
    99 
       
   100     // MMC Removal monitor for Removable Drive
       
   101     TInt removableDrive( EDriveE );
       
   102 #ifdef RD_MULTIPLE_DRIVE
       
   103     User::LeaveIfError( DriveInfo::GetDefaultDrive(
       
   104         DriveInfo::EDefaultRemovableMassStorage,
       
   105         removableDrive ) );
       
   106 #endif // RD_MULTIPLE_DRIVE
       
   107     iMediaRemovalMonitor = CMPXMediaRemovalMonitor::NewL(
       
   108         removableDrive, iFsSession, *this );
       
   109 
       
   110     // USB Event monitor
       
   111     iUSBMonitor = CMPXUsbEventHandler::NewL( *this );
       
   112 
       
   113     // MMC Event handling
       
   114     iMMCMonitor = CMPXMMCEjectMonitor::NewL( *this );
       
   115 
       
   116     // File handler to handle file related events
       
   117     iFileHandler = CMPXHarvesterFileHandler::NewL( iFsSession );
       
   118 
       
   119     // Active task queue for async requests
       
   120     iTaskQueue = CMPXActiveTaskQueue::NewL();
       
   121 
       
   122     // Playlist Engine for export/import playlist
       
   123     iPlaylistEngine = CMPXPlaylistEngine::NewL( *this );
       
   124 
       
   125     MPX_DEBUG1("CMPXHarvesterEngine::ConstructL --->");
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // Two-Phased Constructor
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 CMPXHarvesterEngine* CMPXHarvesterEngine::NewL()
       
   133     {
       
   134     CMPXHarvesterEngine* self = new(ELeave) CMPXHarvesterEngine();
       
   135     CleanupStack::PushL( self );
       
   136     self->ConstructL();
       
   137     CleanupStack::Pop( self );
       
   138     return self;
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // Start scanning all drives
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 void CMPXHarvesterEngine::ScanL()
       
   146     {
       
   147     iFileHandler->ScanL();
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // Cancel scanning
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 void CMPXHarvesterEngine::CancelScan()
       
   155     {
       
   156     iFileHandler->CancelScan();
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // Add a file to the db by file name
       
   161 // ---------------------------------------------------------------------------
       
   162 //
       
   163 void CMPXHarvesterEngine::AddFileL( RHvsMsg* aMsg, const TDesC& aFile,
       
   164                                     MMPXHarvesterEngineObserver* aObs )
       
   165     {
       
   166     // If it is a playlist, get playlist engine to extract,
       
   167     // if it is a normal media file, we get file handler to extract
       
   168     //
       
   169     if( iPlaylistEngine->IsPlaylistL( aFile ) )
       
   170         {
       
   171         iTaskQueue->AddTaskL( aMsg->Op(), aObs, this, 0, NULL, aMsg);
       
   172         }
       
   173     else
       
   174         {
       
   175         CMPXMedia* media(NULL);
       
   176         TRAPD( err,
       
   177                media = iFileHandler->AddFileL( aFile )
       
   178              );
       
   179         CleanupStack::PushL( media );
       
   180         aObs->HandleImportCompleteL( media, err, aMsg );
       
   181         CleanupStack::PopAndDestroy( media );
       
   182         }
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // Add a file to the db (with media properties)
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 TInt CMPXHarvesterEngine::AddFileL( CMPXMedia*& aMedia )
       
   190     {
       
   191     return iFileHandler->AddFileL( *aMedia );
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // Create a media object for the file
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 void CMPXHarvesterEngine::GetMediaL( RHvsMsg* aMsg, const TDesC& aFile,
       
   199                                     MMPXHarvesterEngineObserver* aObs )
       
   200     {
       
   201     // If it is a playlist, get playlist engine to extract,
       
   202     // if it is a normal media file, we get file handler to extract
       
   203     //
       
   204     if( iPlaylistEngine->IsPlaylistL( aFile ) )
       
   205         {
       
   206         iTaskQueue->AddTaskL( aMsg->Op(), aObs, this, 0, NULL, aMsg);
       
   207         }
       
   208     else
       
   209         {
       
   210         CMPXMedia* media(NULL);
       
   211         TRAPD(err, media = iFileHandler->GetMediaForFileL(aFile));
       
   212         CleanupStack::PushL( media );
       
   213         aObs->HandleImportCompleteL( media, err, aMsg );
       
   214         CleanupStack::PopAndDestroy( media );
       
   215         }
       
   216     }
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // Get collection id for the file
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 TInt CMPXHarvesterEngine::GetColUidL( const TDesC& aFile )
       
   223     {
       
   224     return iFileHandler->GetColUidForFileL(aFile);
       
   225     }
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // Remove a single file
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 TInt CMPXHarvesterEngine::RemoveFileL( const TDesC& aFile, TBool aDelete, TBool aEndTransaction )
       
   232     {
       
   233     // Delete this file? If so, delete it from the file system, if a problem
       
   234     // occurs deleting this file, the correlating entry in the harvester db
       
   235     // is not removed.
       
   236     //
       
   237     if( aDelete )
       
   238         {
       
   239 		MPX_PERF_START( MPX_PERF_FS_DELETE );
       
   240         TInt err = iFsSession.Delete( aFile );
       
   241         MPX_PERF_END( MPX_PERF_FS_DELETE );
       
   242         if (err == KErrAccessDenied)
       
   243             {
       
   244             iFsSession.SetAtt(aFile,KEntryAttNormal,KEntryAttReadOnly);
       
   245             err = iFsSession.Delete(aFile);
       
   246             }
       
   247         User::LeaveIfError( err );
       
   248         }
       
   249 
       
   250 	MPX_PERF_START( MPX_PERF_HARV_DB_DELETE );
       
   251     TInt r = iFileHandler->RemoveFileL( aFile, aEndTransaction );
       
   252     MPX_PERF_END( MPX_PERF_HARV_DB_DELETE );
       
   253     return r;
       
   254     }
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // Remove Multiple files
       
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 void CMPXHarvesterEngine::RemoveMultipleFilesL( const MDesCArray& aArray,
       
   261                                                 TBool aDelete, TBool aEndTransaction)
       
   262     {
       
   263     //
       
   264     // delete the file from the file system before updating harvester database.
       
   265     // delete one file from the file system and update that file in the harvester
       
   266     // database one by one; otherwise, file system and database will be out of
       
   267     // sync
       
   268     //
       
   269     if( aDelete )
       
   270         {
       
   271         TInt c = aArray.MdcaCount();
       
   272         for( TInt i=0; i<c; ++i )
       
   273             {
       
   274             User::LeaveIfError(RemoveFileL(aArray.MdcaPoint(i), aDelete, aEndTransaction));
       
   275             }
       
   276         }
       
   277     else
       
   278         {
       
   279         iFileHandler->RemoveFilesL( aArray );
       
   280         }
       
   281     }
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // Remove all files
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CMPXHarvesterEngine::RemoveAllFilesL()
       
   288     {
       
   289     iFileHandler->RemoveAllFilesL();
       
   290     }
       
   291 
       
   292 // ---------------------------------------------------------------------------
       
   293 // Update a particular file
       
   294 // ---------------------------------------------------------------------------
       
   295 //
       
   296 void CMPXHarvesterEngine::UpdateFileL( const TDesC& aFile, TInt aCollection )
       
   297     {
       
   298     MPX_DEBUG1("CMPXHarvesterEngine::UpdateFileL <---");
       
   299     iFileHandler->UpdateFileL( aFile, aCollection );
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------------------------
       
   303 // Update a particular file
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 void CMPXHarvesterEngine::RenameFileL( const TDesC& aOldPath,
       
   307                                        const TDesC& aNewPath,
       
   308                                        TInt aCollection )
       
   309     {
       
   310     MPX_DEBUG1("CMPXHarvesterEngine::RenameFileL <---");
       
   311     iFileHandler->RenameFileL( aOldPath, aNewPath, aCollection );
       
   312     }
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 // Export a playlist, asynchronous
       
   316 // ---------------------------------------------------------------------------
       
   317 //
       
   318 void CMPXHarvesterEngine::ExportPlaylistL(
       
   319                                             RHvsMsg* aMessage,
       
   320                                             MMPXHarvesterEngineObserver* aObs )
       
   321     {
       
   322     MPX_DEBUG1("CMPXHarvesterEngine::HandleExportPlaylistL <---");
       
   323     iTaskQueue->AddTaskL( aMessage->Op(), aObs, this, 0, NULL, aMessage);
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------------------------
       
   327 // Find the collection id
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 TInt CMPXHarvesterEngine::FindCollectionIdL( const TDesC& aFile )
       
   331     {
       
   332     MPX_DEBUG1("CMPXHarvesterEngine::FindCollectionIDL <---");
       
   333     return iFileHandler->FindCollectionIdL( aFile );
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // Import a playlist
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 void CMPXHarvesterEngine::ImportPlaylistL(
       
   341                                             RHvsMsg* aMessage,
       
   342                                             MMPXHarvesterEngineObserver* aObs )
       
   343     {
       
   344     MPX_DEBUG1("CMPXHarvesterEngine::ImportPlaylistL <---");
       
   345     iTaskQueue->AddTaskL( aMessage->Op(), aObs, this, 0, NULL, aMessage);
       
   346     }
       
   347 
       
   348 // ---------------------------------------------------------------------------
       
   349 // Import a file
       
   350 // ---------------------------------------------------------------------------
       
   351 //
       
   352 void CMPXHarvesterEngine::ImportFileL( RHvsMsg* aMessage,
       
   353                                        MMPXHarvesterEngineObserver* aObs )
       
   354     {
       
   355     MPX_DEBUG1("CMPXHarvesterEngine::ImportFileL <---");
       
   356     iTaskQueue->AddTaskL( aMessage->Op(), aObs, this, 0, NULL, aMessage);
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------------------------
       
   360 // Query the required attributes for tracks for the specified playlist type
       
   361 // ---------------------------------------------------------------------------
       
   362 const TArray<TMPXAttribute> CMPXHarvesterEngine::RequiredAttributesL(TInt aPlaylistType) const
       
   363     {
       
   364     iPlaylistEngine->PlaylistPluginHandler().SelectPlaylistPluginL(aPlaylistType);
       
   365 
       
   366     CMPXPlaylistPlugin* playlistPlugin = iPlaylistEngine->PlaylistPluginHandler().Plugin();
       
   367     if (!playlistPlugin)
       
   368         {
       
   369         User::Leave(KErrNotSupported);
       
   370         }
       
   371     return playlistPlugin->RequiredAttributes();
       
   372     }
       
   373 
       
   374 // ---------------------------------------------------------------------------
       
   375 // Query the optional attributes for tracks for the specified playlist type
       
   376 // ---------------------------------------------------------------------------
       
   377 const TArray<TMPXAttribute> CMPXHarvesterEngine::OptionalAttributesL(TInt aPlaylistType) const
       
   378     {
       
   379     iPlaylistEngine->PlaylistPluginHandler().SelectPlaylistPluginL(aPlaylistType);
       
   380 
       
   381     CMPXPlaylistPlugin* playlistPlugin = iPlaylistEngine->PlaylistPluginHandler().Plugin();
       
   382     if (!playlistPlugin)
       
   383         {
       
   384         User::Leave(KErrNotSupported);
       
   385         }
       
   386     return playlistPlugin->OptionalAttributes();
       
   387     }
       
   388 
       
   389 // ---------------------------------------------------------------------------
       
   390 // Query the optional attributes for tracks for the specified playlist type
       
   391 // ---------------------------------------------------------------------------
       
   392 const TDesC& CMPXHarvesterEngine::PlaylistFileExtensionL(TInt aPlaylistType) const
       
   393     {
       
   394     iPlaylistEngine->PlaylistPluginHandler().SelectPlaylistPluginL(aPlaylistType);
       
   395 
       
   396     CMPXPlaylistPlugin* playlistPlugin = iPlaylistEngine->PlaylistPluginHandler().Plugin();
       
   397     if (!playlistPlugin)
       
   398         {
       
   399         User::Leave(KErrNotSupported);
       
   400         }
       
   401     return playlistPlugin->FileExtension();
       
   402     }
       
   403 
       
   404 // ---------------------------------------------------------------------------
       
   405 // Determines if the given file is a playlist
       
   406 // ---------------------------------------------------------------------------
       
   407 //
       
   408 TBool CMPXHarvesterEngine::IsPlaylistL( const TDesC& aFile )
       
   409     {
       
   410     MPX_DEBUG1("CMPXHarvesterEngine::IsPlaylistL <---");
       
   411     return iPlaylistEngine->IsPlaylistL( aFile );
       
   412     }
       
   413 
       
   414 // ---------------------------------------------------------------------------
       
   415 // Determines if the given file is a playlist
       
   416 // ---------------------------------------------------------------------------
       
   417 //
       
   418 void CMPXHarvesterEngine::RecreateDatabases()
       
   419     {
       
   420     MPX_DEBUG1("CMPXHarvesterEngine::RecreateDatabasesL");
       
   421     iFileHandler->RecreateDatabases();
       
   422     }
       
   423 
       
   424 // ---------------------------------------------------------------------------
       
   425 // Checks for any system events
       
   426 // ---------------------------------------------------------------------------
       
   427 //
       
   428 void CMPXHarvesterEngine::CheckForSystemEvents()
       
   429     {
       
   430     MPX_DEBUG1("CMPXHarvesterEngine::CheckForSystemEventsL()");
       
   431 
       
   432     // Check for formatting, mtp and usb
       
   433     // No need to check for disk inserted or not because we
       
   434     // wont be able to open the db anyways.
       
   435     //
       
   436     iFormatMonitor->PollStatus();
       
   437     iUSBMonitor->PollStatus();
       
   438     }
       
   439 
       
   440 // ---------------------------------------------------------------------------
       
   441 // Handling system events
       
   442 // ---------------------------------------------------------------------------
       
   443 //
       
   444 void CMPXHarvesterEngine::HandleSystemEventL( TSystemEvent aEvent,
       
   445                                               TInt aData )
       
   446     {
       
   447     MPX_DEBUG2("CMPXHarvesterEngine::HandleSystemEventL %i <---", aEvent);
       
   448 
       
   449     // The engine is a delegator, it sends the events to
       
   450     // different classes to do the actual work
       
   451     TRAPD( openErr, iFileHandler->HandleSystemEventL( aEvent, aData ) );
       
   452 
       
   453     // Avoid Message queue already exist problem
       
   454     //
       
   455     TBool notify(ETrue);
       
   456     switch( aEvent )
       
   457         {
       
   458         case EPowerKeyEjectEvent:
       
   459             {
       
   460             notify=EFalse;
       
   461             TRAP_IGNORE( DoStopPlaybackL() );
       
   462             break;
       
   463             }
       
   464         case EFormatStartEvent:
       
   465         case EUSBMassStorageStartEvent:   // deliberate fall through
       
   466         case EUSBMTPStartEvent:           // deliberate fall through
       
   467         case EDiskInsertedEvent:          // deliberate fall through
       
   468         case EDiskRemovedEvent:           // deliberate fall through
       
   469             {
       
   470             iDiskOpActive = ETrue;
       
   471             TRAP_IGNORE( DoStopPlaybackL() );
       
   472             }
       
   473         default: //lint !e616 !e825
       
   474             if( !iTempCollectionUtil )
       
   475                 {
       
   476                 iTempCollectionUtil = MMPXCollectionUtility::NewL( NULL, KMcModeDefault );
       
   477                 }
       
   478             break;
       
   479         }
       
   480 
       
   481     // Send a message to the collection server about the event
       
   482     //
       
   483     if( notify )
       
   484         {
       
   485         TRAP_IGNORE(
       
   486                iTempCollectionUtil->Collection().NotifyL( (TMPXCollectionBroadCastMsg)aEvent,
       
   487                                                           aData )
       
   488              );
       
   489         }
       
   490 
       
   491     // Avoid Message queue already exist problem
       
   492     //
       
   493     switch( aEvent )
       
   494         {
       
   495         case EFormatEndEvent:
       
   496             {
       
   497             // On some, the formatting app remounts
       
   498             // the disk before it is writable, so
       
   499             // we have to "fake" the insert event back after the
       
   500             // format is actually done
       
   501             //
       
   502             if( openErr == KErrNone )
       
   503                 {
       
   504                 HandleSystemEventL( EDiskInsertedEvent, aData );
       
   505                 }
       
   506             }                            // deliberate fall through
       
   507         case EUSBMassStorageEndEvent:    // deliberate fall through
       
   508         case EUSBMTPEndEvent:            // deliberate fall through
       
   509             iDiskOpActive = EFalse;
       
   510         default: //lint !e616 !e825
       
   511             if( iTempCollectionUtil && !iDiskOpActive )
       
   512                 {
       
   513                 iTempCollectionUtil->Close();
       
   514                 iTempCollectionUtil = NULL;
       
   515                 }
       
   516         }
       
   517     }
       
   518 
       
   519 // ---------------------------------------------------------------------------
       
   520 // Execute task queue events
       
   521 // ---------------------------------------------------------------------------
       
   522 //
       
   523 void CMPXHarvesterEngine::ExecuteTask(TInt aTask,
       
   524                                       TInt /*aParamData*/,
       
   525                                       TAny* aPtrData,
       
   526                                       const CBufBase& /*aBuf*/,
       
   527                                       TAny* aCallback,
       
   528                                       CBase* /*aCObject1*/,
       
   529                                       CBase* /*aCObject2*/)
       
   530     {
       
   531     MPX_DEBUG1("CMPXHarvesterEngine::ExecuteTask <---");
       
   532 
       
   533     // Execute the next task in the queue
       
   534     //
       
   535     //
       
   536     TInt err( KErrNone );
       
   537     switch( aTask )
       
   538         {
       
   539         case EHvsServerGetMedia:
       
   540         case EHvsServerAddFile:
       
   541         case EHvsServerImportPlaylist:  // deliberate fall through
       
   542             TRAP( err, DoImportPlaylistL( (RHvsMsg*) aPtrData ) );
       
   543             break;
       
   544 
       
   545         case EHvsServerExportPlaylist:
       
   546             TRAP( err, DoExportPlaylistL( (RHvsMsg*) aPtrData ) );
       
   547             break;
       
   548         case EHvsServerImportFile:
       
   549             TRAP( err, DoImportFileL( (RHvsMsg*) aPtrData ) );
       
   550             break;
       
   551         default:
       
   552         break;
       
   553         }
       
   554 
       
   555     // If we failed, we complete with error.
       
   556     //
       
   557     if( err != KErrNone )
       
   558         {
       
   559         MPX_DEBUG2("CMPXHarvesterEngine::ExecuteTask err %i", err );
       
   560         MMPXHarvesterEngineObserver* obs =
       
   561                                      (MMPXHarvesterEngineObserver*) aCallback;
       
   562         obs->CompleteTask( (RHvsMsg*) aPtrData, err );
       
   563         iTaskQueue->CompleteTask();
       
   564         }
       
   565     }
       
   566 
       
   567 // ----------------------------------------------------------------------------
       
   568 // Indicates that a task was terminated with an error
       
   569 // ----------------------------------------------------------------------------
       
   570 //
       
   571 void CMPXHarvesterEngine::HandleTaskError(
       
   572     TInt /* aTask */,
       
   573     TAny* /*aPtrData*/,
       
   574     TAny* /*aCallback*/,
       
   575     TInt /* aError */)
       
   576     {
       
   577     // Do Nothing
       
   578     }
       
   579 
       
   580 // ---------------------------------------------------------------------------
       
   581 // Handle the completion of importing a playlist
       
   582 // ---------------------------------------------------------------------------
       
   583 //
       
   584 void CMPXHarvesterEngine::HandlePlaylistL( CMPXMedia* aPlaylist,
       
   585                                            const TInt aError,
       
   586                                            const TBool /*aCompleted*/ )
       
   587     {
       
   588     MPX_DEBUG2("CMPXHarvesterEngine::HandlePlaylistL err %i", aError );
       
   589     TInt err( aError );
       
   590     if( iCurAsyncOp != EHvsServerImportPlaylist && iCurAsyncOp != EHvsServerAddFile &&
       
   591         iCurAsyncOp != EHvsServerImportFile  && iCurAsyncOp != EHvsServerGetMedia)
       
   592         {
       
   593         err = KErrArgument;
       
   594         }
       
   595 
       
   596     if( aPlaylist )
       
   597         {
       
   598         CleanupStack::PushL( aPlaylist );
       
   599         }
       
   600 
       
   601     // If we were adding a file, we need to add it to file handler as well
       
   602     if( iCurAsyncOp == EHvsServerAddFile && aPlaylist )
       
   603         {
       
   604         iFileHandler->AddFileL( *aPlaylist );
       
   605         }
       
   606 
       
   607     // Complete the message and tell the client
       
   608     //
       
   609     MMPXHarvesterEngineObserver* obs = (MMPXHarvesterEngineObserver*)
       
   610                                                        iTaskQueue->Callback();
       
   611     RHvsMsg* msg = (RHvsMsg*) iTaskQueue->PtrData();
       
   612     if( err == KErrNone )
       
   613         {
       
   614         obs->HandleImportCompleteL( aPlaylist, err, msg );
       
   615         }
       
   616     else
       
   617         {
       
   618         obs->HandleImportCompleteL( NULL, err, msg );
       
   619         }
       
   620     iTaskQueue->CompleteTask();
       
   621 
       
   622     if( aPlaylist )
       
   623         {
       
   624         CleanupStack::PopAndDestroy( aPlaylist );
       
   625         }
       
   626     }
       
   627 
       
   628 // ---------------------------------------------------------------------------
       
   629 // Handle the completion of exporting a playlist
       
   630 // ---------------------------------------------------------------------------
       
   631 //
       
   632 void CMPXHarvesterEngine::HandlePlaylistL(const TDesC& aPlaylistUri,
       
   633                                           const TInt aError)
       
   634     {
       
   635     MPX_DEBUG2("CMPXHarvesterEngine::HandlePlaylistL err %i", aError );
       
   636 
       
   637     TInt err( aError );
       
   638     if( iCurAsyncOp != EHvsServerExportPlaylist )
       
   639         {
       
   640         err = KErrArgument;
       
   641         }
       
   642 
       
   643     // Complete the message and tell the client
       
   644     //
       
   645     MMPXHarvesterEngineObserver* obs = (MMPXHarvesterEngineObserver*)
       
   646                                                        iTaskQueue->Callback();
       
   647     RHvsMsg* msg = (RHvsMsg*) iTaskQueue->PtrData();
       
   648     if ( err == KErrNone )
       
   649         {
       
   650         CMPXMedia* media(NULL);
       
   651         ::NewFromMessageL<CMPXMedia>( msg->Message(), 0, media );
       
   652         CleanupStack::PushL( media );
       
   653 
       
   654         // set playlist uri
       
   655         media->SetTextValueL(
       
   656             TMPXAttribute(KMPXMediaIdGeneral, EMPXMediaGeneralUri),
       
   657             aPlaylistUri);
       
   658 
       
   659         obs->HandleExportCompleteL( media, err, msg );
       
   660         CleanupStack::PopAndDestroy( media );
       
   661         }
       
   662     else
       
   663         {
       
   664         obs->HandleExportCompleteL( NULL, err, msg );
       
   665         }
       
   666     iTaskQueue->CompleteTask();
       
   667     }
       
   668 
       
   669 // ---------------------------------------------------------------------------
       
   670 // Does the actual work for exporting a playlist
       
   671 // ---------------------------------------------------------------------------
       
   672 //
       
   673 void CMPXHarvesterEngine::DoExportPlaylistL( RHvsMsg* aMsg )
       
   674     {
       
   675     MPX_DEBUG1("CMPXHarvesterEngine::DoExportPlaylistL  <---");
       
   676     iCurAsyncOp = EHvsServerExportPlaylist;
       
   677 
       
   678     // Unpack the message
       
   679     //
       
   680     CMPXMedia* media(NULL);
       
   681     ::NewFromMessageL<CMPXMedia>( aMsg->Message(), 0, media );
       
   682     CleanupStack::PushL( media );
       
   683 
       
   684     HBufC* path = HBufC::NewLC( aMsg->Message().GetDesLength(1) );
       
   685     TPtr ptr = path->Des();
       
   686     aMsg->Message().Read( 1, ptr );
       
   687 
       
   688     TPckgBuf<TInt> t;
       
   689     aMsg->Message().Read(2, t);
       
   690     TInt plType = t();
       
   691 
       
   692     // Find the playlist plugin
       
   693     CMPXPlaylistPluginHandler& plHandler =
       
   694                                       iPlaylistEngine->PlaylistPluginHandler();
       
   695     plHandler.SelectPlaylistPluginL( plType );
       
   696 
       
   697     // Call playlist engine
       
   698     //
       
   699     iPlaylistEngine->ExternalizePlaylistL( *media, *path );
       
   700 
       
   701     // Cleanup
       
   702     CleanupStack::PopAndDestroy( 2, media );
       
   703     MPX_DEBUG1("CMPXHarvesterEngine::DoExportPlaylistL  --->");
       
   704     }
       
   705 
       
   706 // ---------------------------------------------------------------------------
       
   707 // Does the actual work for importing a playlist
       
   708 // ---------------------------------------------------------------------------
       
   709 //
       
   710 void CMPXHarvesterEngine::DoImportPlaylistL( RHvsMsg* aMsg )
       
   711     {
       
   712     MPX_DEBUG1("CMPXHarvesterEngine::DoImportPlaylistL  <---");
       
   713     iCurAsyncOp = aMsg->Op();
       
   714 
       
   715     // Unpack the message
       
   716     //
       
   717     HBufC* filename = HBufC::NewLC( aMsg->Message().GetDesLength(0) );
       
   718     TPtr ptr = filename->Des();
       
   719     aMsg->Message().Read(0, ptr );
       
   720 
       
   721     // Call playlist engine
       
   722     //
       
   723     iPlaylistEngine->InternalizePlaylistL( *filename );
       
   724 
       
   725     // Cleanup
       
   726     CleanupStack::PopAndDestroy( filename );
       
   727     MPX_DEBUG1("CMPXHarvesterEngine::DoImportPlaylistL  --->");
       
   728     }
       
   729 
       
   730 // ---------------------------------------------------------------------------
       
   731 // Does the actual work for importing a file
       
   732 // ---------------------------------------------------------------------------
       
   733 //
       
   734 void CMPXHarvesterEngine::DoImportFileL( RHvsMsg* aMsg )
       
   735     {
       
   736     MPX_DEBUG1("CMPXHarvesterEngine::DoImportFileL  <---");
       
   737     iCurAsyncOp = aMsg->Op();
       
   738 
       
   739     // Unpack the message
       
   740     //
       
   741     HBufC* filename = HBufC::NewLC( aMsg->Message().GetDesLength(0) );
       
   742     TPtr ptr = filename->Des();
       
   743     aMsg->Message().Read(0, ptr );
       
   744 
       
   745     if( iPlaylistEngine->IsPlaylistL( *filename ) )
       
   746         {
       
   747         // Call playlist engine
       
   748         iPlaylistEngine->InternalizePlaylistL( *filename );
       
   749         }
       
   750     else
       
   751         {
       
   752         // Import file details
       
   753         CMPXMedia* media = CMPXMedia::NewL();
       
   754         CleanupStack::PushL( media );
       
   755         media->SetTObjectValueL( TMPXAttribute(KMPXMediaIdGeneral,
       
   756                                                EMPXMediaGeneralType),
       
   757                                  EMPXItem );
       
   758         media->SetTObjectValueL( TMPXAttribute(KMPXMediaIdGeneral,
       
   759                                                EMPXMediaGeneralCategory),
       
   760                                  EMPXSong );
       
   761 
       
   762         media->SetTextValueL( TMPXAttribute(KMPXMediaIdGeneral,
       
   763                                                EMPXMediaGeneralUri),
       
   764                               *filename );
       
   765 
       
   766         // Callback to observer
       
   767         MMPXHarvesterEngineObserver* obs = (MMPXHarvesterEngineObserver*)
       
   768                                             iTaskQueue->Callback();
       
   769         obs->HandleImportCompleteL( media, KErrNone, aMsg );
       
   770         CleanupStack::PopAndDestroy( media );
       
   771 
       
   772         // Task is completed for file case
       
   773         iTaskQueue->CompleteTask();
       
   774         }
       
   775 
       
   776     // Cleanup
       
   777     CleanupStack::PopAndDestroy( filename );
       
   778     MPX_DEBUG1("CMPXHarvesterEngine::DoImportFileL  --->");
       
   779     }
       
   780 
       
   781 // ---------------------------------------------------------------------------
       
   782 // Stop playback
       
   783 // ---------------------------------------------------------------------------
       
   784 //
       
   785 void CMPXHarvesterEngine::DoStopPlaybackL()
       
   786     {
       
   787     MMPXPlaybackUtility* pbUtil = MMPXPlaybackUtility::UtilityL( KPbModeActivePlayer );
       
   788     CleanupClosePushL( *pbUtil );
       
   789 
       
   790     CMPXCommand* closeCmd = CMPXCommand::NewL();
       
   791     CleanupStack::PushL( closeCmd );
       
   792 
       
   793     // Send a stop command
       
   794     closeCmd->SetTObjectValueL(KMPXCommandGeneralId, KMPXCommandIdPlaybackGeneral);
       
   795     closeCmd->SetTObjectValueL(KMPXCommandGeneralDoSync, ETrue);
       
   796     closeCmd->SetTObjectValueL<TInt>(KMPXCommandPlaybackGeneralType,EPbCmdStop);
       
   797     closeCmd->SetTObjectValueL<TInt>(KMPXCommandPlaybackGeneralNoBuffer, ETrue);
       
   798     pbUtil->CommandL( *closeCmd );
       
   799 
       
   800     // Send a clear buffer command
       
   801     closeCmd->SetTObjectValueL<TInt>(KMPXCommandPlaybackGeneralType,EPbCmdClearKeyBuffer);
       
   802     pbUtil->CommandL( *closeCmd );
       
   803     CleanupStack::PopAndDestroy( closeCmd );
       
   804 
       
   805     CleanupStack::Pop( pbUtil );
       
   806     pbUtil->Close();
       
   807     }
       
   808 
       
   809 // ---------------------------------------------------------------------------
       
   810 // Close the database transaction
       
   811 // ---------------------------------------------------------------------------
       
   812 //
       
   813 void CMPXHarvesterEngine::CloseTransactionL()
       
   814     {
       
   815     iFileHandler->CloseTransactionL();
       
   816     }
       
   817 
       
   818 // END OF FILE
       
   819