// OptFiles.cpp : implementation file
//

#include "stdafx.h"
#include "osmo4.h"
#include "OptFiles.h"

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

/////////////////////////////////////////////////////////////////////////////
// OptFiles dialog


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


void OptFiles::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(OptFiles)
	DDX_Control(pDX, IDC_ASSOCIATE, m_DoAssociate);
	DDX_Control(pDX, IDC_FILES_PLUG, m_PlugName);
	DDX_Control(pDX, IDC_FILES_MIMES, m_mimes);
	DDX_Control(pDX, IDC_FILES_EXT, m_extensions);
	DDX_Control(pDX, IDC_FILELIST, m_FileDescs);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(OptFiles, CDialog)
	//{{AFX_MSG_MAP(OptFiles)
	ON_CBN_SELCHANGE(IDC_FILELIST, OnSelchangeFilelist)
	ON_BN_CLICKED(IDC_ASSOCIATE, OnAssociate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// OptFiles message handlers

BOOL OptFiles::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	WinGPAC *app = GetApp();
	u32 count, i;

	while (m_FileDescs.GetCount()) m_FileDescs.DeleteString(0);
	count = IF_GetKeyCount(app->m_config, "MimeTypes");
	for (i=0; i<count; i++) {
		char *sMime, *sKey, sDesc[200];
		sMime = (char *) IF_GetKeyName(app->m_config, "MimeTypes", i);
		if (!sMime) continue;
		sKey = IF_GetKey(app->m_config, "MimeTypes", sMime);
		if (!sKey) continue;
		sKey = strstr(sKey, "\" \"");
		if (!sKey) continue;
		strcpy(sDesc, sKey+3);
		sKey = strchr(sDesc, '\"');
		if (!sKey) continue;
		sKey[0] = 0;
		m_FileDescs.AddString(sDesc);
	}
	m_FileDescs.SetCurSel(0);
	SetSelection(0);
	return TRUE;
}

void OptFiles::OnSelchangeFilelist() 
{
	SetSelection(m_FileDescs.GetCurSel());
}

void OptFiles::SetSelection(u32 sel)
{
	WinGPAC *app = GetApp();
	char *sMime, *sKey, sDesc[200], sText[200];
	sMime = (char *) IF_GetKeyName(app->m_config, "MimeTypes", sel);
	sprintf(sText, "Mime Type: %s", sMime);
	m_mimes.SetWindowText(sText);
	strcpy(cur_mime, sMime);
	sMime = (char *) IF_GetKey(app->m_config, "MimeTypes", sMime);
	strcpy(sDesc, sMime+1);
	sKey = strchr(sDesc, '\"');
	sKey[0] = 0;
	sprintf(sText, "Extensions: %s", sDesc);
	strcpy(cur_ext, sDesc);
	m_extensions.SetWindowText(sText);
	sKey = strrchr(sMime, '\"');
	sprintf(sText, "Plugin: %s", sKey+2);
	m_PlugName.SetWindowText(sText);

	Bool has_asso, need_asso, go = 1;
	sKey = cur_ext;
	need_asso = has_asso = 0;

	HKEY hKey;
	DWORD dwSize;
	while (go) {
		Bool ok;
		char szExt[50], szReg[60], c;
		char *tmp = strchr(sKey, ' ');
		if (!tmp) { go = 0;}
		else { c = tmp[0]; tmp[0] = 0; }
		sprintf(szExt, ".%s", sKey);
		sprintf(szReg, "GPAC\\%s", sKey);
		if (tmp) { tmp[0] = c; tmp += 1; }

		if (RegOpenKeyEx(HKEY_CLASSES_ROOT, szExt, 0, 0, &hKey ) == ERROR_SUCCESS) {
			dwSize = 200;
			ok = 1;
			if (RegQueryValueEx(hKey, "", NULL, NULL,(unsigned char*) sDesc, &dwSize) != ERROR_SUCCESS) ok = 0;
			RegCloseKey(hKey);
			if (ok && !stricmp((char *)sDesc, szReg)) has_asso = 1;
			else need_asso = 1;
		} else need_asso = 1;
		sKey = tmp;

	}
	m_DoAssociate.SetCheck(has_asso);
	if (need_asso && has_asso)
		OnAssociate();
}


void OptFiles::OnAssociate() 
{
	char *sKey, sDesc[200];
	unsigned char szApp[MAX_PATH];
	unsigned char szIco[MAX_PATH];

	strcpy((char *) szApp, GetApp()->szAppPath);
	strcpy((char *) szIco, (const char *) szApp);
	strcat((char *) szIco, "Osmo4.ico");
	strcat((char *) szApp, "Osmo4.exe \"%L\"");

	if (m_DoAssociate.GetCheck()) {
		Bool go = 1;
		sKey = cur_ext;

		HKEY hKey;
		DWORD dwSize;
		while (go) {
			Bool ok;
			char szExt[50], szReg[60], szOld[80], szPath[1024], c;
			char *tmp = strchr(sKey, ' ');
			if (!tmp) { go = 0;}
			else { c = tmp[0]; tmp[0] = 0; }
			sprintf(szExt, ".%s", sKey);
			sprintf(szReg, "GPAC\\%s", sKey);
			if (tmp) { tmp[0] = c; tmp += 1; }

			RegOpenKeyEx(HKEY_CLASSES_ROOT, szExt, 0, 0, &hKey );
			dwSize = 200;
			ok = 1;
			if (RegQueryValueEx(hKey, "", NULL, NULL,(unsigned char*) sDesc, &dwSize) != ERROR_SUCCESS) ok = 0;
			RegCloseKey(hKey);
			strcpy(szOld, "");
			if (ok && stricmp((char *)sDesc, szReg)) strcpy(szOld, sDesc);

			strcpy(szPath, szReg); strcat(szPath, "\\DefaultIcon");
			RegCreateKeyEx(HKEY_CLASSES_ROOT, szPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwSize);
			RegSetValueEx(hKey, "", 0, REG_SZ, szIco, strlen((const char *) szIco)+1);
			RegCloseKey(hKey);

			strcpy(szPath, szReg); strcat(szPath, "\\Shell\\open\\command");
			RegCreateKeyEx(HKEY_CLASSES_ROOT, szPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwSize);
			RegSetValueEx(hKey, "", 0, REG_SZ, szApp, strlen((const char *) szApp)+1);
			RegCloseKey(hKey);

			if (strlen(szOld)) {
				strcpy(szPath, szReg); strcat(szPath, "\\Backup");
				RegCreateKeyEx(HKEY_CLASSES_ROOT, szPath, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwSize);
				RegSetValueEx(hKey, "", 0, REG_SZ, (unsigned char *) szOld, strlen((const char *) szIco)+1);
				RegCloseKey(hKey);
			}

			RegCreateKeyEx(HKEY_CLASSES_ROOT, szExt, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwSize);
			RegSetValueEx(hKey, "", 0, REG_SZ, (const unsigned char *) szReg, strlen(szReg)+1);
			RegCloseKey(hKey);

			sKey = tmp;
		}
	} else {
		Bool go = 1;
		sKey = cur_ext;

		HKEY hKey;
		DWORD dwSize;
		while (go) {
			Bool ok;
			char szExt[50], szReg[60], szPath[1024], c;
			char *tmp = strchr(sKey, ' ');
			if (!tmp) { go = 0;}
			else { c = tmp[0]; tmp[0] = 0; }
			sprintf(szExt, ".%s", sKey);
			sprintf(szReg, "GPAC\\%s", sKey);
			if (tmp) { tmp[0] = c; tmp += 1; }

			strcpy(szPath, szReg); strcat(szPath, "\\Backup");
			RegOpenKeyEx(HKEY_CLASSES_ROOT, szPath, 0, 0, &hKey );
			dwSize = 200;
			ok = 1;
			if (RegQueryValueEx(hKey, "", NULL, NULL,(unsigned char*) sDesc, &dwSize) != ERROR_SUCCESS) ok = 0;
			RegCloseKey(hKey);
			if (ok && strlen((char *)sDesc)) {
				RegCreateKeyEx(HKEY_CLASSES_ROOT, szExt, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwSize);
				RegSetValueEx(hKey, "", 0, REG_SZ, (unsigned char*) sDesc, strlen((const char *) sDesc)+1);
				RegCloseKey(hKey);
			}

			RegOpenKeyEx(HKEY_CLASSES_ROOT, szReg, 0, 0, &hKey );
			RegDeleteKey(hKey, "Backup");
			RegDeleteKey(hKey, "DefaultIcon");
			RegDeleteKey(hKey, "Shell\\open\\command");
			RegDeleteKey(hKey, "Shell\\open");
			RegDeleteKey(hKey, "Shell");
			RegCloseKey(hKey);
			RegDeleteKey(HKEY_CLASSES_ROOT, szReg);

			sKey = tmp;
		}
	}	
}
