⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpfilebrowse.cpp

📁 ftp 服务器源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FtpFileBrowse.cpp : implementation file
//

#include "stdafx.h"
#include "FtpBrowse.h"
#include "FtpFileBrowse.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// FtpFileBrowse dialog


FtpFileBrowse::FtpFileBrowse(CWnd* pParent /*=NULL*/)
	: CDialog(FtpFileBrowse::IDD, pParent)
{
	connected=FALSE;
	m_ftpprop.hwndOwner =NULL;
	m_ftpprop.Server="";
	m_ftpprop.username = "";
	m_ftpprop.password="";
	m_ftpprop.iPort=21;
	dblclick = FALSE;
	//set image list
	HIMAGELIST  hSystemSmallImageList;//, hSystemLargeImageList; 
	SHFILEINFO    ssfi; 

   //get a handle to the system small icon list 
	hSystemSmallImageList = 
	 (HIMAGELIST)SHGetFileInfo( 
	(LPCTSTR)_T("c:\\"), 
	0, 
	 &ssfi, 
	 sizeof(SHFILEINFO), 
	 SHGFI_SYSICONINDEX | SHGFI_SMALLICON); 
	//attach it to the small image list 
	//--DON'T FORGET TO PUT m_smallImageList.Detach();  in your destructor 
	m_smallImageList.Attach(hSystemSmallImageList); 
	if (!m_iconlist.Create(IDB_ICONLIST, 16, 1, RGB (255, 0, 255)) ) 
		AfxMessageBox("Fail to create item icons!");
	
	
	//{{AFX_DATA_INIT(FtpFileBrowse)
	m_filename = _T("");
	//}}AFX_DATA_INIT
}
FtpFileBrowse::~FtpFileBrowse()
{
	m_smallImageList.Detach();
	if(connected) ftp_connection->Close();
}
	


void FtpFileBrowse::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(FtpFileBrowse)
	DDX_Control(pDX, IDC_COMBOBOXEX3, m_excom);
	DDX_Control(pDX, IDC_PROP, m_prop);
	DDX_Control(pDX, IDC_DEL, m_del);
	DDX_Control(pDX, IDC_VIEW, m_list);
	DDX_Control(pDX, IDC_UP, m_up);
	DDX_Control(pDX, IDC_TYPE, m_type);
	DDX_Control(pDX, IDC_NEW, m_new);
	DDX_Text(pDX, IDC_FILENAME, m_filename);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(FtpFileBrowse, CDialog)
	//{{AFX_MSG_MAP(FtpFileBrowse)
	ON_CBN_SELCHANGE(IDC_DIR, OnSelchangeDir)
	ON_NOTIFY(NM_RCLICK, IDC_VIEW, OnRclickView)
	ON_NOTIFY(NM_CLICK, IDC_VIEW, OnClickView)
	ON_NOTIFY(NM_DBLCLK, IDC_VIEW, OnDblclkView)
	ON_BN_CLICKED(IDC_UP, OnUp)
	ON_BN_CLICKED(IDC_NEW, OnNew)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_COMMAND(ID_FILES_SELECT, OnFilesSelect)
	ON_COMMAND(ID_FILES_OPENWITH, OnFilesOpenwith)
	ON_COMMAND(ID_FILES_DELETE, OnFilesDelete)
	ON_COMMAND(ID_FILES_SENDTONOTEPAD, OnFilesSendtonotepad)
	ON_COMMAND(ID_DIR_SELECT, OnDirSelect)
	ON_COMMAND(ID_DIR_DELETE, OnDirDelete)
	ON_COMMAND(ID_FILES_PROPERTIES, OnFilesProperties)
	ON_COMMAND(ID_DIR_PROPERTIES, OnDirProperties)
	ON_NOTIFY(LVN_BEGINLABELEDIT, IDC_VIEW, OnBeginlabeleditView)
	ON_NOTIFY(LVN_ENDLABELEDIT, IDC_VIEW, OnEndlabeleditView)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	ON_BN_CLICKED(IDC_PROP, OnProp)
	ON_COMMAND(ID_EMTPY_NEWDIRECTORY, OnEmtpyNewdirectory)
	ON_COMMAND(ID_EMTPY_PROPERTIES, OnEmtpyProperties)
	ON_CBN_SELCHANGE(IDC_COMBOBOXEX3, OnSelchangeComboboxex3)
	ON_COMMAND(ID_FILES_RENAME, OnFilesRename)
	ON_COMMAND(ID_DIR_RENAME, OnDirRename)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// FtpFileBrowse message handlers

BOOL FtpFileBrowse::OnInitDialog() 
{
	CDialog::OnInitDialog();
	//image.Add(LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME)));
	m_list.SetImageList(&m_smallImageList, LVSIL_SMALL); 
	m_type.SetCurSel(0);
	ftp_connection = inet_session.GetFtpConnection(m_ftpprop.Server,m_ftpprop.username,m_ftpprop.password,m_ftpprop.iPort,TRUE);
	if(ftp_connection==NULL)
	{
		
		DWORD err = GetLastError();
		char errmsg[1024];
		if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_MAX_WIDTH_MASK,0,err,0x0409,errmsg,1024,NULL)==0)
			MessageBox("FAILED","Error");
		else
		MessageBox(errmsg,"Error");
		return TRUE;
	}
	connected=TRUE;
	upbit.LoadBitmap(IDB_UP);
	newbit.LoadBitmap(IDB_NEW);
	delbit.LoadBitmap(IDB_DEL);
	propbit.LoadBitmap(IDB_PROP);
	m_up.SetBitmap(upbit.operator HBITMAP());
	m_new.SetBitmap(newbit.operator HBITMAP());
	m_del.SetBitmap(delbit.operator HBITMAP());
	m_prop.SetBitmap(propbit.operator HBITMAP());
	CString CurrentDirectory;
	ftp_connection->GetCurrentDirectory(CurrentDirectory);
	m_curdir = CurrentDirectory;
	CString look = m_ftpprop.Server + " (/)";
//	InsertString(0,look)	;

	look = m_ftpprop.Server + " (";
	look+=CurrentDirectory+")";
//	InsertString(1,look);
	m_excom.SetImageList(&m_iconlist);
	char txt[256];
	sprintf(txt,"/ (%s)",m_ftpprop.Server);
	COMBOBOXEXITEM myitem;
	myitem.mask = CBEIF_TEXT |CBEIF_IMAGE|CBEIF_SELECTEDIMAGE|CBEIF_OVERLAY;
	myitem.iItem=0;
	myitem.pszText = txt;
	myitem.cchTextMax = strlen(txt);
	myitem.iImage = HDRIVE;
	myitem.iSelectedImage =HDRIVE;
	myitem.iOverlay=1;
	m_excom.InsertItem(&myitem);
	int pos=CurrentDirectory.ReverseFind('/');
	if(pos==-1)
	{
		MessageBox("Error in finding the directory");
		return TRUE;
	}
	CString temp;
	if(pos==0)
		temp="/";
	else
		temp = CurrentDirectory.Right(CurrentDirectory.GetLength()-1-pos);

	myitem.mask = CBEIF_TEXT |CBEIF_IMAGE|CBEIF_INDENT |CBEIF_SELECTEDIMAGE|CBEIF_OVERLAY;
	myitem.iItem=1;
	myitem.pszText = temp.GetBuffer(0);
	myitem.cchTextMax = temp.GetLength();
	myitem.iImage = CFOLDER;
	myitem.iSelectedImage =OFOLDER;
	myitem.iIndent = 1;
	m_excom.InsertItem(&myitem);
	
	m_excom.SetCurSel(1);
	CString current;
	m_excom.GetLBText(m_excom.GetCurSel(),current);
	//current = current.Mid(m_ftpprop.Server.GetLength()+2,current.GetLength()-m_ftpprop.Server.GetLength()-3);
	FillFiles(CurrentDirectory);
	//add tooltip
	toolnew.Create(this);
	toolnew.AddTool(GetDlgItem(IDC_NEW),"Create New Folder");	
	toolnew.AddTool(GetDlgItem(IDC_UP),"Up One Level");
	toolnew.AddTool(GetDlgItem(IDC_DEL),"Delete");
	toolnew.AddTool(GetDlgItem(IDC_PROP),"Properties");
	toolnew.Activate(TRUE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void FtpFileBrowse::OnSelchangeDir() 
{
	CString current;
	int pos = m_excom.GetCurSel();
	if(pos==2)
		FillFiles(lastdir);
	else if(pos==1)
		FillFiles(m_curdir);
	else
		FillFiles("/");

}

void FtpFileBrowse::OnRclickView(NMHDR* pNMHDR, LRESULT* pResult) 
{
	CMenu menu, *pSubMenu;
		CWnd* pTarget = this;
		int i = m_list.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
		if(i==LB_ERR)
		{
			if (!menu.LoadMenu(IDR_FILE)) return ;
			if (!(pSubMenu = menu.GetSubMenu(2))) return ;
			
		} 
		else
		{
			CString itemname;
			itemname = m_list.GetItemText(i,0);
			if (!menu.LoadMenu(IDR_FILE)) return ;
			if(IsDir(itemname))
			{	
				if (!(pSubMenu = menu.GetSubMenu(1))) return ;
			}
			else
			{
				if (!(pSubMenu = menu.GetSubMenu(0))) return ;
			}
			 ::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);
		}

        // Make chosen menu item the default (bold font)
       

        // Display and track the popup menu
        CPoint pos;
        GetCursorPos(&pos);

        pTarget->SetForegroundWindow();  
        ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, 
                         pTarget->GetSafeHwnd(), NULL);

        // BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
        pTarget->PostMessage(WM_NULL, 0, 0);

        menu.DestroyMenu();	
	*pResult = 0;
}

int FtpFileBrowse::GetIconIndex(const CString &csFileName)
{
     SHFILEINFO    sfi;
        SHGetFileInfo(
           (LPCTSTR)csFileName, 
           0,
           &sfi, 
           sizeof(SHFILEINFO), 
           SHGFI_SYSICONINDEX | SHGFI_SMALLICON );

        return sfi.iIcon;

}

void FtpFileBrowse::OnClickView(NMHDR* pNMHDR, LRESULT* pResult) 
{
	BOOL direc=FALSE;
	int i = m_list.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
		if(i==LB_ERR)
		{
			return;
		} 
	
	CString item = m_list.GetItemText(i,0);
	if(!IsDir(item))	
	{
		m_filename = item;
		UpdateData(FALSE);
	}	
	*pResult = 0;
}

BOOL FtpFileBrowse::IsDir(CString dir)
{
	BOOL direc=FALSE;
	CFtpFileFind finder(ftp_connection);
	BOOL bWorking = finder.FindFile("*");
	while (bWorking)
   {

	   bWorking = finder.FindNextFile();
		if(finder.IsDirectory())
		{
			 if(strcmp(dir,finder.GetFileName())==0)
			 {
				lastdir = finder.GetFilePath();
				 direc = TRUE;
				 break;
			 }
		}
	}
	finder.Close();
	return (direc);
}

void FtpFileBrowse::OnDblclkView(NMHDR* pNMHDR, LRESULT* pResult) 
{
	BOOL direc=FALSE;
	int i = m_list.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
		if(i==LB_ERR)
		{
			return;
		} 
	
	CString item = m_list.GetItemText(i,0);
	if(!IsDir(item))	
	{
		dblclick = TRUE;
		OnOpen();
	}
	else
		OnDirSelect();

	*pResult = 0;
}

void FtpFileBrowse::OnUp() 
{
	CString CurDir;
	ftp_connection->GetCurrentDirectory(CurDir);
	if(CurDir=="/")
	{
		MessageBox("You are at the top level and cannot go up another level.","Error");
		return;
	}
	int pos = CurDir.ReverseFind('/');
	if(pos==-1)
	{
		MessageBox("Canot go one level up","Error");
		return;
	}
	CString updir;
	
	updir = CurDir.Left(pos);
	if(pos==0)
		updir="/";
	FillFiles(updir);
	
}

void FtpFileBrowse::OnNew() 
{
	newdir=TRUE;
	CString CurDir;
	ftp_connection->GetCurrentDirectory(CurDir);
	char newdir[MAX_PATH],appen[20],constdir[MAX_PATH];
	appen[0]='\0';
	strcpy(newdir,"NewDirectory");
	strcpy(constdir,"NewDirectory");

	int i=1;
	while(TRUE)
	{
		if(strcmp(appen,"")!=0)
			sprintf(newdir,"%s%s",constdir,appen);
		if (ftp_connection->CreateDirectory(newdir)==0)
		{
			DWORD err = GetLastError();
			if(err==12003)	// already present
			{
				sprintf(appen,"[%d]",i++);
				continue;
			}
			char errmsg[1024];
			if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_MAX_WIDTH_MASK|FORMAT_MESSAGE_FROM_STRING,0,err,0x0409,errmsg,1024,NULL)==0)
			{
				MessageBox("FAILED","Error");
				break;
			}
			else
			{
				MessageBox(errmsg,"Error");
				break;
			}
			return ;
		}
		else break;
	}


	FillFiles(CurDir);
	LVFINDINFO indinf;
	indinf.flags = LVFI_STRING;
	indinf.psz=newdir;
	int pos = m_list.FindItem(&indinf);
	if(pos!=-1)
	{
		CString item = m_list.GetItemText(pos,0);
		m_list.SetFocus();
		CEdit * myedit;
		CString newval;
		myedit= m_list.EditLabel(pos);
		myedit->GetWindowText(newval);
		//while(m_list.GetEditControl()->SetFocus());
		
		
		myedit->GetWindowText(item);
		
		//if(ftp_connection->Rename(newdir,item)==0)
		//	MessageBox("poda");
	}
		
}

void FtpFileBrowse::OnOpen() 
{
	UpdateData();
	if(m_filename.IsEmpty())
		return;

	CString compare,CurDir;
	ftp_connection->GetCurrentDirectory(CurDir);
		;
	if(!dblclick)
	{
		dblclick = FALSE;
		compare = m_filename;
	}
	else
	{
		int i = m_list.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
		if(i==LB_ERR)
		{
			return;
		} 
		compare= m_list.GetItemText(i,0);
	}

	CFtpFileFind finder(ftp_connection);
	BOOL bWorking = finder.FindFile("*");
	BOOL found=FALSE,foundfile=FALSE;
	while (bWorking)
   {
	   bWorking = finder.FindNextFile();
		if(finder.IsDirectory())
		{
			 if(strcmp(compare,finder.GetFileName())==0)
			 {
				found = TRUE;
				break;
			 }
		}
		else
		{
			 if(strcmp(compare,finder.GetFileName())==0)
			 {
			found=TRUE;
			foundfile=TRUE;
			file_filepath = finder.GetFilePath();
			file_filename= finder.GetFileName();
			file_root =finder.GetRoot();
			file_fileurl = finder.GetFileURL();
			break;
			 }
			
		}
		
	}
	finder.Close();
	
	//new if-else
	if(found )
	{
		if(!foundfile)
		{
			FillFiles(compare);
			return;
		}
		else
		{
			CDialog::OnOK();
			return;
		}
	}
	else
	{
		if(ftp_connection->SetCurrentDirectory(compare))
		{	
			FillFiles(compare);
			return;
		}
		else
			{
				//it can be a file
//start
			//check directly
					CFtpFileFind finder(ftp_connection);
					BOOL bWorking = finder.FindFile(compare);
				//	BOOL found=FALSE,foundfile=FALSE;
					CString myfile;
					while (bWorking)
					{
						bWorking = finder.FindNextFile();
						file_filename= finder.GetFileName();
						if(file_filename == compare)
						{
							file_filepath = finder.GetFilePath();
							//find the director now
							int pos = compare.ReverseFind('/');
							if(pos==-1)
							{
								MessageBox("Canot go one level up","Error");
								return;
							}
							CString updir;
							updir = compare.Left(pos);
							if(pos==0)
							updir="/";
							//pos = file_filepath.Find(updir.GetBuffer(0));
							file_filepath = file_filepath.Right(file_filepath.GetLength()-1-pos);
							if(compare.GetAt(0)!='/')
							{
								CString temppath;
								temppath = CurDir+"/";
								temppath+=file_filepath;
								file_filepath = temppath;
							}
							//Now get file name
							pos = file_filepath.ReverseFind('/');
							file_filename=file_filepath.Right(file_filepath.GetLength()-1-pos);
							file_root=file_filepath.Left(pos);
							file_fileurl = finder.GetFileURL();
							file_fileurl="ftp://";
							file_fileurl+=m_ftpprop.Server;
							file_fileurl+=file_filepath;
							finder.Close();
							CDialog::OnOK();
							return;
						}
						
						break;
					}
					finder.Close();
				}
	}			
//end
	CString ermsg = "The file '";
	ermsg += CurDir+"/"+m_filename + "' cannot be found.\nMake sure that the path and file name are correct.";
	MessageBox(ermsg,"File not found",MB_ICONEXCLAMATION);
			
}

void FtpFileBrowse::FillFiles(CString dir)
{
	CString CurrentDirectory;
	ftp_connection->GetCurrentDirectory(CurrentDirectory);
	if(CurrentDirectory!=dir)
		if(!ftp_connection->SetCurrentDirectory(dir))
		{
			MessageBox("Unable to change directory","Error");
			return;
		}
	CString look;
	look = dir;
	if(dir=="/")	//disable one-up
		m_up.EnableWindow(FALSE);
	else
		m_up.EnableWindow(TRUE);


	//check if it is three already
	int count = m_excom.GetCount();
	switch(count)
	{
	case 2:
		if (look!="/" && look != m_curdir)
		{
			InsertString(2,look);
			m_excom.SetCurSel(2);
		}
		if(look=="/")
			m_excom.SetCurSel(0);
		else if(look==m_curdir)
			m_excom.SetCurSel(1);
		break;
	case 3:
		if (look!="/" && look != m_curdir)
		{
			m_excom.DeleteItem(2);
			InsertString(2,look);
			m_excom.SetCurSel(2);
		}
		if(look=="/")
			m_excom.SetCurSel(0);
		else if(look==m_curdir)
			m_excom.SetCurSel(1);
		break;
	}
	
  //clear all the controls in the list
	m_list.DeleteAllItems();
	CFtpFileFind finder(ftp_connection);
   BOOL bWorking = finder.FindFile("*.*");
   int i=0;
   
   while (bWorking)
   {
		char file[MAX_PATH];	
		 bWorking = finder.FindNextFile();
		strcpy(file,finder.GetFileName());
		LVITEM it;
		it.mask = LVIF_TEXT|LVIF_IMAGE;
		it.iItem = i;
		it.iSubItem=0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -