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

Windows Kernel

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include <wininet.h>
  3. #include "fldrpg.h"
  4. enum {
  5.     COL_FOLDER,
  6.     COL_COMMENT,
  7.     NUM_COLUMNS
  8. };
  9. INT_PTR CNetPlacesWizardPage2::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  10. {
  11.     switch(uMsg)
  12.     {
  13.         HANDLE_MSG(hwndDlg, WM_INITDIALOG, OnInitDialog);
  14.         HANDLE_MSG(hwndDlg, WM_DESTROY, OnDestroy);
  15.         HANDLE_MSG(hwndDlg, WM_NOTIFY, OnNotify);
  16.     default:
  17.         break;
  18.     }
  19.     return FALSE;
  20. }
  21. BOOL CNetPlacesWizardPage2::OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
  22. {
  23.     HWND hwndList = GetDlgItem(hwnd, IDC_SELECTFOLDER_LIST);
  24.     //
  25.     // Limit the size of the edit controls
  26.     //
  27.     Edit_LimitText(GetDlgItem(hwnd, IDC_FOLDER_EDIT),
  28.                    MAX_PATH - 1);
  29.     // Trace(TRACE_LEVEL_FLOW, TEXT("Entering InitListViewn"));
  30.     //
  31.     // Use a do-while loop so we can break out of it
  32.     // on errors (note:  it's while(0))
  33.     //
  34.     do
  35.     {
  36.         ListView_SetImageList(hwndList, 
  37.                               m_pdata->himlSmall, 
  38.                               LVSIL_SMALL);
  39.             
  40.         //
  41.         // Create 2 listview columns.  If more are added, the column
  42.         // width calculation needs to change.
  43.         //
  44.         _ASSERT(NUM_COLUMNS == 2);
  45.         LV_COLUMN   lvc;
  46.         RECT        rcLV;
  47.         TCHAR       tszColumnLabel[MAX_CAPTION + 1];
  48.         DWORD       dwCol;
  49.  
  50.         GetClientRect(hwndList, &rcLV);
  51.         rcLV.right -= GetSystemMetrics(SM_CXVSCROLL);
  52.         ZeroMemory(&lvc, sizeof(lvc));
  53.         lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  54.         lvc.fmt = LVCFMT_LEFT;
  55.         lvc.cx = rcLV.right / 3;
  56.         lvc.pszText = tszColumnLabel;
  57.  
  58.         for (dwCol = 0; dwCol < NUM_COLUMNS; dwCol++) 
  59.         { 
  60.             lvc.iSubItem = dwCol; 
  61.             LoadString(g_hInstance, IDS_FIRSTCOLUMN + dwCol, tszColumnLabel, 
  62.                 ARRAYSIZE(tszColumnLabel));
  63.             //
  64.             // Once the first column has been inserted, allocate the 
  65.             // remaining width to the second column.
  66.             //
  67.             if (dwCol)
  68.             {
  69.                 lvc.cx = rcLV.right - lvc.cx;
  70.             }
  71.             if (ListView_InsertColumn(hwndList, dwCol, &lvc) == -1)
  72.             {
  73.                 _ASSERT(FALSE);
  74.                 break;
  75.             }
  76.         }
  77.     } while (0);   
  78.     
  79.     m_fListValid = FALSE;
  80.     return TRUE;
  81. }
  82. BOOL CNetPlacesWizardPage2::OnDestroy(HWND hwnd)
  83. {
  84.     HWND hwndList = GetDlgItem(hwnd, IDC_SELECTFOLDER_LIST);
  85.     
  86.     if (hwndList != NULL)
  87.         ListView_DeleteAllItems(hwndList);
  88.     return TRUE;
  89. }
  90. BOOL CNetPlacesWizardPage2::OnNotify(HWND hwnd, int idCtrl, LPNMHDR pnmh)
  91. {
  92.     switch (pnmh->code) 
  93.     {
  94.     case PSN_SETACTIVE:
  95.         {
  96.             CWaitCursor     waitcursor;
  97.             TCHAR           tszStaticText[MAX_PATH + MAX_STATIC + 1];
  98.             TCHAR           tszFormatString[MAX_STATIC + 1];
  99.             DWORD           dwError;
  100.             HWND            hwndList = GetDlgItem(hwnd, IDC_SELECTFOLDER_LIST);
  101.             ZeroMemory(tszStaticText,  sizeof(tszStaticText));
  102.             ZeroMemory(tszFormatString, sizeof(tszFormatString));
  103.             //
  104.             // Load in the static text
  105.             //
  106.             FormatMessageString(IDS_SELECTFOLDER_STATIC, tszStaticText, ARRAYSIZE(tszStaticText), m_pdata->netplace.GetResourceName());
  107.             dwError = SetDlgItemText(hwnd, IDC_SELECTFOLDER_STATIC, tszStaticText);
  108.             if (!m_fListValid)
  109.             {
  110.                 if (!AddShareNamesToList(hwndList))
  111.                 {
  112.                     SetWindowLongPtr(hwnd, DWLP_MSGRESULT, m_pdata->idWelcomePage);
  113.                     return -1;
  114.                 }
  115.             
  116.                 //
  117.                 // Focus on the first item in the ListView
  118.                 //
  119.                 ListView_SetItemState(hwndList, 0, LVIS_FOCUSED, LVIS_FOCUSED);
  120.             }
  121.             // Set wizard buttons depending on whether an item is selected in
  122.             // the listbox
  123.             if (ListView_GetNextItem(hwndList, -1, LVNI_SELECTED) == -1)
  124.             {
  125.                 PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_BACK);
  126.             }
  127.             else
  128.             {
  129.                 PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_BACK | PSWIZB_NEXT);
  130.             }
  131.             return TRUE;
  132.         }
  133.     case PSN_WIZBACK:
  134.         {
  135.             //
  136.             // Get rid of whatever's in the ListView
  137.             //
  138.             ListView_DeleteAllItems(GetDlgItem(hwnd, IDC_SELECTFOLDER_LIST));
  139.             m_fListValid = FALSE;
  140.         
  141.             SetWindowLongPtr(hwnd, DWLP_MSGRESULT, m_pdata->idWelcomePage);
  142.             return -1;
  143.         }
  144.     case PSN_WIZNEXT:
  145.         {
  146.             HWND hwndList = GetDlgItem(hwnd, IDC_SELECTFOLDER_LIST);
  147.             int iItem = ListView_GetNextItem(hwndList, -1, LVNI_SELECTED);
  148.             if (iItem != -1)
  149.             {
  150.                 GetFolderChoice(hwndList, iItem);
  151.                 // Set m_fListValid to TRUE so we don't refill the ListView
  152.                 // if the user presses Back from the next page
  153.     
  154.                 m_fListValid = TRUE;
  155.         
  156.                 // Set the wizard to display page 3 (completion)
  157.                 SetWindowLongPtr(hwnd, DWLP_MSGRESULT, m_pdata->idCompletionPage);
  158.             }
  159.             else
  160.             {
  161.                 // Some weird error occured; don't proceed to the completion page
  162.                 SetWindowLongPtr(hwnd, DWLP_MSGRESULT, m_pdata->idFoldersPage);
  163.             }
  164.             return -1;
  165.         }
  166.     case LVN_ITEMCHANGED:
  167.         {
  168.             LPNMLISTVIEW pnmhList = (LPNMLISTVIEW) pnmh;
  169.             if (pnmhList->uChanged & LVIF_STATE)
  170.             {
  171.                 if (pnmhList->uNewState & LVIS_SELECTED)
  172.                 {
  173.                     // User has selected a different folder in the ListView,
  174.                     // so activate the Next button
  175.                     PropSheet_SetWizButtons(GetParent(hwnd),
  176.                                             PSWIZB_BACK | PSWIZB_NEXT);
  177.                 }
  178.                 else
  179.                 {
  180.                     // There is now nothing selected in the ListView, so
  181.                     // disable the Next button.
  182.                     PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_BACK);
  183.                 }
  184.             }
  185.             return FALSE;
  186.         }
  187.     case NM_DBLCLK:
  188.         {
  189.             LPNMLISTVIEW pnmhList = (LPNMLISTVIEW) pnmh;
  190.             if (-1 != pnmhList->iItem)
  191.             {
  192.                 // Act like we pressed next
  193.                 PropSheet_PressButton(GetParent(hwnd), PSBTN_NEXT);
  194.             }
  195.             return TRUE;
  196.         }
  197.     }
  198.     return FALSE;
  199. }
  200. BOOL CNetPlacesWizardPage2::AddShareNamesToList(HWND hwndList)
  201. {
  202.    // Trace(TRACE_LEVEL_FLOW, TEXT("Entering EnumerateMachineFoldersn"));
  203.     BOOL fSuccess = FALSE;
  204.     DWORD       dwRetVal;
  205.     NETRESOURCE nr = {0};
  206.     HANDLE      hEnum;
  207.     TCHAR szResourceName[MAX_PATH + 1];
  208.     lstrcpyn(szResourceName, m_pdata->netplace.GetResourceName(), ARRAYSIZE(szResourceName));
  209.     nr.dwType           = RESOURCETYPE_DISK;         // Disk resources only
  210.     nr.lpRemoteName     = szResourceName;            // Remote machine name
  211.     //
  212.     // Open a resource enumeration
  213.     //
  214.     if (NO_ERROR == WNetOpenEnum(RESOURCE_GLOBALNET,  // Resources on the network
  215.                      RESOURCETYPE_DISK,   // Disk resources only
  216.                      0,                   // List all resources
  217.                      &nr,
  218.                      &hEnum))
  219.     {
  220.         DWORD           dwIndex;
  221.         DWORD           dwCount     = 0xffffffff;
  222.         BYTE            lpResources[4096];
  223.         DWORD           dwSize = sizeof(lpResources);
  224.         LV_ITEM         lvi;
  225.         UINT            i;
  226.         LPNETRESOURCE   lpnr;
  227.     
  228.         lvi.mask        = LVIF_TEXT | LVIF_IMAGE;
  229.         lvi.iItem       = COL_FOLDER;
  230.         lvi.iSubItem    = 0;
  231.         lvi.iImage      = Shell_GetCachedImageIndex(c_szShell32Dll, II_FOLDER, 0); // EIRESID(IDI_FOLDER), 0);
  232.         //
  233.         // Enumerate the network resources until there are none left
  234.         //
  235.         while ((dwRetVal = WNetEnumResource(hEnum,
  236.                                             &dwCount,
  237.                                             lpResources,
  238.                                             &dwSize))
  239.                     == NO_ERROR)
  240.         {
  241.             lpnr = (LPNETRESOURCE) lpResources;
  242.             //
  243.             // dwCount contains the number of resources read in
  244.             //
  245.             for (i = 0; i < dwCount; i++)
  246.             {
  247.                 //
  248.                 // Traverse the resource name and strip off the server name
  249.                 //
  250.                 lvi.pszText = StrChrW(lpnr[i].lpRemoteName + 2, L'\');
  251.                 TraceAssert(lvi.pszText);
  252.                 lvi.pszText++;
  253.                 //
  254.                 // Add the folder to the ListView
  255.                 //
  256.                 if ((dwIndex = ListView_InsertItem(hwndList, &lvi)) == -1)
  257.                 {                
  258.                     WNetCloseEnum(hEnum);
  259.                     TraceAssert(FALSE);
  260.                     return FALSE;
  261.                 }
  262.             
  263.                 //
  264.                 // Add the comment to the ListView
  265.                 //
  266.                 ListView_SetItemText(hwndList, 
  267.                                      dwIndex, 
  268.                                      COL_COMMENT, 
  269.                                      lpnr[i].lpComment);
  270.             }
  271.     
  272.             //
  273.             // Reset dwCount so WNetEnumResource reads in as much as possible
  274.             //
  275.             dwCount = 0xffffffff;
  276.         }
  277.     }
  278.     //
  279.     // We stopped enumerating -- it should only be 
  280.     // because we enumerated everything
  281.     //
  282.     switch (dwRetVal)
  283.     {
  284.     case ERROR_INVALID_ACCESS:
  285.         // A server with the provided name could not be found
  286.         DisplayFormatMessage(hwndList, IDS_ERR_CAPTION, IDS_INVALID_REMOTE_PATH,
  287.             MB_OK|MB_ICONINFORMATION, m_pdata->netplace.GetResourceName());
  288.         break;
  289.     default:
  290.         // This may be a normal situation or an error
  291.         fSuccess = (dwRetVal == ERROR_NO_MORE_ITEMS) && (ListView_GetItemCount(hwndList) > 0);
  292.         if (!fSuccess)
  293.         {
  294.             DisplayFormatMessage(hwndList, IDS_ERR_CAPTION, IDS_NOFOLDERS_STATIC, 
  295.                 MB_OK|MB_ICONINFORMATION, m_pdata->netplace.GetResourceName());
  296.         }
  297.         break;            
  298.     }
  299.     WNetCloseEnum(hEnum);
  300.     return fSuccess;
  301. }
  302. void CNetPlacesWizardPage2::GetFolderChoice(HWND hwndList, int iItem)
  303. {
  304.     BOOL fEnableFolderEdit = FALSE;
  305.     BOOL fEnableBrowse = FALSE;
  306.     BOOL fEnableNext = FALSE;
  307.     // Trace(TRACE_LEVEL_FLOW, TEXT("Entering WizardSelectFolderPage_ProcessChoicen"));
  308.     TCHAR   tszFolder[MAX_PATH - CNLEN];
  309.     DWORD   dwError;
  310.     LVITEM  lvi;
  311.     ZeroMemory(tszFolder, sizeof(tszFolder));
  312.     lvi.mask        = LVIF_TEXT;
  313.     lvi.iItem       = iItem;
  314.     lvi.iSubItem    = 0;
  315.     lvi.pszText     = tszFolder;
  316.     lvi.cchTextMax  = MAX_PATH - CNLEN - 1;
  317.     dwError = ListView_GetItem(hwndList, &lvi);
  318.     TCHAR szTemp[MAX_PATH + 1];
  319.     dwError = wnsprintf(szTemp, ARRAYSIZE(szTemp), TEXT("%s\%s"), m_pdata->netplace.GetResourceName(), tszFolder);
  320.     if (0 != dwError)
  321.     {
  322.         m_pdata->netplace.SetResourceName(GetParent(hwndList), szTemp);
  323.     }
  324. }