S60 3rd Edition SDK for Symbian OS
Example Applications Guide

SIPExAppUi.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : SIPExAppUi.cpp
00004 *  Part of     : SIPEx
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 
00018 #include "SIPExAppUi.h"
00019 #include "SIPExGameView.h"
00020 #include "SIPExLogView.h"
00021 #include "SIPEx.hrh"
00022 #include "SIPExAddrQueryDlg.h"
00023 #include "SIPExIMDialog.h"
00024 #include "SIPExDoc.h"
00025 #include "SIPExGameContainer.h"
00026 #include "SIPExLogContainer.h"
00027 #include <SIPEx.rsg>
00028 #include <eikenv.h>
00029 #include <aknquerydialog.h>
00030 #include <akntitle.h>
00031 #include <stringloader.h>
00032 
00033 
00034 // ============================ MEMBER FUNCTIONS ===============================
00035 
00036 // -----------------------------------------------------------------------------
00037 // CSIPExAppUi::CSIPExAppUi
00038 // C++ default constructor can NOT contain any code, that
00039 // might leave.
00040 // -----------------------------------------------------------------------------
00041 //
00042 CSIPExAppUi::CSIPExAppUi()
00043 : iState( CSIPExEngine::EIdle )
00044     {
00045     }
00046 
00047 // -----------------------------------------------------------------------------
00048 // CSIPExAppUi::ConstructL
00049 // Symbian 2nd phase constructor can leave.
00050 // -----------------------------------------------------------------------------
00051 //
00052 void CSIPExAppUi::ConstructL()
00053     {
00054     BaseConstructL( EAknEnableSkin );
00055     
00056     // Show tabs for main views from resources
00057     CEikStatusPane* sp = StatusPane();
00058 
00059     // Fetch pointer to the default navi pane control
00060     iNaviPane = static_cast< CAknNavigationControlContainer*>( 
00061         sp->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) ) );
00062 
00063     // Tabgroup has been read from resource and it was pushed to the navi 
00064     // pane. Get pointer to the navigation decorator with the 
00065     // ResourceDecorator() function. 
00066     // Application owns the decorator and it has responsibility to 
00067     // delete the object.
00068     iDecoratedTabGroup = iNaviPane->ResourceDecorator();
00069     if (iDecoratedTabGroup)
00070         {
00071         iTabGroup = static_cast< CAknTabGroup* >( 
00072                 iDecoratedTabGroup->DecoratedControl() );
00073         }
00074 
00075     iEngine = 
00076         ( static_cast< CSIPExDoc* >( Document() ) )->CreateEngineL( *this );
00077     
00078     // Receive view notifications from engine. Depending on currently 
00079     // active view, request (show message etc.) is either executed or 
00080     // stored for later usage
00081     iEngine->SetViewNotifier( *this );
00082     
00083     
00084     CSIPExGameView* gameview = CSIPExGameView::NewLC( *iEngine );
00085     AddViewL( gameview ); //transfer ownership to CAknViewAppUi
00086     CleanupStack::Pop( gameview );
00087     
00088     CSIPExLogView* logview = CSIPExLogView::NewLC( *iEngine );
00089     AddViewL( logview ); //transfer ownership to CAknViewAppUi
00090     CleanupStack::Pop( logview );
00091 
00092     SetDefaultViewL( *gameview );
00093     }
00094 
00095 // -----------------------------------------------------------------------------
00096 // CSIPExAppUi::~CSIPExAppUi
00097 // Destructor
00098 // -----------------------------------------------------------------------------
00099 //
00100 CSIPExAppUi::~CSIPExAppUi()
00101     {
00102     delete iDecoratedTabGroup;
00103     }
00104     
00105 // -----------------------------------------------------------------------------
00106 // CSIPExAppUi::HandleKeyEventL
00107 // Handle certain key events (for changing views etc.)
00108 // -----------------------------------------------------------------------------
00109 //
00110 TKeyResponse CSIPExAppUi::HandleKeyEventL(
00111     const TKeyEvent& /*aKeyEvent*/, 
00112     TEventCode /*aType*/)
00113     {
00114     // This would be good place for changing views according to left and right
00115     // key presses but those are reserved for game view for moving the piece
00116     return EKeyWasConsumed;
00117     }
00118 
00119 // -----------------------------------------------------------------------------
00120 // CSIPExAppUi::HandleCommandL
00121 // User interaction handling. The views capture most of the events, but some
00122 // might get pushed here
00123 // -----------------------------------------------------------------------------
00124 //
00125 void CSIPExAppUi::HandleCommandL( TInt aCommand )
00126     {
00127     switch ( aCommand )
00128         {
00129         case EEikCmdExit:
00130             {
00131             // If game is ongoing, notify opponent, ignore errors
00132             TRAPD( err, iEngine->EndGameL() );
00133             Exit();
00134             break;
00135             }
00136 
00137         case ECmdInviteForGame:
00138             {
00139             CSIPExAddrQueryDlg* dlg = CSIPExAddrQueryDlg::NewL( iAddress );
00140             if( dlg->ExecuteLD( R_INVITE_ADDRESS_DLG ) )
00141                 {
00142                 iEngine->InviteL( iAddress );
00143                 }
00144             break;
00145             }     
00146 
00147         case ECmdEnableProfile:
00148             {
00149             iEngine->EnableProfileL();
00150             break;
00151             }
00152 
00153         case ECmdDisableProfile:
00154             {
00155             iEngine->DisableProfileL();
00156             break;
00157             }
00158 
00159         // Send instant message
00160         case ECmdSendIM:
00161             {
00162             TBuf< 256 > msg;
00163             CSIPExIMDialog* dlg = CSIPExIMDialog::NewL( iAddress, msg );
00164             if( dlg->ExecuteLD( R_INSTANT_MESSAGE_DLG ) )
00165                 {
00166                 iEngine->SendInstantMsgL( iAddress, msg );
00167                 }
00168             break;
00169             }
00170             
00171         // Change to Game View
00172         case ECmdChangeViewToGame:
00173             {
00174             if ( iTabGroup )
00175                 {
00176                 TInt active = iTabGroup->ActiveTabIndex();
00177                 TInt count = iTabGroup->TabCount();
00178                 if ( active > 0 )
00179                     {
00180                     active--;
00181                     iTabGroup->SetActiveTabByIndex( active );
00182                     // ActivateLocalViewL() is used to change the view. 
00183                     ActivateLocalViewL( 
00184                         TUid::Uid( iTabGroup->TabIdFromIndex( active ) ) );
00185                     }
00186                 }
00187             break;
00188             }
00189             
00190         // Change to Log View
00191         case ECmdChangeViewToLog:
00192             {
00193             if ( iTabGroup )
00194                 {
00195                 TInt active = iTabGroup->ActiveTabIndex();
00196                 TInt count = iTabGroup->TabCount();
00197                 if ( (active + 1) < count )
00198                     {
00199                     active++;
00200                     iTabGroup->SetActiveTabByIndex( active );
00201                     // ActivateLocalViewL() is used to change the view. 
00202                     ActivateLocalViewL( 
00203                         TUid::Uid( iTabGroup->TabIdFromIndex( active ) ) );
00204                     }
00205                 }    
00206             break;
00207             }
00208             
00209 
00210         // End game
00211         case ECmdEndGame:
00212             {
00213             iEngine->EndGameL();
00214             break;
00215             }
00216 
00217         default:
00218             {
00219             
00220             // Do nothing, no-one will handle it
00221             // No use of leaving though, because this isn't an error - only
00222             // an unhandled command
00223             break;
00224             }
00225         }
00226     }
00227         
00228 // -----------------------------------------------------------------------------
00229 // CSIPExAppUi::GameStateChanged
00230 // From MSIPExGameObserver.
00231 // Game state changed. Then the game should be redraw.
00232 // -----------------------------------------------------------------------------
00233 //
00234 void CSIPExAppUi::GameStateChanged( CSIPExEngine::TEngineState aState )
00235     {
00236     iState = aState;
00237     
00238     if ( GameViewActive() )
00239         {
00240         GameContainer()->DrawDeferred();
00241         }
00242     }
00243     
00244 
00245 // -----------------------------------------------------------------------------
00246 // CSIPExAppUi::AcceptInvitationL
00247 // From MSIPExGameObserver.
00248 // Show confirmation dialog with summoner's address.
00249 // If accepted return ETrue else EFalse.
00250 // -----------------------------------------------------------------------------
00251 //
00252 TBool CSIPExAppUi::AcceptInvitationL( const TDesC& aFrom )
00253     {
00254     HBufC* buf = 
00255         StringLoader::LoadLC( R_SIP_EX_ACCEPT_INVITATION, aFrom, iEikonEnv );
00256     TBool retVal( EFalse );
00257 
00258     CAknQueryDialog* query = CAknQueryDialog::NewL();
00259     if( query->ExecuteLD( R_ACCEPT_INVITATION_DLG, *buf ) )
00260         {
00261         retVal = ETrue;
00262         }
00263 
00264     CleanupStack::PopAndDestroy( buf );
00265     return retVal;
00266     }
00267 
00268 // -----------------------------------------------------------------------------
00269 // CSIPExAppUi::IMReceivedL
00270 // From MSIPExGameObserver.
00271 // Show the received instant message in info editor.
00272 // -----------------------------------------------------------------------------
00273 //
00274 void CSIPExAppUi::IMReceivedL( const TDesC8& aFrom, const TDesC8& aMsg )
00275     {
00276     HBufC* from = HBufC::NewLC( aFrom.Length() );
00277     from->Des().Copy( aFrom );
00278 
00279     HBufC* msg = HBufC::NewLC( aMsg.Length() );
00280     msg->Des().Copy( aMsg );
00281 
00282     CDesCArrayFlat* strings = new (ELeave) CDesCArrayFlat( 2 );
00283     CleanupStack::PushL( strings );
00284     strings->AppendL( *from );
00285     strings->AppendL( *msg );
00286     HBufC* buf = 
00287             StringLoader::LoadL( R_SIP_EX_IM_RECEIVED, *strings, iEikonEnv );
00288             
00289     CleanupStack::PopAndDestroy( strings );
00290     CleanupStack::PushL( buf );
00291     ShowInfoL( *buf );
00292     CleanupStack::PopAndDestroy( 3 );
00293     }
00294 
00295 // -----------------------------------------------------------------------------
00296 // CSIPExAppUi::ShowStatusInfoL
00297 // -----------------------------------------------------------------------------
00298 //
00299 void CSIPExAppUi::ShowStatusInfoL( const TDesC& aText )
00300     {
00301     // Show status info in title pane (could be additionally 
00302     // shown in some popup note)
00303     //
00304     TUid titlePaneUid = { EEikStatusPaneUidTitle };
00305     
00306     CEikStatusPane* statusPane = StatusPane();
00307     
00308     CEikStatusPaneBase::TPaneCapabilities subPane = 
00309                                 statusPane->PaneCapabilities( titlePaneUid );
00310     
00311     
00312     HBufC* text = StringLoader::LoadLC( R_SIP_EX_TITLE_TXT, aText, iEikonEnv );
00313     
00314     if ( subPane.IsPresent() && subPane.IsAppOwned() )
00315         {
00316         CAknTitlePane* titlePane = 
00317             static_cast< CAknTitlePane* >( statusPane->ControlL( titlePaneUid ) );
00318         
00319         titlePane->SetText( text );
00320         
00321         CleanupStack::Pop( text );
00322         }
00323     else
00324         {
00325         CleanupStack::PopAndDestroy( text );
00326         }
00327     }
00328     
00329 // -----------------------------------------------------------------------------
00330 // CSIPExAppUi::ShowInfoL
00331 // -----------------------------------------------------------------------------
00332 //
00333 void CSIPExAppUi::ShowInfoL( const TDesC& aText )
00334     {
00335     static_cast< CSIPExDoc* >( Document() )->StoreInfoTextL( aText );
00336     if( !GameViewActive() )
00337         {
00338         LogContainer()->ShowInfoL();
00339         } 
00340     }
00341     
00342 // -----------------------------------------------------------------------------
00343 // CSIPExAppUi::ShowInfoL
00344 // -----------------------------------------------------------------------------
00345 //
00346 void CSIPExAppUi::ShowInfoL( const TDesC8& aText )
00347     {
00348     HBufC* buf = HBufC::NewLC( aText.Length() );
00349     buf->Des().Copy( aText );
00350     static_cast< CSIPExDoc* >( Document() )->StoreInfoTextL( *buf );
00351     CleanupStack::PopAndDestroy( buf );
00352     
00353     if( !GameViewActive() )
00354         {
00355         LogContainer()->ShowInfoL();
00356         } 
00357     }
00358         
00359 // -----------------------------------------------------------------------------
00360 // CSIPExAppUi::EngineState
00361 // -----------------------------------------------------------------------------
00362 //
00363 const CSIPExEngine::TEngineState CSIPExAppUi::EngineState()
00364     {
00365     return iState;
00366     }
00367 
00368 // -----------------------------------------------------------------------------
00369 // CSIPExAppUi::GameViewActive
00370 // -----------------------------------------------------------------------------
00371 //    
00372 TBool CSIPExAppUi::GameViewActive()
00373     {
00374     return IsViewActive( KGameViewId );
00375     }
00376     
00377 // -----------------------------------------------------------------------------
00378 // CSIPExAppUi::LogViewActive
00379 // -----------------------------------------------------------------------------
00380 //    
00381 TBool CSIPExAppUi::LogViewActive()
00382     {
00383     return IsViewActive( KLogViewId );
00384     }
00385    
00386 // -----------------------------------------------------------------------------
00387 // CSIPExAppUi::IsViewActive
00388 // -----------------------------------------------------------------------------
00389 // 
00390 TBool CSIPExAppUi::IsViewActive( TUid aViewUid )
00391         {
00392         TInt active = iTabGroup->ActiveTabIndex();
00393     return ( TUid::Uid( iTabGroup->TabIdFromIndex( active ) ) == aViewUid );
00394         }
00395     
00396 // -----------------------------------------------------------------------------
00397 // CSIPExAppUi::GameContainer
00398 // -----------------------------------------------------------------------------
00399 //   
00400 CSIPExGameContainer* CSIPExAppUi::GameContainer()
00401     {
00402     return static_cast< CSIPExGameView* >( View( KGameViewId ) )->Container();
00403     }
00404     
00405 // -----------------------------------------------------------------------------
00406 // CSIPExAppUi::LogContainer
00407 // -----------------------------------------------------------------------------
00408 //   
00409 CSIPExLogContainer* CSIPExAppUi::LogContainer()
00410     {
00411     return static_cast< CSIPExLogView* >( View( KLogViewId ) )->Container();
00412     }
00413     
00414 //  End of File

© Nokia 2006

Back to top