📄 sabberappui.cpp
字号:
/*
* ============================================================================
* Name : CSabberAppUi from SabberAppUi.cpp
* Part of : Sabber
* Created : 2004-12-9 by Xie Tian Lu
* Implementation notes:
* Initial content was generated by Series 60 AppWizard.
* Version : 0.1
* Copyright: (c) Xie Tian Lu Http://sabber.jabberstudio.org/
* ============================================================================
*/
// INCLUDE FILES
#include <avkon.hrh>
#include <Sabber.rsg>
#include "SabberAppUi.h"
#include "SabberRosterView.h"
#include "SabberMessageView.h"
#include "sabber.hrh"
#include "osaux.h"
#include "osdlg.h"
#include "iconset.h"
#include "symsock.h"
#include "eventhandler.h"
#include "roster.h"
#include "IAPConnectEngine.h"
#include "RosterItem.h"
#include "RosterModel.h"
#include "MessageItem.h"
#include "MessageModel.h"
#include "ListBoxIcon.h"
#include "ftrans.h"
_LIT(KArialFont,"Arial");
const TInt KPollTimerInterval = 100000;
#define STR_JABBER_RESOURCE "/mobile"
#define STR_DEFAULT_GROUP "Unfiled"
// ================= MEMBER FUNCTIONS =======================
//
// ----------------------------------------------------------
// CSabberAppUi::ConstructL()
// ?implementation_description
// ----------------------------------------------------------
//
void CSabberAppUi::ConstructL()
{
BaseConstructL();
iTime = new (ELeave) TTime;
iSettingData = CSettingData::NewL();
iSettingData->Load();
iIconSet = new CIconSet( KListBoxIconSetFile, KListIconNum, iEikonEnv );
iFont = CUtil::GetFont( KArialFont, 600 );
// Show tabs for main views from resources
CEikStatusPane* sp = StatusPane();
// Fetch pointer to the default navi pane control
iNaviPane = (CAknNavigationControlContainer*)sp->ControlL(
TUid::Uid(EEikStatusPaneUidNavi));
// Tabgroup has been read from resource and it were pushed to the navi pane.
// Get pointer to the navigation decorator with the ResourceDecorator() function.
// Application owns the decorator and it has responsibility to delete the object.
iDecoratedTabGroup = iNaviPane->ResourceDecorator();
if (iDecoratedTabGroup)
{
iTabGroup = (CAknTabGroup*) iDecoratedTabGroup->DecoratedControl();
}
iRosterView = new (ELeave) CSabberRosterView;
CleanupStack::PushL( iRosterView );
iRosterView->SetFont( iFont );
iRosterView->ConstructL();
iRosterView->SetSettingData( iSettingData );
AddViewL( iRosterView ); // transfer ownership to CAknViewAppUi
CleanupStack::Pop();
iMessageView = new (ELeave) CSabberMessageView;
CleanupStack::PushL( iMessageView );
iMessageView->SetFont( iFont );
iMessageView->ConstructL();
AddViewL( iMessageView ); // transfer ownership to CAknViewAppUi
CleanupStack::Pop();
SetDefaultViewL(*iRosterView);
iGData = globaldata_new();
iGData->_ui = this;
const TInt KCacheBufSize = 16384;
iJabSock = CSymSocket::NewL( this, KCacheBufSize, KCacheBufSize );
iGData->_symsock = iJabSock;
iIap = CIAPConnectEngine::NewL( this );
iIapConnected = EFalse;
iJabber = 0;
iPollTimer = CUserTimer::NewL( KPollTimerInterval, this );
}
// ----------------------------------------------------
// CSabberAppUi::~CSabberAppUi()
// Destructor
// Frees reserved resources
// ----------------------------------------------------
//
CSabberAppUi::~CSabberAppUi()
{
delete iPollTimer;
jab_delete( iJabber );
delete iIap;
delete iJabSock;
iGData->_symsock = 0;
globaldata_delete( iGData );
delete iDecoratedTabGroup;
delete iIconSet;
delete iSettingData;
delete iTime;
}
// ------------------------------------------------------------------------------
// CSabberAppUi::::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
// This function is called by the EIKON framework just before it displays
// a menu pane. Its default implementation is empty, and by overriding it,
// the application can set the state of menu items dynamically according
// to the state of application data.
// ------------------------------------------------------------------------------
//
void CSabberAppUi::DynInitMenuPaneL(
TInt /*aResourceId*/,CEikMenuPane* /*aMenuPane*/)
{
}
// ----------------------------------------------------
// CSabberAppUi::HandleKeyEventL(
// const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
// ?implementation_description
// ----------------------------------------------------
//
TKeyResponse CSabberAppUi::HandleKeyEventL(
const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
{
if ( iTabGroup == NULL )
{
return EKeyWasNotConsumed;
}
TInt active = iTabGroup->ActiveTabIndex();
TInt count = iTabGroup->TabCount();
switch ( aKeyEvent.iCode )
{
case EKeyLeftArrow:
if ( active > 0 )
{
active--;
iTabGroup->SetActiveTabByIndex( active );
ActivateLocalViewL(TUid::Uid(iTabGroup->TabIdFromIndex(active)));
}
break;
case EKeyRightArrow:
if( (active + 1) < count )
{
active++;
iTabGroup->SetActiveTabByIndex( active );
ActivateLocalViewL(TUid::Uid(iTabGroup->TabIdFromIndex(active)));
}
break;
default:
return EKeyWasNotConsumed;
break;
}
return EKeyWasConsumed;
}
// ----------------------------------------------------
// CSabberAppUi::HandleCommandL(TInt aCommand)
// ?implementation_description
// ----------------------------------------------------
//
void CSabberAppUi::HandleCommandL(TInt aCommand)
{
switch ( aCommand )
{
case EEikCmdExit:
{
Exit();
break;
}
case ESabberLogon:
{
if ( iState == CON_STATE_OFF )
Logon();
}
break;
case ESabberLogoff:
{
if ( iState != CON_STATE_OFF )
Logoff();
}
break;
case ESabberFree:
ChangePresence( CS_CHAT );
break;
case ESabberHide:
ChangePresence( CS_XA );
break;
case ESabberBusy:
ChangePresence( CS_DND );
break;
case ESabberAway:
ChangePresence( CS_AWAY );
break;
case ESabberAbout:
ShowConfirmDialogL( R_BUF_ABOUT_SABBER );
break;
default:
break;
}
}
void CSabberAppUi::OnTimer( CUserTimer* aTimer )
{
TInt interval = KPollTimerInterval;
if ( aTimer == iPollTimer ) {
switch ( iSockState ) {
case EConnected :
if ( iJabber->state == JCONN_STATE_CONNECTING ) {
jab_start( iJabber );
}
if ( iState == CON_STATE_LOGGED_IN ) {
}
if ( iState == CON_STATE_AUTHFAILED ) {
jab_stop( iJabber );
}
jab_poll( iJabber, -1 );
break;
case EConnectFailed:
jab_stop( iJabber );
break;
}
iPollTimer->Queue( interval );
}
}
void CSabberAppUi::Notify( TSymSocketState aState )
{
switch ( aState )
{
case EConnected:
iSockState = aState;
break;
default:
break;
}
}
void CSabberAppUi::IAPStateChangedL( TInt aState )
{
switch (aState)
{
case EIAPConnectStateConnected:
if ( !iIapConnected ) {
iSockState = EConnecting;
jab_socket( iJabber );
//SetCurState( CON_STATE_CONNECTING );
}
iIapConnected = ETrue;
iSettingData->iIapId = iIap->iIapId;
iSettingData->Save();
//iAddrView->iSettingListBox->ValidateIapSelection( _iap->iIapId );
break;
case EIAPConnectStateConnecting:
case EIAPConnectStateRetry:
break;
case EIAPConnectStateNotConnected:
default:
//SYS_TRACE1( 0, "Access GPRS error" );
break;
}
}
void CSabberAppUi::ActivateView( TInt index )
{
iTabGroup->SetActiveTabByIndex( index );
ActivateLocalViewL(TUid::Uid(iTabGroup->TabIdFromIndex(index)));
}
void CSabberAppUi::GetTimeStamp( TDes& stamp )
{
_LIT( KDataTimeFormat,"%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B" );
//_LIT( KDataTimeFormat, "%:0%H%:1%T%:2%S" );
iTime->HomeTime();
iTime->FormatL( stamp, KDataTimeFormat );
}
void CSabberAppUi::RetrieveJid()
{
char jid[ 128 ];
CUtil::Unicode2UTF8( jid, iSettingData->iUserID );
strcat( jid, STR_JABBER_RESOURCE );
strcpy( iJid, jid );
}
void CSabberAppUi::RetrievePswd()
{
char pswd[ 32 ];
CUtil::Unicode2UTF8( pswd, iSettingData->iUserPassword );
strcpy( iPswd, pswd );
}
void CSabberAppUi::CheckJidChanged()
{
iJidChanged = EFalse;
char jid[ 128 ];
CUtil::Unicode2UTF8( jid, iSettingData->iUserID );
strcat( jid, STR_JABBER_RESOURCE );
if ( strcmp( iJid, jid ) != 0 ) {
iJidChanged = ETrue;
strcpy( iJid, jid );
iSettingData->Save();
}
}
void CSabberAppUi::CheckPswdChanged()
{
iPswdChanged = EFalse;
char pswd[ 32 ];
CUtil::Unicode2UTF8( pswd, iSettingData->iUserPassword );
if ( strcmp( pswd, iPswd ) != 0 ) {
iPswdChanged = ETrue;
strcpy( iPswd, pswd );
iSettingData->Save();
}
}
void CSabberAppUi::ConnectServer()
{
if ( !iJabber )
return;
#ifndef __WINS__
if ( !iIapConnected )
{
TUint32 iapid = iSettingData->iIapId;
if ( iapid == 0 )
iIap->SetConnectionPreferences( ECommDbBearerUnknown, ETrue, iapid );
else
iIap->SetConnectionPreferences( ECommDbBearerUnknown, EFalse, iapid );
iIap->ConnectL();
}
else
#endif
{
iSockState = EConnecting;
jab_socket( iJabber );
}
}
void CSabberAppUi::ConnectTimeout()
{
iSockState = EConnectFailed;
SetCurState( CON_STATE_OFF );
//_reconnect = 0;
SYS_TRACE1( 0, "Connect to server timeout!" );
}
void CSabberAppUi::ConnectFailed()
{
iSockState = EConnectFailed;
SetCurState( CON_STATE_OFF );
//_reconnect = 0;
SYS_TRACE1( 0, "Can not connect to server!" );
}
void CSabberAppUi::Register()
{
jab_reg( iJabber );
//_regist = CON_STATE_REGISTING;
}
void CSabberAppUi::RegisterFailed()
{
//_reconnect = 0;
SetCurState( CON_STATE_OFF );
SYS_TRACE1( 0, "Register a new account failed!" );
}
void CSabberAppUi::Logon()
{
CheckJidChanged();
CheckPswdChanged();
if ( 0 == strcmp( iJid, "" ) || 0 == strcmp( iPswd, "" ) ) {
ShowConfirmDialogL( R_BUF_NEED_JID );
return;
}
if ( iJidChanged || iPswdChanged ) {
//iAddrModel->Init();
//iAddrView->Update();
jab_delete( iJabber );
iJabber = 0;
}
if ( !iJabber ) {
iJabber = jab_new( iJid, iPswd, iSettingData->iPort, iGData );
SYS_ASSERT( iJabber != NULL );
jab_state_handler ( iJabber, jconn_state_handler );
jab_packet_handler( iJabber, jconn_packet_handler );
//_regist = CON_STATE_NOTREGISTED;
}
if ( iJabber->state != JCONN_STATE_OFF ) {
jab_stop( iJabber );
}
ConnectServer();
}
void CSabberAppUi::Logoff()
{
iSockState = ESocketUnknown;
jab_stop( iJabber );
}
void CSabberAppUi::LogonFailed()
{
SetCurState( CON_STATE_AUTHFAILED );
//if ( _regist == CON_STATE_REGISTED ) {
// _reconnect = 0;
SYS_TRACE1( 0, "Can't get authoriziton" );
//} else {
// Register();
//}
}
void CSabberAppUi::GetAuthorized()
{
//if ( _regist == CON_STATE_REGISTING ) {
// jab_auth( _jabber );
//} else {
SetCurState( CON_STATE_LOGGED_IN );
jabr_request( iJabber );
ChangePresence( CS_ONLINE );
//}
//_regist = CON_STATE_REGISTED;
}
void CSabberAppUi::SetCurState( int state )
{
if ( JCONN_STATE_OFF == state ) {
iPresence = CS_OFFLINE;
iSockState = ESocketUnknown;
} else if ( JCONN_STATE_CONNECTED == state ) {
}
//SetContextIcon( state );
//UpdateView();
iState = ( EJabberState )state;
}
int CSabberAppUi::GetCurState()
{
return iState;
}
void CSabberAppUi::ChangePresence( int s )
{
if ( !iJabber )
return;
switch ( s ) {
case CS_OFFLINE:
jabr_change_status( iJabber, STR_CS_OFFLINE, 0 );
break;
case CS_ONLINE:
case CS_CHAT:
jabr_change_status( iJabber, STR_SHOW_CHAT, 0 );
break;
case CS_AWAY:
jabr_change_status( iJabber, STR_SHOW_AWAY, 0 );
break;
case CS_DND:
jabr_change_status( iJabber, STR_SHOW_DND, 0 );
break;
case CS_XA:
jabr_change_status( iJabber, STR_SHOW_XA, 0 );
break;
}
iPresence = s;
}
TUint CSabberAppUi::GetPresence()
{
return iPresence;
}
void CSabberAppUi::RecvMsg ( char* body, char* from, char* to )
{
CMessageModel* model = iMessageView->GetMessageModel();
CMessageItem* item = CMessageItem::CreateMessageItem( from, to, body, ETrue );
item->SetIconSet( iIconSet );
model->AddItem( item );
}
void CSabberAppUi::RecvRoster( char* jid, char* nick, char* group, char* subs )
{
if ( subs && 0 == strcmp( subs, "from" ) ) {
jabr_subscribe_friend( iJabber, jid, 1 );
}
if ( !group )
group = STR_DEFAULT_GROUP;
CRosterModel* model = iRosterView->GetRosterModel();
CRosterItem* item;
for ( int i = 0; i < model->NumberOfItems(); i++ ) {
item = ( CRosterItem* )model->GetItemAt( i );
if ( item->EqualTo( jid ) )
return;
}
item = CRosterItem::CreateRosterItem( jid, nick, group, EJabberUser );
item->SetIconSet( iIconSet );
model->AddItem( item );
iRosterView->Update();
}
void CSabberAppUi::RecvPresence( char* jid, char* show, char* status, int on )
{
CRosterModel* model = iRosterView->GetRosterModel();
model->CheckPresence( jid, show, status, on );
iRosterView->Update();
}
void CSabberAppUi::SendMsg( char* body, char* to )
{
if ( iState == CON_STATE_OFF ) {
SYS_TRACE1( 0, "Please logon first!" );
return;
}
jabm_send_msg( iJabber, to, body );
CMessageModel* model = iMessageView->GetMessageModel();
CMessageItem* item = CMessageItem::CreateMessageItem( iJid, to, body, EFalse );
item->SetIconSet( iIconSet );
model->AddItem( item );
iMessageView->Update();
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -