Code:
/*
// INCLUDE FILES
// Class include
#include <HttpStringConstants.h>// HTTP
#include <msvids.h> // KMsvGlobalInBoxIndexEntryId
#include <txtrich.h> // CRichText
#include <mtclreg.h> // CClientMtmRegistry
#include <mtclbase.h> // CBaseMtm
#include <commdb.h> // CCommsDatabase
#include <commdbconnpref.h> // TCommDbConnPref
#include <es_enum.h> // TConnectionInfoV2Buf
#include <eikenv.h>
#include "PMHttpEngine.h"
#include "PM.pan"
#include <AknexGrid.rsg>
#include "Response.h"
#include "PMConnectionOpener.h"
//Constants
_LIT8(KUserAgent, "..");
_LIT8(KAccept, "*/*");
_LIT8(KContentTypeForm, "application/x-www-form-urlencoded\0");
//_LIT8(KUrlFetchStocks, "http://%S/symbiantest.asp");
_LIT8(KUrlFetchStocks, "http://..");
_LIT8(KUrlFetchTicker, "http://..");
_LIT8(KUsername, "username");
_LIT8(KPassword, "password");
_LIT(KIapNotSet, "IAP not selected");
_LIT(KServerNotSet, "Server name not set");
_LIT(KUserNotSet, "Set username and/or password");
_LIT(KSmsUpdateMessage, "PortfolioManagerUpdate");
// ================= MEMBER FUNCTIONS =======================
CPMHttpEngine* CPMHttpEngine::NewL()
{
CPMHttpEngine* self = new (ELeave) CPMHttpEngine();
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
void CPMHttpEngine::ConstructL()
{
User::LeaveIfError(iSockServ.Connect());
User::LeaveIfError(iConnection.Open(iSockServ));
iConnOpener = CPMConnectionOpener::NewL(iConnection,*this);
iMsvSession = CMsvSession::OpenAsyncL(*this);
iHttpSession.OpenL();
LoadIapsL();
// no IAPs defined in the device.
if (iIAPs.Count() == 0)
{
CEikonEnv::Static()->LeaveWithInfoMsg(R_PM_NO_IAPS_DEFINED);
}
}
void CPMHttpEngine::SetTransactionObserver(MTransactionObserver *aObserver)
{
iTransactionObserver=aObserver;
}
void CPMHttpEngine::SetConnectionSettingsL(const TDesC& aServerName,
const TDesC& aUsername,
const TDesC& aPassword)
{
iUsername.Copy(aUsername);
iPassword.Copy(aPassword);
iServer.Copy(aServerName);
delete iGetTasksForm;
iGetTasksForm = NULL;
// if username and password are set, create an "HTML form".
if (0 < iUsername.Length() && 0 < aPassword.Length())
{
iGetTasksForm = CHTTPFormEncoder::NewL();
iGetTasksForm->AddFieldL(KUsername, iUsername);
iGetTasksForm->AddFieldL(KPassword, iPassword);
}
}
void CPMHttpEngine::SetIap(const TUint32& aId)
{
iIap = aId;
}
TBool CPMHttpEngine::IapSet() const
{
if (iIap == 0)
{
return EFalse;
}
else
{
return ETrue;
}
}
void CPMHttpEngine::SetHeaderL(RHTTPHeaders aHeaders,
TInt aHdrField,
const TDesC8& aHdrValue)
{
RStringF valStr = iHttpSession.StringPool().OpenFStringL(aHdrValue);
CleanupClosePushL(valStr);
THTTPHdrVal val(valStr);
aHeaders.SetFieldL(iHttpSession.StringPool().StringF(aHdrField, RHTTPSession::GetTable()), val);
CleanupStack::PopAndDestroy(&valStr);
}
TBool CPMHttpEngine::CheckAndReportErrorsL()
{
if (!IapSet())
{
iTransactionObserver->ErrorL(KIapNotSet);
return ETrue;
}
if (iServer.Length() == 0)
{
iTransactionObserver->ErrorL(KServerNotSet);
return ETrue;
}
return EFalse;
}
void CPMHttpEngine::FetchStocksL()
{
// Till Here
ResetData();
iUrl.Format(KUrlFetchStocks, &iServer);
iMode=1;
iHttpForm = iGetTasksForm;
// open up a (GPRS) connection and post the form.
ConnectL();
}
void CPMHttpEngine::FetchTickerL()
{
// Till Here
ResetData();
iUrl.Format(KUrlFetchTicker, &iServer);
iMode=0;
iHttpForm = iGetTasksForm;
// open up a (GPRS) connection and post the form.
ConnectL();
}
void CPMHttpEngine::ConnectL()
{
// this functionality is not applicable for the emulator
#ifndef __WINS__
TBool connected = EFalse;
// Lets first check are we already connected.
TUint connectionCount;
User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));
TPckgBuf<TConnectionInfoV2> connectionInfo;
for (TUint i = 1; i <= connectionCount; i++)
{
User::LeaveIfError(iConnection.GetConnectionInfo(i, connectionInfo));
if (connectionInfo().iIapId == iIap)
{
connected = ETrue;
break;
}
}
// Not yet connected, start connection
if (!connected)
{
//Define preferences for connection
TCommDbConnPref prefs;
prefs.SetIapId(iIap);
prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
//Start Connection
iOpeningConnection = ETrue;
iTransactionObserver->OpeningConnectionL();
iConnOpener->OpenConnection(prefs);
return;
}
//Set properties for the HTTP session
RStringPool strP = iHttpSession.StringPool();
RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), THTTPHdrVal (iSockServ.Handle()) );
TInt connPtr = REINTERPRET_CAST(TInt, &iConnection);
connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable() ), THTTPHdrVal (connPtr) );
#endif // __WINS__
DoPostL();
}
void CPMHttpEngine::ConnectionCreated(const TInt& aError)
{
iOpeningConnection = EFalse;
if (aError != KErrNone)
{
TRAPD(error, iTransactionObserver->FailedL(aError))
PanicIfError(error);
}
else
{
TRAPD(error,
//Set properties for the HTTP session
RStringPool strP = iHttpSession.StringPool();
RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable() ), THTTPHdrVal (iSockServ.Handle()) );
TInt connPtr = REINTERPRET_CAST(TInt, &iConnection);
connInfo.SetPropertyL ( strP.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable() ), THTTPHdrVal (connPtr) );
DoPostL();
)
PanicIfError(error);
}
}
void CPMHttpEngine::DoPostL(/*const TDesC& connectUrl*/)
{
/*TBuf8<connectUrl.Length()> buf8;
buf8.Copy(connectUrl);*/
TUriParser8 uri;
// User::LeaveIfError(uri.Parse(buf8));
User::LeaveIfError(uri.Parse(iUrl));
RStringF method = iHttpSession.StringPool().StringF(HTTP::EGET, RHTTPSession::GetTable());
iTransaction = iHttpSession.OpenTransactionL(uri, *this, method);
RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
SetHeaderL(hdr, HTTP::EAccept, KAccept);
iTransaction.SubmitL();
iRunning = ETrue;
if (iHttpForm == iGetTasksForm)
{
iTransactionObserver->ConnectingToServerL(ETrue);
}
else
{
iTransactionObserver->ConnectingToServerL(EFalse);
}
}
void CPMHttpEngine::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
{
////////////////////////Error here///////////////
switch (aEvent.iStatus)
{
case THTTPEvent::EGotResponseHeaders:
{
RHTTPResponse resp = aTransaction.Response();
TInt status = resp.StatusCode();
RStringF statusStr = resp.StatusText();
TBuf<32> statusStr16;
statusStr16.Copy(statusStr.DesC());
// iUtils.Test().Printf(_L("Status: %d (%S)\n"), status, &statusStr16); *** Shrikanta ***
}
break;
case THTTPEvent::EGotResponseBodyData:
{
MHTTPDataSupplier* body = aTransaction.Response().Body();
TPtrC8 dataChunk;
body->GetNextDataPart(dataChunk);
body->ReleaseData();
WriteDataL(dataChunk);
}
break;
// Indicates that header & body of response is completely received.
case THTTPEvent::EResponseComplete:
{
}
break;
// Indicates that transaction succeeded.
case THTTPEvent::ESucceeded:
{
iTransaction.Close();
iRunning = EFalse;
CResponse* response = CResponse::NewLC(iMode);
TRAPD(error,
response->InputDataL(*iReceivedData);
iTransactionObserver->SuccessL(*response);
CheckRefreshL();)
PanicIfError(error);
CleanupStack::PopAndDestroy(response);
}
break;
// Transaction completed with failure.
case THTTPEvent::EFailed:
{
iTransaction.Close();
iRunning = EFalse;
// After transaction has been closed, leaves are no
// longer handled by the calling active object.
TRAPD(error,
iTransactionObserver->FailedL(0);
CheckRefreshL();)
PanicIfError(error);
}
break;
default:
{
if (aEvent.iStatus < 0)
{
iTransaction.Close();
iRunning = EFalse;
TRAPD(error,
iTransactionObserver->FailedL(aEvent.iStatus);
CheckRefreshL();)
PanicIfError(error);
}
else
{
// Other events are not errors (e.g. permanent and temporary
// redirections)
}
}
break;
}
}
TInt CPMHttpEngine::MHFRunError( TInt aError,
RHTTPTransaction /*aTransaction*/,
const THTTPEvent& /*aEvent*/)
{
// only report about the error.
TRAPD(error, iTransactionObserver->FailedL(aError);)
return error;
}