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.

324 lines
8.2 KiB

  1. // KeyDView.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "KeyRing.h"
  5. #include "MainFrm.h"
  6. #include "keyobjs.h"
  7. #include "KeyDView.h"
  8. #include "machine.h"
  9. #include "KRDoc.h"
  10. #include "KRView.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. extern CKeyRingView* g_pTreeView;
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CKeyDataView
  19. IMPLEMENT_DYNCREATE(CKeyDataView, CFormView)
  20. CKeyDataView::CKeyDataView()
  21. : CFormView(CKeyDataView::IDD)
  22. {
  23. //{{AFX_DATA_INIT(CKeyDataView)
  24. m_szBits = _T("");
  25. m_szCountry = _T("");
  26. m_szName = _T("");
  27. m_szDNNetAddress = _T("");
  28. m_szOrganization = _T("");
  29. m_szStatus = _T("");
  30. m_szUnit = _T("");
  31. m_szState = _T("");
  32. m_szLocality = _T("");
  33. m_szExpires = _T("");
  34. m_szStarts = _T("");
  35. //}}AFX_DATA_INIT
  36. }
  37. CKeyDataView::~CKeyDataView()
  38. {
  39. }
  40. void CKeyDataView::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CFormView::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(CKeyDataView)
  44. DDX_Control(pDX, IDC_GROUP_DN, m_ctrlGroupDN);
  45. DDX_Control(pDX, IDC_STATIC_STARTS, m_ctrlStarts);
  46. DDX_Control(pDX, IDC_STATIC_EXPIRES, m_ctrlExpires);
  47. DDX_Control(pDX, IDC_STATIC_STATEPROVICE, m_ctrlState);
  48. DDX_Control(pDX, IDC_STATIC_LOCALITY, m_ctrlLocality);
  49. DDX_Control(pDX, IDC_STATIC_UNIT, m_ctrlUnit);
  50. DDX_Control(pDX, IDC_STATIC_ORG, m_ctrlOrg);
  51. DDX_Control(pDX, IDC_STATIC_NETADDR, m_ctrlNetAddr);
  52. DDX_Control(pDX, IDC_STATIC_NAME, m_ctrlStaticName);
  53. DDX_Control(pDX, IDC_STATIC_COUNTRY, m_ctrlCountry);
  54. DDX_Control(pDX, IDC_STATIC_BITS, m_ctrlBits);
  55. DDX_Control(pDX, IDC_VIEWKEY_NAME, m_ctrlName);
  56. DDX_Text(pDX, IDC_VIEWKEY_BITS, m_szBits);
  57. DDX_Text(pDX, IDC_VIEWKEY_COUNTRY, m_szCountry);
  58. DDX_Text(pDX, IDC_VIEWKEY_NAME, m_szName);
  59. DDX_Text(pDX, IDC_VIEWKEY_NETADDR, m_szDNNetAddress);
  60. DDX_Text(pDX, IDC_VIEWKEY_ORG, m_szOrganization);
  61. DDX_Text(pDX, IDC_VIEWKEY_STATUS, m_szStatus);
  62. DDX_Text(pDX, IDC_VIEWKEY_UNIT, m_szUnit);
  63. DDX_Text(pDX, IDC_VIEWKEY_STATEPROVINCE, m_szState);
  64. DDX_Text(pDX, IDC_VIEWKEY_LOCALITY, m_szLocality);
  65. DDX_Text(pDX, IDC_VIEWKEY_EXPIRES, m_szExpires);
  66. DDX_Text(pDX, IDC_VIEWKEY_STARTS, m_szStarts);
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP(CKeyDataView, CFormView)
  70. //{{AFX_MSG_MAP(CKeyDataView)
  71. ON_EN_CHANGE(IDC_VIEWKEY_NAME, OnChangeViewkeyName)
  72. //}}AFX_MSG_MAP
  73. END_MESSAGE_MAP()
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CKeyDataView diagnostics
  76. #ifdef _DEBUG
  77. void CKeyDataView::AssertValid() const
  78. {
  79. CFormView::AssertValid();
  80. }
  81. void CKeyDataView::Dump(CDumpContext& dc) const
  82. {
  83. CFormView::Dump(dc);
  84. }
  85. #endif //_DEBUG
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CKeyDataView message handlers
  88. //----------------------------------------------------------------------------
  89. void CKeyDataView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
  90. {
  91. CKey* pKey = (CKey*)g_pTreeView->PGetSelectedItem();
  92. // we only work on selection changes an "none" hints
  93. switch ( lHint )
  94. {
  95. case HINT_None:
  96. case HINT_ChangeSelection:
  97. break;
  98. default:
  99. return;
  100. }
  101. // if there is no selected key, bail
  102. if ( !pKey || !pKey->IsKindOf(RUNTIME_CLASS(CKey)) )
  103. {
  104. EnableDataView( FALSE, FALSE );
  105. return;
  106. }
  107. else
  108. {
  109. EnableDataView( TRUE, TRUE );
  110. }
  111. // put all the string stuff into a try/catch to get mem errors
  112. try
  113. {
  114. ASSERT( pKey );
  115. ASSERT( pKey->IsKindOf(RUNTIME_CLASS(CKey)) );
  116. // set the character strings
  117. m_szName = pKey->GetName();
  118. // get certificate specific info directly out of the certifiect
  119. FillInCrackedInfo( pKey );
  120. // if this key is not completed, disable the button altogether
  121. if ( !pKey->m_pCertificate )
  122. // the item is not completed
  123. {
  124. EnableDataView( FALSE, TRUE );
  125. m_szStatus.LoadString(IDS_KEY_STATUS_INCOMPLETE);
  126. }
  127. // set the data into the form
  128. UpdateData( FALSE );
  129. }
  130. catch( CException e )
  131. {
  132. }
  133. }
  134. //----------------------------------------------------------------------------
  135. void CKeyDataView::OnChangeViewkeyName()
  136. {
  137. CKey* pKey = (CKey*)g_pTreeView->PGetSelectedItem();
  138. if ( !pKey || !pKey->IsKindOf(RUNTIME_CLASS(CKey)) )
  139. {
  140. ASSERT( FALSE );
  141. return;
  142. }
  143. // get the data from the form
  144. UpdateData( TRUE );
  145. pKey->SetName( m_szName );
  146. }
  147. //----------------------------------------------------------------------------
  148. void CKeyDataView::EnableDataView( BOOL fEnable, BOOL fEnableName )
  149. {
  150. // enable the name seperately from the rest
  151. if ( fEnableName )
  152. {
  153. m_ctrlName.EnableWindow( TRUE );
  154. m_ctrlStaticName.EnableWindow( TRUE );
  155. }
  156. else
  157. {
  158. m_szName.Empty();
  159. m_ctrlName.EnableWindow( FALSE );
  160. m_ctrlStaticName.EnableWindow( FALSE );
  161. }
  162. // do the right thing. (wasn't a bad movie)
  163. if ( fEnable )
  164. {
  165. // enable what needs to be enabled. The DoUpdate routine takes care of the rest
  166. m_ctrlGroupDN.EnableWindow( TRUE );
  167. m_ctrlUnit.EnableWindow( TRUE );
  168. m_ctrlUnit.EnableWindow( TRUE );
  169. m_ctrlNetAddr.EnableWindow( TRUE );
  170. m_ctrlCountry.EnableWindow( TRUE );
  171. m_ctrlState.EnableWindow( TRUE );
  172. m_ctrlLocality.EnableWindow( TRUE );
  173. m_ctrlBits.EnableWindow( TRUE );
  174. m_ctrlOrg.EnableWindow( TRUE );
  175. m_ctrlExpires.EnableWindow( TRUE );
  176. m_ctrlStarts.EnableWindow( TRUE );
  177. }
  178. else
  179. // disabling the window
  180. {
  181. // empty the information strings
  182. m_szCountry.Empty();
  183. m_szState.Empty();
  184. m_szLocality.Empty();
  185. m_szDNNetAddress.Empty();
  186. m_szOrganization.Empty();
  187. m_szUnit.Empty();
  188. m_szBits.Empty();
  189. m_szExpires.Empty();
  190. m_szStarts.Empty();
  191. // set the status string
  192. m_szStatus.LoadString(IDS_MACHINE_SELECTED);
  193. // set the data into the form
  194. UpdateData( FALSE );
  195. // disable everything
  196. m_ctrlGroupDN.EnableWindow( FALSE );
  197. m_ctrlUnit.EnableWindow( FALSE );
  198. m_ctrlOrg.EnableWindow( FALSE );
  199. m_ctrlNetAddr.EnableWindow( FALSE );
  200. m_ctrlCountry.EnableWindow( FALSE );
  201. m_ctrlState.EnableWindow( FALSE );
  202. m_ctrlLocality.EnableWindow( FALSE );
  203. m_ctrlBits.EnableWindow( FALSE );
  204. m_ctrlExpires.EnableWindow( FALSE );
  205. m_ctrlStarts.EnableWindow( FALSE );
  206. }
  207. }
  208. //----------------------------------------------------------------------------
  209. void CKeyDataView::FillInCrackedInfo( CKey* pKey )
  210. {
  211. CKeyCrackedData cracker;
  212. // crack the key
  213. if ( !cracker.CrackKey(pKey) )
  214. return;
  215. // fill in the distinguishing information
  216. cracker.GetDNCountry( m_szCountry );
  217. cracker.GetDNState( m_szState );
  218. cracker.GetDNLocality( m_szLocality );
  219. cracker.GetDNNetAddress( m_szDNNetAddress );
  220. cracker.GetDNOrganization( m_szOrganization );
  221. cracker.GetDNUnit( m_szUnit );
  222. // set the bit length
  223. DWORD nBits = cracker.GetBitLength();
  224. if ( nBits )
  225. m_szBits.Format( "%d", nBits );
  226. else
  227. m_szBits.LoadString( IDS_KEY_UNKNOWN );
  228. // get the dates
  229. FILETIME timeStart = cracker.GetValidFrom();
  230. FILETIME timeEnd = cracker.GetValidUntil();
  231. // set the start date string
  232. CTime ctimeStart( timeStart );
  233. m_szStarts = ctimeStart.Format( IDS_EXPIRETIME_FORMAT );
  234. // set the end date string
  235. CTime ctimeEnd( timeEnd );
  236. m_szExpires = ctimeEnd.Format( IDS_EXPIRETIME_FORMAT );
  237. // get the current time
  238. CTime ctimeCurrent = CTime::GetCurrentTime();
  239. #ifdef _DEBUG
  240. CString szTest = ctimeCurrent.Format( IDS_EXPIRETIME_FORMAT );
  241. #endif
  242. // get the expire soon test time - current time plus two weeks
  243. CTimeSpan ctsSoonSpan(14,0,0,0); // two weeks
  244. CTime ctimeSoon = ctimeCurrent + ctsSoonSpan;
  245. #ifdef _DEBUG
  246. szTest = ctimeSoon.Format( IDS_EXPIRETIME_FORMAT );
  247. #endif
  248. // test if it has expired first
  249. if ( ctimeCurrent > ctimeEnd )
  250. {
  251. m_szStatus.LoadString(IDS_KEY_STATUS_EXPIRED);
  252. }
  253. else if ( ctimeSoon > ctimeEnd )
  254. {
  255. // well then does it expire soon?
  256. m_szStatus.LoadString(IDS_KEY_STATUS_EXPIRES_SOON);
  257. }
  258. else
  259. {
  260. // that must mean it is ok
  261. m_szStatus.LoadString(IDS_KEY_STATUS_COMPLETE);
  262. }
  263. }
  264. //----------------------------------------------------------------------------
  265. BOOL CKeyDataView::PreTranslateMessage(MSG* pMsg)
  266. {
  267. // The user pushes the tab button, send the focus back to the tree view
  268. if ( pMsg->message == WM_KEYDOWN )
  269. {
  270. int nVirtKey = (int) pMsg->wParam;
  271. if ( nVirtKey == VK_TAB )
  272. {
  273. // get the parental frame window
  274. CMainFrame* pFrame = (CMainFrame*)GetParentFrame();
  275. // give the data view the focus
  276. pFrame->SetActiveView( g_pTreeView );
  277. return TRUE;
  278. }
  279. }
  280. return CFormView::PreTranslateMessage(pMsg);
  281. }