/* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
**
** Copyright (C) 2012-2012 Opera Software ASA.  All rights reserved.
**
** This file is part of the Opera web browser.  It may not be distributed
** under any circumstances.
*/

#ifndef __OP_SERVER_NAME_IMPL_H__
#define __OP_SERVER_NAME_IMPL_H__

#include "modules/url/url_sn.h"
#include "modules/network/op_server_name.h"

class OpServerNameImpl:public OpServerName
{
public:
	/** Destructor. */
	virtual ~OpServerNameImpl(){ OP_DELETE(m_server_name); }

	/** Get the current SocketAddress. */
	virtual OpSocketAddress* SocketAddress() { return m_server_name->SocketAddress(); }

	/** Set this Host's IP address from the string */
	virtual OP_STATUS SetHostAddressFromString(const OpStringC& address) { return m_server_name->SetHostAddressFromString(address); }

	/** Return the unicode version of this server name. */
	virtual const uni_char*	UniName() const { return m_server_name->UniName(); }

	/** Return the 8-bit version of this server's name. */
	virtual const char *Name() const { return m_server_name->Name(); }

	/** Return the number of components in the servername. */
	virtual uint32 GetNameComponentCount() { return m_server_name->GetNameComponentCount(); }

	/** Return a OpStringC8 object referencing the given name component item of the servername, empty if no component. */
	virtual OpStringC8 GetNameComponent(uint32 item) { return m_server_name->GetNameComponent(item); }

	/** Return a pointer to the servername object for the parent domain.
	 *
	 *	@param servername Pointer to the servername object for the parent domain. Servername will be NULL if no parent domain exists.
	 *	@return OK or ERR_NO_MEMORY
	 */
	virtual OP_STATUS GetParentDomain(OpServerName *&servername)
	{
		servername = NULL;
		if (m_server_name->GetParentDomain())
		{
			servername = OP_NEW(OpServerNameImpl, (m_server_name->GetParentDomain()));
			if (!servername)
				return OpStatus::ERR_NO_MEMORY;
		}
		return OpStatus::OK;
	}

	/** Get a pointer for the most specific common domain name the two domains, NULL if no common domain exists.
	 *
	 *  E.g. the common domain of "www.opera.com" and "web.opera.com" is "opera.com", for
	 *	"www.microsoft.com" and "www.opera.com" it is "com", and for "www.opera.com" and
	 *	"www.opera.com" there is no common domain and it will return a NULL pointer.
	 *
	 *	@param	servername	The servername we are comparing with.
	 *	@param	result		The domain common between this name and the other name, may be NULL.
	 *	@return OK or ERR_NO_MEMORY
	 */
	virtual OP_STATUS GetCommonDomain(OpServerName *servername, OpServerName *&result)
	{
		result = NULL;
		ServerName *temp = m_server_name->GetCommonDomain(((OpServerNameImpl*)servername)->m_server_name);
		if (temp)
		{
			result = OP_NEW(OpServerNameImpl, (temp));
			if (!result)
				return OpStatus::ERR_NO_MEMORY;
		}
		return OpStatus::OK;
	}

#ifdef _ASK_COOKIE
	/** Set the cookie filter setting. */
	virtual void SetAcceptCookies(COOKIE_MODES mode) { return m_server_name->SetAcceptCookies(mode); }

	/** Get the cookie filter setting.
	 *
	 * @param follow_domain	If set the top parent domain is used.
	 * @param use_local		Use the domain "local" if follow_domain==TRUE and there is no parent domain.
	 */
	virtual COOKIE_MODES GetAcceptCookies(BOOL follow_domain = FALSE, BOOL use_local = TRUE) { return m_server_name->GetAcceptCookies(follow_domain, use_local); }

	/** Set the illegal path filter.
	 *
	 * @param mode Should cookies be deleted on exit.
	 */
	virtual void SetAcceptIllegalPaths(COOKIE_ILLPATH_MODE mode) { m_server_name->SetAcceptIllegalPaths(mode); }

	/** Get the illegal path filter.
	 *
	 * @param follow_domain	If set the top parent domain is used.
	 * @param use_local		Use the domain "local" if follow_domain==TRUE and there is no parent domain.
	 */
	virtual COOKIE_ILLPATH_MODE GetAcceptIllegalPaths(BOOL follow_domain = FALSE, BOOL use_local = TRUE) { return m_server_name->GetAcceptIllegalPaths(follow_domain, use_local); }

	/** Set Domain specific empty on exit.
	 *
	 * @param mode Should cookies be deleted on exit.
	 */
	virtual void SetDeleteCookieOnExit(COOKIE_DELETE_ON_EXIT_MODE mode) { return m_server_name->SetDeleteCookieOnExit(mode); }

	/** Get the delete cookie on exit setting.
	 *
	 * @param follow_domain	If set the top parent domain is used.
	 * @param use_local		Use the domain "local" if follow_domain==TRUE and there is no parent domain.
	 */
	virtual COOKIE_DELETE_ON_EXIT_MODE GetDeleteCookieOnExit(BOOL follow_domain = FALSE, BOOL use_local = TRUE) { return m_server_name->GetDeleteCookieOnExit(follow_domain, use_local); }
#endif
	/** Set the third party filter setting. */
	virtual void SetAcceptThirdPartyCookies(COOKIE_MODES mod) { return m_server_name->SetAcceptThirdPartyCookies(mod); }

	/** Get the third party filter setting.
	 * @param follow_domain	If set the top parent domain is used.
	 * @param first 		If set, the "local" domain is used.
	 */
	virtual COOKIE_MODES GetAcceptThirdPartyCookies(BOOL follow_domain = FALSE, BOOL first = TRUE) { return m_server_name->GetAcceptThirdPartyCookies(follow_domain, first); }

	/** Are we permitted to pass usernames (and passwords) embedded in the URL to this server without challenging the user? */
	virtual BOOL GetPassUserNameURLsAutomatically(const OpStringC8 &p_name) { return m_server_name->GetPassUserNameURLsAutomatically(p_name); }

	/** Set whether or not we are permitted to pass usernames (and passwords) embedded in the URL to this server without challenging the user? */
	virtual OP_STATUS SetPassUserNameURLsAutomatically(const OpStringC8 &p_name) { return m_server_name->SetPassUserNameURLsAutomatically(p_name); }

#ifdef TRUST_RATING
	/** Set Time To Live for the trust rating. */
	virtual void SetTrustTTL(unsigned int TTL) { return m_server_name->SetTrustTTL(TTL); }

	/** Get trust rating level. */
	virtual TrustRating GetTrustRating() { return m_server_name->GetTrustRating(); }

	/** Set trust rating level. */
	virtual void SetTrustRating(TrustRating new_trust_rating) { m_server_name->SetTrustRating(new_trust_rating); }

	/** Is the trust rating bypassed for this server? */
	virtual BOOL GetTrustRatingBypassed() { return m_server_name->GetTrustRatingBypassed(); }

	/** Specify that trust rating warning is to be bypassed for this server. */
	virtual void SetTrustRatingBypassed(BOOL new_trust_rating_bypassed) { m_server_name->SetTrustRatingBypassed(new_trust_rating_bypassed); }

	/** Add a url to the list of fraud urls we will warn about. */
	virtual OP_STATUS AddTrustRatingUrl(const OpStringC &url, int id) {TRAPD(op_err, m_server_name->AddUrlL(url, id)); return op_err; }

	/** See if a fraud 	url can be found. */
	virtual BOOL FindTrustRatingUrl(const OpStringC &url) { return m_server_name->IsUrlInFraudList(url, NULL); }

	/** Add a regular expression to the list of fraud expressions we will warn about. */
	virtual OP_STATUS AddTrustRatingRegExp(const OpStringC &regexp) { TRAPD(op_err, m_server_name->AddRegExpL(regexp)); return op_err; }

	/** Retrieve first regular expression item. */
	virtual FraudListItem *GetFirstTrustRatingRegExp() { return m_server_name->GetFirstRegExpItem(); }

	/** Is fraud list empty? */
	virtual BOOL FraudListIsEmpty() { return m_server_name->FraudListIsEmpty(); }

	/** Add advisory for a URL.
	 *
	 *  @param homepage_url	url for the advisory.
	 *  @param advisory_url	url for the advisory.
	 *  @param text
	 *  @param type
	 */
	virtual void AddAdvisoryInfo(const uni_char *homepage_url, const uni_char *advisory_url, const uni_char *text, unsigned int src, ServerSideFraudType type) { m_server_name->AddAdvisoryInfo(homepage_url, advisory_url, text, src, type); }

	/** Retrieve the advisory for a URL.
	 *
	 *  @param url	url for the advisory.
	 *  @return 	Advisory containing more info about the url.
	 */
	virtual Advisory *GetAdvisory(const OpStringC &url) { return m_server_name->GetAdvisory(url); }
#endif

private:
	OpServerNameImpl(ServerName *server_name):m_server_name(server_name) { }
	ServerName *m_server_name;
	friend class Network;
	friend class OpURL;
};

#endif
