// -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-

//
//  Copyright (C) 2003 Takuro Ashie
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//


#include <nsCOMPtr.h>
#include <docshell/nsIDocShell.h>
#include <nsIDOMDocument.h>
#include <nsIDOMWindow.h>
#include <nsIDOMRange.h>
#include <nsIWebBrowser.h>
#define MOZILLA_STRICT_API
#include <nsEmbedString.h>
#undef MOZILLA_STRICT_API
#include <nsIDOMEventReceiver.h>
#include <nsIWebNavigation.h>
#include <nsISHistory.h>
#include <nsICacheEntryDescriptor.h>
#include "kz-gecko-embed.h"
#include "kz-mozselectionlistener.h"
#include "kz-mozeventlistener.h"

class nsISecureBrowserUI;
class nsISSLStatus;

class KzMozWrapper
{
public:
	KzMozWrapper ();
	virtual ~KzMozWrapper ();

	nsresult Init    (KzGeckoEmbed *kzembed);
	void     Destroy (void);

	//
	// Our own methods
	//

	nsresult GetDocShell        (nsIDocShell    **docShell);
	nsresult GetMainDomDocument (nsIDOMDocument **doc);
	nsresult GetDOMWindow	    (nsIDOMWindow   **aDOMWindow);
	nsresult GetDocument        (nsIDOMDocument **doc);
	nsresult GetDocumentUrl     (nsACString &url);
	nsresult GetContentViewer   (nsIContentViewer **aViewer);

	nsresult CanCutSelection  (PRBool *result);
	nsresult CanCopySelection (PRBool *result);
	nsresult CanPaste         (PRBool *result);
	nsresult CutSelection     (void);
	nsresult CopySelection    (void);
	nsresult Paste            (void);
	nsresult SelectAll        (void);

	// for selection
	nsresult GetSelection            (nsISelection **selection);
#if 0
	nsresult GetSelectionSource      (nsISelection *selection,
					  PRBool useLocalImage,
					  nsAString &string);
#endif
	nsresult GetHtmlWithContents     (nsISelection *selection,
					  const gchar *storedir,
					  nsAString &string);
	
	nsresult GetBodyString           (nsAString &string);
	nsresult GetStringSelection (nsAString &string, gboolean backward);
   
	nsresult GetPageDescriptor (nsISupports **aPageDescriptor);
	nsresult LoadDocument      (nsISupports *aPageDescriptor,
				    PRUint32 aDisplayType);

	nsresult ForceEncoding     (const char *encoding);
	nsresult GetEncoding       (char **encoding,
				    PRBool& forced);

	nsresult CopyHistoryTo     (KzMozWrapper *dest, 
				    PRBool back_history, 
				    PRBool forward_history, 
				    PRBool set_current);
	nsresult GetSHInfo         (PRInt32 *count, PRInt32 *index);
	nsresult GetSHUrlAtIndex   (PRInt32 index, nsACString &url);
	nsresult GetSHTitleAtIndex (PRInt32 index, PRUnichar **title);
	nsresult GetHistoryEntry   (PRInt32 index, nsIHistoryEntry **he);
	nsresult GoHistoryIndex    (gint index);

	nsresult Print             (void);
	nsresult PrintPreview      (void);
	nsresult GetPrinterList    (GList **list);
	nsresult SetZoom           (float aZoom, PRBool reflow);
	nsresult SetZoomOnDocshell (float aZoom, nsIDocShell *DocShell);
	nsresult GetZoom           (float *aZoom);
	nsresult SetImageZoom      (float aZoom);
	
	nsresult SetAllowJavascript(PRBool aAllow);
	nsresult GetAllowJavascript(PRBool *aAllow);
	nsresult SetAllowImages    (PRBool aAllow);
	nsresult GetAllowImages    (PRBool *aAllow);
	
	nsresult GetSHistory       (nsISHistory **aSHistory);

	nsresult GetAttributeFromNode    (nsIDOMNode *node, 
				          const char *attr,
				          char **value);
	nsresult GetLinkAndTitleFromNode (nsIDOMDocument *domDoc, 
					  nsIDOMNode *node,
			                  gchar **url, gchar **title);
	nsresult GetLinkFromNode         (nsIDOMDocument *domDoc,
				          nsIDOMNode *node,
		                          gchar **url);
	nsresult GetLinksFromWindow      (nsIDOMWindow *domWindow,
				          GList **list,
				          nsISelection *selection, 
				          gboolean selected_only);
	nsresult GetDestAnchorsFromWindow (nsIDOMWindow *domWindow,
					   GList **list);
	nsresult GetPostData             (nsIHistoryEntry *he,
				          nsAString &postData);
	nsresult SetPostData             (nsIHistoryEntry *he,
				          const nsAString &postData);
	nsresult FineScroll              (int horiz, int vert);
	nsresult PageUp			 (void);
	nsresult PageDown		 (void);

	nsresult AddSelectionListener    (void);
	nsresult RemoveSelectionListener (void);

	nsresult GetBodyNode             (nsIDOMNode **node);
	nsresult SetHighlightRange       (nsIDOMRange **searchRange,
	    				  nsIDOMRange **startRange,
	    				  nsIDOMRange **endRange);

	nsresult ShowPageCertificate     (void);

#ifdef MOZ_NSICANVASRENDERINGCONTEXTINTERNAL_HAVE_GETINPUTSTREAM_
	nsresult CreateThumbnail	 (void);
#endif

	nsCOMPtr<nsIWebBrowser>    mWebBrowser;
protected:
	nsCOMPtr<nsIDOMEventTarget>   mEventTarget;
	nsCOMPtr<nsIDOMWindow>        mDOMWindow;
	KzGeckoEmbed                 *mKzGeckoEmbed;
	KzMozEventListener           *mKzMozEventListener;
	KzMozSelectionListener       *mKzMozSelectionListener;
	PRPackedBool                  mSelectionWasCollapsed;
	nsCOMPtr<nsISecureBrowserUI> mSecurityInfo;

	nsresult GetListener         (void);
	nsresult AttachListeners     (void);
	nsresult DetachListeners     (void);
	nsresult GetWebNavigation    (nsIWebNavigation **aWebNavigation);
	nsresult GetFocusedDOMWindow (nsIDOMWindow **aDOMWindow);
	void     HTMLSourceFromNode  (nsIDOMDocument *domDoc,
				      nsIDOMNode *node,
				      nsISelection *selection,
				      nsIDOMRange *range,
				      const gchar *storedir, 
				      nsAString &string);
	
	nsresult ResolveURI 	     (nsIDOMDocument *domDoc,
				      const nsACString &origURI,
			  	      nsACString &URI);

	nsresult GetCacheEntryDescriptor(const nsACString &aKey,
	  				 nsICacheEntryDescriptor **aCacheEntryDescriptor);
	
	void	  SetHTMLHeadSource (nsIDOMDocument *domDocument,
				     const gchar *storedir,
			      	     nsAString &aSource);
	void 	  SetAttributes     (nsIDOMNode *node, 
			             nsIDOMDocument *domDoc,
			             nsAString &aString);
	void      SetStartTag       (const nsAString &tag,
			   	     nsIDOMDocument *domDoc,
			             nsAString &aString);
	void      GetFileToLocal    (const nsACString &URI,
				     const gchar *storedir,
				     const gchar *type,
				     nsAString &LocalFile);
	nsresult  GetSSLStatus      (nsISSLStatus **aSSLStatus);
};
