/* --- CONFIG.CPP ---
/  
/ Written by Phil Himsworth, 2001-2002.
/
/*/


#include <windows.h>
#include <prsht.h>

#include "types.h"
#include "main.h"
#include "resource.h"
#include "plugin.h"

extern int con_port, hide_error, frames, securepassword, pass_debug;
extern int show_other_files, dl_wa_files, dl_other_files, title_refresh;

extern char mp3root[MAX_PATH], playlistdir[MAX_PATH], filetypes[255], pagetitle[255], logfiledir[MAX_PATH];


extern char szAppVer[], szAppName[];
extern winampGeneralPurposePlugin plugin;

HWND ConfigWnd = NULL;		// To ensure only one config window appears at once
HWND UsersWnd = NULL;

// --------------------------------------------------------------------------------
// Server options window proc
BOOL CALLBACK ConfigServerWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	char TempString[10];
	int port_temp;
	switch(uMsg)
	{
		case WM_INITDIALOG:
			wsprintf(TempString,"%d",con_port);
			SetDlgItemText(hwndDlg,IDC_PORT_EDIT,TempString);
			CheckDlgButton(hwndDlg,IDC_SECUREPASSWORD,securepassword);
			CheckDlgButton(hwndDlg,IDC_PASS_DEBUG,pass_debug);
			CheckDlgButton(hwndDlg,IDC_HIDE_ERROR,hide_error);
			break;

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_SECUREPASSWORDHELP:
								MessageBox(hwndDlg,"Secure Passwords...\n\nWawi is able to encrypt passwords in the config file using the MD5 algorithm, or they can just be stored as jumbled but potentially retrievable strings using the Base64 algorithm.\n\nLeave this checked unless you have problems. Note that changing between secure and insecure passwords will require entering all passwords in again.\n\nThe \"debug\" option is in case of further trouble. This will make message boxes pop up telling you what it is doing when saving a password; if it doesn't work let me know what these say and I'll see what I can do. Ta...","Winamp Web Interface Help",MB_OK);
								break;
			}
			break;
			
		case WM_NOTIFY:
			switch (((NMHDR FAR *) lParam)->code)
			{
			case PSN_APPLY:			port_temp = GetDlgItemInt(hwndDlg,IDC_PORT_EDIT,NULL,false);
									if (port_temp > 0 && port_temp < 65536)
									{
										con_port = port_temp;
										hide_error = IsDlgButtonChecked(hwndDlg,IDC_HIDE_ERROR);
										securepassword = IsDlgButtonChecked(hwndDlg,IDC_SECUREPASSWORD);
										pass_debug = IsDlgButtonChecked(hwndDlg,IDC_PASS_DEBUG);
									}
									else
									{
										MessageBox(hwndDlg,"Port must be from 1 to 65535!","Winamp Web Interface",MB_OK);
										SetDlgItemInt(hwndDlg,IDC_PORT_EDIT,con_port,false);
										SetWindowLong(hwndDlg,DWL_MSGRESULT,true);
										return true;
									}
									break;
			}
			break;
	}
	return false;
}


// --------------------------------------------------------------------------------
// Web page options window proc
BOOL CALLBACK ConfigWebPageWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	char TempString[10];
	HANDLE img;
	switch(uMsg)
	{
		case WM_INITDIALOG:

			wsprintf(TempString,"%d",title_refresh);
			SetDlgItemText(hwndDlg,IDC_REFRESH,TempString);
			SetDlgItemText(hwndDlg,IDC_PAGETITLE,pagetitle);

			/*
			LONG SendDlgItemMessage(
			  HWND hDlg,      // handle of dialog box
			  int nIDDlgItem, // identifier of control
			  UINT Msg,       // message to send
			  WPARAM wParam,  // first message parameter
			  LPARAM lParam   // second message parameter
			);
			*/


			switch(frames)
			{
			case 1:		CheckDlgButton(hwndDlg,IDC_NOFRAMES,1);
						img = LoadImage(plugin.hDllInstance,MAKEINTRESOURCE(IDB_FRAMES_ONE),IMAGE_BITMAP,0,0,0);
						SendDlgItemMessage(hwndDlg,IDC_FRAMEIMG,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)img);
						CloseHandle(img);
						break;
			case 3:		CheckDlgButton(hwndDlg,IDC_THREEFRAMES,1);
						img = LoadImage(plugin.hDllInstance,MAKEINTRESOURCE(IDB_FRAMES_THREE),IMAGE_BITMAP,0,0,0);
						SendDlgItemMessage(hwndDlg,IDC_FRAMEIMG,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)img);
						CloseHandle(img);
						break;
			default:	CheckDlgButton(hwndDlg,IDC_TWOFRAMES,1);
						img = LoadImage(plugin.hDllInstance,MAKEINTRESOURCE(IDB_FRAMES_TWO),IMAGE_BITMAP,0,0,0);
						SendDlgItemMessage(hwndDlg,IDC_FRAMEIMG,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)img);
						CloseHandle(img);
						break;
			}
			break;

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_PAGETITLEHELP:
								MessageBox(hwndDlg,"Page Title...\n\nThis text is what appears as the title on the main page on a user's browser. It can be up to 255 characters long, but should be made to fit into the width of a page in the browser.\n\nHTML tags can be included.","Winamp Web Interface Help",MB_OK);
								break;
				case IDC_NOFRAMES:
								if (HIWORD(wParam) == BN_CLICKED)
								{
									//MessageBox(hwndDlg,"No frames","wawi",MB_OK| MB_TOPMOST);
									img = LoadImage(plugin.hDllInstance,MAKEINTRESOURCE(IDB_FRAMES_ONE),IMAGE_BITMAP,0,0,0);
									SendDlgItemMessage(hwndDlg,IDC_FRAMEIMG,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)img);
									CloseHandle(img);
								}
								break;
				case IDC_TWOFRAMES:
								if (HIWORD(wParam) == BN_CLICKED)
								{
									//MessageBox(hwndDlg,"Two frames","wawi",MB_OK| MB_TOPMOST);
									img = LoadImage(plugin.hDllInstance,MAKEINTRESOURCE(IDB_FRAMES_TWO),IMAGE_BITMAP,0,0,0);
									SendDlgItemMessage(hwndDlg,IDC_FRAMEIMG,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)img);
									CloseHandle(img);
								}
								break;
				case IDC_THREEFRAMES:
								if (HIWORD(wParam) == BN_CLICKED)
								{
									//MessageBox(hwndDlg,"Three frames","wawi",MB_OK| MB_TOPMOST);
									img = LoadImage(plugin.hDllInstance,MAKEINTRESOURCE(IDB_FRAMES_THREE),IMAGE_BITMAP,0,0,0);
									SendDlgItemMessage(hwndDlg,IDC_FRAMEIMG,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)img);
									CloseHandle(img);
								}
								break;

			}
			break;
			
		case WM_NOTIFY:
			switch (((NMHDR FAR *) lParam)->code)
			{
			case PSN_APPLY:		GetDlgItemText(hwndDlg,IDC_PAGETITLE,(char*)pagetitle,255);
								title_refresh = GetDlgItemInt(hwndDlg,IDC_REFRESH,NULL,false);

								if (IsDlgButtonChecked(hwndDlg,IDC_NOFRAMES))
									frames = 1;
								else
									if (IsDlgButtonChecked(hwndDlg,IDC_THREEFRAMES))
										frames = 3;
									else
										frames = 2;

								break;
			}
			break;
	}
	return false;
}

// --------------------------------------------------------------------------------
// Browse options window proc
BOOL CALLBACK ConfigBrowseWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	int length;
	switch(uMsg)
	{
		case WM_INITDIALOG:
			CheckDlgButton(hwndDlg,IDC_WA_FILES_SHOW,1);
			CheckDlgButton(hwndDlg,IDC_OTHER_FILES_SHOW,show_other_files);
			CheckDlgButton(hwndDlg,IDC_WA_FILES_DL,dl_wa_files);
			CheckDlgButton(hwndDlg,IDC_OTHER_FILES_DL,dl_other_files);

			SetDlgItemText(hwndDlg,IDC_MP3_ROOT,mp3root);
			SetDlgItemText(hwndDlg,IDC_FILETYPES,filetypes);
			SetDlgItemText(hwndDlg,IDC_PLAYLISTDIR,playlistdir);

			break;

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_ROOTDIRHELP:
								MessageBox(hwndDlg,"Root Music Directory...\n\nEnter the path of the directory you want users to be able to browse. Any files and folders within this folder will appear in the Music Collection. Files and folders outside this folder will not be accessible.","Winamp Web Interface Help",MB_OK);
								break;
				case IDC_FILETYPEHELP:
								MessageBox(hwndDlg,"Loadable Filetypes...\n\nEnter file extensions of files that winamp can play, separated by spaces. For example, entering \"mp3 wav mid\" will enable files with those extensions to be clicked on and played from the browser.\n\nIncluding 'url' will enable Wawi to follow internet shortcuts to load songs or streams rather than just treat them as non-winamp files.","Winamp Web Interface Help",MB_OK);
								break;
				case IDC_PLAYLISTDIRHELP:
								MessageBox(hwndDlg,"Playlist Directory...\n\nWhen a playlist is saved from the web interface it is saved in this directory. The name given here is relative to the Root Music Directory; for example if the root directory is \"c:\\mp3\" and the playlist directory is \"playlists\", saved playlists will be stored in \"c:\\mp3\\playlists\\\".\n\nEnsure the directory exists before saving playlists into it!", "Winamp Web Interface Help", MB_OK);
								break;

			}
			break;
			
		case WM_NOTIFY:
			switch (((NMHDR FAR *) lParam)->code)
			{
			case PSN_APPLY:		show_other_files = IsDlgButtonChecked(hwndDlg,IDC_OTHER_FILES_SHOW);
								dl_other_files = IsDlgButtonChecked(hwndDlg,IDC_OTHER_FILES_DL);
								dl_wa_files = IsDlgButtonChecked(hwndDlg,IDC_WA_FILES_DL);

								GetDlgItemText(hwndDlg,IDC_FILETYPES,(char*)filetypes,255);

								length = GetDlgItemText(hwndDlg,IDC_MP3_ROOT,(char*)mp3root,MAX_PATH);
								if (mp3root[length-1] == '\\')
									mp3root[length-1] = 0;		// chop off trailing '\'
								else
								mp3root[length] = 0;			// make null terminated

								length = GetDlgItemText(hwndDlg,IDC_PLAYLISTDIR,(char*)playlistdir,MAX_PATH);
								if (playlistdir[length-1] == '\\')
									playlistdir[length-1] = 0;		// chop off trailing '\'
								else
								playlistdir[length] = 0;			// make null terminated


								break;
			}
			break;
	}
	return false;
}


// --------------------------------------------------------------------------------
// Log options window proc
BOOL CALLBACK ConfigLogWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	int length;
	switch(uMsg)
	{
		case WM_INITDIALOG:
			SetDlgItemText(hwndDlg,IDC_LOGFILEPATH,logfiledir);

			break;

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_VIEWLOG:	ViewLog();
									break;
			}
			break;
			
		case WM_NOTIFY:
			switch (((NMHDR FAR *) lParam)->code)
			{
				case PSN_APPLY:		length = GetDlgItemText(hwndDlg,IDC_LOGFILEPATH,(char*)logfiledir,MAX_PATH);
									if (logfiledir[length-1] == '\\')
										logfiledir[length-1] = 0;		// chop off trailing '\'
									else
									logfiledir[length] = 0;			// make null terminated
	

									break;
			}
			break;
	}
	return false;
}

// --------------------------------------------------------------------------------
// About options window proc
BOOL CALLBACK ConfigAboutWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	HANDLE img;
	char title[100];
	switch(uMsg)
	{
		case WM_INITDIALOG:
			ConfigWnd = (HWND) (((NMHDR FAR *) lParam)->hwndFrom);
			img = LoadImage(plugin.hDllInstance,MAKEINTRESOURCE(IDB_TITLE),IMAGE_BITMAP,0,0,0);
			SendDlgItemMessage(hwndDlg,IDC_MAINIMAGE,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)img);
			CloseHandle(img);
			wsprintf(title,"Winamp Web Interface %s",szAppVer);
			SetDlgItemText(hwndDlg,IDC_ABOUT_TITLE,title);

			break;

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDOK:	break;
				default:	break;
			}
			break;
			
		case WM_NOTIFY:
			switch (((NMHDR FAR *) lParam)->code)
			{
				case PSN_APPLY:		
									break;
			}
			break;
	}
	return false;
}


// --------------------------------------------------------------------------------
// Loading / Saving of config
void config_read()
{
	char ini_file[MAX_PATH], *p;
	GetModuleFileName(plugin.hDllInstance,ini_file,sizeof(ini_file));
	p=ini_file+lstrlen(ini_file);
	while (p >= ini_file && *p != '\\') p--;
	if (++p >= ini_file) *p = 0;
	lstrcat(ini_file,"gen_httpsrv.ini");

	con_port = GetPrivateProfileInt(szAppName,"Port",80,ini_file);
	hide_error = GetPrivateProfileInt(szAppName,"HideErrors",0,ini_file);
	frames = GetPrivateProfileInt(szAppName,"Frames",2,ini_file);
	securepassword = GetPrivateProfileInt(szAppName,"SecurePassword",1,ini_file);
	pass_debug = GetPrivateProfileInt(szAppName,"PassDebug",0,ini_file);

	show_other_files = GetPrivateProfileInt(szAppName,"ShowOtherFiles",0,ini_file);
	dl_wa_files = GetPrivateProfileInt(szAppName,"DLWaFiles",0,ini_file);
	dl_other_files = GetPrivateProfileInt(szAppName,"DLOtherFiles",0,ini_file);

	title_refresh = GetPrivateProfileInt(szAppName,"TitleRefresh",60,ini_file);

	GetPrivateProfileString(szAppName,"filetypes","mp3 wav m3u url",(char*)&filetypes,255,ini_file);
	GetPrivateProfileString(szAppName,"pagetitle","... Winamp Web Interface ...",(char*)&pagetitle,255,ini_file);
	GetPrivateProfileString(szAppName,"logfiledir","",(char*)&logfiledir,MAX_PATH,ini_file);
	GetPrivateProfileString(szAppName,"playlistdir","",(char*)&playlistdir,MAX_PATH,ini_file);

	int length = GetPrivateProfileString(szAppName,"mp3root","c:\\",(char*)&mp3root,MAX_PATH,ini_file);
	
	if (mp3root[length-1] == '\\')
		mp3root[length-1] = 0;		// chop off trailing '\'
	else
		mp3root[length] = 0;		// make null terminated

}

void config_write()
{
	char ini_file[MAX_PATH], *p;
	char string[32];
	GetModuleFileName(plugin.hDllInstance,ini_file,sizeof(ini_file));
	p=ini_file+lstrlen(ini_file);
	while (p >= ini_file && *p != '\\') p--;
	if (++p >= ini_file) *p = 0;
	lstrcat(ini_file,"gen_httpsrv.ini");

	wsprintf(string,"%d",con_port);
	WritePrivateProfileString(szAppName,"Port",string,ini_file);
	wsprintf(string,"%d",hide_error);
	WritePrivateProfileString(szAppName,"HideErrors",string,ini_file);
	wsprintf(string,"%d",frames);
	WritePrivateProfileString(szAppName,"Frames",string,ini_file);
	wsprintf(string,"%d",securepassword);
	WritePrivateProfileString(szAppName,"Securepassword",string,ini_file);
	wsprintf(string,"%d",pass_debug);
	WritePrivateProfileString(szAppName,"PassDebug",string,ini_file);

	wsprintf(string,"%d",title_refresh);
	WritePrivateProfileString(szAppName,"TitleRefresh",string,ini_file);

	wsprintf(string,"%d",show_other_files);
	WritePrivateProfileString(szAppName,"ShowOtherFiles",string,ini_file);
	wsprintf(string,"%d",dl_other_files);
	WritePrivateProfileString(szAppName,"DLOtherFiles",string,ini_file);
	wsprintf(string,"%d",dl_wa_files);
	WritePrivateProfileString(szAppName,"DLWaFiles",string,ini_file);

	WritePrivateProfileString(szAppName,"mp3root",mp3root,ini_file);
	WritePrivateProfileString(szAppName,"pagetitle",pagetitle,ini_file);
	WritePrivateProfileString(szAppName,"filetypes",filetypes,ini_file);
	WritePrivateProfileString(szAppName,"logfiledir",logfiledir,ini_file);
	WritePrivateProfileString(szAppName,"playlistdir",playlistdir,ini_file);

}




// --------------------------------------------------------------------------------
// Open log in user's favourite text editor - Registry! Aaargh!
void ViewLog()
{
	/*

	'log' files might not have an association, so open it with whatever
	program opens text files.

	- enumerate keys until '.log' is found
	- if it's not, look for '.txt'
	- find filetype (eg. txtfile)
	- enumerate keys until 'filetype' is found
	- read filetype\open\command
	- if that goes arse over tit, just run 'notepad.exe <logfile>', but that's cheating.

	*/

	// Ah, sod it, let's just run notepad!


	// Find log file, in winamp plugin directory (same dir as gen_httpsrv.dll)
	char logfilepath[MAX_PATH], *p;

	if (lstrlen(logfiledir) == 0)
	{
		GetModuleFileName(plugin.hDllInstance,logfilepath,sizeof(logfilepath));
		p=logfilepath+lstrlen(logfilepath);
		while (p >= logfilepath && *p != '\\') p--;
		if (++p >= logfilepath) *p = 0;
	}
	else
	{
		lstrcpy(logfilepath,logfiledir);
		lstrcat(logfilepath,"\\");
	}

	char logfilecmd[MAX_PATH];
	//lstrcat(logfile,"gen_httpsrv_log.log);
	wsprintf(logfilecmd,"notepad.exe %shttpsrv.log",logfilepath);

	WinExec(logfilecmd,SW_SHOWDEFAULT);

}
	

BOOL CALLBACK NewConfigWndProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	return false;
}

// --------------------------------------------------------------------------------
// Entry point for new Config window, using Property Sheets instead of a single dialog.
void NewConfig()
{
	PROPSHEETPAGE psp[6];
	PROPSHEETHEADER psh;

	psp[0].dwSize = sizeof(PROPSHEETPAGE);
	psp[0].dwFlags = PSP_USEICONID | PSP_USETITLE;
	psp[0].hInstance = plugin.hDllInstance;
	psp[0].pszTemplate = MAKEINTRESOURCE(IDD_ABOUT);
	psp[0].pszIcon = MAKEINTRESOURCE(IDI_WINAMP);
	psp[0].pfnDlgProc = ConfigAboutWndProc;
	psp[0].pszTitle = "About WAWI";
	psp[0].lParam = 0;
	psp[0].pfnCallback = NULL;

	psp[1].dwSize = sizeof(PROPSHEETPAGE);
	psp[1].dwFlags = PSP_USEICONID | PSP_USETITLE;
	psp[1].hInstance = plugin.hDllInstance;
	psp[1].pszTemplate = MAKEINTRESOURCE(IDD_SERVER);
	psp[1].pszIcon = MAKEINTRESOURCE(IDI_SERVER);
	psp[1].pfnDlgProc = ConfigServerWndProc;
	psp[1].pszTitle = "Server";
	psp[1].lParam = 0;
	psp[1].pfnCallback = NULL;

	psp[2].dwSize = sizeof(PROPSHEETPAGE);
	psp[2].dwFlags = PSP_USEICONID | PSP_USETITLE;
	psp[2].hInstance = plugin.hDllInstance;
	psp[2].pszTemplate = MAKEINTRESOURCE(IDD_USERS);
	psp[2].pszIcon = MAKEINTRESOURCE(IDI_USERS);
	psp[2].pfnDlgProc = ConfigUsersWndProc;
	psp[2].pszTitle = "Users";
	psp[2].lParam = 0;
	psp[2].pfnCallback = NULL;

	psp[3].dwSize = sizeof(PROPSHEETPAGE);
	psp[3].dwFlags = PSP_USEICONID | PSP_USETITLE;
	psp[3].hInstance = plugin.hDllInstance;
	psp[3].pszTemplate = MAKEINTRESOURCE(IDD_WEBPAGE);
	psp[3].pszIcon = MAKEINTRESOURCE(IDI_WEBPAGE);
	psp[3].pfnDlgProc = ConfigWebPageWndProc;
	psp[3].pszTitle = "Web Page";
	psp[3].lParam = 0;
	psp[3].pfnCallback = NULL;

	psp[4].dwSize = sizeof(PROPSHEETPAGE);
	psp[4].dwFlags = PSP_USEICONID | PSP_USETITLE;
	psp[4].hInstance = plugin.hDllInstance;
	psp[4].pszTemplate = MAKEINTRESOURCE(IDD_BROWSE);
	psp[4].pszIcon = MAKEINTRESOURCE(IDI_BROWSE);
	psp[4].pfnDlgProc = ConfigBrowseWndProc;
	psp[4].pszTitle = "Music Collection";
	psp[4].lParam = 0;
	psp[4].pfnCallback = NULL;

	psp[5].dwSize = sizeof(PROPSHEETPAGE);
	psp[5].dwFlags = PSP_USEICONID | PSP_USETITLE;
	psp[5].hInstance = plugin.hDllInstance;
	psp[5].pszTemplate = MAKEINTRESOURCE(IDD_LOG);
	psp[5].pszIcon = MAKEINTRESOURCE(IDI_LOG);
	psp[5].pfnDlgProc = ConfigLogWndProc;
	psp[5].pszTitle = "Log";
	psp[5].lParam = 0;
	psp[5].pfnCallback = NULL;


	psh.dwSize = sizeof(PROPSHEETHEADER);
	psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE;
	psh.hwndParent = plugin.hwndParent;
	psh.hInstance = plugin.hDllInstance;
	psh.pszIcon = NULL;
	psh.pszCaption = "Winamp Web Interface Config";
	psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
	psh.nStartPage = 0;
	psh.ppsp = (LPCPROPSHEETPAGE) &psp;
	psh.pfnCallback = NULL;

	PropertySheet(&psh);
	config_write();
	ConfigWnd = NULL;
	return;
}