?? taskmanagerengine.cpp
字號(hào):
/*
* ============================================================================
* Name : CTaskManagerEngine from TaskManagerEngine.cpp
* Part of : TaskManager
* Created : 03/13/2005 by Forum Nokia
* Version : 1.2
* Copyright: Nokia Corporation
* ============================================================================
*/
// INCLUDE FILES
#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 "TaskManagerEngine.h"
#include "TaskManager.pan"
#include "Response.h"
#include "TaskManagerConnectionOpener.h"
#include <HttpTaskManager.rsg>
// CONSTANTS
_LIT8(KUserAgent, "TaskManager 1.0");
_LIT8(KAccept, "*/*");
_LIT8(KUsername, "username");
_LIT8(KPassword, "password");
_LIT8(KTaskId, "task_id");
_LIT8(KContentTypeForm, "application/x-www-form-urlencoded\0");
_LIT8(KPhpUrlGetTasksFormat, "https://%S/get_tasks.php");
_LIT8(KPhpUrlSetTaskDoneFormat, "https://%S/set_task_done.php");
_LIT8(KJspUrlGetTasksFormat, "https://%S/TaskManagerWebUI/Controller?action=GetTasksAction");
_LIT8(KJspUrlSetTaskDoneFormat, "https://%S/TaskManagerWebUI/Controller?action=SetTaskDoneAction");
_LIT(KUserNotSet, "Set username and/or password");
_LIT(KIapNotSet, "IAP not selected");
_LIT(KServerNotSet, "Server name not set");
_LIT(KSmsUpdateMessage, "TaskManagerUpdate");
// ================= MEMBER FUNCTIONS =======================
// constructor
CTaskManagerEngine::CTaskManagerEngine(MTransactionObserver& aObserver)
: iTransactionObserver(aObserver)
{
}
// destructor
CTaskManagerEngine::~CTaskManagerEngine()
{
delete iConnOpener;
iHttpSession.Close();
iConnection.Close();
iSockServ.Close();
delete iGetTasksForm;
delete iMarkTaskDoneForm;
delete iMsvEntry;
delete iMsvSession;
iIAPs.Close();
ResetData();
}
// ----------------------------------------------------
// CTaskManagerEngine::NewL()
// Two-phased constructor.
// ----------------------------------------------------
//
CTaskManagerEngine* CTaskManagerEngine::NewL(MTransactionObserver& aObserver)
{
CTaskManagerEngine* self = new (ELeave) CTaskManagerEngine(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
// ----------------------------------------------------
// CTaskManagerEngine::ConstructL()
// Symbian OS default constructor can leave.
// ----------------------------------------------------
//
void CTaskManagerEngine::ConstructL()
{
User::LeaveIfError(iSockServ.Connect());
User::LeaveIfError(iConnection.Open(iSockServ));
iConnOpener = CTaskManagerConnectionOpener::NewL(iConnection,*this);
iMsvSession = CMsvSession::OpenAsyncL(*this);
// open RHTTPSession with default protocol ("HTTP/TCP")
iHttpSession.OpenL();
// show a dialog when when server certificate needs to be accepted.
RStringPool stringPool = iHttpSession.StringPool();
RHTTPConnectionInfo connInfo = iHttpSession.ConnectionInfo();
connInfo.SetPropertyL(stringPool.StringF(HTTP::ESecureDialog, RHTTPSession::GetTable()), THTTPHdrVal(stringPool.StringF(HTTP::EDialogPrompt, RHTTPSession::GetTable())));
LoadIapsL();
// no IAPs defined in the device.
if (iIAPs.Count() == 0)
{
CEikonEnv::Static()->LeaveWithInfoMsg(R_TASKMANAGER_NO_IAPS_DEFINED);
}
}
// ----------------------------------------------------
// CTaskManagerEngine::SetConnectionSettingsL()
// Sets all connections settings. If username and
// password are set, a "HTML form" is created. This
// form is used for fetching the tasks from the server.
// ----------------------------------------------------
//
void CTaskManagerEngine::SetConnectionSettingsL(const TDesC& aServerName,
const TBool& aPhp,
const TDesC& aUsername,
const TDesC& aPassword)
{
iUsername.Copy(aUsername);
iPassword.Copy(aPassword);
iPhp = aPhp;
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);
}
}
// ----------------------------------------------------
// CTaskManagerEngine::SetIap()
// Sets the IAP to be used in the HTTP connections.
// ----------------------------------------------------
//
void CTaskManagerEngine::SetIap(const TUint32& aId)
{
iIap = aId;
}
// ----------------------------------------------------
// CTaskManagerEngine::IapSet()
// Returns ETrue if the IAP is set, EFalse if not.
// ----------------------------------------------------
//
TBool CTaskManagerEngine::IapSet() const
{
if (iIap == 0)
{
return EFalse;
}
else
{
return ETrue;
}
}
// ----------------------------------------------------
// CTaskManagerEngine::SetHeaderL()
// Sets a field-value pair to an HTTP header.
// ----------------------------------------------------
//
void CTaskManagerEngine::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);
}
// ----------------------------------------------------
// CTaskManagerEngine::CheckAndReportErrorsL()
// Checks that all necessary connections settings are
// set and reports from missing data.
// ----------------------------------------------------
//
TBool CTaskManagerEngine::CheckAndReportErrorsL()
{
if (!IapSet())
{
iTransactionObserver.ErrorL(KIapNotSet);
return ETrue;
}
if (iServer.Length() == 0)
{
iTransactionObserver.ErrorL(KServerNotSet);
return ETrue;
}
return EFalse;
}
// ----------------------------------------------------
// CTaskManagerEngine::FetchTasksL()
// Starts fetching tasks from the server.
// ----------------------------------------------------
//
void CTaskManagerEngine::FetchTasksL()
{
// tasks form has not been created, username and/or password is not set.
if (iGetTasksForm == NULL)
{
iTransactionObserver.ErrorL(KUserNotSet);
return;
}
// if connection settings are invalid, just return
if (CheckAndReportErrorsL())
{
return;
}
// reset all previously received data.
ResetData();
if (iPhp)
{
iUrl.Format(KPhpUrlGetTasksFormat, &iServer);
}
else
{
iUrl.Format(KJspUrlGetTasksFormat, &iServer);
}
iHttpForm = iGetTasksForm;
// open up a (GPRS) connection and post the form.
ConnectL();
}
// ----------------------------------------------------
// CTaskManagerEngine::CreateMarkTaskDoneFormL()
// Creates an HTML form that can be used for completing
// tasks in the server.
// ----------------------------------------------------
//
void CTaskManagerEngine::CreateMarkTaskDoneFormL(const TInt& aTaskId)
{
delete iMarkTaskDoneForm;
iMarkTaskDoneForm = NULL;
// username and/or password not set
if (1 > iUsername.Length() || 1 > iPassword.Length())
{
iTransactionObserver.ErrorL(KUserNotSet);
return;
}
// reset all previously received data.
ResetData();
// create the form
iMarkTaskDoneForm = CHTTPFormEncoder::NewL();
iMarkTaskDoneForm->AddFieldL(KUsername, iUsername);
iMarkTaskDoneForm->AddFieldL(KPassword, iPassword);
TBuf8<6> id;
_LIT8(KIdFormat, "%d");
id.Format(KIdFormat, aTaskId);
iMarkTaskDoneForm->AddFieldL(KTaskId, id);
}
// ----------------------------------------------------
// CTaskManagerEngine::MarkTaskDoneL()
// Will post an HTML form to the server, which will mark
// a task as completed in the server database.
// ----------------------------------------------------
//
void CTaskManagerEngine::MarkTaskDoneL(const TInt& aTaskId)
{
CreateMarkTaskDoneFormL(aTaskId);
if (iMarkTaskDoneForm == NULL)
{
return;
}
// if connection settings are invalid, just return
if (CheckAndReportErrorsL())
{
return;
}
if (iPhp)
{
iUrl.Format(KPhpUrlSetTaskDoneFormat, &iServer);
}
else
{
iUrl.Format(KJspUrlSetTaskDoneFormat, &iServer);
}
iHttpForm = iMarkTaskDoneForm;
// open up a (GPRS) connection and post the form.
ConnectL();
}
// ----------------------------------------------------
// CTaskManagerEngine::DoPostL()
// Posts the defined HTML form to the server.
// ----------------------------------------------------
//
void CTaskManagerEngine::DoPostL()
{
TUriParser8 uri;
User::LeaveIfError(uri.Parse(iUrl));
// Get request method string for HTTP POST
RStringF method = iHttpSession.StringPool().StringF(HTTP::EPOST, RHTTPSession::GetTable());
// Open transaction with previous method and parsed uri. This class will
// receive transaction events in MHFRunL and MHFRunError.
iTransaction = iHttpSession.OpenTransactionL(uri, *this, method);
// Set headers for request; user agent, accepted content type and body's
// content type.
RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
SetHeaderL(hdr, HTTP::EAccept, KAccept);
SetHeaderL(hdr, HTTP::EContentType, KContentTypeForm);
iTransaction.Request().SetBody(*iHttpForm);
// Submit the transaction. After this the framework will give transaction
// events via MHFRunL and MHFRunError.
iTransaction.SubmitL();
iRunning = ETrue;
if (iHttpForm == iGetTasksForm)
{
iTransactionObserver.ConnectingToServerL(ETrue);
}
else
{
iTransactionObserver.ConnectingToServerL(EFalse);
}
}
// ----------------------------------------------------
// CTaskManagerEngine::MHFRunL()
// Called when the client's registration conditions are
// satisfied for events that occur on a transaction.
// ----------------------------------------------------
//
void CTaskManagerEngine::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
{
switch (aEvent.iStatus)
{
case THTTPEvent::EGotResponseHeaders:
{
// HTTP response headers have been received. Use
// aTransaction.Response() to get the response. However, it's not
// necessary to do anything with the response when this event occurs.
}
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;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -