// MBEmotionsWnd.cpp : implementation file
|
//
|
|
#include "stdafx.h"
|
#include "UpdateProgressDlg.h"
|
#include "PromptUpdateDlg.h"
|
#include "Toolhelp.h"
|
#include "TaskItemData.h"
|
|
// CUpdateProgressDlg
|
CUpdateProgressDlg::CUpdateProgressDlg() : SHostWnd(_T("LAYOUT:XML_UPDATEPROGRESSWND"))
|
{
|
m_bLayoutInited = FALSE;
|
m_bSetFlag = false;
|
m_nFileSize = 100;
|
m_nTranferedSize = 0;
|
m_nLastTransferedSize = 0;
|
m_dwLastUpdate = 0;
|
m_strID = _T("");
|
m_pOwner = NULL;
|
m_strFileServer = _T("");
|
m_nFileSPort = OIFILE_DEFAULT_PORT;
|
m_strLoginName = _T("");
|
m_strUserName = _T("");
|
m_strFileID = _T("");
|
m_strFileName = _T("");
|
m_strSaveFileName = _T("");
|
m_bIsForcible = FALSE;
|
m_bDownloadFinish = FALSE;
|
m_strAppProcName = _T("");
|
m_nTrID = 0;
|
m_pTaskItemData = NULL;
|
m_bCloseDlg = FALSE;
|
}
|
|
CUpdateProgressDlg::~CUpdateProgressDlg()
|
{
|
|
}
|
BOOL CUpdateProgressDlg::OnInitDialog(HWND wndFocus, LPARAM lInitParam)
|
{
|
m_bLayoutInited = TRUE;
|
CString strFilePath = _T("");
|
m_bSetFlag = false;
|
m_nFileSize = 100;
|
m_nTranferedSize = 0;
|
m_nLastTransferedSize = 0;
|
m_dwLastUpdate = GetTickCount();
|
m_bCloseDlg = FALSE;
|
SetTimer( TIMER_UPDATETRANSPROGRESS, TIME_UPDATETRANSPROGRESS );
|
if ( !DownloadFile( m_strFileServer, m_nFileSPort, m_strFileID, m_strFileName, strFilePath ) )
|
{
|
m_pTaskItemData->TaskEnd();
|
m_pTaskItemData->SetLastErrCode( 0);
|
m_pTaskItemData->SetLastErrInfo( _T("µ÷ÓÃDownloadFileʧ°Ü"));
|
if(m_pOwner)
|
m_pOwner->SendMessage( WM_ENDTASK, (WPARAM)&m_strID, 2 );
|
MessageBox(m_hWnd,_T("ÏÂÔØÉý¼¶Îļþʧ°Ü"),_T("Ìáʾ"),MB_OK);
|
PostMessage(WM_CLOSE);
|
return TRUE;
|
}
|
m_strSaveFileName = strFilePath;
|
SProgress *pProgress = FindChildByName2<SProgress>(L"progress_update");
|
if(pProgress)
|
{
|
pProgress->SetRange(0,m_nFileSize);
|
}
|
return TRUE;
|
}
|
int CUpdateProgressDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
|
{
|
SetMsgHandled(FALSE);
|
return 0;
|
}
|
|
void CUpdateProgressDlg::SetServerInfo(CString strFileServer, int nFileSPort)
|
{
|
m_strFileServer = strFileServer;
|
m_nFileSPort= nFileSPort;
|
}
|
void CUpdateProgressDlg::SetUserInfo(CString strLoginName, CString strUserName)
|
{
|
m_strLoginName= strLoginName;
|
m_strUserName =strUserName;
|
}
|
void CUpdateProgressDlg::SetFileInfo(CString strFileID, CString strFileName)
|
{
|
m_strFileID= strFileID;
|
m_strFileName= strFileName;
|
}
|
void CUpdateProgressDlg::SetForcible(BOOL bIsForcible )
|
{
|
m_bIsForcible = bIsForcible;
|
}
|
|
|
void CUpdateProgressDlg::OnTimer(UINT_PTR nIDEvent)
|
{
|
if(nIDEvent == TIMER_UPDATETRANSPROGRESS)
|
{
|
DWORD dwTickCount;
|
DWORD dwMillSecond, dwSecondLeft;
|
__int64 nTransferedSpeed;
|
__int64 dwBodySize;
|
double dKbPerSec;
|
|
dwTickCount = GetTickCount();
|
dwMillSecond = dwTickCount - m_dwLastUpdate;
|
m_dwLastUpdate = dwTickCount;
|
|
dwBodySize = (m_nTranferedSize - m_nLastTransferedSize);
|
m_nLastTransferedSize= m_nTranferedSize;
|
if ( dwBodySize < 1 )
|
{
|
dKbPerSec = 0;
|
dwSecondLeft = -1;
|
nTransferedSpeed = 0;
|
}
|
else
|
{
|
dKbPerSec = ((double)dwBodySize/ (double)dwMillSecond )* 1000;
|
nTransferedSpeed = (__int64)dKbPerSec;
|
dwSecondLeft = ((m_nFileSize -m_nTranferedSize) * dwMillSecond / 1000 / dwBodySize);
|
}
|
CString strText = _T("");
|
strText.Format( _T("ËÙ¶È£º%s"),GetSpeedStr(nTransferedSpeed));
|
SStatic *pStatic= FindChildByName2<SStatic>(L"text_speed");
|
if(pStatic)
|
pStatic->SetWindowText(strText);
|
strText.Format(L"%s/%s",GetFileSize(m_nTranferedSize),GetFileSize(m_nFileSize));
|
pStatic= FindChildByName2<SStatic>(L"text_filesize");
|
if(pStatic)
|
pStatic->SetWindowText(strText);
|
if(m_nTranferedSize < 1)
|
{
|
strText = L"½ÓÊÕÎļþ×¼±¸ÖС¡";
|
}
|
else
|
{
|
if( dwSecondLeft < 0)
|
strText = L"Ê£Óàʱ¼ä:δ֪";
|
else
|
strText.Format(L"Ê£Óàʱ¼ä£º%s",GetRemainTime( dwSecondLeft));
|
}
|
pStatic= FindChildByName2<SStatic>(L"text_retime");
|
if(pStatic)
|
pStatic->SetWindowText(strText);
|
SProgress *pProgress = FindChildByName2<SProgress>(L"progress_update");
|
if(pProgress)
|
{
|
pProgress->SetValue(m_nTranferedSize);
|
}
|
}
|
if(nIDEvent == TIMER_CHECKTRANSFEROUTTIME)
|
{
|
DWORD dwTickCount = ::GetTickCount();
|
if(m_nTrID > 0)
|
{
|
if(m_dwLastTransfer > 0 && dwTickCount - m_dwLastTransfer > MAX_TRANSFEREDOUTTIME)
|
{
|
m_FileClientMgr.CancelCmder(m_nTrID);
|
m_nTrID = 0;
|
DWORD dwOption = OPTION_TRANOPTION_REPORT;
|
CString strMFileID;
|
CString strSaveFilePath;
|
CString strWorkDir;
|
strMFileID =m_strFileID ;
|
strMFileID.TrimLeft( _T("{") );
|
strMFileID.TrimRight( _T("}") );
|
strMFileID = _T("{") + strMFileID + _T("}");
|
strWorkDir = CBaseCommFun::GetModulePath( );
|
strSaveFilePath.Format( _T("%s\\OIUpsDownload\\%s"), strWorkDir,m_strFileName );
|
m_nTrID = m_FileClientMgr.DownloadFile( strMFileID, _T(""), strSaveFilePath, dwOption );
|
}
|
}
|
}
|
SetMsgHandled(FALSE);
|
}
|
|
LRESULT CUpdateProgressDlg::OnMsgFileConnectError(UINT uMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)
|
{
|
long nErrCode;
|
CString strBody;
|
CString strErrInfo;
|
CString strDateTime;
|
CString strSubject;
|
CString strTip;
|
|
m_nTrID = 0;
|
//nTrID = (long)wParam;
|
nErrCode = (long)wParam;
|
CFileClientAsynMgr fileClientMgr;
|
strErrInfo = fileClientMgr.GetErrInfo(nErrCode);
|
m_pTaskItemData->SetLastErrCode( nErrCode);
|
m_pTaskItemData->SetLastErrInfo( strErrInfo);
|
m_pTaskItemData->TaskEnd();
|
if(m_pOwner)
|
m_pOwner->SendMessage( WM_ENDTASK, (WPARAM)&m_strID, 2 );
|
strSubject = _T("ÏÂÔØÉý¼¶°ü");
|
strBody.Format( _T("ÏÂÔØÉý¼¶°ü(%s)ʧ°Ü£¬ÔÒò£º%s£¬´íÎóÂ룺%d"),m_strFileName, strErrInfo, nErrCode );
|
MessageBox(m_hWnd,strBody,strSubject,MB_OK);
|
PostMessage(WM_CLOSE);
|
return S_OK;
|
}
|
LRESULT CUpdateProgressDlg::OnMsgFileResError(UINT uMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)
|
{
|
long nTrID;
|
CString strErrInfo;
|
CString strBody;
|
long nErrCode;
|
CString strMsg;
|
CString strParam;
|
CString strItem;
|
CString strName;
|
CString strValue;
|
CString strSubject;
|
|
if(m_bCloseDlg)
|
return S_OK;
|
nTrID = (long)wParam;
|
strParam = *(CString*)lParam;
|
while( CBaseStringMgr::GetElementItem(strParam,strItem, _T(";") ))
|
{
|
CBaseStringMgr::GetElementItem(strItem,strName, _T("=") );
|
strValue = strItem;
|
if(strName.CompareNoCase( _T("ErrCode")) == 0 )
|
{
|
nErrCode = _tstol(strValue);
|
}
|
}
|
if(nErrCode != 3)
|
{
|
m_nTrID = 0;
|
KillTimer(TIMER_UPDATETRANSPROGRESS);
|
KillTimer(TIMER_CHECKTRANSFEROUTTIME);
|
strSubject = _T("ÏÂÔØÉý¼¶°ü");
|
strBody.Format( _T("ÏÂÔØÉý¼¶°ü(%s)ʧ°Ü£¬ÔÒò£º%s£¬´íÎóÂ룺%d"),m_strFileName, strErrInfo, nErrCode );
|
MessageBox(m_hWnd,strBody,strSubject,MB_OK);
|
m_pTaskItemData->SetLastErrCode( nErrCode);
|
m_pTaskItemData->SetLastErrInfo( strErrInfo);
|
m_pTaskItemData->TaskEnd();
|
if(m_pOwner)
|
m_pOwner->SendMessage( WM_ENDTASK, (WPARAM)&m_strID, 2 );
|
PostMessage(WM_CLOSE);
|
}
|
return S_OK;
|
}
|
void CUpdateProgressDlg::DoMessageLoop()
|
{
|
MSG msg;
|
while ( ::PeekMessage( &msg, m_hWnd, 0, 0, PM_REMOVE ) )
|
{
|
::TranslateMessage( &msg );
|
::DispatchMessage( &msg );
|
}
|
}
|
LRESULT CUpdateProgressDlg::OnMsgFileResReceive(UINT uMsg,WPARAM wParam,LPARAM lParam,BOOL & bHandled)
|
{
|
long nTrID;
|
if(m_bCloseDlg)
|
return S_OK;
|
DoMessageLoop();
|
COISysMsg *pSMsg = NULL;
|
nTrID = (long)wParam;
|
pSMsg = (COISysMsg*)lParam;
|
if( !pSMsg)
|
return S_OK;
|
CString strMethod = pSMsg->GetMethod();
|
CString strConnectType = pSMsg->GetProp( _T( "ConnectType" ) );
|
m_dwLastTransfer = ::GetTickCount();
|
if(m_pTaskItemData)
|
m_pTaskItemData->SetDownloading();
|
if ( CMD_FILE_DOWNLOAD == strMethod )
|
{
|
CString strAppType = pSMsg->GetProp( _T( "AppType" ) );
|
if ( pSMsg->GetParamCount() > 1 )
|
{
|
//ShowWindow(SW_NORMAL);
|
m_nFileSize = pSMsg->GetParamInt64( 2 );
|
m_nTranferedSize = pSMsg->GetParamInt64( 1 );
|
if( !m_bSetFlag)
|
{
|
SProgress *pProgress = FindChildByName2<SProgress>(L"progress_update");
|
if(pProgress)
|
{
|
m_bSetFlag = true;
|
pProgress->SetRange(0,m_nFileSize);
|
pProgress->SetValue(m_nTranferedSize);
|
}
|
|
}
|
}
|
else
|
{
|
m_nTrID = 0;
|
BOOL bFlag = FALSE;
|
CString strFileName;
|
strFileName = pSMsg->GetProp( _T( "LocalPath" ) );
|
KillTimer(TIMER_UPDATETRANSPROGRESS);
|
KillTimer(TIMER_CHECKTRANSFEROUTTIME);
|
// Éý¼¶
|
ShowWindow(SW_HIDE);
|
if( !m_bIsForcible)
|
{
|
CPromptUpdateDlg dlgUpdate;
|
if ( dlgUpdate.DoModal( ) == IDOK )
|
{
|
// Éý¼¶
|
::ShellExecute( NULL, _T("open"), strFileName, _T(""),
|
CBaseCommFun::GetPathName( strFileName ), SW_SHOWNORMAL );
|
bFlag = TRUE;
|
|
}
|
}
|
else
|
{
|
bFlag = TRUE;
|
::ShellExecute( NULL, _T("open"), strFileName, _T(""),
|
CBaseCommFun::GetPathName( strFileName ), SW_SHOWNORMAL );
|
}
|
m_bDownloadFinish = TRUE;
|
m_pTaskItemData->TaskEnd();
|
if(m_pOwner)
|
m_pOwner->SendMessage( WM_ENDTASK, (WPARAM)&m_strID, 0 );
|
PostMessage(WM_CLOSE);
|
if(bFlag)
|
{
|
if(m_pOwner)
|
m_pOwner->PostMessage( WM_CLOSE );
|
if( !m_strAppProcName.IsEmpty())
|
{
|
CString strErrInfo;
|
KillProcess(m_strAppProcName, strErrInfo );
|
}
|
}
|
}
|
|
}
|
return S_OK;
|
}
|
CString CUpdateProgressDlg::GetFileSize(__int64 lSize)
|
{
|
CString strText;
|
double nRespSize;
|
_int64 nTempSize;
|
_int64 nTempUnit;
|
_int64 nTempUnit2;
|
|
nTempUnit2 = 1024 * 1024 * 1024;
|
nTempUnit = nTempUnit2 * 1024;
|
if(lSize / nTempUnit> 0)
|
{
|
nTempSize = lSize/ (1024 * 1024 * 1024);
|
nRespSize = (double )nTempSize/1024;
|
if(nRespSize > 100)
|
strText.Format(_T("%.0lf TB"), nRespSize);
|
else
|
strText.Format(_T("%.1lf TB"), nRespSize);
|
return strText;
|
}
|
nTempUnit = 1024 * 1024 * 1024;
|
if(lSize / nTempUnit > 0)
|
{
|
nTempSize = lSize/ (1024 * 1024);
|
nRespSize = (double )nTempSize/1024;
|
if(nRespSize > 100)
|
strText.Format(_T("%.0lf GB"), nRespSize);
|
else
|
strText.Format(_T("%.1lf GB"), nRespSize);
|
return strText;
|
}
|
if(lSize / (1024 * 1024) > 0)
|
{
|
nRespSize = (double )lSize/ ( 1024 * 1024 );
|
if(nRespSize > 100)
|
strText.Format(_T("%.0lf MB"), nRespSize);
|
else
|
strText.Format(_T("%.1lf MB"), nRespSize);
|
return strText;
|
}
|
if(lSize / 1024 > 0)
|
{
|
nTempSize = lSize/ 1024;
|
if (lSize % (1024 ))
|
nTempSize++;
|
strText.Format(_T("%I64d KB"), nTempSize);
|
return strText;
|
}
|
strText.Format(_T("%I64d B"), lSize);
|
return strText;
|
}
|
|
CString CUpdateProgressDlg::GetSpeedStr( __int64 nSpeed)
|
{
|
float nTempSize;
|
CString strText;
|
if(nSpeed / (1024 * 1024 * 1024) > 0)
|
{
|
nTempSize = (float)nSpeed/ (1024 * 1024 * 1024);
|
strText.Format(_T("%.2fGB/S"), nTempSize);
|
return strText;
|
}
|
if(nSpeed / (1024 * 1024) > 0)
|
{
|
nTempSize = (float)nSpeed/ ( 1024 * 1024 );
|
strText.Format(_T("%.2fMB/S"), nTempSize);
|
return strText;
|
}
|
if(nSpeed / 1024 > 0)
|
{
|
nTempSize = (float)nSpeed/ 1024;
|
strText.Format(_T("%.2fKB/S"), nTempSize);
|
return strText;
|
}
|
strText.Format(_T("%dB/S"), nSpeed);
|
return strText;
|
}
|
|
CString CUpdateProgressDlg::GetRemainTime(DWORD nRemainedTime)
|
{
|
CString strText = _T("");
|
|
if(nRemainedTime > 0)
|
{
|
if( nRemainedTime > 60 * 60)
|
{
|
if( ( nRemainedTime % (60 * 60) ) / 60 > 30)
|
strText.Format(_T("%02d:00:00"), nRemainedTime/(60 *60) + 1);
|
else
|
strText.Format(_T("%02d:00:00"), nRemainedTime/(60 *60));
|
}
|
else if( nRemainedTime > 600)
|
{
|
if( nRemainedTime % 60 > 30)
|
strText.Format(_T("00:%02d:00"), nRemainedTime/60 + 1);
|
else
|
strText.Format(_T("00:%02d:00"), nRemainedTime/60);
|
}
|
else
|
strText.Format(_T("00:%02d:%02d"), nRemainedTime/60, nRemainedTime % 60);
|
}
|
return strText;
|
}
|
void CUpdateProgressDlg::OnClose()
|
{
|
if( m_bIsForcible && !m_bDownloadFinish)
|
{
|
return;
|
}
|
m_bCloseDlg = TRUE;
|
if ( m_nTrID > 0 )
|
{
|
m_FileClientMgr.CancelCmder(m_nTrID);
|
m_nTrID = 0;
|
}
|
KillTimer(TIMER_CHECKTRANSFEROUTTIME);
|
DestroyWindow( );
|
}
|
|
bool CUpdateProgressDlg::DownloadFile( CString strMFileServer, long nMFilePort, CString strMFileID,
|
CString strFileName, CString &strFilePath )
|
{
|
|
DWORD dwOption = OPTION_TRANOPTION_REPORT;
|
CString strInfo;
|
CString strSaveFilePath;
|
CString strWorkDir;
|
BOOL bReturn = true;
|
CString strLastErrInfo;
|
|
strFilePath.Empty( );
|
strMFileID.TrimLeft( _T("{") );
|
strMFileID.TrimRight( _T("}") );
|
strMFileID = _T("{") + strMFileID + _T("}");
|
strWorkDir = CBaseCommFun::GetModulePath( );
|
strSaveFilePath.Format( _T("%s\\MBUpsDownload"), strWorkDir );
|
if ( GetFileAttributes( strSaveFilePath ) == -1 )
|
::CreateDirectory( strSaveFilePath, NULL );
|
strSaveFilePath += ( _T("\\") + strFileName );
|
strFilePath = strSaveFilePath;
|
if(CStrFileUtils::IsExist(strFilePath))
|
{
|
DeleteFile(strFilePath);
|
}
|
m_FileClientMgr.SetServerInfo( strMFileServer, nMFilePort );
|
m_FileClientMgr.SetAppType( APPTYPE_OIUPS );
|
m_FileClientMgr.SetUserInfo( m_strLoginName, m_strUserName);
|
m_FileClientMgr.SetOwnerHwnd(m_hWnd);
|
if(strMFileServer.IsEmpty())
|
return false;
|
else
|
{
|
if( !m_FileClientMgr.Init(strLastErrInfo))
|
return false;
|
}
|
m_nTrID = m_FileClientMgr.DownloadFile( strMFileID, _T(""), strSaveFilePath, dwOption );
|
if ( m_nTrID == 0 )
|
{
|
return false;
|
}
|
m_dwLastTransfer = ::GetTickCount();
|
KillTimer(TIMER_CHECKTRANSFEROUTTIME);
|
SetTimer(TIMER_CHECKTRANSFEROUTTIME,TIME_CHECKTRANSFEROUTTIME);
|
return true;
|
}
|
|
|
BOOL CUpdateProgressDlg::ParseErrerInfo( CString strErrorInfo, long &nErrID, CString &strErr)
|
{
|
COIXMLParser xmlParser;
|
IXMLDOMElementPtr ptrBody;
|
IXMLDOMNodePtr ptrNode,ptrNext;
|
CString strNodeName,strNodeValue;
|
|
nErrID = 0;
|
strErr = _T("");
|
if ( strErrorInfo.IsEmpty( ) )
|
return FALSE;
|
if( !xmlParser.ParseXMLStr(strErrorInfo, ptrBody) )
|
{
|
return FALSE;
|
}
|
ptrBody->get_firstChild( &ptrNode);
|
while(ptrNode)
|
{
|
COIXMLParser::GetNodeContent(ptrNode, strNodeName, strNodeValue);
|
if ( strNodeName.CompareNoCase( _T("ErrCode") ) == 0 )
|
{
|
nErrID = _tstol(strNodeValue);
|
}
|
if ( strNodeName.CompareNoCase( _T("ErrInfo") ) == 0 )
|
{
|
strErr = strNodeValue;
|
}
|
ptrNode->get_nextSibling( &ptrNext);
|
ptrNode = ptrNext;
|
}
|
return TRUE;
|
}
|
|
|
BOOL CUpdateProgressDlg::KillProcess( CString strProcess, CString &strErrInfo )
|
{
|
CToolhelp::EnableDebugPrivilege(TRUE);
|
|
CToolhelp toolhelp(TH32CS_SNAPPROCESS);
|
PROCESSENTRY32 pe = {sizeof(PROCESSENTRY32)};
|
|
strErrInfo.Empty( );
|
if( !toolhelp.ProcessFirst( &pe ) )
|
{
|
CToolhelp::EnableDebugPrivilege(FALSE);
|
return FALSE;
|
}
|
|
while( toolhelp.ProcessNext( &pe ) )
|
{
|
CString strExeFile = pe.szExeFile;
|
|
if( strProcess.CompareNoCase( strExeFile ) == 0 )
|
{
|
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, pe.th32ProcessID );
|
|
if ( !TerminateProcess( hProcess, 0 ) )
|
{
|
strErrInfo.Format( _T("Kill Process %s (PID = %d) failed"), strProcess, pe.th32ProcessID );
|
}
|
}
|
}
|
|
CToolhelp::EnableDebugPrivilege(FALSE);
|
return true;
|
}
|