Nokia N97 SDK
Example Applications Guide

IMOpenAPIExampleModel.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : IMOpenAPIExampleModel.cpp
00004 *  Part of     : IMOpenAPIExample
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2005-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 // INCLUDE FILES
00016 #include <imconnection.h>
00017 #include <imclient.h>
00018 #include <bamdesca.h>
00019 #include <badesca.h>
00020 #include <imerrors.h>
00021 #include <e32math.h>
00022 #include "IMOpenAPIExamplemodel.h"
00023 #include "IMOpenAPIExampleDocument.h"
00024 #include "IMOpenAPIExampleAppUi.h"
00025 #include "IMOpenAPIExamplemsg.h"
00026 
00027 // ================= MEMBER FUNCTIONS =======================
00028 //
00029 // ----------------------------------------------------------
00030 // CIMOpenAPIExamplemodel::ConstructL()
00031 // takes a pointer to document
00032 // ----------------------------------------------------------
00033 //
00034 void CIMOpenAPIExamplemodel::ConstructL(CIMOpenAPIExampleDocument* aDocument)
00035     {
00036     TRAPD(err,iIMConnection = CImConnection::NewL(KNullDesC));
00037     if (err!=KErrNone)
00038         {
00039         iNotSupported = ETrue;
00040         iState = EStateNotLogin;
00041         }
00042     else
00043         {
00044         iIMConnection->RegisterObserverL(this); // registering the connection
00045         iIMClient = iIMConnection->CreateImClientL();// creating the client
00046         iIMClient->RegisterObserverL(this); // registering the client
00047             
00048         // Check the IM status and set the application accordingly
00049         if (iIMConnection->ImConnectionStatus() == ELoggedIn)
00050                 iState = EStateLoginIdle;
00051         else
00052             
00053         iNotSupported=EFalse;        
00054         }
00055 
00056     iDocument = aDocument;
00057     ResetGameData(); // reset game data grid
00058         
00059 
00060         iArrayOfIDs = new (ELeave) CDesCArrayFlat(1);
00061    
00062         // msg sent ids are zero in the begining
00063         iMsgID = 0;
00064     iSending = EFalse;
00065     iLastMsg = MsgEndChallenge;
00066     SetOppID(KNullDesC);
00067         }
00068 
00069 // ----------------------------------------------------------
00070 // CIMOpenAPIExamplemodel::NewLC()
00071 // 
00072 // ----------------------------------------------------------
00073 //
00074 CIMOpenAPIExamplemodel* CIMOpenAPIExamplemodel::NewLC(CIMOpenAPIExampleDocument* aDocument)
00075     {
00076     CIMOpenAPIExamplemodel* self = new (ELeave) CIMOpenAPIExamplemodel;
00077     CleanupStack::PushL(self);
00078     self->ConstructL(aDocument);
00079     return self;
00080     }
00081 
00082 // ----------------------------------------------------------
00083 // CIMOpenAPIExamplemodel::NewL()
00084 // 
00085 // ----------------------------------------------------------
00086 //
00087 CIMOpenAPIExamplemodel* CIMOpenAPIExamplemodel::NewL(CIMOpenAPIExampleDocument* aDocument)
00088     {
00089     CIMOpenAPIExamplemodel* self = NewLC(aDocument);
00090     CleanupStack::Pop(self);
00091     return self;
00092         }
00093 
00094 // ----------------------------------------------------
00095 // CIMOpenAPIExamplemodel::~CIMOpenAPIExamplemodel()
00096 // Destructor
00097 // Frees reserved resources
00098 // ----------------------------------------------------
00099 //
00100 CIMOpenAPIExamplemodel::~CIMOpenAPIExamplemodel()
00101         {
00102         if( iIMClient )
00103                 {
00104         iIMClient->UnregisterObserver();
00105         delete iIMClient;
00106         iIMClient = NULL;
00107         }
00108     
00109     if (iIMConnection)
00110         {
00111         iIMConnection->UnregisterObserver();
00112         delete iIMConnection;
00113         iIMConnection = NULL;
00114         }
00115     
00116     if(iOppID)                    
00117         delete iOppID;
00118     iOppID = NULL;
00119     
00120     if (iArrayOfIDs)
00121         {
00122         iArrayOfIDs->Reset();
00123         delete iArrayOfIDs;
00124         }
00125         }
00126 
00127 
00128 // ----------------------------------------------------
00129 // CIMOpenAPIExamplemodel::CIMOpenAPIExamplemodel()
00130 // 
00131 // ----------------------------------------------------
00132 //
00133 CIMOpenAPIExamplemodel::CIMOpenAPIExamplemodel()
00134         {
00135         }
00136         
00137 // FUNCTIONS FROM THE BASE CLASSES
00138 // ----------------------------------------------------
00139 // CIMOpenAPIExamplemodel::HandleLoginL(const TInt aErrorCode)
00140 // 
00141 // ----------------------------------------------------
00142 //
00143 void CIMOpenAPIExamplemodel::HandleLoginL(const TInt aErrorCode)
00144     {
00145         iDocument->UI()->HandleMessageIMConnectionFromModel(Connected, aErrorCode);
00146         if (aErrorCode == KErrNone)
00147                 {
00148                 SetGameState(EStateLoginIdle);
00149                 }
00150     }
00151 
00152 // ----------------------------------------------------
00153 // CIMOpenAPIExamplemodel::HandleLogoutL()
00154 // 
00155 // ----------------------------------------------------
00156 //
00157 void CIMOpenAPIExamplemodel::HandleLogoutL(const TInt aErrorCode)
00158     {
00159         iDocument->UI()->HandleMessageIMConnectionFromModel(DisConnected, aErrorCode);
00160         SetGameState(EStateNotLogin);                                                                                   
00161     }
00162     
00163 // ----------------------------------------------------
00164 // CIMOpenAPIExamplemodel::HandleLogoutL()
00165 // 
00166 // ----------------------------------------------------
00167 //    
00168 void CIMOpenAPIExamplemodel::HandleCancelLoginL(const TInt /*aErrorCode*/)
00169         {
00170         
00171         
00172         }
00173 
00174 // ----------------------------------------------------
00175 // CIMOpenAPIExamplemodel::HandleMessageSentL()
00176 // 
00177 // ----------------------------------------------------
00178 //
00179 void CIMOpenAPIExamplemodel::HandleMessageSentL( const TInt /*aOpCode*/, const TInt aErrorCode )
00180     {
00181     iSending = EFalse;
00182     switch (iLastMsg)
00183         {
00184         case MsgChallenge:
00185             SetGameState(EStateChallenged);
00186             iDocument->UI()->HandleMessageFromModel(ChallengeSent, aErrorCode, KNullDesC);
00187             break;
00188         
00189         case MsgChallengeAccepted:
00190             SetGameState(EStateReadytoStart);
00191             break;
00192 
00193         case MsgChallengeRejected:
00194             SetGameState(EStateLoginIdle);
00195             break;
00196 
00197         case MsgStartGameYourTurn:
00198             if (GetGameState() == EStateReadytoStart)
00199                 SetGameState(EStateWaitingForOppMove);
00200             break;
00201 
00202         case MsgStartGameOppTurn:
00203             if (GetGameState() == EStateReadytoStart)
00204                 SetGameState(EStateWaitingForOwnMove);
00205             break;
00206 
00207         case MsgExitGame:
00208             iDocument->UI()->HandleMessageFromModel(EndGameRec, aErrorCode, KNullDesC);
00209             SetGameState(EStateReadytoStart);
00210             break;
00211 
00212         case MsgPlayedIndex:
00213             break;
00214 
00215         case MsgEndChallenge:
00216             iDocument->UI()->HandleMessageFromModel(EndChallengeRec, aErrorCode, KNullDesC);
00217             SetGameState(EStateLoginIdle);
00218             break;
00219 
00220         case MsgAlreadyInGame:
00221             break;
00222 
00223         default:
00224             break;
00225         }
00226         }
00227 
00228 // ----------------------------------------------------
00229 // CIMOpenAPIExamplemodel::HandleSendErrorL()
00230 // 
00231 // ----------------------------------------------------
00232 //
00233 void CIMOpenAPIExamplemodel::HandleSendErrorL(const TInt /*aOpCode*/, 
00234                                       const TInt aErrorCode, 
00235                                       MImClientDetailedError* /*aDetailedError*/)
00236     {
00237     iSending = EFalse;
00238 
00239 
00240     switch (aErrorCode)
00241         {
00242         case KImApiErrNotLogged:
00243             SetGameState(EStateNotLogin);
00244             iDocument->UI()->HandleMessageErrorFromModel(aErrorCode,KNullDesC );
00245             break;
00246 
00247         default:
00248             iDocument->UI()->HandleMessageErrorFromModel(aErrorCode,KNullDesC );
00249             break;
00250         }
00251     }
00252 
00253 
00254 // ----------------------------------------------------
00255 // CIMOpenAPIExamplemodel::HandleNewPToPMessageL()
00256 // 
00257 // ----------------------------------------------------
00258 //
00259 void CIMOpenAPIExamplemodel::HandleNewPToPMessageL(
00260             const TInt aErrorCode, 
00261             const TContactItemId  /*aContactId*/,
00262             const TDesC& aUserId,
00263             const TDesC& /*aMessageType*/,
00264             const TDesC16& aContent )
00265     {
00266     TInt integerInfo = 0;
00267     TLex16 receivedMsgLex;
00268     TPtrC16 receivedMsg;
00269     TBool isDraw = EFalse;
00270 
00271 
00272     // checking for played index, which is different from other messages
00273     if (aContent.Find(KPlayedIndex) != KErrNotFound)
00274         {
00275         receivedMsgLex.Assign(aContent.Right(1));
00276         receivedMsgLex.Val(integerInfo);
00277         receivedMsg.Set( aContent.Left(11) );
00278         }
00279     else
00280         receivedMsg.Set(aContent);
00281 
00282     switch (iState)
00283         {
00284         // In login idle state we only expect a Challenge from any other user
00285         case EStateLoginIdle:
00286             if ( receivedMsg.Compare(KChallenge) == 0)
00287                 {
00288                 SetOppID(aUserId);
00289                 iDocument->UI()->HandleChallengeRec(aUserId);
00290                 }
00291             break;
00292 
00293         // In Challenged state we only expects challenge accepted or rejected from our opp
00294         case EStateChallenged:
00295             if( aUserId.Find(*iOppID) == KErrNotFound )
00296                 break;
00297             else if ( receivedMsg.Compare(KChallengeAccepted) == 0)
00298                 {
00299                 SetGameState(EStateReadytoStart);
00300                 iDocument->UI()->HandleMessageFromModel(ChallengeAcceptedRec, aErrorCode, aContent);
00301                 }
00302             else if ( receivedMsg.Compare(KChallengeRejected) == 0)
00303                 {
00304                 SetGameState(EStateLoginIdle);
00305                 iDocument->UI()->HandleMessageFromModel(ChallengeRejectedRec, aErrorCode, aContent);
00306                 }
00307             else if ( receivedMsg.Compare(KAlreadyInGame) == 0)
00308                 {
00309                 SetGameState(EStateLoginIdle);
00310                 iDocument->UI()->HandleMessageFromModel(AlreadyInGameRec, aErrorCode, aContent);
00311                 }
00312             // if opp also sent the challenge at the same time, we will reply already in game
00313             else if ( receivedMsg.Compare(KChallenge) == 0)
00314                 {
00315                 SetGameState(EStateLoginIdle);
00316                 iDocument->UI()->HandleMessageFromModel(AlreadyInGameRec, aErrorCode, aContent);
00317                 }
00318             // in all othe requests we will not take any action
00319             break;
00320 
00321         
00322         case EStateReadytoStart:
00323             if( aUserId.Find(*iOppID) == KErrNotFound )
00324                 break;
00325             else if ( receivedMsg.Compare(KStartGameYourTurn) == 0)
00326                 {
00327                 SetGameState(EStateWaitingForOwnMove);
00328                 iDocument->UI()->HandleMessageFromModel(StartGameRec, aErrorCode, aContent);
00329                 }
00330             else if ( receivedMsg.Compare(KStartGameOppTurn) == 0)
00331                 {
00332                 SetGameState(EStateWaitingForOppMove);
00333                 iDocument->UI()->HandleMessageFromModel(StartGameRec, aErrorCode, aContent);
00334                 }
00335             else if ( receivedMsg.Compare(KExitGame) == 0)
00336                 {
00337                 iDocument->UI()->HandleMessageFromModel(EndGameRec, aErrorCode, aContent);
00338                 SetGameState(EStateReadytoStart);
00339                 }
00340             else if ( receivedMsg.Compare(KEndChallenge) == 0)
00341                 {
00342                 iDocument->UI()->HandleMessageFromModel(EndChallengeRec, aErrorCode, KNullDesC);
00343                 SetGameState(EStateLoginIdle);
00344                 }
00345             else
00346                 SendEndChallenge();
00347             break;
00348 
00349 
00350         case EStateWaitingForOppMove:
00351             if( aUserId.Find(*iOppID) == KErrNotFound )
00352                 break;
00353             else if ( receivedMsg.Compare(KPlayedIndex) == 0)
00354                 {
00355                 iDocument->UI()->HandleMessageFromModel(PlayedIndexRec, integerInfo, aContent);
00356                 iGameData.At(integerInfo) = iOppSign;
00357                 if ( IsGameFinnished(isDraw) )
00358                     {
00359                     if (isDraw)
00360                         iDocument->UI()->HandleMessageFromModel(GameDraw, aErrorCode, aContent);
00361                     else
00362                         iDocument->UI()->HandleMessageFromModel(YouLost, aErrorCode, aContent);
00363                     ResetGameData();
00364                     SetGameState(EStateReadytoStart);
00365                     break;
00366                     }
00367                 SetGameState(EStateWaitingForOwnMove);
00368                 }
00369             else if ( receivedMsg.Compare(KExitGame) == 0)
00370                 {
00371                 iDocument->UI()->HandleMessageFromModel(EndGameRec, aErrorCode, aContent);
00372                 SetGameState(EStateReadytoStart);
00373                 }
00374             else
00375                 SendEndChallenge();
00376             break;
00377 
00378 
00379         case EStateWaitingForOwnMove:
00380             if( aUserId.Find(*iOppID) == KErrNotFound )
00381                 break;
00382             if ( receivedMsg.Compare(KExitGame) == 0)
00383                 {
00384                 iDocument->UI()->HandleMessageFromModel(EndGameRec, aErrorCode, aContent);
00385                 SetGameState(EStateReadytoStart);
00386                 }
00387             else
00388                 SendEndChallenge();
00389             break;
00390 
00391 
00392         default:
00393             break;
00394 
00395         }
00396 
00397         }
00398 
00399 // ----------------------------------------------------
00400 // CIMOpenAPIExamplemodel::HandleNewPToPMessageL
00401 // Sends messages to IM
00402 // ----------------------------------------------------
00403 //
00404 void CIMOpenAPIExamplemodel::HandleNewPToPMessageL(
00405                                            const TInt /*aErrorCode*/, 
00406                                            const TContactItemId /*aContactId*/,
00407                                            const TDesC& /*aUserId*/,
00408                                            const TDesC& /*aMessageType*/,
00409                                            const TDesC8& /*aContent*/ )
00410     {
00411     }
00412 
00413 // ----------------------------------------------------
00414 // CIMOpenAPIExamplemodel::LoginL
00415 // 
00416 // ----------------------------------------------------
00417 //
00418 void CIMOpenAPIExamplemodel::LoginL(const TDesC& aServerAddress, const TDesC& aUserId,
00419                                                                 const TDesC& aPassword, const TInt aAccessPoint)
00420     {
00421         // We are using TRAPD at the UI level, because it has GUI to display
00422         if (iNotSupported)
00423             User::Leave(KErrNotSupported);      
00424     iIMConnection->LoginL(aServerAddress, aUserId, aPassword, aAccessPoint);
00425     }
00426     
00427 // ----------------------------------------------------
00428 // CIMOpenAPIExamplemodel::LogoutL
00429 // 
00430 // ----------------------------------------------------
00431 //
00432 void CIMOpenAPIExamplemodel::LogoutL()
00433         {
00434         // We are using TRAPD at the UI level, because it has GUI to display
00435         iIMConnection->LogoutL();
00436         }
00437 
00438 // ----------------------------------------------------
00439 // CIMOpenAPIExamplemodel::SendMsg
00440 // Sends messages to IM
00441 // ----------------------------------------------------
00442 //
00443 TInt CIMOpenAPIExamplemodel::SendMsg(TDesC16& aContent)
00444         {
00445         iSending = ETrue;
00446     //SetGameState (EStateSending);
00447         return iIMClient->SendPToPMessageL(*iArrayOfIDs, aContent);
00448         }
00449         
00450 // ----------------------------------------------------
00451 // CIMOpenAPIExamplemodel::SendChallenge
00452 // ----------------------------------------------------
00453 //
00454 void CIMOpenAPIExamplemodel::SendChallenge()
00455         {
00456         iMessageToSend.Copy(KChallenge);
00457         iMsgID = SendMsg(iMessageToSend);
00458     iLastMsg = MsgChallenge;
00459     iMySign = ECrossSign;
00460     iOppSign = EZeroSign;
00461         }
00462         
00463 // ----------------------------------------------------
00464 // CIMOpenAPIExamplemodel::SendChallengeAccepted
00465 // ----------------------------------------------------
00466 //
00467 void CIMOpenAPIExamplemodel::SendChallengeAccepted()
00468         {
00469         iMessageToSend.Copy(KChallengeAccepted);
00470     iMsgID = SendMsg(iMessageToSend);
00471     iLastMsg = MsgChallengeAccepted;
00472     iMySign = EZeroSign;
00473     iOppSign = ECrossSign;
00474         }
00475         
00476 // ----------------------------------------------------
00477 // CIMOpenAPIExamplemodel::SendChallengeRejected
00478 // ----------------------------------------------------
00479 //
00480 void CIMOpenAPIExamplemodel::SendChallengeRejected()
00481         {
00482     iMessageToSend.Copy(KChallengeRejected);    
00483     iMsgID = SendMsg(iMessageToSend);
00484     iLastMsg = MsgChallengeRejected;
00485     SetOppID(KNullDesC);
00486         }
00487         
00488 // ----------------------------------------------------
00489 // CIMOpenAPIExamplemodel::SendAlreadyInGame
00490 // ----------------------------------------------------
00491 //
00492 void CIMOpenAPIExamplemodel::SendAlreadyInGame()
00493         {
00494     iMessageToSend.Copy(KAlreadyInGame);        
00495     iMsgID = SendMsg(iMessageToSend);
00496     iLastMsg = MsgAlreadyInGame;
00497         }
00498         
00499 // ----------------------------------------------------
00500 // CIMOpenAPIExamplemodel::SendStartGame
00501 // ----------------------------------------------------
00502 //
00503 void CIMOpenAPIExamplemodel::SendStartGame()
00504         {
00505     TBool turn;
00506     
00507     // Find a randomize turn
00508     turn = ( Math::Rand(iSeed) ) % 2;
00509 
00510     if (turn)
00511         {
00512         iMessageToSend.Copy(KStartGameOppTurn);
00513             iMsgID = SendMsg(iMessageToSend);
00514         iLastMsg = MsgStartGameOppTurn;
00515         }
00516     else
00517         {
00518         iMessageToSend.Copy(KStartGameYourTurn);
00519             iMsgID = SendMsg(iMessageToSend);
00520         iLastMsg = MsgStartGameYourTurn;
00521         }
00522         }
00523         
00524 // ----------------------------------------------------
00525 // CIMOpenAPIExamplemodel::SendExitGame
00526 // ----------------------------------------------------
00527 //
00528 void CIMOpenAPIExamplemodel::SendExitGame()
00529         {
00530     iMessageToSend.Copy(KExitGame);
00531         iMsgID = SendMsg(iMessageToSend);
00532     iLastMsg = MsgExitGame;
00533         }
00534 
00535 // ----------------------------------------------------
00536 // CIMOpenAPIExamplemodel::SendPlayedIndex
00537 // ----------------------------------------------------
00538 //
00539 void CIMOpenAPIExamplemodel::SendPlayedIndex(TInt aIndex)
00540         {
00541     TBool isDraw = EFalse;
00542 
00543     iMessageToSend.Copy(KPlayedIndex);
00544         iMessageToSend.AppendNumUC(aIndex,EDecimal);
00545         iMsgID = SendMsg(iMessageToSend);
00546     iLastMsg = MsgPlayedIndex;
00547 
00548     iGameData.At(aIndex) = iMySign;
00549     if ( IsGameFinnished(isDraw) )
00550         {
00551         if (isDraw)
00552             iDocument->UI()->HandleMessageFromModel(GameDraw, KErrNone, KNullDesC);
00553         else
00554             iDocument->UI()->HandleMessageFromModel(YouWon, KErrNone, KNullDesC);
00555         ResetGameData();
00556         SetGameState(EStateReadytoStart);
00557         return;
00558         }
00559     SetGameState(EStateWaitingForOppMove);
00560         }
00561         
00562 // ----------------------------------------------------
00563 // CIMOpenAPIExamplemodel::SendEndChallenge
00564 // ----------------------------------------------------
00565 //
00566 void CIMOpenAPIExamplemodel::SendEndChallenge()
00567         {
00568     iMessageToSend.Copy(KEndChallenge);
00569         iMsgID = SendMsg(iMessageToSend);
00570     iLastMsg = MsgEndChallenge;
00571         }
00572 // ----------------------------------------------------
00573 // CIMOpenAPIExamplemodel::IsGameFinnished
00574 // ----------------------------------------------------
00575 //
00576 TBool CIMOpenAPIExamplemodel::IsGameFinnished(TBool& aIsDraw)
00577         {
00578         // checking for sign ECrossSign (*) win in the matrix
00579         if ( CheckForSeries(ECrossSign,ECrossSign,EZeroSign) || CheckForSeries(ECrossSign,3,EZeroSign) || 
00580         CheckForSeries(ECrossSign,6,EZeroSign) || CheckForSeries(ECrossSign,ECrossSign,3) || 
00581         CheckForSeries(ECrossSign,EZeroSign,3) || CheckForSeries(ECrossSign,EEmptySign,3) ||
00582                 CheckForSeries(ECrossSign,ECrossSign,4) || CheckForSeries(ECrossSign,EEmptySign,EEmptySign) )
00583                 {
00584         aIsDraw = EFalse;
00585                 return ETrue;
00586                 }
00587 
00588         // checking for sign EZeroSign (o) win in the matrix
00589         else if ( CheckForSeries(EZeroSign,ECrossSign,EZeroSign) || CheckForSeries(EZeroSign,3,EZeroSign) || CheckForSeries(EZeroSign,6,EZeroSign) ||
00590                 CheckForSeries(EZeroSign,ECrossSign,3) || CheckForSeries(EZeroSign,EZeroSign,3) || CheckForSeries(EZeroSign,EEmptySign,3) ||
00591                 CheckForSeries(EZeroSign,ECrossSign,4) || CheckForSeries(EZeroSign,EEmptySign,EEmptySign) )
00592                 {
00593         aIsDraw = EFalse;
00594                 return ETrue;
00595                 }
00596 
00597         else if (IsGameDraw())
00598         {
00599         aIsDraw = ETrue;
00600         return ETrue;
00601         }
00602 
00603         return EFalse;
00604         }
00605 
00606 // ----------------------------------------------------
00607 // CIMOpenAPIExamplemodel::IsGameDraw()
00608 // checks if the game has draw, should be called after checking for winner
00609 // ----------------------------------------------------
00610 //
00611 TBool CIMOpenAPIExamplemodel::IsGameDraw()
00612     {
00613     for (TInt i=0;i<9;i++)
00614         {
00615         if (iGameData.At(i) == EEmptySign)
00616             return false;
00617         }
00618     return true;
00619     }
00620 
00621 // ----------------------------------------------------
00622 // CIMOpenAPIExamplemodel::CheckForSeries(TInt8 value, TInt8 startIndex, TInt8 indexIncrement)
00623 // Check for a series of 3 consective 'value', give start index and increment by number
00624 // ----------------------------------------------------
00625 //
00626 TBool CIMOpenAPIExamplemodel::CheckForSeries(TGameSign aValue, TInt aStartIndex, TInt aIndexIncrement)
00627         {
00628         return ( (iGameData.At(aStartIndex) == aValue) && 
00629                                 (iGameData.At(aStartIndex+aIndexIncrement) == aValue) && 
00630                                 (iGameData.At(aStartIndex+aIndexIncrement+aIndexIncrement) == aValue) );
00631 
00632         }
00633 
00634 // ----------------------------------------------------
00635 // CIMOpenAPIExamplemodel::GetGameState()
00636 // returns Game current state, only call from outside this class
00637 // ----------------------------------------------------
00638 //
00639 TGameState CIMOpenAPIExamplemodel::GetGameState() const
00640         {
00641     if(iSending)
00642         return EStateSending;
00643         return iState;
00644         }
00645 
00646 // ----------------------------------------------------
00647 // CIMOpenAPIExamplemodel::SetGameState()
00648 // Sets game status, in all the game
00649 // ----------------------------------------------------
00650 //
00651 void CIMOpenAPIExamplemodel::SetGameState(TGameState const aState)
00652         {
00653         iState = aState;
00654         iDocument->UI()->SetGameState(aState);
00655     if (iState == EStateReadytoStart)
00656         ResetGameData();
00657     else if (iState == EStateLoginIdle)
00658         SetOppID(KNullDesC);
00659         }
00660 
00661 // ----------------------------------------------------
00662 // CIMOpenAPIExamplemodel::SetOppID(TDesC16& aOppID)
00663 // Sets the oponent id and pushed it into an array
00664 // ----------------------------------------------------
00665 //
00666 void CIMOpenAPIExamplemodel::SetOppID(const TDesC16& aOppID)
00667         {
00668     TBuf<KMaxUserIDLength> oppID(aOppID);
00669     oppID.LowerCase();    
00670     delete iOppID;
00671     iOppID = NULL;
00672     iOppID = oppID.AllocL();
00673     iArrayOfIDs->Reset();
00674         iArrayOfIDs->AppendL(*iOppID);
00675         }
00676 
00677 // ----------------------------------------------------
00678 // CIMOpenAPIExamplemodel::MatchOppID(TDesC16& aOppID)
00679 // matches incoming opp id to existing opp id, returns true if success
00680 // ----------------------------------------------------
00681 //
00682 TBool CIMOpenAPIExamplemodel::MatchOppID(TDesC16& aOppID)
00683         {
00684         if ( iOppID->Compare(aOppID) == 0 )
00685                 return ETrue;
00686         return EFalse;
00687         }
00688 
00689 // ----------------------------------------------------
00690 // CIMOpenAPIExamplemodel::GetOppSign()
00691 // Gets opponent sign
00692 // ----------------------------------------------------
00693 //
00694 TGameSign CIMOpenAPIExamplemodel::GetOppSign() const
00695     {
00696     return iOppSign;
00697     }
00698 
00699 // ----------------------------------------------------
00700 // CIMOpenAPIExamplemodel::ResetGameData()
00701 // ----------------------------------------------------
00702 //
00703 void CIMOpenAPIExamplemodel::ResetGameData()
00704     {
00705     for (TInt8 i=0; i<9; i++) // set all game matrix data to 'empty'
00706                 iGameData.At(i) = EEmptySign;
00707     }
00708     
00709 // ----------------------------------------------------
00710 // CIMOpenAPIExamplemodel::IsIndexPositionEmpty
00711 // ----------------------------------------------------
00712 //
00713 TBool CIMOpenAPIExamplemodel::IsIndexPositionEmpty(TInt aIndex)
00714     {
00715     if ( iGameData.At(aIndex) == EEmptySign )
00716         return ETrue;
00717     return EFalse;
00718     }
00719 
00720 // ----------------------------------------------------
00721 // CIMOpenAPIExamplemodel::PrepareToExit()
00722 // ----------------------------------------------------
00723 //
00724 void CIMOpenAPIExamplemodel::PrepareToExit()
00725     {
00726     if ( (iState != EStateNotLogin) && (iState != EStateLoginIdle) )
00727         SendEndChallenge();
00728     }
00729     
00730 // End of File
00731 

© Nokia 2009

Back to top