mpviewplugins/mpcollectionviewplugin/src/mpcollectionpopuphandler.cpp
changeset 25 3ec52facab4d
child 29 8192e5b5c935
equal deleted inserted replaced
22:ecf06a08d4d9 25:3ec52facab4d
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Music Player collection popup handler.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <hbaction.h>
       
    20 #include <hbmenu.h>
       
    21 #include <hbmessagebox.h>
       
    22 #include <hbselectiondialog.h>
       
    23 #include <hbinputdialog.h>
       
    24 #include <hblabel.h>
       
    25 #include <hblistview.h>
       
    26 #include <hbscrollbar.h>
       
    27 
       
    28 #include "mpengine.h"
       
    29 #include "mpcollectionview.h"
       
    30 #include "mpmpxcollectiondata.h"
       
    31 #include "mpcollectiondatamodel.h"
       
    32 #include "mpmpxcollectionviewdefs.h"
       
    33 #include "mpcollectionpopuphandler.h"
       
    34 #include "mptrace.h"
       
    35 
       
    36 const int KNullIndex = -1;
       
    37 
       
    38 /*!
       
    39     \class MpCollectionPopupHandler
       
    40     \brief Music Player collection popup handler.
       
    41 
       
    42     This class controls the asynchronous operation of all dialogs 
       
    43     used by collection view.
       
    44 */
       
    45 
       
    46 /*!
       
    47  Constructs the collection popup handler.
       
    48  */
       
    49 MpCollectionPopupHandler::MpCollectionPopupHandler( MpCollectionView *parent )
       
    50     : QObject( parent ),
       
    51       mView( parent ),
       
    52       mOutstandingPopup( 0 ),
       
    53       mMpEngine( 0 ),
       
    54       mPermanentData( 0 )
       
    55 {
       
    56     TX_ENTRY
       
    57     mMpEngine = MpEngine::instance();
       
    58     mPermanentData = new MpPopupHandlerPermanentData( this );
       
    59     TX_EXIT
       
    60 }
       
    61 
       
    62 /*!
       
    63  Destructs the collection popup handler.
       
    64  */
       
    65 MpCollectionPopupHandler::~MpCollectionPopupHandler()
       
    66 {
       
    67     TX_ENTRY
       
    68     delete mOutstandingPopup;
       
    69     TX_EXIT
       
    70 }
       
    71 
       
    72 /*!
       
    73  Default view context menu.
       
    74  */
       
    75 void MpCollectionPopupHandler::openDefaultViewContextMenu( int index, const QPointF &coords )
       
    76 {
       
    77     TX_ENTRY
       
    78     HbMenu *contextMenu = 0;
       
    79     HbAction *action;
       
    80     bool usbBlocked = mMpEngine->verifyUsbBlocking();
       
    81 
       
    82     switch ( mMpEngine->collectionData()->context() ) {
       
    83         case ECollectionContextAllSongs:
       
    84         case ECollectionContextArtistSongs:
       
    85         case ECollectionContextAlbumSongs:
       
    86             contextMenu = new HbMenu();
       
    87             action = contextMenu->addAction( hbTrId( "txt_common_menu_play_music" ) );
       
    88             action->setObjectName( "open" );
       
    89             action = contextMenu->addAction( hbTrId( "txt_mus_menu_add_to_playlist" ) );
       
    90             action->setObjectName( "add" );
       
    91             action->setEnabled( !usbBlocked );
       
    92             action = contextMenu->addAction( hbTrId( "txt_common_menu_delete" ) );
       
    93             action->setObjectName( "delete" );
       
    94             action->setEnabled( !usbBlocked );
       
    95             break;
       
    96         case ECollectionContextAlbums:
       
    97         case ECollectionContextArtists:
       
    98         case ECollectionContextArtistAlbums:
       
    99             contextMenu = new HbMenu();
       
   100             action = contextMenu->addAction( hbTrId( "txt_common_menu_open" ) );
       
   101             action->setObjectName( "open" );
       
   102             action = contextMenu->addAction( hbTrId( "txt_mus_menu_add_to_playlist" ) );
       
   103             action->setObjectName( "add" );
       
   104             action->setEnabled( !usbBlocked );
       
   105             action = contextMenu->addAction( hbTrId( "txt_common_menu_delete" ) );
       
   106             action->setObjectName( "delete" );
       
   107             action->setEnabled( !usbBlocked );
       
   108             break;
       
   109         case ECollectionContextPlaylists:
       
   110             if ( !mMpEngine->collectionData()->isAutoPlaylist( index ) ) {
       
   111                 contextMenu = new HbMenu();
       
   112                 action = contextMenu->addAction( hbTrId( "txt_common_menu_open" ) );
       
   113                 action->setObjectName( "open" );
       
   114                 action = contextMenu->addAction( hbTrId( "txt_common_menu_delete" ) );
       
   115                 action->setObjectName("delete");
       
   116                 action->setEnabled( !usbBlocked );
       
   117                 action = contextMenu->addAction( hbTrId( "txt_common_menu_rename_item" ) );
       
   118                 action->setObjectName( "rename playlist" );
       
   119                 action->setEnabled( !usbBlocked );
       
   120             }
       
   121             break;
       
   122         case ECollectionContextPlaylistSongs:
       
   123             if ( !mMpEngine->collectionData()->isAutoPlaylist() ) {
       
   124                 contextMenu = new HbMenu();
       
   125                 action = contextMenu->addAction( hbTrId( "txt_common_menu_play_music" ) );
       
   126                 action->setObjectName( "open" );
       
   127                 action = contextMenu->addAction( hbTrId( "txt_common_menu_remove" ) );
       
   128                 action->setObjectName( "delete" );
       
   129                 action->setEnabled( !usbBlocked );
       
   130             }
       
   131             break;
       
   132         default:
       
   133             break;
       
   134     }
       
   135 
       
   136     if ( contextMenu ) {
       
   137         if ( mView->isActivated() ) {
       
   138             mPermanentData->mContextMenuIndex = index;
       
   139             contextMenu->setPreferredPos( coords );
       
   140             contextMenu->setAttribute( Qt::WA_DeleteOnClose );
       
   141             setOutstandingPopup( contextMenu );
       
   142             contextMenu->open( this, SLOT( defaultContextMenuOptionSelected( HbAction* ) ) );
       
   143         }
       
   144         else {
       
   145             delete contextMenu;
       
   146             contextMenu = 0;
       
   147         }
       
   148     }
       
   149 
       
   150     TX_EXIT
       
   151 }
       
   152 
       
   153 /*!
       
   154  Fetch view context menu
       
   155  */
       
   156 void MpCollectionPopupHandler::openFetchViewContextMenu( int index, const QPointF &coords )
       
   157 {
       
   158     TX_ENTRY_ARGS( "index=" << index );
       
   159 
       
   160     HbAction *action;
       
   161     HbMenu *contextMenu = 0;
       
   162 
       
   163     switch ( mMpEngine->collectionData()->context() ) {
       
   164         case ECollectionContextAllSongs:
       
   165         case ECollectionContextArtistSongs:
       
   166         case ECollectionContextAlbumSongs:
       
   167         case ECollectionContextPlaylistSongs:
       
   168         case ECollectionContextGenreSongs:
       
   169             contextMenu = new HbMenu();
       
   170             action = contextMenu->addAction( hbTrId("txt_common_menu_play_music") );
       
   171             action->setEnabled( mMpEngine->verifyUsbBlocking() );
       
   172             break;
       
   173         default:
       
   174             break;
       
   175     }
       
   176 
       
   177     if ( contextMenu ) {
       
   178         if ( mView->isActivated() ) {
       
   179             mPermanentData->mContextMenuIndex = index;
       
   180             contextMenu->setPreferredPos( coords );
       
   181             contextMenu->setAttribute( Qt::WA_DeleteOnClose );
       
   182             setOutstandingPopup( contextMenu );
       
   183             contextMenu->open( this, SLOT( fetcherContextMenuOptionSelected( HbAction* ) ) );        
       
   184         }
       
   185         else {
       
   186             delete contextMenu;
       
   187             contextMenu = 0;
       
   188         }
       
   189     }
       
   190 
       
   191     TX_EXIT
       
   192 }
       
   193 
       
   194 /*!
       
   195  Request a string to rename the playlist container.
       
   196  */
       
   197 void MpCollectionPopupHandler::openRenamePlaylistContainerDialog( const QString &currentName )
       
   198 {
       
   199     TX_ENTRY_ARGS( "Current name=" << currentName );
       
   200     mPermanentData->mOriginalName = currentName;
       
   201     getText( hbTrId( "txt_mus_dialog_enter_name" ), currentName,
       
   202              SLOT( handleRenamePlaylistContainer( HbAction* ) ) );
       
   203     TX_EXIT
       
   204 }
       
   205 
       
   206 /*!
       
   207  Request a string to rename the playlist item.
       
   208  */
       
   209 void MpCollectionPopupHandler::openRenamePlaylistItemDialog( const QString &currentName )
       
   210 {
       
   211     TX_ENTRY_ARGS( "Current name=" << currentName );
       
   212     mPermanentData->mOriginalName = currentName;
       
   213     getText( hbTrId( "txt_mus_dialog_enter_name" ), currentName,
       
   214              SLOT( handleRenamePlaylistItem( HbAction* ) ) );
       
   215     TX_EXIT
       
   216 }
       
   217 
       
   218 /*!
       
   219  Request to select songs to add to the playlist
       
   220  */
       
   221 void MpCollectionPopupHandler::openAddSongsToPlaylist( QAbstractItemModel* model )
       
   222 {
       
   223     TX_ENTRY
       
   224     getModelIndexes( hbTrId( "txt_mus_title_select_songs" ), model,
       
   225                      SLOT( handleAddSongsToPlayList( HbAction* ) ) );
       
   226     TX_EXIT
       
   227 }
       
   228 
       
   229 /*!
       
   230  Request to select songs to be deleted
       
   231  */
       
   232 void MpCollectionPopupHandler::openDeleteSongs( QAbstractItemModel* model )
       
   233 {
       
   234     TX_ENTRY
       
   235     getModelIndexes( hbTrId( "txt_mus_title_select_songs" ), model, 
       
   236                      SLOT( handleDeleteSongs( HbAction* ) ) );
       
   237     TX_EXIT
       
   238 }
       
   239 
       
   240 /*!
       
   241  Request to select songs to be added to the current play list
       
   242  */
       
   243 void MpCollectionPopupHandler::openAddToCurrentPlaylist( MpMpxCollectionData* collectionData )
       
   244 {
       
   245     TX_ENTRY
       
   246     mPermanentData->mIsolatedCollectionData = collectionData;
       
   247     MpCollectionDataModel *collectionDataModel;
       
   248     collectionDataModel = new MpCollectionDataModel( collectionData );
       
   249     collectionDataModel->refreshModel();
       
   250 
       
   251     getModelIndexes( hbTrId( "txt_mus_title_select_songs" ), collectionDataModel, 
       
   252                      SLOT( handleAddToCurrentPlaylist( HbAction* ) ) );
       
   253     TX_EXIT
       
   254 }
       
   255 
       
   256 /*!
       
   257  Request to create a new play list and then add songs to it.
       
   258  */
       
   259 void MpCollectionPopupHandler::openCreateNewPlaylist( MpMpxCollectionData* collectionData )
       
   260 {
       
   261     TX_ENTRY
       
   262     mPermanentData->mIsolatedCollectionData = collectionData;
       
   263 
       
   264     MpCollectionDataModel *collectionDataModel;
       
   265     collectionDataModel = new MpCollectionDataModel( collectionData );
       
   266     collectionDataModel->refreshModel();
       
   267     mPermanentData->mAbstractItemModel = collectionDataModel;
       
   268 
       
   269     QStringList playlists;
       
   270     mMpEngine->findPlaylists( playlists );
       
   271     queryNewPlaylistName( playlists, SLOT( handleCreateNewPlaylistGetTextFinished( HbAction* ) ) );
       
   272     TX_EXIT
       
   273 }
       
   274 
       
   275 /*!
       
   276  Request to reorder songs
       
   277  */
       
   278 void MpCollectionPopupHandler::openArrangeSongs( MpMpxCollectionData* collectionData )
       
   279 {
       
   280     TX_ENTRY
       
   281     launchArrangeSongsDialog( collectionData );
       
   282     TX_EXIT
       
   283 }
       
   284 
       
   285 /*!
       
   286  Closes any active popup
       
   287  */
       
   288 void MpCollectionPopupHandler::cancelOngoingPopup()
       
   289 {
       
   290     TX_ENTRY    
       
   291     if ( mOutstandingPopup ) {
       
   292          mOutstandingPopup->close();
       
   293          
       
   294          //Delete/Clear permanent data until current popup gets deleted
       
   295          mPermanentData->setParent( mOutstandingPopup );
       
   296          //Generate new permanent data for future popups
       
   297          mPermanentData = new MpPopupHandlerPermanentData( this );
       
   298     }
       
   299     else {
       
   300         mPermanentData->clear();
       
   301     }
       
   302 
       
   303     TX_EXIT
       
   304 }
       
   305 
       
   306 /*!
       
   307  TODO: Workaround for HbListView multiple events: longPress and activated. Remove once HbListView get fixed (wk16)
       
   308  */
       
   309 bool MpCollectionPopupHandler::showingPopup()
       
   310 {
       
   311     return mOutstandingPopup;
       
   312 }
       
   313 
       
   314 /*!
       
   315  Slot to be called when an option has been selected from default context menu.
       
   316  */
       
   317 void MpCollectionPopupHandler::defaultContextMenuOptionSelected( HbAction *selectedAction )
       
   318 {
       
   319     TX_ENTRY
       
   320     if ( selectedAction ) {
       
   321         QString objectName = selectedAction->objectName();
       
   322         QList<int> selection;
       
   323         selection.append( mPermanentData->mContextMenuIndex );
       
   324         if ( objectName == "open" ) {
       
   325             mView->openItem( mPermanentData->mContextMenuIndex );
       
   326         }
       
   327         if ( objectName == "add" ) {
       
   328             launchAddToPlaylistDialog( selection );
       
   329         }
       
   330         else if ( objectName == "delete" ) {
       
   331             requestDelete( selection );
       
   332         }
       
   333         else if ( objectName == "rename playlist" ) {
       
   334             QString currentName;
       
   335             currentName = mMpEngine->collectionData()->itemData( 
       
   336                     mPermanentData->mContextMenuIndex, MpMpxCollectionData::Title );
       
   337             openRenamePlaylistItemDialog( currentName );
       
   338         }
       
   339     }
       
   340     TX_EXIT
       
   341 }
       
   342 
       
   343 /*!
       
   344  Slot to be called when play option has been selected from fetcher context menu.
       
   345  */
       
   346 void MpCollectionPopupHandler::fetcherContextMenuOptionSelected( HbAction *selectedAction )
       
   347 {
       
   348     TX_ENTRY
       
   349     if ( selectedAction ) {
       
   350         // Start the playback process. View will switch to playbackview.
       
   351         mMpEngine->previewItem( mPermanentData->mContextMenuIndex );
       
   352     }        
       
   353     TX_EXIT
       
   354 }
       
   355 
       
   356 /*!
       
   357  Slot to be called when AddToPlaylist dialog has finished
       
   358  */
       
   359 void MpCollectionPopupHandler::handleAddToPlaylistDialogFinished( HbAction *selectedAction )
       
   360 {
       
   361     TX_ENTRY
       
   362     HbSelectionDialog *dialog = qobject_cast<HbSelectionDialog *>( sender() );
       
   363     clearOutstandingPopup( dialog );
       
   364     bool operationCompleted( true );
       
   365 
       
   366     if ( selectedAction ) {
       
   367         QString objectName = selectedAction->objectName();
       
   368         if ( objectName == "new" ) {
       
   369             QStringList playlists;
       
   370             mMpEngine->findPlaylists( playlists );
       
   371             queryNewPlaylistName( playlists, SLOT( handleAddToPlaylistGetTextFinished( HbAction* ) ) );
       
   372             operationCompleted = false;
       
   373         }
       
   374     } 
       
   375     else if ( dialog->selectedItems().count() ) {//this only works for SingleSelection
       
   376         // User selected existing playlist.
       
   377         mMpEngine->saveToPlaylist( dialog->selectedItems().at( 0 ).toInt(), mPermanentData->mSelectedItems );
       
   378     }
       
   379     
       
   380     if ( operationCompleted ) {
       
   381         mPermanentData->clear();
       
   382     }
       
   383     TX_EXIT
       
   384 }
       
   385 
       
   386 /*!
       
   387  Slot to be called when input dialog (getText) has finished, comming from AddToPlayList.
       
   388  */
       
   389 void MpCollectionPopupHandler::handleAddToPlaylistGetTextFinished( HbAction *selectedAction )
       
   390 {
       
   391     TX_ENTRY
       
   392     HbInputDialog *dialog = qobject_cast<HbInputDialog *>( sender() );
       
   393     clearOutstandingPopup( dialog );
       
   394     bool operationCompleted( true );
       
   395 
       
   396     if ( selectedAction ) {
       
   397         QString objectName = selectedAction->objectName();
       
   398         if ( objectName == "ok" ) {
       
   399             QString newPlaylistName = dialog->value().toString();
       
   400             if ( newPlaylistName.length() ) {
       
   401                 //Create new playlist with given name
       
   402                 mMpEngine->createPlaylist( newPlaylistName, mPermanentData->mSelectedItems );
       
   403             }
       
   404             else {
       
   405                 //No valid name, prompt for one again.
       
   406                 getText( hbTrId( "txt_mus_dialog_enter_name" ), newPlaylistName,
       
   407                          SLOT( handleAddToPlaylistGetTextFinished( HbAction* ) ) );
       
   408                 operationCompleted = false;
       
   409             }
       
   410         }
       
   411         else {
       
   412             // user decided to not provide a new name, go back to playlist list selection
       
   413             QStringList playlists;
       
   414             mMpEngine->findPlaylists( playlists );
       
   415             if ( playlists.count() ) {
       
   416                 //are saved playlists, back to playlist selection dialog
       
   417                 launchAddToPlaylistDialog( mPermanentData->mSelectedItems );
       
   418                 operationCompleted = false;
       
   419             }
       
   420         }
       
   421     }
       
   422 
       
   423     if ( operationCompleted ) {
       
   424         mPermanentData->clear();
       
   425     }
       
   426     TX_EXIT
       
   427 }
       
   428 
       
   429 /*!
       
   430  Slot to be called when input dialog (getText) has finished, comming from RenamePlaylistContainer.
       
   431  */
       
   432 void MpCollectionPopupHandler::handleRenamePlaylistContainer( HbAction *selectedAction )
       
   433 {
       
   434     TX_ENTRY
       
   435     HbInputDialog *dialog = qobject_cast<HbInputDialog *>( sender() );
       
   436     clearOutstandingPopup( dialog );
       
   437     bool operationCompleted( true );
       
   438 
       
   439     if ( selectedAction ) {
       
   440         QString objectName = selectedAction->objectName();
       
   441         if ( objectName == "ok" ) {
       
   442             QString newPlaylistName = dialog->value().toString();
       
   443             if ( newPlaylistName.length() ) {
       
   444                 if ( newPlaylistName != mPermanentData->mOriginalName ) {
       
   445                     mMpEngine->renamePlaylist( newPlaylistName );
       
   446                 }
       
   447             }
       
   448             else {
       
   449                 //No valid name, prompt for one again.
       
   450                 getText( hbTrId( "txt_mus_dialog_enter_name" ), newPlaylistName,
       
   451                          SLOT( handleRenamePlaylistContainer( HbAction* ) ) );
       
   452                 operationCompleted = false;
       
   453             }
       
   454         }
       
   455     }
       
   456 
       
   457     if ( operationCompleted ) {
       
   458         mPermanentData->clear();
       
   459     }
       
   460     TX_EXIT
       
   461 }
       
   462 
       
   463 /*!
       
   464  Slot to be called when input dialog (getText) has finished, comming from RenamePlaylist ContextMenu.
       
   465  */
       
   466 void MpCollectionPopupHandler::handleRenamePlaylistItem( HbAction *selectedAction )
       
   467 {
       
   468     TX_ENTRY
       
   469     HbInputDialog *dialog = qobject_cast<HbInputDialog *>( sender() );
       
   470     clearOutstandingPopup( dialog );
       
   471     bool operationCompleted( true );
       
   472 
       
   473     if ( selectedAction ) {
       
   474         QString objectName = selectedAction->objectName();
       
   475         if ( objectName == "ok" ) {
       
   476             QString newPlaylistName = dialog->value().toString();
       
   477             if ( newPlaylistName.length() ) {
       
   478                 if ( newPlaylistName != mPermanentData->mOriginalName ) {
       
   479                     mMpEngine->renamePlaylist( newPlaylistName, mPermanentData->mContextMenuIndex );
       
   480                 }
       
   481             }
       
   482             else {
       
   483                 //No valid name, prompt for one again.
       
   484                 getText( hbTrId( "txt_mus_dialog_enter_name" ), newPlaylistName,
       
   485                          SLOT( handleRenamePlaylistItem( HbAction* ) ) );
       
   486                 operationCompleted = false;
       
   487             }
       
   488         }
       
   489     }
       
   490 
       
   491     if ( operationCompleted ) {
       
   492         mPermanentData->clear();
       
   493     }
       
   494     TX_EXIT
       
   495 }
       
   496 
       
   497 /*!
       
   498  Slot to be called when select songs dialog (getModelIndexes) has finished, coming from AddSongsToPlayList.
       
   499  */
       
   500 void MpCollectionPopupHandler::handleAddSongsToPlayList( HbAction *selectedAction )
       
   501 {
       
   502     TX_ENTRY
       
   503     HbSelectionDialog *dialog = qobject_cast<HbSelectionDialog *>( sender() );
       
   504     clearOutstandingPopup( dialog );
       
   505 
       
   506     if ( selectedAction ) {
       
   507         QString objectName = selectedAction->objectName();
       
   508         if ( objectName == "ok" ) {
       
   509             QModelIndexList selectedModelIndexes;
       
   510             selectedModelIndexes = dialog->selectedModelIndexes();
       
   511             if ( selectedModelIndexes.count() ) {
       
   512                 QList<int> selection;
       
   513                 for ( int i = 0; i < selectedModelIndexes.size(); ++i ) {
       
   514                     selection.append( selectedModelIndexes.at( i ).row() );
       
   515                 }
       
   516                 launchAddToPlaylistDialog( selection );
       
   517             }
       
   518         }
       
   519     }
       
   520 
       
   521     //Dialog is using CollectionView main model, avoid dialog destructor to alter it.
       
   522     dialog->setModel( 0 );
       
   523 
       
   524     TX_EXIT
       
   525 }
       
   526 
       
   527 /*!
       
   528  Slot to be called when select songs dialog (getModelIndexes) has finished, coming from DeleteSongs.
       
   529  */
       
   530 void MpCollectionPopupHandler::handleDeleteSongs( HbAction *selectedAction )
       
   531 {
       
   532     TX_ENTRY
       
   533     HbSelectionDialog *dialog = qobject_cast<HbSelectionDialog *>( sender() );
       
   534     clearOutstandingPopup( dialog );
       
   535 
       
   536     if ( selectedAction ) {
       
   537         QString objectName = selectedAction->objectName();
       
   538         if ( objectName == "ok" ) {
       
   539             QModelIndexList selectedModelIndexes;
       
   540             selectedModelIndexes = dialog->selectedModelIndexes();
       
   541             if ( selectedModelIndexes.count() ) {
       
   542                 QList<int> selection;
       
   543                 for ( int i = 0; i < selectedModelIndexes.size(); ++i ) {
       
   544                     selection.append( selectedModelIndexes.at( i ).row() );
       
   545                 }
       
   546                 requestDelete( selection );
       
   547             }
       
   548         }
       
   549     }
       
   550 
       
   551     //Dialog is using CollectionView main model, avoid dialog destructor to alter it.
       
   552     dialog->setModel( 0 );
       
   553 
       
   554     TX_EXIT
       
   555 }
       
   556 
       
   557 /*!
       
   558  Slot to be called when select songs dialog (getModelIndexes) has finished, coming from AddToCurrentPlaylist.
       
   559  */
       
   560 void MpCollectionPopupHandler::handleAddToCurrentPlaylist( HbAction *selectedAction )
       
   561 {
       
   562     TX_ENTRY
       
   563     HbSelectionDialog *dialog = qobject_cast<HbSelectionDialog *>( sender() );
       
   564     clearOutstandingPopup( dialog );
       
   565 
       
   566     if ( selectedAction ) {
       
   567         QString objectName = selectedAction->objectName();
       
   568         if ( objectName == "ok" ) {
       
   569             QModelIndexList selectedModelIndexes;
       
   570             selectedModelIndexes = dialog->selectedModelIndexes();
       
   571             if ( selectedModelIndexes.count() ) {
       
   572                 QList<int> selection;
       
   573                 for ( int i = 0; i < selectedModelIndexes.size(); ++i ) {
       
   574                     selection.append( selectedModelIndexes.at( i ).row() );
       
   575                 }
       
   576                 mMpEngine->saveToCurrentPlaylist( selection,
       
   577                         mPermanentData->mIsolatedCollectionData );
       
   578             }
       
   579         }
       
   580     }
       
   581 
       
   582     //Pull the model to delete it. Dialog destructor only removes items from model, but doesn't delete them.
       
   583     mPermanentData->mAbstractItemModel = dialog->model();
       
   584     dialog->setModel( 0 );
       
   585 
       
   586     mPermanentData->clear();
       
   587 
       
   588     TX_EXIT
       
   589 }
       
   590 
       
   591 /*!
       
   592  Slot to be called when the input dialog (getText) has finished, comming from CreateNewPlaylist.
       
   593  */
       
   594 void MpCollectionPopupHandler::handleCreateNewPlaylistGetTextFinished( HbAction *selectedAction )
       
   595 {
       
   596     TX_ENTRY
       
   597     HbInputDialog *dialog = qobject_cast<HbInputDialog *>( sender() );
       
   598     clearOutstandingPopup( dialog );
       
   599     bool operationCompleted( true );
       
   600 
       
   601     if ( selectedAction ) {
       
   602         QString objectName = selectedAction->objectName();
       
   603         if ( objectName == "ok" ) {
       
   604             QString newPlaylistName = dialog->value().toString();
       
   605             if ( newPlaylistName.length() ) {
       
   606                 //Store the new playlist name and query for the items to be added
       
   607                 mPermanentData->mOriginalName = newPlaylistName;
       
   608                 getModelIndexes( hbTrId( "txt_mus_title_select_songs" ), 
       
   609                                  mPermanentData->mAbstractItemModel, 
       
   610                                  SLOT( handleCreateNewPlaylistGetModelIndexesFinished( HbAction* ) ) );
       
   611             }
       
   612             else {
       
   613                 //No valid name, prompt for one again.
       
   614                 getText( hbTrId( "txt_mus_dialog_enter_name" ), newPlaylistName,
       
   615                          SLOT( handleCreateNewPlaylistGetTextFinished( HbAction* ) ) );
       
   616             }
       
   617             operationCompleted = false;
       
   618         }
       
   619     }
       
   620 
       
   621     if ( operationCompleted ) {
       
   622         mPermanentData->clear();
       
   623     }
       
   624     TX_EXIT
       
   625 }
       
   626 
       
   627 /*!
       
   628  Slot to be called when the select songs dialog (getModelIndexes) has finished, coming from CreateNewPlaylist-GetText.
       
   629  */
       
   630 void MpCollectionPopupHandler::handleCreateNewPlaylistGetModelIndexesFinished( HbAction *selectedAction )
       
   631 {
       
   632     TX_ENTRY
       
   633     HbSelectionDialog *dialog = qobject_cast<HbSelectionDialog *>( sender() );
       
   634     clearOutstandingPopup( dialog );
       
   635 
       
   636     if ( selectedAction ) {
       
   637         QString objectName = selectedAction->objectName();
       
   638         if ( objectName == "ok" ) {
       
   639             QList<int> selection;
       
   640             QModelIndexList selectedModelIndexes;
       
   641             selectedModelIndexes = dialog->selectedModelIndexes();
       
   642             if ( selectedModelIndexes.count() ) {
       
   643                 for ( int i = 0; i < selectedModelIndexes.size(); ++i ) {
       
   644                     selection.append( selectedModelIndexes.at( i ).row() );
       
   645                 }
       
   646             }
       
   647             //Creating Playlist even when there is no selection.
       
   648             mMpEngine->createPlaylist( mPermanentData->mOriginalName, selection,
       
   649                     mPermanentData->mIsolatedCollectionData );
       
   650         }
       
   651     }
       
   652 
       
   653     dialog->setModel( 0 );
       
   654 
       
   655     mPermanentData->clear();
       
   656 
       
   657     TX_EXIT
       
   658 }
       
   659 
       
   660 /*!
       
   661  Slot to be called when arrange songs dialog has finished.
       
   662  */
       
   663 void MpCollectionPopupHandler::handleArrangeSongs( HbAction *selectedAction )
       
   664 {
       
   665     TX_ENTRY
       
   666     Q_UNUSED( selectedAction );
       
   667     HbDialog *dialog = qobject_cast<HbDialog *>( sender() );
       
   668     clearOutstandingPopup( dialog );
       
   669     
       
   670     //Reopen the collection so the ordinals get fixed on the view list, if we
       
   671     //delete items the index will not match to the item on the collection.
       
   672     mMpEngine->reopenCollection();
       
   673             
       
   674     TX_EXIT
       
   675 }
       
   676 
       
   677 /*!
       
   678  Slot to be called when delete confirmation dialog has finished.
       
   679  */
       
   680 void MpCollectionPopupHandler::handleRequestDelete( HbAction *selectedAction )
       
   681 {
       
   682     TX_ENTRY
       
   683     HbMessageBox *dialog = qobject_cast<HbMessageBox *>( sender() );
       
   684     clearOutstandingPopup( dialog );
       
   685     
       
   686     if ( selectedAction ) {
       
   687         QString objectName = selectedAction->objectName();
       
   688         if ( objectName == "ok" ) {
       
   689             mMpEngine->deleteSongs( mPermanentData->mSelectedItems );
       
   690         }
       
   691         selectedAction->setEnabled( false );
       
   692     }
       
   693 
       
   694     mPermanentData->clear();
       
   695 
       
   696     TX_EXIT
       
   697 }
       
   698 
       
   699 /*!
       
   700  Slot to be called when a popup is getting closed. Usefull when a dialog is closed before it finishes
       
   701  (dialog not closed by a direct user action).
       
   702  */
       
   703 void MpCollectionPopupHandler::outstandingPopupClosing()
       
   704 {
       
   705     TX_ENTRY
       
   706     HbPopup *popup = qobject_cast<HbPopup *>( sender() );
       
   707     if ( popup ) {
       
   708         Q_ASSERT( popup == mOutstandingPopup );
       
   709         mOutstandingPopup = 0;
       
   710     }
       
   711     TX_EXIT
       
   712 }
       
   713 
       
   714 /*!
       
   715  \internal
       
   716  sets \a popup as the current outstanding popup and cancels any other previous popup.
       
   717  */
       
   718 void MpCollectionPopupHandler::setOutstandingPopup( HbPopup *popup )
       
   719 {
       
   720     TX_ENTRY
       
   721     if ( mOutstandingPopup ) {
       
   722         TX_LOG_ARGS( "Warning: Multiple popups attempted to be displayed" );
       
   723         mOutstandingPopup->close();
       
   724     }
       
   725 
       
   726     connect( popup, SIGNAL( aboutToClose() ), this, SLOT( outstandingPopupClosing() ) );
       
   727     mOutstandingPopup = popup;
       
   728     TX_EXIT
       
   729 }
       
   730 
       
   731 /*!
       
   732  \internal
       
   733  clears and disconnects \a popup. In some cases it's needed to open a new dialog when a current one has just finished.
       
   734  */
       
   735 void MpCollectionPopupHandler::clearOutstandingPopup( HbPopup *popup )
       
   736 {
       
   737     TX_ENTRY
       
   738     disconnect( popup, SIGNAL( aboutToClose() ), this, SLOT( outstandingPopupClosing() ) );
       
   739     mOutstandingPopup = 0;
       
   740     TX_EXIT
       
   741 }
       
   742 
       
   743 /*!
       
   744  \internal
       
   745  Launches the 'Add to playlist' dialog.
       
   746  */
       
   747 void MpCollectionPopupHandler::launchAddToPlaylistDialog( QList<int> &selection )
       
   748 {
       
   749     TX_ENTRY
       
   750     if (!mView->isActivated()) {
       
   751         mPermanentData->clear();
       
   752         return;
       
   753     }
       
   754  
       
   755     QStringList playlists;
       
   756     mPermanentData->mSelectedItems = selection;
       
   757     mMpEngine->findPlaylists( playlists );
       
   758     if ( playlists.count() ) {
       
   759         //There are saved playlists, query for a saved playlist or new.
       
   760         HbAction *action;
       
   761         HbSelectionDialog *dialog = new HbSelectionDialog();
       
   762         dialog->setStringItems( playlists );
       
   763         dialog->setSelectionMode( HbAbstractItemView::SingleSelection );
       
   764         dialog->setHeadingWidget(new HbLabel( hbTrId( "txt_mus_title_select_playlist" ) ) );
       
   765         dialog->clearActions();
       
   766         action = new HbAction( hbTrId( "txt_mus_button_new" ) );
       
   767         action->setObjectName( "new" );
       
   768         dialog->addAction( action );
       
   769         action = new HbAction( hbTrId( "txt_common_button_cancel" ) );
       
   770         action->setObjectName( "cancel" );
       
   771         dialog->addAction( action );
       
   772         dialog->setAttribute( Qt::WA_DeleteOnClose );
       
   773         setOutstandingPopup( dialog );
       
   774         dialog->open( this, SLOT( handleAddToPlaylistDialogFinished( HbAction* ) ) );
       
   775     }
       
   776     else {
       
   777         //querying for a new playlist name.
       
   778         queryNewPlaylistName( playlists, SLOT( handleAddToPlaylistGetTextFinished( HbAction* ) ) );
       
   779     }
       
   780     TX_EXIT
       
   781 }
       
   782 
       
   783 /*!
       
   784  \internal
       
   785  Trigger an imput text dialog with a suggested playlist name.
       
   786  uses \a playlists to generate a suggested playlist name
       
   787  \handler Slot that should be called when input dialog finished 
       
   788  */
       
   789 void MpCollectionPopupHandler::queryNewPlaylistName(const QStringList &playlists,
       
   790                                                     const char *handler ) 
       
   791 {
       
   792     TX_ENTRY
       
   793     int i = 0;
       
   794     for ( ;
       
   795          playlists.contains( hbTrId( "txt_mus_dialog_enter_name_entry_playlist_l1" ).arg( i ) ) ;
       
   796          i++ ) {};
       
   797     QString suggestedPlaylistName = hbTrId( "txt_mus_dialog_enter_name_entry_playlist_l1" ).arg( i );
       
   798     getText( hbTrId( "txt_mus_dialog_enter_name" ), suggestedPlaylistName, handler );
       
   799     TX_EXIT
       
   800 }
       
   801 
       
   802 /*!
       
   803  \internal
       
   804  Launches an input text dialog.
       
   805  \a label Dialog title.
       
   806  \a text Suggested text.
       
   807  \a handler Slot to be called when input dialog finishes.
       
   808  */
       
   809 void MpCollectionPopupHandler::getText( const QString &label, const QString &text,
       
   810                                         const char *handler )
       
   811 {
       
   812     TX_ENTRY
       
   813     if ( !mView->isActivated() ) {
       
   814         mPermanentData->clear();
       
   815         return;
       
   816     }
       
   817 
       
   818     HbAction *action;
       
   819     HbInputDialog *dialog = new HbInputDialog();
       
   820     dialog->setPromptText( label );
       
   821     dialog->setInputMode( HbInputDialog::TextInput );
       
   822     dialog->setValue( text );
       
   823     dialog->clearActions();
       
   824     action = new HbAction( hbTrId( "txt_common_button_ok" ) );
       
   825     action->setObjectName( "ok" );
       
   826     dialog->addAction( action );
       
   827     action = new HbAction( hbTrId( "txt_common_button_cancel" ) );
       
   828     action->setObjectName( "cancel" );
       
   829     dialog->addAction( action );
       
   830     dialog->setAttribute( Qt::WA_DeleteOnClose );
       
   831     setOutstandingPopup( dialog );
       
   832     dialog->open( this, handler );
       
   833     TX_EXIT
       
   834 }
       
   835 
       
   836 /*!
       
   837  \internal
       
   838  Launches a list dialog to select items.
       
   839  \a label Dialog title.
       
   840  \a model List model.
       
   841  \a handler Slot to be called when list dialog finishes.
       
   842  */
       
   843 void MpCollectionPopupHandler::getModelIndexes( const QString &label, QAbstractItemModel* model,
       
   844                                                 const char *handler)
       
   845 {   
       
   846     TX_ENTRY
       
   847 
       
   848     if ( !mView->isActivated() ) {
       
   849         mPermanentData->clear();
       
   850         return;
       
   851     }
       
   852 
       
   853     HbAction *action;
       
   854     HbSelectionDialog *dialog = new HbSelectionDialog();
       
   855     dialog->setHeadingWidget( new HbLabel( label ) );
       
   856     dialog->setSelectionMode( HbAbstractItemView::MultiSelection );
       
   857     dialog->setModel( model );
       
   858     dialog->clearActions();
       
   859     action = new HbAction( hbTrId( "txt_common_button_ok" ) );
       
   860     action->setObjectName( "ok" );
       
   861     dialog->addAction( action );
       
   862     action = new HbAction( hbTrId( "txt_common_button_cancel" ) );
       
   863     action->setObjectName( "cancel" );
       
   864     dialog->addAction( action );
       
   865     dialog->setAttribute( Qt::WA_DeleteOnClose );
       
   866     setOutstandingPopup( dialog );
       
   867     dialog->open( this, handler );
       
   868 
       
   869     TX_EXIT
       
   870 }
       
   871 
       
   872 /*!
       
   873  \internal
       
   874  Launches a list dialog to reorder them.
       
   875  \a collectionData Base to generate dialog's model.
       
   876  */
       
   877 void MpCollectionPopupHandler::launchArrangeSongsDialog( MpMpxCollectionData* collectionData )
       
   878 {   
       
   879     TX_ENTRY
       
   880 
       
   881     if ( !mView->isActivated() ) {
       
   882         return;
       
   883     }
       
   884 
       
   885     HbListView *listView = new HbListView();
       
   886     listView->setItemRecycling( true );
       
   887     listView->setScrollingStyle( HbListView::PanOrFlick );
       
   888     listView->setClampingStyle( HbListView::BounceBackClamping );
       
   889     HbScrollBar *scrollbar = listView->verticalScrollBar();
       
   890     scrollbar->show();
       
   891     scrollbar->setInteractive( true);
       
   892     listView->setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAsNeeded);
       
   893     MpCollectionDataModel *model;
       
   894     //Ownership of the model is passed to the listView as a child object.
       
   895     model = new MpCollectionDataModel( collectionData, listView );
       
   896     model->refreshModel();
       
   897     connect( model,
       
   898              SIGNAL( orderChanged( int, int, int, int ) ),
       
   899              mMpEngine,
       
   900              SLOT( reorderPlaylist( int, int, int, int ) ) );
       
   901     listView->setModel( model );
       
   902     listView->setArrangeMode( true );
       
   903     HbDialog *dialog = new HbDialog();
       
   904     dialog->setDismissPolicy( HbPopup::NoDismiss );
       
   905     dialog->setTimeout( HbPopup::NoTimeout );
       
   906 
       
   907     HbLabel *label = new HbLabel( hbTrId( "txt_mus_title_arrange"  ) );
       
   908     dialog->setHeadingWidget( label );
       
   909     dialog->setContentWidget( listView );
       
   910     dialog->clearActions();
       
   911     HbAction *action;
       
   912     action = new HbAction( hbTrId( "txt_common_button_ok" ) );
       
   913     dialog->addAction( action );
       
   914     dialog->setAttribute( Qt::WA_DeleteOnClose );
       
   915     setOutstandingPopup(dialog);
       
   916     dialog->open( this, SLOT( handleArrangeSongs( HbAction* ) ) );
       
   917 
       
   918     TX_EXIT
       
   919 }
       
   920 
       
   921 /*!
       
   922  \internal
       
   923  Request a delete operation always it has been confirmed.
       
   924  \a selection Items selected to be deleted.
       
   925  */
       
   926 void MpCollectionPopupHandler::requestDelete( QList<int> &selection )
       
   927 {
       
   928     TX_ENTRY
       
   929     QString message;
       
   930     mPermanentData->mSelectedItems = selection;
       
   931     bool needsConfirmation = true;
       
   932 
       
   933     switch ( mMpEngine->collectionData()->context() ) {
       
   934         case ECollectionContextAllSongs:
       
   935         case ECollectionContextArtistSongs:
       
   936         case ECollectionContextAlbumSongs:
       
   937             message = hbTrId( "txt_mus_delete_song" );
       
   938             break;
       
   939         case ECollectionContextArtists:
       
   940             message = hbTrId( "txt_mus_delete_artist" );
       
   941             break;
       
   942         case ECollectionContextAlbums:
       
   943         case ECollectionContextArtistAlbums:
       
   944             message = hbTrId( "txt_mus_delete_album" );
       
   945             break;
       
   946         case ECollectionContextPlaylists:
       
   947             message = hbTrId( "txt_mus_delete_playlist" );
       
   948             break;
       
   949         case ECollectionContextPlaylistSongs:
       
   950         case ECollectionContextGenres:
       
   951         case ECollectionContextGenreSongs:
       
   952             needsConfirmation = false;
       
   953             mMpEngine->deleteSongs( mPermanentData->mSelectedItems );
       
   954             mPermanentData->clear();
       
   955             break;
       
   956         case ECollectionContextUnknown:
       
   957         default:
       
   958             // We shouldn't be here
       
   959             needsConfirmation = false;
       
   960             mPermanentData->clear();
       
   961             TX_LOG_ARGS( "Invalid Collection Context" );
       
   962             break;
       
   963     }
       
   964 
       
   965     if ( needsConfirmation ) {
       
   966         if ( !mView->isActivated() ) {
       
   967             mPermanentData->clear();
       
   968             return;
       
   969         }
       
   970 
       
   971         HbAction *action;
       
   972         HbMessageBox *dialog = new HbMessageBox( HbMessageBox::MessageTypeQuestion );
       
   973 
       
   974         dialog->setText( message );
       
   975         dialog->setTimeout( HbPopup::NoTimeout );
       
   976         dialog->clearActions();
       
   977         action = new HbAction( hbTrId( "txt_common_button_yes" ) );
       
   978         action->setObjectName( "ok" );
       
   979         dialog->addAction( action );
       
   980         action = new HbAction( hbTrId( "txt_common_button_no" ) );
       
   981         action->setObjectName( "cancel" );
       
   982         dialog->addAction( action );
       
   983         dialog->setAttribute( Qt::WA_DeleteOnClose );
       
   984         setOutstandingPopup( dialog );
       
   985         dialog->open( this, SLOT( handleRequestDelete( HbAction* ) ) );
       
   986     }
       
   987 
       
   988     TX_EXIT
       
   989 }
       
   990 
       
   991 
       
   992 //------------------------------------------------------------------
       
   993 //    MpPopupHandlerPermanentData
       
   994 //------------------------------------------------------------------
       
   995 /*!
       
   996  Constructs the popup handler permanent data.
       
   997  */
       
   998 MpCollectionPopupHandler::MpPopupHandlerPermanentData::MpPopupHandlerPermanentData( QObject *parent )
       
   999     : QObject( parent ),
       
  1000       mIsolatedCollectionData( 0 ),
       
  1001       mAbstractItemModel( 0 ),
       
  1002       mContextMenuIndex( KNullIndex )
       
  1003 {
       
  1004       TX_ENTRY
       
  1005       mSelectedItems.clear();
       
  1006       mOriginalName.clear();
       
  1007       TX_EXIT
       
  1008 }
       
  1009 
       
  1010 /*!
       
  1011  Destructs the popup handler permanent data.
       
  1012  */
       
  1013 MpCollectionPopupHandler::MpPopupHandlerPermanentData::~MpPopupHandlerPermanentData()
       
  1014 {
       
  1015     TX_ENTRY
       
  1016     clear();
       
  1017     TX_EXIT
       
  1018 }
       
  1019 
       
  1020 /*!
       
  1021  \internal
       
  1022   Clears all permanent data. 
       
  1023  */
       
  1024 void MpCollectionPopupHandler::MpPopupHandlerPermanentData::clear()
       
  1025 {
       
  1026     TX_ENTRY
       
  1027     mSelectedItems.clear();
       
  1028     mOriginalName.clear();
       
  1029     mContextMenuIndex = KNullIndex;
       
  1030     if ( mIsolatedCollectionData ) {
       
  1031         MpEngine::instance()->releaseIsolatedCollection();
       
  1032         mIsolatedCollectionData = 0;
       
  1033     }
       
  1034     if ( mAbstractItemModel ) {
       
  1035         delete mAbstractItemModel;
       
  1036         mAbstractItemModel = 0;
       
  1037     }
       
  1038     TX_EXIT
       
  1039 }