MainWnd.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 23k
Development Platform:

C/C++

  1. // MainWnd.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "S3DBClient.h"
  5. #include "PerformanceDlg.h"
  6. #include "LoginDlg.h"
  7. #include "MainWnd.h"
  8. #include "AddUserDlg.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. extern CS3DBClientApp theApp;
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAboutDlg dialog used for App About
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20. CAboutDlg();
  21. // Dialog Data
  22. //{{AFX_DATA(CAboutDlg)
  23. enum { IDD = IDD_ABOUTBOX };
  24. //}}AFX_DATA
  25. // ClassWizard generated virtual function overrides
  26. //{{AFX_VIRTUAL(CAboutDlg)
  27. protected:
  28. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  29. //}}AFX_VIRTUAL
  30. // Implementation
  31. protected:
  32. //{{AFX_MSG(CAboutDlg)
  33. //}}AFX_MSG
  34. DECLARE_MESSAGE_MAP()
  35. };
  36. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  37. {
  38. //{{AFX_DATA_INIT(CAboutDlg)
  39. //}}AFX_DATA_INIT
  40. }
  41. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialog::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CAboutDlg)
  45. //}}AFX_DATA_MAP
  46. }
  47. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  48. //{{AFX_MSG_MAP(CAboutDlg)
  49. // No message handlers
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CMainWnd
  54. IMPLEMENT_DYNAMIC(CMainWnd, CPropertySheet)
  55. CMainWnd::CMainWnd(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
  56. :CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
  57. {
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. AddPage( &m_page1 );
  60. AddPage( &m_page2 );
  61. }
  62. CMainWnd::CMainWnd(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
  63. :CPropertySheet(pszCaption, pParentWnd, iSelectPage)
  64. {
  65. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  66. AddPage( &m_page1 );
  67. AddPage( &m_page2 );
  68. }
  69. CMainWnd::~CMainWnd()
  70. {
  71. }
  72. BEGIN_MESSAGE_MAP(CMainWnd, CPropertySheet)
  73. //{{AFX_MSG_MAP(CMainWnd)
  74. ON_WM_PAINT()
  75. ON_WM_SYSCOMMAND()
  76. ON_COMMAND(ID_ABOUT, OnAbout)
  77. ON_COMMAND(ID_LOGIN, OnLogin)
  78. ON_COMMAND(ID_LOGOUT, OnLogout)
  79. ON_COMMAND(ID_ADDUSER, OnAdduser)
  80. ON_COMMAND(ID_DELETEUSER, OnDeleteuser)
  81. ON_COMMAND(ID_PREFERENCES, OnPreferences)
  82. ON_COMMAND(ID_EXIT, OnExit)
  83. ON_WM_DESTROY()
  84. ON_COMMAND(ID_MAINFACE, OnMainface)
  85. ON_COMMAND(ID_DATABASEUSER, OnDatabaseuser)
  86. ON_COMMAND(ID_REFRESH, OnRefresh)
  87. //}}AFX_MSG_MAP
  88. ON_MESSAGE(WM_INPUTLANGCHANGEREQUEST,OnIMEInputLangChangeRequest)
  89. ON_MESSAGE(WM_TURNTOPAGE2,OnTurntoPage2)
  90. END_MESSAGE_MAP()
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CMainWnd message handlers
  93. BOOL CMainWnd::OnInitDialog() 
  94. {
  95. CPropertySheet::OnInitDialog();
  96. InitClientSocket();
  97. CTabCtrl* pTabCtrl = GetTabControl();
  98. CRect oldTabRc;
  99. CRect newTabRc;
  100. CRect rc;
  101. GetWindowRect( &rc );
  102. if ( NULL != pTabCtrl )
  103. {
  104. pTabCtrl->GetWindowRect( &oldTabRc );
  105. }
  106. m_menu.LoadMenu( IDR_MAINMENU );
  107. SetMenu( &m_menu );
  108. if ( NULL != pTabCtrl )
  109. {
  110. pTabCtrl->GetWindowRect( &newTabRc );
  111. rc.InflateRect( 0, abs( newTabRc.top - oldTabRc.top ) / 2 );
  112. MoveWindow( &rc );
  113. }
  114. // TODO: Add extra initialization here
  115. // Add "About..." menu item to system menu.
  116. // IDM_ABOUTBOX must be in the system command range.
  117. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  118. ASSERT(IDM_ABOUTBOX < 0xF000);
  119. CMenu* pSysMenu = GetSystemMenu(FALSE);
  120. if (pSysMenu != NULL)
  121. {
  122. CString strAboutMenu;
  123. strAboutMenu.LoadString(IDS_ABOUTBOX);
  124. if (!strAboutMenu.IsEmpty())
  125. {
  126. pSysMenu->AppendMenu(MF_SEPARATOR);
  127. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  128. }
  129. }
  130. // Set the icon for this dialog.  The framework does this automatically
  131. //  when the application's main window is not a dialog
  132. SetIcon(m_hIcon, TRUE); // Set big icon
  133. SetIcon(m_hIcon, FALSE); // Set small icon
  134. SetActivePage( &m_page1 );
  135. UpdateMenuState();
  136. return TRUE;  // return TRUE unless you set the focus to a control
  137.               // EXCEPTION: OCX Property Pages should return FALSE
  138. }
  139. void CMainWnd::OnPaint() 
  140. {
  141. if (IsIconic())
  142. {
  143. CPaintDC dc(this); // device context for painting
  144. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  145. // Center icon in client rectangle
  146. int cxIcon = GetSystemMetrics(SM_CXICON);
  147. int cyIcon = GetSystemMetrics(SM_CYICON);
  148. CRect rect;
  149. GetClientRect(&rect);
  150. int x = (rect.Width() - cxIcon + 1) / 2;
  151. int y = (rect.Height() - cyIcon + 1) / 2;
  152. // Draw the icon
  153. dc.DrawIcon(x, y, m_hIcon);
  154. }
  155. else
  156. {
  157. CPropertySheet::OnPaint();
  158. }
  159. }
  160. void CMainWnd::OnAbout()
  161. {
  162. CAboutDlg dlg;
  163. dlg.DoModal();
  164. }
  165. void CMainWnd::OnSysCommand(UINT nID, LPARAM lParam) 
  166. {
  167. // TODO: Add your message handler code here and/or call default
  168. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  169. {
  170. CAboutDlg dlgAbout;
  171. dlgAbout.DoModal();
  172. }
  173. else
  174. {
  175. CPropertySheet::OnSysCommand(nID, lParam);
  176. }
  177. }
  178. HCURSOR CMainWnd::OnQueryDragIcon()
  179. {
  180. return ( HCURSOR )m_hIcon;
  181. }
  182. static DWORD CALLBACK LoginCallback( DWORD dwPara1, DWORD dwPara2 )
  183. {
  184. DWORD dwRet = 0;
  185. IBYTE* pRecvMsg = ( IBYTE* )dwPara1;
  186. int* piRet = ( int* )dwPara2;
  187. if ( ( NULL != pRecvMsg )
  188. || ( NULL != piRet ) )
  189. {
  190. DWORD dwMsgParam;
  191. memcpy( &dwMsgParam, &( pRecvMsg[1+sizeof( DWORD )] ), sizeof( DWORD ) );
  192. if ( ( DBLOGIN_R_LOGIN | DBLOGIN_R_SUCCESS ) == dwMsgParam )
  193. {
  194. *piRet = 1;
  195. }
  196. else if ( ( DBLOGIN_R_LOGIN | DBLOGIN_R_HASLOGGEDIN ) == dwMsgParam )
  197. {
  198. *piRet = -1;
  199. }
  200. else
  201. {
  202. *piRet = 0;
  203. }
  204. }
  205. return dwRet;
  206. }
  207. void CMainWnd::OnLogin() 
  208. {
  209. // TODO: Add your command handler code here
  210. if ( theApp.m_bConnected )
  211. {
  212. return;
  213. }
  214. if ( NULL != theApp.m_pClientSocket )
  215. {
  216. CLoginDlg dlg;
  217. if ( IDOK == dlg.DoModal() )
  218. {
  219. _DBLOGINSTRUCT dbLoginStruct;
  220. memset( &dbLoginStruct, 0, sizeof( _DBLOGINSTRUCT ) );
  221. memcpy( dbLoginStruct.szUserName,
  222. dlg.m_cstrUserName,
  223. dlg.m_cstrUserName.GetLength() );
  224. theApp.m_cstrCurUsername = dlg.m_cstrUserName;
  225. memcpy( dbLoginStruct.szPassword,
  226. dlg.m_cstrPassword,
  227. dlg.m_cstrPassword.GetLength() );
  228. theApp.m_cstrCurPassword = dlg.m_cstrPassword;
  229. dbLoginStruct.Size = sizeof( _DBLOGINSTRUCT );
  230. dbLoginStruct.Param = DBLOGIN_A_LOGIN;
  231. int iResult = 0;
  232. BOOL bResult =
  233. theApp.m_pClientSocket->RemoteQuery( &dbLoginStruct,
  234. LoginCallback, ( DWORD )( &iResult ), theApp.m_serverInfo.dwWaitTime );
  235. if ( bResult )
  236. {
  237. CString cstrMsg;
  238. if ( 1 == iResult ) // Log in successfully
  239. {
  240. cstrMsg = _T("Successful to log in.");
  241. UpdateMenuState( TRUE );
  242. OnRefresh();
  243. }
  244. else if ( -1 == iResult ) // Has logged in
  245. {
  246. //cstrMsg = _T("The same user have connected the database server from other machine.");
  247. //UpdateMenuState( FALSE );
  248. cstrMsg = _T("Successful to log in.");
  249. UpdateMenuState( TRUE );
  250. OnRefresh();
  251. }
  252. else
  253. {
  254. cstrMsg = _T("Failed to log in.rnMaybe the username or password you input is wrong.");
  255. }
  256. MessageBox( cstrMsg );
  257. }
  258. else
  259. {
  260. MessageBox( _T("Failed to log in"), NULL, MB_OK | MB_ICONASTERISK );
  261. }
  262. }
  263. }
  264. else
  265. {
  266. MessageBox( _T("No availd socket"), NULL, MB_OK | MB_ICONASTERISK );
  267. }
  268. }
  269. static DWORD CALLBACK LogoutCallback( DWORD dwPara1, DWORD dwPara2 )
  270. {
  271. DWORD dwRet = 0;
  272. IBYTE* pRecvMsg = ( IBYTE* )dwPara1;
  273. int* piRet = ( int* )dwPara2;
  274. if ( ( NULL != pRecvMsg )
  275. || ( NULL != piRet ) )
  276. {
  277. DWORD dwMsgParam;
  278. memcpy( &dwMsgParam, &( pRecvMsg[1+sizeof( DWORD )] ), sizeof( DWORD ) );
  279. if ( ( DBLOGIN_R_LOGOUT | DBLOGIN_R_SUCCESS ) == dwMsgParam )
  280. {
  281. *piRet = 1;
  282. }
  283. else
  284. {
  285. *piRet = -1;
  286. }
  287. }
  288. return dwRet;
  289. }
  290. void CMainWnd::OnLogout() 
  291. {
  292. // TODO: Add your command handler code here
  293. if ( !theApp.m_bConnected )
  294. {
  295. return;
  296. }
  297. if ( NULL != theApp.m_pClientSocket )
  298. {
  299. _DBLOGINSTRUCT dbLoginStruct;
  300. memset( &dbLoginStruct, 0, sizeof( _DBLOGINSTRUCT ) );
  301. memcpy( dbLoginStruct.szUserName,
  302. theApp.m_cstrCurUsername,
  303. theApp.m_cstrCurUsername.GetLength() );
  304. memcpy( dbLoginStruct.szPassword,
  305. theApp.m_cstrCurPassword,
  306. theApp.m_cstrCurPassword.GetLength() );
  307. dbLoginStruct.Size = sizeof( _DBLOGINSTRUCT );
  308. dbLoginStruct.Param = DBLOGIN_A_LOGOUT;
  309. int iResult = 0;
  310. BOOL bResult =
  311. theApp.m_pClientSocket->RemoteQuery( &dbLoginStruct,
  312. LogoutCallback, ( DWORD )( &iResult ), theApp.m_serverInfo.dwWaitTime );
  313. if ( bResult )
  314. {
  315. CString cstrMsg;
  316. if ( 1 == iResult ) // Log in successfully
  317. {
  318. cstrMsg = _T("Successful to log out.");
  319. UpdateMenuState( FALSE );
  320. OnRefresh();
  321. }
  322. else
  323. {
  324. cstrMsg = _T("Failed to log out.rnMaybe the net is busy, please try again later.");
  325. MessageBox( cstrMsg );
  326. }
  327. }
  328. else
  329. {
  330. MessageBox( _T("Failed to log out"), NULL, MB_OK | MB_ICONASTERISK );
  331. }
  332. }
  333. else
  334. {
  335. MessageBox( _T("No availd socket"), NULL, MB_OK | MB_ICONASTERISK );
  336. }
  337. }
  338. static DWORD CALLBACK AddUserCallback( DWORD dwPara1, DWORD dwPara2 )
  339. {
  340. DWORD dwRet = 0;
  341. IBYTE* pRecvMsg = ( IBYTE* )dwPara1;
  342. int* piRet = ( int* )dwPara2;
  343. if ( ( NULL != pRecvMsg )
  344. || ( NULL != piRet ) )
  345. {
  346. DWORD dwMsgParam;
  347. memcpy( &dwMsgParam, &( pRecvMsg[1+sizeof( DWORD )] ), sizeof( DWORD ) );
  348. if ( ( DBLOGIN_R_ADDDBUSER | DBLOGIN_R_SUCCESS ) == dwMsgParam )
  349. {
  350. *piRet = 1;
  351. }
  352. else if ( ( DBLOGIN_R_ADDDBUSER | DBLOGIN_R_HASNOENOUGHPRIORITY ) == dwMsgParam )
  353. {
  354. *piRet = -1;
  355. }
  356. else if ( ( DBLOGIN_R_ADDDBUSER | DBLOGIN_R_USERHASEXISTED ) == dwMsgParam )
  357. {
  358. *piRet = -2;
  359. }
  360. else
  361. {
  362. *piRet = 0;
  363. }
  364. }
  365. return dwRet;
  366. }
  367. void CMainWnd::OnAdduser() 
  368. {
  369. // TODO: Add your command handler code here
  370. if ( !theApp.m_bConnected )
  371. {
  372. return;
  373. }
  374. if ( NULL != theApp.m_pClientSocket )
  375. {
  376. CAddUserDlg dlg;
  377. if ( IDOK == dlg.DoModal() )
  378. {
  379. _DBLOGINSTRUCT dbLoginStruct;
  380. memset( &dbLoginStruct, 0, sizeof( _DBLOGINSTRUCT ) );
  381. memcpy( dbLoginStruct.szUserName,
  382. theApp.m_cstrCurUsername,
  383. theApp.m_cstrCurUsername.GetLength() );
  384. memcpy( dbLoginStruct.szPassword,
  385. theApp.m_cstrCurPassword,
  386. theApp.m_cstrCurPassword.GetLength() );
  387. dbLoginStruct.Size =
  388. sizeof( _DBLOGINSTRUCT )
  389. + def_DBUSERNAME_MAX_LEN
  390. + def_DBPASSWORD_MAX_LEN
  391. + sizeof( short int );
  392. dbLoginStruct.Param = DBLOGIN_A_ADDDBUSER;
  393. IBYTE* pSendData = new IBYTE[dbLoginStruct.Size];
  394. if ( NULL != pSendData )
  395. {
  396. memset( pSendData, 0, dbLoginStruct.Size );
  397. DWORD dwOffset = 0;
  398. memcpy( pSendData, &dbLoginStruct, sizeof( _DBLOGINSTRUCT ) );
  399. dwOffset += sizeof( _DBLOGINSTRUCT );
  400. memcpy( pSendData + dwOffset, dlg.m_cstrName, dlg.m_cstrName.GetLength() );
  401. dwOffset += def_DBUSERNAME_MAX_LEN;
  402. memcpy( pSendData + dwOffset, dlg.m_cstrPassword, dlg.m_cstrPassword.GetLength() );
  403. dwOffset += def_DBPASSWORD_MAX_LEN;
  404. memcpy( pSendData + dwOffset, &( dlg.m_siPriority ), sizeof( short int ) );
  405. dwOffset += sizeof( short int );
  406. int iResult = 0;
  407. BOOL bResult =
  408. theApp.m_pClientSocket->RemoteQuery( ( _PDBLOGINSTRUCT )pSendData,//&dbLoginStruct,
  409. AddUserCallback, ( DWORD )( &iResult ), theApp.m_serverInfo.dwWaitTime );
  410. if ( bResult )
  411. {
  412. CString cstrMsg;
  413. if ( 1 == iResult ) // Log in successfully
  414. {
  415. OnRefresh();
  416. }
  417. else if ( -1 == iResult )
  418. {
  419. cstrMsg =
  420. _T("Has no enough priority to carry out this operation.");
  421. MessageBox( cstrMsg );
  422. }
  423. else if ( -2 == iResult )
  424. {
  425. cstrMsg.Format( _T("User "%s" has existed."), dlg.m_cstrName );
  426. }
  427. else
  428. {
  429. cstrMsg =
  430. _T("Failed to access database.rnMaybe the net is busy, please try again later.");
  431. MessageBox( cstrMsg );
  432. }
  433. }
  434. else
  435. {
  436. MessageBox( _T("Failed to access database.rnMaybe the net is busy, please try again later."), NULL, MB_OK | MB_ICONASTERISK );
  437. }
  438. delete []pSendData;
  439. pSendData = NULL;
  440. }
  441. else
  442. {
  443. MessageBox( _T("Failed to allocate memory.") );
  444. }
  445. }
  446. }
  447. else
  448. {
  449. MessageBox( _T("No availd socket"), NULL, MB_OK | MB_ICONASTERISK );
  450. }
  451. }
  452. static DWORD CALLBACK DeleteUserCallBack( DWORD dwPara1, DWORD dwPara2 )
  453. {
  454. DWORD dwRet = 0;//DBLOGIN_R_HASNOENOUGHPRIORITY
  455. IBYTE* pRecvMsg = ( IBYTE* )dwPara1;
  456. int* piRet = ( int* )dwPara2;
  457. if ( ( NULL != pRecvMsg )
  458. || ( NULL != piRet ) )
  459. {
  460. DWORD dwMsgParam;
  461. memcpy( &dwMsgParam, &( pRecvMsg[1+sizeof( DWORD )] ), sizeof( DWORD ) );
  462. if ( ( DBLOGIN_R_DELDBUSER | DBLOGIN_R_SUCCESS ) == dwMsgParam )
  463. {
  464. *piRet = 1;
  465. }
  466. else if ( ( DBLOGIN_R_DELDBUSER | DBLOGIN_R_HASNOENOUGHPRIORITY ) == dwMsgParam )
  467. {
  468. *piRet = -1;
  469. }
  470. else
  471. {
  472. *piRet = 0;
  473. }
  474. }
  475. return dwRet;
  476. }
  477. void CMainWnd::OnDeleteuser() 
  478. {
  479. // TODO: Add your command handler code here
  480. if ( ( &m_page2 == GetActivePage() )
  481. && theApp.m_bConnected )
  482. {
  483. UINT uSelectedCount = m_page2.m_dbUserListCtrl.GetSelectedCount();
  484. int  nItem = -1;
  485. if ( 1 == uSelectedCount )
  486. {
  487. nItem = m_page2.m_dbUserListCtrl.GetNextItem( nItem, LVNI_SELECTED );
  488. ASSERT(nItem != -1);
  489. CString cstrSelName = m_page2.m_dbUserListCtrl.GetItemText( nItem, 0 );
  490. if ( NULL != theApp.m_pClientSocket )
  491. {
  492. _DBLOGINSTRUCT dbLoginStruct;
  493. memset( &dbLoginStruct, 0, sizeof( _DBLOGINSTRUCT ) );
  494. memcpy( dbLoginStruct.szUserName,
  495. theApp.m_cstrCurUsername,
  496. theApp.m_cstrCurUsername.GetLength() );
  497. memcpy( dbLoginStruct.szPassword,
  498. theApp.m_cstrCurPassword,
  499. theApp.m_cstrCurPassword.GetLength() );
  500. dbLoginStruct.Size = sizeof( _DBLOGINSTRUCT ) + def_DBUSERNAME_MAX_LEN;
  501. dbLoginStruct.Param = DBLOGIN_A_DELDBUSER;
  502. IBYTE* pSendBuf = new IBYTE[dbLoginStruct.Size];
  503. memset( pSendBuf, 0, dbLoginStruct.Size );
  504. if ( NULL != pSendBuf )
  505. {
  506. memcpy( pSendBuf, &dbLoginStruct, sizeof( _DBLOGINSTRUCT ) );
  507. memcpy( pSendBuf + sizeof( _DBLOGINSTRUCT ), cstrSelName, cstrSelName.GetLength() );
  508. CString cstrMsg;
  509. int iResult = 0;
  510. BOOL bResult =
  511. theApp.m_pClientSocket->RemoteQuery( ( _PDBLOGINSTRUCT )pSendBuf,
  512. DeleteUserCallBack, ( DWORD )( &iResult ), theApp.m_serverInfo.dwWaitTime );
  513. if ( bResult )
  514. {
  515. if ( 1 == iResult )
  516. {
  517. if ( theApp.m_cstrCurUsername == cstrSelName )
  518. {
  519. UpdateMenuState( FALSE );
  520. }
  521. OnRefresh();
  522. }
  523. else if ( -1 == iResult )
  524. {
  525. cstrMsg = ( _T("You have no enough priority to carry out this operation.") );
  526. MessageBox( cstrMsg );
  527. }
  528. else
  529. {
  530. cstrMsg.Format( _T("Failed to delete "%s".rnMaybe the net is busy, please try again later."), cstrSelName );
  531. MessageBox( cstrMsg );
  532. }
  533. }
  534. else
  535. {
  536. cstrMsg.Format( _T("Failed to delete "%s".rnMaybe the net is busy, please try again later."), cstrSelName );
  537. MessageBox( cstrMsg );
  538. }
  539. delete []pSendBuf;
  540. pSendBuf = NULL;
  541. }
  542. else
  543. {
  544. MessageBox( _T("Failed to allocate memory.") );
  545. }
  546. }
  547. else
  548. {
  549. MessageBox( _T("No availd socket"), NULL, MB_OK | MB_ICONASTERISK );
  550. }
  551. }
  552. }
  553. }
  554. void CMainWnd::OnPreferences() 
  555. {
  556. // TODO: Add your command handler code here
  557. CPerformanceDlg dlg;
  558. dlg.SetAddress( theApp.m_serverInfo.szDBServerIP );
  559. dlg.m_siDBServerPort = theApp.m_serverInfo.siDBServerPort;
  560. dlg.m_siLocalPort = theApp.m_serverInfo.siLocalPort;
  561. dlg.m_dwWaitTime = theApp.m_serverInfo.dwWaitTime;
  562. if ( IDOK == dlg.DoModal() )
  563. {
  564. theApp.m_serverInfo.siDBServerPort = dlg.m_siDBServerPort;
  565. BYTE byField[4];
  566. dlg.GetAddress( byField[0], byField[1],
  567. byField[2], byField[3] );
  568. sprintf( theApp.m_serverInfo.szDBServerIP, "%d.%d.%d.%d",
  569. byField[0], byField[1], byField[2], byField[3] );
  570. theApp.m_serverInfo.siLocalPort = dlg.m_siLocalPort;
  571. theApp.m_serverInfo.dwWaitTime = dlg.m_dwWaitTime;
  572. }
  573. }
  574. void CMainWnd::OnExit() 
  575. {
  576. // TODO: Add your command handler code here
  577. m_page1.OnCancel();
  578. m_page2.OnCancel();
  579. EndDialog( IDCANCEL );
  580. }
  581. void CMainWnd::OnDestroy() 
  582. {
  583. CPropertySheet::OnDestroy();
  584. // TODO: Add your message handler code here
  585. char szSetupFilePath[MAX_PATH+1];
  586. KPIGetExePath( szSetupFilePath, MAX_PATH );
  587. strcat( szSetupFilePath, "setup.ini" );
  588. char szPort[32];
  589. sprintf( szPort, "%d", theApp.m_serverInfo.siLocalPort );
  590. KPIWritePrivateProfileString( _T("Local"), _T("port"), szPort, szSetupFilePath );
  591. sprintf( szPort, "%d", theApp.m_serverInfo.dwWaitTime );
  592. KPIWritePrivateProfileString( _T("Local"), _T("waittime"), szPort, szSetupFilePath );
  593. KPIWritePrivateProfileString( _T("DBServer"), _T("addr"), theApp.m_serverInfo.szDBServerIP, szSetupFilePath );
  594. sprintf( szPort, "%d", theApp.m_serverInfo.siDBServerPort );
  595. KPIWritePrivateProfileString( _T("DBServer"), _T("port"), szPort, szSetupFilePath );
  596. OnLogout();
  597. CClientSocket::ReleaseInstance();
  598. }
  599. void CMainWnd::UpdateMenuState( BOOL bEnable /* = FALSE  */ )
  600. {
  601. theApp.m_bConnected = bEnable;
  602. if ( m_menu.GetSafeHmenu() )
  603. {
  604. m_menu.EnableMenuItem( ID_LOGIN, theApp.m_bConnected );
  605. m_menu.EnableMenuItem( ID_LOGOUT, !theApp.m_bConnected );
  606. m_menu.EnableMenuItem( ID_NEWACCOUNT, !theApp.m_bConnected );
  607. m_menu.EnableMenuItem( ID_DELETEACCOUNT, !theApp.m_bConnected );
  608. m_menu.EnableMenuItem( ID_ADDUSER, !theApp.m_bConnected );
  609. m_menu.EnableMenuItem( ID_DELETEUSER, !theApp.m_bConnected );
  610. m_menu.EnableMenuItem( ID_REFRESH, !theApp.m_bConnected );
  611. m_menu.EnableMenuItem( ID_PREFERENCES, theApp.m_bConnected );
  612. }
  613. }
  614. void CMainWnd::InitClientSocket()
  615. {
  616. char szSetupFilePath[MAX_PATH+1];
  617. KPIGetExePath( szSetupFilePath, MAX_PATH );
  618. strcat( szSetupFilePath, "setup.ini" );
  619. char szPort[32];
  620. KPIGetPrivateProfileString( _T("Local"), _T("port"), _T("8000"),
  621. szPort, 32, szSetupFilePath );
  622. theApp.m_serverInfo.siLocalPort = atoi( szPort );
  623. KPIGetPrivateProfileString( _T("Local"), _T("waittime"), _T("6000"),
  624. szPort, 32, szSetupFilePath );
  625. theApp.m_serverInfo.dwWaitTime = atoi( szPort );
  626. KPIGetPrivateProfileString( _T("DBServer"), _T("addr"), _T(""),
  627. theApp.m_serverInfo.szDBServerIP, 16 * sizeof( TCHAR ), szSetupFilePath );
  628. KPIGetPrivateProfileString( _T("DBServer"), _T("port"), _T(""),
  629. szPort, 32, szSetupFilePath );
  630. theApp.m_serverInfo.siDBServerPort = atoi( szPort );
  631. theApp.m_pClientSocket = CClientSocket::Instance( &( theApp.m_serverInfo.siLocalPort ) );
  632. if ( NULL != theApp.m_pClientSocket )
  633. {
  634. theApp.m_pClientSocket->SetServerAddr( theApp.m_serverInfo.szDBServerIP,
  635. theApp.m_serverInfo.siDBServerPort );
  636. theApp.m_pClientSocket->Connect( theApp.m_serverInfo.szDBServerIP,
  637. theApp.m_serverInfo.siDBServerPort );
  638. theApp.m_pClientSocket->Start();
  639. }
  640. }
  641. void CMainWnd::OnIMEInputLangChangeRequest( WPARAM wParam, LPARAM lParam )
  642. {
  643. ActivateKeyboardLayout( HKL( lParam ), wParam );
  644. }
  645. void CMainWnd::OnMainface() 
  646. {
  647. // TODO: Add your command handler code here
  648. SetActivePage( &m_page1 );
  649. }
  650. void CMainWnd::OnDatabaseuser() 
  651. {
  652. // TODO: Add your command handler code here
  653. SetActivePage( &m_page2 );
  654. if ( theApp.m_bConnected )
  655. {
  656. OnRefresh();
  657. }
  658. }
  659. void CMainWnd::OnRefresh() 
  660. {
  661. // TODO: Add your command handler code here
  662. if ( m_page2.m_dbUserListCtrl.GetSafeHwnd() )
  663. //if ( GetActivePage() == &m_page2 )
  664. {
  665. if ( theApp.m_bConnected )
  666. QueryUserlist( theApp.m_cstrCurUsername, theApp.m_cstrCurPassword );
  667. else
  668. m_page2.m_dbUserListCtrl.DeleteAllItems();
  669. }
  670. if ( GetActivePage() == &m_page1 )
  671. {
  672. m_page1.UpdateUI();
  673. }
  674. }
  675. static DWORD CALLBACK QueryUserlistCallback( DWORD dwPara1, DWORD dwPara2 )
  676. {
  677. DWORD dwRet = 0;
  678. IBYTE* pRecvBuf = ( IBYTE* )dwPara1;
  679. CListCtrl* pListCtrl = ( CListCtrl* )dwPara2;
  680. if ( ( NULL != pRecvBuf )
  681. && ( NULL != pListCtrl ) )
  682. {
  683. if ( pListCtrl->GetSafeHwnd() )
  684. {
  685. CClientSocket::_USERINFO item;
  686. DWORD dwSize = 0;
  687. DWORD dwUserListSize = 0;
  688. memcpy( &dwSize, &pRecvBuf[1], sizeof( DWORD ) );
  689. dwUserListSize = dwSize - sizeof( _DBLOGINSTRUCT ) - sizeof( GUID );
  690. if ( 0 == dwUserListSize % sizeof( CClientSocket::_USERINFO ) )
  691. {
  692. int iItemNum =
  693. dwUserListSize / sizeof( CClientSocket::_USERINFO );
  694. pListCtrl->DeleteAllItems();
  695. for ( int i = 0; i < iItemNum; i++ )
  696. {
  697. memset( &item, 0, sizeof( CClientSocket::_USERINFO ) );
  698. memcpy( &item,
  699. &pRecvBuf[1+sizeof( _DBLOGINSTRUCT )+sizeof( GUID )+i*sizeof( CClientSocket::_USERINFO )],
  700. sizeof( CClientSocket::_USERINFO ) );
  701. pListCtrl->InsertItem( i, item.szUserName );
  702. pListCtrl->SetItemText( i, 1, item.szHostAddr );
  703. CString cstrPriority;
  704. if ( 0 == item.siPriority )
  705. {
  706. cstrPriority = _T("Read");
  707. }
  708. else if ( 1 == item.siPriority )
  709. {
  710. cstrPriority = _T("Write");
  711. }
  712. else if ( 2 == item.siPriority )
  713. {
  714. cstrPriority = _T("Read-Write");
  715. }
  716. else if ( 3 == item.siPriority )
  717. {
  718. cstrPriority = _T("Administrator");
  719. }
  720. pListCtrl->SetItemText( i, 2, cstrPriority );
  721. CString cstrState;
  722. if ( TRUE == item.bLoggedin )
  723. {
  724. cstrState = _T("Connected");
  725. }
  726. else
  727. {
  728. cstrState = _T("Disconnected");
  729. }
  730. pListCtrl->SetItemText( i, 3, cstrState );
  731. pListCtrl->SetItemText( i, 4, item.szLastLoginTime );
  732. pListCtrl->SetItemText( i, 5, item.szLastLogoutTime );
  733. }
  734. }
  735. }
  736. }
  737. return dwRet;
  738. }
  739. BOOL CMainWnd::QueryUserlist(const char *lpszUserName, const char *lpszPassword)
  740. {
  741. BOOL bRet = FALSE;
  742. if ( ( NULL == lpszUserName )
  743. || ( NULL == lpszPassword ) )
  744. {
  745. bRet = FALSE;
  746. return bRet;
  747. }
  748. if ( ( 0 == lpszUserName[0] )
  749. || ( 0 == lpszPassword[0] ) )
  750. {
  751. bRet = FALSE;
  752. return bRet;
  753. }
  754. if ( NULL != theApp.m_pClientSocket )
  755. {
  756. _DBLOGINSTRUCT dbLoginStruct;
  757. memset( &dbLoginStruct, 0, sizeof( _DBLOGINSTRUCT ) );
  758. memcpy( dbLoginStruct.szUserName,
  759. theApp.m_cstrCurUsername,
  760. theApp.m_cstrCurUsername.GetLength() );
  761. memcpy( dbLoginStruct.szPassword,
  762. theApp.m_cstrCurPassword,
  763. theApp.m_cstrCurPassword.GetLength() );
  764. dbLoginStruct.Size = sizeof( _DBLOGINSTRUCT );
  765. dbLoginStruct.Param = DBLOGIN_A_QUERYUSERLIST;
  766. CListCtrl* pListCtrl = &( m_page2.m_dbUserListCtrl );
  767. BOOL bResult =
  768. theApp.m_pClientSocket->RemoteQuery( &dbLoginStruct,
  769. QueryUserlistCallback, ( DWORD )( pListCtrl ), theApp.m_serverInfo.dwWaitTime );
  770. if ( bResult )
  771. {
  772. bRet = TRUE;
  773. }
  774. else
  775. {
  776. MessageBox( _T("Failed to query user list.rnMaybe the net is busy, please try again later."),
  777. NULL, MB_OK | MB_ICONASTERISK );
  778. }
  779. }
  780. else
  781. {
  782. MessageBox( _T("No availd socket"), NULL, MB_OK | MB_ICONASTERISK );
  783. }
  784. return bRet;
  785. }
  786. void CMainWnd::OnTurntoPage2( WPARAM wParam, LPARAM lParam )
  787. {
  788. OnRefresh();
  789. }