Auto select Internet Access Point programmatically. We use rgenericagent and cactive to form an engine based on Observer pattern which is most used in Symbian app development.
The following is extracted from my project.
Notes: it is rather simplified and only tested on uiq platform. Use it at your own risk.
1. ipaengine. HRH
# Ifndef _ ipaengine_hrh
# DEFINE _ ipaengine_hrh
Enum tiapconnectenginestates // engine states
{
Eiapconnectstatenotconnected,
Eiapconnectstateconnecting,
Eiapconnectstateconnected,
Eiapconnectstatedisconnecting
};
# Endif
2. iapconnectstateobserver. h
# Ifndef _ iapconnectstateobserver_h
# DEFINE _ iapconnectstateobserver_h
# Include <e32std. h>
Class miapconnectstateobserver
{
Public:
// Notifies the changed state
Virtual void changestatel (tint astate) = 0;
};
# Endif/_ iapconnectstateobserver_h
3. ciapconnectengine. h
# Ifndef _ ciapconnectengine_h
# DEFINE _ ciapconnectengine_h
# Include <e32base. h>
# Include <nifman. h>
# Include <agentclient. h>
# Include "iapconnectstateobserver. H"
// Forward declarations;
Class rgenericagent;
Class cstoreableoverridesettings;
// Constants
Const tuint32 kundefinediapid = 0x00;
Class ciapconnectengine: Public cactive
{
Public:
Static ciapconnectengine * newl (miapconnectstateobserver * aobserver );
Static ciapconnectengine * newlc (miapconnectstateobserver * aobserver );
~ Ciapconnectengine ();
Void connectl ();
Void releasel ();
Void terminateconnectionl ();
Void setconnectionpreferences (tint abearer, tbool adialog, tuint32 aiapid = kundefinediapid );
Protected:
// From cactive
Void runl ();
Void docancel ();
PRIVATE:
// Constructor
Ciapconnectengine (miapconnectstateobserver * aobserver );
Void constructl ();
Tint isdisconnected (tuint32 & aiap );
PRIVATE:
Rgenericagent iconnmgr;
Tint istate;
Ccommsdbconnectionpreftableview: tcommdbiapconnectionpref ipref;
Miapconnectstateobserver * iobserver;
Cstoreableoverridesettings * cs;
};
# Endif
4. ciapconnectengine. cpp
# Include "ciapconnectengine. H"
# Include "iapconnectstateobserver. H"
# Include <eikenv. h>
# Include "ipaengine. HRH"
Const tuint32 kbearersetunknown = 1000;
Ciapconnectengine * ciapconnectengine: newl (miapconnectstateobserver * aobserver)
{
Ciapconnectengine * Self = newlc (aobserver );
Cleanupstack: Pop (Self );
Return self;
}
Ciapconnectengine * ciapconnectengine: newlc (miapconnectstateobserver * aobserver)
{
Ciapconnectengine * Self = new (eleave) ciapconnectengine (aobserver );
Cleanupstack: pushl (Self );
Self-> constructl ();
Return self;
}
Ciapconnectengine: ciapconnectengine (miapconnectstateobserver * aobserver)
: Cactive (cactive: eprioritystandard ),
Istate (eiapconnectstatenotconnected ),
Iobserver (aobserver)
{
Setconnectionpreferences (ecommdbbearerunknown, etrue );
}
Ciapconnectengine ::~ Ciapconnectengine ()
{
Delete Cs;
Cancel ();
Iconnmgr. Close ();
}
Void ciapconnectengine: constructl ()
{
Cs = cstoreableoverridesettings: newl (cstoreableoverridesettings: eparamlistfull );
Iconnmgr. open ();
Cactivescheduler: add (this );
}
Void ciapconnectengine: connectl ()
{
Istate = eiapconnectstateconnecting,
Iobserver-> changestatel (istate );
Tuint32 IAP;
If (! Isdisconnected (IAP ))
{
If (IAP = ipref. ibearer. iiapid)
{
Istate = eiapconnectstateconnected;
Iobserver-> changestatel (istate );
Return;
} Else
Terminateconnectionl ();
}
If (! Isactive ())
{
Istatus = krequestpending;
CS-> setconnectionpreferenceoverride (ipref );
Iconnmgr. startoutgoing (* cs, istatus );
Setactive ();
}
}
Void ciapconnectengine: runl ()
{
If (istatus = kerrnone)
{
Istate = eiapconnectstateconnected;
Iobserver-> changestatel (istate );
}
}
Void ciapconnectengine: docancel ()
{
Istate = eiapconnectstatenotconnected;
Iobserver-> changestatel (istate );
}
Void ciapconnectengine: releasel ()
{
Tuint32 IAP; // not used
If (! Isdisconnected (IAP ))
{
Cancel ();
Istate = eiapconnectstatenotconnected;
Iobserver-> changestatel (istate );
}
}
Void ciapconnectengine: terminateconnectionl ()
{
Tuint32 IAP;
If (! Isdisconnected (IAP ))
{
User: leaveiferror (iconnmgr. Stop ());
Istate = eiapconnectstatenotconnected;
Iobserver-> changestatel (istate );
}
}
Tint ciapconnectengine: isdisconnected (tuint32 & aiap)
{
Return iconnmgr. getactiveintsetting (tptrc (IAP), tptrc (commdb_id), aiap );
}
Void ciapconnectengine: setconnectionpreferences (tint abearer,
Tbool adialog, tuint32 aiapid)
{
Ipref. iranking = 1;
Ipref. idirection = ecommdbconnectiondirectionoutgoing;
Adialog? Ipref. idialogpref = ecommdbdialogprefprompt: ipref. idialogpref = ecommdbdialogprefdonotprompt;
Ccommsdbconnectionpreftableview: tcommdbiapbearer bearer;
Bearer. ibearerset = abearer;
Bearer. iiapid = aiapid;
Ipref. ibearer = bearer;
}