SendFileDlg.cpp
上传用户:zslianheng
上传日期:2013-04-03
资源大小:946k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Visual C++

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   This program is free software; you can redistribute it and/or modify  *
  4.  *   it under the terms of the GNU General Public License as published by  *
  5.  *   the Free Software Foundation; either version 2 of the License, or     *
  6.  *   (at your option) any later version.                                   *
  7.  *                                                                         *
  8.  *   copyright            : (C) 2002 by Zhang Yong                         *
  9.  *   email                : z-yong163@163.com                              *
  10.  ***************************************************************************/
  11. // SendFileDlg.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "SendFile.h"
  15. #include "SendFileDlg.h"
  16. #include "icqlinkbase.h"
  17. #include "contactinfo.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. #define IDT_BROWSE_FILE 1001
  24. #define IDT_ANIM 1002
  25. static CWnd *pWnd;
  26. static VOID CALLBACK onTimer(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
  27. {
  28. KillTimer(hwnd, idEvent);
  29. delete pWnd;
  30. }
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CSendFileDlg dialog
  33. CSendFileDlg::CSendFileDlg(FileSession *s, CWnd* pParent /*=NULL*/)
  34. : CDialog(CSendFileDlg::IDD, pParent)
  35. {
  36. //{{AFX_DATA_INIT(CSendFileDlg)
  37. //}}AFX_DATA_INIT
  38. session = s;
  39. icqLink = s->getLink();
  40. uint32 uin = s->getUIN();
  41. contact = icqLink->getContactInfo(uin);
  42. m_uin.Format("%lu", uin);
  43. m_nick = contact->nick.c_str();
  44. bytesSent = 0;
  45. lastBytes = 0;
  46. lastTime = 0;
  47. frame = 0;
  48. }
  49. CSendFileDlg::~CSendFileDlg()
  50. {
  51. if (session)
  52. delete session;
  53. }
  54. const char *CSendFileDlg::getPathName(const char *name, uint32 size)
  55. {
  56. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  57. fileName = name;
  58. fileSize = size;
  59. CString str;
  60. str.Format(IDS_FILESIZE, fileSize);
  61. SetDlgItemText(IDC_FILESIZE, str);
  62. str.LoadString(IDS_RECEIVING_FILE);
  63. SetDlgItemText(IDC_STATUS, str);
  64. CFileDialog dlg(FALSE, NULL, fileName,
  65. OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "*.*|*.*||", this);
  66. if (dlg.DoModal() != IDOK)
  67. return NULL;
  68. pathName = dlg.GetPathName();
  69. str.Format(IDS_TITLE_RECVFILE, (LPCTSTR) pathName);
  70. SetWindowText(str);
  71. m_ctlProgress.SetRange32(0, fileSize);
  72. return pathName;
  73. }
  74. void CSendFileDlg::onFileReceive()
  75. {
  76. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  77. CString str;
  78. str.LoadString(IDS_SENDING_FILE);
  79. SetDlgItemText(IDC_STATUS, str);
  80. }
  81. void CSendFileDlg::onFileProgress(uint32 bytes)
  82. {
  83. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  84. DWORD thisTime = GetTickCount();
  85. bool isSend = session->isSend;
  86. if (bytesSent == 0) {
  87. lastBytes = 0;
  88. lastTime = thisTime;
  89. CString str;
  90. str.LoadString(isSend ? IDS_SENDING_FILE : IDS_RECEIVING_FILE);
  91. SetDlgItemText(IDC_STATUS, str);
  92. }
  93. bytesSent = bytes;
  94. m_ctlProgress.SetPos(bytesSent);
  95. CString str;
  96. int percent = (int) (bytesSent * 100.0f / fileSize);
  97. str.Format(isSend ? IDS_SENDFILE_BYTES : IDS_RECVFILE_BYTES,
  98. bytesSent, percent);
  99. SetDlgItemText(IDC_STATUS_DETAIL, str);
  100. if (thisTime - lastTime >= 500) {
  101. float speed = (float) (bytesSent - lastBytes) / (thisTime - lastTime) / 1.024f;
  102. lastTime = thisTime;
  103. lastBytes = bytesSent;
  104. str.Format("%.1fKB/S", speed);
  105. SetDlgItemText(IDC_SPEED, str);
  106. }
  107. }
  108. void CSendFileDlg::onFileFinished()
  109. {
  110. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  111. CString text, title;
  112. if (bytesSent == fileSize) {
  113. title.LoadString(IDS_FINISHED);
  114. text.LoadString(session->isSend ? IDS_SUCCESS_SENDFILE : IDS_SUCCESS_RECVFILE);
  115. } else {
  116. title.LoadString(IDS_FAILED);
  117. text.LoadString(IDS_ERROR_SENDFILE);
  118. }
  119. MessageBox(text, title);
  120. DestroyWindow();
  121. }
  122. void CSendFileDlg::DoDataExchange(CDataExchange* pDX)
  123. {
  124. CDialog::DoDataExchange(pDX);
  125. //{{AFX_DATA_MAP(CSendFileDlg)
  126. DDX_Control(pDX, IDC_FACE, m_faceStatic);
  127. DDX_Control(pDX, IDC_PROGRESS, m_ctlProgress);
  128. DDX_Text(pDX, IDC_UIN, m_uin);
  129. DDX_Text(pDX, IDC_NICK, m_nick);
  130. //}}AFX_DATA_MAP
  131. }
  132. BEGIN_MESSAGE_MAP(CSendFileDlg, CDialog)
  133. //{{AFX_MSG_MAP(CSendFileDlg)
  134. ON_WM_TIMER()
  135. //}}AFX_MSG_MAP
  136. END_MESSAGE_MAP()
  137. /////////////////////////////////////////////////////////////////////////////
  138. // CSendFileDlg message handlers
  139. BOOL CSendFileDlg::OnInitDialog() 
  140. {
  141. CDialog::OnInitDialog();
  142. CString str;
  143. if (session->isSend)
  144. SetTimer(IDT_BROWSE_FILE, 0, NULL);
  145. else {
  146. str.LoadString(IDS_ACCEPT_FILE);
  147. SetWindowText(str);
  148. }
  149. str.LoadString(IDS_PLEASE_WAIT);
  150. SetDlgItemText(IDC_STATUS, str);
  151. HICON icon = AfxGetApp()->LoadIcon(IDI_SENDFILE);
  152. SetIcon(icon, TRUE);
  153. SetIcon(icon, FALSE);
  154. m_faceStatic.SetIcon((HICON) icqLink->getFaceIcon(contact->face, STATUS_ONLINE));
  155. SetTimer(IDT_ANIM, 500, NULL);
  156. return TRUE;  // return TRUE unless you set the focus to a control
  157.               // EXCEPTION: OCX Property Pages should return FALSE
  158. }
  159. void CSendFileDlg::OnTimer(UINT nIDEvent) 
  160. {
  161. if (nIDEvent == IDT_BROWSE_FILE) {
  162. KillTimer(nIDEvent); // one shot trigger
  163. CFileDialog dlg(TRUE, NULL, NULL,
  164. OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, "*.*|*.*||", this);
  165. if (dlg.DoModal() != IDOK) {
  166. DestroyWindow();
  167. return;
  168. }
  169. pathName = dlg.GetPathName();
  170. fileName = dlg.GetFileName();
  171. CFile file(pathName, CFile::modeRead);
  172. fileSize = file.GetLength();
  173. m_ctlProgress.SetRange32(0, fileSize);
  174. file.Close();
  175. CString str;
  176. str.Format(IDS_TITLE_SENDFILE, (LPCTSTR) pathName);
  177. SetWindowText(str);
  178. str.Format(IDS_FILESIZE, fileSize);
  179. SetDlgItemText(IDC_FILESIZE, str);
  180. session->sendFileInfo(pathName, fileName, fileSize);
  181. } else if (nIDEvent == IDT_ANIM) {
  182. frame ^= 1;
  183. m_faceStatic.SetIcon(
  184. (HICON) icqLink->getFaceIcon(contact->face, frame ? STATUS_AWAY : STATUS_ONLINE));
  185. } else
  186. CDialog::OnTimer(nIDEvent);
  187. }
  188. void CSendFileDlg::OnCancel() 
  189. {
  190. CString title, text;
  191. title.LoadString(IDS_WARNING);
  192. text.LoadString(IDS_PROMPT_STOP);
  193. if (MessageBox(text, title, MB_YESNO) == IDYES)
  194. DestroyWindow();
  195. }
  196. void CSendFileDlg::PostNcDestroy() 
  197. {
  198. session->setListener(NULL);
  199. pWnd = this;
  200. ::SetTimer(NULL, 0, 0, onTimer);
  201. }