// RemoteListCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "ConsoleApp.h"
#include "RemoteListCtrl.h"
#include "Struct.h"

#include "MainFrm.h"
#include "WebDlg.h"
#include "RemoteView.h"
#include "LocalView.h"
#include ".\remotelistctrl.h"
#include ".\distinfodlg.h"
#include "NoticeTrans.h"

#include "ne_socket.h"
#include "ne_uri.h"
#include "ne_request.h"
#include "ne_xml.h"
#include "ne_string.h"

typedef struct COMMLIB_HANDLE {
	ne_session*  pnsess;
	ne_uri       nuri;
	string       sUserId;
	string       sPassword;
	string       sDavAuth;
	DWORD        dwError;
	string       sError;

	// 업/다운 관련
	HANDLE       hThread;
	HANDLE       hWaitEvent;
	UINT32       uJob;
	string       sJobParam;
	cl_callback  proc;
	void*        pParam;
	HANDLE       hTranFile;
	__int64		 nExFileSize;
	__int64      iTranPos;
	__int64      nFileSize;
	DWORD        dwOldTick;
	int          nTranStop;
	string       sRedirect;
	string		 sDomain2;

	set<PCLSERVICE_STRUCT>  setServiceList;
	set<PCLPATHINFO_STRUCT> setPathInfoList;
	set<PCLFILE_STRUCT>     setFileList;
} COMMLIB_STRUCT, *PCOMMLIB_STRUCT;

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


int KrCompare(LPCTSTR pszText1, LPCTSTR pszText2);
bool IsPathItself(CString sSrcPath, CString sDstPath);

/////////////////////////////////////////////////////////////////////////////
// CRemoteListCtrl

CRemoteListCtrl::CRemoteListCtrl()
{
	m_nStyle = 4;

	m_nSortColumn = 0;
	m_nSortDirect = -1;
}

CRemoteListCtrl::~CRemoteListCtrl()
{
}


BEGIN_MESSAGE_MAP(CRemoteListCtrl, CListCtrl)
	//{{AFX_MSG_MAP(CRemoteListCtrl)
	//}}AFX_MSG_MAP
	ON_WM_CREATE()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_CONTEXTMENU()
	ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBegindrag)
	ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
	ON_COMMAND(ID_LIST_NEWFOLDER, OnListNewfolder)
	ON_COMMAND(ID_LIST_REFRESH, OnListRefresh)
	ON_COMMAND(ID_LIST_TYPE1, OnListType1)
	ON_COMMAND(ID_LIST_TYPE2, OnListType2)
	ON_COMMAND(ID_LIST_TYPE3, OnListType3)
	ON_COMMAND(ID_LIST_TYPE4, OnListType4)
	ON_COMMAND(ID_LIST_SORT1, OnListSort1)
	ON_COMMAND(ID_LIST_SORT2, OnListSort2)
	ON_COMMAND(ID_LIST_SORT3, OnListSort3)
	ON_COMMAND(ID_LIST_SORT4, OnListSort4)
	ON_COMMAND(ID_LIST_PASTE, OnListPaste)
	ON_COMMAND(ID_LIST_ALL, OnListAll)
	ON_COMMAND(ID_LISTFILE_DOWNLOAD, OnListfileDownload)
	ON_COMMAND(ID_LISTFILE_SAVEURL, OnListfileSaveUrl)
	ON_COMMAND(ID_LISTFILE_CUT, OnListfileCut)
	ON_COMMAND(ID_LISTFILE_COPY, OnListfileCopy)
	ON_COMMAND(ID_LISTFILE_PASTE, OnListfilePaste)
	ON_COMMAND(ID_LISTFILE_DELETE, OnListfileDelete)
	ON_COMMAND(ID_LISTFILE_RENAME, OnListfileRename)
	ON_COMMAND(ID_LIST_VIEW_DISTSTATUS, OnListViewDiststatus)
	ON_COMMAND(ID_LIST_DIST_AGAIN, OnListDistAgain)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRemoteListCtrl message handlers

BOOL CRemoteListCtrl::PreTranslateMessage(MSG* pMsg)
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	switch (pMsg->message) {
	case WM_SETFOCUS:
	case WM_LBUTTONDOWN:
	case WM_RBUTTONDOWN:
		pmf->SetFocus(this);
		break;
	case WM_KEYDOWN:
		CEdit *edit = GetEditControl();
		if (edit) {
			if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE) {
				edit->SendMessage(WM_KEYDOWN, pMsg->wParam, pMsg->lParam);
				return TRUE;
			}
		}
		else if (pMsg->wParam == VK_F2) {
			for (int i = 0; i < GetItemCount(); i++) {
				if (LVIS_FOCUSED & GetItemState(i, LVIF_TEXT)) {
					EditLabel(i);
					break;
				}
			}
			return TRUE;
		}
		else if (pMsg->wParam == VK_DELETE) {
			((CRemoteView*)GetParent())->DeleteListItem();
			return TRUE;
		}
		else if (GetKeyState(VK_CONTROL) & 128) {
			if (pMsg->wParam == VK_A) {
				CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

				pmf->m_pRemoteView->m_bUpdating = TRUE;
				for (int i = 0; i < GetItemCount(); i++)
					SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
				pmf->m_pRemoteView->m_bUpdating = FALSE;

				pmf->m_pRemoteView->ShowStatusObject(TRUE, FALSE, TRUE);
				return TRUE;
			}
			else if (pMsg->wParam == VK_C) {
				SetClipboard(CBS_COPY);
				return TRUE;
			}
			else if (pMsg->wParam == VK_X) {
				SetClipboard(CBS_CUT);
				return TRUE;
			}
			else if (pMsg->wParam == VK_V) {
				PasteClipboard();
				return TRUE;
			}
			else if (pMsg->wParam == VK_D) {
				OnListfileDelete();
				return TRUE;
			}
		}
	}

	return CListCtrl::PreTranslateMessage(pMsg);
}

int CRemoteListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
//	if (CListCtrl::OnCreate(lpCreateStruct) == -1)
//		return -1;

	m_dropTarget.Register(this);

	int anWidths[7+1]={270, 130,110, 110, 150, 54, 90, 90};

	CString sWidths = COptions::GetOption(OPTION_REMOTECOLUMNWIDTHS);
	if (sWidths != "")
	{
		int pos=-1;
		int i;
		for (i=0; i<6+1; i++)
		{
			int oldpos = pos + 1;
			pos = sWidths.Find(_T(" "), oldpos);
			if (pos == -1)
				break;
			sWidths.SetAt(pos, 0);
			int size = _ttoi(sWidths.Mid(oldpos));
			if (size > 0)
				anWidths[i] = size;
		}
		if (i == 6+1)
		{
			int size = _ttoi(sWidths.Mid(pos+1));
			if (size > 0)
				anWidths[i] = size;
		}
	}

	m_asHeader[0] = "이름";
	m_asHeader[1] = "배포현황 ( 완료/전체 )";
	m_asHeader[2] = "크기";
	m_asHeader[3] = "종류";
	m_asHeader[4] = "수정일자";

	m_anHeaderAlign[0] = LVCFMT_LEFT;
	m_anHeaderAlign[1] = LVCFMT_LEFT;
	m_anHeaderAlign[2] = LVCFMT_RIGHT;
	m_anHeaderAlign[3] = LVCFMT_LEFT;
	m_anHeaderAlign[4] = LVCFMT_LEFT;

	for (int i = 0; i < 4+1; i++)
		InsertColumn(i, m_asHeader[i], m_anHeaderAlign[i], anWidths[i]);

	m_ilSort.Create(9, 9, ILC_MASK, 3, 3);
	HICON hicon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_SORT_NONE));
	m_ilSort.Add(hicon);
	hicon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_SORT_ASC));
	m_ilSort.Add(hicon);
	hicon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_SORT_DESC));
	m_ilSort.Add(hicon);
	m_ilSort.SetBkColor(CLR_NONE);

	SetListStyle(m_nStyle);

	SetExtendedStyle(LVS_EX_INFOTIP);
	SetCallbackMask(GetCallbackMask() | LVIS_OVERLAYMASK); 

	return 0;
}

BOOL CRemoteListCtrl::OnEraseBkgnd(CDC* pDC)
{
//	return TRUE;
	return CListCtrl::OnEraseBkgnd(pDC);
}

void CRemoteListCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	int iItem = HitTest(point);
	if (iItem < 0)
		return;

	if (((PRLISTITEM_STRUCT)GetItemData(iItem))->bFolder) {
		CRemoteView* pview = (CRemoteView*)GetParent();

		CString sName = GetItemText(iItem, 0);

		HTREEITEM hti = pview->m_tree.GetSelectedItem();
		hti = pview->m_tree.GetChildItem(hti);
		while (hti) {
			if (sName == pview->m_tree.GetItemText(hti)) {
				pview->m_tree.SelectItem(hti);
				return;
			}

			hti = pview->m_tree.GetNextSiblingItem(hti);
		}
	}

//	CListCtrl::OnLButtonDblClk(nFlags, point);
}

void CRemoteListCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
	CRemoteTreeCtrl* ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();
	if (!ptree->GetRootItem())
		return;

	CPoint ptHit = point;

	ScreenToClient(&ptHit);

	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	HTREEITEM htiRoot     = pmf->m_pRemoteView->m_tree.GetRootItem();
	HTREEITEM htiSelected = pmf->m_pRemoteView->m_tree.GetSelectedItem();
	HTREEITEM htiParent   = pmf->m_pRemoteView->m_tree.GetParentItem(htiSelected);

	CMenu menu, *pmenu;
	if ((m_iSelectedItem = HitTest(ptHit)) >= 0) {
		menu.LoadMenu(IDR_POP_RLISTFILE);
		pmenu = menu.GetSubMenu(0);
		ASSERT(pmenu != NULL);

		if (htiSelected == htiRoot)
			return;

		pmenu->SetMenuItemBitmaps(ID_LISTFILE_CUT, MF_BYCOMMAND, &pmf->m_bmCut, &pmf->m_bmCut);
		pmenu->SetMenuItemBitmaps(ID_LISTFILE_COPY, MF_BYCOMMAND, &pmf->m_bmCopy, &pmf->m_bmCopy);
		pmenu->SetMenuItemBitmaps(ID_LISTFILE_PASTE, MF_BYCOMMAND, &pmf->m_bmPaste, &pmf->m_bmPaste);

		if (((PRLISTITEM_STRUCT)GetItemData(m_iSelectedItem))->bFolder) {
			COleDataObject odo;

			if (!(odo.AttachClipboard() && odo.IsDataAvailable(CF_HDROP))
				&& !(pmf->m_cbs != CBS_NONE && pmf->m_saDragSource.GetCount() > 0))
				pmenu->EnableMenuItem(ID_LISTFILE_PASTE, MF_GRAYED);
		}
		else
			pmenu->DeleteMenu(ID_LISTFILE_PASTE, MF_BYCOMMAND);

		if (((PRTREEITEM_STRUCT)ptree->GetItemData(htiSelected))->nClass == TNS_FOLDER) {
			HTREEITEM hti = ptree->GetParentItem(htiSelected);
			while (((PRTREEITEM_STRUCT)ptree->GetItemData(hti))->nClass == TNS_FOLDER) {
				htiSelected = hti;
				hti = ptree->GetParentItem(hti);
			}

			if (((PRTREEITEM_STRUCT)ptree->GetItemData(htiSelected))->saAttr[1] != "1")
				pmenu->EnableMenuItem(ID_LISTFILE_SAVEURL, MF_GRAYED);
		}
		else
			pmenu->EnableMenuItem(ID_LISTFILE_SAVEURL, MF_GRAYED);
	}
	else {
		menu.LoadMenu(IDR_POP_RLIST);
		pmenu = menu.GetSubMenu(0);
		ASSERT(pmenu != NULL);

		pmenu->SetMenuItemBitmaps(ID_LIST_NEWFOLDER, MF_BYCOMMAND, &pmf->m_bmFolder, &pmf->m_bmFolder);
		pmenu->SetMenuItemBitmaps(ID_LIST_PASTE, MF_BYCOMMAND, &pmf->m_bmPaste, &pmf->m_bmPaste);

		switch (pmf->m_nRemoteListType) {
		case 1:
			pmenu->CheckMenuItem(ID_LIST_TYPE1, MF_CHECKED);
			break;
		case 2:
			pmenu->CheckMenuItem(ID_LIST_TYPE2, MF_CHECKED);
			break;
		case 3:
			pmenu->CheckMenuItem(ID_LIST_TYPE3, MF_CHECKED);
			break;
		case 4:
			pmenu->CheckMenuItem(ID_LIST_TYPE4, MF_CHECKED);
			break;
		}

		switch (m_nSortColumn) {
		case 0:
			pmenu->CheckMenuItem(ID_LIST_SORT1, MF_CHECKED);
			break;
		case 1:
			pmenu->CheckMenuItem(ID_LIST_SORT2, MF_CHECKED);
			break;
		case 2:
			pmenu->CheckMenuItem(ID_LIST_SORT3, MF_CHECKED);
			break;
		case 3:
			pmenu->CheckMenuItem(ID_LIST_SORT4, MF_CHECKED);
			break;
		}

		if (htiSelected == htiRoot) {
			pmenu->EnableMenuItem(ID_LIST_NEWFOLDER, MF_GRAYED);
			pmenu->EnableMenuItem(ID_LIST_ALL, MF_GRAYED);
			pmenu->EnableMenuItem(ID_LIST_PASTE, MF_GRAYED);
		}
		else {
			COleDataObject odo;

			if (!(odo.AttachClipboard() && odo.IsDataAvailable(CF_HDROP))
				&& !(pmf->m_cbs != CBS_NONE && pmf->m_saDragSource.GetCount() > 0))
				pmenu->EnableMenuItem(ID_LIST_PASTE, MF_GRAYED);
		}
	}

	pmenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}

void CRemoteListCtrl::OnBegindrag(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

	CRemoteTreeCtrl* ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();

	POSITION pos = GetFirstSelectedItemPosition();
	if (ptree->GetSelectedItem() == ptree->GetRootItem() || !pos)
		return;

	CString sPath = ptree->GetPath(ptree->GetSelectedItem()) + "/";

	CMainFrame* pmf = (CMainFrame*)AfxGetMainWnd();
	pmf->m_bDragFromRemote = TRUE;
	pmf->m_htiDragTarget   = ptree->GetVolumeItem(ptree->GetSelectedItem());
	pmf->m_saDragSource.RemoveAll();
	while (pos)
	{
		int iItem = GetNextSelectedItem(pos);
		pmf->m_saDragSource.Add(sPath + GetItemText(iItem, 0));
	}

	pmf->m_pLocalView->StartDrag();

	COleDataSource ods;
	DROPEFFECT de = ods.DoDragDrop(DROPEFFECT_COPY | DROPEFFECT_MOVE, NULL);

	pmf->m_pLocalView->EndDrag();

	pmf->m_bDragFromRemote = FALSE;

	*pResult = 0;
}

void CRemoteListCtrl::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	Sort(pNMListView->iSubItem);
	
	*pResult = 0;
}

void CRemoteListCtrl::OnListNewfolder()
{
	SetFocus();

	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->m_pRemoteView->MakeNewFolder();
}

void CRemoteListCtrl::OnListRefresh() 
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->m_pRemoteView->RefreshItem();
}

void CRemoteListCtrl::OnListType1() 
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->OnViewRlistType1();
}

void CRemoteListCtrl::OnListType2() 
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->OnViewRlistType2();
}

void CRemoteListCtrl::OnListType3() 
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->OnViewRlistType3();
}

void CRemoteListCtrl::OnListType4() 
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->OnViewRlistType4();
}

void CRemoteListCtrl::OnListSort1() 
{
	Sort(0);
}

void CRemoteListCtrl::OnListSort2() 
{
	Sort(1);
}

void CRemoteListCtrl::OnListSort3() 
{
	Sort(2);
}

void CRemoteListCtrl::OnListSort4() 
{
	Sort(3);
}

void CRemoteListCtrl::OnListPaste() 
{
	PasteClipboard();
}

void CRemoteListCtrl::OnListAll() 
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->m_pRemoteView->m_bUpdating = TRUE;
	for (int i = 0; i < GetItemCount(); i++)
		SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
	pmf->m_pRemoteView->m_bUpdating = FALSE;
	pmf->m_pRemoteView->ShowStatusObject(TRUE, FALSE, TRUE);
}

void CRemoteListCtrl::OnListfileDownload()
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->Download();
}

void CRemoteListCtrl::OnListfileSaveUrl()
{
/*
	if (((CRemoteView*)GetParent())->m_nSelectedFiles == 0) {
		AfxMessageBox("URL로 저장할 파일을 선택하십시오. URL 저장은 파일만 가능합니다.");
		return;
	}
*/

	((CMainFrame*)AfxGetMainWnd())->SaveUrl();
}

void CRemoteListCtrl::OnListfileCut()
{
	SetClipboard(CBS_CUT);
}

void CRemoteListCtrl::OnListfileCopy()
{
	SetClipboard(CBS_COPY);
}

void CRemoteListCtrl::OnListfilePaste()
{
	//TODO...
	PasteClipboard(GetItemText(m_iSelectedItem, 0));
}

void CRemoteListCtrl::OnListfileDelete()
{
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->m_pRemoteView->DeleteListItem();
}

void CRemoteListCtrl::OnListfileRename()
{
	EditLabel(m_iSelectedItem);
}

BOOL CRemoteListCtrl::SetItemText(int nItem, int nSubItem, LPCTSTR lpszText)
{
	if (nSubItem == 0) {
		PRLISTITEM_STRUCT prli = (PRLISTITEM_STRUCT)GetItemData(nItem);
		if (prli) {
			CString sText = lpszText;

			prli->sName     = sText;
			prli->saAttr[0] = sText.MakeUpper();
		}
	}

	return CListCtrl::SetItemText(nItem, nSubItem, lpszText);
}

void CRemoteListCtrl::SetListStyle(int nStyle)
{
	UINT nSHOption = SHGFI_ICON | SHGFI_SYSICONINDEX;
	int  nLVOption;

	if (nStyle == 1) {
		m_nStyle  = LVS_ICON;
		nSHOption |= SHGFI_LARGEICON;
		nLVOption = LVSIL_NORMAL;
	}
	else if (nStyle == 2) {
		m_nStyle  = LVS_SMALLICON;
		nSHOption |= SHGFI_SMALLICON;
		nLVOption = LVSIL_SMALL;
	}
	else if (nStyle == 3) {
		m_nStyle  = LVS_LIST;
		nSHOption |= SHGFI_SMALLICON;
		nLVOption = LVSIL_SMALL;
	}
	else if (nStyle == 4) {
		m_nStyle  = LVS_REPORT;
		nSHOption |= SHGFI_SMALLICON;
		nLVOption = LVSIL_SMALL;
	}

	int nRemove = ~m_nStyle & (LVS_REPORT | LVS_ICON | LVS_SMALLICON | LVS_LIST);
	ModifyStyle(nRemove, m_nStyle, SWP_NOZORDER);

    SHFILEINFO shfi;
	HIMAGELIST hil = (HIMAGELIST)SHGetFileInfo("", 0, &shfi, sizeof(shfi), nSHOption);
	SetImageList(CImageList::FromHandle(hil), nLVOption);

	Arrange(LVA_DEFAULT);

	Sort();
}

void CRemoteListCtrl::SetClipboard(CLIPBOARD_STATE cbs)
{
	if (OpenClipboard()) {
		EmptyClipboard();
		CloseClipboard();
	}

	POSITION pos = GetFirstSelectedItemPosition();
	if (!pos)
		return;

	CRemoteTreeCtrl* ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();

	HTREEITEM hti = ptree->GetSelectedItem();
	if (hti == ptree->GetRootItem())
		return;

	CString sPath = ptree->GetPath(hti);

	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	pmf->m_cbs           = cbs;
	pmf->m_htiDragTarget = ptree->GetVolumeItem(hti);
	pmf->m_saDragSource.RemoveAll();
	while (pos) {
		int iItem = GetNextSelectedItem(pos);
		pmf->m_saDragSource.Add(sPath + "/" + GetItemText(iItem, 0));
	}
}

void CRemoteListCtrl::PasteClipboard(CString sFolder)
{
	if (sFolder != "")
		sFolder = "/" + sFolder;

	CRemoteTreeCtrl* ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();

	HTREEITEM htiTarget = ptree->GetSelectedItem();
	if (htiTarget == ptree->GetRootItem())
		return;

	COleDataObject odo;

	if (odo.AttachClipboard() && odo.IsDataAvailable(CF_HDROP)) {
		STGMEDIUM sm;
		FORMATETC fe = {CF_HDROP, (DVTARGETDEVICE FAR*)NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};

		if (!odo.GetData(CF_HDROP, &sm, &fe))
			return;

		HDROP hdrop = (HDROP)sm.hGlobal;

		int nDragCount = ::DragQueryFile(hdrop, 0xFFFFFFFF, NULL, 0);
		if (nDragCount == 0)
			return;

		CMainFrame* pmf = (CMainFrame*)AfxGetMainWnd();

		pmf->m_saDragSource.RemoveAll();

		char szFile[MAX_PATH];
		for (int i = 0; i < nDragCount; i++) {
			::DragQueryFile(hdrop, i, szFile, sizeof(szFile));

			CString sFile(szFile);
			if (sFile.Find('\\') < 0) {
				AfxMessageBox("시스템 파일은 업로드 할 수 없습니다.");
				pmf->m_saDragSource.RemoveAll();
				break;
			}

			pmf->m_saDragSource.Add(sFile);
		}

		if (pmf->m_saDragSource.GetCount() == 0)
			return;

		pmf->m_sDragTarget = ptree->GetPath(htiTarget) + sFolder;
		pmf->m_htiDragTarget   = ptree->GetVolumeItem(htiTarget);

		pmf->Upload(false);
	}
	else {
		CMainFrame* pmf = (CMainFrame*)AfxGetMainWnd();

		if (pmf->m_cbs == CBS_NONE)
			return;

		if (pmf->m_saDragSource.GetCount() == 0)
			return;

		if (ptree->GetVolumeItem(htiTarget) != pmf->m_htiDragTarget) {
			AfxMessageBox("서로 다른 서비스사이에는 파일을 복사 또는 이동 할 수 없습니다.");
			return;
		}

		pmf->m_sDragTarget = ptree->GetPath(htiTarget) + sFolder;
		for (int i = 0; i < pmf->m_saDragSource.GetCount(); i++) {
			if (IsPathItself(pmf->m_saDragSource[i], pmf->m_sDragTarget)) {
				AfxMessageBox("자기 자신의 하위 폴더로 복사 또는 이동 할 수 없습니다.");
				return;
			}
		}

		pmf->BeginWaitMessage();

		CStringArray saSucceed;
		if (pmf->m_cbs == CBS_COPY)
			pmf->CopyRemoteObject(pmf->m_htiDragTarget, pmf->m_saDragSource, pmf->m_sDragTarget, saSucceed);
		else
			pmf->MoveRemoteObject(pmf->m_htiDragTarget, pmf->m_saDragSource, pmf->m_sDragTarget, saSucceed);

		CRemoteView* pview = (CRemoteView*)GetParent();
		for (int i = 0; i < saSucceed.GetCount(); i++) {
			HTREEITEM hti = ptree->GetItem(pmf->m_htiDragTarget, saSucceed[i]);
			if (!hti)
				continue;

			CString sName = saSucceed[i].Mid(saSucceed[i].ReverseFind('/') + 1);
			if (!ptree->IsExistsChild(htiTarget, sName))
				pview->AppendObject(htiTarget, true, sName, 0, "");
			if (pmf->m_cbs == CBS_CUT)
				ptree->DeleteItem(hti);
		}

		pmf->EndWaitMessage();

		if (saSucceed.GetCount() == 0)
			return;

		pmf->m_pRemoteView->RefreshItem();
	}
}

void CRemoteListCtrl::Sort(int nColumn)
{
	HDITEM *phdi = new HDITEM;

	if (nColumn >= 0) {
		if (m_nSortColumn != nColumn) {
			phdi->pszText    = m_asHeader[m_nSortColumn].GetBuffer(0);
			phdi->cchTextMax = 0;
			phdi->mask       = HDI_TEXT | HDI_IMAGE | HDI_FORMAT;
			phdi->fmt        = m_anHeaderAlign[m_nSortColumn] | HDF_IMAGE | HDF_STRING | HDF_BITMAP_ON_RIGHT;
			phdi->iImage     = 0;
			GetHeaderCtrl()->SetItem(m_nSortColumn, phdi);
		}
		else
			m_nSortDirect = -m_nSortDirect;

		m_nSortColumn = nColumn;
	}

	GetHeaderCtrl()->SetImageList(&m_ilSort);

	phdi->pszText    = m_asHeader[m_nSortColumn].GetBuffer(0);
	phdi->cchTextMax = 0;
	phdi->mask       = HDI_TEXT | HDI_IMAGE | HDI_FORMAT;
	phdi->fmt        = m_anHeaderAlign[m_nSortColumn] | HDF_IMAGE | HDF_STRING | HDF_BITMAP_ON_RIGHT;
	phdi->iImage     = (m_nSortDirect > 0 ? 1 : 2);
	GetHeaderCtrl()->SetItem(m_nSortColumn, phdi);

	delete phdi;

	SortItems(SortProc, (LPARAM)this);
}

int CRemoteListCtrl::SortProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	PRLISTITEM_STRUCT prli1 = (PRLISTITEM_STRUCT)lParam1;
	PRLISTITEM_STRUCT prli2 = (PRLISTITEM_STRUCT)lParam2;

	CRemoteListCtrl *plist = (CRemoteListCtrl*)lParamSort;

	CString sText1 = (prli1->bFolder ? "0" : "1");
	CString sText2 = (prli2->bFolder ? "0" : "1");
	switch (plist->m_nSortColumn) {
	case 0:// 파일명
		sText1 += prli1->saAttr[0];
		sText2 += prli2->saAttr[0];
		break;
	case 1: // 사이즈 정렬
		if (!prli1->bFolder && prli2->bFolder)
			return -1 * (-plist->m_nSortDirect);
		if (prli1->bFolder)
			return 1 * (-plist->m_nSortDirect);
		if (prli1->bFolder && prli2->bFolder)
			return 0;

		if (prli1->nSize > prli2->nSize)
			return 1 * (-plist->m_nSortDirect);
		if (prli1->nSize < prli2->nSize)
			return -1 * (-plist->m_nSortDirect);
		if (prli1->nSize == prli2->nSize)
			return 0;
		break;
	case 2: // 종류
		sText1 += prli1->saAttr[2];
		sText2 += prli2->saAttr[2];
		break;
	case 3: // 날짜
		sText1 += prli1->saAttr[plist->m_nSortColumn];
		sText2 += prli2->saAttr[plist->m_nSortColumn];
		break;
	default:
		sText1 += prli1->saAttr[plist->m_nSortColumn] + prli1->saAttr[0];
		sText2 += prli2->saAttr[plist->m_nSortColumn] + prli2->saAttr[0];
		break;
	}

	return (KrCompare(sText1, sText2) * (-plist->m_nSortDirect));
}


void CRemoteListCtrl::OnListViewDiststatus()
{
	CString sPath;
	CMainFrame* pmf = (CMainFrame*)AfxGetMainWnd();

	CRemoteTreeCtrl *ptree = pmf->m_pRemoteView->GetTreeCtrl();

	HTREEITEM htiSelected = ptree->GetSelectedItem();

	CString sBasePath = ptree->GetPath(htiSelected) + "/";

	PRLISTITEM_STRUCT prli = (PRLISTITEM_STRUCT)GetItemData(m_iSelectedItem);

	//HTREEITEM         htiVolume  = bUseSelected ? ptree->GetVolumeItem(ptree->GetSelectedItem()) :m_htiDragTarget;
	HTREEITEM         htiVolume  = ptree->GetVolumeItem(ptree->GetSelectedItem());
	PRTREEITEM_STRUCT prtiVolume = (PRTREEITEM_STRUCT)ptree->GetItemData(htiVolume);
	
	CString sCp = ptree->GetItemText(htiVolume);
	CString sUser = pmf->m_sUserId;
	CString sPasswd = pmf->m_sPassword;
	

	
	if(prli)
	{
		sPath = sBasePath + prli->sName;

		if (prli->bFolder)
		{
			HCOMMLIB  t_hcl       = (HCOMMLIB)pmf->m_mapHcl[htiVolume];
			PCOMMLIB_STRUCT t_pcl = (PCOMMLIB_STRUCT)t_hcl;
			
			CDistInfoDlg		dlg;
			
			dlg.m_dist_path = sPath;
			dlg.m_cp = sCp;
			dlg.m_user = sUser;
			dlg.m_passwd = sPasswd;
			dlg.m_RCTSAddress = t_pcl->sDomain2.c_str();
			dlg.DoModal();
            
		}

		else
		{
			CWebDlg*	pdlg;
			pdlg = new CWebDlg();
			pdlg->Create(IDD_WEB_DLG,GetDesktopWindow( ));
			pdlg->ShowWindow(TRUE);
			pdlg->GetPage(::MAN_DISTINFO,sCp,sUser,sPath);
		}
	}
	else
		return;	

}
void CRemoteListCtrl::OnListDistAgain()
{
	POSITION pos = GetFirstSelectedItemPosition();
	if (!pos)
		return;

	CRemoteTreeCtrl* ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();

//	HTREEITEM hti = ptree->GetSelectedItem();

	HTREEITEM htiVolume  = ptree->GetVolumeItem(ptree->GetSelectedItem());
	HTREEITEM htiFolder = ptree->GetSelectedItem();

	if (htiVolume == ptree->GetRootItem())
		return;

	CString sPath = ptree->GetPath(htiFolder);
	CMainFrame *pmf = (CMainFrame *)AfxGetMainWnd();

	CArray<tsvc_da_contents_t*>* pArrDac = new CArray<tsvc_da_contents_t*>();

	while (pos) {
		int iItem = GetNextSelectedItem(pos);

		CString file_name = sPath + "/" + GetItemText(iItem, 0);
		PRLISTITEM_STRUCT prli = (PRLISTITEM_STRUCT)GetItemData(iItem);
		
		tsvc_da_contents_t* pDac = new tsvc_da_contents_t;
		pDac->bFolder = prli->bFolder;
		strncpy(pDac->file_name , file_name , MAX_SIZE_OF_URI);
		
		pArrDac->Add(pDac);

	}

	if (pArrDac->GetCount()>0)
	{
		
		CNoticeTrans dlg(pArrDac);

		HCOMMLIB  t_hcl       = (HCOMMLIB)pmf->m_mapHcl[htiVolume];
		PCOMMLIB_STRUCT t_pcl = (PCOMMLIB_STRUCT)t_hcl;
		CString szTransGuid;
		pmf->GetNewGUID(szTransGuid);

		dlg.m_cp = ptree->GetItemText(htiVolume);
		dlg.m_user = pmf->m_sUserId;
		dlg.m_passwd = pmf->m_sPassword;
		dlg.m_RCTSAddress = t_pcl->sDomain2.c_str();
		dlg.m_sp_tid_str = szTransGuid;

		dlg.DoModal();

		for (int i=0; i < pArrDac->GetCount(); i++)
			delete pArrDac->GetAt(i);

	}

	delete pArrDac;

#if 0

	CString sPath;
	CMainFrame* pmf = (CMainFrame*)AfxGetMainWnd();

	CRemoteTreeCtrl *ptree = pmf->m_pRemoteView->GetTreeCtrl();

	HTREEITEM htiSelected = ptree->GetSelectedItem();

	CString sBasePath = ptree->GetPath(htiSelected) + "/";

	PRLISTITEM_STRUCT prli = (PRLISTITEM_STRUCT)GetItemData(m_iSelectedItem);

	//HTREEITEM         htiVolume  = bUseSelected ? ptree->GetVolumeItem(ptree->GetSelectedItem()) :m_htiDragTarget;
	HTREEITEM         htiVolume  = ptree->GetVolumeItem(ptree->GetSelectedItem());
	PRTREEITEM_STRUCT prtiVolume = (PRTREEITEM_STRUCT)ptree->GetItemData(htiVolume);

	CString sCp = ptree->GetItemText(htiVolume);
	CString sUser = pmf->m_sUserId;
	CString sPasswd = pmf->m_sPassword;

	if(prli)
	{
		sPath = sBasePath + prli->sName;

		if (prli->bFolder)
		{

/*
			HCOMMLIB  t_hcl       = (HCOMMLIB)pmf->m_mapHcl[htiVolume];
			PCOMMLIB_STRUCT t_pcl = (PCOMMLIB_STRUCT)t_hcl;

			CDistInfoDlg		dlg;

			dlg.m_dist_path = sPath;
			dlg.m_cp = sCp;
			dlg.m_user = sUser;
			dlg.m_passwd = sPasswd;
			dlg.m_RCTSAddress = t_pcl->sDomain2.c_str();
			dlg.DoModal();
*/
		}

		else
		{
			PTRANPARAM_STRUCT ptp = new TRANPARAM_STRUCT;
			//ptp->szRCTSAddress;
			

//			CNoticeTrans dlg(ptp , sPath);
//			dlg.DoModal();
		}
	}
	else
		return;	

#endif

}