photosgallery/viewframework/commandhandlers/commoncommandhandlers/src/glxcommandhandleraddtocontainer.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Tue, 26 Jan 2010 11:58:28 +0200
changeset 3 9a9c174934f5
parent 2 7d9067c6fcb1
child 9 6b87b143d312
permissions -rw-r--r--
Revision: 201001 Kit: 201004

/*
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:    Add to container command handler
*
*/




/**
 * @internal reviewed 05/06/2007 by Dave Schofield
 */

#include "glxcommandhandleraddtocontainer.h"

#include <AknUtils.h>
#include <bautils.h>
#include <data_caging_path_literals.hrh>
#include <eikmenup.h> 
#include <glxcollectionpluginalbums.hrh>
#include <glxcollectionplugintags.hrh>
#include <glxcommandfactory.h>
#include <glxcommandhandlers.hrh>
#include <glxfilterfactory.h>
#include <glxuiutility.h>
#include <glxuistd.h>
#include <glxresourceutilities.h>                // for CGlxResourceUtilities
#include <glxuiutilities.rsg>
#include <mglxmedialist.h>

#include <mpxcollectioncommanddefs.h>
#include <mpxcollectionpath.h>
#include <mpxcommandgeneraldefs.h>
#include <mpxmediaarray.h>
#include <mpxmediacontainerdefs.h>
#include <StringLoader.h>

#include "glxmediaselectionpopup.h"
#include <glxicons.mbg>
#include <glxtracer.h>

TInt CGlxCommandHandlerAddToContainer::iSelectionCount = 0;

// ---------------------------------------------------------------------------
// Return add to album command handler
// ---------------------------------------------------------------------------
//
EXPORT_C CGlxCommandHandlerAddToContainer* 
    CGlxCommandHandlerAddToContainer::NewAddToAlbumCommandHandlerL(
        MGlxMediaListProvider* aMediaListProvider, TBool aHasToolbarItem) 
    {
    TRACER("CGlxCommandHandlerAddToContainer::NewAddToAlbumCommandHandlerL");
    return CGlxCommandHandlerAddToContainer::NewL (aMediaListProvider, 
                                                EGlxCmdAddToAlbum, aHasToolbarItem);
    }

// ---------------------------------------------------------------------------
// Return add to album command handler for singleclick options menu
// ---------------------------------------------------------------------------
//
EXPORT_C CGlxCommandHandlerAddToContainer* 
    CGlxCommandHandlerAddToContainer::NewAddToAlbumSingleClickCommandHandlerL(
        MGlxMediaListProvider* aMediaListProvider, TBool aHasToolbarItem) 
    {
    return CGlxCommandHandlerAddToContainer::NewL (aMediaListProvider, 
                              EGlxCmdSingleClickAddToAlbum, aHasToolbarItem);
    }


// ---------------------------------------------------------------------------
// Return add (to) tags command handler
// ---------------------------------------------------------------------------
//
EXPORT_C CGlxCommandHandlerAddToContainer* 
    CGlxCommandHandlerAddToContainer::NewAddToTagCommandHandlerL(
        MGlxMediaListProvider* aMediaListProvider, TBool aHasToolbarItem) 
    {
    TRACER("CGlxCommandHandlerAddToContainer::NewAddToTagCommandHandlerL");
    return CGlxCommandHandlerAddToContainer::NewL(aMediaListProvider, 
                                                EGlxCmdAddTag, aHasToolbarItem);
    }

// ---------------------------------------------------------------------------
// Return add (to) tags command handler for singleclick option menu
// ---------------------------------------------------------------------------
//
EXPORT_C CGlxCommandHandlerAddToContainer* 
    CGlxCommandHandlerAddToContainer::NewAddToTagSingleClickCommandHandlerL(
        MGlxMediaListProvider* aMediaListProvider, TBool aHasToolbarItem) 
    {
    return CGlxCommandHandlerAddToContainer::NewL(aMediaListProvider, 
                                EGlxCmdSingleClickAddTag, aHasToolbarItem);
    }


// ---------------------------------------------------------------------------
// Return add (to) Favourites command handler
// ---------------------------------------------------------------------------
//
EXPORT_C CGlxCommandHandlerAddToContainer* 
    CGlxCommandHandlerAddToContainer::NewAddToFavCommandHandlerL(
        MGlxMediaListProvider* aMediaListProvider, TBool aHasToolbarItem) 
    {
    TRACER("CGlxCommandHandlerAddToContainer::NewAddToFavCommandHandlerL");
    return CGlxCommandHandlerAddToContainer::NewL(aMediaListProvider, 
                                    EGlxCmdAddToFavourites, aHasToolbarItem);
    }

// ---------------------------------------------------------------------------
// Two-phased constructor.
// ---------------------------------------------------------------------------
//
EXPORT_C CGlxCommandHandlerAddToContainer* CGlxCommandHandlerAddToContainer::NewL(
        MGlxMediaListProvider* aMediaListProvider, TInt aCommandId, TBool aHasToolbarItem)
    {
    TRACER("CGlxCommandHandlerAddToContainer::NewL");
    CGlxCommandHandlerAddToContainer* self = 
        new ( ELeave ) CGlxCommandHandlerAddToContainer( aMediaListProvider, aHasToolbarItem );
    CleanupStack::PushL( self );
    self->ConstructL( aCommandId );
    CleanupStack::Pop( self );
    return self;
    }
    
// ---------------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------------
//
CGlxCommandHandlerAddToContainer::CGlxCommandHandlerAddToContainer(
    MGlxMediaListProvider* aMediaListProvider, TBool aHasToolbarItem)
        : CGlxMpxCommandCommandHandler( aMediaListProvider, aHasToolbarItem )
    {
    // Do nothing
    }

// ---------------------------------------------------------------------------
// Symbian 2nd phase constructor can leave.
// ---------------------------------------------------------------------------
//
void CGlxCommandHandlerAddToContainer::ConstructL(TInt aCommandId)
    {
    TRACER("CGlxCommandHandlerAddToContainer::ConstructL");
    // Load resource
	TParse parse;
    parse.Set(KGlxUiUtilitiesResource, &KDC_APP_RESOURCE_DIR, NULL);
    TFileName resourceFile;
    resourceFile.Append(parse.FullName());
    CGlxResourceUtilities::GetResourceFilenameL(resourceFile);  
   	iResourceOffset = CCoeEnv::Static()->AddResourceFileL(resourceFile);
	// Reset the Flag inorder to allow for a new selection pop-up to be created.
   	iIsProcessOngoing = EFalse;
   	// Add supported command
   	TCommandInfo info(aCommandId);
   	
    // Filter out static items - user must have selected at least one item to
    // enable the command handler    
    const TInt KGlxCommandHandlerMinSelectionAllowSingle = 1;
    info.iMinSelectionLength = KGlxCommandHandlerMinSelectionAllowSingle;
    
   	AddCommandL(info);
   	iCommandId = aCommandId;
	}

// ---------------------------------------------------------------------------
// Destructor
// ---------------------------------------------------------------------------
//
EXPORT_C CGlxCommandHandlerAddToContainer::~CGlxCommandHandlerAddToContainer()
    {
    TRACER("CGlxCommandHandlerAddToContainer::~CGlxCommandHandlerAddToContainer");
    if ( iResourceOffset )
        {
        CCoeEnv::Static()->DeleteResourceFile(iResourceOffset);
        }
	// Reset the Flag inorder to allow for a new selection pop-up to be created.
    iIsProcessOngoing = EFalse;
    }

// ---------------------------------------------------------------------------
// Create an add to container command
// ---------------------------------------------------------------------------
//
CMPXCommand* CGlxCommandHandlerAddToContainer::CreateCommandL(TInt aCommandId, 
        MGlxMediaList& aMediaList, TBool& /*aConsume*/) const
    {
    TRACER("CGlxCommandHandlerAddToContainer::CreateCommandL");
	// Ignore the current command in case the previous command has still not been completed
	if(iIsProcessOngoing)
	    {
	    return NULL;
	    }
    iSelectionCount = 0;
	// Set the Flag inorder to block any new subsequent selection pop-ups from getting created.
	iIsProcessOngoing = ETrue;
    CMPXCollectionPath* targetCollection = CMPXCollectionPath::NewL();
    CleanupStack::PushL(targetCollection);
    TBool enableMultipleSelection = EFalse;
    TBool enablePopup = EFalse;
    CMPXFilter* filter = NULL;
    switch(aCommandId)
        {
        case EGlxCmdAddToFavourites:
            {
            targetCollection->AppendL(KGlxCollectionPluginAlbumsImplementationUid);
            // The target collection has also to be appeneded with the the relation id.
            // appending another level into the albums to get favourites and 1 is the relation id of albums
            targetCollection->AppendL( TMPXItemId(1) );
            targetCollection->Set( 0 );
            break;
            }
        case EGlxCmdAddToAlbum:
        case EGlxCmdSingleClickAddToAlbum:
            {
            enablePopup = ETrue;
            targetCollection->AppendL(KGlxCollectionPluginAlbumsImplementationUid);
	        filter = TGlxFilterFactory::CreateCameraAlbumExclusionFilterL();
	        CleanupStack::PushL(filter);
            break;
            }
        case EGlxCmdAddTag:
        case EGlxCmdSingleClickAddTag:
            {
            enableMultipleSelection = ETrue;
            enablePopup = ETrue;
            targetCollection->AppendL(KGlxTagCollectionPluginImplementationUid);
            filter = TGlxFilterFactory::CreateIncludeEmptyContainersFilterL();
            CleanupStack::PushL(filter);
            break;
            }
        }
               
    CMPXCollectionPath* sourceItems = aMediaList.PathLC( NGlxListDefs::EPathFocusOrSelection );
    CMPXCommand* command = NULL;
    if (enablePopup )
        {
        CGlxMediaSelectionPopup* popup = new (ELeave) CGlxMediaSelectionPopup;
        
        TBool accepted = EFalse;
        CMPXCollectionPath* targetContainers = 
            popup->ExecuteLD(*targetCollection, accepted, enableMultipleSelection, ETrue, filter);

        if (accepted)
            {        
            // Set the container selection count to give correct completion text
            TArray<TInt> selection = targetContainers->Selection();
            iSelectionCount = selection.Count();
            
            CleanupStack::PushL(targetContainers);
            command = TGlxCommandFactory::AddToContainerCommandLC(
                *sourceItems, *targetContainers);
            CleanupStack::Pop(command); 
            CleanupStack::PopAndDestroy(targetContainers);
            }
        else
	    	{
    		// Reset the Flag inorder to allow any new selection pop-up to be created.
	    	iIsProcessOngoing = EFalse;
	    	}
        }
    else
        {
        command = TGlxCommandFactory::AddToContainerCommandLC(
            *sourceItems, *targetCollection);
        CleanupStack::Pop(command); 
        }
    CleanupStack::PopAndDestroy(sourceItems);
    if (filter)
        {
        CleanupStack::PopAndDestroy(filter);
        }
    CleanupStack::PopAndDestroy(targetCollection);
	return command;
    } 

// -----------------------------------------------------------------------------
// CGlxCommandHandlerAddToContainer::CompletionTextL
// -----------------------------------------------------------------------------
//	
HBufC* CGlxCommandHandlerAddToContainer::CompletionTextL() const
    {
    TRACER("CGlxCommandHandlerAddToContainer::CompletionTextL()");
    if (iCommandId == EGlxCmdAddToAlbum || 
        iCommandId == EGlxCmdSingleClickAddToAlbum )
    	{
   		if (iSelectionCount > 1)
   			{
	   		return StringLoader::LoadL(R_GLX_COMPLETION_ADD_TO_CONTAINER_MULTI_ITEM_ALBUM);
   			}
   		return StringLoader::LoadL(R_GLX_COMPLETION_ADD_TO_CONTAINER_ONE_ITEM_ALBUM);
    	}
   	else if (iCommandId == EGlxCmdAddTag || 
             iCommandId == EGlxCmdSingleClickAddTag)
   		{
   		if (iSelectionCount > 1)
   			{
	   		return StringLoader::LoadL(R_GLX_COMPLETION_ADD_TO_CONTAINER_MULTI_ITEM_TAG);
   			}
		return StringLoader::LoadL(R_GLX_COMPLETION_ADD_TO_CONTAINER_ONE_ITEM_TAG);	
   		}
    else // considered as favourites 
        {
        return StringLoader::LoadL(R_GLX_COMPLETION_ADD_TO_FAVOURITES);
        }
    }

// -----------------------------------------------------------------------------
// CGlxCommandHandlerAddToContainer::DoHandleCommandCompleteL
// -----------------------------------------------------------------------------
//
void CGlxCommandHandlerAddToContainer::DoHandleCommandCompleteL(TAny* /*aSessionId*/,
        CMPXCommand* /*aCommandResult*/, TInt /*aError*/, MGlxMediaList* /*aList*/)
      {
      TRACER("CGlxCommandHandlerAddToContainer::DoHandleCommandCompleteL()");
	  // Reset the Flag inorder to allow any new selection pop-up to be created.
      iIsProcessOngoing = EFalse;
      }



// -----------------------------------------------------------------------------
// DialogDismissedL
// -----------------------------------------------------------------------------
//  
void CGlxCommandHandlerAddToContainer::DialogDismissedL(TInt aButtonId)
    {
    TRACER("CGlxCommandHandlerAddToContainer::DialogDismissedL");
    iIsProcessOngoing = EFalse;
    CGlxMpxCommandCommandHandler::DialogDismissedL(aButtonId);
    }



// ---------------------------------------------------------------------------
// CGlxCommandHandlerAddToContainer::DoActivateL
// ---------------------------------------------------------------------------
//
void CGlxCommandHandlerAddToContainer::DoActivateL(TInt /*aViewId*/)
	{

 	}

void CGlxCommandHandlerAddToContainer::PopulateToolbarL()
	{

	}

// ---------------------------------------------------------------------------
// CGlxCommandHandlerAddToContainer::DoIsDisabled
// ---------------------------------------------------------------------------
//
TBool CGlxCommandHandlerAddToContainer::DoIsDisabled(TInt aCommandId, 
                                                 MGlxMediaList& aList) const
    {
	TRACER("CGlxCommandHandlerAddToContainer::DoIsDisabled");
    if ( (EGlxCmdSingleClickAddToAlbum==aCommandId || 
          EGlxCmdSingleClickAddTag == aCommandId) && 
          aList.SelectionCount() > 0 )
        {   
        return EFalse;
        }
    else if (EGlxCmdAddToAlbum==aCommandId || EGlxCmdAddTag == aCommandId)
        {
        return EFalse;
        }

    return ETrue;
    }