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.

573 lines
16 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 1999
  5. *
  6. * File: props.cpp
  7. *
  8. * Contents: Implementation file for console property sheet and page(s)
  9. *
  10. * History: 05-Dec-97 JeffRo Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #include "stdafx.h"
  14. #include "amc.h"
  15. #include "props.h"
  16. #include "mainfrm.h"
  17. #include "amcdoc.h"
  18. #include "pickicon.h"
  19. //#ifdef _DEBUG
  20. //#define new DEBUG_NEW
  21. //#undef THIS_FILE
  22. //static char THIS_FILE[] = __FILE__;
  23. //#endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CConsolePropSheet
  26. IMPLEMENT_DYNAMIC(CConsolePropSheet, CPropertySheet)
  27. CConsolePropSheet::CConsolePropSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
  28. : CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
  29. {
  30. CommonConstruct ();
  31. }
  32. CConsolePropSheet::CConsolePropSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
  33. : CPropertySheet(pszCaption, pParentWnd, iSelectPage)
  34. {
  35. CommonConstruct ();
  36. }
  37. void CConsolePropSheet::CommonConstruct()
  38. {
  39. DECLARE_SC(sc, TEXT("CConsolePropSheet::CommonConstruct"));
  40. CAMCApp *pAMCApp = AMCGetApp();
  41. sc = ScCheckPointers( pAMCApp, E_UNEXPECTED );
  42. if (sc)
  43. sc.TraceAndClear();
  44. // add the main page only for author mode
  45. if ( (pAMCApp != NULL) && (pAMCApp->GetMode() == eMode_Author) )
  46. {
  47. AddPage (&m_ConsolePage);
  48. }
  49. AddPage (&m_diskCleanupPage);
  50. }
  51. BOOL CConsolePropSheet::OnInitDialog()
  52. {
  53. ModifyStyleEx(0, WS_EX_CONTEXTHELP, SWP_NOSIZE);
  54. return CPropertySheet::OnInitDialog();
  55. }
  56. CConsolePropSheet::~CConsolePropSheet()
  57. {
  58. }
  59. BEGIN_MESSAGE_MAP(CConsolePropSheet, CPropertySheet)
  60. //{{AFX_MSG_MAP(CConsolePropSheet)
  61. //}}AFX_MSG_MAP
  62. END_MESSAGE_MAP()
  63. /*+-------------------------------------------------------------------------*
  64. * CConsolePropSheet::DoModal
  65. *
  66. *
  67. *--------------------------------------------------------------------------*/
  68. INT_PTR CConsolePropSheet::DoModal()
  69. {
  70. CThemeContextActivator activator;
  71. return CPropertySheet::DoModal();
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CConsolePropPage property page
  75. IMPLEMENT_DYNCREATE(CConsolePropPage, CPropertyPage)
  76. CConsolePropPage::CConsolePropPage()
  77. : CPropertyPage(CConsolePropPage::IDD),
  78. m_pDoc (CAMCDoc::GetDocument())
  79. {
  80. ASSERT (m_pDoc != NULL);
  81. ASSERT_VALID (m_pDoc);
  82. ASSERT_KINDOF (CAMCDoc, m_pDoc);
  83. m_hinstSelf = AfxGetInstanceHandle ();
  84. m_fTitleChanged = false;
  85. m_fIconChanged = false;
  86. m_strTitle = m_pDoc->GetCustomTitle ();
  87. m_nConsoleMode = m_pDoc->GetMode ();
  88. m_fDontSaveChanges = m_pDoc->IsLogicalReadOnly ();
  89. m_fAllowViewCustomization = m_pDoc->AllowViewCustomization ();
  90. }
  91. CConsolePropPage::~CConsolePropPage()
  92. {
  93. }
  94. void CConsolePropPage::DoDataExchange(CDataExchange* pDX)
  95. {
  96. CPropertyPage::DoDataExchange(pDX);
  97. //{{AFX_DATA_MAP(CConsolePropPage)
  98. DDX_Control(pDX, IDC_DONTSAVECHANGES, m_wndDontSaveChanges);
  99. DDX_Control(pDX, IDC_AllowViewCustomization, m_wndAllowViewCustomization);
  100. DDX_Control(pDX, IDC_CONSOLE_MODE_DESCRIPTION, m_wndModeDescription);
  101. DDX_Control(pDX, IDC_CUSTOM_TITLE, m_wndTitle);
  102. DDX_CBIndex(pDX, IDC_CONSOLE_MODE, m_nConsoleMode);
  103. DDX_Check(pDX, IDC_DONTSAVECHANGES, m_fDontSaveChanges);
  104. DDX_Text(pDX, IDC_CUSTOM_TITLE, m_strTitle);
  105. DDX_Check(pDX, IDC_AllowViewCustomization, m_fAllowViewCustomization);
  106. //}}AFX_DATA_MAP
  107. }
  108. BEGIN_MESSAGE_MAP(CConsolePropPage, CPropertyPage)
  109. //{{AFX_MSG_MAP(CConsolePropPage)
  110. ON_CBN_SELENDOK(IDC_CONSOLE_MODE, OnSelendokConsoleMode)
  111. ON_BN_CLICKED(IDC_DONTSAVECHANGES, OnDontSaveChanges)
  112. ON_BN_CLICKED(IDC_AllowViewCustomization, OnAllowViewCustomization)
  113. ON_BN_CLICKED(IDC_CHANGE_ICON, OnChangeIcon)
  114. ON_EN_CHANGE(IDC_CUSTOM_TITLE, OnChangeCustomTitle)
  115. //}}AFX_MSG_MAP
  116. ON_MMC_CONTEXT_HELP()
  117. END_MESSAGE_MAP()
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CConsolePropPage message handlers
  120. void CConsolePropPage::OnOK()
  121. {
  122. m_pDoc->SetMode (static_cast<ProgramMode>(m_nConsoleMode));
  123. m_pDoc->SetLogicalReadOnlyFlag (m_fDontSaveChanges);
  124. m_pDoc->AllowViewCustomization (m_fAllowViewCustomization);
  125. if (m_fIconChanged)
  126. {
  127. m_pDoc->SetCustomIcon (m_strIconFile, m_nIconIndex);
  128. m_fIconChanged = false;
  129. }
  130. if (m_fTitleChanged)
  131. {
  132. m_pDoc->SetCustomTitle (m_strTitle);
  133. m_fTitleChanged = false;
  134. }
  135. CPropertyPage::OnOK();
  136. }
  137. void CConsolePropPage::OnSelendokConsoleMode()
  138. {
  139. SetModified ();
  140. UpdateData ();
  141. SetDescriptionText ();
  142. EnableDontSaveChanges ();
  143. }
  144. void CConsolePropPage::SetDescriptionText ()
  145. {
  146. // make sure the mode index is within range
  147. ASSERT (IsValidProgramMode (static_cast<ProgramMode>(m_nConsoleMode)));
  148. m_wndModeDescription.SetWindowText (m_strDescription[m_nConsoleMode]);
  149. }
  150. BOOL CConsolePropPage::OnInitDialog()
  151. {
  152. CPropertyPage::OnInitDialog();
  153. /*
  154. * make sure the string IDs are as the code expects them
  155. */
  156. ASSERT ((IDS_ModeAuthor + 1) == IDS_ModeUserFull);
  157. ASSERT ((IDS_ModeUserFull + 1) == IDS_ModeUserMDI);
  158. ASSERT ((IDS_ModeUserMDI + 1) == IDS_ModeUserSDI);
  159. ASSERT ((IDS_ModeAuthor_Description + 1) == IDS_ModeUserFull_Description);
  160. ASSERT ((IDS_ModeUserFull_Description + 1) == IDS_ModeUserMDI_Description);
  161. ASSERT ((IDS_ModeUserMDI_Description + 1) == IDS_ModeUserSDI_Description);
  162. /*
  163. * load the mode names into the combo box
  164. */
  165. int i;
  166. CString strComboText;
  167. CComboBox* pCombo = reinterpret_cast<CComboBox*>(GetDlgItem (IDC_CONSOLE_MODE));
  168. ASSERT (pCombo != NULL);
  169. for (i = 0; i < eMode_Count; i++)
  170. {
  171. VERIFY (LoadString (strComboText, IDS_ModeAuthor + i));
  172. pCombo->AddString (strComboText);
  173. }
  174. pCombo->SetCurSel (m_nConsoleMode - eMode_Author);
  175. /*
  176. * load up the description text
  177. */
  178. ASSERT (countof (m_strDescription) == eMode_Count);
  179. for (i = 0; i < countof (m_strDescription); i++)
  180. {
  181. VERIFY (LoadString (m_strDescription[i], IDS_ModeAuthor_Description + i));
  182. }
  183. SetDescriptionText ();
  184. EnableDontSaveChanges ();
  185. /*
  186. * Get the current icon for this console file
  187. */
  188. ASSERT (m_pDoc != NULL);
  189. HICON hIcon = m_pDoc->GetCustomIcon (true, &m_strIconFile, &m_nIconIndex);
  190. m_wndIcon.SubclassDlgItem (IDC_CONSOLE_ICON, this);
  191. /*
  192. * if we haven't specified a custom icon yet, use MMC.EXE
  193. */
  194. if (hIcon == NULL)
  195. {
  196. ASSERT (m_strIconFile.IsEmpty());
  197. const int cchBuffer = MAX_PATH;
  198. GetModuleFileName (AfxGetInstanceHandle(), m_strIconFile.GetBuffer(cchBuffer), cchBuffer);
  199. m_strIconFile.ReleaseBuffer();
  200. m_nIconIndex = 0;
  201. }
  202. else
  203. m_wndIcon.SetIcon (hIcon);
  204. /*
  205. * Get the current title for this console file
  206. */
  207. m_wndTitle.SetWindowText (m_pDoc->GetCustomTitle());
  208. m_fTitleChanged = false;
  209. return TRUE; // return TRUE unless you set the focus to a control
  210. // EXCEPTION: OCX Property Pages should return FALSE
  211. }
  212. void CConsolePropPage::OnDontSaveChanges()
  213. {
  214. SetModified ();
  215. }
  216. void CConsolePropPage::OnAllowViewCustomization()
  217. {
  218. SetModified ();
  219. }
  220. void CConsolePropPage::EnableDontSaveChanges()
  221. {
  222. if (m_nConsoleMode == eMode_Author)
  223. {
  224. m_wndDontSaveChanges. EnableWindow (false);
  225. m_wndDontSaveChanges. SetCheck (0);
  226. m_wndAllowViewCustomization.EnableWindow (false);
  227. m_wndAllowViewCustomization.SetCheck (1);
  228. }
  229. else
  230. {
  231. m_wndDontSaveChanges. EnableWindow (true);
  232. m_wndAllowViewCustomization.EnableWindow (true);
  233. }
  234. }
  235. void CConsolePropPage::OnChangeIcon()
  236. {
  237. DECLARE_SC(sc, TEXT("CConsolePropPage::OnChangeIcon"));
  238. int nIconIndex = m_nIconIndex;
  239. TCHAR szIconFile[MAX_PATH];
  240. sc = StringCchCopy(szIconFile, countof(szIconFile), m_strIconFile);
  241. if (sc)
  242. return;
  243. /*
  244. * show the pick 'em dialog; if something changed, enable OK/Apply
  245. */
  246. if (MMC_PickIconDlg (m_hWnd, szIconFile, countof (szIconFile), &nIconIndex) &&
  247. ((nIconIndex != m_nIconIndex) || (lstrcmpi (szIconFile, m_strIconFile) != 0)))
  248. {
  249. m_icon.Attach (ExtractIcon (m_hinstSelf, szIconFile, nIconIndex));
  250. if (m_icon != NULL)
  251. {
  252. m_fIconChanged = true;
  253. m_strIconFile = szIconFile;
  254. m_nIconIndex = nIconIndex;
  255. m_wndIcon.SetIcon (m_icon);
  256. SetModified();
  257. }
  258. }
  259. }
  260. void CConsolePropPage::OnChangeCustomTitle()
  261. {
  262. m_fTitleChanged = true;
  263. SetModified();
  264. }
  265. /////////////////////////////////////////////////////////////////////////////
  266. // CDiskCleanupPage property page
  267. IMPLEMENT_DYNCREATE(CDiskCleanupPage, CPropertyPage)
  268. BEGIN_MESSAGE_MAP(CDiskCleanupPage, CPropertyPage)
  269. ON_MMC_CONTEXT_HELP()
  270. ON_BN_CLICKED(IDC_DELETE_TEMP_FILES, OnDeleteTemporaryFiles)
  271. END_MESSAGE_MAP()
  272. CDiskCleanupPage::CDiskCleanupPage() : CPropertyPage(CDiskCleanupPage::IDD)
  273. {
  274. }
  275. CDiskCleanupPage::~CDiskCleanupPage()
  276. {
  277. }
  278. BOOL CDiskCleanupPage::OnInitDialog()
  279. {
  280. CPropertyPage::OnInitDialog();
  281. ScRecalculateUsedSpace();
  282. return TRUE;
  283. }
  284. /***************************************************************************\
  285. *
  286. * METHOD: CDiskCleanupPage::OnDeleteTemporaryFiles
  287. *
  288. * PURPOSE: Invoked when "Delete Filed" button is pressed
  289. * Removes all files from MMC folder storing the user data
  290. *
  291. * PARAMETERS:
  292. *
  293. * RETURNS:
  294. *
  295. \***************************************************************************/
  296. void CDiskCleanupPage::OnDeleteTemporaryFiles()
  297. {
  298. DECLARE_SC(sc, TEXT("CDiskCleanupPage::OnDeleteTemporaryFiles"));
  299. // ask user if he is sure...
  300. CString strConfirmMessage;
  301. CString strConfirmCaption;
  302. if (!LoadString(strConfirmMessage, IDS_ConfirmDeleteTempFiles) ||
  303. !LoadString(strConfirmCaption, IDR_MAINFRAME))
  304. {
  305. sc = E_UNEXPECTED;
  306. return;
  307. }
  308. int ans = ::MessageBox( m_hWnd, strConfirmMessage, strConfirmCaption, MB_YESNO | MB_ICONWARNING);
  309. if ( ans != IDYES )
  310. return;
  311. // display wait cursor while working
  312. CWaitCursor cursorWait;
  313. // get folder
  314. tstring strFileFolder;
  315. sc = CConsoleFilePersistor::ScGetUserDataFolder(strFileFolder);
  316. if (sc)
  317. return;
  318. // get file mask
  319. tstring strFileMask = strFileFolder;
  320. strFileMask += _T("\\*.*");
  321. WIN32_FIND_DATA findFileData;
  322. ZeroMemory( &findFileData, sizeof(findFileData) );
  323. // start file search
  324. HANDLE hFindFile = FindFirstFile( strFileMask.c_str(), &findFileData );
  325. if ( hFindFile == INVALID_HANDLE_VALUE )
  326. {
  327. sc.FromLastError();
  328. return;
  329. }
  330. // loop thru files and delete them
  331. bool bContinue = true;
  332. while ( bContinue )
  333. {
  334. tstring strFileToDelete = strFileFolder + _T('\\') + findFileData.cFileName;
  335. DWORD dwFileAttributes = findFileData.dwFileAttributes;
  336. // get to the next file first
  337. bContinue = FindNextFile( hFindFile, &findFileData );
  338. // delete files, but not directories
  339. if ( 0 == (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
  340. {
  341. // delete
  342. if ( !DeleteFile( strFileToDelete.c_str() ) )
  343. {
  344. // trace on errors (but do not stop)
  345. sc.FromLastError().TraceAndClear();
  346. }
  347. }
  348. }
  349. // Done, release the handle
  350. FindClose(hFindFile);
  351. sc = ScRecalculateUsedSpace();
  352. if (sc)
  353. sc.TraceAndClear();
  354. }
  355. /***************************************************************************\
  356. *
  357. * METHOD: CDiskCleanupPage::ScRecalculateUsedSpace
  358. *
  359. * PURPOSE: Recalculates and displays disk space occupied by user data in this profile
  360. *
  361. * PARAMETERS:
  362. *
  363. * RETURNS:
  364. * SC - result code
  365. *
  366. \***************************************************************************/
  367. SC CDiskCleanupPage::ScRecalculateUsedSpace()
  368. {
  369. DECLARE_SC(sc, TEXT("CDiskCleanupPage::ScRecalculateUsedSpace"));
  370. // display wait cursor while working
  371. CWaitCursor cursorWait;
  372. // get folder
  373. tstring strFileFolder;
  374. sc = CConsoleFilePersistor::ScGetUserDataFolder(strFileFolder);
  375. if (sc)
  376. return sc;
  377. // get file mask
  378. tstring strFileMask = strFileFolder;
  379. strFileMask += _T("\\*.*");
  380. WIN32_FIND_DATA findFileData;
  381. ZeroMemory( &findFileData, sizeof(findFileData) );
  382. // start file search
  383. HANDLE hFindFile = FindFirstFile( strFileMask.c_str(), &findFileData );
  384. if ( hFindFile == INVALID_HANDLE_VALUE )
  385. return sc.FromLastError();
  386. // loop thru files and count size
  387. ULARGE_INTEGER ulgOccupied = {0};
  388. bool bContinue = true;
  389. while ( bContinue )
  390. {
  391. DWORD dwFileAttributes = findFileData.dwFileAttributes;
  392. if ( 0 == (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
  393. {
  394. // add the high and low part separatelly, since findFileData does not have it bundled
  395. ulgOccupied.HighPart += findFileData.nFileSizeHigh;
  396. // add lower part to the whole large integer not to lose carry on overflow
  397. ulgOccupied.QuadPart += findFileData.nFileSizeLow;
  398. }
  399. // get to the next file first
  400. bContinue = FindNextFile( hFindFile, &findFileData );
  401. }
  402. // Done, release the handle
  403. FindClose(hFindFile);
  404. // now conver the size into string
  405. UINT nTerraBytes = (UINT)(ulgOccupied.QuadPart >> 40);
  406. UINT nGigaBytes = (UINT)(ulgOccupied.QuadPart >> 30) & 0x3ff;
  407. UINT nMegaBytes = ((ulgOccupied.LowPart >> 20) & 0x3ff);
  408. UINT nKiloBytes = ((ulgOccupied.LowPart >> 10) & 0x3ff);
  409. UINT nBytes = ( ulgOccupied.LowPart & 0x3ff);
  410. CString strUnit;
  411. double dSize = 0.0;
  412. bool bNonZeroOccupied = (ulgOccupied.QuadPart != 0);
  413. // display only biggest units, and never more than 999
  414. // instead of "1001 KB" we display "0.98 MB"
  415. if ( (nTerraBytes) > 0 || (nGigaBytes > 999) )
  416. {
  417. LoadString(strUnit, IDS_FileSize_TB);
  418. dSize = (double)nTerraBytes + ((double)nGigaBytes / 1024.);
  419. }
  420. else if ( (nGigaBytes) > 0 || (nMegaBytes > 999) )
  421. {
  422. LoadString(strUnit, IDS_FileSize_GB);
  423. dSize = (double)nGigaBytes + ((double)nMegaBytes / 1024.);
  424. }
  425. else if ( (nMegaBytes) > 0 || (nKiloBytes > 999) )
  426. {
  427. LoadString(strUnit, IDS_FileSize_MB);
  428. dSize = (double)nMegaBytes + ((double)nKiloBytes / 1024.);
  429. }
  430. else if ( (nKiloBytes) > 0 || (nBytes > 999) )
  431. {
  432. LoadString(strUnit, IDS_FileSize_KB);
  433. dSize = (double)nKiloBytes + ((double)nBytes / 1024.);
  434. }
  435. else
  436. {
  437. LoadString(strUnit, IDS_FileSize_bytes);
  438. dSize = (double)nBytes;
  439. }
  440. // format with op to two decimal points
  441. CString strSize;
  442. strSize.Format(_T("%.2f"), dSize);
  443. //truncate trailing zeros
  444. while (strSize.Right(1) == _T("0"))
  445. strSize = strSize.Left(strSize.GetLength() - 1);
  446. //truncate trailing decimal point
  447. if (strSize.Right(1) == _T("."))
  448. strSize = strSize.Left(strSize.GetLength() - 1);
  449. // add units ( in locale independant way )
  450. strUnit.Replace(_T("%1"), strSize);
  451. // set to the window
  452. SetDlgItemText( IDC_DISKCLEANUP_OCCUPIED, strUnit );
  453. // enable/disable "Delete Files" button
  454. CWnd *pWndDeleteBtn = GetDlgItem(IDC_DELETE_TEMP_FILES);
  455. sc = ScCheckPointers( pWndDeleteBtn, E_UNEXPECTED );
  456. if (sc)
  457. return sc;
  458. pWndDeleteBtn->EnableWindow( bNonZeroOccupied );
  459. // if the focus went away (belonged to the window being disabled)
  460. // set it to the OK button
  461. if ( ::GetFocus() == NULL && GetParent())
  462. {
  463. GetParent()->SetFocus();
  464. GetParent()->SendMessage(DM_SETDEFID, IDOK);
  465. }
  466. return sc;
  467. }