ui/commandhandlers/commoncommandhandlers/src/glxcommandhandleraddtocontainer.cpp
changeset 40 112f0ac2d1f0
parent 26 c499df2dbb33
child 45 863223ea6961
equal deleted inserted replaced
26:c499df2dbb33 40:112f0ac2d1f0
    27 
    27 
    28 #include <glxalbummodel.h>
    28 #include <glxalbummodel.h>
    29 #include <glxcollectionpluginalbums.hrh>
    29 #include <glxcollectionpluginalbums.hrh>
    30 #include <glxmodelparm.h>
    30 #include <glxmodelparm.h>
    31 #include <glxfilterfactory.h>
    31 #include <glxfilterfactory.h>
       
    32 #include <QEventLoop>
    32 
    33 
    33 #include <hblistwidget.h>
    34 #include <hblistwidget.h>
    34 #include <hbview.h>
    35 #include <hbview.h>
    35 #include <hbpushbutton.h>
    36 #include <hbpushbutton.h>
    36 #include <QGraphicsGridLayout>
    37 #include <QGraphicsGridLayout>
    37 #include <hbdialog.h>
    38 #include <hbdialog.h>
    38 #include <hbmessagebox.h>
    39 #include <hbmessagebox.h>
    39 
    40 
       
    41 #include <glxcommandhandlers.hrh>
    40 #include "glxlocalisationstrings.h"
    42 #include "glxlocalisationstrings.h"
    41 
    43 
    42 #include "OstTraceDefinitions.h"
    44 #include "OstTraceDefinitions.h"
    43 #ifdef OST_TRACE_COMPILER_IN_USE
    45 #ifdef OST_TRACE_COMPILER_IN_USE
    44 #include "glxcommandhandleraddtocontainerTraces.h"
    46 #include "glxcommandhandleraddtocontainerTraces.h"
    48 TInt GlxCommandHandlerAddToContainer::iSelectionCount = 0;
    50 TInt GlxCommandHandlerAddToContainer::iSelectionCount = 0;
    49 
    51 
    50 const TInt KSelectionPopupListHierarchy = 5;
    52 const TInt KSelectionPopupListHierarchy = 5;
    51 const TInt KListPrefferedHeight = 400;
    53 const TInt KListPrefferedHeight = 400;
    52 
    54 
    53 GlxQueryContentWidget::GlxQueryContentWidget(QGraphicsItem* parent) :
    55 GlxAlbumSelectionPopup::GlxAlbumSelectionPopup() 
    54     QGraphicsWidget(parent), mButton(0), mListView(0), mGrid(0)
    56     : mPopupDlg( 0 ), 
    55     {
    57       mSelectionModel( 0 ),
    56     OstTraceFunctionEntry0( DUP1_GLXQUERYCONTENTWIDGET_GLXQUERYCONTENTWIDGET_ENTRY );
    58       mEventLoop( 0 ),
    57     mGrid = new QGraphicsGridLayout;
    59       mResult( false )
    58     mGrid->setContentsMargins(0, 0, 0, 0);
    60 {
    59 
    61 }
    60     mButton = new HbPushButton("New Item");
    62 
    61     mListView = new HbListView(this);
    63 GlxAlbumSelectionPopup::~GlxAlbumSelectionPopup()
    62     mListView->setSelectionMode(HbAbstractItemView::MultiSelection);
    64 {
    63     mListView->setPreferredHeight(KListPrefferedHeight);
    65 }
    64 
    66 
    65     mGrid->addItem(mButton, 0, 0);
    67 QModelIndexList GlxAlbumSelectionPopup::GetSelectionList(GlxAlbumModel *model, bool *ok) 
    66     mGrid->addItem(mListView, 1, 0);
    68 {
    67     setLayout(mGrid); //ownership transfered
    69     // Create a popup
    68 
    70     HbDialog popup;
    69     OstTraceFunctionExit0( DUP1_GLXQUERYCONTENTWIDGET_GLXQUERYCONTENTWIDGET_EXIT );
    71     QEventLoop eventLoop;
    70     }
    72     mEventLoop = &eventLoop;
    71 
    73     
    72 GlxQueryContentWidget::~GlxQueryContentWidget()
    74     popup.setPreferredHeight( KListPrefferedHeight );
    73     {
    75     // Set dismiss policy that determines what tap events will cause the popup
    74     OstTraceFunctionEntry0( GLXQUERYCONTENTWIDGET_GLXQUERYCONTENTWIDGET_ENTRY );
    76     // to be dismissed
    75     delete mListView;
    77     popup.setDismissPolicy(HbDialog::NoDismiss);
    76     delete mButton;
    78 
    77     OstTraceFunctionExit0( GLXQUERYCONTENTWIDGET_GLXQUERYCONTENTWIDGET_EXIT );
    79     // Set timeout to zero to wait user to either click Ok or Cancel
    78     }
    80     popup.setTimeout(HbDialog::NoTimeout);
       
    81     popup.setHeadingWidget( new HbLabel("Select Album") );      
       
    82     
       
    83     mPopupDlg = &popup;
       
    84     HbListView *listview = new HbListView();
       
    85     listview->setSelectionMode(HbAbstractItemView::MultiSelection);
       
    86     listview->setModel(model);
       
    87     mSelectionModel = listview->selectionModel() ;
       
    88     connect( mSelectionModel, SIGNAL( selectionChanged( const QItemSelection &, const QItemSelection& ) ), this, SLOT( changeButtonText() ) );
       
    89     
       
    90     HbAction *primary = new HbAction( "New" );
       
    91     popup.addAction( primary ) ;
       
    92 
       
    93     HbAction *secondary = new HbAction( GLX_BUTTON_CANCEL );
       
    94     popup.addAction( secondary );
       
    95     
       
    96     popup.setContentWidget( listview ); //ownership transfer
       
    97     listview->show();
       
    98     
       
    99     popup.open( this, SLOT( dialogClosed( HbAction* ) ) ); 
       
   100     eventLoop.exec( );
       
   101     mEventLoop = 0 ;
       
   102     if ( ok ) {
       
   103         *ok = mResult ;
       
   104     }
       
   105     QModelIndexList selectedIndexes = mSelectionModel->selectedIndexes();       
       
   106     disconnect( mSelectionModel, SIGNAL( selectionChanged( const QItemSelection &, const QItemSelection& ) ), this, SLOT( changeButtonText() ) );
       
   107     delete primary;
       
   108     delete secondary;
       
   109 
       
   110     return selectedIndexes;
       
   111 }
       
   112 
       
   113 void GlxAlbumSelectionPopup::changeButtonText()
       
   114 {
       
   115     if ( mSelectionModel->selectedIndexes().count() ) {
       
   116         mPopupDlg->actions().first()->setText( GLX_BUTTON_OK );
       
   117     }
       
   118     else {
       
   119         mPopupDlg->actions().first()->setText("New");
       
   120     }    
       
   121 }
       
   122 
       
   123 
       
   124 void GlxAlbumSelectionPopup::dialogClosed(HbAction *action)
       
   125 {
       
   126     HbDialog *dlg = static_cast<HbDialog*>(sender());
       
   127     if( action == dlg->actions().first() ) {
       
   128         mResult = true ;
       
   129     }
       
   130     else {
       
   131         mResult = false ;
       
   132     }
       
   133     if ( mEventLoop && mEventLoop->isRunning( ) ) {
       
   134         mEventLoop->exit( 0 );
       
   135     }
       
   136 }
    79 
   137 
    80 GlxCommandHandlerAddToContainer::GlxCommandHandlerAddToContainer() :
   138 GlxCommandHandlerAddToContainer::GlxCommandHandlerAddToContainer() :
    81     mNewMediaAdded(false)
   139     mNewMediaAdded(false)
    82     {
   140     {
    83     OstTraceFunctionEntry0( GLXCOMMANDHANDLERADDTOCONTAINER_GLXCOMMANDHANDLERADDTOCONTAINER_ENTRY );
   141     OstTraceFunctionEntry0( GLXCOMMANDHANDLERADDTOCONTAINER_GLXCOMMANDHANDLERADDTOCONTAINER_ENTRY );
    90     OstTraceFunctionEntry0( DUP1_GLXCOMMANDHANDLERADDTOCONTAINER_GLXCOMMANDHANDLERADDTOCONTAINER_ENTRY );
   148     OstTraceFunctionEntry0( DUP1_GLXCOMMANDHANDLERADDTOCONTAINER_GLXCOMMANDHANDLERADDTOCONTAINER_ENTRY );
    91     delete mTargetContainers;
   149     delete mTargetContainers;
    92     OstTraceFunctionExit0( DUP1_GLXCOMMANDHANDLERADDTOCONTAINER_GLXCOMMANDHANDLERADDTOCONTAINER_EXIT );
   150     OstTraceFunctionExit0( DUP1_GLXCOMMANDHANDLERADDTOCONTAINER_GLXCOMMANDHANDLERADDTOCONTAINER_EXIT );
    93     }
   151     }
    94 
   152 
    95 CMPXCommand* GlxCommandHandlerAddToContainer::CreateCommandL(TInt /*aCommandId*/,
   153 CMPXCommand* GlxCommandHandlerAddToContainer::CreateCommandL(TInt aCommandId,
    96         MGlxMediaList& aMediaList, TBool& /*aConsume*/) const
   154         MGlxMediaList& aMediaList, TBool& /*aConsume*/) const
    97     {
   155     {
    98     OstTraceFunctionEntry0( GLXCOMMANDHANDLERADDTOCONTAINER_CREATECOMMANDL_ENTRY );
   156     OstTraceFunctionEntry0( GLXCOMMANDHANDLERADDTOCONTAINER_CREATECOMMANDL_ENTRY );
    99     iSelectionCount = 0;
   157     iSelectionCount = 0;
   100 
   158     CMPXCommand* command = NULL;
   101     CMPXCollectionPath* sourceItems = aMediaList.PathLC(
   159 
       
   160     if(aCommandId == EGlxCmdAddToFav)
       
   161         {
       
   162            CMPXCollectionPath* targetCollection = CMPXCollectionPath::NewL();
       
   163            CleanupStack::PushL(targetCollection);
       
   164            // The target collection has to be appeneded with the albums plugin id
       
   165            targetCollection->AppendL(KGlxCollectionPluginAlbumsImplementationUid);
       
   166            // The target collection has also to be appeneded with the the relation id.
       
   167            // appending another level into the albums to get favourites and 1 is the relation id of albums
       
   168            targetCollection->AppendL( TMPXItemId(KGlxCollectionFavoritesId) );
       
   169            targetCollection->Set( 0 );
       
   170     
       
   171            CMPXCollectionPath* sourceItems = aMediaList.PathLC( NGlxListDefs::EPathFocusOrSelection );
       
   172       
       
   173            command = TGlxCommandFactory::AddToContainerCommandLC(
       
   174                *sourceItems, *targetCollection);
       
   175            CleanupStack::Pop(command); 
       
   176            
       
   177            CleanupStack::PopAndDestroy(sourceItems);
       
   178            CleanupStack::PopAndDestroy(targetCollection);
       
   179         }
       
   180     else
       
   181         {
       
   182 
       
   183             CMPXCollectionPath* sourceItems = aMediaList.PathLC(
   102             NGlxListDefs::EPathFocusOrSelection);
   184             NGlxListDefs::EPathFocusOrSelection);
   103     bool ok = false;
   185             bool ok = false;
   104 
   186         
   105     //create target medialist
   187             //create target medialist
   106     CMPXCollectionPath* path = CMPXCollectionPath::NewL();
   188             CMPXCollectionPath* path = CMPXCollectionPath::NewL();
   107     CleanupStack::PushL(path);
   189             CleanupStack::PushL(path);
   108     path->AppendL(KGlxCollectionPluginAlbumsImplementationUid);
   190             path->AppendL(KGlxCollectionPluginAlbumsImplementationUid);
   109 
   191         
   110     CMPXFilter* filter =
   192             CMPXFilter* filter =
   111             TGlxFilterFactory::CreateCameraAlbumExclusionFilterL();
   193             TGlxFilterFactory::CreateCameraAlbumExclusionFilterL();
   112     CleanupStack::PushL(filter);
   194             CleanupStack::PushL(filter);
   113 
   195     
   114     // Create the media list
   196             // Create the media list
   115     MGlxMediaList* targetMediaList = MGlxMediaList::InstanceL(*path,
   197             MGlxMediaList* targetMediaList = MGlxMediaList::InstanceL(*path,
   116             TGlxHierarchyId(KSelectionPopupListHierarchy), filter); //todo take actual hierarchy 
   198                     TGlxHierarchyId(KSelectionPopupListHierarchy), filter); //todo take actual hierarchy 
   117     CleanupStack::PopAndDestroy(filter);
   199             CleanupStack::PopAndDestroy(filter);
   118     CleanupStack::PopAndDestroy(path);
   200             CleanupStack::PopAndDestroy(path);
   119 
   201     
   120     //create target model
   202             //create target model
   121     GlxModelParm modelParm(KGlxCollectionPluginAlbumsImplementationUid,
   203             GlxModelParm modelParm(KGlxCollectionPluginAlbumsImplementationUid,
   122             KSelectionPopupListHierarchy, EGlxFilterExcludeCamera);
   204                     KSelectionPopupListHierarchy, EGlxFilterExcludeCamera);
   123     GlxAlbumModel *albumMediaModel = new GlxAlbumModel(modelParm);
   205             GlxAlbumModel *albumMediaModel = new GlxAlbumModel(modelParm);
   124     albumMediaModel->setData(QModelIndex(), (int) GlxContextSelectionList,
   206             albumMediaModel->setData(QModelIndex(), (int) GlxContextSelectionList,
   125             GlxContextRole);
   207                     GlxContextRole);
   126 
   208     
   127     QModelIndexList modelList = GetSelectionList(albumMediaModel, &ok);
   209             GlxAlbumSelectionPopup popupWidget;
   128 
   210             QModelIndexList modelList = popupWidget.GetSelectionList(albumMediaModel, &ok);
   129     CMPXCommand* command = NULL;
   211             if(ok && modelList.count() == 0)
   130     OstTraceExt2( TRACE_NORMAL, GLXCOMMANDHANDLERADDTOCONTAINER_CREATECOMMANDL, "GlxCommandHandlerAddToContainer::CreateCommandL;ok=%d;newMedia=%d", ok, mNewMediaAdded );
       
   131 
       
   132     if (ok || mNewMediaAdded)
       
   133         {
       
   134         int count = modelList.count();
       
   135 
       
   136         for (int i = 0; i < count; i++)
       
   137             {
   212             {
   138             albumMediaModel->setData(modelList[i], modelList[i].row(),
   213                 createNewMedia();
   139                     GlxSelectedIndexRole);
   214                 ok = false;        
   140             }
   215             }    
   141 
   216             OstTraceExt2( TRACE_NORMAL, GLXCOMMANDHANDLERADDTOCONTAINER_CREATECOMMANDL, "GlxCommandHandlerAddToContainer::CreateCommandL;ok=%d;newMedia=%d", ok, mNewMediaAdded );
   142         if (!mNewMediaAdded)
   217     
   143             {
   218             if (ok || mNewMediaAdded)
   144             CMPXCollectionPath* targetContainers = targetMediaList->PathLC(
   219                 {
   145                     NGlxListDefs::EPathFocusOrSelection);
   220                 int count = modelList.count();
   146             CleanupStack::Pop(targetContainers);
   221     
   147             delete mTargetContainers;
   222                 for (int i = 0; i < count; i++)
   148             mTargetContainers = NULL;
   223                     {
   149             mTargetContainers = targetContainers;
   224                     albumMediaModel->setData(modelList[i], modelList[i].row(),
   150             }
   225                             GlxSelectedIndexRole);
   151 
   226                     }
   152         command = TGlxCommandFactory::AddToContainerCommandLC(*sourceItems,
   227     
   153                 *mTargetContainers);
   228                 if (!mNewMediaAdded)
   154         CleanupStack::Pop(command);
   229                     {
   155         mNewMediaAdded = false;
   230                     CMPXCollectionPath* targetContainers = targetMediaList->PathLC(
   156         }
   231                             NGlxListDefs::EPathFocusOrSelection);
   157     else
   232                     CleanupStack::Pop(targetContainers);
   158         {
   233                     delete mTargetContainers;
   159         MGlxMediaList::UnmarkAllL(aMediaList);
   234                     mTargetContainers = NULL;
   160         }
   235                     mTargetContainers = targetContainers;
   161 
   236                     }
   162     MGlxMediaList::UnmarkAllL(*targetMediaList);
   237     
   163     targetMediaList->Close();
   238                 command = TGlxCommandFactory::AddToContainerCommandLC(*sourceItems,
   164     CleanupStack::PopAndDestroy(sourceItems);
   239                         *mTargetContainers);
   165     delete albumMediaModel;
   240                 CleanupStack::Pop(command);
       
   241                 mNewMediaAdded = false;
       
   242                 }
       
   243             else
       
   244                 {
       
   245                 MGlxMediaList::UnmarkAllL(aMediaList);
       
   246                 }
       
   247     
       
   248             MGlxMediaList::UnmarkAllL(*targetMediaList);
       
   249             targetMediaList->Close();
       
   250             CleanupStack::PopAndDestroy(sourceItems);
       
   251             delete albumMediaModel;
       
   252         }
   166     OstTraceFunctionExit0( GLXCOMMANDHANDLERADDTOCONTAINER_CREATECOMMANDL_EXIT );
   253     OstTraceFunctionExit0( GLXCOMMANDHANDLERADDTOCONTAINER_CREATECOMMANDL_EXIT );
   167     return command;
   254     return command;
   168     }
   255     }
   169 
   256   
   170 QModelIndexList GlxCommandHandlerAddToContainer::GetSelectionList(
   257 void GlxCommandHandlerAddToContainer::createNewMedia() const
   171         GlxAlbumModel *model, bool *ok) const
       
   172     {
       
   173     OstTraceFunctionEntry0( GLXCOMMANDHANDLERADDTOCONTAINER_GETSELECTIONLIST_ENTRY );
       
   174     // Create a popup
       
   175     HbDialog popup;
       
   176     popup.setPreferredHeight(400);
       
   177     // Set dismiss policy that determines what tap events will cause the popup
       
   178     // to be dismissed
       
   179     popup.setDismissPolicy(HbDialog::NoDismiss);
       
   180 
       
   181     // Set timeout to zero to wait user to either click Ok or Cancel
       
   182     popup.setTimeout(HbDialog::NoTimeout);
       
   183     popup.setHeadingWidget(new HbLabel("Selection List"));
       
   184 
       
   185     GlxQueryContentWidget* view = new GlxQueryContentWidget();
       
   186     view->mListView->setModel(model);
       
   187 
       
   188     connect(view->mButton, SIGNAL(released ()), &popup, SLOT(close()));
       
   189     connect(view->mButton, SIGNAL(released ()), this, SLOT(createNewMedia()));
       
   190 
       
   191 
       
   192     HbAction *primary = new HbAction(GLX_BUTTON_OK);
       
   193     popup.setPrimaryAction(primary);
       
   194 
       
   195     HbAction *secondary = new HbAction(GLX_BUTTON_CANCEL);
       
   196     popup.setSecondaryAction(secondary);
       
   197     
       
   198     popup.setContentWidget(view); //ownership transfer
       
   199     view->mListView->show();
       
   200     
       
   201     QModelIndexList selectedIndexes;
       
   202 
       
   203     do
       
   204         {
       
   205         HbAction* action = popup.exec();
       
   206         if (action == popup.primaryAction())
       
   207             {
       
   208             *ok = true;
       
   209             }
       
   210         else
       
   211             {
       
   212             *ok = false;
       
   213             }
       
   214         OstTrace1( TRACE_NORMAL, GLXCOMMANDHANDLERADDTOCONTAINER_GETSELECTIONLIST, "GlxCommandHandlerAddToContainer::GetSelectionList;ok=%d", *ok );
       
   215 
       
   216 
       
   217         if (*ok)
       
   218             {
       
   219             GlxQueryContentWidget* cWidget = qobject_cast<
       
   220                     GlxQueryContentWidget*> (popup.contentWidget());
       
   221             QItemSelectionModel* selModel =
       
   222                     cWidget->mListView->selectionModel();
       
   223             selectedIndexes = selModel->selectedIndexes();
       
   224             }
       
   225         }
       
   226     while (*ok && selectedIndexes.count() == 0); //continue until user select one album list or new item
       
   227 
       
   228     delete primary;
       
   229     delete secondary;
       
   230 
       
   231     OstTraceFunctionExit0( GLXCOMMANDHANDLERADDTOCONTAINER_GETSELECTIONLIST_EXIT );
       
   232     return selectedIndexes;
       
   233     }
       
   234 
       
   235 void GlxCommandHandlerAddToContainer::createNewMedia()
       
   236     {
   258     {
   237     OstTraceFunctionEntry0( GLXCOMMANDHANDLERADDTOCONTAINER_CREATENEWMEDIA_ENTRY );
   259     OstTraceFunctionEntry0( GLXCOMMANDHANDLERADDTOCONTAINER_CREATENEWMEDIA_ENTRY );
   238     GlxCommandHandlerNewMedia* commandHandlerNewMedia =
   260     GlxCommandHandlerNewMedia* commandHandlerNewMedia =
   239             new GlxCommandHandlerNewMedia();
   261             new GlxCommandHandlerNewMedia();
   240     TGlxMediaId newMediaId;
   262     TGlxMediaId newMediaId;