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

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. // SearchResultDlg.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "myicq.h"
  15. #include "SearchResultDlg.h"
  16. #include "SearchWizard.h"
  17. #include "ViewDetailDlg.h"
  18. #include "serversession.h"
  19. #include "icqlink.h"
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25. static int CALLBACK compareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  26. {
  27. SEARCH_RESULT *p1 = (SEARCH_RESULT *) lParam1;
  28. SEARCH_RESULT *p2 = (SEARCH_RESULT *) lParam2;
  29. switch (lParamSort) {
  30. case 0: // uin
  31. if (p1->uin > p2->uin)
  32. return 1;
  33. else if (p1->uin < p2->uin)
  34. return -1;
  35. return 0;
  36. case 1: // nick
  37. return p1->nick.compare(p2->nick);
  38. case 2: // province
  39. return p1->province.compare(p2->province);
  40. }
  41. return 0;
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CSearchResultDlg property page
  45. IMPLEMENT_DYNCREATE(CSearchResultDlg, CPropertyPage)
  46. CSearchResultDlg::CSearchResultDlg() : CPropertyPage(CSearchResultDlg::IDD)
  47. {
  48. //{{AFX_DATA_INIT(CSearchResultDlg)
  49. // NOTE: the ClassWizard will add member initialization here
  50. //}}AFX_DATA_INIT
  51. pageCount = 0;
  52. }
  53. CSearchResultDlg::~CSearchResultDlg()
  54. {
  55. deleteResults();
  56. }
  57. SEARCH_RESULT *CSearchResultDlg::getResult()
  58. {
  59. POSITION pos = m_ctlResult.GetFirstSelectedItemPosition();
  60. if (pos) {
  61. int sel = m_ctlResult.GetNextSelectedItem(pos);
  62. return (SEARCH_RESULT *) m_ctlResult.GetItemData(sel);
  63. }
  64. return NULL;
  65. }
  66. void CSearchResultDlg::enableControls(BOOL enable)
  67. {
  68. GetDlgItem(IDC_SEARCH_PREV)->EnableWindow(enable && curPage > 0);
  69. GetDlgItem(IDC_SEARCH_NEXT)->EnableWindow(enable && curPage < MAX_PAGES - 1);
  70. GetDlgItem(IDC_SEARCH_DETAIL)->EnableWindow(enable);
  71. GetDlgItem(IDC_SEARCH_ALL)->EnableWindow(enable);
  72. }
  73. void CSearchResultDlg::displayPage(int page, BOOL showStatus)
  74. {
  75. if (page < 0 || page >= pageCount)
  76. return;
  77. if (showStatus) {
  78. CString str;
  79. str.Format(IDS_SEARCH_PAGE_NO, page + 1);
  80. SetDlgItemText(IDC_STATUS, str);
  81. }
  82. PtrList *l = pages[page];
  83. PtrList::iterator it;
  84. for (it = l->begin(); it != l->end(); ++it) {
  85. SEARCH_RESULT *p = (SEARCH_RESULT *) *it;
  86. int image = p->face;
  87. if (!p->online)
  88. image += getApp()->nrFaces;
  89. CString str;
  90. str.Format("%lu", p->uin);
  91. int i = m_ctlResult.InsertItem(m_ctlResult.GetItemCount(), str, image);
  92. m_ctlResult.SetItemData(i, (DWORD) p);
  93. m_ctlResult.SetItemText(i, 1, p->nick.c_str());
  94. m_ctlResult.SetItemText(i, 2, p->province.c_str());
  95. }
  96. }
  97. void CSearchResultDlg::deleteResults()
  98. {
  99. for (int i = 0; i < pageCount; i++) {
  100. PtrList *l = pages[i];
  101. PtrList::iterator it;
  102. for (it = l->begin(); it != l->end(); ++it)
  103. delete (SEARCH_RESULT *) *it;
  104. l->clear();
  105. }
  106. curPage = -1;
  107. pageCount = 0;
  108. startUIN = 0;
  109. }
  110. void CSearchResultDlg::onSendError(uint32 seq)
  111. {
  112. myMessageBox(IDS_TIMEOUT, IDS_FAILED, this);
  113. }
  114. void CSearchResultDlg::onUserFound(PtrList *l)
  115. {
  116. if (!l->empty()) {
  117. pages[pageCount++] = l;
  118. displayPage(++curPage);
  119. startUIN = ((SEARCH_RESULT *) l->back())->uin;
  120. }
  121. enableControls();
  122. }
  123. void CSearchResultDlg::DoDataExchange(CDataExchange* pDX)
  124. {
  125. CPropertyPage::DoDataExchange(pDX);
  126. //{{AFX_DATA_MAP(CSearchResultDlg)
  127. DDX_Control(pDX, IDC_SEARCH_RESULT, m_ctlResult);
  128. //}}AFX_DATA_MAP
  129. }
  130. BEGIN_MESSAGE_MAP(CSearchResultDlg, CPropertyPage)
  131. //{{AFX_MSG_MAP(CSearchResultDlg)
  132. ON_BN_CLICKED(IDC_SEARCH_ALL, OnSearchAll)
  133. ON_BN_CLICKED(IDC_SEARCH_NEXT, OnSearchNext)
  134. ON_BN_CLICKED(IDC_SEARCH_DETAIL, OnSearchDetail)
  135. ON_BN_CLICKED(IDC_SEARCH_PREV, OnSearchPrev)
  136. ON_NOTIFY(LVN_COLUMNCLICK, IDC_SEARCH_RESULT, OnColumnClick)
  137. ON_NOTIFY(NM_DBLCLK, IDC_SEARCH_RESULT, OnDblclkSearchResult)
  138. //}}AFX_MSG_MAP
  139. END_MESSAGE_MAP()
  140. /////////////////////////////////////////////////////////////////////////////
  141. // CSearchResultDlg message handlers
  142. BOOL CSearchResultDlg::OnInitDialog() 
  143. {
  144. CPropertyPage::OnInitDialog();
  145. m_ctlResult.SetExtendedStyle(m_ctlResult.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
  146. m_ctlResult.SetImageList(&getApp()->smallImageList, LVSIL_SMALL);
  147. CString str;
  148. str.LoadString(IDS_UIN);
  149. m_ctlResult.InsertColumn(0, str, LVCFMT_LEFT, 90);
  150. str.LoadString(IDS_NICK);
  151. m_ctlResult.InsertColumn(1, str, LVCFMT_CENTER, 90);
  152. str.LoadString(IDS_FROM_WHERE);
  153. m_ctlResult.InsertColumn(2, str, LVCFMT_CENTER, 120);
  154. return TRUE;  // return TRUE unless you set the focus to a control
  155.               // EXCEPTION: OCX Property Pages should return FALSE
  156. }
  157. BOOL CSearchResultDlg::OnSetActive() 
  158. {
  159. ((CPropertySheet *) GetParent())->SetWizardButtons(PSWIZB_BACK | PSWIZB_NEXT);
  160. m_ctlResult.DeleteAllItems();
  161. deleteResults();
  162. CString str;
  163. str.Format(IDS_TITLE_SEARCH_RESULT, serverSession()->sessionCount);
  164. SetWindowText(str);
  165. OnSearchNext();
  166. return CPropertyPage::OnSetActive();
  167. }
  168. void CSearchResultDlg::OnSearchAll() 
  169. {
  170. CString str;
  171. str.Format(IDS_SEARCH_RESULT_ALL, pageCount);
  172. SetDlgItemText(IDC_STATUS, str);
  173. m_ctlResult.DeleteAllItems();
  174. for (int i = 0; i < pageCount; i++)
  175. displayPage(i, FALSE);
  176. }
  177. void CSearchResultDlg::OnSearchPrev()
  178. {
  179. if (--curPage <= 0)
  180. GetDlgItem(IDC_SEARCH_PREV)->EnableWindow(FALSE);
  181. m_ctlResult.DeleteAllItems();
  182. displayPage(curPage);
  183. }
  184. void CSearchResultDlg::OnSearchNext() 
  185. {
  186. m_ctlResult.DeleteAllItems();
  187. CString str;
  188. str.LoadString(IDS_SEARCHING_CONTACT);
  189. SetDlgItemText(IDC_STATUS, str);
  190. if (curPage < pageCount - 1) {
  191. if (curPage == 0)
  192. GetDlgItem(IDC_SEARCH_PREV)->EnableWindow();
  193. displayPage(++curPage);
  194. return;
  195. }
  196. CSearchWizard *wiz = (CSearchWizard *) GetParent();
  197. int mode = wiz->modeDlg.m_mode;
  198. if (mode == SEARCH_MODE_ONLINE)
  199. wiz->seq = serverSession()->searchRandom();
  200. else if (mode == SEARCH_MODE_CUSTOM) {
  201. uint32 uin = wiz->customDlg.m_uin;
  202. const char *nick = wiz->customDlg.m_nick;
  203. const char *email = wiz->customDlg.m_email;
  204. wiz->seq = serverSession()->searchCustom(uin, nick, email, startUIN);
  205. }
  206. enableControls(FALSE);
  207. }
  208. void CSearchResultDlg::OnSearchDetail() 
  209. {
  210. SEARCH_RESULT *result = getResult();
  211. if (result) {
  212. CViewDetailDlg *win = new CViewDetailDlg(result->uin);
  213. win->Create(this);
  214. }
  215. }
  216. LRESULT CSearchResultDlg::OnWizardBack() 
  217. {
  218. CSearchWizard *wiz = (CSearchWizard *) GetParent();
  219. if (wiz->modeDlg.m_mode == SEARCH_MODE_ONLINE)
  220. return IDD_SEARCH_MODE;
  221. else
  222. return CPropertyPage::OnWizardBack();
  223. }
  224. void CSearchResultDlg::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult) 
  225. {
  226. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  227. m_ctlResult.SortItems(compareFunc, (LPARAM) pNMListView->iSubItem);
  228. *pResult = 0;
  229. }
  230. LRESULT CSearchResultDlg::OnWizardNext() 
  231. {
  232. SEARCH_RESULT *result = getResult();
  233. if (!result) {
  234. myMessageBox(IDS_SELECT_CONTACT, IDS_ERROR, this);
  235. return -1;
  236. }
  237. if (result->uin == icqLink->myInfo.uin) {
  238. myMessageBox(IDS_ERROR_ADD_YOURSELF, IDS_ERROR, this);
  239. return -1;
  240. }
  241. return CPropertyPage::OnWizardNext();
  242. }
  243. void CSearchResultDlg::OnDblclkSearchResult(NMHDR* pNMHDR, LRESULT* pResult) 
  244. {
  245. OnSearchDetail();
  246. *pResult = 0;
  247. }