📄 downloadentity.cpp
字号:
#include "DownloadEntity.h"
#include "Train.rsg"
CDownloadEntity* CDownloadEntity::NewL(
RFs& aFs,
MDownloadManagerObserver& aObserver )
{
CDownloadEntity* self = new (ELeave) CDownloadEntity(aFs,aObserver);
CleanupStack::PushL(self);
TRAPD(err,self->ConstructL(););
if (err != KErrNone) {
CleanupStack::PopAndDestroy(self);
User::Leave(err);
}
CleanupStack::Pop(self);
return self;
}
CDownloadEntity::~CDownloadEntity()
{
if (iFileName) delete iFileName;
if (iUrl) delete iUrl;
if (iHttpClient) {
//iHttpClient->CancelTransactionL();
delete iHttpClient;
}
}
void CDownloadEntity::ConstructL()
{
}
void CDownloadEntity::Initialize(const TDesC8& aUrl, const TDesC& aFileName)
{
if (iFileName) delete iFileName;
iFileName = HBufC::NewL(aFileName.Length());
*iFileName = aFileName;
if (iUrl) delete iUrl;
iUrl = HBufC8::NewL(aUrl.Length());
*iUrl = aUrl;
RFile file;
TInt err;
err= file.Replace(iFs,iFileName->Des(),EFileWrite|EFileShareAny);
if (err == -12)
err = file.Create(iFs,iFileName->Des(),EFileWrite|EFileShareAny);
file.Close();
iHttpClient = CHttpClientEngine::NewL( *this );
// iHttpClient->SetCallBack( this );
TRAP(err , iHttpClient->IssueHTTPGetL( iUrl->Des() ););
if (err != KErrNone) {
iObserver.DownloadFinished(EDownloadFailed);
User::Leave(err);
}
iWaitDialog = new ( ELeave )CAknWaitDialog(
reinterpret_cast<CEikDialog**>( &iWaitDialog ), ETrue );
// Set DialogDissmissed callback
iWaitDialog->SetCallback( this );
iWaitDialog->ExecuteLD( R_WEBCLIENT_DOWNLOAD_WAIT_NOTE );
}
CDownloadEntity::CDownloadEntity(
RFs& aFs,
MDownloadManagerObserver& aObserver )
:iObserver(aObserver),iFs(aFs)
{
}
void CDownloadEntity::ClientEvent( const TDesC& aEventDescription )
{
}
void CDownloadEntity::ClientHeaderReceived(const TDesC& aHeaderData)
{
TBuf8<5000> zz;
zz.Copy(aHeaderData);
TInt locationpos=zz.Find(_L8("Content-Length: "));
if (locationpos != -1) {
zz = zz.Right(zz.Length() - locationpos - 16);
// TInt returnpos = zz.Find(_L8("\r\n"));
// zz = zz.Left(returnpos);
//If now is a Http redirect answer
TBuf<20> z;
z.Copy(zz);
TLex iLex(z);
iLex.Val(iSize);
}
// RFile file;
// TInt err;
// err= file.Open(iFs,_L("C:\\Data\\installs\\test.txt"),EFileWrite|EFileShareAny);
// CleanupClosePushL(file);
// TInt size;
// err = file.Size(size);
// err = file.Write(size,zz);
// CleanupStack::PopAndDestroy(1);
}
void CDownloadEntity::ClientBodyReceived( const TDesC8& aBodyData )
{
RFile file;
TInt err;
err= file.Open(iFs,iFileName->Des(),EFileWrite|EFileShareAny);
CleanupClosePushL(file);
TInt size;
err = file.Size(size);
if (iSize != 0)
iObserver.DownloadPercent(100*size/iSize);
err = file.Write(size,aBodyData);
file.Close();
CleanupStack::Pop(1);
}
void CDownloadEntity::ClientFailed()
{
if ( iWaitDialog )
{
iWaitDialog->ProcessFinishedL(); // deletes the dialog
}
iObserver.DownloadFinished(EDownloadFailed);
}
void CDownloadEntity::ClientSuccess()
{
if ( iWaitDialog )
{
iWaitDialog->ProcessFinishedL(); // deletes the dialog
}
iObserver.DownloadFinished(EDownloaded);
}
void CDownloadEntity::Cancel()
{
if ( iWaitDialog )
{
iWaitDialog->ProcessFinishedL(); // deletes the dialog
}
iHttpClient->CancelTransactionL();
iObserver.DownloadFinished(EDownloadFailed);
}
void CDownloadEntity::DialogDismissedL( TInt aButtonId )
{
if ( aButtonId == EAknSoftkeyCancel )
{
// Cancel current transaction
if ( iHttpClient->IsRunning() )
{
iHttpClient->CancelTransactionL();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -