myftpDlg.cpp
资源名称:myftp.zip [点击查看]
上传用户:kkaqas
上传日期:2007-01-07
资源大小:38k
文件大小:21k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // myftpDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "myftp.h"
- #include "myftpDlg.h"
- #include "..\share\tcp.h"
- #include "..\share\ftp.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <io.h>
- #include <direct.h>
- #include <errno.h>
- #include "extern.h"
- #include "SetupDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern char *GetFileName(char *);
- int reconnect(char *host, char *user, char *pass, char *remote_dir);
- #define SETSTATUS(msg) SetDlgItemText(IDC_STATUS, msg)
- char *del_lr_spaces(char *pbuf); /* 删除左右的空格 */
- BOOL g_stop =FALSE;
- HWND g_hMainDlg =NULL;
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyftpDlg dialog
- CMyftpDlg::CMyftpDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMyftpDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMyftpDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CMyftpDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMyftpDlg)
- DDX_Control(pDX, IDL_LOCAL, m_listLocal);
- DDX_Control(pDX, IDL_REMOTE, m_listRemote);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMyftpDlg, CDialog)
- //{{AFX_MSG_MAP(CMyftpDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_CLOSE()
- ON_NOTIFY(NM_DBLCLK, IDL_LOCAL, OnDblClickLocal)
- ON_NOTIFY(NM_DBLCLK, IDL_REMOTE, OnDblClickRemote)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyftpDlg message handlers
- BOOL CMyftpDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- g_hMainDlg =m_hWnd;
- GetCurrentDirectory(sizeof(g_cur_path), g_cur_path);
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- if(tcp_init()<0)
- {
- MessageBox("tcp_init failed");
- EndDialog(0);
- return FALSE;
- }
- SetDlgItemText(IDE_LOCAL_DIR, "d:\myweb\downloads\sourcecode");
- SetDlgItemText(IDE_REMOTE_DIR, "/public_html/downloads/sourcecode");
- SetDlgItemText(IDE_HOST, "www.nease.net");
- SetDlgItemText(IDE_USER, "inetsoft");
- m_listLocal.InsertColumn(0, "名称", LVCFMT_LEFT, 80);
- m_listLocal.InsertColumn(1, "大小", LVCFMT_RIGHT, 80);
- m_listRemote.InsertColumn(0, "名称", LVCFMT_LEFT, 80);
- m_listRemote.InsertColumn(1, "大小", LVCFMT_RIGHT, 80);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CMyftpDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CMyftpDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CMyftpDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- int g_sd_ftp =-1;
- char g_host[60], g_user[60], g_pass[20], g_local_dir[128], g_remote_dir[128],
- g_cur_remote_dir[128], g_cur_local_dir[128];
- BOOL CMyftpDlg::OnCommand(WPARAM wParam, LPARAM lParam)
- {
- struct _finddata_t fileinfo;
- char filesize[50], *pfilename, filename[128], rfilesize[50], temp[256];
- long hfind =0;
- int i, j, count, from_login =0, item_num;
- switch(wParam)
- {
- case ID_LOGIN:
- g_stop =FALSE;
- from_login =1;
- if(GetDlgItemText(IDE_HOST, g_host, sizeof(g_host)) ==0)
- return FALSE;
- if(GetDlgItemText(IDE_USER, g_user, sizeof(g_user)) ==0)
- return FALSE;
- if(GetDlgItemText(IDE_PASS, g_pass, sizeof(g_pass)) ==0)
- return FALSE;
- if(g_sd_ftp >0) tcp_close(g_sd_ftp);
- ::EnableWindow(::GetDlgItem(m_hWnd, ID_LOGIN), FALSE);
- SetDlgItemText(IDC_STATUS, "login host...");
- if((g_sd_ftp =ftp_login(g_host, g_user, g_pass)) <0)
- {
- SetDlgItemText(IDC_STATUS, "ftp login failed");
- ::EnableWindow(::GetDlgItem(m_hWnd, ID_LOGIN), TRUE);
- return FALSE;
- }
- SetDlgItemText(IDE_PASS, NULL);
- SetDlgItemText(IDC_STATUS, "ftp login success");
- ::EnableWindow(::GetDlgItem(m_hWnd, ID_LOGIN), TRUE);
- case ID_CHDIR:
- g_stop =FALSE;
- m_listLocal.DeleteAllItems();
- GetDlgItemText(IDE_LOCAL_DIR, g_local_dir, sizeof(g_local_dir));
- if(chdir(g_local_dir) !=0)
- {
- SetDlgItemText(IDC_STATUS, "cd local dir failed");
- return FALSE;
- }
- strcpy(g_cur_local_dir, g_local_dir);
- memset(&fileinfo, 0, sizeof(fileinfo));
- if((hfind =_findfirst("*", &fileinfo)) <0)
- {
- if(errno ==EINVAL) return FALSE;
- return 0;
- }
- while(1)
- {
- if((pfilename =GetFileName(fileinfo.name)) ==NULL)
- goto next;
- if(!strcmp(pfilename, "_vti_cnf")
- || !strcmp(pfilename, "listtmp.txt"))
- goto next;
- if((fileinfo.attrib & _A_HIDDEN) ==_A_HIDDEN)
- goto next;
- if((fileinfo.attrib & _A_SUBDIR) == _A_SUBDIR)
- {
- strcpy(filesize, "-");
- wsprintf(filename, ">%s",pfilename);
- }
- else
- {
- wsprintf(filesize, "%d", fileinfo.size);
- strcpy(filename, pfilename);
- }
- item_num =m_listLocal.InsertItem(m_listLocal.GetItemCount(), filename);
- m_listLocal.SetItemText(item_num, 1, filesize);
- next:
- if(_findnext(hfind, &fileinfo) <0) break;
- if(fileinfo.name[0] ==0) break;
- }
- _findclose(hfind);
- count =m_listLocal.GetItemCount();
- for(i =0; i<count; i++)
- {
- for(j =0; j<i; j++)
- {
- }
- }
- if(from_login ==0 || g_sd_ftp <0) break;
- case ID_REMOTE_CHDIR:
- g_stop =FALSE;
- GetDlgItemText(IDE_REMOTE_DIR, g_remote_dir, sizeof(g_remote_dir));
- SetDlgItemText(IDC_STATUS, "cd remote dir...");
- if(ftp_chdir(g_sd_ftp, g_remote_dir) <0)
- {
- SetDlgItemText(IDC_STATUS, "cd remote dir failed");
- return FALSE;
- }
- else SetDlgItemText(IDC_STATUS, "cd remote dir success");
- strcpy(g_cur_remote_dir, g_remote_dir);
- m_listRemote.DeleteAllItems();
- SetDlgItemText(IDC_STATUS, "list remote dir...");
- int ret;
- if((ret =ftp_list(g_sd_ftp, "listtmp.txt")) <0)
- SetDlgItemText(IDC_STATUS, "list remote dir failed");
- else SetDlgItemText(IDC_STATUS, "list remote dir success");
- if(ret ==0)
- {
- SETSTATUS("no file list");
- break;
- }
- FILE *fp;
- char type;
- if((fp =fopen("listtmp.txt", "r")) ==NULL)
- {
- SetDlgItemText(IDC_STATUS, "open listtmp.txt failed");
- return FALSE;
- }
- while(!feof(fp))
- {
- if(fscanf(fp, "%c%*s %*s %*s %*s %s %*s %*s %*s", &type, filesize) !=2)
- break;
- if(fgets(filename, sizeof(filename), fp) ==NULL)
- break;
- strtok(filename, "rn");
- del_lr_spaces(filename);
- if(type =='d')
- {
- wsprintf(temp, ">%s", filename);
- strcpy(filename, temp);
- }
- item_num =m_listRemote.InsertItem(m_listRemote.GetItemCount(), filename);
- m_listRemote.SetItemText(item_num, 1, filesize);
- }
- fclose(fp);
- //remove("listtmp.txt");
- break;
- case ID_PUT:
- g_stop =FALSE;
- POSITION pos;
- pos =m_listLocal.GetFirstSelectedItemPosition();
- if(pos ==NULL) break;
- int nitem, re_put_times;
- put_next:
- nitem =m_listLocal.GetNextSelectedItem(pos);
- if(nitem <0) break;
- m_listLocal.GetItemText(nitem, 0, filename, sizeof(filename));
- if(filename[0] =='>') goto put_next;
- m_listLocal.GetItemText(nitem, 1, filesize, sizeof(filesize));
- LVFINDINFO findinfo;
- memset(&findinfo, 0, sizeof(findinfo));
- findinfo.flags =LVFI_STRING;
- findinfo.psz =filename;
- if((nitem =m_listRemote.FindItem(&findinfo)) >=0)
- {
- m_listRemote.GetItemText(nitem, 1, rfilesize, sizeof(rfilesize));
- if(!strcmp(rfilesize, filesize)) goto put_next;
- }
- else strcpy(rfilesize, "-");
- re_put_times =0;
- re_put:
- if(g_stop)
- {
- SETSTATUS("put file break ok");
- break;
- }
- wsprintf(temp, "put file %s, size:%s, rsize:%s ...", filename, filesize, rfilesize);
- SetDlgItemText(IDC_STATUS, temp);
- if(ftp_put_file(g_sd_ftp, filename, 0, filename) <0)
- {
- ftp_quit(g_sd_ftp);
- for(i =0; i<g_max_reconnect; i++)
- {
- if(g_stop)
- {
- SETSTATUS("reconnect break ok");
- break;
- }
- if((g_sd_ftp =reconnect(g_host, g_user, g_pass, g_remote_dir)) <0)
- {
- SetDlgItemText(IDC_STATUS, "reconnect failed");
- Sleep(5000);
- }
- else if(re_put_times++ <g_max_retry) goto re_put;
- }
- if(i ==g_max_reconnect)
- {
- SetDlgItemText(IDC_STATUS, "put file failed for max times");
- break;
- }
- }
- else
- {
- wsprintf(temp, "success put %s...", filename);
- SetDlgItemText(IDC_STATUS, temp);
- m_listRemote.SetItemText(nitem, 1, filesize);
- goto put_next;
- }
- break;
- case ID_PUT_ALL:
- g_stop =FALSE;
- put_dir(g_local_dir, g_remote_dir);
- break;
- case ID_REMOTE_MKDIR:
- g_stop =FALSE;
- if(g_sd_ftp <0) break;
- GetDlgItemText(IDE_REMOTE_DIR, g_remote_dir, sizeof(g_remote_dir));
- SETSTATUS("remote mkdir ...");
- if(ftp_mkdir(g_sd_ftp, g_remote_dir) <0)
- SETSTATUS("remote mkdir failed");
- else SETSTATUS("success remote mkdir");
- break;
- case ID_SETUP:
- CSetupDlg *pdlg;
- pdlg =new CSetupDlg();
- pdlg->DoModal();
- delete pdlg;
- break;
- case ID_STOP:
- g_stop =TRUE;
- break;
- case IDCANCEL:
- if(MessageBox("您是否确定退出MyFtp?", "Paladinchina.com", MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2) ==IDNO)
- return TRUE;
- g_stop =TRUE;
- if(g_sd_ftp >0) tcp_close(g_sd_ftp);
- g_sd_ftp =0;
- EndDialog(0);
- break;
- case IDOK:
- return TRUE;
- }
- return CDialog::OnCommand(wParam, lParam);
- }
- int reconnect(char *host, char *user, char *pass, char *remote_dir)
- {
- int sd;
- if(g_sd_ftp >0) tcp_close(g_sd_ftp);
- g_sd_ftp =-1;
- if((sd =ftp_login(host, user, pass)) <0)
- return -1;
- if(remote_dir ==NULL) return sd;
- if(ftp_chdir(sd, remote_dir) <0)
- {
- ftp_quit(sd);
- return -1;
- }
- strcpy(g_cur_remote_dir, remote_dir);
- return sd;
- }
- char *del_lr_spaces(char *pbuf) /* 删除左右的空格 */
- {
- char *p =pbuf;
- int i =0;
- i =0;
- while(*p ==' ') p++;
- strcpy(pbuf, p);
- i =strlen(pbuf);
- while(i >=0 && *(p + i-1) ==' ') i--;
- *(p+i) =0;
- return pbuf;
- }
- int CMyftpDlg::list_local_dir(char *local_dir)
- {
- struct _finddata_t fileinfo;
- char filesize[50], *pfilename, filename[128];
- long hfind =0;
- int count, from_login =0, item_num;
- if(chdir(local_dir) !=0)
- {
- SETSTATUS("cd local dir failed");
- return -1;
- }
- strcpy(g_cur_local_dir, local_dir);
- SetDlgItemText(IDE_LOCAL_DIR, local_dir);
- memset(&fileinfo, 0, sizeof(fileinfo));
- m_listLocal.DeleteAllItems();
- if((hfind =_findfirst("*", &fileinfo)) <0)
- {
- if(errno ==EINVAL) return FALSE;
- return 0;
- }
- count =0;
- while(1)
- {
- if((pfilename =GetFileName(fileinfo.name)) ==NULL)
- goto next;
- if(!strcmp(pfilename, "_vti_cnf")
- || !strcmp(pfilename, "listtmp.txt"))
- goto next;
- if((fileinfo.attrib & _A_HIDDEN) ==_A_HIDDEN)
- goto next;
- if((fileinfo.attrib & _A_SUBDIR) == _A_SUBDIR)
- {
- strcpy(filesize, "-");
- wsprintf(filename, ">%s",pfilename);
- }
- else
- {
- wsprintf(filesize, "%d", fileinfo.size);
- strcpy(filename, pfilename);
- }
- item_num =m_listLocal.InsertItem(m_listLocal.GetItemCount(), filename);
- m_listLocal.SetItemText(item_num, 1, filesize);
- count ++;
- next:
- if(_findnext(hfind, &fileinfo) <0) break;
- if(fileinfo.name[0] ==0) break;
- }
- _findclose(hfind);
- return count;
- }
- int CMyftpDlg::list_remote_dir(char *remote_dir, BOOL from_cache)
- {
- char filesize[50], filename[128], temp[256];
- long hfind =0;
- int count, from_login =0, ret;
- FILE *fp;
- char type;
- int i, re_chdir_times =0, re_list_times =0, item_num;
- BOOL f_create_ok =-1;
- if(g_sd_ftp <0) return -1;
- re_chdir:
- if(g_stop)
- {
- SETSTATUS("cd remote dir stopped");
- return -1;
- }
- SETSTATUS("cd remote dir");
- if(ftp_chdir(g_sd_ftp, remote_dir) <0)
- {
- if(++re_chdir_times <g_max_retry)
- {
- if(f_create_ok <0 && (f_create_ok =ftp_create_path(g_sd_ftp, remote_dir)) >=0)
- goto re_chdir;
- Sleep(1000);
- for(i =0; i<g_max_reconnect; i++)
- {
- if(g_stop)
- {
- SETSTATUS("reconnect stopped");
- return -1;
- }
- SETSTATUS("reconnect host");
- if((g_sd_ftp =reconnect(g_host, g_user, g_pass, remote_dir)) <0)
- {
- SETSTATUS("reconnect host failed!");
- Sleep(2000);
- }
- else break;
- }
- if(i <g_max_reconnect) goto re_chdir;
- }
- SetDlgItemText(IDC_STATUS, "cd remote dir failed");
- return -1;
- }
- else SetDlgItemText(IDC_STATUS, "cd remote dir success");
- strcpy(g_cur_remote_dir, remote_dir);
- SetDlgItemText(IDE_REMOTE_DIR, remote_dir);
- m_listRemote.DeleteAllItems();
- if(!from_cache)
- {
- re_list:
- if(g_stop)
- {
- SETSTATUS("list remote dir stopped");
- return -1;
- }
- SetDlgItemText(IDC_STATUS, "list remote dir...");
- if((ret =ftp_list(g_sd_ftp, "listtmp.txt")) <0)
- {
- if(++re_list_times <g_max_retry)
- {
- Sleep(1000);
- for(i =0; i<g_max_reconnect; i++)
- {
- if(g_stop)
- {
- SETSTATUS("reconnect stopped");
- return -1;
- }
- SETSTATUS("reconnect host...");
- if((g_sd_ftp =reconnect(g_host, g_user, g_pass, remote_dir)) <0)
- {
- SETSTATUS("reconnect host failed!");
- Sleep(2000);
- }
- else break;
- }
- if(i <g_max_reconnect) goto re_list;
- }
- SetDlgItemText(IDC_STATUS, "list remote dir failed");
- }
- else SetDlgItemText(IDC_STATUS, "list remote dir success");
- if(ret ==0)
- {
- SETSTATUS("no file list");
- return 0;
- }
- }
- if((fp =fopen("listtmp.txt", "r")) ==NULL)
- {
- SetDlgItemText(IDC_STATUS, "open listtmp.txt failed");
- goto re_list;
- }
- count =0;
- while(!feof(fp))
- {
- if(fscanf(fp, "%c%*s %*s %*s %*s %s %*s %*s %*s", &type, filesize) !=2)
- break;
- if(fgets(filename, sizeof(filename), fp) ==NULL)
- break;
- strtok(filename, "rn");
- del_lr_spaces(filename);
- if(type =='d')
- {
- wsprintf(temp, ">%s", filename);
- strcpy(filename, temp);
- }
- item_num =m_listRemote.InsertItem(m_listRemote.GetItemCount(), filename);
- m_listRemote.SetItemText(item_num, 1, filesize);
- count ++;
- }
- fclose(fp);
- //remove("listtmp.txt");
- return count;
- }
- int CMyftpDlg::put_dir(char *local_dir, char *remote_dir)
- {
- char filesize[50], filename[128], rfilesize[50], temp[256];
- long hfind =0;
- int i, from_login =0, item_count;
- char local_dir1[128], remote_dir1[128];
- list_local_dir(local_dir);
- list_remote_dir(remote_dir, FALSE);
- strcpy(local_dir1, local_dir);
- strcpy(remote_dir1, remote_dir);
- //POSITION pos;
- //pos =m_listLocal.GetFirstSelectedItemPosition();
- //if(pos ==NULL) return 0;
- int nitem, nitem_remote, re_put_times;
- nitem =-1;
- item_count =m_listLocal.GetItemCount();
- put_next:
- if(g_stop)
- {
- SETSTATUS("put next stopped");
- return -1;
- }
- nitem ++;
- if(nitem >=item_count) goto f_exit;
- //nitem =m_listLocal.GetNextSelectedItem(pos);
- //if(nitem <0) return 0;
- m_listLocal.GetItemText(nitem, 0, filename, sizeof(filename));
- if(!strcmp(filename, "listtmp.txt")
- || !strcmp(filename, "_vti_cnf"))
- goto put_next;
- if(filename[0] =='>')
- {
- if(filename[1] =='.') goto put_next;
- if(local_dir[strlen(local_dir)-1] =='\' || local_dir[strlen(local_dir)-1] =='/')
- local_dir[strlen(local_dir)-1] =0;
- if(remote_dir[strlen(remote_dir)-1] =='/' || remote_dir[strlen(remote_dir)-1] =='\')
- remote_dir[strlen(remote_dir)-1] =0;
- wsprintf(local_dir1, "%s\%s", local_dir, &filename[1]);
- wsprintf(remote_dir1, "%s/%s", remote_dir, &filename[1]);
- put_dir(local_dir1, remote_dir1);
- goto put_next;
- }
- m_listLocal.GetItemText(nitem, 1, filesize, sizeof(filesize));
- if(g_max_file_size >0)
- {
- if(g_max_file_size < atoi(filesize))
- {
- log_msg("file %s size %s too large", filename, filesize);
- goto put_next;
- }
- }
- LVFINDINFO findinfo;
- memset(&findinfo, 0, sizeof(findinfo));
- findinfo.flags =LVFI_STRING;
- findinfo.psz =filename;
- if((nitem_remote =m_listRemote.FindItem(&findinfo)) >=0)
- {
- m_listRemote.GetItemText(nitem_remote, 1, rfilesize, sizeof(rfilesize));
- if(!strcmp(rfilesize, filesize)) goto put_next;
- }
- else strcpy(rfilesize, "-");
- re_put_times =0;
- re_put:
- if(g_stop)
- {
- SETSTATUS("put file stopped");
- return -1;
- }
- wsprintf(temp, "put file %s, size:%s, rsize:%s ...", filename, filesize, rfilesize);
- SetDlgItemText(IDC_STATUS, temp);
- if(ftp_put_file(g_sd_ftp, filename, 0, filename) <0)
- {
- ftp_quit(g_sd_ftp);
- for(i =0; i<g_max_reconnect; i++)
- {
- if(g_stop)
- {
- SETSTATUS("reconnect stopped");
- return -1;
- }
- SETSTATUS("put failed,reconnect host...");
- if((g_sd_ftp =reconnect(g_host, g_user, g_pass, remote_dir)) <0)
- {
- SetDlgItemText(IDC_STATUS, "reconnect failed");
- Sleep(5000);
- }
- else if(re_put_times++ <g_max_retry) goto re_put;
- }
- if(i ==g_max_reconnect)
- {
- SetDlgItemText(IDC_STATUS, "put file failed for max reconnect times");
- return -1;
- }
- }
- else
- {
- wsprintf(temp, "success put %s...", filename);
- SetDlgItemText(IDC_STATUS, temp);
- m_listRemote.SetItemText(nitem_remote, 1, filesize);
- goto put_next;
- }
- f_exit:
- int len;
- len =strlen(local_dir);
- for(i =len-1; i>=0; i--)
- {
- if(local_dir[i] =='/' || local_dir[i] =='\')
- {
- local_dir[i+1] =0;
- break;
- }
- }
- len =strlen(remote_dir);
- for(i =len-1; i>=0; i--)
- {
- if(remote_dir[i] =='/' || remote_dir[i] =='\')
- {
- if(i ==0) remote_dir[i+1] =0;
- else remote_dir[i] =0;
- break;
- }
- }
- list_local_dir(local_dir);
- list_remote_dir(remote_dir, TRUE);
- return nitem;
- }
- void CMyftpDlg::OnClose()
- {
- CDialog::OnClose();
- }
- void CMyftpDlg::OnDblClickLocal(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- POSITION pos;
- pos =m_listLocal.GetFirstSelectedItemPosition();
- if(pos ==NULL) return;
- int nitem;
- char filename[80], path[128];
- nitem =m_listLocal.GetNextSelectedItem(pos);
- if(nitem <0) return;
- m_listLocal.GetItemText(nitem, 0, filename, sizeof(filename));
- if(filename[0] !='>') return;
- if(strcmp(filename, ">.") ==0) return;
- wsprintf(path, "%s\%s", g_cur_local_dir, &filename[1]);
- list_local_dir(path);
- }
- void CMyftpDlg::OnDblClickRemote(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- POSITION pos;
- pos =m_listRemote.GetFirstSelectedItemPosition();
- if(pos ==NULL) return;
- int nitem;
- char filename[80], path[128];
- nitem =m_listRemote.GetNextSelectedItem(pos);
- if(nitem <0) return;
- m_listRemote.GetItemText(nitem, 0, filename, sizeof(filename));
- if(filename[0] !='>') return;
- if(strcmp(filename, ">.") ==0) return;
- wsprintf(path, "%s/%s", g_cur_remote_dir, &filename[1]);
- list_remote_dir(path, FALSE);
- }