S60 3rd Edition SDK for Symbian OS
Example Applications Guide

IMOpenAPIExampleModel.h

00001 /*
00002 * ==============================================================================
00003 *  Name        : IMOpenAPIExampleModel.h
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 
00016 #ifndef __IMOPENAPIEXAMPLEMODEL_H__
00017 #define __IMOPENAPIEXAMPLEMODEL_H__
00018 
00019 // INCLUDES
00020 #include <e32base.h>
00021 #include <e32std.h>
00022 #include <imconnection.h>
00023 #include <imclient.h>
00024 #include "IMOpenAPIExampleDocument.h"
00025 #include "IMOpenAPIExamplemsg.h"
00026 
00027 
00028 // CONSTANTS
00029 // Client ID of this messaging application
00030 _LIT(KClientID,"IMOPENAPIEXAMPLE1");
00031 
00032 // FORWARD DECLARATIONS
00033 class CIMOpenAPIExampleDocument;
00034 class CDesC16Array;
00035 class TTimeIntervalMicroSeconds32;
00036 
00037 
00038 // CLASS DECLARATION
00039 /**
00040  * This class is like an engine to the application. It holds the connection
00041  * and communication to the IM server. It also holds most of the game logic and
00042  * game data. The protocol allows only one outstanding opponent.
00043  */
00044 class CIMOpenAPIExamplemodel :  public CBase,
00045                                                                 public MImObserver,
00046                                                                 public MImConnectionObserver
00047 
00048     {
00049         public: // methods from base classes
00050         
00051         /**
00052         * HandleLoginL.
00053         * From MImConnectionObserver
00054         */
00055                 void HandleLoginL(const TInt aErrorCode);
00056                 
00057         /**
00058         * HandleCancelLoginL.
00059         * From MImConnectionObserver
00060         */
00061                 void HandleCancelLoginL(const TInt aErrorCode);
00062                 
00063         /**
00064         * HandleLogoutL.
00065         * From MImConnectionObserver
00066         */
00067         void HandleLogoutL(const TInt aErrorCode);
00068         
00069         /**
00070         * HandleMessageSentL.
00071         * From MImObserver
00072         */
00073         void HandleMessageSentL(const TInt aOpCode, const TInt aErrorCode );
00074         
00075         /**
00076         * HandleSendErrorL.
00077         * From MImObserver
00078         */
00079         void HandleSendErrorL(const TInt aOpCode, const TInt aErrorCode, 
00080                                                         MImClientDetailedError* aDetailedError);
00081                                                         
00082         /**
00083         * HandleNewPToPMessageL.
00084         * From MImObserver
00085         */
00086         void HandleNewPToPMessageL(     const TInt aErrorCode, 
00087                                                                 const TContactItemId  aContactId,
00088                                                             const TDesC& aUserId,
00089                                                             const TDesC& aMessageType,
00090                                                             const TDesC8& aContent );
00091                 
00092         /**
00093         * HandleNewPToPMessageL.
00094         * From MImObserver
00095         */
00096         void HandleNewPToPMessageL( const TInt aErrorCode, 
00097                                                             const TContactItemId  aContactId,
00098                                                             const TDesC& aUserId,
00099                                                             const TDesC& aMessageType,
00100                                                             const TDesC16& aContent );
00101                                                             
00102                 
00103         public: // own methods
00104         
00105         /**
00106         * LoginL.
00107         * Login request to IM server. This method is Asynchronous. called from
00108         * UI
00109         * @param aServerAddress IM server address
00110         * aUserId user id
00111         * aPassword password
00112         * aAccessPoint access point number as integer
00113         */
00114         void LoginL(const TDesC& aServerAddress, const TDesC& aUserId,
00115                                                         const TDesC& aPassword, const TInt aAccessPoint);
00116                                                                 
00117         /**
00118         * LogoutL.
00119         * Logout request from IM server. Called from UI
00120         */
00121         void LogoutL();
00122         
00123         /**
00124         * SendChallenge.
00125         * Send the challenge to the oponent. Called from UI
00126         */
00127                 void SendChallenge();
00128                 
00129         /**
00130         * SendChallengeAccepted.
00131         * Send that the challenge has been accepted by the UI. Called from UI
00132         */
00133                 void SendChallengeAccepted();
00134                 
00135         /**
00136         * SendChallengeRejected.
00137         * Send that the challenge has been rejected by the UI. Called from UI
00138         */
00139                 void SendChallengeRejected();
00140                 
00141         /**
00142         * SendAlreadyInGame.
00143         * Send that user is already in a game so he/she cant accept messages
00144         * from users other than opponent.
00145         */
00146         void SendAlreadyInGame();
00147         
00148         /**
00149         * SendStartGame.
00150         * Send to the opponent that user has started the game.
00151         */
00152                 void SendStartGame();
00153                 
00154         /**
00155         * SendExitGame.
00156         * Send to the opponent that user has exited the game.
00157         */
00158                 void SendExitGame();
00159                 
00160         /**
00161         * SendPlayedIndex.
00162         * Send to the opponent that user has played on this index in the game
00163         * matrix, so that opponent mark that position
00164         * @param aIndex the location where user mark. 0 to 8
00165         */
00166                 void SendPlayedIndex(TInt aIndex);
00167                 
00168         /**
00169         * SendEndChallenge.
00170         * Send to the opponent that user has ended the challenge. This means
00171         * that user is now free to play with any other users
00172         */
00173                 void SendEndChallenge();
00174                 
00175         /**
00176         * SetOppID.
00177         * Set the opponent's user's id locally
00178         * @param aOppID Opponent's user id
00179         */
00180         void SetOppID(const TDesC16& aOppID);
00181         
00182 
00183         public: // constructor and destructor
00184         
00185         /**
00186         * NewLC.
00187         * Factory method for creating the class instance
00188         * @param aDocument Pointer to the application's document
00189         */
00190         static CIMOpenAPIExamplemodel* NewLC(CIMOpenAPIExampleDocument* aDocument );
00191         
00192         /**
00193         * NewL.
00194         * Factory method for creating the class instance
00195         * @param aDocument Pointer to the application's document
00196         */
00197         static CIMOpenAPIExamplemodel* NewL(CIMOpenAPIExampleDocument* aDocument );
00198         
00199         /**
00200         * ~CIMOpenAPIExamplemodel.
00201         * Destructor
00202         */
00203         ~CIMOpenAPIExamplemodel();
00204         
00205         /**
00206         * GetGameState.
00207         * Get current game state from model.
00208         * @return TGameState current game state
00209         */
00210                 TGameState GetGameState() const;
00211                 
00212         /**
00213         * SetGameState.
00214         * Set current game state
00215         * @param aState current game state
00216         */
00217                 void SetGameState(TGameState const aState);
00218                 
00219         /**
00220         * GetOppSign.
00221         * Get opponent's sign
00222         * @return TGameSign opponent's sign
00223         */
00224         TGameSign GetOppSign() const;
00225         
00226         /**
00227         * IsIndexPositionEmpty.
00228         * Find whether a position on game's grid is empty
00229         * @param aIndex location of position in the grid
00230         * @return TBool ETrue if position is empty
00231         */
00232         TBool IsIndexPositionEmpty(TInt aIndex);
00233         
00234         /**
00235         * PrepareToExit.
00236         * Close connections and prepare to exit application
00237         */
00238         void PrepareToExit();
00239 
00240     private: // own private methods
00241     
00242         /**
00243         * CIMOpenAPIExamplemodel.
00244         * C++ constructor
00245         */
00246         CIMOpenAPIExamplemodel();
00247         
00248         /**
00249         * ConstructL.
00250         * 2nd phase constructor
00251         * @param aDocument Pointer to the application's document
00252         */
00253         void ConstructL(CIMOpenAPIExampleDocument* aDocument);
00254         
00255         /**
00256         * CheckForSeries.
00257         * Check for a series of 3 consective values on the grid. The functions
00258         * checks whether the three positions starting from an starting index 
00259         * and increased by given increament has the same values.
00260         * @param aValue The value which needs to be continuous
00261         * aStartIndex Starting index on the grid
00262         * aIndexIncrement Increament for the next position
00263         * @return ETrue if series found
00264         */
00265                 TBool CheckForSeries(TGameSign aValue, TInt aStartIndex, TInt aIndexIncrement);
00266                 
00267         /**
00268         * MatchOppID.
00269         * Match whether the given id is same as opponent's id, Each message
00270         * goes through this check in a game.
00271         * @param aOppID id to be matched with opponent's id
00272         * @return ETrue if matched
00273         */
00274                 TBool MatchOppID(TDesC16& aOppID);
00275                 
00276         /**
00277         * SendMsg.
00278         * Send given message
00279         * @param aContent The message needs to be send
00280         * @return TInt Message id
00281         */
00282                 TInt SendMsg(TDesC16& aContent);
00283                 
00284         /**
00285         * ResetGameData.
00286         * Resets all game data, usually done before starting a game
00287         */
00288         void ResetGameData();
00289         
00290         /**
00291         * IsGameFinnished.
00292         * Find whether the game is finnished.
00293         * @param aIsDraw ETrue if game is a draw
00294         * @return ETrue if game is finnished
00295         */
00296         TBool IsGameFinnished(TBool& aIsDraw);
00297         
00298         /**
00299         * IsGameDraw.
00300         * Find whether the game has draw.
00301         * @return ETrue if game is draw
00302         */
00303         TBool IsGameDraw();
00304 
00305     private: // data
00306     
00307         /**
00308         * iIMConnection.
00309         * Pointer to CImConnection. Used for IM connection handling
00310         */
00311                 CImConnection*                                  iIMConnection;
00312                 
00313         /**
00314         * iIMClient.
00315         * Pointer to MImClient. Used for IM communication
00316         */
00317         MImClient*                                              iIMClient;
00318         
00319         /**
00320         * iDocument.
00321         * A local copy to the document pointer
00322         */
00323                 CIMOpenAPIExampleDocument*                      iDocument;
00324 
00325         /**
00326         * iGameData.
00327         * This is a 9 member array, represents game data during a game. It can
00328         * have three posible values. 0, 1 and -1. 0 is equivalent to circle
00329         * sign, 1 is to cross and -1 represents an empty location in game grid
00330         */
00331                 TFixedArray<TGameSign,9> iGameData;
00332                 
00333         /**
00334         * iState.
00335         * This is Game state, all othe classes will get the game state from
00336         * here. The TGameState is defined in IMOpenAPIExamplemsg.h
00337         */
00338                 TGameState iState;
00339                 
00340         /**
00341         * iMsgID.
00342         * This is last sent message id
00343         */
00344                 TInt iMsgID;
00345 
00346         /**
00347         * iArrayOfIDs.
00348         * Array of oponent ids, to match with returning messages
00349         */
00350                 CDesC16Array* iArrayOfIDs;
00351 
00352         /**
00353         * iOppID.
00354         * Opponent ID
00355         */
00356         HBufC* iOppID;
00357 
00358         /**
00359         * iOppID.
00360         * ETrue while some message is being send and response didnt yet come
00361         */
00362                 TBool iSending;
00363 
00364         /**
00365         * iOppID.
00366         * Stores last message sent. TMsg is defined in IMOpenAPIExamplemsg.h
00367         */
00368         TMsg iLastMsg;
00369 
00370         /**
00371         * iMySign, iOppSign.
00372         * User's own and opponent's signs. TGameSign is defined in IMOpenAPIExamplemsg.h
00373         */
00374         TGameSign iMySign, iOppSign;
00375         
00376         /**
00377         * iMessageToSend
00378         * Buffer containing message to be sent
00379         */
00380         TBuf<KMaxProtocolStrLength> iMessageToSend;
00381 
00382         /**
00383         * iSeed
00384         * This seed is used to randomize who's turn is first.
00385         */
00386         TInt64 iSeed;
00387         
00388         /**
00389         * iNotSupported
00390         * Whether the IM functionality is supported or not
00391         */
00392         TBool iNotSupported;
00393 
00394     };
00395 
00396 #endif // __IMOPENAPIEXAMPLEMODEL_H__
00397 
00398 // end of file
00399 

© Nokia 2006

Back to top