// PPgDirectories.cpp : implementation file
//

#include "stdafx.h"
#include "emule.h"
#include "PPgDirectories.h"
#include "otherfunctions.h"

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


// CPPgDirectories dialog

IMPLEMENT_DYNAMIC(CPPgDirectories, CPropertyPage)
CPPgDirectories::CPPgDirectories()
	: CPropertyPage(CPPgDirectories::IDD)
{
	
}

CPPgDirectories::~CPPgDirectories()
{
}

void CPPgDirectories::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SHARESELECTOR, m_ShareSelector);
}


BEGIN_MESSAGE_MAP(CPPgDirectories, CPropertyPage)
	ON_BN_CLICKED(IDC_SELTEMPDIR, OnBnClickedSeltempdir)
	ON_BN_CLICKED(IDC_SELINCDIR, OnBnClickedSelincdir)
	ON_EN_CHANGE(IDC_INCFILES, OnSettingsChange)
	ON_EN_CHANGE(IDC_TEMPFILES, OnSettingsChange)
	ON_BN_CLICKED(IDC_CHECK1, OnSettingsChange)
	ON_EN_CHANGE(IDC_VIDEOPLAYER, OnSettingsChange)
	ON_BN_CLICKED(IDC_VIDEOBACKUP, OnSettingsChange)
	ON_BN_CLICKED(IDC_BROWSEV, BrowseVideoplayer)
END_MESSAGE_MAP()


// CPPgDirectories message handlers

BOOL CPPgDirectories::OnInitDialog()
{
	CWaitCursor curWait; // initialization of that dialog may take a while..
	CPropertyPage::OnInitDialog();
	m_ShareSelector.Init();	

	((CEdit*)GetDlgItem(IDC_INCFILES))->SetLimitText(509);
	((CEdit*)GetDlgItem(IDC_TEMPFILES))->SetLimitText(509);
	
	LoadSettings();
	Localize();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CPPgDirectories::LoadSettings(void)
{
	GetDlgItem(IDC_INCFILES)->SetWindowText(app_prefs->prefs->incomingdir);
	GetDlgItem(IDC_TEMPFILES)->SetWindowText(app_prefs->prefs->tempdir);
	m_ShareSelector.SetSharedDirectories(&app_prefs->shareddir_list);

	GetDlgItem(IDC_VIDEOPLAYER)->SetWindowText(app_prefs->prefs->VideoPlayer);
	if(app_prefs->prefs->moviePreviewBackup)
		CheckDlgButton(IDC_VIDEOBACKUP,1);
	else
		CheckDlgButton(IDC_VIDEOBACKUP,0);

}

void CPPgDirectories::OnBnClickedSelincdir()
{
	TCHAR buffer[MAX_PATH] = {0};
	GetDlgItemText(IDC_INCFILES, buffer, ARRSIZE(buffer));
	if(SelectDir(GetSafeHwnd(),buffer,GetResString(IDS_SELECT_INCOMINGDIR)))
		GetDlgItem(IDC_INCFILES)->SetWindowText(buffer);
}

void CPPgDirectories::OnBnClickedSeltempdir()
{
	TCHAR buffer[MAX_PATH] = {0};
	GetDlgItemText(IDC_TEMPFILES, buffer, ARRSIZE(buffer));
	if(SelectDir(GetSafeHwnd(),buffer,GetResString(IDS_SELECT_TEMPDIR)))
		GetDlgItem(IDC_TEMPFILES)->SetWindowText(buffer);
}


BOOL CPPgDirectories::OnApply()
{
	char buffer[510];
	
	if(GetDlgItem(IDC_INCFILES)->GetWindowTextLength())
	{
		GetDlgItem(IDC_INCFILES)->GetWindowText(buffer,509);
		strcpy(app_prefs->prefs->incomingdir,buffer);
	}
	MakeFoldername(app_prefs->prefs->incomingdir);
	sprintf(app_prefs->GetCategory(0)->incomingpath,"%s",app_prefs->prefs->incomingdir);
	
	if(GetDlgItem(IDC_TEMPFILES)->GetWindowTextLength())
	{
		GetDlgItem(IDC_TEMPFILES)->GetWindowText(buffer,509);
		strcpy(app_prefs->prefs->tempdir,buffer);
	}
	MakeFoldername(app_prefs->prefs->tempdir);

	GetDlgItem(IDC_VIDEOPLAYER)->GetWindowText(buffer,509);
	strcpy(app_prefs->prefs->VideoPlayer ,buffer);

	app_prefs->prefs->moviePreviewBackup = IsDlgButtonChecked(IDC_VIDEOBACKUP);
	app_prefs->shareddir_list.RemoveAll();

	m_ShareSelector.GetSharedDirectories(&app_prefs->shareddir_list);
	theApp.sharedfiles->Reload();
	
	SetModified(0);
	return CPropertyPage::OnApply();
}

BOOL CPPgDirectories::OnCommand(WPARAM wParam, LPARAM lParam)
{
//	if(wParam == USRMSG_ITEMSTATECHANGED)
//		SetModified();	
	return CPropertyPage::OnCommand(wParam, lParam);
}

void CPPgDirectories::Localize(void)
{
	if(m_hWnd)
	{
		SetWindowText(GetResString(IDS_PW_DIR));

		GetDlgItem(IDC_INCOMING_FRM)->SetWindowText(GetResString(IDS_PW_INCOMING));
		GetDlgItem(IDC_TEMP_FRM)->SetWindowText(GetResString(IDS_PW_TEMP));
		GetDlgItem(IDC_SELINCDIR)->SetWindowText(GetResString(IDS_PW_BROWSE));
		GetDlgItem(IDC_SELTEMPDIR)->SetWindowText(GetResString(IDS_PW_BROWSE));
		GetDlgItem(IDC_BROWSEV)->SetWindowText(GetResString(IDS_PW_BROWSE));
		GetDlgItem(IDC_SHARED_FRM)->SetWindowText(GetResString(IDS_PW_SHARED));

		GetDlgItem(IDC_STATICVIDEOPLAYER)->SetWindowText(GetResString(IDS_PW_VIDEOPLAYER));
		GetDlgItem(IDC_VIDEOBACKUP)->SetWindowText(GetResString(IDS_VIDEOBACKUP));		
		GetDlgItem(IDC_STATIC_EMPTY)->SetWindowText(GetResString(IDS_STATIC_EMPTY));
	}
}

void CPPgDirectories::BrowseVideoplayer(){
	CString strPlayerPath;
	GetDlgItemText(IDC_VIDEOPLAYER, strPlayerPath);
	CFileDialog dlgFile(TRUE, "*.exe", strPlayerPath,OFN_FILEMUSTEXIST | OFN_NOREADONLYRETURN ,"Executable (*.exe)|*.exe||", NULL, 0);
	if (dlgFile.DoModal()==IDOK){
		GetDlgItem(IDC_VIDEOPLAYER)->SetWindowText(dlgFile.GetPathName());
		SetModified();
	}
}
