📄 httpmanager.cpp
字号:
#include "HttpManager.h"
#include "MEM_FREE.h"
#include "Pbengine.h"
#include <HelperGenius.rsg>
#include <AknWaitDialog.h>
#include <utf.h >
_LIT8(KPostUri, "http://www.ldci.mobi/snsProject/update");
_LIT(KHEAD,"<Rss>");
_LIT(KEND,"</Rss>");
_LIT(KRECORD,"<record>");
_LIT(KRECORDEND,"</record>");
_LIT(KLNAME, "<lname>");
_LIT(KLNAMEEND, "</lname>");
_LIT(KFNAME, "<fname>");
_LIT(KFNAMEEND, "</fname>");
_LIT(KMOBILE, "<mob>");
_LIT(KMOBILEEND, "</mob>");
_LIT(KEMAIL, "<mail>");
_LIT(KEMAILEND, "</mail>");
_LIT(KADDRESS, "<addr>");
_LIT(KADDRESSEND, "</addr>");
_LIT(KCOMPANY, "<company>");
_LIT(KCOMPANYEND, "</company>");
_LIT(KHOMET,"<home>");
_LIT(KHOMETEND,"</home>");
CHttpManager* CHttpManager::NewL( )
{
CHttpManager* self = CHttpManager::NewLC( );
CleanupStack::Pop( );
return self;
}
CHttpManager* CHttpManager::NewLC( )
{
CHttpManager* self = new (ELeave) CHttpManager;
CleanupStack::PushL( self );
self->ConstructL( );
return self;
}
CHttpManager::CHttpManager( )
{
// m_pBookEngine = NULL;
m_pHttpEngine = NULL;
m_pStatus = NULL;
m_pAddress = NULL;
m_pContent = NULL;
m_pRegister = NULL;
m_pLogin = NULL;
m_pBook = NULL;
m_pName = NULL;
m_pPassWord = NULL;
dlg = NULL;
m_pConutf8 = NULL;
}
CHttpManager::~CHttpManager( )
{
// MEM_FREE( m_pBookEngine );
MEM_FREE( m_pHttpEngine );
MEM_FREE( m_pStatus );
MEM_FREE( m_pAddress );
MEM_FREE( m_pContent );
MEM_FREE( m_pName );
MEM_FREE( m_pPassWord );
MEM_FREE( dlg );
MEM_FREE(m_pConutf8);
}
void CHttpManager::ConstructL( )
{
m_pStatus = HBufC::NewL( 10 );
m_pHttpEngine = CHTTPEngine::NewL ( *this );
// m_pBookEngine = CBookEngine::NewL( );
}
void CHttpManager::AddressPakge( )
{
MEM_FREE( m_pAddress );
m_pAddress = HBufC::NewL( 100 );
m_pAddress->Des().Copy(_L("http://www.ldci.mobi/snsProject/"));
m_pAddress->Des().Append( m_pStatus->Des() );
m_pAddress->Des().Append(_L("?mobile="));
m_pAddress->Des().Append( m_pName->Des() );
m_pAddress->Des().Append(_L("&pass="));
m_pAddress->Des().Append( m_pPassWord->Des() );
}
CDesC16ArrayFlat* CHttpManager::PakgeXML( )
{
CDesC16ArrayFlat* labelArray = new(ELeave) CDesC16ArrayFlat(10);
CleanupStack::PushL(labelArray);
labelArray->AppendL(_L("<rss><type>update</type><mobile>"));//0
labelArray->AppendL(_L("</mobile>"));//1
labelArray->AppendL(_L("<record><lname>"));//2
labelArray->AppendL(_L("</lname><fname>"));//3
labelArray->AppendL(_L("</fname><mob>"));//4
labelArray->AppendL(_L("</mob><mail>"));//5
labelArray->AppendL(_L("</mail><addr>"));//6
labelArray->AppendL(_L("</addr><company>"));//7
labelArray->AppendL(_L("</company></record>"));//8
labelArray->AppendL(_L("</rss>"));
return labelArray;
}
void CHttpManager::BodyPakge( )
{
CDesC16ArrayFlat* labelArray = PakgeXML( );
RPointerArray<CPBInfo>* bookArray = new (ELeave)RPointerArray<CPBInfo>(5);
CleanupStack::PushL(bookArray);
CPBEngine* m_pBookEngine = CPBEngine::NewL( );
CleanupStack::PushL(m_pBookEngine);
m_pBookEngine->UpdatePhoneBook( 0, bookArray );
MEM_FREE( m_pAddress );
m_pAddress = HBufC::NewL( 1024 );
m_pAddress->Des( ).Copy( (*labelArray)[0] );
m_pAddress->Des( ).Append( m_pName->Des() );
m_pAddress->Des( ).Append( (*labelArray)[1] );
for( TInt i = 0; i<bookArray->Count( ); ++i )
{
if((*bookArray)[i]->GetFirstName()->Length() != 0 && (*bookArray)[i]->GetLastName()->Length()
!= 0 && (*bookArray)[i]->GetTelephone()->Length() != 0)
{
m_pAddress->Des().Append((*labelArray)[2]);
m_pAddress->Des().Append((*bookArray)[i]->GetLastName()->Des());
m_pAddress->Des().Append((*labelArray)[3]);
m_pAddress->Des().Append((*bookArray)[i]->GetFirstName()->Des());
m_pAddress->Des().Append((*labelArray)[4]);
if ( (*bookArray)[i]->GetHomeMobileNumber() != NULL )
m_pAddress->Des().Append((*bookArray)[i]->GetHomeMobileNumber()->Des());
m_pAddress->Des().Append((*labelArray)[5]);
if ((*bookArray)[i]->GetEmail() != NULL )
m_pAddress->Des().Append((*bookArray)[i]->GetEmail()->Des());
if ( (*bookArray)[i]->GetTelephone() != NULL )
m_pAddress->Des().Append((*bookArray)[i]->GetTelephone()->Des());
m_pAddress->Des().Append((*labelArray)[7]);
m_pAddress->Des().Append((*labelArray)[8]);
}
}
m_pAddress->Des( ).Append((*labelArray)[9]);
labelArray->Reset();
bookArray->ResetAndDestroy();
CleanupStack::PopAndDestroy( 3 );
}
void CHttpManager::BodyPakge( const TInt aIndex )
{
CDesC16ArrayFlat* labelArray = PakgeXML( );
RPointerArray<CPBInfo>* bookArray = new (ELeave)RPointerArray<CPBInfo>(5);
CleanupStack::PushL(bookArray);
CPBEngine* m_pBookEngine = CPBEngine::NewL( );
CleanupStack::PushL(m_pBookEngine);
m_pBookEngine->UpdatePhoneBook( 0, bookArray );
MEM_FREE( m_pAddress );
m_pAddress = HBufC::NewL( 512 );
m_pAddress->Des( ).Copy( (*labelArray)[0] );
m_pAddress->Des( ).Append( m_pName->Des() );
m_pAddress->Des( ).Append( (*labelArray)[1] );
if((*bookArray)[aIndex]->GetFirstName()->Length() != 0 && (*bookArray)[aIndex]->GetLastName()->Length() != 0 && (*bookArray)[aIndex]->GetHomeMobileNumber( )->Length() != 0)
{
m_pAddress->Des().Append((*labelArray)[2]);
m_pAddress->Des().Append((*bookArray)[aIndex]->GetLastName()->Des());
m_pAddress->Des().Append((*labelArray)[3]);
m_pAddress->Des().Append((*bookArray)[aIndex]->GetFirstName()->Des());
m_pAddress->Des().Append((*labelArray)[4]);
if ( (*bookArray)[aIndex]->GetHomeMobileNumber() != NULL )
m_pAddress->Des().Append((*bookArray)[aIndex]->GetHomeMobileNumber()->Des());
m_pAddress->Des().Append((*labelArray)[5]);
if ((*bookArray)[aIndex]->GetEmail() != NULL )
m_pAddress->Des().Append((*bookArray)[aIndex]->GetEmail()->Des());
m_pAddress->Des().Append((*labelArray)[6]);
if ( (*bookArray)[aIndex]->GetTelephone() != NULL )
m_pAddress->Des().Append((*bookArray)[aIndex]->GetTelephone()->Des());
m_pAddress->Des().Append((*labelArray)[7]);
m_pAddress->Des().Append((*labelArray)[8]);
}
m_pAddress->Des( ).Append((*labelArray)[9]);
labelArray->Reset();
bookArray->ResetAndDestroy();
CleanupStack::PopAndDestroy(3);
}
void CHttpManager::Register( )
{
MEM_FREE(m_pStatus);
m_pStatus = HBufC::NewL( 10 );
m_pStatus->Des( ).Copy(_L("register"));
AddressPakge( );
m_pHttpEngine->GetRequestL( m_pAddress->Des( ) );
StartWaitDialogL();
}
void CHttpManager::Login( )
{
MEM_FREE(m_pStatus);
m_pStatus = HBufC::NewL( 10 );
m_pStatus->Des( ).Copy(_L("login"));
AddressPakge( );
m_pHttpEngine->GetRequestL( m_pAddress->Des( ) );
StartWaitDialogL();
}
void CHttpManager::DownLoad( )
{
MEM_FREE(m_pStatus);
m_pStatus = HBufC::NewL( 10 );
m_pStatus->Des( ).Copy(_L("download"));
AddressPakge( );
m_pHttpEngine->GetRequestL( m_pAddress->Des( ) );
StartWaitDialogL();
}
void CHttpManager::UpLoad( )
{
MEM_FREE(m_pStatus);
m_pStatus = HBufC::NewL( 10 );
m_pStatus->Des( ).Copy(_L("update"));
BodyPakge( );
MEM_FREE(m_pConutf8);
m_pConutf8 = HBufC8::NewL(1024);
CnvUtfConverter::ConvertFromUnicodeToUtf8(m_pConutf8->Des( ),m_pAddress->Des( ));
m_pHttpEngine->PostRequestL( m_pConutf8->Des( ) ,KPostUri );
MEM_FREE(m_pAddress);
// m_pHttpEngine->PostRequestL( m_pAddress->Des( ) ,KPostUri );
StartWaitDialogL();
}
void CHttpManager::UpLoad( const TInt aIndex )
{
MEM_FREE(m_pStatus);
m_pStatus = HBufC::NewL( 10 );
m_pStatus->Des( ).Copy(_L("update"));
BodyPakge( aIndex );
MEM_FREE(m_pConutf8);
m_pConutf8 = HBufC8::NewL(1024);
CnvUtfConverter::ConvertFromUnicodeToUtf8(m_pConutf8->Des( ),m_pAddress->Des( ));
m_pHttpEngine->PostRequestL( m_pConutf8->Des( ) ,KPostUri );
MEM_FREE(m_pAddress);
// m_pHttpEngine->PostRequestL( m_pAddress->Des( ) ,KPostUri );
StartWaitDialogL();
}
TInt CHttpManager::ResuiltParse( )
{
HBufC* header = HBufC::NewL( 8);
HBufC* ender = HBufC::NewL( 9 );
HBufC* news = HBufC::NewL(1);
header->Des( ).Copy(_L("<result>"));
ender->Des( ).Copy(_L("</result>"));
TInt length = m_pContent->Des().Find(header->Des())+header->Des().Length();
TInt length1 = m_pContent->Des().Find(ender->Des());
if (m_pContent->Des().Find(header->Des()) != KErrNotFound)
{
news->Des().Copy(m_pContent->Des().Mid(length, length1-length));
}
if(!news->Compare(_L("0")))
{
MEM_FREE(header)
MEM_FREE(ender)
MEM_FREE(news)
return 0;
}
else if(!news->Compare(_L("1")))
{
MEM_FREE(header)
MEM_FREE(ender)
MEM_FREE(news)
return 1;
}
else
{
MEM_FREE(header)
MEM_FREE(ender)
MEM_FREE(news)
return 2;
}
}
void CHttpManager::DownLoadParse( )
{
CPBEngine* m_pBookEngine = CPBEngine::NewL( );
CleanupStack::PushL(m_pBookEngine);
RPointerArray<CPBInfo>* bookArray = new (ELeave)RPointerArray<CPBInfo>(5);
CleanupStack::PushL(bookArray);
HBufC* result = NULL;
CPBInfo* bookInfo = NULL;
TInt nStart = 0;
TInt nEnd = 0;
while(m_pContent->Find(KRECORD) != KErrNotFound)
{
bookInfo = CPBInfo::NewL();
nStart = m_pContent->Find(KLNAME) + 7;
nEnd = m_pContent->Find(KLNAMEEND);
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetLastName(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
nStart = m_pContent->Find(KFNAME) + 7;
nEnd = m_pContent->Find(KFNAMEEND);
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetFirstName(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
nStart = m_pContent->Find(KMOBILE) + 5;
nEnd = m_pContent->Find(KMOBILEEND);
if ( nEnd - nStart != 0 )
{
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetHomeMobileNumber(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
}
nStart = m_pContent->Find(KEMAIL) + 6;
nEnd = m_pContent->Find(KEMAILEND);
if ( nEnd - nStart != 0 )
{
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetEmail(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
}
nStart = m_pContent->Find(KADDRESS) + 6;
nEnd = m_pContent->Find(KADDRESSEND);
if ( nEnd - nStart != 0 )
{
result = HBufC::NewLC(nEnd - nStart);
result->Des().Copy(m_pContent->Mid(nStart,nEnd-nStart));
bookInfo->SetTelephone(result->Des());
CleanupStack::PopAndDestroy(result);
result = NULL;
}
bookInfo->SetItemID(-1);
bookArray->Append(bookInfo);
nStart = m_pContent->Find(KRECORDEND) + 9;
m_pContent->Des().Copy(m_pContent->Right(m_pContent->Length()-nStart));
}
m_pBookEngine->UpdatePhoneBook(1,bookArray);
bookArray->ResetAndDestroy();
CleanupStack::PopAndDestroy( 2 );
}
void CHttpManager::ResponseStatusL(TInt aStatusCode, const TDesC& aStatusText)
{
}
void CHttpManager::ResponseReceivedL(const TDesC& aResponseBuffer)
{
MEM_FREE(m_pContent);
m_pContent = HBufC::NewL(aResponseBuffer.Length());
m_pContent->Des().Copy(aResponseBuffer);
MEM_FREE(m_pConutf8);
m_pConutf8 = HBufC8::NewL(aResponseBuffer.Length());
m_pConutf8->Des().Copy(aResponseBuffer);
CnvUtfConverter::ConvertToUnicodeFromUtf8(m_pContent->Des(),m_pConutf8->Des());
MEM_FREE(m_pConutf8);
if(!m_pStatus->Compare(_L("register")))
{
m_pRegister->ResultShow(ResuiltParse( ));
}
else if(!m_pStatus->Compare(_L("login")))
{
m_pLogin->ResultShow(ResuiltParse( ));
}
else if(!m_pStatus->Compare(_L("update")))
{
m_pBook->ResultShow(ResuiltParse( ));
}
else
{
if (m_pContent->Length() <= 28 )
{
m_pBook->ResultShow(2);
}
else
{
DownLoadParse();
m_pBook->ResultShow(3);
}
}
}
void CHttpManager::SetReisterPtr( MResultShow* aPtr )
{
m_pRegister = aPtr;
}
void CHttpManager::SetLoginPtr( MResultShow* aPtr )
{
m_pLogin = aPtr;
}
void CHttpManager::SetBookPtr( MResultShow* aPtr )
{
m_pBook = aPtr;
}
void CHttpManager::SetName( const TDesC& aName )
{
MEM_FREE( m_pName );
m_pName = HBufC::NewL( 11 );
m_pName->Des().Copy( aName );
}
void CHttpManager::SetPassWord( const TDesC& aPsw )
{
MEM_FREE( m_pPassWord );
m_pPassWord = HBufC::NewL( 6 );
m_pPassWord->Des().Copy( aPsw );
}
void CHttpManager::StartWaitDialogL()
{
if (dlg)
{
delete dlg;
dlg = NULL;
}
dlg = new (ELeave) CAknWaitDialog((REINTERPRET_CAST(CEikDialog**, &dlg)), ETrue);
dlg->SetTone(CAknNoteDialog::EConfirmationTone);
dlg->ExecuteLD(R_HELPERGENIUS_WAIT_NOTE);
}
// void CHttpManager::StartWaitDialogL()
// {
// MEM_FREE(dlg);
// CAknProgressDialog* dlg = new (ELeave) CAknProgressDialog(200,5,20, NULL);
// dlg->ExecuteLD(R_GUICTRLS_PROGRESS_NOTE );
// }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -