(c) BigSpeed Computing Inc. - Secure private networking
//Visual C++ .NET example of secure file sharing client
//To keep this VC .NET example simple, we are going to implement
//only the main file operations



// bsFileClnDlg.cpp : implementation file
//

#include "stdafx.h"
#include "bsFileCln.h"
#include "bsFileClnDlg.h"
#include "dlgConnect.h"
#include ".\bsfileclndlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif



const int FolderImgIdx = 0;
const int FileImgIdx = 1;



// CbsFileClnDlg dialog



CbsFileClnDlg::CbsFileClnDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CbsFileClnDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CbsFileClnDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, ID_btnConnect, c_btnConnect);
  DDX_Control(pDX, ID_btnCancel, c_btnCancel);
  DDX_Control(pDX, ID_btnDisconnect, c_btnDisconnect);
  DDX_Control(pDX, IDC_STATUS1, c_Status1);
  DDX_Control(pDX, IDC_STATUS2, c_Status2);
  DDX_Control(pDX, IDC_STATUS3, c_Status3);
  DDX_Control(pDX, IDC_lvFiles, c_lvFiles);
  DDX_Control(pDX, IDC_stFolder, c_stFolder);
  DDX_Control(pDX, ID_btnList, c_btnList);
  DDX_Control(pDX, ID_btnIn, c_btnIn);
  DDX_Control(pDX, ID_btnUp, c_btnUp);
  DDX_Control(pDX, ID_btnNew, c_btnNew);
  DDX_Control(pDX, ID_btnDownload, c_btnDownload);
  DDX_Control(pDX, ID_btnUpload, c_btnUpload);
  DDX_Control(pDX, ID_btnRename, c_btnRename);
  DDX_Control(pDX, ID_btnDelete, c_btnDelete);
  DDX_Control(pDX, ID_btnAbortDnld, c_btnAbortDnld);
  DDX_Control(pDX, ID_btnAbortUpld, c_btnAbortUpld);
  DDX_Control(pDX, IDC_stDownload, c_stDownload);
  DDX_Control(pDX, IDC_stUpload, c_stUpload);
  DDX_Control(pDX, IDC_BSFILECLNX1, FileCln);
}



BEGIN_MESSAGE_MAP(CbsFileClnDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_btnConnect, OnBnClickedbtnconnect)
ON_BN_CLICKED(ID_btnExit, OnBnClickedbtnexit)
ON_BN_CLICKED(ID_btnCancel, OnBnClickedbtncancel)
ON_BN_CLICKED(ID_btnDisconnect, OnBnClickedbtndisconnect)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_lvFiles, OnLvnItemchangedlvfiles)
ON_BN_CLICKED(ID_btnList, OnBnClickedbtnlist)
ON_BN_CLICKED(ID_btnIn, OnBnClickedbtnin)
ON_BN_CLICKED(ID_btnUp, OnBnClickedbtnup)
ON_BN_CLICKED(ID_btnNew, OnBnClickedbtnnew)
ON_BN_CLICKED(ID_btnRename, OnBnClickedbtnrename)
ON_BN_CLICKED(ID_btnDelete, OnBnClickedbtndelete)
ON_BN_CLICKED(ID_btnUpload, OnBnClickedbtnupload)
ON_BN_CLICKED(ID_btnAbortUpld, OnBnClickedbtnabortupld)
ON_BN_CLICKED(ID_btnDownload, OnBnClickedbtndownload)
ON_NOTIFY(NM_DBLCLK, IDC_lvFiles, OnNMDblclklvfiles)
ON_BN_CLICKED(ID_btnAbortDnld, OnBnClickedbtnabortdnld)
END_MESSAGE_MAP()




// CbsFileClnDlg message handlers

BOOL CbsFileClnDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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

	// TODO: Add extra initialization here
	c_lvFiles.InsertColumn(0, "Name", LVCFMT_LEFT, 120); 
	c_lvFiles.InsertColumn(1, "Size", LVCFMT_RIGHT, 100); 
	c_lvFiles.InsertColumn(2, "Time", LVCFMT_LEFT, 150); 
	c_lvFiles.ModifyStyle(NULL, LVS_REPORT, 0);
	c_lvFiles.SetExtendedStyle(LVS_EX_FULLROWSELECT);

  m_ImgLst.Create(IDB_IMAGES, 16, 2, RGB(255, 0, 255)); 
  c_lvFiles.SetImageList(&m_ImgLst, LVSIL_SMALL);
 
	dlgConnect.m_edHost = "127.0.0.1";
	dlgConnect.m_edPort = "2222";

  m_NowList = false;
  m_NowCreateFolder = false;
  m_NowDownload = false;
  m_NowUpload = false;
  m_NowRenameFolder = false;
  m_NowDeleteFolder = false;
  m_NowRenameFile = false;
  m_NowDeleteFile = false;

  UpdateStatus();  	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CbsFileClnDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CbsFileClnDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



BEGIN_EVENTSINK_MAP(CbsFileClnDlg, CDialog)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 201, OnClosedBsfileclnx1, VTS_NONE)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 203, OnConnectedBsfileclnx1, VTS_NONE)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 204, OnCreateFolderDoneBsfileclnx1, VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 205, OnDeleteFileDoneBsfileclnx1, VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 206, OnDeleteFolderDoneBsfileclnx1, VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 207, OnDisconnectedBsfileclnx1, VTS_NONE)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 208, OnDownloadDoneBsfileclnx1, VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 209, OnDownloadProgressBsfileclnx1, VTS_I4 VTS_I4 VTS_I4 VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 213, OnHaveListItemBsfileclnx1, VTS_BSTR VTS_BOOL VTS_I4 VTS_I4 VTS_I4 VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 214, OnKeyChangedBsfileclnx1, VTS_NONE)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 218, OnRenameFileDoneBsfileclnx1, VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 219, OnRenameFolderDoneBsfileclnx1, VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 220, OnSigninDoneBsfileclnx1, VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 229, OnUploadDoneBsfileclnx1, VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 230, OnUploadProgressBsfileclnx1, VTS_I4 VTS_I4 VTS_I4 VTS_I4)
ON_EVENT(CbsFileClnDlg, IDC_BSFILECLNX1, 215, OnListFolderDoneBsfileclnx1, VTS_I4)
END_EVENTSINK_MAP()




// *** General ***


//Add a trailing slash
CString AddSlash(CString aPath)
{
  if (aPath.Right(1) == "\\")
    return aPath;
  else
    return aPath + "\\";
}


//Remove the trailing slash
CString SlashOff(CString aPath)
{
  if (aPath.Right(1) == "\\")
    return aPath.Left(aPath.GetLength() - 1);
  else
    return aPath;
}


//Insert a leading slash
CString PreSlash(CString aPath)
{
  if (aPath.Left(1) == "\\")
    return aPath;
  else
    return "\\" + aPath;
}



int CbsFileClnDlg::GetImageIndex(int aItemIndex)
{
  LVITEM lvi;
  lvi.mask = LVIF_IMAGE;
  lvi.iItem = aItemIndex;
  lvi.iSubItem = 0;
  c_lvFiles.GetItem(&lvi);
  return lvi.iImage;
}

int CbsFileClnDlg::GetSelectedIndex(void)
{
  POSITION pos = c_lvFiles.GetFirstSelectedItemPosition();
  if (pos == NULL) 
    return -1;
  else
    return c_lvFiles.GetNextSelectedItem(pos);
}


bool CbsFileClnDlg::GetInputText(CString& aValue, CString aTitle, CString aCaption)
{
  dlgInputBox.v_Edit = aValue;
  dlgInputBox.m_Title = aTitle;
  dlgInputBox.m_Caption = aCaption;
  if (dlgInputBox.DoModal() != IDOK) return false;
  aValue = dlgInputBox.v_Edit;
  return true;
}




// *** Working ***






//Describe an error code
CString CbsFileClnDlg::ErrorText(int aCode)
{
  CString S;
  if (aCode == 0) return "";

  S = "Unknown error";
  switch (aCode)
  {
    case   2: S= "Illegal operation"; break;
    case   3: S= "Not connected"; break;
    case 100: S= "Wrong CRC"; break;
    case 101: S= "Broken connection"; break;
    case 200: S= "User break (client)"; break;
    case 201: S= "Invalid handle (client)"; break;
    case 202: S= "Client is not signed in"; break;
    case 203: S= "There is no assigned event handler (client)"; break;
    case 204: S= "There is an error in the event handler (client)"; break;
    case 205: S= "Operation is already in progress (client)"; break;
    case 206: S= "Cannot get file information (client)"; break;
    case 207: S= "Cannot create folder (client)"; break;
    case 208: S= "Cannot delete folder (client)"; break;
    case 209: S= "Cannot delete file (client)"; break;
    case 210: S= "Cannot rename folder (client)"; break;
    case 211: S= "Cannot rename file (client)"; break;
    case 212: S= "Cannot open file (client)"; break;
    case 213: S= "Cannot create file (client)"; break;
    case 214: S= "Cannot read from file (client)"; break;
    case 215: S= "Cannot write to file (client)"; break;
    case 216: S= "Cannot rename temporary file (client)"; break;
    case 301: S= "Cannot start server"; break;
    case 302: S= "Access denied"; break;
    case 303: S= "User break (server)"; break;
    case 304: S= "Invalid handle (server)"; break;
    case 305: S= "There is no assigned event handler (server)"; break;
    case 306: S= "There is an error in the event handler (server)"; break;
    case 307: S= "Operation is already in progress (server)"; break;
    case 308: S= "Cannot get file information (server)"; break;
    case 309: S= "Cannot create folder (server)"; break;
    case 310: S= "Cannot delete folder (server)"; break;
    case 311: S= "Cannot delete file (server)"; break;
    case 312: S= "Cannot rename folder (server)"; break;
    case 313: S= "Cannot rename file (server)"; break;
    case 314: S= "Cannot open file (server)"; break;
    case 315: S= "Cannot create file (server)"; break;
    case 316: S= "Cannot read from file (server)"; break;
    case 317: S= "Cannot write to file (server)"; break;
    case 318: S= "Cannot rename temporary file (server)"; break;
  };

  return S;
}








//Display an error message, if there is any
void CbsFileClnDlg::CheckError(int aCode)
{
  if (aCode == 0) return;
  AfxMessageBox(ErrorText(aCode));
}







//Update the status text
void CbsFileClnDlg::UpdateStatus(void)
{
  if (FileCln.get_Connected())
  {
    //already connected
    c_btnConnect.EnableWindow(false);
    c_btnCancel.EnableWindow(false);
    c_btnDisconnect.EnableWindow(true);
    if (FileCln.get_WrongKey())
      c_Status1.SetWindowText(" Wrong key");
    else
      c_Status1.SetWindowText(" Connected");

    c_Status2.SetWindowText(FileCln.get_ServerAddress() + ":" + FileCln.get_ServerPort());
    if (FileCln.get_Signedin())
      c_Status2.SetWindowText(" User: " + GetUsername());
    else
      c_Status2.SetWindowText(" User: not signed in");
  }   
  else
  {
    c_Status2.SetWindowText("");
    c_Status3.SetWindowText("");
    if (FileCln.get_Connecting())
    {
      //now connecting
      c_btnConnect.EnableWindow(false);
      c_btnCancel.EnableWindow(true);
      c_btnDisconnect.EnableWindow(false);
      c_Status1.SetWindowText(" Connecting");
    }
    else
    {
      //not connected
      c_btnConnect.EnableWindow(true);
      c_btnCancel.EnableWindow(false);
      c_btnDisconnect.EnableWindow(false);
      c_Status1.SetWindowText(" Disconnected");
    }
  }

  UpdateButtons();
}



// Update the buttons state
void CbsFileClnDlg::UpdateButtons(void)
{
  CString S;
  c_btnList.EnableWindow(false);
  c_btnIn.EnableWindow(false);
  c_btnUp.EnableWindow(false);
  c_btnNew.EnableWindow(false);
  c_btnDownload.EnableWindow(false);
  c_btnUpload.EnableWindow(false);
  c_btnRename.EnableWindow(false);
  c_btnDelete.EnableWindow(false);
  c_btnAbortDnld.EnableWindow(m_NowDownload);
  c_btnAbortUpld.EnableWindow(m_NowUpload);

  if (!c_lvFiles.IsWindowEnabled()) return;
    
  c_stFolder.GetWindowText(S);  
  c_btnUp.EnableWindow(S > "\\");
  

  c_btnNew.EnableWindow(!m_NowCreateFolder);
  c_btnList.EnableWindow(!m_NowCreateFolder);
  c_btnUpload.EnableWindow(!m_NowUpload);

  if (c_lvFiles.GetSelectedCount() > 0)
  {
    if (GetImageIndex(GetSelectedIndex()) == FolderImgIdx) 
    {
      //folder
      c_btnIn.EnableWindow(!m_NowList);      
      c_btnRename.EnableWindow(!m_NowRenameFolder);
      c_btnDelete.EnableWindow(!m_NowDeleteFolder);
    }
    else
    {
      //file
      c_btnDownload.EnableWindow(!m_NowDownload);
      c_btnRename.EnableWindow(!m_NowRenameFile);
      c_btnDelete.EnableWindow(!m_NowDeleteFile);
    }
  }
}



//Return the name of the signed-in user
CString CbsFileClnDlg::GetUsername(void)
{
  CString R;
  
  R = FileCln.get_Username();
  if (R > "") 
    return R;
  else
    return "Default";
}



CString CbsFileClnDlg::GetCurFol(void)
{
  CString R;
  c_stFolder.GetWindowText(R);
  return R;
}




//Terminate the application
void CbsFileClnDlg::OnBnClickedbtnexit()
{
  OnCancel();
}





//Initiate a connection request
void CbsFileClnDlg::OnBnClickedbtnconnect()
{
  CString Msg;
  if (dlgConnect.DoModal() != IDOK) return;

  FileCln.put_CryptoKey(dlgConnect.m_edKey);
  FileCln.put_UseCompression(dlgConnect.m_chkCompress);
    
  if (!FileCln.Connect(dlgConnect.m_edHost, dlgConnect.m_edPort))
  {
    Msg.Format("Error Code: %d", FileCln.get_LastError());
    AfxMessageBox(Msg);
  }
    
  UpdateStatus();
}



//Cancel the connection attempt
void CbsFileClnDlg::OnBnClickedbtncancel()
{
  if (FileCln.get_Connecting())
    FileCln.Disconnect();
}



//Disconnect from the server
void CbsFileClnDlg::OnBnClickedbtndisconnect()
{
  if (FileCln.get_Connected())
    FileCln.Disconnect();
}




void CbsFileClnDlg::OnLvnItemchangedlvfiles(NMHDR *pNMHDR, LRESULT *pResult)
{
  NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  *pResult = 0;
  // Has the item's state changed?
  if ((pNMListView->uChanged & LVIF_STATE) != LVIF_STATE)  return; // state has not changed

  UpdateStatus();
}




void CbsFileClnDlg::OnNMDblclklvfiles(NMHDR *pNMHDR, LRESULT *pResult)
{
  *pResult = 0;

  int si = GetSelectedIndex();
  if (si < 0) return;

  if (GetImageIndex(si) == FolderImgIdx) 
    GoInFolder();
  else
    GoDownload();
}








// *** component events ***





//Connection request is completed
void CbsFileClnDlg::OnConnectedBsfileclnx1()
{
  c_lvFiles.EnableWindow(true);
  c_stFolder.SetWindowText("\\");
  UpdateStatus();
  FileCln.SignIn(dlgConnect.m_edUsername, dlgConnect.m_edPassword);
}



//Connection request failed
void CbsFileClnDlg::OnClosedBsfileclnx1()
{
  CString Msg;
  
  Msg.Format("Cannot connect to %s : %s", dlgConnect.m_edHost, dlgConnect.m_edPort);
  AfxMessageBox(Msg);
  UpdateStatus();
}


//The connection is broken
void CbsFileClnDlg::OnDisconnectedBsfileclnx1()
{
  c_lvFiles.EnableWindow(false);
  c_stFolder.SetWindowText("");
  c_lvFiles.DeleteAllItems();
  UpdateStatus();
}


//The key matching is changed
void CbsFileClnDlg::OnKeyChangedBsfileclnx1()
{
  UpdateStatus();
}


//The sign-in request is completed
void CbsFileClnDlg::OnSigninDoneBsfileclnx1(long aCode)
{
  UpdateStatus();
  if (FileCln.get_Signedin())
    ListFolder();
}









// *** listing ***




void CbsFileClnDlg::OnBnClickedbtnlist()
{
  ListFolder();
}



//Request the folder contents
void CbsFileClnDlg::ListFolder(void)
{
  CString S;
  if (m_NowList) return;
  c_stFolder.GetWindowText(S);  
  if (FileCln.ListFolder(S))
  {
    m_NowList = true;
    c_lvFiles.DeleteAllItems();
    c_btnList.EnableWindow(false);
    if (S > "\\")
      OnHaveListItemBsfileclnx1("..", true, 0, 0, 0, 0);
  }
  else
    CheckError(FileCln.get_LastError());
}





//A list item is available
void CbsFileClnDlg::OnHaveListItemBsfileclnx1(LPCTSTR aName, BOOL aFolder, long aSizeLo, long aSizeHi, long aTimeLo, long aTimeHi)
{
  LVITEM lvi;
  CString Sz, Tm;
  FILETIME ft;

  int i = c_lvFiles.GetItemCount();

  lvi.mask =  LVIF_TEXT | LVIF_IMAGE;
  lvi.iItem = i;
  lvi.iSubItem = 0;
  if (aFolder)
  {
    lvi.iImage = 0;
    Sz = "";
  }  
  else
  {
    lvi.iImage = 1;
    Sz.Format("%d", aSizeLo);
  }
  
  lvi.pszText = (LPTSTR)(LPCTSTR) aName;
  c_lvFiles.InsertItem(&lvi);

  
  if (aName == "..")
    Tm = "";
  else
  {
    ft.dwLowDateTime = aTimeLo;
    ft.dwHighDateTime = aTimeHi;
    CTime tm(ft, -1);
    Tm = tm.Format("%m/%d/%Y %H:%M:%S");
  }
  c_lvFiles.SetItemText(i, 1, Sz);
  c_lvFiles.SetItemText(i, 2, Tm);
}




//End of the list
void CbsFileClnDlg::OnListFolderDoneBsfileclnx1(long aCode)
{
  m_NowList = false;
  CheckError (aCode);
  UpdateButtons();
}




void CbsFileClnDlg::GoInFolder(void)
{
  CString S = AddSlash("");
  int i;
  
  i = GetSelectedIndex();
  if (i < 0) return;
  c_stFolder.GetWindowText(S);
  S = AddSlash(S);
  S = S + c_lvFiles.GetItemText(i, 0);
  c_stFolder.SetWindowText(S);
  ListFolder();
}



void CbsFileClnDlg::GoUpFolder(void)
{
  CString S;
  int p;
  
  c_stFolder.GetWindowText(S);
  S = SlashOff(S);
  if (S == "") return;               //nothing to do
  p = S.GetLength();
  while (p >= 0)
  {
    if (S.Mid(p, 1) == "\\") break;
    p = p - 1;
  }
  S = S.Left(p-1);
  S = PreSlash(S);
  c_stFolder.SetWindowText(S);
  ListFolder();
}



void CbsFileClnDlg::OnBnClickedbtnin()
{
  GoInFolder();
}





void CbsFileClnDlg::OnBnClickedbtnup()
{
  GoUpFolder();
}




// *** new folder ***


//Request a new folder
void CbsFileClnDlg::OnBnClickedbtnnew()
{
  CString Nm, Fol;
  if (!GetInputText(Nm, "Create folder", "Folder name:")) return;
  c_stFolder.GetWindowText(Fol);
  Nm = AddSlash(Fol) + Nm;

  if (FileCln.CreateFolder(Nm))
    m_NowCreateFolder = true;
  else
    CheckError(FileCln.get_LastError());
  UpdateButtons();
}




//A new folder is created
void CbsFileClnDlg::OnCreateFolderDoneBsfileclnx1(long aCode)
{
  m_NowCreateFolder = false;
  CheckError(aCode);
  UpdateButtons();
  if (aCode > 0) return;
  ListFolder();
}




// *** renaming ***




//Send rename request
void CbsFileClnDlg::OnBnClickedbtnrename()
{
  CString Nm, Fol;
  if (c_lvFiles.GetSelectedCount() == 0) return;
  if (GetImageIndex(GetSelectedIndex()) == FolderImgIdx) 
  {
    //folder
    if (!GetInputText(Nm, "Rename folder", "New name:")) return;
    Nm = AddSlash(GetCurFol()) + Nm;
    
    if (FileCln.RenameFolder(AddSlash(GetCurFol()) + c_lvFiles.GetItemText(GetSelectedIndex(), 0), Nm))
      m_NowRenameFolder = true;
    else
      CheckError(FileCln.get_LastError());
  }
  else
  {
    //file
    if (!GetInputText(Nm, "Rename file", "New name:")) return;
    Nm = AddSlash(GetCurFol()) + Nm;
    
    if (FileCln.RenameFile(AddSlash(GetCurFol()) + c_lvFiles.GetItemText(GetSelectedIndex(), 0), Nm))
      m_NowRenameFile = true;
    else
      CheckError(FileCln.get_LastError());
  }
  UpdateButtons();
}






//A folder is renamed
void CbsFileClnDlg::OnRenameFolderDoneBsfileclnx1(long aCode)
{
  m_NowRenameFolder = false;
  CheckError(aCode);
  UpdateButtons();
  if (aCode > 0) return;
  ListFolder();
}


//A file is renamed
void CbsFileClnDlg::OnRenameFileDoneBsfileclnx1(long aCode)
{
  m_NowRenameFile = false;
  CheckError(aCode);
  UpdateButtons();
  if (aCode > 0) return;
  ListFolder();
}




// *** deletion ***




//Send delete request
void CbsFileClnDlg::OnBnClickedbtndelete()
{
  CString Nm, Fol;
  if (c_lvFiles.GetSelectedCount() == 0) return;
  if (GetImageIndex(GetSelectedIndex()) == FolderImgIdx) 
  {
    //folder
    if (FileCln.DeleteFolder(AddSlash(GetCurFol()) + c_lvFiles.GetItemText(GetSelectedIndex(), 0)))
      m_NowDeleteFolder = true;
    else
      CheckError(FileCln.get_LastError());
  }
  else
  {
    //file
    if (FileCln.DeleteFile(AddSlash(GetCurFol()) + c_lvFiles.GetItemText(GetSelectedIndex(), 0)))
      m_NowDeleteFile = true;
    else
      CheckError(FileCln.get_LastError());
  }
  UpdateButtons();
}




//A folder is deleted
void CbsFileClnDlg::OnDeleteFolderDoneBsfileclnx1(long aCode)
{
  m_NowDeleteFolder = false;
  CheckError(aCode);
  UpdateButtons();
  if (aCode > 0) return;
  ListFolder();
}


//A file is deleted
void CbsFileClnDlg::OnDeleteFileDoneBsfileclnx1(long aCode)
{
  m_NowDeleteFile = false;
  CheckError(aCode);
  UpdateButtons();
  if (aCode > 0) return;
  ListFolder();
}







// *** Uploading ***



//Send an upload request
void CbsFileClnDlg::OnBnClickedbtnupload()
{
	CFileDialog FileDlg(TRUE, "", NULL, 0, NULL);
	if( FileDlg.DoModal() != IDOK ) return;
  m_UpldFile = FileDlg.GetFileName();

  if (FileCln.Upload(FileDlg.GetPathName(), GetCurFol()))
  {
    m_NowUpload = true;
    m_UpldFol = GetCurFol();
    c_stUpload.SetWindowText(m_UpldFile + ": handshaking");
  }
  else
    CheckError (FileCln.get_LastError());
  
  UpdateStatus();
}



//Abort the upload operation
void CbsFileClnDlg::OnBnClickedbtnabortupld()
{
  if (!FileCln.CancelUpload())
    CheckError(FileCln.get_LastError());
}



//Upload progress info
void CbsFileClnDlg::OnUploadProgressBsfileclnx1(long aCountLo, long aCountHi, long aSizeLo, long aSizeHi)
{
  CString S;
  S.Format("%s - %d/%d", m_UpldFile, aCountLo, aSizeLo);
  c_stUpload.SetWindowText(S);
}



//The upload is completed
void CbsFileClnDlg::OnUploadDoneBsfileclnx1(long aCode)
{
  if (aCode == 0)
    c_stUpload.SetWindowText(m_UpldFile + " - Done.");
  else
    c_stUpload.SetWindowText(m_UpldFile + " - Aborted: " + ErrorText(aCode));
  m_NowUpload = false;
  UpdateButtons();
  if (m_UpldFol == GetCurFol())
    ListFolder();
}









// *** downloading ***


CString ExtractFilePath(CString aPath)
{
    int P;
    P = aPath.ReverseFind('\\');
    return aPath.Left(P+1);
}

//Send a download request
void CbsFileClnDlg::GoDownload(void)
{
  CString Nm, Fol;
  if (c_lvFiles.GetSelectedCount() == 0) return;
  if (GetImageIndex(GetSelectedIndex()) == FolderImgIdx) return;
  Nm = c_lvFiles.GetItemText(GetSelectedIndex(), 0);

	CFileDialog FileDlg(FALSE, "", Nm, 0, NULL);
	if( FileDlg.DoModal() != IDOK ) return;
  m_DnldFile = Nm;

  if (FileCln.Download(AddSlash(GetCurFol()) + Nm, ExtractFilePath(FileDlg.GetPathName())))
  {
    m_NowDownload = true;
    c_stDownload.SetWindowText(m_DnldFile + ": handshaking");
  }
  else
    CheckError (FileCln.get_LastError());
  UpdateStatus();
}



void CbsFileClnDlg::OnBnClickedbtndownload()
{
  GoDownload();
}


//Abort the download operation
void CbsFileClnDlg::OnBnClickedbtnabortdnld()
{
  if (!FileCln.CancelDownload())
    CheckError(FileCln.get_LastError());
}



//The download operation is completed
void CbsFileClnDlg::OnDownloadDoneBsfileclnx1(long aCode)
{
  if (aCode == 0)
    c_stDownload.SetWindowText(m_DnldFile + " - Done.");
  else
    c_stDownload.SetWindowText(m_DnldFile + " - Aborted: " + ErrorText(aCode));
  m_NowDownload = false;
  UpdateButtons();
}


//Download progress info
void CbsFileClnDlg::OnDownloadProgressBsfileclnx1(long aCountLo, long aCountHi, long aSizeLo, long aSizeHi)
{
  CString S;
  S.Format("%s - %d/%d", m_DnldFile, aCountLo, aSizeLo);
  c_stDownload.SetWindowText(S);
}




(c) BigSpeed Computing Inc. - Secure private networking