nsCOMPtr<nsIPref> pPref(do_GetService(kPrefCID, &rv));
nsCOMPtr<nsIPref> pPref(do_GetService(NS_PREF_CONTRACTID, &rv));
PRBool DIR_SetServerPosition(nsVoidArray *wholeList, DIR_Server *server, PRInt32 position){    nsresult rv = NS_OK;    nsCOMPtr<nsIPref> pPref(do_GetService(kPrefCID, &rv));     if (NS_FAILED(rv) || !pPref) 		return PR_FALSE;	PRInt32    i, count, num;	PRBool     resort = PR_FALSE;	DIR_Server *s=nsnull;	switch (position) {	case DIR_POS_APPEND:		/* Do nothing if the request is to append a server that is already		 * in the list.		 */		count = wholeList->Count();		for (i= 0; i < count; i++)		{			if  ((s = (DIR_Server *)wholeList->ElementAt(i)) != nsnull)				if (s == server)					return PR_FALSE;		}		/* In general, if there are any servers already in the list, set the		 * position to the position of the last server plus one.  If there		 * are none, set it to position 1.		 */		if (count > 0)		{			/* Exception to the rule: if the last server is a locked server,			 * find the position of last unlocked server.  If there are no			 * unlocked servers, set the position to 1; otherwise, set it to			 * the position of the last unlocked server plus one.  In either			 * case the list must be resorted (to find the correct position).			 */			s = (DIR_Server *)wholeList->ElementAt(count - 1);			if (DIR_TestFlag(s, DIR_POSITION_LOCKED))			{				DIR_Server *sLast = nsnull;				for (i= 0; i < count; i++)				{					if  ((s = (DIR_Server *)wholeList->ElementAt(i)) != nsnull)						if (!DIR_TestFlag(s, DIR_POSITION_LOCKED))							sLast = s;				}				if (sLast)					server->position = sLast->position + 1;				else					server->position = 1;				resort = PR_TRUE;			}			else				server->position = s->position + 1;		}		else			server->position = 1;		wholeList->AppendElement(server);		if (wholeList == dir_ServerList)			DIR_SendNotification(server, DIR_NOTIFY_ADD, idNone);		break;	case DIR_POS_DELETE:		/* Undeletable servers cannot be deleted.		 */		if (DIR_TestFlag(server, DIR_UNDELETABLE))			return PR_FALSE;		/* Remove the prefs corresponding to the given server.  If the prefName		 * value is nsnull, the server has never been saved and there are no		 * prefs to remove.		 */		if (server->prefName)		{			char *name = PR_smprintf("%s.position", server->prefName);			if (name != nsnull)			{				/* We can't just delete default servers, we can only mark them				 * as "deleted" by setting their position to 0.  Since there				 * doesn't seem to be a way to figure out if a server is a				 * default server we must set the position of all deleted				 * servers to 0.				 */       			DIR_ClearPrefBranch(server->prefName);				pPref->SetIntPref(name, 0);				PR_smprintf_free(name);			}		}		/* If the server is in the server list, remove it.		 */		num = wholeList->IndexOf(server);		if (num >= 0)		{			/* The list does not need to be re-sorted if the server is the			 * last one in the list.			 */			count = wholeList->Count();			if (num == count - 1)			{				wholeList->RemoveElementAt(num);			}			else			{				resort = PR_TRUE;				wholeList->RemoveElement(server);			}			if (wholeList == dir_ServerList)				DIR_SendNotification(server, DIR_NOTIFY_DELETE, idNone);		}		break;	default:		/* See if the server is already in the list.		 */		count = wholeList->Count();		for (i= 0; i < count; i++)		{			if  ((s = (DIR_Server *)wholeList->ElementAt(i)) != nsnull)				if (s == server)					break;		}		/* If the server is not in the list, add it to the beginning and re-sort.		 */		if (s == nsnull)		{			server->position = position;			wholeList->AppendElement(server);			resort = PR_TRUE;			if (wholeList == dir_ServerList)				DIR_SendNotification(server, DIR_NOTIFY_ADD, idNone);		}		/* Servers with locked position values cannot be moved.		 */		else if (DIR_TestFlag(server, DIR_POSITION_LOCKED))			return PR_FALSE;				/* Don't re-sort if the server is already in the requested position.		 */		else if (server->position != position)		{			server->position = position;			wholeList->RemoveElement(server);			wholeList->AppendElement(server);			resort = PR_TRUE;		}		break;	}	/* If necessary, re-sort the server list.  This function tries to keep from	 * calling the sort function when it can, but complicated cases may be	 * more appropriate to leave up to the sort function to see if the list	 * needs to be re-sorted.	 */	if (resort)		resort = DIR_SortServersByPosition(wholeList);	/* Make sure our position changes get saved back to prefs	 */	DIR_SaveServerPreferences(wholeList);	return resort;}