Leaked source code of windows server 2003
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.

382 lines
10 KiB

  1. #include "stdafx.h"
  2. #include "WizardSheet.h"
  3. #include "UIUtils.h"
  4. static _ATL_FUNC_INFO StateChangeInfo = { CC_STDCALL,
  5. VT_BOOL,
  6. 4,
  7. { VT_I4, VT_VARIANT, VT_VARIANT, VT_VARIANT }
  8. };
  9. CImportProgress::CImportProgress( CWizardSheet* pTheSheet ) :
  10. m_pTheSheet( pTheSheet )
  11. {
  12. m_strTitle.LoadString( IDS_TITLE_IMPORT_PROGRESS );
  13. m_strSubTitle.LoadString( IDS_SUBTITLE_IMPORT_PROGRESS );
  14. SetHeaderTitle( m_strTitle );
  15. SetHeaderSubTitle( m_strSubTitle );
  16. }
  17. BOOL CImportProgress::OnSetActive()
  18. {
  19. SetWizardButtons( 0 );
  20. ListBox_ResetContent( GetDlgItem( IDC_OPLIST ) );
  21. AddStatusText( IDC_OPS_INITENGINE );
  22. m_ProgressBar = GetDlgItem( IDC_PROGRESS );
  23. m_ProgressBar.SetPos( 0 );
  24. VERIFY( SetDlgItemText( IDC_STATUS, L"" ) );
  25. m_strImportError.Empty();
  26. m_nImportCanceled = 0;
  27. UINT nThreadID = 0;
  28. // Start the thread where the actuall export process will take place
  29. m_shThread = reinterpret_cast<HANDLE>( ::_beginthreadex( NULL,
  30. 0,
  31. CImportProgress::ThreadProc,
  32. this,
  33. 0,
  34. &nThreadID ) );
  35. return TRUE;
  36. }
  37. BOOL CImportProgress::OnQueryCancel( void )
  38. {
  39. // If Export is not in progress - allow exit
  40. if ( !m_shThread.IsValid() ) return TRUE;
  41. // Preven reentrancy ( Cancel the export when it's already cancedl )
  42. // while we wait for next event from the COM object
  43. if ( m_nImportCanceled != 0 ) return FALSE;
  44. if ( UIUtils::MessageBox( m_hWnd, IDS_MSG_CANCELIMPORT, IDS_APPTITLE, MB_YESNO | MB_ICONQUESTION ) != IDYES )
  45. {
  46. return FALSE;
  47. }
  48. // m_nImportCanceled is used by the event handler which is another thread
  49. ::InterlockedIncrement( &m_nImportCanceled );
  50. // Set the status text
  51. CString str;
  52. VERIFY( str.LoadString( IDS_PRG_IMPORTCANCELED ) );
  53. SetDlgItemText( IDC_STATUS, str );
  54. HANDLE hThread = m_shThread.get();
  55. do
  56. {
  57. DWORD dwWaitRes = ::MsgWaitForMultipleObjects( 1, &hThread, FALSE, INFINITE, QS_ALLEVENTS );
  58. if ( dwWaitRes == ( WAIT_OBJECT_0 + 1 ) )
  59. {
  60. // MSG
  61. MSG msg;
  62. ::GetMessage( &msg, NULL, 0, 0 );
  63. ::TranslateMessage( &msg );
  64. ::DispatchMessage( &msg );
  65. }
  66. else
  67. {
  68. break;
  69. }
  70. }while( true );
  71. return TRUE;
  72. }
  73. unsigned __stdcall CImportProgress::ThreadProc( void* pCtx )
  74. {
  75. CImportProgress* pThis = reinterpret_cast<CImportProgress*>( pCtx );
  76. pThis->SetCompleteStat();
  77. pThis->AddStatusText( IDS_OPS_CONFIGENGINE );
  78. HRESULT hr = ::CoInitialize( NULL );
  79. IImportPackagePtr spImport;
  80. LONG nOpt = 0;
  81. bool bAdvised = false; // Is connected to the event source
  82. pThis->GetOptions( /*r*/nOpt );
  83. if ( SUCCEEDED( hr ) )
  84. {
  85. hr = spImport.CreateInstance( CLSID_ImportPackage );
  86. if ( FAILED( hr ) )
  87. {
  88. VERIFY( pThis->m_strImportError.LoadString( IDS_E_NOENGINE ) );
  89. }
  90. }
  91. // Advise to the state events
  92. if ( SUCCEEDED( hr ) )
  93. {
  94. hr = pThis->DispEventAdvise( spImport.GetInterfacePtr() );
  95. bAdvised = SUCCEEDED( hr );
  96. }
  97. if ( SUCCEEDED( hr ) )
  98. {
  99. CComBSTR bstrPkg( pThis->m_pTheSheet->m_pageLoadPkg.m_strFilename );
  100. CComBSTR bstrPwd( pThis->m_pTheSheet->m_pageLoadPkg.m_strPassword );
  101. if ( ( NULL != bstrPkg.m_str ) && ( NULL != bstrPwd.m_str ) )
  102. {
  103. hr = spImport->LoadPackage( bstrPkg, bstrPwd );
  104. }
  105. }
  106. // Import the site
  107. if ( SUCCEEDED( hr ) )
  108. {
  109. CComBSTR bstrCustomPath;
  110. if ( pThis->m_pTheSheet->m_pageImpOpt.m_bUseCustomPath )
  111. {
  112. bstrCustomPath = pThis->m_pTheSheet->m_pageImpOpt.m_strCustomPath;
  113. if ( NULL == bstrCustomPath.m_str )
  114. {
  115. hr = E_OUTOFMEMORY;
  116. }
  117. }
  118. if ( SUCCEEDED( hr ) )
  119. {
  120. hr = spImport->ImportSite( 0, bstrCustomPath, nOpt );
  121. }
  122. }
  123. // Get the error
  124. if ( pThis->m_strImportError.IsEmpty() && FAILED( hr ) )
  125. {
  126. CComBSTR bstrText( L"Unknown Error" );;
  127. IErrorInfoPtr spInfo;
  128. VERIFY( SUCCEEDED( ::GetErrorInfo( 0, &spInfo ) ) );
  129. if ( spInfo != NULL )
  130. {
  131. VERIFY( SUCCEEDED( spInfo->GetDescription( &bstrText ) ) );
  132. }
  133. pThis->m_strImportError = bstrText;
  134. }
  135. // Disconnect from the event source
  136. if ( bAdvised )
  137. {
  138. VERIFY( SUCCEEDED( pThis->DispEventUnadvise( spImport.GetInterfacePtr() ) ) );
  139. }
  140. spImport = NULL;
  141. ::CoUninitialize();
  142. // Notify the dialog that the export is complete
  143. VERIFY( ::PostMessage( pThis->m_hWnd, MSG_COMPLETE, hr, 0 ) );
  144. return 0;
  145. }
  146. void CImportProgress::AddStatusText( UINT nID, LPCWSTR wszText /*= NULL*/, DWORD dw1 /*= 0*/, DWORD dw2 /*= 0*/ )
  147. {
  148. CString str;
  149. str.Format( nID, wszText, dw1, dw2 );
  150. ListBox_InsertString( GetDlgItem( IDC_OPLIST ), -1, str );
  151. }
  152. void CImportProgress::SetCompleteStat()
  153. {
  154. CListBox LB( GetDlgItem( IDC_OPLIST ) );
  155. int iLast = LB.GetCount() - 1;
  156. _ASSERT( iLast >= 0 );
  157. CString strCurrent;
  158. LB.GetText( iLast, strCurrent );
  159. strCurrent += L"OK";
  160. LB.InsertString( iLast, strCurrent );
  161. LB.DeleteString( iLast + 1 );
  162. }
  163. void CImportProgress::GetOptions( LONG& rnImpportOpt )
  164. {
  165. rnImpportOpt = impDefault;
  166. if ( !m_pTheSheet->m_pageImpOpt.m_bApplyACLs )
  167. {
  168. rnImpportOpt |= impSkipFileACLs;
  169. }
  170. if ( !m_pTheSheet->m_pageImpOpt.m_bImportCert )
  171. {
  172. rnImpportOpt |= impSkipCertificate;
  173. }
  174. if ( !m_pTheSheet->m_pageImpOpt.m_bImportContent )
  175. {
  176. rnImpportOpt |= impSkipContent;
  177. }
  178. if ( m_pTheSheet->m_pageImpOpt.m_bImportInherited )
  179. {
  180. rnImpportOpt |= impImortInherited;
  181. }
  182. if ( !m_pTheSheet->m_pageImpOpt.m_bPerformPostProcess )
  183. {
  184. rnImpportOpt |= impSkipPostProcess;
  185. }
  186. if ( m_pTheSheet->m_pageImpOpt.m_bPurgeOldData )
  187. {
  188. rnImpportOpt |= impPurgeOldData;
  189. }
  190. if ( m_pTheSheet->m_pageImpOpt.m_bReuseCerts )
  191. {
  192. rnImpportOpt |= impUseExistingCerts;
  193. }
  194. }
  195. /*
  196. This is the event handler that will be fired for status notifications by the COM Object
  197. Note that this will execute in different thread then the Wizard code
  198. */
  199. VARIANT_BOOL __stdcall CImportProgress::OnStateChange( IN enExportState State,
  200. IN VARIANT vntArg1,
  201. IN VARIANT vntArg2,
  202. IN VARIANT vntArg3 )
  203. {
  204. static enExportState CurrentState = estInitializing;
  205. WCHAR wszPath[ MAX_PATH ];
  206. CString strStatus;
  207. // If the user canceled the import - notify the COM object that we want to terminate the export
  208. if ( m_nImportCanceled != 0 )
  209. {
  210. return VARIANT_FALSE;
  211. }
  212. // We can receive a particular state more then once
  213. // But when we moove to the next state we need to update the status list box
  214. switch( State )
  215. {
  216. case istProgressInfo:
  217. // Set the progress range
  218. m_ProgressBar.SetRange( 0, V_I4( &vntArg1 ) );
  219. m_ProgressBar.SetStep( 1 );
  220. m_ProgressBar.SetPos( 0 );
  221. break;
  222. case istImportingVDir:
  223. SetCompleteStat();
  224. strStatus.Format( IDS_PRG_IMPORTINGVDIR, V_BSTR( &vntArg1 ), V_BSTR( &vntArg2 ));
  225. ListBox_InsertString( GetDlgItem( IDC_OPLIST ), -1, strStatus );
  226. m_ProgressBar.StepIt();
  227. break;
  228. case istImportingFile:
  229. VERIFY( ::PathCompactPathExW( wszPath, V_BSTR( &vntArg1 ), 70, 0 ) );
  230. strStatus.Format( IDS_PRG_EXTRACTING_FILE, wszPath );
  231. VERIFY( SetDlgItemText( IDC_STATUS, strStatus ) );
  232. m_ProgressBar.StepIt();
  233. break;
  234. case istImportingCertificate:
  235. SetCompleteStat();
  236. AddStatusText( IDS_PRG_IMPORT_CERT );
  237. break;
  238. case istImportingConfig:
  239. SetCompleteStat();
  240. AddStatusText( IDS_PRG_IMPORT_CONFIG );
  241. break;
  242. case istPostProcess:
  243. SetCompleteStat();
  244. AddStatusText( IDS_PRG_IMPORT_POSTPROCESS );
  245. if ( V_BOOL( &vntArg1 ) != VARIANT_FALSE )
  246. {
  247. strStatus.Format( IDS_PRG_EXEC_PP_FILE, V_BSTR( &vntArg2 ) );
  248. }
  249. else
  250. {
  251. strStatus.Format( IDS_PRG_EXEC_PP_CMD, V_BSTR( &vntArg2 ) );
  252. }
  253. VERIFY( SetDlgItemText( IDC_STATUS, strStatus ) );
  254. break;
  255. case istFinalizing:
  256. SetCompleteStat();
  257. AddStatusText( IDS_PRG_FINALIZING );
  258. break;
  259. };
  260. return VARIANT_TRUE;
  261. }
  262. LRESULT CImportProgress::OnImportComplete( UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/ )
  263. {
  264. m_shThread.Close();
  265. if ( FAILED( wParam ) )
  266. {
  267. CString strError;
  268. CString strTitle;
  269. strError.Format( IDS_E_IMPORT, static_cast<LPCWSTR>( m_strImportError ) );
  270. strTitle.LoadString( IDS_APPTITLE );
  271. ::MessageBox( m_hWnd, strError, strTitle, MB_OK | MB_ICONSTOP );
  272. // Go to the summary page
  273. m_pTheSheet->SetActivePageByID( IDD_WPIMP_OPTIONS );
  274. }
  275. else
  276. {
  277. CString strTip;
  278. VERIFY( strTip.LoadString( IDS_TIP_PRESSNEXT ) );
  279. VERIFY( SetDlgItemText( IDC_TIP, strTip ) );
  280. SetWindowFont( GetDlgItem( IDC_TIP ), m_pTheSheet->m_fontBold.get(), TRUE );
  281. SetCompleteStat();
  282. VERIFY( SetDlgItemText( IDC_STATUS, L"" ) );
  283. SetWizardButtons( PSWIZB_NEXT );
  284. }
  285. return 0;
  286. }