Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
7.5 KiB

  1. // emshellDoc.cpp : implementation of the CEmshellDoc class
  2. //
  3. #include "stdafx.h"
  4. #include "emshell.h"
  5. #include "emshellview.h"
  6. #include "emshellDoc.h"
  7. #include "mainfrm.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CEmshellDoc
  15. IMPLEMENT_DYNCREATE(CEmshellDoc, CDocument)
  16. BEGIN_MESSAGE_MAP(CEmshellDoc, CDocument)
  17. //{{AFX_MSG_MAP(CEmshellDoc)
  18. ON_COMMAND(ID_FILE_CONNECT, OnFileConnect)
  19. ON_COMMAND(ID_FILE_DISCONNECT, OnFileDisconnect)
  20. ON_UPDATE_COMMAND_UI(ID_FILE_DISCONNECT, OnUpdateFileDisconnect)
  21. //}}AFX_MSG_MAP
  22. END_MESSAGE_MAP()
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CEmshellDoc construction/destruction
  25. CEmshellDoc::CEmshellDoc()
  26. {
  27. // TODO: add one-time construction code here
  28. m_pIEmManager = NULL;
  29. m_bConnectedToServer = FALSE;
  30. }
  31. CEmshellDoc::~CEmshellDoc()
  32. {
  33. //m_pEImManager should have been released in OnDestroy()
  34. _ASSERTE(m_pIEmManager == NULL);
  35. }
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CEmshellDoc serialization
  38. void CEmshellDoc::Serialize(CArchive& ar)
  39. {
  40. if (ar.IsStoring())
  41. {
  42. // TODO: add storing code here
  43. }
  44. else
  45. {
  46. // TODO: add loading code here
  47. }
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CEmshellDoc diagnostics
  51. #ifdef _DEBUG
  52. void CEmshellDoc::AssertValid() const
  53. {
  54. CDocument::AssertValid();
  55. }
  56. void CEmshellDoc::Dump(CDumpContext& dc) const
  57. {
  58. CDocument::Dump(dc);
  59. }
  60. #endif //_DEBUG
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CEmshellDoc commands
  63. HRESULT CEmshellDoc::ConnectToServer(CString &strServer)
  64. {
  65. COSERVERINFO si;
  66. MULTI_QI qi;
  67. HRESULT hr = E_FAIL;
  68. POSITION pos = NULL;
  69. CEmshellView* pView = NULL;
  70. //NOTE: We need to determine if this remains
  71. //Synchronize the dialog controls to their member vairables
  72. // UpdateData(TRUE);
  73. //***********************************
  74. si.dwReserved1 = 0;
  75. //Since we know the server name won't be changed by the COM call, we'll cast it
  76. //to an LPTSTR so we can assign it to the COSERVERINFO.pwszName data member
  77. si.pwszName = (LPTSTR) (LPCTSTR) strServer;
  78. si.pAuthInfo = NULL;
  79. si.dwReserved2 = 0;
  80. //Later, we might want to get a whole slew of interfaces, for now
  81. //just get the EmManager
  82. qi.pIID = &IID_IEmManager;
  83. qi.pItf = NULL;
  84. qi.hr = 0;
  85. do {
  86. hr = CoCreateInstanceEx(CLSID_EmManager, NULL, CLSCTX_ALL, &si, 1, &qi);
  87. if ( FAILED(hr) ) break;
  88. //Store off the pointer to the EmManager interface
  89. m_pIEmManager = (IEmManager*) qi.pItf;
  90. //Set the connected to server flag
  91. m_bConnectedToServer = TRUE;
  92. //Append the build number to the server name
  93. // AppendVersionOfShell(strServer)
  94. //Set the title to the server we're connected too
  95. SetTitle(strServer);
  96. //Store off the server name
  97. m_strServerName = strServer;
  98. CString strStatus;
  99. strStatus.LoadString(IDS_CONNECTED);
  100. ((CMainFrame*)AfxGetMainWnd())->GetStatusBar()->SetPaneText(1, strStatus);
  101. pos = GetFirstViewPosition();
  102. if ( pos == NULL ) break;
  103. pView = (CEmshellView*) GetNextView(pos);
  104. if ( pView == NULL ) break;
  105. //Maybe we should move this into the view... Say PostConnectServer()
  106. EMShellViewState state = pView->GetViewState();
  107. pView->SetShellState(state);
  108. pView->ListCtrlPopulate(state);
  109. pView->m_mainListControl.ResizeColumnsFitScreen();
  110. DWORD dwPollSessionsFreq = 30L; // default is 30 secs
  111. ((CEmshellApp*)AfxGetApp())->GetEmShellRegOptions( TRUE, &dwPollSessionsFreq );
  112. pView->SetTimer(1, dwPollSessionsFreq*1000, 0);
  113. } while (FALSE);
  114. if (FAILED(hr)) {
  115. ((CEmshellApp*)AfxGetApp())->DisplayErrMsgFromHR(hr);
  116. }
  117. return hr;
  118. }
  119. BOOL CEmshellDoc::DisconnectFromServer()
  120. {
  121. BOOL bDisconnected = FALSE;
  122. CEmshellView* pView = NULL;
  123. POSITION pos = NULL;
  124. CString strText;
  125. strText.LoadString(IDS_DISCONNECTWARNING);
  126. do {
  127. pos = GetFirstViewPosition();
  128. if ( pos == NULL ) break;
  129. pView = (CEmshellView*) GetNextView(pos);
  130. if ( pView == NULL ) break;
  131. //Set the cursor to a wait cursor
  132. CWaitCursor wait;
  133. bDisconnected = TRUE;
  134. //Reuse strText
  135. strText.LoadString(IDS_DISCONNECTED);
  136. //Set the title to the server we're connected to
  137. SetTitle(strText);
  138. ((CMainFrame*)AfxGetMainWnd())->GetStatusBar()->SetPaneText(1, strText);
  139. //We need to make sure the timer doesn't get through when the list is empty
  140. //Destroy the timer first
  141. pView->KillTimer(1);
  142. //Clear all the sessions from the session table
  143. pView->ClearSessionTable();
  144. //Clear all the items from the list control
  145. pView->ListCtrlClear();
  146. //Release the m_pIEmManager
  147. SAFE_RELEASEIX(m_pIEmManager);
  148. //Set the connected state to false
  149. m_bConnectedToServer = FALSE;
  150. } while (FALSE);
  151. return bDisconnected;
  152. }
  153. void CEmshellDoc::OnFileConnect()
  154. {
  155. //Create the connection dialog
  156. int result = 0;
  157. HRESULT hr = E_FAIL;
  158. BOOL bDisconnected = FALSE;
  159. CString strText, strCaption;
  160. do {
  161. if (m_bConnectedToServer) {
  162. strText.LoadString(IDS_DISCONNECTWARNING);
  163. strCaption.LoadString(IDS_DISCONNECTCAPTION);
  164. //Notify the user that they are going to disconnect their current
  165. //session if they want to continue
  166. if (AfxGetMainWnd()->MessageBox(strText, strCaption, MB_OKCANCEL|MB_ICONWARNING) == IDCANCEL) break;
  167. DisconnectFromServer();
  168. }
  169. //Open the connection dialog
  170. result = (int)m_connectionDlg.DoModal();
  171. //If the user selected OK, call ConnectToServer
  172. if (result == IDOK) {
  173. //Set the cursor to a wait cursor
  174. CWaitCursor wait;
  175. //Store off the server name and
  176. if (m_connectionDlg.m_bLocalServer)
  177. hr = ConnectToServer(m_connectionDlg.m_strLocalMachineName);
  178. else
  179. hr = ConnectToServer(m_connectionDlg.m_strRemoteMachineName);
  180. if (SUCCEEDED(hr)) break;
  181. }
  182. } while (result == IDOK);
  183. }
  184. void CEmshellDoc::OnFileDisconnect()
  185. {
  186. CString strText, strCaption;
  187. strText.LoadString(IDS_DISCONNECTWARNING);
  188. strCaption.LoadString(IDS_DISCONNECTCAPTION);
  189. if ( AfxGetMainWnd()->MessageBox( strText, strCaption, MB_OKCANCEL|MB_ICONWARNING ) == IDOK )
  190. DisconnectFromServer();
  191. }
  192. IEmManager* CEmshellDoc::GetEmManager()
  193. {
  194. return m_pIEmManager;
  195. }
  196. BOOL CEmshellDoc::GetConnectedToServerState()
  197. {
  198. return m_bConnectedToServer;
  199. }
  200. BOOL CEmshellDoc::CanCloseFrame(CFrameWnd* pFrame)
  201. {
  202. BOOL bDisconnect = TRUE;
  203. // TODO: Add your specialized code here and/or call the base class
  204. //Ask the user if they would like to close the connection to the server
  205. if ( m_bConnectedToServer ) {
  206. bDisconnect = DisconnectFromServer();
  207. }
  208. if ( bDisconnect ) {
  209. bDisconnect = CDocument::CanCloseFrame(pFrame);
  210. }
  211. return bDisconnect;
  212. }
  213. BOOL CEmshellDoc::OnNewDocument()
  214. {
  215. // TODO: Add your specialized code here and/or call the base class
  216. CString strVal;
  217. strVal.LoadString(IDS_DISCONNECTED);
  218. SetTitle(strVal);
  219. CWnd * pWnd = AfxGetMainWnd();
  220. if ( pWnd && pWnd->IsKindOf ( RUNTIME_CLASS (CMainFrame) ) ) {
  221. CMainFrame *pFrame = ( CMainFrame* ) pWnd;
  222. pFrame->GetStatusBar()->SetPaneText ( 1, strVal );
  223. }
  224. return CDocument::OnNewDocument();
  225. }
  226. void CEmshellDoc::OnUpdateFileDisconnect(CCmdUI* pCmdUI)
  227. {
  228. // TODO: Add your command update UI handler code here
  229. pCmdUI->Enable ( GetConnectedToServerState() );
  230. }
  231. LPCTSTR CEmshellDoc::GetServerName()
  232. {
  233. return m_strServerName;
  234. }
  235. void CEmshellDoc::AppendVersionOfShell(CString &strVersion)
  236. {
  237. // GetFileVersionInfoSize();
  238. // GetFileVersionInfo();
  239. // VerQueryValue();
  240. }