mapnavproviderrefapp/src/mnrpappui.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  CMnrpAppUi class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <aknnavi.h>
       
    20 #include <aknnavide.h>
       
    21 #include <eikbtgpc.h>
       
    22 
       
    23 #include <AknIconUtils.h>
       
    24 #include <aknselectionlist.h>
       
    25 #include <gulicon.h>
       
    26 
       
    27 #include <mnrefprovider.rsg>
       
    28 #include <MnRefProvider.mbg>
       
    29 #include "mnrp.hrh"
       
    30 
       
    31 #include <EPos_CPosLandmark.h>
       
    32 #include <EPos_CPosLandmarkDatabase.h>
       
    33 
       
    34 #include "mnrpappview.h"
       
    35 #include "mnrpnaviview.h"
       
    36 #include "mnrpmapview.h"
       
    37 
       
    38 #include "mnrpmapviewservice.h"
       
    39 #include "mnrpnavigationservice.h"
       
    40 
       
    41 #include "mnrpdocument.h"
       
    42 #include "mnrpengine.h"
       
    43 #include "mnrpappui.h"
       
    44 #include "mnrputils.h"
       
    45 #include "debug.h"
       
    46 
       
    47 const TInt KDefaultLmIconIndex = 0;
       
    48 _LIT( KMbmFile, "\\resource\\apps\\MnRefProvider.mbm");
       
    49 
       
    50 // ============================ LOCAL FUNCTIONS ===============================
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 void CleanupIconArray( TAny* aArray )
       
    56     {
       
    57     LOG("MnRefProvider::CleanupIconArray in");
       
    58     ( ( CArrayPtrSeg<CGulIcon>* ) aArray)->ResetAndDestroy();
       
    59     delete aArray;
       
    60     LOG("MnRefProvider::CleanupIconArray out");
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 void AddIconL(
       
    67     CArrayPtr<CGulIcon>& aIconArray,
       
    68     const TDesC& aFileName,
       
    69     TInt aIconIndex,
       
    70     TInt aMaskIndex )
       
    71     {
       
    72     LOG("MnRefProvider::AddIconL in");
       
    73     CFbsBitmap* icon = NULL;
       
    74     CFbsBitmap* mask = NULL;
       
    75 
       
    76     AknIconUtils::CreateIconLC( icon, mask, aFileName, aIconIndex, aMaskIndex );
       
    77     CGulIcon* gul = CGulIcon::NewL( icon, mask );
       
    78 
       
    79     gul->SetBitmapsOwnedExternally( EFalse );
       
    80     CleanupStack::PushL( gul );
       
    81     aIconArray.AppendL( gul );
       
    82     CleanupStack::Pop( 3 ); // gul, icon & mask
       
    83     LOG("MnRefProvider::AddIconL out");
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 CArrayPtrSeg<CGulIcon>* CreateIconListLC( TInt aGranularity )
       
    90     {
       
    91     CArrayPtrSeg<CGulIcon>* iconList =
       
    92         new (ELeave) CArrayPtrSeg<CGulIcon>( aGranularity );
       
    93     CleanupStack::PushL( TCleanupItem( CleanupIconArray, iconList ) );
       
    94 
       
    95     AddIconL(
       
    96         *iconList,
       
    97         KMbmFile,
       
    98         EMbmMnrefproviderDefault_lm,
       
    99         EMbmMnrefproviderDefault_lm_mask);
       
   100 
       
   101     return iconList;
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 void AddLandmarkL(
       
   108     const CPosLandmark& aLandmark,
       
   109     CDesCArray& aTextArray,
       
   110     CArrayPtr<CGulIcon>& aIconArray )
       
   111     {
       
   112     TPtrC name;
       
   113     aLandmark.GetLandmarkName( name );
       
   114 
       
   115     HBufC* printableName = MnrpUtils::PrintableStringLC( name );
       
   116     
       
   117     TPtrC iconFile;
       
   118     TInt iconIndex, maskIndex, iconArrayIndex;
       
   119 
       
   120     if ( aLandmark.GetIcon( iconFile, iconIndex, maskIndex ) == KErrNone )
       
   121         {
       
   122         AddIconL( aIconArray, iconFile, iconIndex, maskIndex );
       
   123         iconArrayIndex = aIconArray.Count() - 1;
       
   124         }
       
   125     else
       
   126         {
       
   127         iconArrayIndex = KDefaultLmIconIndex;
       
   128         }
       
   129 
       
   130     _LIT(KListItemFormat, "%d\t%S\t\t");
       
   131     const TInt KListItemMinSize = 20;
       
   132 
       
   133     HBufC* item = HBufC::NewLC( printableName->Size() + KListItemMinSize );
       
   134     item->Des().Format( KListItemFormat, iconArrayIndex, printableName );
       
   135 
       
   136     aTextArray.AppendL( *item );
       
   137 
       
   138     CleanupStack::PopAndDestroy( item );
       
   139     CleanupStack::PopAndDestroy( printableName );
       
   140     }
       
   141 
       
   142 // ============================ MEMBER FUNCTIONS ===============================
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 CMnrpAppUi::CMnrpAppUi( CMnrpEngine& aEngine )
       
   148   : iEngine( aEngine )
       
   149     {
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CMnrpAppUi::ConstructL()
       
   156     {
       
   157     BaseConstructL( EAknEnableSkin );
       
   158     
       
   159     // This is empty view, it will be shown by UI framework,
       
   160     // when application starts
       
   161     iAppView = CMnrpAppView::NewL();
       
   162     AddViewL( iAppView );
       
   163 
       
   164     if ( !iEikonEnv->StartedAsServerApp() )
       
   165         {
       
   166         // switch to map view if started normally
       
   167         iMapView = CMnrpMapView::NewL( EFalse, iEngine );
       
   168         AddViewL( iMapView );
       
   169         ActivateLocalViewL( iMapView->Id() );
       
   170         }
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // -----------------------------------------------------------------------------
       
   175 //
       
   176 CMnrpAppUi::~CMnrpAppUi()
       
   177     {
       
   178     delete iNaviPaneLabel;
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 void CMnrpAppUi::HandleCommandL( TInt aCommand )
       
   185     {
       
   186     switch(aCommand)
       
   187         {
       
   188         case EAknSoftkeyBack:
       
   189         	{
       
   190     	    CEikAppServer* server = iEikonEnv->AppServer();
       
   191 		    if ( server )
       
   192 		    	{
       
   193 		        server->NotifyServerExit( EEikCmdExit );
       
   194 		    	}
       
   195 		    Exit();	
       
   196         	break;
       
   197         	}
       
   198         	
       
   199         case EEikCmdExit:
       
   200         case EAknSoftkeyExit:
       
   201             {
       
   202     	    CEikAppServer* server = iEikonEnv->AppServer();
       
   203 		    if ( server )
       
   204 		    	{
       
   205 		        server->NotifyServerExit( EAknCmdExit ); // this will close client also
       
   206 		    	}
       
   207 		    Exit();	
       
   208         	break;
       
   209             }
       
   210         default:
       
   211             break;
       
   212         }
       
   213     }
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 TBool CMnrpAppUi::IsChainedMode()
       
   219     {
       
   220     return ( iParentWg != 0 );
       
   221     }
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 TBool CMnrpAppUi::IsAnyUserViewActive()
       
   227     {
       
   228     TVwsViewId viewId;
       
   229     if ( ( GetActiveViewId( viewId ) == KErrNotFound ) ||
       
   230          ( viewId.iViewUid.iUid != EMnrpMapViewId && 
       
   231            viewId.iViewUid.iUid != EMnrpNavigationViewId ) )
       
   232         {
       
   233         return EFalse;
       
   234         }
       
   235     else
       
   236         {
       
   237         return ETrue;
       
   238         }
       
   239     }
       
   240 
       
   241 // ----------------------------------------------------------------------------
       
   242 // ----------------------------------------------------------------------------
       
   243 //
       
   244 void CMnrpAppUi::SetNaviPaneTitleL( const TDesC& aPaneTitle )
       
   245     {
       
   246     if ( !iNaviPane )
       
   247         {
       
   248         iNaviPane = static_cast<CAknNavigationControlContainer*>(
       
   249                 StatusPane()->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) ) );
       
   250         }
       
   251 
       
   252     if ( iNaviPaneLabel )
       
   253         {
       
   254         CAknNavigationDecorator* naviLabel = iNaviPane->CreateNavigationLabelL( aPaneTitle );
       
   255         CleanupStack::PushL( naviLabel );
       
   256 
       
   257         iNaviPane->ReplaceL( *iNaviPaneLabel, *naviLabel );
       
   258 
       
   259         delete iNaviPaneLabel;
       
   260         iNaviPaneLabel = naviLabel;
       
   261 
       
   262         CleanupStack::Pop( naviLabel );
       
   263         }
       
   264     else
       
   265         {
       
   266         iNaviPaneLabel = iNaviPane->CreateNavigationLabelL( aPaneTitle );
       
   267         iNaviPane->PushL( *iNaviPaneLabel );
       
   268         }
       
   269     }    
       
   270 
       
   271 // ----------------------------------------------------------------------------
       
   272 // ----------------------------------------------------------------------------
       
   273 //
       
   274 void CMnrpAppUi::SetRightSoftkeyCommandL( TInt aCommand, const TDesC& aButtonTitle )
       
   275     {
       
   276     if ( Cba() )
       
   277         {
       
   278         Cba()->SetCommandL( 1, aCommand, aButtonTitle );
       
   279         }
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 TBool CMnrpAppUi::ProcessCommandParametersL( CApaCommandLine &aCommandLine )
       
   286     {
       
   287     iParentWg = aCommandLine.ParentWindowGroupID();
       
   288     LOG1("parent window group id %d", iParentWg );
       
   289     
       
   290     return CAknAppUi::ProcessCommandParametersL( aCommandLine );
       
   291     }
       
   292 
       
   293 // ----------------------------------------------------------------------------
       
   294 // ----------------------------------------------------------------------------
       
   295 //
       
   296 void CMnrpAppUi::HandleResourceChangeL( TInt aType )
       
   297     {
       
   298     CAknAppUi::HandleResourceChangeL( aType );
       
   299     
       
   300     TRect rect( ClientRect() );
       
   301     if ( Cba() )
       
   302         {
       
   303 		Cba()->SetBoundingRect( TRect() );
       
   304 		Cba()->ReduceRect( rect );
       
   305         }
       
   306     }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 void CMnrpAppUi::ShowMapL( CMnrpMapViewService& aService )
       
   312     {
       
   313     if ( !iMapView )
       
   314         {
       
   315         iMapView = CMnrpMapView::NewL( IsChainedMode(), iEngine );
       
   316         AddViewL( iMapView );
       
   317         }
       
   318         
       
   319     iMapView->SetMapModeL( aService );
       
   320     ActivateLocalViewL( iMapView->Id() );
       
   321     }
       
   322 
       
   323 // -----------------------------------------------------------------------------
       
   324 // -----------------------------------------------------------------------------
       
   325 //
       
   326 void CMnrpAppUi::ShowNavigationL( const CPosLandmark& aDestination )
       
   327     {
       
   328     if ( !iNaviView )
       
   329         {
       
   330         iNaviView = CMnrpNaviView::NewL( IsChainedMode(), iEngine );
       
   331         AddViewL( iNaviView );
       
   332         }
       
   333         
       
   334     iNaviView->SetDestinationL( aDestination );
       
   335     ActivateLocalViewL( iNaviView->Id() );
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 void CMnrpAppUi::StartSelectionL( 
       
   342     CMnrpMapViewService& aService,  
       
   343     MMapSelectionCallback& aCallback )
       
   344     {
       
   345 	SetNaviPaneTitleL( aService.SelectionRequestText() );
       
   346 
       
   347     if ( aService.Options() & CMnMapView::EOptionRestrictSelection )
       
   348         {
       
   349         // select from dialog 
       
   350         SelectFromDialogL( aService, aCallback );
       
   351         }
       
   352     else
       
   353         {
       
   354         // select from map
       
   355         if ( !iMapView )
       
   356             {
       
   357             iMapView = CMnrpMapView::NewL( IsChainedMode(), iEngine );
       
   358             AddViewL( iMapView );
       
   359             }
       
   360 
       
   361         iMapView->SetSelectionModeL( aService, aCallback );
       
   362         ActivateLocalViewL( iMapView->Id() );
       
   363         }
       
   364     }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // -----------------------------------------------------------------------------
       
   368 //
       
   369 void CMnrpAppUi::StopSelection()
       
   370     {
       
   371     __ASSERT_DEBUG( iMapView, Panic( KErrGeneral ) );
       
   372     iMapView->StopSelection();
       
   373 
       
   374     // Selection is done - close selection view
       
   375     TRAP_IGNORE( ActivateLocalViewL( iAppView->Id() ) );
       
   376     }
       
   377     
       
   378 // -----------------------------------------------------------------------------
       
   379 // -----------------------------------------------------------------------------
       
   380 //
       
   381 void CMnrpAppUi::SelectFromDialogL(
       
   382     CMnrpMapViewService& aService,
       
   383     MMapSelectionCallback& aCallback )
       
   384     {
       
   385     LOG("MnRefProvider::SelectFromDialogL in");
       
   386 
       
   387     const TInt KGranularity = 8;
       
   388 
       
   389     CDesCArraySeg *textArray = new (ELeave) CDesCArraySeg( KGranularity );
       
   390     CleanupStack::PushL( textArray );
       
   391     CArrayPtrSeg<CGulIcon>* iconArray = CreateIconListLC( KGranularity );
       
   392 
       
   393     TInt linkedLmsOffset = 0;
       
   394 
       
   395     CMnrpDocument* doc = ( CMnrpDocument* ) Document();
       
   396     CMnrpEngine& engine = doc->Engine();
       
   397 
       
   398     // add non-linked landmarks
       
   399     LOG("MnRefProvider::DoSelectionDialogL adding non-linked landmarks");
       
   400     for ( TInt i = 0; i < aService.LandmarksToShow().Count(); i++)
       
   401         {
       
   402         const CPosLandmark* lm = aService.LandmarksToShow()[i];
       
   403         AddLandmarkL( *lm, *textArray, *iconArray );
       
   404         }
       
   405 
       
   406     // add linked landmarks
       
   407     LOG("MnRefProvider::DoSelectionDialogL adding linked landmarks");
       
   408 
       
   409     // create dictionary of linked landmarks
       
   410     // in order to ease searching for return values
       
   411     // when completing request
       
   412     RArray<TInt> dbIndices;
       
   413     RArray<TPosLmItemId> lmIds;
       
   414 
       
   415     CleanupClosePushL( dbIndices );
       
   416     CleanupClosePushL( lmIds );
       
   417 
       
   418     linkedLmsOffset = textArray->Count();
       
   419     for ( TInt db = 0; db < aService.LandmarksToShowDatabases().Count(); db++ )
       
   420         {
       
   421         const HBufC* uri = aService.LandmarksToShowDatabases()[db];
       
   422 
       
   423         CPosLandmarkDatabase* lmdb = CPosLandmarkDatabase::OpenL( *uri );
       
   424         CleanupStack::PushL( lmdb );
       
   425 
       
   426         if ( lmdb->IsInitializingNeeded() )
       
   427             {
       
   428             ExecuteAndDeleteLD( lmdb->InitializeL() );
       
   429             }
       
   430 
       
   431         for ( TInt i = 0; i < aService.LinkedLandmarksToShow(db).Count(); i++)
       
   432             {
       
   433             TPosLmItemId id = aService.LinkedLandmarksToShow(db)[i];
       
   434 
       
   435             CPosLandmark* lm = lmdb->ReadLandmarkLC( id );
       
   436             AddLandmarkL( *lm, *textArray, *iconArray );
       
   437             
       
   438             dbIndices.AppendL( db );
       
   439             lmIds.AppendL( id );
       
   440 
       
   441             CleanupStack::PopAndDestroy( lm );
       
   442             }
       
   443 
       
   444         CleanupStack::PopAndDestroy( lmdb );
       
   445         }
       
   446 
       
   447     TInt selected = 0;
       
   448     CAknSelectionListDialog *dialog =
       
   449         CAknSelectionListDialog::NewL( selected, textArray, R_MNREFPROVIDER_MENUBAR );
       
   450 
       
   451     dialog->PrepareLC( R_MNREFPROVIDER_SELECTION_DIALOG );
       
   452 
       
   453     dialog->SetIconArrayL( iconArray );
       
   454     CleanupStack::Pop(); // iconArray cleanup
       
   455 
       
   456     LOG1("MnRefProvider::SelectFromDialogL launching dialog with %d items", textArray->Count() );
       
   457     TInt result = dialog->RunLD();
       
   458     if ( result )
       
   459         {
       
   460         LOG("MnRefProvider::SelectFromDialogL approved");
       
   461         if ( selected < linkedLmsOffset )
       
   462             {
       
   463             aCallback.HandleSelectionL( selected );
       
   464             }
       
   465         else
       
   466             {
       
   467             TInt linkedLmIndex = selected - linkedLmsOffset;
       
   468             if ( linkedLmIndex < dbIndices.Count() && 
       
   469                  linkedLmIndex < lmIds.Count() )
       
   470                 {
       
   471                 aCallback.HandleSelectionL( lmIds[linkedLmIndex], dbIndices[linkedLmIndex] );
       
   472                 }
       
   473             else
       
   474                 {
       
   475                 // never happens
       
   476                 LOG("MnRefProvider::SelectFromDialogL selection out of range, out");
       
   477                 aCallback.HandleSelectionErrorL( KErrGeneral );
       
   478                 }
       
   479             }
       
   480         }
       
   481     else
       
   482         {
       
   483         LOG("MnRefProvider::SelectFromDialogL cancelled, out");
       
   484         aCallback.HandleSelectionErrorL( KErrCancel );
       
   485         }
       
   486 
       
   487     CleanupStack::PopAndDestroy( &lmIds );
       
   488     CleanupStack::PopAndDestroy( &dbIndices );
       
   489 
       
   490     CleanupStack::PopAndDestroy( textArray );
       
   491     LOG("MnRefProvider::SelectFromDialogL out");
       
   492 }