//this file is part of NeoMule
//Copyright (C)2006 David Xanatos ( Xanatos@Lycos.at / http://neomule.sourceforge.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


#include "stdafx.h"
#include "emule.h"
#include "PPgNeo.h"
#include "Preferences.h"
#include "OtherFunctions.h"
#include "Sion/Functions.h"
#include "Sion/GUI/CP/TreeFunctions.h"
#include "Sion/Defaults.h"
#include "KnownFileList.h"
#include "Opcodes.h"

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



#ifdef ISPBYPASS // NEO: SCC - [SecureClinetConnection]
class CNeedISPBypass : public CTreeOptionsCombo
{
public:
	CNeedISPBypass();
	virtual ~CNeedISPBypass();

protected:
	//{{AFX_VIRTUAL(CNeedISPBypass)
	//}}AFX_VIRTUAL

	//{{AFX_MSG(CNeedISPBypass)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()

  DECLARE_DYNCREATE(CNeedISPBypass)
};


IMPLEMENT_DYNCREATE(CNeedISPBypass, CTreeOptionsCombo)

CNeedISPBypass::CNeedISPBypass()
{
}

CNeedISPBypass::~CNeedISPBypass()
{
}

BEGIN_MESSAGE_MAP(CNeedISPBypass, CTreeOptionsCombo)
	//{{AFX_MSG_MAP(CNeedISPBypass)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


int CNeedISPBypass::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CTreeOptionsCombo::OnCreate(lpCreateStruct) == -1)
		return -1;
	
  //Add strings to the combo
	AddString(GetResString(IDS_X_CD_ISPTRAVERSAL_UNNEDED));
	AddString(GetResString(IDS_X_CD_ISPTRAVERSAL_PARTIAL));
	AddString(GetResString(IDS_X_CD_ISPTRAVERSAL_IMPLICIT));
#ifdef _DEBUG // for the moment don't allow users tu use explicit mode 
	AddString(GetResString(IDS_X_CD_ISPTRAVERSAL_EXPLICIT));
#endif

	return 0;
}
#endif // ISPBYPASS // NEO: SCC END

///////////////////////////////////////////////////////////////////////////////
// CPPgNeo dialog

IMPLEMENT_DYNAMIC(CPPgNeo, CPropertyPage)

BEGIN_MESSAGE_MAP(CPPgNeo, CPropertyPage)
	ON_WM_DESTROY()
	ON_MESSAGE(WM_TREEOPTSCTRL_NOTIFY, OnTreeOptsCtrlNotify)
	ON_MESSAGE(WM_TREEITEM_HELP, DrawTreeItemHelp)
	ON_WM_HELPINFO()
END_MESSAGE_MAP()

CPPgNeo::CPPgNeo()
	: CPropertyPage(CPPgNeo::IDD)
	, m_ctrlTreeOptions(theApp.m_iDfltImageListColorFlags)
{
	ClearAllMembers();
	m_ctrlTreeOptions.SetNeoStyle();
}

CPPgNeo::~CPPgNeo()
{
}

void CPPgNeo::ClearAllMembers()
{
	m_bInitializedTreeOpts = false;
	
	m_htiMain = NULL;
		m_htiUseSaveComments = NULL; // NEO: XC - [ExtendedComments]
		m_htiUseKnownComments = NULL; // NEO: XCk - [ExtendedKnownComments]
#ifdef ISPBYPASS // NEO: SCC - [SecureClinetConnection]
		m_htiSupportISPBypass = NULL;
			m_htiNeedISPBypass = NULL;
			m_htiEncryptedPorts = NULL;
#endif // ISPBYPASS // NEO: SCC END
		m_htiUseLugdunumCredits = NULL; // NEO: KLC - [KhaosLugdunumCredits]
		m_htiUseCustomPermissions = NULL; // NEO: SSP - [ShowSharePermissions]
}

void CPPgNeo::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MOD_OPTS, m_ctrlTreeOptions);
	if (!m_bInitializedTreeOpts)
	{
		int iImgMain = 8;
        CImageList* piml = m_ctrlTreeOptions.GetImageList(TVSIL_NORMAL);
		if (piml){
			iImgMain = piml->Add(CTempIconLoader(_T("MAINTWEAKS")));
		}
		
		SetTreeGroup(m_ctrlTreeOptions,m_htiMain,GetResString(IDS_X_MAIN_TWEAKS),iImgMain, TVI_ROOT, GetResString(IDS_X_MAIN_TWEAKS_INFO));
			SetTreeCheck(m_ctrlTreeOptions,m_htiUseSaveComments,GetResString(IDS_X_SAVE_COMMENTS),m_htiMain,GetResString(IDS_X_SAVE_COMMENTS_INFO),FALSE,m_bUseSaveComments); // NEO: XC - [ExtendedComments]
			SetTreeCheck(m_ctrlTreeOptions,m_htiUseKnownComments,GetResString(IDS_X_KNOWN_COMMENTS),m_htiMain,GetResString(IDS_X_KNOWN_COMMENTS_INFO),FALSE,m_bUseKnownComments); // NEO: XCk - [ExtendedKnownComments]
#ifdef ISPBYPASS // NEO: SCC - [SecureClinetConnection]
			SetTreeCheck(m_ctrlTreeOptions,m_htiSupportISPBypass,GetResString(IDS_X_SUPPORT_ISPTRAVERSAL),m_htiMain,GetResString(IDS_X_SUPPORT_ISPTRAVERSAL_INFO),TRUE,m_uSupportISPBypass);
				m_htiNeedISPBypass = m_ctrlTreeOptions.InsertItem(GetResString(IDS_X_NEED_ISPTRAVERSAL), TREEOPTSCTRLIMG_EDIT, TREEOPTSCTRLIMG_EDIT, m_htiSupportISPBypass);
				m_ctrlTreeOptions.AddComboBox(m_htiNeedISPBypass,RUNTIME_CLASS(CNeedISPBypass));
				m_ctrlTreeOptions.SetItemInfo(m_htiNeedISPBypass,GetResString(IDS_X_NEED_ISPTRAVERSAL_INFO));
				SetTreeEdit(m_ctrlTreeOptions,m_htiEncryptedPorts,GetResString(IDS_X_AUX_PORTS), m_htiSupportISPBypass,GetResString(IDS_X_AUX_PORTS_INFO));
#endif // ISPBYPASS // NEO: SCC END
			SetTreeCheck(m_ctrlTreeOptions,m_htiUseLugdunumCredits,GetResString(IDS_X_USE_LUGDUNUM_CREDITS),m_htiMain,GetResString(IDS_X_USE_LUGDUNUM_CREDITS_INFO),TRUE,m_uUseLugdunumCredits); // NEO: KLC - [KhaosLugdunumCredits]
			SetTreeCheck(m_ctrlTreeOptions,m_htiUseCustomPermissions,GetResString(IDS_X_SHOW_SHARE),m_htiMain,GetResString(IDS_X_SHOW_SHARE_INFO),FALSE,m_bUseCustomPermissions); // NEO: SSP - [ShowSharePermissions]

		m_ctrlTreeOptions.Expand(m_htiMain, TVE_EXPAND);
		m_bInitializedTreeOpts = true;
	}

	DDX_TreeCheck(pDX, IDC_MOD_OPTS, m_htiUseSaveComments, m_bUseSaveComments); // NEO: XC - [ExtendedComments]
	DDX_TreeCheck(pDX, IDC_MOD_OPTS, m_htiUseKnownComments, m_bUseKnownComments); // NEO: XCk - [ExtendedKnownComments]

#ifdef ISPBYPASS // NEO: SCC - [SecureClinetConnection]
	DDX_TreeCheck(pDX, IDC_MOD_OPTS, m_htiSupportISPBypass, m_uSupportISPBypass);

		if (pDX->m_bSaveAndValidate)
		{
			CString sOperator;
			sOperator = m_ctrlTreeOptions.GetComboText(m_htiNeedISPBypass);
			if(sOperator.Compare(GetResString(IDS_X_CD_ISPTRAVERSAL_UNNEDED)) == 0)
				m_uNeedISPBypass = 0;
			else if(sOperator.Compare(GetResString(IDS_X_CD_ISPTRAVERSAL_PARTIAL)) == 0)
				m_uNeedISPBypass = 1;
			else if(sOperator.Compare(GetResString(IDS_X_CD_ISPTRAVERSAL_IMPLICIT)) == 0)
				m_uNeedISPBypass = 2;
			else if(sOperator.Compare(GetResString(IDS_X_CD_ISPTRAVERSAL_EXPLICIT)) == 0)
				m_uNeedISPBypass = 3;
			else 
				ASSERT(0);
		}
		else
		{
			if(m_uNeedISPBypass == 0)
				m_ctrlTreeOptions.SetComboText(m_htiNeedISPBypass,GetResString(IDS_X_CD_ISPTRAVERSAL_UNNEDED));
			else if(m_uNeedISPBypass == 1)
				m_ctrlTreeOptions.SetComboText(m_htiNeedISPBypass,GetResString(IDS_X_CD_ISPTRAVERSAL_PARTIAL));
			else if(m_uNeedISPBypass == 2)
				m_ctrlTreeOptions.SetComboText(m_htiNeedISPBypass,GetResString(IDS_X_CD_ISPTRAVERSAL_IMPLICIT));
			else if(m_uNeedISPBypass == 3)
				m_ctrlTreeOptions.SetComboText(m_htiNeedISPBypass,GetResString(IDS_X_CD_ISPTRAVERSAL_EXPLICIT));
			else
				ASSERT(0);
		}

		DDX_TreeEdit(pDX, IDC_MOD_OPTS, m_htiEncryptedPorts, m_strEncryptedPorts);
#endif // ISPBYPASS // NEO: SCC END

	DDX_TreeCheck(pDX, IDC_MOD_OPTS, m_htiUseLugdunumCredits, m_uUseLugdunumCredits); // NEO: KLC - [KhaosLugdunumCredits]

	DDX_TreeCheck(pDX, IDC_MOD_OPTS, m_htiUseCustomPermissions, m_bUseCustomPermissions); // NEO: SSP - [ShowSharePermissions]
}

BOOL CPPgNeo::OnInitDialog()
{
	LoadSettings();

	CPropertyPage::OnInitDialog();
	InitWindowStyles(this);

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

void CPPgNeo::LoadSettings()
{
	/*
	* Globale Einstellungen Laden
	*/

	m_bUseSaveComments = thePrefs.m_bUseSaveComments; // NEO: XC - [ExtendedComments]
	m_bUseKnownComments = thePrefs.m_bUseKnownComments; // NEO: XCk - [ExtendedKnownComments]
#ifdef ISPBYPASS // NEO: SCC - [SecureClinetConnection]
	m_uSupportISPBypass = thePrefs.m_uSupportISPBypass;
	m_strEncryptedPorts = thePrefs.m_strEncryptedPorts;
	m_uNeedISPBypass = thePrefs.m_uNeedISPBypass;
#endif // ISPBYPASS // NEO: SCC END
	m_uUseLugdunumCredits = thePrefs.m_uUseLugdunumCredits; // NEO: KLC - [KhaosLugdunumCredits]
	m_bUseCustomPermissions = thePrefs.m_bUseCustomPermissions; // NEO: SSP - [ShowSharePermissions]
}

BOOL CPPgNeo::OnApply()
{
	// if prop page is closed by pressing ENTER we have to explicitly commit any possibly pending
	// data from an open edit control
	m_ctrlTreeOptions.HandleChildControlLosingFocus();

	if (!UpdateData())
		return FALSE;

	/*
	* Globale Einstellungen Speichern
	*/

	// NEO: XC - [ExtendedComments]
	bool OldSCState = thePrefs.m_bUseSaveComments;
	thePrefs.m_bUseSaveComments = m_bUseSaveComments; 
	if (OldSCState != thePrefs.m_bUseSaveComments && thePrefs.m_bUseSaveComments
	 && !theApp.knownfiles->IsCommentsLoaded())
		theApp.knownfiles->LoadComments();
	// NEO: XC END
	thePrefs.m_bUseKnownComments = m_bUseKnownComments; // NEO: XCk - [ExtendedKnownComments]
#ifdef ISPBYPASS // NEO: SCC - [SecureClinetConnection]
	thePrefs.m_uSupportISPBypass = m_uSupportISPBypass;
	thePrefs.m_strEncryptedPorts = m_strEncryptedPorts;
	thePrefs.m_uNeedISPBypass = m_uNeedISPBypass;
#endif // ISPBYPASS // NEO: SCC END
	thePrefs.m_uUseLugdunumCredits = m_uUseLugdunumCredits; // NEO: KLC - [KhaosLugdunumCredits]
	thePrefs.m_bUseCustomPermissions = m_bUseCustomPermissions; // NEO: SSP - [ShowSharePermissions]

	thePrefs.CheckSionPreferences();
	LoadSettings();

	SetModified(FALSE);
	return CPropertyPage::OnApply();
}

BOOL CPPgNeo::OnKillActive()
{
	// if prop page is closed by pressing ENTER we have to explicitly commit any possibly pending
	// data from an open edit control
	m_ctrlTreeOptions.HandleChildControlLosingFocus();
	return CPropertyPage::OnKillActive();
}

void CPPgNeo::OnDestroy()
{
	m_ctrlTreeOptions.DeleteAllItems();
	m_ctrlTreeOptions.DestroyWindow();
	ClearAllMembers();
	CPropertyPage::OnDestroy();
}

LRESULT CPPgNeo::OnTreeOptsCtrlNotify(WPARAM wParam, LPARAM lParam)
{
	if(!IsWindowVisible())
		return 0;

	if (wParam == IDC_MOD_OPTS){
		TREEOPTSCTRLNOTIFY* pton = (TREEOPTSCTRLNOTIFY*)lParam;
		if(pton->nmhdr.code == EN_KILLFOCUS){
//			}
			// NEO: MOD END
			SetModified();
		}
	}
	return 0;
}

LRESULT CPPgNeo::DrawTreeItemHelp(WPARAM wParam, LPARAM lParam)
{
	if(!IsWindowVisible())
		return 0;

	if (wParam == IDC_MOD_OPTS){
		CString* sInfo = (CString*)lParam;
		SetDlgItemText(IDC_MOD_OPTS_INFO, *sInfo);
	}
	return FALSE;
}

void CPPgNeo::OnHelp()
{
	//theApp.ShowHelp(0);
}

BOOL CPPgNeo::OnCommand(WPARAM wParam, LPARAM lParam)
{
	if (wParam == ID_HELP)
	{
		OnHelp();
		return TRUE;
	}
	return __super::OnCommand(wParam, lParam);
}

BOOL CPPgNeo::OnHelpInfo(HELPINFO* /*pHelpInfo*/)
{
	OnHelp();
	return TRUE;
}
