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

// ExportDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dslavsplit.h"
#include "ExportDlg.h"
#include "Shlwapi.h" //PathFileExists()

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

/////////////////////////////////////////////////////////////////////////////
// CExportDlg dialog


CExportDlg::CExportDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CExportDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CExportDlg)
	m_strPath = _T("");
	m_bCheckExclude = FALSE;
	m_bCheckMediaListFmt = FALSE;
	m_bCheckDat = FALSE;
	m_bCheckUnicode = FALSE;
	m_bCheckAlias = FALSE;
	m_bCheckM3u = FALSE;
	//}}AFX_DATA_INIT
}


void CExportDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExportDlg)
	DDX_Control(pDX, IDC_CHECK_M3U, m_ctrlCheckM3u);
	DDX_Control(pDX, IDC_CHECK_ALIASES, m_ctrlCheckAlias);
	DDX_Control(pDX, IDC_CHECK_UNICODE, m_ctrlCheckUnicode);
	DDX_Control(pDX, IDC_CHECK_EXCLUDE, m_ctrlCheckExclude);
	DDX_Control(pDX, IDC_CHECK_DAT, m_ctrlCheckDat);
	DDX_Control(pDX, IDC_CHECK_TXT, m_ctrlCheckMediaListFmt);
	DDX_Control(pDX, IDC_EDIT_PATH, m_ctrlPath);
	DDX_Text(pDX, IDC_EDIT_PATH, m_strPath);
	DDX_Check(pDX, IDC_CHECK_EXCLUDE, m_bCheckExclude);
	DDX_Check(pDX, IDC_CHECK_TXT, m_bCheckMediaListFmt);
	DDX_Check(pDX, IDC_CHECK_DAT, m_bCheckDat);
	DDX_Check(pDX, IDC_CHECK_UNICODE, m_bCheckUnicode);
	DDX_Check(pDX, IDC_CHECK_ALIASES, m_bCheckAlias);
	DDX_Check(pDX, IDC_CHECK_M3U, m_bCheckM3u);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CExportDlg, CDialog)
	//{{AFX_MSG_MAP(CExportDlg)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_BN_CLICKED(IDC_CHECK_DAT, OnCheckDat)
	ON_BN_CLICKED(IDC_CHECK_TXT, OnCheckTxt)
	ON_BN_CLICKED(IDC_CHECK_M3U, OnCheckM3u)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExportDlg message handlers


BOOL CExportDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// Set checks.
void CExportDlg::OnCheckDat() 
{
	// If checked,
	if(m_ctrlCheckDat.GetCheck())
	{
		m_ctrlCheckMediaListFmt.SetCheck(FALSE);
		m_ctrlCheckM3u.SetCheck(FALSE);

		// Disable disc files.
		m_ctrlCheckExclude.SetCheck(FALSE);
		m_ctrlCheckExclude.EnableWindow(FALSE);
		// Disable unicode
		m_ctrlCheckUnicode.SetCheck(FALSE);
		m_ctrlCheckUnicode.EnableWindow(FALSE);
		// Disable Aliases
		m_ctrlCheckAlias.SetCheck(FALSE);
		m_ctrlCheckAlias.EnableWindow(FALSE);
	}	
}
void CExportDlg::OnCheckTxt() 
{
	// If checked,
	if(m_ctrlCheckMediaListFmt.GetCheck())
	{
		m_ctrlCheckDat.SetCheck(FALSE);
		m_ctrlCheckM3u.SetCheck(FALSE);

		// Enable disc files.
		m_ctrlCheckExclude.EnableWindow(TRUE);
		// Ebable unicode
		m_ctrlCheckUnicode.EnableWindow(TRUE);
		// Enable Aliases
		m_ctrlCheckAlias.EnableWindow(TRUE);

		// Uncheck - 7/22
		m_ctrlCheckExclude.SetCheck(FALSE);
		m_ctrlCheckAlias.SetCheck(FALSE);
		m_ctrlCheckUnicode.SetCheck(FALSE);
	}	
}
void CExportDlg::OnCheckM3u() 
{
	// If checked,
	if(m_ctrlCheckM3u.GetCheck())
	{
		m_ctrlCheckDat.SetCheck(FALSE);
		m_ctrlCheckMediaListFmt.SetCheck(FALSE);

		// Disable disc files. 7/22
		// Set Exlude checked and disabled as disc files
		// ae not written.
		//  See: CTopicTree::WriteM3uStreamsEx().
		m_ctrlCheckExclude.SetCheck(TRUE);
		m_ctrlCheckExclude.EnableWindow(FALSE);

		// Disable disc files.
	//	m_ctrlCheckExclude.SetCheck(FALSE);
	//	m_ctrlCheckExclude.EnableWindow(FALSE);

		// Enable unicode
		m_ctrlCheckUnicode.SetCheck(FALSE);
		m_ctrlCheckUnicode.EnableWindow(TRUE);
		// Disable Aliases
		m_ctrlCheckAlias.SetCheck(FALSE);
		m_ctrlCheckAlias.EnableWindow(FALSE);
	}
}


// Browse for file.
void CExportDlg::OnButtonBrowse() 
{
	TRACE(_T("In CUpdateDlg::OnButtonBrowse\n"));

	// Get entered data.
	UpdateData(TRUE);

	CString defExtension, defFileName, strFilters;

	// Prep.
	//
	// Dat.
	if(m_bCheckDat)
	{
		defExtension = _T(".dat");
		defFileName  = _T("");
		strFilters = _T("DsLav Contents File - binary (*.dat)|*.dat|All Files (*.*)|*.*||");
	}
	// M3u.
	else if(m_bCheckM3u)
	{
		defExtension = _T(".m3u");
		defFileName  = _T("");
		strFilters = _T("M3u File (*.m3u)|*.m3u|All Files (*.*)|*.*||");
	}
	// Else text file - default or m_bCheckMediaListFmt
	else
	{
		defExtension = _T(".txt");
		defFileName  = _T("");
		strFilters = _T("Media Record Format text file - (*.txt)|*.txt|All Files (*.*)|*.*||");
	}
	// Open dlg for reading.
	CFileDialog dlg(FALSE,						// FALSE - SaveAs dlg
					defExtension, 
					defFileName, 
					OFN_OVERWRITEPROMPT |		// prompts on overwrite
				//	OFN_DONTADDTORECENT |		// don't add to mru - add is good
					OFN_FILEMUSTEXIST	|		// only existing files
					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_strPath = dlg.GetPathName();
		UpdateData(FALSE);
	}	
}

// OK pressed.
void CExportDlg::OnOK() 
{
	// Get entered data.
	UpdateData(TRUE);	

	// Validate
	if(!m_bCheckDat && !m_bCheckMediaListFmt && !m_bCheckM3u)
	{
		AfxMessageBox(_T("Please check a export type."));
		return;
	}
	if(m_strPath.IsEmpty())
	{
		AfxMessageBox(_T("Please enter a file path."));
		return;
	}
	// Allowing user to type File Path - problematic.
	// Bad chars. PathCleanupSpec() perhaps better
	if(m_strPath.FindOneOf(_T("/*?\"<>|") ) != -1) // invalid: \ / : * ? " < > |
	{ 
		AfxMessageBox(_T("File path contains invalid characters."));
		return;
	}
	
	CDialog::OnOK();
}




