home *** CD-ROM | disk | FTP | other *** search
-
- #include <windows.h>
- #include <tchar.h>
- #include <commctrl.h>
- #include <wininet.h>
- #include "comctlhd.h"
- #include "cdlgopt.h"
- #include "clist.h"
- #include "cdlg.h"
- #include "capp.h"
- #include "resource.h"
-
- extern CApp *lpCApp;
-
-
-
- //**********************************************************************
- // CDlg::CDlg
- //
- // Purpose: Constructor for CDlg class
- // Parameters:
- // HANDLE hInst - Instance handle for this instance from WinMain
- //
- //********************************************************************
-
- CDlg::CDlg(HINSTANCE hInst)
- {
- m_hInst = hInst;
- m_hWnd = NULL;
- m_pszUser = m_szUser;
- lstrcpy(m_pszUser, _TEXT("anonymous"));
- m_pszPwd = m_szPwd;
- *m_pszPwd = NULL;
- m_pszProxy = m_szProxy;
- *m_pszProxy = NULL;
- m_dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
- m_anonymous = 1;
- m_hmnu = NULL;
-
- //initialize global handle used to cancel requests
- m_hLastHandleCreated = NULL;
-
- //initialize global session handle
- m_hSession = NULL;
-
- }
-
-
- //**********************************************************************
- // CDlg::~CDlg
- //
- // Purpose: Destructor for CDlg class
- //********************************************************************
-
- CDlg::~CDlg()
- {
- delete m_pcctrl;
- return;
- }
-
- //**********************************************************************
- // CDlg::Init
- //
- // Purpose: Create and show modeless dialog
- //
- // Parameters:
- // nCmdShow int from WinMain
- //
- // Return Value:
- // BOOL - TRUE if initialization succeeded, FALSE otherwise.
- //
- //********************************************************************
-
- BOOL CDlg::Init(int nCmdShow)
- {
- m_hWnd = CreateDialog(m_hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (int (__stdcall *)(void))DialogProc);
-
- // If window could not be created, return "failure"
- if (!m_hWnd)
- return FALSE;
-
- ShowWindow(m_hWnd, nCmdShow);
-
- return TRUE;
- }
-
-
- //**********************************************************************
- // CDlg::OnGoto
- //
- // Purpose: Event handler for initiating an Internet connection
- // and enumerating directories after user has entered a URL
- //
- // Parameters: None
- //
- // Return Value:
- // BOOL - TRUE if initialization succeeded, FALSE otherwise.
- //
- //********************************************************************
-
- BOOL CDlg::OnGoto(DWORD dwParam)
- {
- TCHAR szURL[80], bufURL[80], szServer[80], msgbuf[80];
- TVNODEINFO tvnodei;
- LPTVNODEINFO lptvnodei;
- TVDATA tvdta;
- HTREEITEM hItem;
- HINTERNET hConnect;
- int RequestType;
- DWORD dwContext;
- BOOL ret;
-
- if (0 == dwParam)
- {
- RequestType = REQUEST_NEW_INTERNET_SITE;
-
- //obtain the URL entered by the user
- ::GetWindowText(GetDlgItem(m_hWnd, IDC_URL), szURL, 80);
- if (0 == lstrlen(szURL))
- {
- MessageBox(m_hWnd, TEXT("Please enter a URL"), TEXT("Error"), MB_ICONEXCLAMATION | MB_OK) ;
- return FALSE;
- }
- if (0 == lstrlen(m_pszUser) || 0 == lstrlen(m_pszPwd))
- {
- MessageBox(m_hWnd, TEXT("Please set user and password under Options"), TEXT("Error"), MB_ICONEXCLAMATION | MB_OK) ;
- return FALSE;
- }
-
- }
- else
- {
- ret = Animate_Play(GetDlgItem(m_hWnd, IDC_ANIMATE1), 0, -1, -1);
- lptvnodei = (LPTVNODEINFO)dwParam;
- lstrcpy(szURL, lptvnodei->pszSite);
- ParseServer(szURL, bufURL, szServer);
- wsprintf(msgbuf, TEXT("Querying %s ..."), bufURL);
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)msgbuf);
-
- lstrcpy(lptvnodei->pszServer, szServer);
- dwContext = (DWORD)lptvnodei;
- InetConnect(lptvnodei->pszServer, &hConnect, dwContext);
- return TRUE;
-
- }
- ret = Animate_Play(GetDlgItem(m_hWnd, IDC_ANIMATE1), 0, -1, -1);
-
- //parse the server name if the URL contains protocol
- //information (i.e., ftp://). Also prepend the ftp:// to
- //the URL if not entered for tracking purposes later in
- //the TreeView control.
- ParseServer(szURL, bufURL, szServer);
-
- wsprintf(msgbuf, TEXT("Opening %s ..."), bufURL);
- ret = SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)msgbuf);
-
-
- //the TVDATA structure holds information to create and
- //insert a TreeView item
- //SETTVDATA(tvdta, 1, 1, bufURL, NULL);
- SETTVDATA(tvdta, 1, 1, NULL);
-
- //the TVNODEINFO structure holds key information for
- //a TreeView item (32-bit lParam value of the TV_ITEM structure)
- SETTVNODEINFO(tvnodei, NULL, FALSE, hConnect, bufURL, 0,0, NULL, TEXT(""));
-
- tvnodei.requestType = RequestType;
-
- hItem = m_pcctrl->TreeViewAdd(bufURL, &tvdta, &tvnodei, &dwContext);
- lptvnodei = (LPTVNODEINFO)dwContext;
- lptvnodei->hTreeItem = hItem;
- lstrcpy(lptvnodei->pszServer, szServer);
-
- //InternetOpen and InternetConnect
- InetConnect(lptvnodei->pszServer, &hConnect, dwContext);
-
- return TRUE;
-
- }
-
- void CDlg::InetConnect(LPTSTR lpstrURL, LPHINTERNET phConnect, DWORD dwContext)
- {
- INTERNET_STATUS_CALLBACK dwISC;
- DWORD dwError;
- TCHAR buf[80], szEditText[STRMAX];
-
- if (NULL == m_hSession)
- {
- m_hSession = ::InternetOpen(TEXT("AsyncFtp"),
- m_dwAccessType,
- m_szProxy,
- '\0',
- INTERNET_FLAG_ASYNC);
- }
- dwError = GetLastError();
- if (NULL == m_hSession)
- {
- wsprintf(buf, TEXT("InternetOpen failed - error code: %d"), dwError);
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)buf);
- lstrcat(buf, TEXT("\r\n"));
- GetWindowText(GetDlgItem(m_hWnd, IDC_STATUS), szEditText, STRMAX );
- lstrcat(szEditText, buf);
- SetWindowText(GetDlgItem(m_hWnd, IDC_STATUS), szEditText);
-
- return;
- }
-
- dwISC = InternetSetStatusCallback(m_hSession, InternetCallback);
- if (INTERNET_INVALID_STATUS_CALLBACK == dwISC)
- {
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)"Unexpected Error");
- return;
- }
-
- *phConnect = ::InternetConnect(m_hSession,
- lpstrURL,
- INTERNET_INVALID_PORT_NUMBER,
- m_szUser,
- m_szPwd,
- INTERNET_SERVICE_FTP,
- 0,
- (DWORD)dwContext);
-
- dwError = GetLastError();
- return;
- }
-
- //**********************************************************************
- // CDlg::OnCommand
- //
- // Purpose: Process WM_COMMAND messages for the main dialog procedure
- //
- //********************************************************************
- BOOL CDlg::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
- {
- HTREEITEM htvitem;
- TV_ITEM tvi;
- LPTVNODEINFO lptvnodei;
- UINT state1, state2;
- TCHAR buf[80], msgbuf[80];
- BOOL ret;
- DWORD dwContext, dwError;
- HWND hWndEdit;
- node *pnode;
- POINT pt;
- CDlgOpt *lpCDlgOpt;
- DLGDATA dlgdata;
-
- switch (LOWORD(wParam))
- {
- case IDCANCEL:
- DestroyWindow(hWnd);
- break;
- case IDGOTO:
- OnGoto(0);
- break;
- case IDC_CANCELREQ: //cancel asynchronous request
- //an intermediate handle obtained from
- //INTERNET_STATUS_HANDLE_CREATED is stored
- //in a global variable - the value is set
- //to zero once the request has been complete
- if (m_hLastHandleCreated)
- {
- InternetCloseHandle(m_hLastHandleCreated);
- }
- break;
- case IDC_HELPABOUT:
- DialogBox(
- m_hInst,
- MAKEINTRESOURCE(IDD_HELPABOUT),
- hWnd,
- (DLGPROC)AboutDlgProc);
-
- break;
- case IDC_OPTIONS:
- dlgdata.pszUser = m_pszUser;
- dlgdata.pszPwd = m_pszPwd;
- dlgdata.pszProxy = m_pszProxy;
- dlgdata.dwAccessType = m_dwAccessType;
- dlgdata.anonymous = m_anonymous;
- //lpCDlgOpt = new CDlgOpt();
- //lpCApp->m_pCDlgOpt = lpCDlgOpt;
- lpCDlgOpt = lpCApp->m_pCDlgOpt;
- ret = lpCDlgOpt->Init(m_hInst, hWnd, &dlgdata);
- if (ID_SET == ret )
- {
- //store data
- m_dwAccessType = lpCDlgOpt->m_dwAccessType;
- lstrcpy(m_pszProxy, lpCDlgOpt->m_pszProxy);
- lstrcpy(m_pszUser, lpCDlgOpt->m_pszUser);
- lstrcpy(m_pszPwd, lpCDlgOpt->m_pszPwd);
- m_anonymous = lpCDlgOpt->m_anonymous;
-
- }
- //delete lpCDlgOpt;
- return 0;
- break;
- case IDM_FILEGET:
- htvitem = TreeView_GetSelection(GetDlgItem(hWnd,TREEVIEW_CONTROL_ID));
- if (!htvitem)
- return FALSE;
- ret = m_pcctrl->TreeViewItem(htvitem, &tvi, TVIF_TEXT | TVIF_PARAM, buf);
- lptvnodei = (LPTVNODEINFO)(tvi.lParam);
- lstrcpy(lptvnodei->pszFile,lptvnodei->pszDir);
- lstrcat(lptvnodei->pszFile,tvi.pszText);
- lstrcpy(lptvnodei->pszFileTarget, tvi.pszText);
-
- lptvnodei->requestType = REQUEST_FILETRANSFER;
- dwContext = (DWORD)lptvnodei;
- pnode = ((m_pcctrl->m_prootlvl[lptvnodei->index])->getfirstnode());
- wsprintf(msgbuf, TEXT("Downloading file from %s to %s ..."), lptvnodei->pszFile, lptvnodei->pszFileTarget);
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)msgbuf);
-
- ret = FtpGetFile(pnode->data.hConnect,
- lptvnodei->pszFile,lptvnodei->pszFileTarget , 1, FILE_ATTRIBUTE_ARCHIVE, FTP_TRANSFER_TYPE_ASCII, dwContext);
- dwError = GetLastError();
- break;
-
- case IDM_FILEOPEN:
- InetOpen();
- break;
-
- case IDM_FILEDELETE:
- htvitem = TreeView_GetSelection(GetDlgItem(hWnd,TREEVIEW_CONTROL_ID));
- if (!htvitem)
- return FALSE;
- ret = m_pcctrl->TreeViewItem(htvitem, &tvi, TVIF_TEXT | TVIF_PARAM | TVIF_HANDLE, buf);
-
- InetDelete(&tvi);
- break;
-
- case IDM_DELETEDIR:
- htvitem = TreeView_GetSelection(GetDlgItem(hWnd,TREEVIEW_CONTROL_ID));
- if (!htvitem)
- return FALSE;
- ret = m_pcctrl->TreeViewItem(htvitem, &tvi, TVIF_TEXT | TVIF_PARAM | TVIF_HANDLE, buf);
-
- InetDelete(&tvi);
- break;
-
- case IDM_CLEAR:
- //clear the editboxes
-
- //screen coordinates
- GetCursorPos(&pt);
- hWndEdit = WindowFromPoint(pt);
- SetWindowText(hWndEdit, TEXT(""));
- break;
-
- //triggered when the user selects ENTER from the keyboard
- case IDOK:
- if (GetFocus() == GetDlgItem(hWnd,IDC_URL))
- {
- OnGoto(0);
- break;
- }
- if (GetFocus() == GetDlgItem(hWnd,TREEVIEW_CONTROL_ID))
- {
- //if the item is expanded - collapse
- //if the item is collapsed - expand
- htvitem = TreeView_GetSelection(GetDlgItem(hWnd,TREEVIEW_CONTROL_ID));
- if (NULL == htvitem)
- return FALSE;
- ret = m_pcctrl->TreeViewItem(htvitem, &tvi, TVIF_STATE, buf);
- state1 = TVIS_SELECTED | TVIS_EXPANDEDONCE;
- state2 = TVIS_SELECTED | TVIS_EXPANDEDONCE | TVIS_EXPANDED ;
-
- tvi.mask = TVIF_SELECTEDIMAGE;
-
- if (state1 == tvi.state) {
- ret = TreeView_Expand(GetDlgItem(hWnd,TREEVIEW_CONTROL_ID), htvitem, TVE_EXPAND);
- tvi.iSelectedImage = 1; }
- else if (state2 == tvi.state) {
- ret= TreeView_Expand(GetDlgItem(hWnd,TREEVIEW_CONTROL_ID), htvitem, TVE_COLLAPSE);
- tvi.iSelectedImage = 2; }
-
- ret = TreeView_SetItem(GetDlgItem(hWnd,TREEVIEW_CONTROL_ID), &tvi);
-
- break;
- }
- break;
- default:
- return FALSE;
- }
- return TRUE;
- }
-
-
- //**********************************************************************
- // CDlg::DialogProc
- //
- // Purpose: Main dialog procedure
- //
- //********************************************************************
- BOOL CALLBACK CDlg::DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- HTREEITEM htvitem;
- LPTVNODEINFO lptvnodei;
- TV_ITEM tvi;
- NM_TREEVIEW *pTvnm;
- TV_KEYDOWN *ptvkd;
- HIMAGELIST himage;
- LPTSTR lpstrSite = NULL;
- DWORD dwError;
-
- HMENU hmenu;
- RECT rect;
- WORD wMenu;
- POINT pt, pt1;
- CDlg *lpCDlg = lpCApp->m_pCDlg;
- CCommonCtrl *lpCmctrl = lpCDlg->m_pcctrl;
-
- TCHAR buf[80];
- HWND hWndTV, hWndSB;
- BOOL ret, boolChangeImage;
- static HBITMAP hbitmap;
-
- switch (message)
- {
- case WM_INITDIALOG:
-
- //ensure that common controls DLL is loaded
- InitCommonControls();
-
- //create a new class to take care of common controls manipulation
- lpCDlg->m_pcctrl = new CCommonCtrl(lpCApp->m_hInst, hWnd);
-
- //initialize imagelist
- if (!lpCDlg->m_pcctrl->ImageListInit())
- break;
-
- //create a treeview and set its imagelist
- lpCDlg->m_pcctrl->m_hWndTV = CreateWindowEx(WS_EX_LEFT,WC_TREEVIEW,TEXT(""),
- TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | WS_VISIBLE | WS_CHILD,25,76,240,178,hWnd,
- (HMENU)TREEVIEW_CONTROL_ID, lpCApp->m_hInst, NULL);
- hWndTV = lpCDlg->m_pcctrl->m_hWndTV;
- himage = TreeView_SetImageList(hWndTV, lpCDlg->m_pcctrl->m_himg, TVSIL_NORMAL);
-
- //create a status bar
- hWndSB = CreateWindowEx(0L, STATUSCLASSNAME, TEXT(""), WS_CHILD |
- WS_BORDER | WS_VISIBLE , 150, 179, 600, 30, hWnd, (HMENU)STATUSBAR_CONTROL_ID,
- lpCApp->m_hInst, NULL);
-
- //initialize the animation control
- ret = Animate_Open(GetDlgItem(hWnd, IDC_ANIMATE1), MAKEINTRESOURCE(IDR_AVI1));
-
- dwError = GetLastError();
-
- //semaphore to use for FtpOpenFile/InternetReadFile - see comments
- //under CDlg::InetOpen for details
- lpCDlg->m_hFileDownload = CreateSemaphore(NULL, 1, 1, NULL);
-
- //bring up our logon dialog
- PostMessage(hWnd, WM_COMMAND, (WPARAM)(MAKELONG(IDC_OPTIONS,BN_CLICKED)), (LPARAM)(GetDlgItem(hWnd,IDC_OPTIONS)));
-
- //set focus to a particular control - so return FALSE
- SetFocus(GetDlgItem(hWnd, IDC_URL));
- return FALSE;
-
- case WM_SYSCOMMAND:
- switch (wParam)
- {
- case SC_CLOSE:
- DestroyWindow(hWnd);
- break;
- default:
- return FALSE;
- }
- break;
-
- //user define message passed back from Internet callback function
- //indicating we need to open and enumerate a directory
- case WM_INTERNETENUMERATE :
- lptvnodei = (LPTVNODEINFO)lParam;
- lpCDlg->InetEnumerate(lParam, lptvnodei->hTreeItem);
- break;
-
- //user define message passed back from Internet callback function
- //indicating we need to open a file for read
- case WM_INTERNETOPENFILE:
- lpCDlg->InetOpen();
- break;
-
- //user define message passed back from Internet callback function
- //indicating we need to make a new Internet connection
- case WM_INTERNETCONNECT:
- lptvnodei = (LPTVNODEINFO)lParam;
- lptvnodei->requestType = REQUEST_INTERNET_CONNECT;
- lpCDlg->OnGoto((DWORD)lParam);
- break;
-
- //user define message passed back from Internet callback function
- //indicating we need to make a new Internet connection and open
- //and enumerate a directory
- case WM_INTERNETENUMCONNECT:
- lptvnodei = (LPTVNODEINFO)lParam;
- ret = lpCDlg->m_pcctrl->TreeViewItem(lptvnodei->hTreeItem, &tvi,
- TVIF_CHILDREN, buf);
-
- //enumflag reserved for future use for optimization
- if (FILE_ATTRIBUTE_DIRECTORY == lptvnodei->dwAttributes &&
- !tvi.cChildren) //&& !lptvnodei->enumflag)
- {
- //set the enumflag and use TreeView_SetItem to replace the data
- lptvnodei->requestType = REQUEST_INTERNET_CONNECT;
- lpCDlg->OnGoto((DWORD)lParam);
- }
- break;
-
- //user define message passed back from Internet callback function
- //indicating a file has been opened and it needs to be read
- case WM_INTERNETFILEOPENED:
- lpCDlg->InetRead(lParam);
- break;
-
- //user define message passed back from Internet callback function
- //indicating a FtpFindFirstFile was successful - try findnext again
- case WM_INTERNETFILEFOUND:
- ret = lpCDlg->InetEnumNext(lParam);
- break;
-
- //user define message passed back from Internet callback function
- //indicating a file transfer completed
- //case WM_INTERNETFILETRANSFERRED:
- // SendMessage(GetDlgItem(lpCDlg->m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)"File Transfer Completed");
- // break;
-
- //track cursor for right mouse clicks to display our menu
- case WM_SETCURSOR:
- //wParam has hwnd of window
- if ((HWND)wParam == GetDlgItem(hWnd,IDC_EDIT1) ||
- (HWND)wParam == GetDlgItem(hWnd,IDC_STATUS))
- {
- switch (HIWORD(lParam))
- {
- case WM_RBUTTONDOWN:
- if (NULL != lpCDlg->m_hmnu)
- {
- DestroyMenu(lpCDlg->m_hmnu);
- lpCDlg->m_hmnu = NULL;
- }
- //screen coordinates
- GetCursorPos(&pt);
- lpCDlg->m_hmnu = LoadMenu(lpCDlg->m_hInst,MAKEINTRESOURCE(IDM_EDITBOX));
- hmenu = GetSubMenu( lpCDlg->m_hmnu, 0 );
-
- ret = TrackPopupMenu(hmenu, TPM_LEFTBUTTON | TPM_LEFTALIGN ,
- pt.x + 15, pt.y , 0, hWnd, NULL);
-
- break;
- }
- break;
- }
-
- break;
-
- //process the TreeView messages via WM_NOTIFY
- case WM_NOTIFY:
- pTvnm = (NM_TREEVIEW *)lParam;
- hWndTV = lpCDlg->m_pcctrl->m_hWndTV;
- switch (pTvnm->hdr.code)
- {
- case TVN_ITEMEXPANDING:
-
- //get treeview item specifics
- ret = lpCDlg->m_pcctrl->TreeViewItem(pTvnm->itemNew.hItem, &tvi,
- TVIF_TEXT | TVIF_PARAM, buf);
-
- boolChangeImage = FALSE;
- //if a directory or a root level node change the selected image
- if (!_tcsstr(tvi.pszText,TEXT("ftp://")))
- {
- lptvnodei = (LPTVNODEINFO)(tvi.lParam);
- if (FILE_ATTRIBUTE_DIRECTORY == lptvnodei->dwAttributes)
- boolChangeImage = TRUE;
- }
- else
- {
- boolChangeImage = TRUE;
- }
-
- //change selected image type
- if (boolChangeImage)
- {
- tvi.mask = TVIF_SELECTEDIMAGE;
- switch (pTvnm->action)
- {
- case TVE_EXPAND:
- tvi.iSelectedImage = 1;
- break;
- case TVE_COLLAPSE:
- tvi.iSelectedImage = 2;
- break;
- }
- ret = TreeView_SetItem(hWndTV, &tvi);
-
- }
- break;
-
- //process delete key
- case TVN_KEYDOWN:
- ptvkd = (TV_KEYDOWN *)lParam;
- switch (ptvkd->wVKey)
- {
- case VK_DELETE:
- htvitem = TreeView_GetSelection(GetDlgItem(hWnd,TREEVIEW_CONTROL_ID));
- if (!htvitem)
- return FALSE;
- ret = lpCDlg->m_pcctrl->TreeViewItem(htvitem, &tvi, TVIF_TEXT | TVIF_PARAM | TVIF_HANDLE, buf);
-
- return lpCDlg->InetDelete(&tvi);
-
- break;
-
- default:
- return FALSE;
- }
- break;
-
- //selection changed triggers the call to enumerate a directory
- //if it has not yet been enumerated
- case TVN_SELCHANGED:
-
- ret = lpCDlg->m_pcctrl->TreeViewItem(pTvnm->itemNew.hItem, &tvi,
- TVIF_TEXT | TVIF_CHILDREN | TVIF_PARAM | TVIF_HANDLE, buf);
-
- lptvnodei = (LPTVNODEINFO)(tvi.lParam);
- if (FILE_ATTRIBUTE_DIRECTORY == lptvnodei->dwAttributes)
- {
-
-
- lptvnodei->requestType = REQUEST_INTERNET_ENUM_CONNECT;
- lptvnodei->hTreeItem = tvi.hItem;
-
- //if the node represents a directory, it has no
- //child items, and it has not been enumerate,
- //enumerate the directory now
- if (FILE_ATTRIBUTE_DIRECTORY == lptvnodei->dwAttributes &&
- !tvi.cChildren)
- {
- lpCDlg->OnGoto((DWORD)lptvnodei);
-
- }
-
- }
-
- break;
- case NM_DBLCLK:
- //open a file
- lpCDlg->InetOpen();
- break;
-
- case NM_RCLICK:
- //screen coordinates
- GetCursorPos(&pt);
- //relative coordinates
- GetClientRect(hWndTV, &rect);
- pt1 = pt;
- ScreenToClient(hWndTV, &pt1);
-
- if (!PtInRect(&rect, pt1))
- return FALSE;
-
- htvitem = TreeView_GetSelection(hWndTV);
- if (!htvitem)
- return FALSE;
- ret = lpCDlg->m_pcctrl->TreeViewItem(htvitem, &tvi, TVIF_PARAM, buf);
- lptvnodei = (LPTVNODEINFO)(tvi.lParam);
-
- //is this a directory or a FTP site node
- if (FILE_ATTRIBUTE_DIRECTORY == lptvnodei->dwAttributes ||
- 0 == lptvnodei->dwAttributes)
- {
- wMenu = IDM_DIR;
- }
- else
- //this is a file node
- {
- wMenu = IDM_FILE;
- }
-
- if (NULL != lpCDlg->m_hmnu)
- {
- DestroyMenu(lpCDlg->m_hmnu);
- lpCDlg->m_hmnu = NULL;
- }
-
-
- lpCDlg->m_hmnu = LoadMenu(lpCDlg->m_hInst,MAKEINTRESOURCE(wMenu));
- hmenu = GetSubMenu( lpCDlg->m_hmnu, 0 );
-
- pt1.x = rect.right;
- pt1.y = rect.bottom;
- ClientToScreen(hWndTV, &pt1);
- ret = TrackPopupMenu(hmenu, TPM_LEFTBUTTON | TPM_LEFTALIGN ,
- pt.x + 15, pt.y , 0, hWnd, NULL);
-
- break;
-
- default:
- return FALSE;
- }
-
- break;
-
- case WM_NCLBUTTONDOWN:
- switch (wParam)
- {
- case HTCLOSE:
- DestroyWindow(hWnd);
- break;
- default:
- return FALSE;
- }
- break;
-
- case WM_COMMAND: // message: command from application menu
- return lpCDlg->OnCommand(hWnd, wParam, lParam);
-
- break;
-
- case WM_DESTROY: // message: window being destroyed
- if (NULL != lpCDlg->m_hmnu)
- DestroyMenu(lpCDlg->m_hmnu);
-
- //close InternetSession handle
- if (NULL != lpCDlg->m_hSession)
- InternetCloseHandle(lpCDlg->m_hSession);
- PostQuitMessage(0);
- return 0;
-
- default:
- return FALSE;
- }
- return TRUE;
- }
-
-
- //**********************************************************************
- // CDlg::InternetCallback
- //
- // Purpose: Internet callback function used during asynchronous calls
- // to Wininet
- // Parameters:
- // HINTERNET hInternet - Upon first entry into the callback (during
- // the INTERNET_STATUS_HANDLE_CREATED status this value contains the
- // handle passed in on the original call to the asynchronous Wininet
- // API. Upon INTERNET_STATUS_HANDLE_CREATED this value contains
- // the return value of the asynchronous Wininet API
- //
- // DWORD dwContext - an application defined value to associated with
- // the callback. For this application this is a pointer to a
- // TVNODEINFO structure
- //
- // DWORD dwInternetStatus - status value (INTERNET_STATUS_*)
- //
- // LPVOID lpvStatusInformation - value returned by callback function
- // specific to the STATUS type
- //
- // DWORD dwStatusInformationLength
- //********************************************************************
- void CALLBACK CDlg::InternetCallback (HINTERNET hInternet, DWORD dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
- {
- TCHAR strError[80], NewTitle[80], buf[80], szDisplay[STRRDMAX + 1];
- BOOL ret;
- DWORD dwExtErrorLen, dwExtError;
- LONG dwPreviousCount;
- LPTVNODEINFO ptvnodei;
- LPWIN32_FIND_DATA lpw32;
- CDlg *lpCDlg = lpCApp->m_pCDlg;
- HWND hWnd = lpCApp->m_pCDlg->m_hWnd;
- TCHAR szEditText[STRMAX];
- LPINTERNET_ASYNC_RESULT piar;
-
- switch(dwInternetStatus)
- {
- case INTERNET_STATUS_RESOLVING_NAME:
- lstrcpy(NewTitle, TEXT("Resolving "));
- wsprintf(buf, TEXT("Resolving %s ..."), (LPTSTR)lpvStatusInformation);
- SendMessage(GetDlgItem(hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)buf);
-
- break;
-
- case INTERNET_STATUS_NAME_RESOLVED:
- lstrcpy(NewTitle, TEXT("Resolved "));
- wsprintf(buf, TEXT("Resolved %s ..."), (LPTSTR)lpvStatusInformation);
- SendMessage(GetDlgItem(hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)buf);
-
- break;
-
- case INTERNET_STATUS_CONNECTING_TO_SERVER:
- lstrcpy(NewTitle, TEXT("Connecting to "));
- break;
-
- case INTERNET_STATUS_CONNECTED_TO_SERVER:
- wsprintf(buf, TEXT("Connected to %s ..."), (LPTSTR)lpvStatusInformation);
- SendMessage(GetDlgItem(hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)buf);
-
- lstrcpy(NewTitle, TEXT("Connected to "));
- break;
-
- case INTERNET_STATUS_SENDING_REQUEST:
- lstrcpy(NewTitle, TEXT("Sending request "));
- break;
-
- case INTERNET_STATUS_REQUEST_SENT:
- lstrcpy(NewTitle, TEXT("Sent Request "));
- break;
-
- case INTERNET_STATUS_RECEIVING_RESPONSE:
- lstrcpy(NewTitle, TEXT("Receiving response "));
- break;
-
- case INTERNET_STATUS_RESPONSE_RECEIVED:
- lstrcpy(NewTitle, TEXT("Received response "));
- break;
-
- case INTERNET_STATUS_CLOSING_CONNECTION:
- lstrcpy(NewTitle, TEXT("Closing connection "));
- break;
-
- case INTERNET_STATUS_CONNECTION_CLOSED:
- lstrcpy(NewTitle, TEXT("Closed connection "));
-
- break;
-
- case INTERNET_STATUS_HANDLE_CREATED:
- lstrcpy(NewTitle, TEXT("Handle created "));
- ret = Animate_Stop(GetDlgItem(hWnd, IDC_ANIMATE1));
-
- //set global flags used for cancelling intermediate requests
- lpCDlg->m_hLastHandleCreated = (HINTERNET)lpvStatusInformation;
-
-
- ptvnodei = (LPTVNODEINFO)dwContext;
- wsprintf(buf, TEXT("Handle Created %d, %d"), (long)hInternet, (long)lpvStatusInformation);
- #ifdef _DEBUG
- ::OutputDebugString(buf);
- ::OutputDebugString(TEXT("\n"));
- #endif
- lstrcat(buf, TEXT("\r\n"));
- GetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText, STRMAX );
- lstrcat(szEditText, buf);
- SetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText);
-
- break;
-
- case INTERNET_STATUS_HANDLE_CLOSING:
- lstrcpy(NewTitle, TEXT("Handle Closing "));
- lpCDlg->m_hLastHandleCreated = NULL;
- break;
-
- case INTERNET_STATUS_REQUEST_COMPLETE:
- //stop the animation
- ret = Animate_Stop(GetDlgItem(hWnd, IDC_ANIMATE1));
-
- //get the TVNODEINFO structure originally passed in from
- //an API call
- ptvnodei = (LPTVNODEINFO)dwContext;
-
- //check the INTERNET_ASYNC_RESULT structure for error information
- piar = (LPINTERNET_ASYNC_RESULT)(lpvStatusInformation);
-
- //set global flag used for cancelling intermediate requests
- //back to NULL since the request is finished anyway
- lpCDlg->m_hLastHandleCreated = NULL;
- if (!(piar->dwResult))
- {
- wsprintf(NewTitle, TEXT("Internet Request Completed - Status Code: %d"), piar->dwError);
- //SendMessage(GetDlgItem(hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)buf);
-
- lstrcat(NewTitle, TEXT("\r\n"));
- #ifdef _DEBUG
- ::OutputDebugString(NewTitle);
- #endif
- GetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText, STRMAX );
- lstrcat(szEditText, NewTitle);
- SetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText);
-
- switch (piar->dwError)
- {
- //standard error returned from FtpFindFirstFile
- //and InternetFindNextFile
- case ERROR_NO_MORE_FILES:
- //set the request value which will be processed
- //next time in the callback function
- ptvnodei->requestType = REQUEST_FILENOTFOUND;
-
- //get handle values we used and close them
- lpw32 = (LPWIN32_FIND_DATA)(ptvnodei->prequestResult);
-
- ret = InternetCloseHandle(ptvnodei->hFile);
- ret = InternetCloseHandle(ptvnodei->hEnum);
- #ifdef _DEBUG
- wsprintf(buf, TEXT("InternetCloseHandle: %d, %d \n"), ptvnodei->hFile,ptvnodei->hEnum);
- ::OutputDebugString(buf);
- #endif
- //SendMessage(GetDlgItem(hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)"Ready");
-
- break;
-
- case ERROR_INTERNET_EXTENDED_ERROR:
- //error triggered when the server can pass back
- //more information on what went wrong.
- dwExtErrorLen = 80;
- ret = InternetGetLastResponseInfo(&dwExtError, strError, &dwExtErrorLen);
- lstrcat(strError, TEXT("\r\n"));
- #ifdef _DEBUG
- ::OutputDebugString(strError);
- #endif
- GetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText, STRMAX );
- lstrcat(szEditText, strError);
-
- SetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText);
-
- break;
- case 1:
- //no problem - this would be a boolean return value
- //representing success
- break;
- default:
- //this would be a good place to check for all the
- //INTERNET_ERROR_* messages (for example,
- //INTERNET_ERROR_NAME_NOT_RESOLVED) to give more
- //descriptive output to the user
-
- return;
- }
- }
- else
- {
- wsprintf(NewTitle, TEXT("Internet Request Completed - Status Code: %d\n"), piar->dwResult);
- #ifdef _DEBUG
- ::OutputDebugString(NewTitle);
- #endif
- lstrcat(NewTitle, TEXT("\r\n"));
- GetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText, STRMAX );
- lstrcat(szEditText, NewTitle);
- SetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText);
- }
- switch (ptvnodei->requestType)
- {
- case REQUEST_NEW_INTERNET_SITE:
- //we'll use this handle to enumerate directories
- ptvnodei->hEnum = hInternet;
- //now make another connection to cache to use for other operations (FtpDeleteFile, etc)
- //operations which won't require a new handle everytime
- ::PostMessage(lpCDlg->m_hWnd,WM_INTERNETCONNECT, NULL, (LPARAM)(dwContext)) ;
- break;
-
- case REQUEST_INTERNET_CONNECT:
- //cache this handle for general use (FtpOpenFile, InternetReadFile, etc.)
- ptvnodei->hConnect = hInternet;
- ::PostMessage(lpCDlg->m_hWnd,WM_INTERNETENUMERATE, NULL, (LPARAM)(dwContext)) ;
- break;
-
- case REQUEST_INTERNET_ENUM_CONNECT:
- ptvnodei->hEnum = hInternet;
- ::PostMessage(lpCDlg->m_hWnd,WM_INTERNETENUMERATE, NULL, (LPARAM)(dwContext)) ;
- break;
-
- case REQUEST_INTERNET_OPEN_CONNECT:
- ptvnodei->hConnect = hInternet;
- ::PostMessage(lpCDlg->m_hWnd,WM_INTERNETOPENFILE, NULL, (LPARAM)(dwContext)) ;
- break;
-
- case REQUEST_INTERNET_OPENFILE:
- ptvnodei->hFile = hInternet;
- dwContext = (DWORD)ptvnodei;
- ::PostMessage(lpCDlg->m_hWnd,WM_INTERNETFILEOPENED, NULL, (LPARAM)(dwContext)) ;
- break;
-
- case REQUEST_FIRSTFILEFOUND:
- ptvnodei->hFile = hInternet;
- ::PostMessage(lpCDlg->m_hWnd,WM_INTERNETFILEFOUND, NULL, (LPARAM)(dwContext)) ;
- break;
-
- case REQUEST_FILETRANSFER:
- SendMessage(GetDlgItem(lpCDlg->m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)"File Transfer Completed");
- //::PostMessage(lpCDlg->m_hWnd,WM_INTERNETFILETRANSFERRED, NULL, (LPARAM)(dwContext)) ;
- break;
-
- case REQUEST_INTERNETFILEREAD:
- ret = ReleaseSemaphore(lpCDlg->m_hFileDownload, 1, &dwPreviousCount);
- InternetCloseHandle(ptvnodei->hFile);
- ptvnodei->hFile = NULL;
- lstrcpyn(szDisplay, lpCDlg->m_filebuf, (int)(lpCDlg->m_dwBytesRead + 1));
- SetWindowText(GetDlgItem(hWnd, IDC_EDIT1), szDisplay);
- SendMessage(GetDlgItem(hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)"File Transfer Complete");
-
- break;
- case REQUEST_INTERNETDELETEFILE:
- SendMessage(GetDlgItem(lpCDlg->m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)"File Deleted");
- break;
-
- }
- break;
- }
- if (INTERNET_STATUS_REQUEST_COMPLETE != dwInternetStatus)
- {
- if (dwStatusInformationLength && lpvStatusInformation &&
- 1 != lstrlen((LPTSTR)lpvStatusInformation)) {
- if (INTERNET_STATUS_HANDLE_CREATED == dwInternetStatus)
- {
- wsprintf(buf, TEXT(" %d, %d"), (long)hInternet, (long)lpvStatusInformation);
- lstrcat(NewTitle, buf);
- }
- else {
- lstrcat(NewTitle, (LPTSTR) lpvStatusInformation); }
- //SendMessage(GetDlgItem(hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)NewTitle);
-
- }
-
- lstrcat(NewTitle, TEXT("\r\n"));
- #ifdef _DEBUG
- ::OutputDebugString(NewTitle);
- #endif
- GetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText, STRMAX);
- lstrcat(szEditText, NewTitle);
-
- SetWindowText(GetDlgItem(hWnd, IDC_STATUS), szEditText);
-
- }
- return;
- }
-
-
- //**********************************************************************
- // CDlg::AddItem
- //
- // Purpose: Add new TreeView item
- // Parameters:
- // LPARAM lParam - context value representing a TVNODEINFO structure
- // Return Value:
- // BOOL - TRUE if initialization succeeded, FALSE otherwise.
- //
- //********************************************************************
-
- BOOL CDlg::AddItem(LPARAM lParam)
- {
- LPTVNODEINFO lptvnodei;
- LPWIN32_FIND_DATA lptvFindData;
- HTREEITEM htvi, hItem;
- TVDATA tvdta;
- TVNODEINFO tvnodei;
- TV_ITEM tvi;
- DWORD dwContext;
- TCHAR buf[80], dir[80];
- LPTSTR pdir = dir;
- BOOL ret;
-
- lptvnodei = (LPTVNODEINFO)lParam;
- htvi = lptvnodei->hTreeItem;
-
- //set pointer to WIN32_FIND_DATA structure - this will be
- //passed (and returned) via dwContext parameter of FtpFindFirstFile
- //and InternetFindNextFile functions
- lptvFindData = (LPWIN32_FIND_DATA)(lptvnodei->prequestResult);
-
- //for directories image selected and nonselected will initially
- //be a folder
- if (FILE_ATTRIBUTE_DIRECTORY == lptvFindData->dwFileAttributes) {
- SETTVDATA(tvdta, 2, 2, htvi); }
- else {
- //file image
- SETTVDATA(tvdta, 0, 0, htvi); }
-
- lstrcpy(dir, lptvnodei->pszDir);
- //parent directory
- if (lstrcmp(pdir,TEXT("")))
- {
- //parent text
- ret = m_pcctrl->TreeViewItem(htvi, &tvi,
- TVIF_TEXT , buf);
- lstrcat(dir, buf);
- lstrcat(dir, TEXT("/"));
- }
- else
- lstrcat(dir, TEXT("/"));
-
- //initialize TVNODEINFO structure and add a treeview item where
- //the structure will be stored in the TV_ITEM.lParam
- SETTVNODEINFO(tvnodei, lptvFindData->dwFileAttributes, FALSE, lptvnodei->hConnect,
- lptvnodei->pszSite, 0,0, NULL, dir);
- hItem = m_pcctrl->TreeViewAdd(lptvFindData->cFileName, &tvdta, &tvnodei, &dwContext);//tvnodei);
-
- return TRUE;
- }
-
- //**********************************************************************
- // CDlg::ParseServer
- //
- // Purpose: Parse user input into server name and protocol (like ftp://)
- // Parameters:
- // LPTSTR pszInput - IN value, user defined URL
- // LPTSTR pszURL - OUT value, full URL
- // LPTSTR pszServer - OUT value, server name
- // Return Value: None
- //
- //********************************************************************
- void CDlg::ParseServer(LPTSTR pszInput, LPTSTR pszURL, LPTSTR pszServer)
- {
- LPTSTR pstr;
- pstr = NULL;
- pstr = _tcsstr(pszInput, TEXT("ftp://"));
- if (NULL == pstr)
- {
- lstrcpy(pszURL, TEXT("ftp://"));
- lstrcat(pszURL, pszInput);
- lstrcpy(pszServer, pszInput);
- }
- else
- {
- lstrcpy(pszURL, pszInput);
- lstrcpy(pszServer, pstr + 6);
- }
- return;
-
- }
-
- //**********************************************************************
- // CDlg::InetEnumerate
- //
- // Purpose: Wrapper function for the FtpFindFirstFile API
- // Parameters:
- // LPARAM lParam - context value representing a TVNODEINFO structure
- // HTREEITEM htitem - handle of TreeView item representing the
- // directory to enumerate
- // Return Value: None
- //
- //********************************************************************
- void CDlg::InetEnumerate(LPARAM lParam, HTREEITEM htitem)
- {
- LPTVNODEINFO lptvnodei;
- TV_ITEM tvi;
- HINTERNET hFtp;
- CDlg *lpCDlg = lpCApp->m_pCDlg;
- CCommonCtrl *lpCmctrl = lpCDlg->m_pcctrl;
- DWORD dwContext, dwError;
- TCHAR buf[80], strSearch[80], msgbuf[80];
- BOOL ret;
- LPWIN32_FIND_DATA lpwf;
-
- //show the user we are busy
- ret = Animate_Play(GetDlgItem(m_hWnd, IDC_ANIMATE1), 0, -1, -1);
-
- //get a pointer to the TVNODEINFO structure which was originally passed
- //to the Internet callback function via the dwContext parameter and
- //which was returned to the dialog procedure via the lParam parameter
- //of the PostMessage API
- lptvnodei = (LPTVNODEINFO)lParam;
-
- //get text associated with treeview item
- m_pcctrl->TreeViewItem(htitem, &tvi, TVIF_TEXT, buf );
- //if this is a root level ftp site node use "/*" as the search criteria
- if (_tcsstr(tvi.pszText, TEXT("ftp://")))
- lstrcpy(strSearch , TEXT("/*"));
- else
- {
- //this is a directory node - use a fullpath/* as the search criteria
- lstrcpy(strSearch, lptvnodei->pszDir);
- lstrcat(strSearch, tvi.pszText);
- lstrcat(strSearch, TEXT("/*"));
- }
-
- //application defined REQUEST type to send to Internet callback
- lptvnodei->requestType = REQUEST_FIRSTFILEFOUND;
-
- lpwf = &(lpCDlg->m_pcctrl->m_FindData);
- lptvnodei->prequestResult = (LPVOID)lpwf;
- lstrcpy(lptvnodei->pszFile, strSearch);
-
- //package up our TVNODEINFO structure into a context value (dwContext) to
- //pass to the Internet callback function via an asynchronous call.
- //This packet of data will be our method of passing and receiving
- //data with the callback function.
- dwContext = (DWORD)(lptvnodei);
-
- //set a pointer to the WIN32_FIND_DATA structure we want returned
- LPVOID pvx = ((LPTVNODEINFO)lptvnodei)->prequestResult;
-
- #ifdef _DEBUG
- {
- TCHAR szDebugbuf[80];
- wsprintf(szDebugbuf, TEXT("Before FtpFindFirst %d\n"), (long)(lptvnodei->hEnum));
- ::OutputDebugString(szDebugbuf);
- ::OutputDebugString(strSearch);
- }
- #endif
-
- //set the text in the status bar
- wsprintf(msgbuf, TEXT("Opening folder ... %s"), tvi.pszText);
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)msgbuf);
-
- //Use the hEnum to enumerate - this value was set when the user
- //first connected to the site. Note the lptvnodei->pszFile value passed
- //into the API. This parameter will have a lifetime longer than the
- //API's execution. Currently, arguments passed into an asynchronous
- //call must be managed by the user and ensured to not go out of scope
- //before the function terminates
- hFtp = ::FtpFindFirstFile(lptvnodei->hEnum, lptvnodei->pszFile, (LPWIN32_FIND_DATA)pvx, 0, dwContext);
-
- dwError = GetLastError();
- //the handle return value will be set in either the callback function
- //or here (or both)
- lptvnodei->hFile = hFtp;
-
- #ifdef _DEBUG
- {
- TCHAR szDebugbuf[80];
- wsprintf(szDebugbuf, TEXT("After FtpFindFirst %d, %d\n"), (long)hFtp, dwError);
- ::OutputDebugString(szDebugbuf);
- }
- #endif
-
-
- //just in case the function call will not return a
- //GetLastError() value of ERROR_IO_PENDING and will return the
- //function handle immediately continue and explicitly
- //call the InternetFindNextFile API which is wrapped by
- //CDlg::InetEnumNext
- dwContext = (LPARAM)lptvnodei;
- if (ERROR_IO_PENDING != dwError && NULL != hFtp)
- {
- //also to late to cancel an intermediate request - reset
- m_hLastHandleCreated = NULL;
- ret = InetEnumNext(dwContext);
- }
-
- }
-
-
- //**********************************************************************
- // CDlg::InetEnumNext
- // Purpose: Wrapper function for the InternetFindNextFile API
- // Parameters:
- // LPARAM lParam - context value representing a TVNODEINFO structure
- // Return Value:
- // BOOL - TRUE if initialization succeeded, FALSE otherwise.
- //
- //********************************************************************
-
- BOOL CDlg::InetEnumNext(LPARAM lParam)
- {
- LPTVNODEINFO lptvnodei;
- BOOL ret;
- DWORD dwError;
-
- //show the user we are busy
- ret = Animate_Play(GetDlgItem(m_hWnd, IDC_ANIMATE1), 0, -1, -1);
-
- //get a pointer to the TVNODEINFO structure which was originally passed
- //to the Internet callback function via the dwContext parameter and
- //which was returned to the dialog procedure via the lParam parameter
- //of the PostMessage API
- lptvnodei = (LPTVNODEINFO)lParam;
-
- //if the status has been returned from the callback function
- //that no more files were found leave the function
- if (REQUEST_FILENOTFOUND == lptvnodei->requestType)
- return FALSE;
-
- //FtpFindFirstFile/InternetFindNextFile were successful, create a new
- //treeview node representing the file or directory
- ret = AddItem(lParam);
-
- //set a pointer to a WIN32_FIND_DATA structure which will
- //be populated by a call to InternetFindNextFile
- lptvnodei->prequestResult = &(m_pcctrl->m_FindData);
- #ifdef _DEBUG
- {
- TCHAR buf[80];
- wsprintf(buf, TEXT("Before InternetFindNext %d\n"), (long)(lptvnodei->hFile));
- ::OutputDebugString(buf);
- }
- #endif
-
- //find next file - note that the WIN32_FIND_DATA structure will
- //not go out of scope even if we leave this function
- ret = ::InternetFindNextFile(lptvnodei->hFile, lptvnodei->prequestResult);
-
- dwError = GetLastError(); //for debug - should return an ERROR_IO_PENDING
- return TRUE;
- }
-
-
- //**********************************************************************
- // CDlg::InetOpen
- // Purpose: Wrapper function for the FtpOpenFile API
- // Parameters: None
- // Return Value: None
- //
- //********************************************************************
-
- void CDlg::InetOpen()
- {
- LPTVNODEINFO lptvnodei;
- DWORD dwContext, dwError, dwWait;
- TV_ITEM tvi;
- HINTERNET hFile;
- TCHAR buf[80], msgbuf[80];
- BOOL ret;
- HWND hWndTV = m_pcctrl->m_hWndTV;
- node *pnode;
- HTREEITEM hItem;
- hItem = TreeView_GetSelection(hWndTV);
- if (NULL == hItem)
- return;
-
- //get lParam from TreeView item structure (TV_ITEM) it holds
- //a pointer to our TVNODEINFO structure returned from the callback
- //function
- ret = m_pcctrl->TreeViewItem(hItem, &tvi, TVIF_TEXT | TVIF_PARAM, buf);
- lptvnodei = (LPTVNODEINFO)(tvi.lParam);
-
- //make sure this is a file not a directory
- if (FILE_ATTRIBUTE_DIRECTORY == lptvnodei->dwAttributes ||
- 0 == lptvnodei->dwAttributes)
- return;
-
- //show the user we are busy
- ret = Animate_Play(GetDlgItem(m_hWnd, IDC_ANIMATE1), 0, -1, -1);
-
- //build full path names
- lstrcpy(lptvnodei->pszFile, lptvnodei->pszDir);
- lstrcat(lptvnodei->pszFile, tvi.pszText);
-
- //set REQUEST type to pass to callback function
- lptvnodei->requestType = REQUEST_INTERNET_OPENFILE;
- dwContext = (DWORD)lptvnodei;
-
- //since we are going to use the cached connection it is held
- //in the node representing the root level site. get the root
- //level node
- pnode = ((m_pcctrl->m_prootlvl[lptvnodei->index])->getfirstnode());
-
- #ifdef _DEBUG
- wsprintf(msgbuf, TEXT("Before FtpOpenFile %d\n"), pnode->data.hConnect);
- ::OutputDebugString(msgbuf);
- wsprintf(msgbuf, TEXT("Full Path: %s\n"), lptvnodei->pszFile);
- ::OutputDebugString(msgbuf);
- #endif
- if (!(pnode->data.hConnect))
- return;
-
- //Why use Semaphore and WaitForSingleObject?
- // 1) we only have one editbox for viewing files so this is a
- // resource limitation
- // 2) InternetReadFile passes streams back sequentially anyway
- // so going asynchronous will not buy us anything
-
- //Wait for file handle resource to be available
- dwWait = WaitForSingleObject(m_hFileDownload, INFINITE);
-
- wsprintf(msgbuf, TEXT("Opening file %s ..."), lptvnodei->pszFile);
-
- //set status bar text
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)msgbuf);
-
- //open file for read in ascii mode
- hFile = FtpOpenFile(pnode->data.hConnect, lptvnodei->pszFile, GENERIC_READ,FTP_TRANSFER_TYPE_ASCII,dwContext);
-
- dwError = GetLastError();
- #ifdef _DEBUG
- wsprintf(msgbuf, TEXT("After FtpOpenFile %d, %d\n"), hFile, dwError);
- ::OutputDebugString(msgbuf);
- #endif
-
- //just in case the function call will not return a
- //GetLastError() value of ERROR_IO_PENDING and will return the
- //function handle immediately continue and PostMessage
- //to explicitly call the InternetReadFile API which is wrapped
- //by CDlg::InetRead()
-
- if (ERROR_IO_PENDING != dwError && NULL != hFile)
- {
- //also to late to cancel an intermediate request - reset
- m_hLastHandleCreated = NULL;
-
- lptvnodei->hFile = hFile;
- InetRead((LPARAM)lptvnodei);
-
- }
-
- }
-
- //**********************************************************************
- // CDlg::InetRead
- // Purpose: Wrapper function for the InternetReadFile API
- // Parameters:
- // LPARAM lParam - context value representing a TVNODEINFO structure
- // Return Value: None
- //
- //********************************************************************
- void CDlg::InetRead(LPARAM lParam)
- {
- DWORD dwError, dwBytesToRead;
- LPTVNODEINFO lptvnodei;
- BOOL ret;
- TCHAR buf[80], szDisplay[STRRDMAX + 1];
-
- //show the user we are busy
- ret = Animate_Play(GetDlgItem(m_hWnd, IDC_ANIMATE1), 0, -1, -1);
-
- //get our TVNODEINFO structure returned from the callback function
- lptvnodei = (LPTVNODEINFO)lParam;
-
- //set the request type to pass to the callback function
- lptvnodei->requestType = REQUEST_INTERNETFILEREAD;
-
-
- dwBytesToRead = STRRDMAX;
- wsprintf(buf, TEXT("Before InternetReadFile %d\n"), lptvnodei->hFile);
- wsprintf(buf, TEXT("Reading file %s ..."), lptvnodei->pszFile);
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)buf);
-
- //Readfile - use global variables to return buffer and number of
- //bytes read. Since we are not allowing asynchronous reads using
- //a semaphore there is no danger of overwriting
- ret = InternetReadFile(lptvnodei->hFile, m_filebuf,dwBytesToRead, &m_dwBytesRead );
- dwError = GetLastError();
-
- #ifdef _DEBUG
- wsprintf(buf, TEXT("After InternetRead %d, %d, %d\n"), dwError, ret, m_dwBytesRead);
- ::OutputDebugString(buf);
- #endif
-
- if (ERROR_IO_PENDING != dwError)
- {
- //update the user interface
- lstrcpyn(szDisplay, m_filebuf, (int)(m_dwBytesRead + 1));
-
- SetWindowText(GetDlgItem(m_hWnd, IDC_EDIT1), m_filebuf);
- Animate_Stop(GetDlgItem(m_hWnd, IDC_ANIMATE1));
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)"File Transfer Complete");
- if (NULL != lptvnodei->hFile)
- InternetCloseHandle(lptvnodei->hFile);
-
- }
-
- }
-
- //**********************************************************************
- // CDlg::InetDelete
- // Purpose: Process delete requests
- // Parameters: None
- // BOOL - TRUE if initialization succeeded, FALSE otherwise.
- //
- //********************************************************************
- BOOL CDlg::InetDelete(TV_ITEM *ptvi)
- {
- HWND hWndTV = m_pcctrl->m_hWndTV;
- node *pnode;
- HTREEITEM htvitem, hParent;
- TCHAR buf[80], msgbuf[80];
- BOOL ret;
- LPTVNODEINFO lptvnodei;
- int i;
- Clist *pClist;
- DWORD dwError;
- lptvnodei = (LPTVNODEINFO)(ptvi->lParam);
-
- htvitem = TreeView_GetSelection(hWndTV);
- if (NULL == htvitem)
- return FALSE;
- hParent = TreeView_GetParent(hWndTV, htvitem);
-
- //if the node has a parent use FtpDeleteFile or
- if (hParent)
- {
- if (FILE_ATTRIBUTE_DIRECTORY == lptvnodei->dwAttributes) {
-
- //use our cached connection
- //for our case allow file deletes only
- lstrcpy(msgbuf, TEXT("Confirm delete for: "));
- MessageBox(NULL, msgbuf,TEXT("Directory Delete not allowed"), MB_OK | MB_ICONSTOP);
- return TRUE;
- //ret = FtpRemoveDirectory(pnode->data.hConnect, lptvnodei->pszFile)
- }
- else {
- //confirm delete before proceeding
- lstrcpy(msgbuf, TEXT("Confirm delete for: "));
- lstrcat(msgbuf, ptvi->pszText);
- if (IDNO == MessageBox(NULL, msgbuf,TEXT("Confirm Delete"), MB_YESNO))
- return TRUE;
-
- //get the node where our cached connection is stored
- pnode = ((m_pcctrl->m_prootlvl[lptvnodei->index])->getfirstnode());
- //generate full path rather than using relative and worry
- //about changing current directory
- lstrcpy(lptvnodei->pszFile, lptvnodei->pszDir);
- lstrcat(lptvnodei->pszFile, ptvi->pszText);
- //remove data from linked list
- (m_pcctrl->m_prootlvl[lptvnodei->index])->remove(lptvnodei->key);
- //remove TreeView node
- TreeView_DeleteItem(hWndTV, htvitem);
- //set request type
- lptvnodei->requestType = REQUEST_INTERNETDELETEFILE;
- //use our cached connection
- wsprintf(buf, TEXT("Deleting file %s ..."), lptvnodei->pszFile);
- SendMessage(GetDlgItem(m_hWnd,STATUSBAR_CONTROL_ID), SB_SETTEXT, 0, (LPARAM)buf);
-
- ret = FtpDeleteFile(pnode->data.hConnect, lptvnodei->pszFile);
- dwError = GetLastError();
-
- //the user probably doesn't have access to delete a file
- //- this will return an error message "access denied"
- if (ERROR_INTERNET_EXTENDED_ERROR == dwError)
- {
- TCHAR buf[80], szEditText[STRMAX];
- DWORD dwbuf = 80;
- InternetGetLastResponseInfo(&dwError, buf, &dwbuf);
- GetWindowText(GetDlgItem(m_hWnd, IDC_STATUS), szEditText, STRMAX );
- lstrcat(szEditText, buf);
- SetWindowText(GetDlgItem(m_hWnd, IDC_STATUS), szEditText);
-
- }
- #ifdef _DEBUG
- wsprintf(msgbuf, TEXT("Delete File %s with handle %d - return code: %d\n"), lptvnodei->pszFile, pnode->data.hConnect, dwError);
- ::OutputDebugString(msgbuf);
- #endif
- return TRUE;
- }
-
-
- }
- else
- {
- //root level node
- //confirm delete before proceeding
- lstrcpy(msgbuf, TEXT("Confirm delete for: "));
- lstrcat(msgbuf, ptvi->pszText);
- if (IDNO == MessageBox(NULL, msgbuf,TEXT("Confirm Delete"), MB_YESNO))
- return FALSE;
-
- // delete branch and close connection
- pClist = (Clist *)(ptvi->lParam);
- i = lptvnodei->index;
- //close cached connection
- InternetCloseHandle(lptvnodei->hConnect);
- m_pcctrl->m_prootlvl[lptvnodei->index]->remove();
- ret = TreeView_DeleteItem(hWndTV, ptvi->hItem);
- delete m_pcctrl->m_prootlvl[i];
- m_pcctrl->m_prootlvl[i] = NULL;
-
-
- }
- return TRUE;
- }
-
-
- /* AboutDlgProc
- * ------------
- *
- * Dialog procedure for the About function
- */
- BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
- {
-
- switch(Message) {
-
- case WM_INITDIALOG:
- return TRUE;
- break;
-
- case WM_NCLBUTTONDOWN:
- switch (wParam)
- {
- case HTCLOSE:
- EndDialog(hDlg,0);
- default:
- return FALSE;
- }
- break;
-
-
- case WM_COMMAND :
- switch(wParam) {
- case IDOK:
- EndDialog(hDlg,0);
- break;
- }
- break;
-
- default :
- return FALSE;
-
- }
- return TRUE;
- }
-