nsCOMPtr<nsIFile> searchFile; nsCAutoString searchFileURLSpec;
NS_WITH_SERVICE(nsIFileLocator, locator, kFileLocatorCID, &rv); if (NS_FAILED(rv) || !locator)	return(NS_ERROR_FAILURE);
InternetSearchDataSource::GetCategoryList(){	nsIRDFDataSource	*ds = nsnull;	nsresult rv = nsComponentManager::CreateInstance(kRDFXMLDataSourceCID,			nsnull, NS_GET_IID(nsIRDFDataSource), (void**) &ds);	if (NS_FAILED(rv))	return(rv);	if (!ds)		return(NS_ERROR_UNEXPECTED);	categoryDataSource = do_QueryInterface(ds);	NS_RELEASE(ds);	ds = nsnull;	if (!categoryDataSource)	return(NS_ERROR_UNEXPECTED);	nsCOMPtr<nsIRDFRemoteDataSource> remoteCategoryDataSource (do_QueryInterface(categoryDataSource));	if (!remoteCategoryDataSource)	return(NS_ERROR_UNEXPECTED);	// get search.rdf		  nsCOMPtr<nsIFile> searchFile;  nsCAutoString searchFileURLSpec;  rv = NS_GetSpecialDirectory(NS_APP_SEARCH_50_FILE, getter_AddRefs(searchFile));  if (NS_FAILED(rv)) return rv;  NS_GetURLSpecFromFile(searchFile, searchFileURLSpec);  if (NS_FAILED(rv)) return rv;	rv = remoteCategoryDataSource->Init(searchFileURLSpec.get());  if (NS_FAILED(rv)) return rv;    	// synchronous read	rv = remoteCategoryDataSource->Refresh(PR_TRUE);	if (NS_FAILED(rv))	return(rv);	// ensure that all search engine references actually exist; if not, forget about them	PRBool				isDirtyFlag = PR_FALSE;	nsCOMPtr<nsIRDFContainer> 	categoryRoot;	rv = gRDFC->MakeSeq(categoryDataSource, kNC_SearchCategoryRoot, getter_AddRefs(categoryRoot));	if (NS_FAILED(rv))	return(rv);	if (!categoryRoot)	return(NS_ERROR_UNEXPECTED);	rv = categoryRoot->Init(categoryDataSource, kNC_SearchCategoryRoot);	if (NS_FAILED(rv))	return(rv);	PRInt32		numCategories = 0;	rv = categoryRoot->GetCount(&numCategories);	if (NS_FAILED(rv))	return(rv);	// Note: one-based	for (PRInt32 catLoop=1; catLoop <= numCategories; catLoop++)	{		nsCOMPtr<nsIRDFResource>	aCategoryOrdinal;		rv = gRDFC->IndexToOrdinalResource(catLoop,			getter_AddRefs(aCategoryOrdinal));		if (NS_FAILED(rv))	break;		if (!aCategoryOrdinal)	break;		nsCOMPtr<nsIRDFNode>		aCategoryNode;		rv = categoryDataSource->GetTarget(kNC_SearchCategoryRoot, aCategoryOrdinal,			PR_TRUE, getter_AddRefs(aCategoryNode));		if (NS_FAILED(rv))	break;		nsCOMPtr<nsIRDFResource> aCategoryRes (do_QueryInterface(aCategoryNode));		if (!aCategoryRes)	break;		const	char			*catResURI = nsnull;		aCategoryRes->GetValueConst(&catResURI);		if (!catResURI)		break;		nsAutoString		categoryStr;		categoryStr.AssignWithConversion(kURINC_SearchCategoryPrefix);		categoryStr.AppendWithConversion(catResURI);		nsCOMPtr<nsIRDFResource>	searchCategoryRes;		if (NS_FAILED(rv = gRDFService->GetUnicodeResource(categoryStr.get(),			getter_AddRefs(searchCategoryRes))))	break;		nsCOMPtr<nsIRDFContainer> categoryContainer;		rv = gRDFC->MakeSeq(categoryDataSource, searchCategoryRes,			getter_AddRefs(categoryContainer));		if (NS_FAILED(rv))	continue;		rv = categoryContainer->Init(categoryDataSource, searchCategoryRes);		if (NS_FAILED(rv))	return(rv);		PRInt32		numEngines = 0;		rv = categoryContainer->GetCount(&numEngines);		if (NS_FAILED(rv))	break;		// Note: one-based, and loop backwards as we might be removing entries		for (PRInt32 engineLoop=numEngines; engineLoop >= 1; engineLoop--)		{			nsCOMPtr<nsIRDFResource>	aEngineOrdinal;			rv = gRDFC->IndexToOrdinalResource(engineLoop,				getter_AddRefs(aEngineOrdinal));			if (NS_FAILED(rv))	break;			if (!aEngineOrdinal)	break;			nsCOMPtr<nsIRDFNode>		aEngineNode;			rv = categoryDataSource->GetTarget(searchCategoryRes, aEngineOrdinal,				PR_TRUE, getter_AddRefs(aEngineNode));			if (NS_FAILED(rv))	break;			nsCOMPtr<nsIRDFResource> aEngineRes (do_QueryInterface(aEngineNode));			if (!aEngineRes)	break;			if (isSearchCategoryEngineURI(aEngineRes))			{				nsCOMPtr<nsIRDFResource>	trueEngine;				rv = resolveSearchCategoryEngineURI(aEngineRes,					getter_AddRefs(trueEngine));				if (NS_FAILED(rv) || (!trueEngine))				{					// unable to resolve, so forget about this engine reference#ifdef	DEBUG					const	char		*catEngineURI = nsnull;					aEngineRes->GetValueConst(&catEngineURI);					if (catEngineURI)					{						printf("**** Stale search engine reference to '%s'\n",							catEngineURI);					}#endif					nsCOMPtr<nsIRDFNode>	staleCatEngine;					rv = categoryContainer->RemoveElementAt(engineLoop, PR_TRUE,						getter_AddRefs(staleCatEngine));					isDirtyFlag = PR_TRUE;				}			}		}	}	if (isDirtyFlag == PR_TRUE)	{		if (remoteCategoryDataSource)		{			remoteCategoryDataSource->Flush();		}	}	return(rv);}