nsNativeFileSpec	native(fileSpec); nsAutoString		searchURL; native.GetLeafName(searchURL); if (contextType == nsIInternetSearchContext::ICON_DOWNLOAD_CONTEXT) {  if ((extensionOffset = searchURL.RFindChar(PRUnichar('.'))) > 0) { searchURL.Truncate(extensionOffset); searchURL.AppendWithConversion(".src"); } } searchURL.InsertWithConversion(kURINC_SearchCategoryEngineBasenamePrefix, 0);
InternetSearchDataSource::saveContents(nsIChannel* channel, nsIInternetSearchContext *context, PRUint32	contextType){	nsresult	rv = NS_OK;	if (!channel)	return(NS_ERROR_UNEXPECTED);	if (!context)	return(NS_ERROR_UNEXPECTED);	// get real URI	nsCOMPtr<nsIURI>	channelURI;	if (NS_FAILED(rv = channel->GetURI(getter_AddRefs(channelURI))))		return(rv);	if (!channelURI)		return(NS_ERROR_NULL_POINTER);	char			*spec = nsnull;	if (NS_FAILED(rv = channelURI->GetSpec(&spec)))		return(rv);	if (!spec)		return(NS_ERROR_NULL_POINTER);	// get base name	nsAutoString		baseName; baseName.AssignWithConversion(spec);	Recycle(spec);	PRInt32			slashOffset = baseName.RFindChar(PRUnichar('/'));	if (slashOffset < 0)		return(NS_ERROR_UNEXPECTED);	baseName.Cut(0, slashOffset+1);	if (baseName.Length() < 1)	return(NS_ERROR_UNEXPECTED);	// make sure that search engines are .src files	PRInt32	extensionOffset;	if (contextType == nsIInternetSearchContext::ENGINE_DOWNLOAD_CONTEXT)	{		extensionOffset = baseName.RFind(".src", PR_TRUE);		if ((extensionOffset < 0) || (extensionOffset != (PRInt32)(baseName.Length()-4)))		{			return(NS_ERROR_UNEXPECTED);		}	}	nsFileSpec	nativeDir;	if (NS_FAILED(rv = GetSearchFolder(nativeDir)))		return(rv);	const PRUnichar	*dataBuf = nsnull;	if (NS_FAILED(rv = context->GetBufferConst(&dataBuf)))	return(rv);	// if no data, then nothing to do	// Note: do this before opening file, as it would be truncated	PRInt32		bufferLength = 0;	if (NS_FAILED(context->GetBufferLength(&bufferLength)))	return(rv);	if (bufferLength < 1)	return(NS_OK);	nsFileSpec	fileSpec(nativeDir);	fileSpec += baseName;	// save data to file	// Note: write out one character at a time, as we might be dealing	//       with binary data (such as 0x00) [especially for images]	nsOutputFileStream	outputStream(fileSpec);	if (!outputStream.failed())	{		for (PRInt32 loop=0; loop < bufferLength; loop++)		{			outputStream.put((char)(dataBuf[loop]));		}		outputStream.close();		// save the file basename		nsNativeFileSpec	native(fileSpec);		nsAutoString		searchURL;		native.GetLeafName(searchURL);		if (contextType == nsIInternetSearchContext::ICON_DOWNLOAD_CONTEXT)		{			// tweak the URI to point to the search engine file, not this icon file			if ((extensionOffset = searchURL.RFindChar(PRUnichar('.'))) > 0)			{				searchURL.Truncate(extensionOffset);				searchURL.AppendWithConversion(".src");			}		}		searchURL.InsertWithConversion(kURINC_SearchCategoryEngineBasenamePrefix, 0);		if (contextType == nsIInternetSearchContext::ENGINE_DOWNLOAD_CONTEXT)		{#ifdef	XP_MAC			// set appropriate Mac file type/creator for search engine files			fileSpec.SetFileTypeAndCreator('issp', 'fndf');#endif			// check suggested category hint			const PRUnichar	*hintUni = nsnull;			rv = context->GetHintConst(&hintUni);			// update graph with various required info			SaveEngineInfoIntoGraph(searchURL, hintUni, nsAutoString(),				nsAutoString(dataBuf), PR_FALSE);		}		else if (contextType == nsIInternetSearchContext::ICON_DOWNLOAD_CONTEXT)		{			// update graph with icon info			nsFileURL	jpgIconFileURL(fileSpec);			nsAutoString	iconURL; iconURL.AssignWithConversion(jpgIconFileURL.GetURLString());			SaveEngineInfoIntoGraph(searchURL, nsnull, iconURL, nsAutoString(), PR_FALSE);		}	}	// after we're all done with the data buffer, get rid of it	context->Truncate();	return(rv);}