
/*	Copyright (C) G. Jespersen
	See Licence.txt.*/

// ImportDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dslavsplit.h"
#include "ImportDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CImportDlg dialog


CImportDlg::CImportDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CImportDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CImportDlg)
	m_bCheckDat = FALSE;
	m_bCheckM3u = FALSE;
	m_bCheckMrf = FALSE;
	m_bCheckOverwrite = FALSE;
	m_bCheckPls = FALSE;
	m_bCheckTopic = FALSE;
	m_editPathStr = _T("");
	m_editTopicStr = _T("");
	//}}AFX_DATA_INIT
}


void CImportDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CImportDlg)
	DDX_Control(pDX, IDC_EDIT_PATH, m_ctrlEditPath);
	DDX_Control(pDX, IDC_EDIT_TOPIC, m_ctrlEditTopic);
	DDX_Control(pDX, IDC_CHECK_TOPIC, m_ctrlCheckTopic);
	DDX_Control(pDX, IDC_CHECK_OVERWRITE, m_ctrlCheckOverwrite);
	DDX_Control(pDX, IDC_CHECK_PLS, m_ctrlCheckPls);
	DDX_Control(pDX, IDC_CHECK_MRF, m_ctrlCheckMrf);
	DDX_Control(pDX, IDC_CHECK_M3U, m_ctrlCheckM3u);
	DDX_Control(pDX, IDC_CHECK_DAT, m_ctrlCheckDat);
	DDX_Text(pDX, IDC_EDIT_PATH, m_editPathStr);
	DDX_Text(pDX, IDC_EDIT_TOPIC, m_editTopicStr);
	DDX_Check(pDX, IDC_CHECK_DAT, m_bCheckDat);
	DDX_Check(pDX, IDC_CHECK_M3U, m_bCheckM3u);
	DDX_Check(pDX, IDC_CHECK_MRF, m_bCheckMrf);
	DDX_Check(pDX, IDC_CHECK_OVERWRITE, m_bCheckOverwrite);
	DDX_Check(pDX, IDC_CHECK_PLS, m_bCheckPls);
	DDX_Check(pDX, IDC_CHECK_TOPIC, m_bCheckTopic);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CImportDlg, CDialog)
	//{{AFX_MSG_MAP(CImportDlg)
	ON_WM_SIZE()
	ON_WM_VSCROLL()
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_BN_CLICKED(IDC_CHECK_DAT, OnCheckDat)
	ON_BN_CLICKED(IDC_CHECK_M3U, OnCheckM3u)
	ON_BN_CLICKED(IDC_CHECK_MRF, OnCheckMrf)
	ON_BN_CLICKED(IDC_CHECK_OVERWRITE, OnCheckOverwrite)
	ON_BN_CLICKED(IDC_CHECK_PLS, OnCheckPls)
	ON_BN_CLICKED(IDC_CHECK_TOPIC, OnCheckTopic)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImportDlg message handlers

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

	// For Resizable Dlg
	GetWindowRect(m_rect);
	m_nScrollPos = 0; 
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CImportDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// Resizable Dlg
	m_nCurHeight = cy;
	int nScrollMax;
	if (cy < m_rect.Height())
	{
	     nScrollMax = m_rect.Height() - cy;
	}
	else
	     nScrollMax = 0;

	SCROLLINFO si;
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_ALL; // SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS;
	si.nMin = 0;
	si.nMax = nScrollMax;
	si.nPage = si.nMax/10;
	si.nPos = 0;
        SetScrollInfo(SB_VERT, &si, TRUE);	
}

void CImportDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// Resizable Dlg
	int nDelta;
	int nMaxPos = m_rect.Height() - m_nCurHeight;

	switch (nSBCode)
	{
	case SB_LINEDOWN:
		if (m_nScrollPos >= nMaxPos)
			return;
		nDelta = min(nMaxPos/100,nMaxPos-m_nScrollPos);
		break;

	case SB_LINEUP:
		if (m_nScrollPos <= 0)
			return;
		nDelta = -min(nMaxPos/100,m_nScrollPos);
		break;

         case SB_PAGEDOWN:
		if (m_nScrollPos >= nMaxPos)
			return;
		nDelta = min(nMaxPos/10,nMaxPos-m_nScrollPos);
		break;

	case SB_THUMBPOSITION:
		nDelta = (int)nPos - m_nScrollPos;
		break;

	case SB_PAGEUP:
		if (m_nScrollPos <= 0)
			return;
		nDelta = -min(nMaxPos/10,m_nScrollPos);
		break;
	
         default:
		return;
	}
	m_nScrollPos += nDelta;
	SetScrollPos(SB_VERT,m_nScrollPos,TRUE);
	ScrollWindow(0,-nDelta);

	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CImportDlg::OnButtonBrowse() 
{
	TRACE(_T("In CImportDlg::OnButtonBrowse\n"));

	CString defExtension, defFileName, strFilters;

	// Ensure a type selected.
	if(!m_bCheckDat && !m_bCheckM3u && !m_bCheckPls && !m_bCheckMrf)
	{
		AfxMessageBox(_T("Please select an Import From check box."));
		return;
	}

	// Prep.
	if(m_bCheckDat)
	{
		defExtension = _T(".dat");
		defFileName  = _T("");
		strFilters = _T("DsLav Contents File (*.dat)|*.dat|All Files (*.*)|*.*||");
	}
	else if(m_bCheckM3u)
	{
		defExtension = _T(".m3u");
		defFileName  = _T("");
		strFilters = _T("m3u playlist (*.m3u)|*.m3u|All Files (*.*)|*.*||");
	}
	else if(m_bCheckPls)
	{
		defExtension = _T(".pls");
		defFileName  = _T("");
		strFilters = _T("pls playlist (*.pls)|*.pls|All Files (*.*)|*.*||");
	}
	// Else Media Record Format text file - default or m_bCheckMrf
	else
	{
		defExtension = _T(".txt");
		defFileName  = _T("");
		strFilters = _T("Media Record Format text file (*.txt)|*.txt|All Files (*.*)|*.*||");
	}
	// Open dlg for reading.
	CFileDialog dlg(TRUE,						// File Open dialog 
					defExtension, 
					defFileName, 
					OFN_OVERWRITEPROMPT |		// prompts on overwrite
					OFN_PATHMUSTEXIST,			// ensures valid path
					strFilters);

	// Set initial directory if available. 9/27/2021
	if(m_pathContentsFiles != _T(""))
		dlg.m_ofn.lpstrInitialDir = m_pathContentsFiles;

	// Get file path or URL.
	if(dlg.DoModal() == IDOK)
	{
		m_editPathStr = dlg.GetPathName();
		UpdateData(FALSE);
	}	
}

// .dat - DsLav's serialized.
// Note: TopicTree is always overwritten by serialized dat file.
//		 See CDsLavDoc::OnToolsImport() 7/22
void CImportDlg::OnCheckDat() 
{
	// If checked,
	if(m_ctrlCheckDat.GetCheck())
	{
		// Uncheck.
		m_ctrlCheckMrf.SetCheck(FALSE);
		m_ctrlCheckPls.SetCheck(FALSE);
		m_ctrlCheckM3u.SetCheck(FALSE);

		// Set checked overwrite, and disable - always overwritten. 7/22
		m_ctrlCheckOverwrite.SetCheck(TRUE);
		m_ctrlCheckOverwrite.EnableWindow(FALSE);

		// Disable Topic and Topic edit box.
		m_ctrlCheckTopic.EnableWindow(FALSE);
		m_ctrlCheckTopic.SetCheck(FALSE);
		m_ctrlEditTopic.SetWindowText(_T(""));
		m_ctrlEditTopic.EnableWindow(FALSE);

		// Update data members.
		UpdateData(TRUE);
	}

	// Else un-checked, 7/22
	else
	{
		// Uncheck.
		m_ctrlCheckMrf.SetCheck(FALSE);
		m_ctrlCheckPls.SetCheck(FALSE);
		m_ctrlCheckM3u.SetCheck(FALSE);
		m_ctrlCheckOverwrite.SetCheck(FALSE);

		// Enable Topic check and edit box.
		m_ctrlCheckOverwrite.EnableWindow(TRUE);
		m_ctrlCheckTopic.EnableWindow(TRUE);
		m_ctrlEditTopic.EnableWindow(TRUE);

		// Update members.
		UpdateData(TRUE);
	}
}
// Media Record Format - Lav's schema -.txt
void CImportDlg::OnCheckMrf() 
{
	// Prepare controls. 7/22
	//
	// Uncheck other options.
	m_ctrlCheckDat.SetCheck(FALSE);
	m_ctrlCheckPls.SetCheck(FALSE);
	m_ctrlCheckM3u.SetCheck(FALSE);	
	// Enable Overwrite.
	m_ctrlCheckOverwrite.EnableWindow(TRUE);
	m_ctrlCheckOverwrite.SetCheck(FALSE);	
	// Enable Topic.
	m_ctrlCheckTopic.EnableWindow(TRUE);
	m_ctrlCheckTopic.SetCheck(FALSE);		
	m_ctrlEditTopic.SetWindowText(_T(""));
	m_ctrlEditTopic.EnableWindow(FALSE);	// Gets enabled when user checks Topic.
	// Update members.
	UpdateData(TRUE);
}
// .m3u - typically URLs, but can be disc files.
void CImportDlg::OnCheckM3u() 
{
	// If importing M3u, 7/22
	if(m_ctrlCheckM3u.GetCheck())
	{
		// Set checks.
		m_ctrlCheckDat.SetCheck(FALSE);
		m_ctrlCheckMrf.SetCheck(FALSE);
		m_ctrlCheckPls.SetCheck(FALSE);
		// Disable overwrite.
		m_ctrlCheckOverwrite.EnableWindow(FALSE);
		m_ctrlCheckOverwrite.SetCheck(FALSE);
		// Enable Topic.
		m_ctrlCheckTopic.EnableWindow(TRUE);
		m_ctrlCheckTopic.SetCheck(TRUE);
		m_ctrlEditTopic.EnableWindow(TRUE);
		m_ctrlEditTopic.SetWindowText(_T("Imported Media"));
		// Update members.
		UpdateData(TRUE);
	}
}
// .pls - typically URLs, but can be disc files.
void CImportDlg::OnCheckPls() 
{
	// If importing PLS, 7/22
	if(m_ctrlCheckPls.GetCheck())
	{
		m_ctrlCheckDat.SetCheck(FALSE);
		m_ctrlCheckMrf.SetCheck(FALSE);
		m_ctrlCheckM3u.SetCheck(FALSE);	
		// Disable overwrite.
		m_ctrlCheckOverwrite.EnableWindow(FALSE);
		m_ctrlCheckOverwrite.SetCheck(FALSE);
		// Enable Topic.
		m_ctrlCheckTopic.EnableWindow(TRUE);
		m_ctrlCheckTopic.SetCheck(TRUE);
		m_ctrlEditTopic.EnableWindow(TRUE);
		m_ctrlEditTopic.SetWindowText(_T("Imported Media"));
		// Update members.
		UpdateData(TRUE);
	}
}
// Overwrite Contents Tree.
void CImportDlg::OnCheckOverwrite() 
{
	// If overwriting (checked), no need for a topic. 7/22
	if(m_ctrlCheckOverwrite.GetCheck())
	{
		// Topic check off.
		m_ctrlCheckTopic.SetCheck(FALSE);
		// Topic text empty.
		m_ctrlEditTopic.SetWindowText(_T(""));
		// Disable Topic edit box.
		m_ctrlEditTopic.EnableWindow(FALSE);
		// Update members.
		UpdateData(TRUE);
	}
	// If not overwriting, must use topic.
	else
	{
		// Topic checked.
		m_ctrlCheckTopic.SetCheck(TRUE);
		// Enable Topic edit box.
		m_ctrlEditTopic.EnableWindow(TRUE);
		// Set text to default topic.
		m_ctrlEditTopic.SetWindowText(_T("Imported Media"));
		// Update members.
		UpdateData(TRUE);
	}
}
// Imported contents go under this topic.
void CImportDlg::OnCheckTopic() 
{
	// If Topic checked, 7/22
	if(m_ctrlCheckTopic.GetCheck())
	{
		// Uncheck overwrite.
		m_ctrlCheckOverwrite.SetCheck(FALSE);
		// Enable Topic edit box.
		m_ctrlEditTopic.EnableWindow(TRUE);
		// For now set a default folder.
		m_ctrlEditTopic.SetWindowText(_T("Imported Media"));
		// Update members.
		UpdateData(TRUE);
	}
	// Else not using topic.
	else
	{
		// If importing a ,m3u or .pls, topic is always used.
		// Reset check and bail.
		if(m_bCheckM3u || m_bCheckPls)
		{
			m_ctrlCheckTopic.SetCheck(TRUE);
			return;
		}
		// Clear text
		m_ctrlEditTopic.SetWindowText(_T(""));
		// Set Overwrite checked.
		m_ctrlCheckOverwrite.SetCheck(TRUE);
		// Disable Topic edit box.
		m_ctrlEditTopic.EnableWindow(FALSE);
		// Update members.
		UpdateData(TRUE);
	}
}

void CImportDlg::OnOK() 
{
	// Get entered data.
	UpdateData(TRUE);

	///////////////////////////////////////////////////////////////
	// Validate.
	//
	// File type.
	if(!m_bCheckDat && !m_bCheckMrf && !m_bCheckM3u && !m_bCheckPls)
	{
		AfxMessageBox(_T("Please select an Import From check box."));
		return;
	}
	// Path
	if(m_editPathStr.IsEmpty())
	{
		AfxMessageBox(_T("Please enter or browse for a File Path or URL."));
		return;
	}
	// Allowing user to type File Path - problematic.
	// Bad chars. PathCleanupSpec() perhaps better
	if(m_bCheckDat || m_bCheckMrf)
	{
		if(m_editPathStr.FindOneOf(_T("/*?\"<>|") ) != -1) // invalid: \ / : * ? " < > |
		{ 
			AfxMessageBox(_T("File path contains one or more invalid characters."));
			return;
		}
	}
	// Overwrite or send to new topic.
	if(!m_bCheckOverwrite && !m_bCheckTopic)
	{
		AfxMessageBox(_T("Please check either overwrite or topic."));
		return;
	}
	// Importing to a topic.
	if(m_bCheckTopic && m_editPathStr.IsEmpty())
	{
		AfxMessageBox(_T("Please enter the Topic to place media items under. ")
					  _T("This can be an existing or new topic."));
		return;
	}

	//////////////////////////////////////////////////////////////////
	// Process user request.	
	CDialog::OnOK();
}

