// OptMCache.cpp : implementation file
//

#include "stdafx.h"
#include "osmo4.h"
#include "OptMCache.h"

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

/////////////////////////////////////////////////////////////////////////////
// COptMCache dialog


COptMCache::COptMCache(CWnd* pParent /*=NULL*/)
	: CDialog(COptMCache::IDD, pParent)
{
	//{{AFX_DATA_INIT(COptMCache)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void COptMCache::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COptMCache)
	DDX_Control(pDX, IDC_BASEPRES, m_BaseName);
	DDX_Control(pDX, IDC_MCACHE_USENAME, m_UseBase);
	DDX_Control(pDX, IDC_MCACHE_OVERWRITE, m_Overwrite);
	DDX_Control(pDX, IDC_BROWSE_MCACHE, m_RecDir);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COptMCache, CDialog)
	//{{AFX_MSG_MAP(COptMCache)
	ON_BN_CLICKED(IDC_BROWSE_MCACHE, OnBrowseMcache)
	ON_BN_CLICKED(IDC_MCACHE_USENAME, OnMcacheUsename)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COptMCache message handlers

static char szCacheDir[MAX_PATH];

static int CALLBACK LocCbck(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) 
{
	char dir[MAX_PATH];
	if (uMsg == BFFM_INITIALIZED) {
		strcpy(dir, szCacheDir);
		SendMessage(hwnd, BFFM_SETSELECTION, TRUE,(LPARAM) dir);
	}
	return 0;
}

void COptMCache::OnBrowseMcache() 
{
	BROWSEINFO brw;
	LPMALLOC pMalloc;
	LPITEMIDLIST ret;
	char dir[MAX_PATH];

	if (NOERROR == ::SHGetMalloc(&pMalloc) ) {

		m_RecDir.GetWindowText(szCacheDir, MAX_PATH);

		memset(&brw, 0, sizeof(BROWSEINFO));
		brw.hwndOwner = this->GetSafeHwnd();
		brw.pszDisplayName = dir;
		brw.lpszTitle = "Select HTTP Cache Directory...";
		brw.ulFlags = 0L;
		brw.lpfn = LocCbck;

		ret = SHBrowseForFolder(&brw);
		if (ret != NULL) {
			if (::SHGetPathFromIDList(ret, dir)) {
				m_RecDir.SetWindowText(dir);
			}
			pMalloc->Free(ret);
		}
		pMalloc->Release();
	}
}

BOOL COptMCache::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	WinGPAC *gpac = GetApp();
	char *sOpt;
	
	sOpt = IF_GetKey(gpac->m_config, "MediaCache", "RecordDirectory");
	if (!sOpt) sOpt = IF_GetKey(gpac->m_config, "General", "CacheDirectory");
	if (sOpt) m_RecDir.SetWindowText(sOpt);

	sOpt = IF_GetKey(gpac->m_config, "MediaCache", "KeepExistingFiles");
	m_Overwrite.SetCheck((sOpt && !stricmp(sOpt, "yes")) ? 0 : 1);

	sOpt = IF_GetKey(gpac->m_config, "MediaCache", "BaseFileName");
	if (sOpt) {
		m_UseBase.SetCheck(1);
		m_BaseName.EnableWindow(TRUE);
		m_BaseName.SetWindowText(sOpt);
	} else {
		m_UseBase.SetCheck(0);
		m_BaseName.EnableWindow(FALSE);
		m_BaseName.SetWindowText("uses service URL");
	}
	return TRUE;
}

void COptMCache::OnMcacheUsename() 
{
	if (m_UseBase.GetCheck()) {
		m_BaseName.EnableWindow(TRUE);
		m_BaseName.SetWindowText("record");
	} else {
		m_BaseName.EnableWindow(FALSE);
		m_BaseName.SetWindowText("uses service URL");
	}
}

void COptMCache::SaveOptions()
{
	WinGPAC *gpac = GetApp();

	IF_SetKey(gpac->m_config, "MediaCache", "KeepExistingFiles", m_Overwrite.GetCheck() ? "no" : "yes");
	if (m_UseBase.GetCheck()) {
		m_BaseName.GetWindowText(szCacheDir, MAX_PATH);
		IF_SetKey(gpac->m_config, "MediaCache", "BaseFileName", szCacheDir);
	} else {
		IF_SetKey(gpac->m_config, "MediaCache", "BaseFileName", NULL);
	}
	m_RecDir.GetWindowText(szCacheDir, MAX_PATH);
	IF_SetKey(gpac->m_config, "MediaCache", "RecordDirectory", szCacheDir);
}
