sharedlg.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 11k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. //////////////////////////////////////////////////////////////////////////////
  2. /*  File: sharedlg.cpp
  3.     Description: Displays a property-sheet-like dialog containing
  4.         CSC statistics about a given network share.
  5.     Revision History:
  6.     Date        Description                                          Programmer
  7.     --------    ---------------------------------------------------  ----------
  8.     11/25/97    Initial creation.                                    BrianAu
  9.     05/11/98    Changed dialog to a property sheet.                  BrianAu
  10. */
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "pch.h"
  13. #pragma hdrstop
  14. #include "sharedlg.h"
  15. #include <resource.h>
  16. #include "viewerp.h"
  17. #include "uncpath.h"
  18. #include "cscutils.h"
  19. #include "options.h"
  20. #include "msgbox.h"
  21. #include "uihelp.h"
  22. #include "uuid.h"
  23. #include "security.h"
  24. #include "util.h"     // Utils from "dll" directory.
  25. const DWORD SharePropSheet::m_rgHelpIDs[] = {
  26.     IDC_SHRSUM_TXT_SHARENAME,       HIDC_SHRSUM_TXT_SHARENAME,
  27.     IDC_SHRSUM_TXT_SYNC,            HIDC_SHRSUM_TXT_SYNC,
  28.     IDC_STATIC2,                    IDH_NO_HELP,
  29.     IDC_STATIC3,                    IDH_NO_HELP,
  30.     IDC_STATIC4,                    IDH_NO_HELP,
  31.     0,0
  32.     };
  33. SharePropSheet::SharePropSheet(
  34.     HINSTANCE hInstance,
  35.     LONG *pDllRefCnt,
  36.     HWND hwndParent,
  37.     LPCTSTR pszShare
  38.     ) : m_hInstance(hInstance),
  39.         m_pDllRefCnt(pDllRefCnt),
  40.         m_hwndParent(hwndParent),
  41.         m_strShare(pszShare)
  42. {
  43. }
  44. SharePropSheet::~SharePropSheet(
  45.     void
  46.     )
  47. {
  48. }
  49. BOOL CALLBACK 
  50. SharePropSheet::AddPropSheetPage(
  51.     HPROPSHEETPAGE hpage, 
  52.     LPARAM lParam
  53.     )
  54. {
  55.     DBGTRACE((DM_OPTDLG, DL_MID, TEXT("SharePropSheet::AddPropSheetPage")));
  56.     PROPSHEETHEADER * ppsh = (PROPSHEETHEADER *)lParam;
  57.     if (ppsh->nPages < SharePropSheet::MAXPAGES)
  58.     {
  59.         ppsh->phpage[ppsh->nPages++] = hpage;
  60.         return TRUE;
  61.     }
  62.     return FALSE;
  63. }
  64. DWORD
  65. SharePropSheet::Run(
  66.     void
  67.     )
  68. {
  69.     DWORD dwError = ERROR_SUCCESS;
  70.     try
  71.     {
  72.         CString strSheetTitle(m_hInstance, IDS_CSCOPT_PROPSHEET_TITLE);
  73.         HPROPSHEETPAGE rghPages[SharePropSheet::MAXPAGES];
  74.         HPROPSHEETPAGE hpage;
  75.         PROPSHEETHEADER psh;
  76.         PROPSHEETPAGE psp;
  77.         ZeroMemory(&psh, sizeof(psh));
  78.         ZeroMemory(&psp, sizeof(psp));
  79.         //
  80.         // Define sheet.
  81.         //
  82.         psh.dwSize          = sizeof(PROPSHEETHEADER);
  83.         psh.dwFlags         = 0;
  84.         psh.hInstance       = m_hInstance;
  85.         psh.hwndParent      = m_hwndParent;
  86.         psh.pszIcon         = MAKEINTRESOURCE(IDI_CSCUI_ICON);
  87.         psh.pszCaption      = strSheetTitle;
  88.         psh.nPages          = 0;
  89.         psh.nStartPage      = 0;
  90.         psh.phpage          = rghPages;
  91.         if (!m_strShare.IsEmpty())
  92.         {
  93.             //
  94.             // Define the "Summary" page.
  95.             // Remaining pages are added dynamically by options.cpp.
  96.             // The separation is because the dynamic pages are also used by the
  97.             // shell's "View Options" property sheet.
  98.             //
  99.             psp.dwSize          = sizeof(psp);
  100.             psp.dwFlags         = PSP_USEREFPARENT;
  101.             psp.hInstance       = m_hInstance;
  102.             psp.pszTemplate     = MAKEINTRESOURCE(IDD_CACHEVIEW_SHARESUMMARY);
  103.             psp.hIcon           = NULL;
  104.             psp.pszTitle        = NULL;
  105.             psp.pfnDlgProc      = (DLGPROC)SharePropSheet::GenPgDlgProc;
  106.             psp.lParam          = (LPARAM)this;
  107.             psp.pcRefParent     = (UINT *)m_pDllRefCnt;
  108.             psp.pfnCallback     = NULL;
  109.             hpage = CreatePropertySheetPage(&psp);
  110.             if (NULL == hpage)
  111.             {
  112.                 //
  113.                 // Early return if we can't create the "General" page.
  114.                 // No reason to continue on.
  115.                 //
  116.                 return GetLastError();
  117.             }
  118.             psh.phpage[psh.nPages++] = hpage;
  119.         }
  120.         if (!g_pSettings->NoConfigCache())
  121.         {
  122.             //
  123.             // Policy doesn't prevent user from configuring CSC cache.
  124.             // Add the dynamic page(s).
  125.             //
  126.             CCoInit coinit;
  127.             HRESULT hr = coinit.Result();
  128.             if (SUCCEEDED(hr))
  129.             {
  130.                 com_autoptr<IShellExtInit> psei;
  131.                 hr = CoCreateInstance(CLSID_OfflineFilesOptions,
  132.                                       NULL,
  133.                                       CLSCTX_INPROC_SERVER,
  134.                                       IID_IShellExtInit,
  135.                                       reinterpret_cast<void **>(psei.getaddr()));
  136.                 if (SUCCEEDED(hr))
  137.                 {
  138.                     com_autoptr<IShellPropSheetExt> pspse;
  139.                     hr = psei->QueryInterface(IID_IShellPropSheetExt,
  140.                                              reinterpret_cast<void **>(pspse.getaddr()));
  141.                     if (SUCCEEDED(hr))
  142.                     {
  143.                         hr = pspse->AddPages(AddPropSheetPage, (LPARAM)&psh);
  144.                     }
  145.                 }
  146.                 else
  147.                 {
  148.                     DBGERROR((TEXT("CoCreateInstance failed with result 0x%08X"), hr));
  149.                 }
  150.             }
  151.             else
  152.             {
  153.                 DBGERROR((TEXT("CoInitialize failed with result 0x%08X"), hr));
  154.             }
  155.         }
  156.         switch(PropertySheet(&psh))
  157.         {
  158.             case ID_PSREBOOTSYSTEM:
  159.                 //
  160.                 // User wants to change enabled state of CSC.  Requires reboot.
  161.                 //
  162.                 if (IDYES == CscMessageBox(m_hwndParent, 
  163.                                            MB_YESNO | MB_ICONINFORMATION,
  164.                                            m_hInstance,
  165.                                            IDS_REBOOTSYSTEM))
  166.                 {                                     
  167.                     dwError = CSCUIRebootSystem();
  168.                     if (ERROR_SUCCESS != dwError)
  169.                     {
  170.                         DBGERROR((TEXT("Reboot failed with error %d"), dwError));
  171.                         //
  172.                         // BUGBUG:  This needs a reboot-specific message.
  173.                         //
  174.                         CscWin32Message(m_hwndParent, dwError, CSCUI::SEV_ERROR);
  175.                     }
  176.                 }
  177.                 dwError = ERROR_SUCCESS;  // Run() succeeded.
  178.                 break;
  179.             case -1:
  180.             {
  181.                 dwError = GetLastError();
  182.                 DBGERROR((TEXT("PropertySheet failed with error %d"), dwError));
  183.                 CscWin32Message(m_hwndParent, dwError, CSCUI::SEV_ERROR);
  184.                 break;
  185.             }
  186.             default:
  187.                 break;
  188.         }
  189.     }
  190.     catch(CException& e)
  191.     {
  192.         DBGERROR((TEXT("C++ exception %d caught in SharePropSheet::Run"), e.dwError));
  193.         CscWin32Message(NULL, e.dwError, CSCUI::SEV_ERROR);
  194.     }
  195.     return dwError;
  196. }
  197. BOOL CALLBACK
  198. SharePropSheet::GenPgDlgProc(
  199.     HWND hwnd,
  200.     UINT message,
  201.     WPARAM wParam,
  202.     LPARAM lParam
  203.     )
  204. {
  205.     SharePropSheet *pThis = reinterpret_cast<SharePropSheet *>(GetWindowLongPtr(hwnd, DWLP_USER));
  206.     try
  207.     {
  208.         switch(message)
  209.         {
  210.             case WM_INITDIALOG:
  211.             {
  212.                 PROPSHEETPAGE *pPage = (PROPSHEETPAGE *)lParam;
  213.                 pThis = reinterpret_cast<SharePropSheet *>(pPage->lParam);
  214.                 SetWindowLongPtr(hwnd, DWLP_USER, (INT_PTR)pThis);
  215.                 DBGASSERT((NULL != pThis));
  216.                 pThis->OnInitDialog(hwnd);
  217.                 return TRUE;
  218.             }
  219.             case WM_HELP:
  220.                 if (HELPINFO_WINDOW == ((LPHELPINFO)lParam)->iContextType)
  221.                 {
  222.                     WinHelp((HWND)(((LPHELPINFO)lParam)->hItemHandle), 
  223.                              c_szHelpFile,
  224.                              HELP_WM_HELP, 
  225.                              (DWORD)((LPTSTR)m_rgHelpIDs));
  226.                 }
  227.                 break;
  228.             case WM_CONTEXTMENU:
  229.                     WinHelp((HWND)wParam,
  230.                              c_szHelpFile,
  231.                              HELP_CONTEXTMENU, 
  232.                              (DWORD)((LPTSTR)m_rgHelpIDs));
  233.                 break;
  234.             default:
  235.                 break;
  236.         }
  237.     }
  238.     catch(CException& e)
  239.     {
  240.         DBGERROR((TEXT("C++ exception %d caught in SharePropSheet::GenPgDlgProc"), e.dwError));
  241.         CscWin32Message(NULL, e.dwError, CSCUI::SEV_ERROR);
  242.     }
  243.     return FALSE;
  244. }
  245. void
  246. SharePropSheet::OnInitDialog(
  247.     HWND hwnd
  248.     )
  249. {
  250.     CscShareInformation si;
  251.     CscGetShareInformation(m_strShare, &si);
  252.     SHFILEINFO sfi;
  253.     if (0 != SHGetFileInfo(m_strShare,
  254.                            0,
  255.                            &sfi,
  256.                            sizeof(sfi),
  257.                            SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_LARGEICON))
  258.     {
  259.         CString strText(sfi.szDisplayName);
  260.         if (!si.Connected())
  261.         {
  262.             //
  263.             // If disconnected, append "(Disconnected)" to the share name.
  264.             //
  265.             CString strOffline(m_hInstance, IDS_SHARE_STATUS_OFFLINE);
  266.             CString strSuffix;
  267.             strSuffix.Format(TEXT(" (%1)"), strOffline.Cstr());
  268.             strText += strSuffix;
  269.         }
  270.         //
  271.         // Set the share display name and icon.
  272.         //
  273.         SetWindowText(GetDlgItem(hwnd, IDC_SHRSUM_TXT_SHARENAME), (LPCTSTR)strText);
  274.         SendMessage(GetDlgItem(hwnd, IDC_SHRSUM_ICON), STM_SETICON, (WPARAM)sfi.hIcon, 0);
  275.     }
  276.     //
  277.     // Set the two file count fields (pinned and temporary).
  278.     //
  279.     CString strNumber;
  280.     strNumber.FormatNumber(si.PinnedFileCount());
  281.     SetWindowText(GetDlgItem(hwnd, IDC_SHRSUM_TXT_PINNEDFILES), strNumber);
  282.     strNumber.FormatNumber(si.FileCount() - si.PinnedFileCount());
  283.     SetWindowText(GetDlgItem(hwnd, IDC_SHRSUM_TXT_TEMPFILES), strNumber);
  284.     if (0 < si.StaleFileCount())
  285.     {
  286.         //
  287.         // One or more files are stale.  Change the icon and text.
  288.         //
  289.         CString strText;
  290.         SendMessage(GetDlgItem(hwnd, IDC_SHRSUM_ICON_SYNC), 
  291.                     STM_SETICON,
  292.                     (WPARAM)LoadIcon(NULL, MAKEINTRESOURCE(IDI_EXCLAMATION)),
  293.                     0);
  294.         strNumber.FormatNumber(si.StaleFileCount());
  295.         strText.Format(m_hInstance, 
  296.                        1 == si.StaleFileCount() ? IDS_FMT_SHARE_ONENOTSYNC : 
  297.                                                   IDS_FMT_SHARE_MULTNOTSYNC,
  298.                        strNumber.Cstr());
  299.         SetWindowText(GetDlgItem(hwnd, IDC_SHRSUM_TXT_SYNC), strText);
  300.     }        
  301. }