home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / internet / ftptree / ftptrdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  9.1 KB  |  381 lines

  1. // FTPTrDlg.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "FTPTREE.h"
  15. #include "FTPtrDlg.h"
  16.  
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CAboutDlg dialog used for App About
  25.  
  26. class CAboutDlg : public CDialog
  27. {
  28. public:
  29.     CAboutDlg();
  30.  
  31. // Dialog Data
  32.     //{{AFX_DATA(CAboutDlg)
  33.     enum { IDD = IDD_ABOUTBOX };
  34.     //}}AFX_DATA
  35.  
  36.     // ClassWizard generated virtual function overrides
  37.     //{{AFX_VIRTUAL(CAboutDlg)
  38.     protected:
  39.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  40.     //}}AFX_VIRTUAL
  41.  
  42. // Implementation
  43. protected:
  44.     //{{AFX_MSG(CAboutDlg)
  45.     //}}AFX_MSG
  46.     DECLARE_MESSAGE_MAP()
  47. };
  48.  
  49. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  50. {
  51.     //{{AFX_DATA_INIT(CAboutDlg)
  52.     //}}AFX_DATA_INIT
  53. }
  54.  
  55. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  56. {
  57.     CDialog::DoDataExchange(pDX);
  58.     //{{AFX_DATA_MAP(CAboutDlg)
  59.     //}}AFX_DATA_MAP
  60. }
  61.  
  62. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  63.     //{{AFX_MSG_MAP(CAboutDlg)
  64.         // No message handlers
  65.     //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67.  
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CFTPTREEDlg dialog
  71.  
  72. CFTPTREEDlg::CFTPTREEDlg(CWnd* pParent /*=NULL*/)
  73.     : CDialog(CFTPTREEDlg::IDD, pParent)
  74. {
  75.     //{{AFX_DATA_INIT(CFTPTREEDlg)
  76.         // NOTE: the ClassWizard will add member initialization here
  77.     //}}AFX_DATA_INIT
  78.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  79.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  80. }
  81.  
  82. CFTPTREEDlg::~CFTPTREEDlg()
  83. {
  84.     // clean up any objets that are still lying around
  85.     if (m_pFtpConnection != NULL)
  86.     {
  87.         m_pFtpConnection->Close();
  88.         delete m_pFtpConnection;
  89.     }
  90.     if (m_pInetSession != NULL)
  91.     {
  92.         m_pInetSession->Close();
  93.         delete m_pInetSession;
  94.     }
  95. }
  96.  
  97. void CFTPTREEDlg::DoDataExchange(CDataExchange* pDX)
  98. {
  99.     CDialog::DoDataExchange(pDX);
  100.     //{{AFX_DATA_MAP(CFTPTREEDlg)
  101.     DDX_Control(pDX, IDC_FTPTREE, m_FtpTreeCtl);
  102.     DDX_Control(pDX, IDC_FTPSITE, m_comboFtpSite);
  103.     //}}AFX_DATA_MAP
  104. }
  105.  
  106. BEGIN_MESSAGE_MAP(CFTPTREEDlg, CDialog)
  107.     //{{AFX_MSG_MAP(CFTPTREEDlg)
  108.     ON_WM_SYSCOMMAND()
  109.     ON_WM_PAINT()
  110.     ON_WM_QUERYDRAGICON()
  111.     ON_BN_CLICKED(ID_BROWSE_SITE, OnBrowseSite)
  112.     ON_WM_SIZE()
  113.     ON_WM_GETMINMAXINFO()
  114.     //}}AFX_MSG_MAP
  115. END_MESSAGE_MAP()
  116.  
  117. /////////////////////////////////////////////////////////////////////////////
  118. // CFTPTREEDlg message handlers
  119.  
  120. BOOL CFTPTREEDlg::OnInitDialog()
  121. {
  122.     CDialog::OnInitDialog();
  123.  
  124.     // Add "About..." menu item to system menu.
  125.  
  126.     // IDM_ABOUTBOX must be in the system command range.
  127.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  128.     ASSERT(IDM_ABOUTBOX < 0xF000);
  129.  
  130.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  131.     CString strAboutMenu;
  132.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  133.     if (!strAboutMenu.IsEmpty())
  134.     {
  135.         pSysMenu->AppendMenu(MF_SEPARATOR);
  136.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  137.     }
  138.  
  139.     // Set the icon for this dialog.  The framework does this automatically
  140.     // when the application's main window is not a dialog
  141.     SetIcon(m_hIcon, TRUE);         // Set big icon
  142.     SetIcon(m_hIcon, FALSE);        // Set small icon
  143.  
  144.     CRect rect;
  145.     GetWindowRect(rect);
  146.     m_OrigSize.cx = rect.Width();
  147.     m_OrigSize.cy = rect.Height();
  148.  
  149.     // FTPTREE-specific initialization follows...
  150.     m_pFtpConnection = NULL;
  151.  
  152.     // the CInternetSession will not be closed or deleted
  153.     // until the dialog is closed
  154.  
  155.     CString str;
  156.     if (!str.LoadString(IDS_APPNAME))
  157.         str = _T("AppUnknown");
  158.     m_pInetSession = new CInternetSession(str, 1, PRE_CONFIG_INTERNET_ACCESS);
  159.  
  160.     // Alert the user if the internet session could
  161.     // not be started and close app
  162.     if (!m_pInetSession)
  163.     {
  164.         AfxMessageBox(IDS_BAD_SESSION, MB_OK);
  165.         OnCancel();
  166.     }
  167.  
  168.     CImageList* pImageList = new CImageList();
  169.     pImageList->Create(16, 15, TRUE, 3, 2);
  170.  
  171.     CBitmap bitmap;
  172.     for (UINT iCnt=IDB_BMERROR; iCnt<=IDB_BMFILE; iCnt++)
  173.     {
  174.         bitmap.LoadBitmap(iCnt);
  175.         pImageList->Add(&bitmap, (COLORREF)0xFFFFFF);
  176.         bitmap.DeleteObject();
  177.     }
  178.  
  179.     m_FtpTreeCtl.SetImageList(pImageList, TVSIL_NORMAL);
  180.  
  181.     // Initialize Tree Control here (a "No Connection"
  182.     // error will be displayed)
  183.     m_FtpTreeCtl.PopulateTree();
  184.  
  185.     // Initialize combo box box
  186.     m_comboFtpSite.GetLBText(0, str);
  187.     m_comboFtpSite.SetWindowText(str);
  188.  
  189.     return TRUE;  // return TRUE unless you set the focus to a control
  190. }
  191.  
  192. void CFTPTREEDlg::OnSysCommand(UINT nID, LPARAM lParam)
  193. {
  194.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  195.     {
  196.         CAboutDlg dlgAbout;
  197.         dlgAbout.DoModal();
  198.     }
  199.     else
  200.     {
  201.         CDialog::OnSysCommand(nID, lParam);
  202.     }
  203. }
  204.  
  205. // If you add a minimize button to your dialog, you will need the code below
  206. // to draw the icon.  For MFC applications using the document/view model,
  207. // this is automatically done for you by the framework.
  208.  
  209. void CFTPTREEDlg::OnPaint()
  210. {
  211.     if (IsIconic())
  212.     {
  213.         CPaintDC dc(this); // device context for painting
  214.  
  215.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  216.  
  217.         // Center icon in client rectangle
  218.         int cxIcon = GetSystemMetrics(SM_CXICON);
  219.         int cyIcon = GetSystemMetrics(SM_CYICON);
  220.         CRect rect;
  221.         GetClientRect(&rect);
  222.         int x = (rect.Width() - cxIcon + 1) / 2;
  223.         int y = (rect.Height() - cyIcon + 1) / 2;
  224.  
  225.         // Draw the icon
  226.         dc.DrawIcon(x, y, m_hIcon);
  227.     }
  228.     else
  229.     {
  230.         CDialog::OnPaint();
  231.     }
  232. }
  233.  
  234. // The system calls this to obtain the cursor to display while the user drags
  235. // the minimized window.
  236. HCURSOR CFTPTREEDlg::OnQueryDragIcon()
  237. {
  238.     return (HCURSOR) m_hIcon;
  239. }
  240.  
  241. void CFTPTREEDlg::OnBrowseSite()
  242. {
  243.     // OnBrowseSite() provides some basic error-checking before
  244.     // passing a pointer to a FtpConnection (and possibly a path
  245.     // to expand) to the tree control
  246.  
  247.     CString strFtpSite;
  248.     CString strServerName;
  249.     CString strObject;
  250.     INTERNET_PORT nPort;
  251.     DWORD dwServiceType;
  252.  
  253.     if (m_pFtpConnection != NULL)
  254.         m_pFtpConnection->Close();
  255.     delete m_pFtpConnection;
  256.     m_pFtpConnection = NULL;
  257.  
  258.     m_comboFtpSite.GetWindowText(strFtpSite);
  259.  
  260.     // update the combo box so the most recently used URL is at the top
  261.     int nListCount = m_comboFtpSite.GetCount();
  262.     CString strTemp;
  263.     while (nListCount > 0)
  264.     {
  265.         m_comboFtpSite.GetLBText(--nListCount, strTemp);
  266.         if (strFtpSite.Compare(strTemp) == 0)
  267.         {
  268.             m_comboFtpSite.DeleteString(nListCount);
  269.             nListCount = 0;
  270.         }
  271.     }
  272.  
  273.     m_comboFtpSite.InsertString(0, strFtpSite);
  274.  
  275.     // the combo box might have 6 entries now; if so, delete the last one
  276.     if (m_comboFtpSite.GetCount() > 5)
  277.     {
  278.         m_comboFtpSite.DeleteString(5);
  279.     }
  280.  
  281.  
  282.     // check to see if this is a reasonable URL --
  283.     // ie "ftp://servername/dirs" or just "servername/dirs"
  284.  
  285.     if (!AfxParseURL(strFtpSite, dwServiceType, strServerName, strObject, nPort))
  286.     {
  287.         // try adding the "ftp://" protocol
  288.         CString strFtpURL = _T("ftp://");
  289.         strFtpURL += strFtpSite;
  290.  
  291.         if (!AfxParseURL(strFtpURL, dwServiceType, strServerName, strObject, nPort))
  292.         {
  293.             AfxMessageBox(IDS_INVALID_URL, MB_OK);
  294.             m_FtpTreeCtl.PopulateTree();
  295.             return;
  296.         }
  297.     }
  298.  
  299.     CWaitCursor cursor; // this will automatically display a wait cursor
  300.  
  301.     // replace the MRU URL in the combo's edit box
  302.     m_comboFtpSite.SetWindowText(strFtpSite);
  303.     m_comboFtpSite.SetEditSel(-1,-1);
  304.  
  305.     // Now open an annonymous FTP connection to the server
  306.     if ((dwServiceType == INTERNET_SERVICE_FTP) && !strServerName.IsEmpty())
  307.     {
  308.         try
  309.         {
  310.             m_pFtpConnection = m_pInetSession->GetFtpConnection(strServerName);
  311.         }
  312.         catch (CInternetException* pEx)
  313.         {
  314.             // catch errors from WinINet
  315.             TCHAR szErr[1024];
  316.             if (pEx->GetErrorMessage(szErr, 1024))
  317.                 AfxMessageBox(szErr, MB_OK);
  318.             else
  319.                 AfxMessageBox(IDS_EXCEPTION, MB_OK);
  320.             pEx->Delete();
  321.  
  322.             m_pFtpConnection = NULL;
  323.         }
  324.     }
  325.     else
  326.     {
  327.         AfxMessageBox(IDS_INVALID_URL, MB_OK);
  328.     }
  329.  
  330.     // PopulateTree() will display an error if the FTP connection
  331.     // could not be made, otherwise, it grabs the root listing
  332.     // and expands any folder indicated by the site name
  333.  
  334.     if (m_pFtpConnection != NULL)
  335.     {
  336.         m_FtpTreeCtl.PopulateTree(m_pFtpConnection, strObject);
  337.     }
  338.     else
  339.     {
  340.         m_FtpTreeCtl.PopulateTree();
  341.     }
  342. }
  343.  
  344.  
  345.  
  346. void CFTPTREEDlg::OnSize(UINT nType, int cx, int cy)
  347. {
  348.     CDialog::OnSize(nType, cx, cy);
  349.  
  350.     // resize the tree control to appropriately match the new size
  351.     if ((cx > 0) && (cy > 0))
  352.     {
  353.         CRect rect;
  354.         if (m_FtpTreeCtl.m_hWnd != NULL)
  355.         {
  356.             m_FtpTreeCtl.GetWindowRect(rect);
  357.             ScreenToClient(rect);
  358.             m_FtpTreeCtl.MoveWindow(rect.left, rect.top,
  359.                                 cx-(2*rect.left), cy-rect.top-rect.left);
  360.         }
  361.     }
  362.  
  363. }
  364.  
  365. void CFTPTREEDlg::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
  366. {
  367.     CDialog::OnGetMinMaxInfo(lpMMI);
  368.  
  369.     static BOOL bSetSize;
  370.  
  371.     if (bSetSize)
  372.     {
  373.         lpMMI->ptMinTrackSize.x = m_OrigSize.cx;
  374.         lpMMI->ptMinTrackSize.y = m_OrigSize.cy;
  375.     }
  376.     else
  377.     {
  378.         bSetSize = TRUE;
  379.     }
  380. }
  381.