/**********************************************/
/*                                            */    
/* inetscape.cpp                              */
/*                                            */
/* netscape management                        */
/*   - scol plugin for netscape               */
/*   - scol protocol                          */
/*                                            */
/* scol v 4                                   */
/*                                            */
/* by Loïc Berthelot, CryoNetworks, may 2001  */
/*                                            */
/**********************************************/




#include <windows.h>
#include <stdio.h>
#include "scolinstaller.h"


// TODO TO DO : vérif IsNetscapeDefault


// Modifications History
//
//$ LB (28/08/2001) : separate netscape and netscape6 management
//				    : ==> add netscape6PathTab variable and EnumNetscape6Paths function
//					: ==> modif FindNetscape declaration (now includes the tab to fill in parameter, and its length)
//					: ==> modif netscapeInitData function




/****************************************************************************************************************/
/*                                                                                                              */
/*          C O N T E N T                                                                                       */
/*                                                                                                              */
/*                                                                                                              */
/*     I N T E R N A L        B O D Y                                                                           */
/*                                                                                                              */
/* void netscapeInitData ()                                                                                     */
/*                                                                                                              */
/*                                                                                                              */
/*     E X T E R N A L        B O D Y                                                                           */
/*                                                                                                              */
/* DWORD FindNetscape      (LPCTSTR keyName, LPCTSTR exeDirectory, LPCTSTR exeName, char** tab, int *tabLength) */
/*                                                                                                              */
/* DWORD IsNetscapeDefault ()                                                                                   */
/*                                                                                                              */
/* DWORD EnumNetscapePaths (LPCTSTR netscapePathVar)                                                            */
/*                                                                                                              */
/* DWORD EnumNetscape6Paths (LPCTSTR netscapePathVar)                                                           */
/*                                                                                                              */
/****************************************************************************************************************/
















/***********************************************************************************/
/***********************************************************************************/
/**                                                                               **/
/**     D A T A                                                                   **/
/**                                                                               **/
/***********************************************************************************/
/***********************************************************************************/

#define NETSCAPE_KEYPATH "Software\\Netscape\\"

char** netscapePathsTab = NULL;
int    netscapePathsTabLength = 0;

char** netscape6PathsTab = NULL;
int    netscape6PathsTabLength = 0;






/***********************************************************************************/
/***********************************************************************************/
/**                                                                               **/
/**     I N T E R N A L        B O D Y                                            **/
/**                                                                               **/
/***********************************************************************************/
/***********************************************************************************/







void netscapeInitData ()
{
int i;

  setvar ("IS_NETSCAPE_INSTALLED", "");

  if (netscapePathsTab)
  {
	  for (i=0; i < netscapePathsTabLength; i++)
		  if (netscapePathsTab[i])
		  {
			  free (netscapePathsTab[i]);
			  netscapePathsTab = NULL;
		  }
	  free (netscapePathsTab);
	  netscapePathsTab = NULL;
  }

  netscapePathsTabLength = 0;


  //$ LB (28/08/2001) : add netscape6PathsTab init
  if (netscape6PathsTab)
  {
	  for (i=0; i < netscape6PathsTabLength; i++)
		  if (netscape6PathsTab[i])
		  {
			  free (netscape6PathsTab[i]);
			  netscape6PathsTab = NULL;
		  }
	  free (netscape6PathsTab);
	  netscape6PathsTab = NULL;
  }

  netscape6PathsTabLength = 0;

}




















/***********************************************************************************/
/***********************************************************************************/
/**                                                                               **/
/**     E X T E R N A L        B O D Y                                            **/
/**                                                                               **/
/***********************************************************************************/
/***********************************************************************************/










/*****************************************************************/
/*                                                               */
/* DWORD FindNetscape (LPCTSTR keyName,                          */
/*                     LPCTSTR exeDirectory                      */
/*                     LPCTSTR exeName,                          */
/*                     char*** tab, int *tabLength)              */
/*                                                               */
/* find netscape navigator in registry with the key, given in    */
/* parameter. if netscape are found, paths are stored in         */
/* netscapePathsTab                                              */
/*                                                               */
/* parameters :                                                  */
/*                                                               */
/*   LPCTSTR keyName : netscape root key name in registry        */
/*                   (netscape 6 different from netscape < 6)    */
/*                                                               */
/*   LPCTSTR exeDirectory : directory to get the executable file */
/*                                                               */
/*   LPCTSTR exeName : netscape executable file name             */
/*                                                               */
/*****************************************************************/
//$ LB (28/08/2001) : add tab and tabLength parameters
DWORD FindNetscape(LPCTSTR keyName, LPCTSTR exeDirectory, LPCTSTR exeName, char*** tab, int *tabLength)
{
  BYTE buf[512];
  DWORD bufLen;

  TCHAR version [256];
  DWORD versionSize = 256;
  TCHAR versionCompleteName[512];

  TCHAR exeCompleteName[512];
  TCHAR netscapeCompletePath [512];
  TCHAR keyCompleteName[1024];
  HKEY  ntscpKey, ntscpVersionKey;
  DWORD index = (DWORD)0;
  LONG  code;



  sprintf ((LPTSTR)keyCompleteName, "%s%s", NETSCAPE_KEYPATH, keyName);

  //open the netscape root key
  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)keyCompleteName, 0, KEY_READ, &ntscpKey) != ERROR_SUCCESS) 
  		return 0;


  //enumeration of all registered versions 
  while ((code = RegEnumKeyEx  (ntscpKey, index, (LPTSTR)version, &versionSize, 
	                    NULL, NULL, 0, NULL))
		 != ERROR_NO_MORE_ITEMS)
  {
	
		//open the key of current enumerated version
		sprintf ((char*)versionCompleteName, "%s\\%s\\Main", keyCompleteName, version);


		if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, (LPCTSTR)versionCompleteName, 0, KEY_READ, &ntscpVersionKey) == ERROR_SUCCESS)
		{

			//get the installation path of this version
			bufLen = (DWORD)sizeof(buf);
			if (RegQueryValueEx(ntscpVersionKey, "Install Directory", 0, NULL, (BYTE*)buf, &bufLen) != ERROR_SUCCESS) 
			{
				RegCloseKey(ntscpVersionKey);
				
			}
			else
			{
					//last char of Netscape 6 value is a '\'.
					//remove it.

					if (buf [strlen((LPTSTR)buf)-1] == '\\')
						buf [strlen((LPTSTR)buf)-1] = (TCHAR)'\0';


					//check the executable file.
					//if present, add install directory to the netscape paths tab
					sprintf ((LPTSTR)netscapeCompletePath, "%s%s", buf, exeDirectory);
					sprintf ((LPTSTR)exeCompleteName, "%s%s", netscapeCompletePath, exeName);


					if ( exist((LPTSTR)exeCompleteName) > 0)
					{
						(*tabLength)++;

						(*tab) = (TCHAR**)realloc ((*tab), (*tabLength) * sizeof (TCHAR*));

						 (*tab) [(*tabLength)-1] = (TCHAR*) malloc (strlen ((LPCTSTR)netscapeCompletePath)+1);

						strcpy ((LPTSTR)(*tab)[(*tabLength)-1], (LPCTSTR)netscapeCompletePath);
					}
			}

			 RegCloseKey(ntscpVersionKey);
		}

	   //search for next key
	   index++;
	   versionSize = 256;
       
  } //end of enumeration


  RegCloseKey(ntscpKey);

  if ((*tabLength) > 0)
	  setvar ("IS_NETSCAPE_INSTALLED", "YES");


  return 1;
}











/****************************************/
/*                                      */
/* DWORD IsNetscapeDefault()            */
/*                                      */
/* check if Netscape is the default     */
/* web browser. Set the NETSCAPEDEFAULT */
/* environnement variable (value "YES"  */
/* or "NO"), and return 1 or 0.         */
/****************************************/
DWORD IsNetscapeDefault()
{
TCHAR html[1024];
TCHAR comm[1024];
LONG  n,i;

  setvar("NETSCAPEDEFAULT","");

	n=1024;
    if (RegQueryValue(HKEY_CLASSES_ROOT,".htm",html,&n)!=ERROR_SUCCESS) return 0;
	sprintf(html,"%s\\shell\\open\\command",html);
	n=1024;
	if (RegQueryValue(HKEY_CLASSES_ROOT,html,comm,&n)!=ERROR_SUCCESS) return 0;
	n=strlen(comm);
	for(i=0;i<n;i++) if (!strnicmp(&comm[i],"netscape",8))
	{
		/*  NETSCAPE */
    setvar("NETSCAPEDEFAULT","YES");
		return 1;
	}
	return 0;
}







/**************************************************************/
/*                                                            */
/* DWORD EnumNetscapePathsEngine (LPTSTR netscapePathVar,     */
/*                                char*** tab, int* tabLength) */
/*                                                            */
/* process EnumNetscapePaths and EnumNetscape6Paths functions */
/*                                                            */
/**************************************************************/
DWORD EnumNetscapePathsEngine (LPTSTR netscapePathVar, char*** tab, int* tabLength)
{

	char *path;

	//if no netscape had been previously found,
	//there is nothing to enumerate
	if ((*tabLength) <= 0)
	{
		if ((*tab))
		{
			free ((*tab));
			(*tab) = NULL;
		}
		setvar (netscapePathVar, "");
		return 0;
	}

	//get the last netscape path found,
	//and store it into the environnement variable
	(*tabLength)--;


	path = (*tab)[(*tabLength)];
	if ( (path[strlen(path)-1]=='/') || (path[strlen(path)-1]=='\\') )
		path[strlen(path)-1]='\0';
	setvar (netscapePathVar, (*tab)[(*tabLength)]);

	//remove this element from the netscape path tab
	if ((*tab) [(*tabLength)])
	{
		free ((*tab) [(*tabLength)]);

		(*tab) [(*tabLength)] = NULL;
	}

	//means one element has been enumerated
	return 1;
}







/*****************************************************/
/*                                                   */
/* DWORD EnumNetscapePaths (LPTSTR netscapePathVar)  */
/*                                                   */
/* pull a netscape path from the netscape path       */
/* tab, and store it into the environnement var      */
/* given as parameter.                               */
/* Return 1 if a netscape path has been pulled,      */
/* 0 if no more data in netscape path tab.           */
/*                                                   */
/*****************************************************/
DWORD EnumNetscapePaths (LPTSTR netscapePathVar)
{
	return EnumNetscapePathsEngine (netscapePathVar, &netscapePathsTab, &netscapePathsTabLength);
}





/*****************************************************/
/*                                                   */
/* DWORD EnumNetscape6Paths (LPTSTR netscapePathVar) */
/*                                                   */
/* pull a netscape6 path from the netscape6 path     */
/* tab, and store it into the environnement var      */
/* given as parameter.                               */
/* Return 1 if a netscape6 path has been pulled,     */
/* 0 if no more data in netscape6 path tab.          */
/*                                                   */
/*****************************************************/
DWORD EnumNetscape6Paths (LPTSTR netscapePathVar)
{
	return EnumNetscapePathsEngine (netscapePathVar, &netscape6PathsTab, &netscape6PathsTabLength);
}