mmappcomponents/harvester/collectionmediator/src/mpxmoveitemhelper.cpp
changeset 0 a2952bb97e68
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:  Helper class to move items to different collections
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <mpxmedia.h>
       
    21 #include <mpxmediaarray.h>
       
    22 #include <mpxcollectionutility.h>
       
    23 #include <mpxmediageneraldefs.h>
       
    24 #include <mpxmediaaudiodefs.h>
       
    25 #include <mpxmediacontainerdefs.h>
       
    26 #include <mpxmediamusicdefs.h>
       
    27 #include <mpxcollectionplugin.hrh>
       
    28 #include <mpxplaybackutility.h>
       
    29 #include <mpxcommandgeneraldefs.h>
       
    30 #include <mpxcollectioncommanddefs.h>
       
    31 
       
    32 #include "mpxmediatorobserver.h"
       
    33 #include "mpxmoveobserver.h"
       
    34 #include "mpxmoveitemhelper.h"
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Constructor
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CMPXMoveItemHelper::CMPXMoveItemHelper( MMPXMoveItemObserver* aObs )
       
    43                                                           : iObs(aObs),
       
    44                                                             iMoveState(EIdle )
       
    45     {
       
    46     }
       
    47 
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // 2nd phased constructor
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 void CMPXMoveItemHelper::ConstructL()
       
    54     {
       
    55     iCollection = MMPXCollectionUtility::NewL( NULL );
       
    56 
       
    57     RArray<TUid> ary;
       
    58     CleanupClosePushL( ary );
       
    59 
       
    60     ary.AppendL( TUid::Uid(EMPXCollectionPluginPodCast) );
       
    61     iPodCastCollectionID = iCollection->CollectionIDL( ary.Array() );
       
    62 
       
    63     ary.Reset();
       
    64     ary.AppendL( TUid::Uid(EMPXCollectionPluginMusic) );
       
    65     iMusicCollectionID = iCollection->CollectionIDL( ary.Array() );
       
    66 
       
    67     CleanupStack::PopAndDestroy( &ary );
       
    68     }
       
    69 
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Two phase constructor
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 EXPORT_C CMPXMoveItemHelper* CMPXMoveItemHelper::NewL( MMPXMoveItemObserver* aObs )
       
    76     {
       
    77     CMPXMoveItemHelper* self = CMPXMoveItemHelper::NewLC(aObs);
       
    78     CleanupStack::Pop( self );
       
    79     return self;
       
    80     }
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Two phased constructor
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 EXPORT_C CMPXMoveItemHelper* CMPXMoveItemHelper::NewLC( MMPXMoveItemObserver* aObs )
       
    88     {
       
    89     CMPXMoveItemHelper* self = new( ELeave ) CMPXMoveItemHelper(aObs);
       
    90     CleanupStack::PushL( self );
       
    91     self->ConstructL();
       
    92     return self;
       
    93     }
       
    94 
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // virtual destructor
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 CMPXMoveItemHelper::~CMPXMoveItemHelper()
       
   101     {
       
   102     if( iCollection )
       
   103         {
       
   104         iCollection->Close();
       
   105         }
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // Move an item synchronously
       
   110 // Three things to do to move an item
       
   111 // 1: Remove item from old db
       
   112 // 2: Udpdate media with new collection id
       
   113 // 3: Add item into new db
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 EXPORT_C void CMPXMoveItemHelper::MoveItemL( CMPXMedia& aMedia, TUid aNewCollection )
       
   117     {
       
   118     MMPXPlaybackUtility* pbUtil = MMPXPlaybackUtility::UtilityL( KPbModeActivePlayer );
       
   119     CleanupClosePushL(*pbUtil);
       
   120 
       
   121     // Media as a container or item
       
   122     //
       
   123     TMPXGeneralType type =
       
   124          aMedia.ValueTObjectL<TMPXGeneralType>( TMPXAttribute( KMPXMediaIdGeneral,
       
   125                                                                EMPXMediaGeneralType ) );
       
   126 
       
   127     if ( type == EMPXGroup )
       
   128         {
       
   129         const CMPXMediaArray* array = aMedia.Value<CMPXMediaArray>(
       
   130                                 TMPXAttribute( KMPXMediaIdContainer,
       
   131                                                EMPXMediaArrayContents ) );
       
   132         if( !array )
       
   133             {
       
   134             User::Leave( KErrNoMemory );
       
   135             }
       
   136 
       
   137         TInt count( array->Count() );
       
   138         if( count == 0 )
       
   139             {
       
   140             User::Leave( KErrNotFound );
       
   141             }
       
   142         for( TInt i=0; i<count; ++i )
       
   143             {
       
   144             CMPXMedia* entry = array->AtL(i);
       
   145 
       
   146             // Notify the playback utility before removing the media item
       
   147             if(entry->IsSupported(KMPXMediaGeneralId))
       
   148                 {
       
   149                 TMPXItemId mediaId(entry->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   150                 TRAP_IGNORE(pbUtil->CommandL( EPbCmdCloseItem, mediaId));
       
   151                 }
       
   152 
       
   153             TUid oldCollection = entry->ValueTObjectL<TUid>(TMPXAttribute(KMPXMediaIdGeneral,
       
   154                                                                           EMPXMediaGeneralCollectionId));
       
   155 
       
   156             DoRemoveL( entry, oldCollection );
       
   157 
       
   158             entry->SetTObjectValueL<TUid>(TMPXAttribute(KMPXMediaIdGeneral,
       
   159                                                        EMPXMediaGeneralCollectionId),
       
   160                                          aNewCollection);
       
   161 
       
   162             UpdateMediaForMoveL( *entry, oldCollection, aNewCollection );
       
   163             DoAddL( entry, aNewCollection );
       
   164             }
       
   165         }
       
   166     else
       
   167         {
       
   168         TUid oldCollection = aMedia.ValueTObjectL<TUid>(TMPXAttribute(KMPXMediaIdGeneral,
       
   169                                                                       EMPXMediaGeneralCollectionId));
       
   170 
       
   171         aMedia.SetTObjectValueL<TUid>(TMPXAttribute(KMPXMediaIdGeneral,
       
   172                                                     EMPXMediaGeneralCollectionId),
       
   173                                       aNewCollection);
       
   174 
       
   175         if(aMedia.IsSupported(KMPXMediaGeneralId))
       
   176             {
       
   177             TMPXItemId mediaId(aMedia.ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   178             TRAP_IGNORE(pbUtil->CommandL( EPbCmdCloseItem, mediaId));
       
   179             }
       
   180 
       
   181         DoRemoveL( &aMedia, oldCollection );
       
   182 
       
   183         UpdateMediaForMoveL( aMedia, oldCollection, aNewCollection );
       
   184         DoAddL( &aMedia, aNewCollection );
       
   185         }
       
   186 
       
   187     CleanupStack::PopAndDestroy(pbUtil);
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // Fetch an item then move it async
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 EXPORT_C void CMPXMoveItemHelper::FetchAndMoveItemL( const CMPXMedia& aMedia,
       
   195                                                      TUid aNewCollection )
       
   196     {
       
   197     ASSERT( iMoveState == EIdle );
       
   198     ASSERT( iObs );
       
   199     // Fetch item from collection asynchronously
       
   200     //
       
   201     // Look for the item, and fetch all relavant info
       
   202     //
       
   203     RArray<TMPXAttribute> atts;
       
   204     CleanupClosePushL( atts );
       
   205     atts.Append(KMPXMediaGeneralAll);
       
   206     atts.Append(KMPXMediaAudioAudioAll);
       
   207     atts.Append(KMPXMediaMusicAll);
       
   208 
       
   209     iCollection->Collection().FindAllL( aMedia, atts.Array(), *this );
       
   210     CleanupStack::PopAndDestroy( &atts );
       
   211     iMoveState = EFind;
       
   212     iMoveTarget = aNewCollection;
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // Fetch an item then move it sync
       
   217 // ---------------------------------------------------------------------------
       
   218 //
       
   219 EXPORT_C void CMPXMoveItemHelper::FetchAndMoveItemSyncL( const CMPXMedia& aMedia,
       
   220                                                 TUid aNewCollection )
       
   221     {
       
   222     // Look for the item, and fetch all relavant info
       
   223     //
       
   224     RArray<TMPXAttribute> atts;
       
   225     CleanupClosePushL( atts );
       
   226     atts.Append(KMPXMediaGeneralAll);
       
   227     atts.Append(KMPXMediaAudioAudioAll);
       
   228     atts.Append(KMPXMediaMusicAll);
       
   229 
       
   230     CMPXMedia* result = iCollection->Collection().FindAllL( aMedia,
       
   231                                                             atts.Array() );
       
   232     CleanupStack::PopAndDestroy( &atts );
       
   233     CleanupStack::PushL( result );
       
   234 
       
   235     // Move the item as normal
       
   236     MoveItemL( *result, aNewCollection );
       
   237     CleanupStack::PopAndDestroy( result );
       
   238     }
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // Updates a media object for collection specific detail
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 void CMPXMoveItemHelper::UpdateMediaForMoveL( CMPXMedia& aMedia,
       
   245                                               TUid& aOldCollection,
       
   246                                               TUid& aNewCollection )
       
   247     {
       
   248     // If we are moving from podcast collection -> local audio
       
   249     //
       
   250     if( aNewCollection == iMusicCollectionID &&
       
   251         aOldCollection == iPodCastCollectionID )
       
   252         {
       
   253         aMedia.SetTObjectValueL(TMPXAttribute(KMPXMediaIdGeneral,
       
   254                                                EMPXMediaGeneralCategory),
       
   255                                  EMPXSong);
       
   256         }
       
   257     // Move from local collection -> podcast
       
   258     //
       
   259     else if( aOldCollection == iMusicCollectionID &&
       
   260              aNewCollection == iPodCastCollectionID )
       
   261         {
       
   262         aMedia.SetTObjectValueL(TMPXAttribute(KMPXMediaIdGeneral,
       
   263                                          EMPXMediaGeneralCategory),
       
   264                                  EMPXPodcast);
       
   265         }
       
   266     } //lint !e961
       
   267 
       
   268 // ---------------------------------------------------------------------------
       
   269 // Add an item to the collection
       
   270 // ---------------------------------------------------------------------------
       
   271 //
       
   272 void CMPXMoveItemHelper::DoAddL( CMPXMedia* aMedia, TUid aCollectionId )
       
   273     {
       
   274     CMPXCommand* cmd = CMPXCommand::NewL();
       
   275     CleanupStack::PushL( cmd );
       
   276 
       
   277     cmd->SetTObjectValueL( KMPXCommandGeneralId, KMPXCommandIdCollectionAdd );
       
   278     cmd->SetTObjectValueL( KMPXCommandGeneralDoSync, ETrue );
       
   279     cmd->SetTObjectValueL( KMPXCommandGeneralCollectionId, aCollectionId.iUid );
       
   280     cmd->SetCObjectValueL<CMPXMedia>( KMPXCommandColAddMedia, aMedia );
       
   281 
       
   282     iCollection->Collection().CommandL( *cmd );
       
   283     CleanupStack::PopAndDestroy( cmd );
       
   284     }
       
   285 
       
   286 // ---------------------------------------------------------------------------
       
   287 // Set an item to the collection
       
   288 // ---------------------------------------------------------------------------
       
   289 //
       
   290 void CMPXMoveItemHelper::DoRemoveL( CMPXMedia* aMedia, TUid aCollectionId )
       
   291     {
       
   292     CMPXCommand* cmd = CMPXCommand::NewL();
       
   293     CleanupStack::PushL( cmd );
       
   294 
       
   295     cmd->SetTObjectValueL( KMPXCommandGeneralId, KMPXCommandIdCollectionRemoveMedia );
       
   296     cmd->SetTObjectValueL( KMPXCommandGeneralDoSync, ETrue );
       
   297     cmd->SetTObjectValueL( KMPXCommandCollectionRemoveMediaDeleteRecord, ETrue );
       
   298     cmd->SetTObjectValueL( KMPXCommandGeneralCollectionId, aCollectionId.iUid );
       
   299     cmd->SetCObjectValueL<CMPXMedia>( KMPXCommandCollectionRemoveMedia, aMedia );
       
   300 
       
   301     iCollection->Collection().CommandL( *cmd );
       
   302     CleanupStack::PopAndDestroy( cmd );
       
   303     }
       
   304 
       
   305 // ---------------------------------------------------------------------------
       
   306 // Handle find all callback
       
   307 // ---------------------------------------------------------------------------
       
   308 //
       
   309 void CMPXMoveItemHelper::HandleFindAllL( const CMPXMedia& aResults,
       
   310                                          TBool /*aComplete*/,TInt aError)
       
   311     {
       
   312     // Make sure the mode is correct
       
   313     ASSERT( iMoveState == EFind );
       
   314 
       
   315     TInt err ( aError );
       
   316     if( err == KErrNone )
       
   317         {
       
   318         TRAP( err, DoHandleFindAllL( aResults ) );
       
   319         }
       
   320 
       
   321     // Find all is the most time consuming part.
       
   322     //
       
   323     iObs->HandleMoveCompleteL( err );
       
   324     iMoveState = EIdle;
       
   325     }
       
   326 
       
   327 // ---------------------------------------------------------------------------
       
   328 // Handle find all callback
       
   329 // ---------------------------------------------------------------------------
       
   330 //
       
   331 void CMPXMoveItemHelper::DoHandleFindAllL( const CMPXMedia& aResult )
       
   332     {
       
   333     // Need to make a tmp copy because we have to modify collection id
       
   334     //
       
   335     CMPXMedia* tmp = CMPXMedia::NewL();
       
   336     CleanupStack::PushL( tmp );
       
   337     *tmp = aResult;
       
   338     MoveItemL( *tmp, iMoveTarget );
       
   339     CleanupStack::PopAndDestroy( tmp );
       
   340     }
       
   341 
       
   342 // End of file