ui/viewmanagement/statehandler/src/glxstatemanager.cpp
branchRCL_3
changeset 59 8e5f6eea9c9f
equal deleted inserted replaced
57:ea65f74e6de4 59:8e5f6eea9c9f
       
     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:   ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 //include
       
    19 #include <QApplication>
       
    20 #include <QDebug>
       
    21 #include <QItemSelectionModel>
       
    22 #include <hbnotificationdialog.h>
       
    23 #include <QProcess>
       
    24 #include <hbinstance.h>
       
    25 #include <HbActivityManager.h>
       
    26 #include <hbapplication.h>
       
    27 
       
    28 //user includes
       
    29 #include "glxstatemanager.h"
       
    30 #include "glxviewmanager.h"
       
    31 #include "glxviewids.h"
       
    32 #include "glxmediamodel.h"
       
    33 #include "glxgridstate.h"
       
    34 #include "glxliststate.h"
       
    35 #include "glxfullscreenstate.h"
       
    36 #include "glxdetailstate.h"
       
    37 #include "glxslideshowsettingsstate.h"
       
    38 #include "glxslideshowstate.h"
       
    39 #include "glxtnobserver.h"
       
    40 #include "glxmodelparm.h"
       
    41 #include "glxcollectionpluginall.hrh"
       
    42 #include "glxcollectionplugincamera.hrh"
       
    43 #include "glxcollectionpluginalbums.hrh"
       
    44 #include "glxcollectionpluginimageviewer.hrh"
       
    45 #include "glxexternalutility.h"
       
    46 #include "glxalbummodel.h"
       
    47 #include "glxloggerenabler.h"
       
    48 #include "glxmediaid.h"
       
    49 #include "glxactionhandler.h"
       
    50 #include "glxcommandhandlers.hrh"
       
    51 #include "glxplugincommandid.hrh"
       
    52 #include "glxlog.h"
       
    53 #include "glxtracer.h"
       
    54 #include <glximageviewermanager.h>
       
    55 
       
    56 GlxStateManager::GlxStateManager() 
       
    57     : mAllMediaModel( NULL ), 
       
    58       mAlbumGridMediaModel( NULL ),
       
    59       mAlbumMediaModel( NULL ),
       
    60       mImageviewerMediaModel( NULL ), 
       
    61       mCurrentModel( NULL ), 
       
    62       mCurrentState( NULL ), 
       
    63       mActionHandler( NULL ),
       
    64       mTNObserver ( NULL ),
       
    65       isProgressbarRunning ( false )
       
    66 {
       
    67     qDebug("GlxStateManager::GlxStateManager");
       
    68     PERFORMANCE_ADV ( d1, "view manager creation time") {
       
    69         mViewManager = new GlxViewManager();
       
    70     }
       
    71     mTNObserver = new GlxTNObserver();
       
    72     
       
    73     connect ( this, SIGNAL( setupItemsSignal() ), this, SLOT( setupItems() ), Qt::QueuedConnection );
       
    74     connect ( mViewManager, SIGNAL(actionTriggered( qint32 )), this, SLOT(actionTriggered( qint32 )), Qt::QueuedConnection );
       
    75     connect ( mTNObserver, SIGNAL( leftTNCount( int ) ), this, SLOT( updateTNProgress( int ) ) );
       
    76     connect ( qobject_cast<HbApplication*>(qApp), SIGNAL (aboutToQuit()), this, SLOT(saveData()));
       
    77 	//TO:DO TBD through exception when it is null
       
    78 }
       
    79 
       
    80 void GlxStateManager::enterMarkingMode()
       
    81 {
       
    82     mViewManager->enterMarkingMode(mCurrentState->id());
       
    83 }
       
    84 
       
    85 void GlxStateManager::exitMarkingMode()
       
    86 {
       
    87     mViewManager->exitMarkingMode(mCurrentState->id());
       
    88 }
       
    89 
       
    90 bool GlxStateManager::executeCommand(qint32 commandId)
       
    91 {
       
    92     qDebug( "GlxStateManager::executeCommand() : command ID %d", commandId);
       
    93     QItemSelectionModel * selectionModel = mViewManager->getSelectionModel(mCurrentState->id());
       
    94     if ( selectionModel == NULL )
       
    95         return FALSE;
       
    96     
       
    97     QModelIndexList indexList = selectionModel->selectedIndexes();
       
    98     //if no item is selected then do nothing
       
    99     if (indexList.count() == 0) 
       
   100         return FALSE;
       
   101     
       
   102     for ( int i = 0; i < indexList.count(); i++) {
       
   103         qDebug( "GlxGridView::doneAction() : index %d", indexList[i].row());            
       
   104         mCurrentModel->setData( indexList[i], indexList[i].row(), GlxSelectedIndexRole );
       
   105     }
       
   106     mActionHandler->handleAction(commandId, mCollectionId,indexList);
       
   107     return TRUE;
       
   108 }
       
   109 
       
   110 bool GlxStateManager::eventFilter(QObject *obj, QEvent *event)
       
   111 {
       
   112     TRACER("GlxStateManager::eventFilter() ");
       
   113     GLX_LOG_INFO1("GlxStateManager::eventFilter() %d event type", event->type());
       
   114     
       
   115     if ( event->type() ==  QEvent::ApplicationActivate ) {
       
   116         mTNObserver->startTNMDaemon();
       
   117     }
       
   118     if ( event->type() ==  QEvent::ApplicationDeactivate ) {
       
   119         mTNObserver->stopTNMDaemon();
       
   120     }
       
   121     return QObject::eventFilter(obj, event);
       
   122 }
       
   123 
       
   124 void GlxStateManager::launchFetcher()
       
   125 {
       
   126     qDebug("GlxStateManager::launchFetcher");
       
   127     mCurrentState = createState(GLX_GRIDVIEW_ID);
       
   128     mCurrentState->setState(FETCHER_ITEM_S);
       
   129     
       
   130     createModel(GLX_GRIDVIEW_ID);
       
   131     mViewManager->launchApplication( GLX_GRIDVIEW_ID, mCurrentModel);
       
   132 }
       
   133 
       
   134 void GlxStateManager::launchApplication()
       
   135 {
       
   136     qDebug("GlxStateManager::launchApplication");   
       
   137     bool activitySuccess = false;  
       
   138     //To:Do use it in future once performance code is removed nextState(GLX_GRIDVIEW_ID, ALL_ITEM_S)
       
   139     HbApplication* app = qobject_cast<HbApplication*>(qApp);
       
   140     if(app->activateReason() == Hb::ActivationReasonActivity) {
       
   141         activitySuccess = launchActivity();
       
   142     }
       
   143     
       
   144     if( !activitySuccess ) { 
       
   145         mCurrentState = createState( GLX_GRIDVIEW_ID );
       
   146         mCurrentState->setState( ALL_ITEM_S );
       
   147        
       
   148         int leftCount = mTNObserver->getTNLeftCount() ;
       
   149         if (  leftCount > 0  ) {
       
   150             mViewManager->launchApplication( GLX_GRIDVIEW_ID, mCurrentModel );
       
   151             launchProgressDialog();
       
   152         }
       
   153         else {
       
   154             createModel( GLX_GRIDVIEW_ID );
       
   155             mViewManager->launchApplication( GLX_GRIDVIEW_ID, mCurrentModel );
       
   156         }    
       
   157         mTNObserver->startTNObserving() ; 
       
   158     }
       
   159     
       
   160     HbActivityManager* activityManager = app->activityManager();
       
   161     bool ok = activityManager->removeActivity("PhotosMainView");
       
   162     if ( !ok )
       
   163     {
       
   164          qDebug("launchapplication::Remove activity failed" );
       
   165     } 
       
   166 }
       
   167 
       
   168 bool GlxStateManager::launchActivity()
       
   169 {
       
   170     HbApplication* app = qobject_cast<HbApplication*>(qApp);
       
   171     bool ok = app->activityManager()->waitActivity();
       
   172     if ( !ok )
       
   173     {
       
   174         qDebug("subscribing to activity manager failed" );
       
   175     }
       
   176     QVariant data = app->activityManager()->activityData( "PhotosMainView" );
       
   177     QByteArray serializedModel = data.toByteArray();
       
   178     QDataStream stream(&serializedModel, QIODevice::ReadOnly);
       
   179     
       
   180     //Fetch the data from the activity Manager
       
   181     stream >> mSaveActivity;  
       
   182     qint32 stateId = mSaveActivity.value("ID");
       
   183     mCurrentState = createState(stateId);
       
   184     mCurrentState->setState( mSaveActivity.value("InternalState") );
       
   185     createModel( stateId);
       
   186 	/*Model might not be populated yet to set the visibleWindowIndex right away.
       
   187 	*So, let us store the visible index as a temporary Variable, so that  visible Window Index 
       
   188 	*is set once the model is populated.
       
   189     */
       
   190     mCurrentModel->setData(QModelIndex(), mSaveActivity.value("VisibleIndex") , GlxTempVisualWindowIndex );
       
   191     mViewManager->launchApplication(stateId, mCurrentModel); 
       
   192     return true;
       
   193 }
       
   194 
       
   195 void GlxStateManager::launchFromExternal()
       
   196 {
       
   197     qDebug("GlxStateManager::launchFromExternal");
       
   198     mCurrentState = createState(GLX_FULLSCREENVIEW_ID);
       
   199     mCurrentState->setState(IMAGEVIEWER_S);
       
   200     
       
   201     createModel(GLX_FULLSCREENVIEW_ID);
       
   202     mCurrentModel->setData( mCurrentModel->index(0,0), 0, GlxFocusIndexRole );
       
   203     mViewManager->launchApplication( GLX_FULLSCREENVIEW_ID, mCurrentModel);
       
   204 	setupItems();
       
   205 }
       
   206 
       
   207 void GlxStateManager::actionTriggered(qint32 id)
       
   208 {
       
   209     qDebug("GlxStateManager::actionTriggered action ID = %d", id);
       
   210     mCurrentState->eventHandler(id);
       
   211     if (id != EGlxCmdHandled ) {
       
   212         eventHandler(id);
       
   213     }
       
   214 }
       
   215 
       
   216 void GlxStateManager::setupItems()
       
   217 {    
       
   218     qDebug("GlxStateManager::setupItems()");
       
   219     mActionHandler = new GlxActionHandler();
       
   220     connect ( mViewManager, SIGNAL(externalCommand(int )), this, SIGNAL(externalCommand(int )) );
       
   221     mViewManager->setupItems();
       
   222     switch( mSaveActivity.value( "ID" ) ){
       
   223         case GLX_LISTVIEW_ID:
       
   224             mViewManager->updateToolBarIcon(GLX_ALBUM_ACTION_ID);
       
   225             break;
       
   226             
       
   227         case GLX_GRIDVIEW_ID:
       
   228         default:
       
   229             mViewManager->updateToolBarIcon(GLX_ALL_ACTION_ID);
       
   230     }
       
   231 }
       
   232 
       
   233 void GlxStateManager::updateTNProgress( int count)
       
   234 {    
       
   235     TRACER("GlxStateManager::updateTNProgress() ");
       
   236     // this is case when progress bar is not showing
       
   237     // in the case of rename of an image or capture the single item
       
   238     // it is also launching the progress bar, to avoid this scenario add the check of count more than 5
       
   239     if ( mCurrentModel && ( count > 5  ) ) { 
       
   240          goBack( GLX_GRIDVIEW_ID, ALL_ITEM_S ) ;
       
   241          cleanAllModel();
       
   242          launchProgressDialog();
       
   243     }
       
   244     
       
   245     if ( isProgressbarRunning ){
       
   246         if ( count == 0 ) {
       
   247             vanishProgressDialog();
       
   248         }
       
   249         else {
       
   250             mViewManager->updateProgressDialog( count );
       
   251         }
       
   252     }   
       
   253 }
       
   254 
       
   255 void GlxStateManager::thumbnailPopulated()
       
   256 {
       
   257     mViewManager->setModel( mCurrentModel );
       
   258     isProgressbarRunning = false;
       
   259     mViewManager->updateProgressDialog( 0 );  
       
   260     disconnect ( mCurrentModel, SIGNAL( thumbnailPopulated() ), this, SLOT( thumbnailPopulated() ) );
       
   261 }
       
   262 
       
   263 void GlxStateManager::saveData()
       
   264 {
       
   265     if( (mCurrentState->id() == GLX_GRIDVIEW_ID && mCurrentState->state() == ALL_ITEM_S) || mCurrentState->id() == GLX_LISTVIEW_ID ) {
       
   266         mSaveActivity.insert("ID",mCurrentState->id()); 
       
   267         mSaveActivity.insert("InternalState",mCurrentState->state());
       
   268         if(mCurrentModel) {
       
   269             QVariant variant = mCurrentModel->data( mCurrentModel->index(0,0), GlxVisualWindowIndex );
       
   270             if ( variant.isValid() &&  variant.canConvert<int> () ) {
       
   271                 mSaveActivity.insert("VisibleIndex",variant.value<int>());
       
   272             }
       
   273  		}
       
   274  		else {
       
   275  			mSaveActivity.insert("VisibleIndex",0);
       
   276  		}
       
   277     
       
   278         HbActivityManager* activityManager = qobject_cast<HbApplication*>(qApp)->activityManager();
       
   279         QVariantHash metadata;
       
   280         HbMainWindow *window = hbInstance->allMainWindows().first();
       
   281         metadata.insert("screenshot", QPixmap::grabWidget(window, window->rect()));
       
   282         
       
   283         QByteArray serializedModel;
       
   284         QDataStream stream(&serializedModel, QIODevice::WriteOnly | QIODevice::Append);
       
   285         stream << mSaveActivity;
       
   286         
       
   287         bool ok = activityManager->addActivity("PhotosMainView", serializedModel, metadata);
       
   288         if ( !ok )
       
   289         {
       
   290             qDebug("SaveData::Add activity failed" );
       
   291         }
       
   292     }
       
   293 }
       
   294 
       
   295 void GlxStateManager::nextState(qint32 state, int internalState)
       
   296 {
       
   297     qDebug("GlxStateManager::nextState next state = %u", state);
       
   298     GlxEffect effect = NO_EFFECT ;
       
   299     GlxViewEffect viewEffect = NO_VIEW ;
       
   300     
       
   301     PERFORMANCE ( d1, State Creation Time ) {
       
   302         mCurrentState = createState(state); //create a new state
       
   303         mCurrentState->setState(internalState); 
       
   304     }
       
   305     
       
   306     createModel(state); //model should created after the internal state was set.
       
   307     
       
   308     PERFORMANCE ( d2, view launch time ) {
       
   309         mCurrentState->setTranstionParameter( FORWARD_DIR, effect, viewEffect); // to run the transtion effect
       
   310         if ( viewEffect == NO_VIEW ) {
       
   311             mViewManager->launchView( state, mCurrentModel );
       
   312         }
       
   313         else {
       
   314             mViewManager->launchView( state, mCurrentModel, effect, viewEffect);
       
   315         }
       
   316     }
       
   317 }
       
   318 
       
   319 void GlxStateManager::previousState()
       
   320 {
       
   321     qDebug("GlxStateManager::previousState");
       
   322     GlxEffect effect = NO_EFFECT ;
       
   323     GlxViewEffect viewEffect = NO_VIEW ;
       
   324 
       
   325     GlxState *state = mCurrentState; // To delete the current state later  
       
   326 
       
   327     if ( mCurrentState->previousState() == NULL ) { //In the case only one state in stack then exit from the application
       
   328         saveImage(); // save image if it is in private folder
       
   329         exitApplication() ;
       
   330         return ;
       
   331     }
       
   332     else {
       
   333         //effect parameter should be set here other wise there is no way to know from where it come from
       
   334         mCurrentState->setTranstionParameter( BACKWARD_DIR, effect, viewEffect);
       
   335         mCurrentState = mCurrentState->previousState(); //get the pervious state
       
   336         createModel( mCurrentState->id(), BACKWARD_DIR ); 
       
   337 
       
   338         if ( viewEffect == NO_VIEW ) {
       
   339             mViewManager->launchView( mCurrentState->id(), mCurrentModel );
       
   340         }
       
   341         else {
       
   342             mViewManager->launchView( mCurrentState->id(), mCurrentModel, effect, viewEffect);
       
   343         }
       
   344     }
       
   345     delete state;
       
   346 }
       
   347 
       
   348 void GlxStateManager::saveImage()
       
   349     {
       
   350     CGlxImageViewerManager *imageViewerInstance = CGlxImageViewerManager::InstanceL();
       
   351     if(imageViewerInstance->IsPrivate())
       
   352         {
       
   353         HBufC* imagePath = imageViewerInstance->ImageUri();
       
   354 		QString srcPath = QString::fromUtf16(imagePath->Des().Ptr(),imagePath->Length());
       
   355 		QString imageName = srcPath.section('\\', -1);
       
   356 		QString imagesFolderPath("c:/data/images/");
       
   357 		QString destPath;
       
   358 		destPath.append(imagesFolderPath);
       
   359 		destPath.append(imageName);
       
   360 		int cnt = 1;
       
   361 		qDebug() << "GlxStateManager::saveImage path before while = "<< destPath;
       
   362 		while(!QFile::copy(srcPath,destPath))
       
   363 			{
       
   364 			QString filename = imageName.section('.', 0,0);
       
   365 			QString ext = imageName.section('.', -1);
       
   366 			destPath.clear();
       
   367 			destPath = imagesFolderPath + filename + QString::number(cnt) + "." + ext;
       
   368 			qDebug() << "GlxStateManager::saveImage path = "<< destPath;
       
   369 			cnt++;
       
   370 			}
       
   371         }
       
   372     imageViewerInstance->Close();
       
   373     }
       
   374 	
       
   375 void GlxStateManager::goBack(qint32 stateId, int internalState)
       
   376 {
       
   377     qDebug("GlxStateManager::goBack()");
       
   378     
       
   379     //if current state and it internal state is same then no need to do any thing
       
   380     if ( mCurrentState->id() == stateId  && mCurrentState->state() == internalState ) {
       
   381         return ;
       
   382     }
       
   383     
       
   384     GlxState *state = mCurrentState;
       
   385     
       
   386     while ( mCurrentState ) {
       
   387         if ( mCurrentState->id() == stateId  && mCurrentState->state() == internalState ) {
       
   388             break ;           
       
   389         }
       
   390         
       
   391         mCurrentState = mCurrentState->previousState(); // set pervious state to the current state
       
   392         delete state; //delete the current state
       
   393         state = mCurrentState;    
       
   394     }
       
   395     
       
   396     //case when new state is not hierarchy then create a new state
       
   397     if ( mCurrentState == NULL ) {
       
   398         mCurrentState = createState(stateId);
       
   399         mCurrentState->setState( internalState );
       
   400     }   
       
   401     
       
   402     qDebug("GlxStateManager::goBack() state %u", mCurrentState);
       
   403     createModel( mCurrentState->id() ); //model should created after the internal state was set.
       
   404     mViewManager->launchView( mCurrentState->id(), mCurrentModel );   
       
   405 }
       
   406 
       
   407 void GlxStateManager::changeState(qint32 stateId, int internalState)
       
   408 {
       
   409     qDebug("GlxStateManager::changeState %d", stateId);	
       
   410     
       
   411     GlxState *state = mCurrentState;
       
   412     GlxEffect effect = NO_EFFECT ;
       
   413     GlxViewEffect viewEffect = NO_VIEW ;
       
   414 
       
   415     mCurrentState = mCurrentState->previousState();//pervious state of current state become the pervious state of new state 
       
   416     
       
   417     mCurrentState = createState(stateId);
       
   418     mCurrentState->setState( internalState );
       
   419     createModel( stateId ); //model should created after the internal state was set.
       
   420     
       
   421     mCurrentState->setTranstionParameter( NO_DIR, effect, viewEffect); //set the transition parameter    
       
   422     if ( viewEffect == NO_VIEW ) {
       
   423         mViewManager->launchView( mCurrentState->id(), mCurrentModel );
       
   424     }
       
   425     else {
       
   426         mViewManager->launchView( mCurrentState->id(), mCurrentModel, effect, viewEffect);
       
   427     }
       
   428     delete state;
       
   429 }
       
   430 
       
   431 void GlxStateManager::removeCurrentModel()
       
   432 {
       
   433     TRACER("GlxStateManager::removeCurrentModel() ");
       
   434     if ( mCurrentModel == mAllMediaModel ) {
       
   435         qDebug("GlxStateManager::removeCurrentModel() remove all media model");	
       
   436         delete mAllMediaModel ;
       
   437         mAllMediaModel = NULL ;
       
   438         mCurrentModel = NULL ;
       
   439     }
       
   440     else if ( mCurrentModel == mAlbumGridMediaModel ) {
       
   441         qDebug("GlxStateManager::removeCurrentModel() remove album grid media model");	
       
   442         delete mAlbumGridMediaModel ;
       
   443         mAlbumGridMediaModel = NULL ;
       
   444         mCurrentModel = NULL ;
       
   445     }
       
   446     else if ( mCurrentModel == mAlbumMediaModel ) {
       
   447         qDebug("GlxStateManager::removeCurrentModel() remove  model");	
       
   448         delete mAlbumMediaModel ;
       
   449         mAlbumMediaModel = NULL ;
       
   450         mCurrentModel = NULL ;
       
   451     }
       
   452     else {
       
   453     	qDebug("GlxStateManager::removeCurrentModel() do nothing");
       
   454     	//do nothing
       
   455     }
       
   456 }
       
   457 
       
   458 void GlxStateManager::cleanAllModel()
       
   459 {
       
   460     TRACER("GlxStateManager::cleanAllModel() ");
       
   461     delete mAllMediaModel ;
       
   462     mAllMediaModel = NULL ;
       
   463     delete mAlbumMediaModel ; 
       
   464     mAlbumMediaModel = NULL ;
       
   465     delete mAlbumGridMediaModel ;
       
   466     mAlbumGridMediaModel = NULL ;
       
   467     delete mImageviewerMediaModel ;
       
   468     mImageviewerMediaModel = NULL ;
       
   469     mCurrentModel = NULL ;
       
   470 }
       
   471 
       
   472 void GlxStateManager::launchProgressDialog()
       
   473 {
       
   474     TRACER("GlxStateManager::launchProgressDialog() ");
       
   475     
       
   476     QCoreApplication::instance()->installEventFilter( this );
       
   477     if ( isProgressbarRunning ) {
       
   478         mViewManager->updateProgressDialog( mTNObserver->getTNLeftCount() );
       
   479     }
       
   480     else {
       
   481         mViewManager->launchProgressDialog( mTNObserver->getTNLeftCount() );
       
   482     }
       
   483     isProgressbarRunning = true ;
       
   484 }
       
   485 
       
   486 void GlxStateManager::vanishProgressDialog()
       
   487 {
       
   488     TRACER("GlxStateManager::vanishProgressDialog() ");
       
   489     QCoreApplication::instance()->removeEventFilter( this );
       
   490     createModel( mCurrentState->id() );
       
   491     connect ( mCurrentModel, SIGNAL( thumbnailPopulated() ), this, SLOT( thumbnailPopulated() ) );
       
   492 }
       
   493 
       
   494 GlxState * GlxStateManager::createState(qint32 stateId)
       
   495 {
       
   496     qDebug("GlxStateManager::createState state id = %d", stateId);
       
   497     
       
   498     switch( stateId ) {    
       
   499     case GLX_GRIDVIEW_ID :
       
   500         return new GlxGridState( this, mCurrentState );
       
   501         
       
   502     case GLX_LISTVIEW_ID :
       
   503         return new GlxListState( mCurrentState );    	
       
   504     	
       
   505     case GLX_FULLSCREENVIEW_ID :
       
   506         return new GlxFullScreenState( this, mCurrentState );
       
   507         
       
   508     case GLX_DETAILSVIEW_ID:
       
   509         return new GlxDetailState( mCurrentState );
       
   510         
       
   511     case GLX_SLIDESHOWVIEW_ID :
       
   512         return new GlxSlideShowState( this, mCurrentState );
       
   513     case GLX_SLIDESHOWSETTINGSVIEW_ID :
       
   514         return new GlxSlideShowSettingsState(this, mCurrentState );
       
   515     	
       
   516     default :
       
   517         return NULL;		
       
   518     } 
       
   519 }
       
   520 
       
   521 void GlxStateManager::createModel(qint32 stateId, NavigationDir dir)
       
   522 {
       
   523     switch( stateId ) {
       
   524     case GLX_GRIDVIEW_ID :
       
   525         createGridModel( mCurrentState->state(), dir );
       
   526         break;
       
   527 
       
   528     case GLX_LISTVIEW_ID :
       
   529     	//To:Do change the code when integrate the album list model
       
   530         if (mAlbumMediaModel == NULL) {
       
   531             GlxModelParm modelParm (KGlxCollectionPluginAlbumsImplementationUid, 0);
       
   532             mAlbumMediaModel = new GlxAlbumModel (modelParm);
       
   533         }
       
   534         mCurrentModel = mAlbumMediaModel;
       
   535         mCollectionId = KGlxCollectionPluginAlbumsImplementationUid;
       
   536         mCurrentModel->setData(QModelIndex(), (int)GlxContextPtList, GlxContextRole );
       
   537         mViewManager->updateToolBarIcon(GLX_ALBUM_ACTION_ID);
       
   538         break;
       
   539         
       
   540     case GLX_FULLSCREENVIEW_ID :
       
   541 		if ( mCurrentState->state() == IMAGEVIEWER_S) {
       
   542             GlxModelParm modelParm (KGlxCollectionPluginImageViewerImplementationUid, 0);
       
   543             mCurrentModel = mImageviewerMediaModel = new GlxMediaModel (modelParm);
       
   544             mCollectionId = KGlxCollectionPluginImageViewerImplementationUid;
       
   545 		}
       
   546         else if ( mCurrentState->state() == EXTERNAL_S) {
       
   547             if(!mAllMediaModel) {
       
   548                 GlxModelParm modelParm (KGlxCollectionPluginAllImplementationUid, 0);
       
   549                 mCurrentModel = mAllMediaModel = new GlxMediaModel (modelParm);
       
   550             }
       
   551             else {
       
   552                 mCurrentModel = mAllMediaModel;
       
   553             }
       
   554             mCollectionId = KGlxCollectionPluginAllImplementationUid;
       
   555         }
       
   556 		setFullScreenContext();
       
   557         break;
       
   558         
       
   559     case GLX_DETAILSVIEW_ID :
       
   560         setFullScreenContext();
       
   561         break;
       
   562         
       
   563     case GLX_SLIDESHOWVIEW_ID :
       
   564         if ( mCurrentState->state() == SLIDESHOW_ALBUM_ITEM_S ) {
       
   565             GlxModelParm modelParm ( KGlxAlbumsMediaId , 0);
       
   566             mCurrentModel = mAlbumGridMediaModel = new GlxMediaModel( modelParm );
       
   567         }
       
   568         setFullScreenContext();
       
   569         break;
       
   570     	    	
       
   571     default :
       
   572         break;
       
   573     }
       
   574     mCurrentModel->setData( QModelIndex(), mCurrentState->state(), GlxSubStateRole );
       
   575 }
       
   576 
       
   577 void GlxStateManager::createGridModel(int internalState, NavigationDir dir)
       
   578 {
       
   579     GlxModelParm modelParm;
       
   580     GlxContextMode mode;
       
   581     
       
   582     if ( mViewManager->orientation() == Qt::Horizontal ) {
       
   583         mode = GlxContextLsGrid ;
       
   584     }
       
   585     else {
       
   586         mode = GlxContextPtGrid ;
       
   587     }
       
   588     
       
   589     switch( internalState) {
       
   590     case ALL_ITEM_S :
       
   591 	case FETCHER_ITEM_S:
       
   592         if ( mAllMediaModel == NULL ) {
       
   593             modelParm.setCollection( KGlxCollectionPluginAllImplementationUid );
       
   594             modelParm.setDepth(0);
       
   595             modelParm.setContextMode( mode ) ;
       
   596             mAllMediaModel = new GlxMediaModel( modelParm );
       
   597         }
       
   598         mCollectionId = KGlxCollectionPluginAllImplementationUid;
       
   599         mCurrentModel = mAllMediaModel;
       
   600         mViewManager->updateToolBarIcon(GLX_ALL_ACTION_ID);
       
   601         break;
       
   602         
       
   603     case ALBUM_ITEM_S :
       
   604     case FETCHER_ALBUM_ITEM_S :    
       
   605         if ( dir != BACKWARD_DIR ) { 
       
   606             modelParm.setCollection( KGlxAlbumsMediaId );
       
   607             modelParm.setDepth(0);
       
   608             modelParm.setContextMode( mode ) ;
       
   609             mAlbumGridMediaModel = new GlxMediaModel( modelParm );
       
   610         }               
       
   611         mCollectionId = KGlxAlbumsMediaId;
       
   612         mCurrentModel = mAlbumGridMediaModel ;
       
   613         mViewManager->updateToolBarIcon(NO_ACTION_ID);
       
   614         break;
       
   615     
       
   616     default :
       
   617         break;
       
   618     }
       
   619     
       
   620     mCurrentModel->setData(QModelIndex(), (int)mode, GlxContextRole );
       
   621 }
       
   622 
       
   623 void GlxStateManager::setFullScreenContext()
       
   624 {
       
   625     if ( mViewManager->orientation() == Qt::Horizontal || mCurrentState->id() == GLX_SLIDESHOWVIEW_ID ) {
       
   626         mCurrentModel->setData(QModelIndex(), (int)GlxContextLsFs, GlxContextRole );
       
   627     }
       
   628     else {
       
   629         mCurrentModel->setData(QModelIndex(), (int)GlxContextPtFs, GlxContextRole );
       
   630     }    
       
   631 }
       
   632 
       
   633 void GlxStateManager::eventHandler(qint32 &id)
       
   634 {
       
   635     qDebug("GlxStateManager::eventHandler action id = %d", id);
       
   636     switch(id) {
       
   637     case EGlxCmdAllGridOpen :
       
   638         changeState( GLX_GRIDVIEW_ID, ALL_ITEM_S );
       
   639         id = EGlxCmdHandled;
       
   640         break;
       
   641 
       
   642     case EGlxCmdFetcherAllGridOpen :
       
   643         changeState( GLX_GRIDVIEW_ID, FETCHER_ITEM_S );
       
   644         id = EGlxCmdHandled;
       
   645         break;
       
   646     	
       
   647     case EGlxCmdAlbumListOpen:
       
   648         changeState( GLX_LISTVIEW_ID, -1 );
       
   649         id = EGlxCmdHandled;
       
   650         break;
       
   651     case EGlxCmdFetcherAlbumListOpen:
       
   652         changeState( GLX_LISTVIEW_ID, FETCHER_ALBUM_S );
       
   653         id = EGlxCmdHandled;
       
   654         break;
       
   655         
       
   656     case EGlxCmdAlbumGridOpen:
       
   657         nextState( GLX_GRIDVIEW_ID, ALBUM_ITEM_S );
       
   658         id = EGlxCmdHandled;
       
   659         break;
       
   660     case EGlxCmdFetcherAlbumGridOpen:
       
   661         nextState( GLX_GRIDVIEW_ID, FETCHER_ALBUM_ITEM_S );
       
   662         id = EGlxCmdHandled;
       
   663         break;
       
   664         
       
   665     case EGlxCmdFirstSlideshow :
       
   666         //play the slide show with first item
       
   667         mCurrentModel->setData( mCurrentModel->index(0, 0), 0, GlxFocusIndexRole );
       
   668         nextState( GLX_SLIDESHOWVIEW_ID, SLIDESHOW_GRID_ITEM_S );
       
   669         id = EGlxCmdHandled;
       
   670         break;
       
   671         
       
   672     case EGlxCmdSelectSlideshow:
       
   673         //play the slide show with selected item
       
   674         nextState( GLX_SLIDESHOWVIEW_ID, SLIDESHOW_GRID_ITEM_S );
       
   675         id = EGlxCmdHandled;
       
   676         break;
       
   677         
       
   678     case EGlxCmdAlbumSlideShow:
       
   679         //play the slide show for a album
       
   680         nextState( GLX_SLIDESHOWVIEW_ID, SLIDESHOW_ALBUM_ITEM_S );
       
   681         id = EGlxCmdHandled;
       
   682         break;
       
   683         
       
   684     case EGlxCmdSlideshowSettings:
       
   685         nextState(GLX_SLIDESHOWSETTINGSVIEW_ID,-1 );
       
   686         id = EGlxCmdHandled;
       
   687         break;
       
   688         
       
   689     case EGlxCmdDetailsOpen:
       
   690         qDebug("GlxStateManager::eventHandler EGlxCmdDetailsOpen");
       
   691         nextState( GLX_DETAILSVIEW_ID, -1 );
       
   692         id = EGlxCmdHandled;
       
   693         break;
       
   694         
       
   695     case EGlxCmdEmptyData : {
       
   696         GlxState *tmpState = mCurrentState ;
       
   697         while ( tmpState->id() != GLX_GRIDVIEW_ID ) {
       
   698             tmpState = tmpState->previousState() ;
       
   699         }
       
   700         
       
   701         goBack( GLX_GRIDVIEW_ID, tmpState->state() );
       
   702         id = EGlxCmdHandled;
       
   703         break;
       
   704     }
       
   705     	
       
   706     case EGlxCmdBack :
       
   707     case EGlxCmdSlideShowBack :
       
   708     	previousState();
       
   709     	id = EGlxCmdHandled;
       
   710     	break;
       
   711     	
       
   712     case EGlxCmdCameraOpen:
       
   713 		{
       
   714 		QProcess::startDetached(QString("cxui.exe"));
       
   715 		id = EGlxCmdHandled;
       
   716 		}
       
   717         break;	
       
   718         
       
   719     case EGlxCmdOviOpen:
       
   720 		{
       
   721 		HbNotificationDialog::launchDialog("Not Implemented");
       
   722         id = EGlxCmdHandled;
       
   723 		}
       
   724         break;	
       
   725     
       
   726    case EGlxCmdDelete :
       
   727 		{
       
   728         GlxExternalData* externalItems = GlxExternalUtility::instance()->getExternalData();
       
   729         if(externalItems){
       
   730             QVariant variant = mCurrentModel->data( mCurrentModel->index(0,0), GlxFocusIndexRole );    
       
   731             if ( variant.isValid() &&  variant.canConvert<int> () ) {
       
   732                 int selIndex = variant.value<int>(); 
       
   733                 int externalDataCount = externalItems->count();
       
   734                 if(selIndex < externalDataCount){
       
   735                     GlxInterfaceParams tmp = externalItems->value(selIndex);
       
   736                     tmp.isSelected = true;
       
   737                     externalItems->replace(selIndex,tmp);
       
   738                     emit externalCommand(EGlxPluginCmdDelete);
       
   739                     return;
       
   740                 }
       
   741             }
       
   742         }
       
   743         mActionHandler->handleAction(id,mCollectionId);
       
   744 		}
       
   745         break;
       
   746         
       
   747    case EGlxCmdMarkAll :
       
   748    case EGlxCmdUnMarkAll :
       
   749    case EGlxCmd3DEffectOn :
       
   750    case EGlxCmd3DEffectOff :
       
   751    case EGlxCmdPlayBackAnim :
       
   752        mViewManager->handleUserAction( mCurrentState->id(), id );
       
   753        id = EGlxCmdHandled;
       
   754        break;
       
   755        
       
   756    case EGlxCmdSetupItem :
       
   757        emit setupItemsSignal();
       
   758        break;
       
   759    
       
   760    case EGlxCmdFetcherSelect: 
       
   761        {
       
   762        QModelIndex selectedIndex = mCurrentModel->index(mCurrentModel->data(mCurrentModel->index(0,0),GlxFocusIndexRole).value<int>(),0);
       
   763        emit gridItemSelected(selectedIndex,*mCurrentModel);
       
   764        id = EGlxCmdHandled;
       
   765        }
       
   766        break;
       
   767        
       
   768    default :
       
   769         mActionHandler->handleAction(id,mCollectionId);
       
   770     	break;
       
   771     }	
       
   772 }
       
   773 
       
   774 void GlxStateManager::exitApplication()
       
   775 {
       
   776     
       
   777     
       
   778     //To:Do memory cleanup
       
   779         QApplication::quit();
       
   780      
       
   781 }
       
   782 
       
   783 GlxStateManager::~GlxStateManager()
       
   784 {
       
   785     qDebug("GlxStateManager::~GlxStateManager");
       
   786     cleanAllModel();
       
   787     mSaveActivity.clear();
       
   788     delete mActionHandler;
       
   789     qDebug("GlxStateManager::~GlxStateManager delete Model");
       
   790     
       
   791     disconnect ( mViewManager, SIGNAL(actionTriggered(qint32 )), this, SLOT(actionTriggered(qint32 )) );
       
   792     disconnect ( mViewManager, SIGNAL(externalCommand(int )), this, SIGNAL(externalCommand(int )) );
       
   793     disconnect ( mTNObserver, SIGNAL( leftTNCount( int ) ), this, SLOT( updateTNProgress( int ) ) );
       
   794     disconnect ( this, SIGNAL( setupItemsSignal() ), this, SLOT( setupItems() ) );
       
   795     disconnect ( qobject_cast<HbApplication*>(qApp), SIGNAL (aboutToQuit()), this, SLOT(saveData()));
       
   796     
       
   797     delete mTNObserver;
       
   798     delete mViewManager; 
       
   799     qDebug("GlxStateManager::~GlxStateManager delete view manager");
       
   800     
       
   801     GlxState *tmp;   
       
   802     while (mCurrentState) {
       
   803         tmp = mCurrentState;
       
   804         mCurrentState = mCurrentState->previousState();
       
   805         delete tmp;        
       
   806     }
       
   807     
       
   808     qDebug("GlxStateManager::~GlxStateManager Exit");
       
   809 }
       
   810 
       
   811 void GlxStateManager::cleanupExternal()
       
   812 {
       
   813     qDebug("GlxStateManager::cleanupExternal");
       
   814     mViewManager->deactivateCurrentView();
       
   815     GlxMediaModel *glxModel = dynamic_cast<GlxMediaModel *>(mCurrentModel);
       
   816     if(glxModel) {
       
   817 		glxModel->clearExternalItems();  
       
   818 	}
       
   819 }