File:  [mozdev] / chimera / NSBrowserView.mm
Revision 1.31: download - view: text, annotated - select for diffs - revision graph
Fri Apr 12 09:56:34 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Context menus come up! Yay!

    1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
    2: /* ***** BEGIN LICENSE BLOCK *****
    3:  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
    4:  *
    5:  * The contents of this file are subject to the Netscape Public License
    6:  * Version 1.1 (the "License"); you may not use this file except in
    7:  * compliance with the License. You may obtain a copy of the License at
    8:  * http://www.mozilla.org/NPL/
    9:  *
   10:  * Software distributed under the License is distributed on an "AS IS" basis,
   11:  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   12:  * for the specific language governing rights and limitations under the
   13:  * License.
   14:  *
   15:  * The Original Code is mozilla.org code.
   16:  *
   17:  * The Initial Developer of the Original Code is 
   18:  * Netscape Communications Corporation.
   19:  * Portions created by the Initial Developer are Copyright (C) 1998
   20:  * the Initial Developer. All Rights Reserved.
   21:  *
   22:  * Contributor(s):
   23:  *
   24:  * Alternatively, the contents of this file may be used under the terms of
   25:  * either the GNU General Public License Version 2 or later (the "GPL"), or 
   26:  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   27:  * in which case the provisions of the GPL or the LGPL are applicable instead
   28:  * of those above. If you wish to allow use of your version of this file only
   29:  * under the terms of either the GPL or the LGPL, and not to allow others to
   30:  * use your version of this file under the terms of the NPL, indicate your
   31:  * decision by deleting the provisions above and replace them with the notice
   32:  * and other provisions required by the GPL or the LGPL. If you do not delete
   33:  * the provisions above, a recipient may use your version of this file under
   34:  * the terms of any one of the NPL, the GPL or the LGPL.
   35:  *
   36:  * ***** END LICENSE BLOCK ***** */
   37: 
   38: #import "NSBrowserView.h"
   39: #import "ProgressDlgController.h"
   40: #import "FindDlgController.h"
   41: #import "nsCocoaBrowserService.h"
   42: 
   43: // Embedding includes
   44: #include "nsCWebBrowser.h"
   45: #include "nsIInterfaceRequestor.h"
   46: #include "nsIWebBrowserChrome.h"
   47: #include "nsIEmbeddingSiteWindow.h"
   48: #include "nsIWebProgressListener.h"
   49: #include "nsIWebBrowser.h"
   50: #include "nsIWebNavigation.h"
   51: #include "nsIURI.h"
   52: #include "nsIDOMWindow.h"
   53: #include "nsWeakReference.h"
   54: 
   55: // XPCOM and String includes
   56: #include "nsCRT.h"
   57: #include "nsXPIDLString.h"
   58: #include "nsCOMPtr.h"
   59: 
   60: // Printing
   61: #include "nsIWebBrowserPrint.h"
   62: #include "nsIPrintSettings.h"
   63: 
   64: // Saving of links/images/docs
   65: #include "nsIWebBrowserFocus.h"
   66: #include "nsIDOMHTMLDocument.h"
   67: #include "nsIDOMNSDocument.h"
   68: #include "nsIDOMLocation.h"
   69: #include "nsIURL.h"
   70: #include "nsIWebBrowserPersist.h"
   71: #include "nsIProperties.h"
   72: #include "nsIRequest.h"
   73: #include "nsIChannel.h"
   74: #include "nsIHttpChannel.h"
   75: #include "nsIPref.h"
   76: #include "nsIMIMEService.h"
   77: #include "nsIMIMEInfo.h"
   78: #include "nsISHistory.h"
   79: #include "nsIHistoryEntry.h"
   80: #include "nsISHEntry.h"
   81: #include "nsNetUtil.h"
   82: #include "nsIContextMenuListener.h"
   83: 
   84: // Cut/copy/paste
   85: #include "nsIClipboardCommands.h"
   86: #include "nsIInterfaceRequestorUtils.h"
   87: 
   88: const char* persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
   89: const char* dirServiceContractID = "@mozilla.org/file/directory_service;1";
   90: 
   91: class nsCocoaBrowserListener : public nsSupportsWeakReference,
   92:                                public nsIInterfaceRequestor,
   93: 			                         public nsIWebBrowserChrome,
   94:                                public nsIWindowCreator,
   95: 			                         public nsIEmbeddingSiteWindow,
   96:                                public nsIWebProgressListener,
   97:                                public nsIContextMenuListener
   98: {
   99: public:
  100:   nsCocoaBrowserListener(NSBrowserView* aView);
  101:   virtual ~nsCocoaBrowserListener();
  102: 
  103:   NS_DECL_ISUPPORTS
  104:   NS_DECL_NSIINTERFACEREQUESTOR
  105:   NS_DECL_NSIWEBBROWSERCHROME
  106:   NS_DECL_NSIWINDOWCREATOR
  107:   NS_DECL_NSIEMBEDDINGSITEWINDOW
  108:   NS_DECL_NSIWEBPROGRESSLISTENER
  109:   NS_DECL_NSICONTEXTMENULISTENER
  110:     
  111:   void AddListener(id <NSBrowserListener> aListener);
  112:   void RemoveListener(id <NSBrowserListener> aListener);
  113:   void SetContainer(id <NSBrowserContainer> aContainer);
  114: 
  115: private:
  116:   NSBrowserView* mView;     // WEAK - it owns us
  117:   NSMutableArray* mListeners;
  118:   id <NSBrowserContainer> mContainer;
  119:   PRBool mIsModal;
  120:   PRUint32 mChromeFlags;
  121: };
  122: 
  123: nsCocoaBrowserListener::nsCocoaBrowserListener(NSBrowserView* aView)
  124:   : mView(aView), mContainer(nsnull), mIsModal(PR_FALSE), mChromeFlags(0)
  125: {
  126:   NS_INIT_ISUPPORTS();
  127:   mListeners = [[NSMutableArray alloc] init];
  128: }
  129: 
  130: nsCocoaBrowserListener::~nsCocoaBrowserListener()
  131: {
  132:   [mListeners release];
  133:   mView = nsnull;
  134:   if (mContainer) {
  135:     [mContainer release];
  136:   }
  137: }
  138: 
  139: NS_IMPL_ISUPPORTS7(nsCocoaBrowserListener,
  140: 		               nsIInterfaceRequestor,
  141: 		               nsIWebBrowserChrome,
  142:                    nsIWindowCreator,
  143: 		               nsIEmbeddingSiteWindow,
  144: 		               nsIWebProgressListener,
  145:                    nsISupportsWeakReference,
  146:                    nsIContextMenuListener)
  147: 
  148: // Implementation of nsIInterfaceRequestor
  149: NS_IMETHODIMP 
  150: nsCocoaBrowserListener::GetInterface(const nsIID &aIID, void** aInstancePtr)
  151: {
  152:   if (aIID.Equals(NS_GET_IID(nsIDOMWindow))) {
  153:     nsCOMPtr<nsIWebBrowser> browser = dont_AddRef([mView getWebBrowser]);
  154:     if (browser)
  155:       return browser->GetContentDOMWindow((nsIDOMWindow **) aInstancePtr);
  156:   }
  157:   
  158:   return QueryInterface(aIID, aInstancePtr);
  159: }
  160: 
  161: // Implementation of nsIWindowCreator.  The CocoaBrowserService forwards requests
  162: // for a new window that have a parent to us, and we take over from there.  
  163: /* nsIWebBrowserChrome createChromeWindow (in nsIWebBrowserChrome parent, in PRUint32 chromeFlags); */
  164: NS_IMETHODIMP 
  165: nsCocoaBrowserListener::CreateChromeWindow(nsIWebBrowserChrome *parent, 
  166:                                            PRUint32 chromeFlags, 
  167:                                            nsIWebBrowserChrome **_retval)
  168: {
  169:   if (parent != this) {
  170:     printf("Mismatch in nsCocoaBrowserListener::CreateChromeWindow.  We should be the owning parent.\n");
  171:     return NS_ERROR_FAILURE;
  172:   }
  173:   
  174:   NSBrowserView* childView = [mContainer createBrowserWindow: chromeFlags];
  175:   if (!childView) {
  176:     printf("No NSBrowserView hooked up for a newly created window yet.\n");
  177:     return NS_ERROR_FAILURE;
  178:   }
  179:   
  180:   nsCocoaBrowserListener* listener = [childView getCocoaBrowserListener];
  181:   if (!listener) {
  182:     printf("Uh-oh! No listener yet for a newly created window (nsCocoaBrowserlistener)\n");
  183:     return NS_ERROR_FAILURE;
  184:   }
  185:   
  186:   printf("made a chrome window.\n");
  187:   
  188:   *_retval = listener;
  189:   NS_IF_ADDREF(*_retval);
  190:   return NS_OK;
  191: }
  192: 
  193: // Implementation of nsIContextMenuListener
  194: NS_IMETHODIMP
  195: nsCocoaBrowserListener::OnShowContextMenu(PRUint32 aContextFlags, nsIDOMEvent* aEvent, nsIDOMNode* aNode)
  196: {
  197:   [mContainer onShowContextMenu: aContextFlags domEvent: aEvent domNode: aNode];
  198:   return NS_OK;
  199: }
  200: 
  201: // Implementation of nsIWebBrowserChrome
  202: /* void setStatus (in unsigned long statusType, in wstring status); */
  203: NS_IMETHODIMP 
  204: nsCocoaBrowserListener::SetStatus(PRUint32 statusType, const PRUnichar *status)
  205: {
  206:   if (!mContainer) {
  207:     return NS_ERROR_FAILURE;
  208:   }
  209: 
  210:   NSString* str = nsnull;
  211:   if (status && (*status != PRUnichar(0))) {
  212:     str = [NSString stringWithCharacters:status length:nsCRT::strlen(status)];
  213:   }
  214: 
  215:   [mContainer setStatus:str ofType:(NSStatusType)statusType];
  216: 
  217:   return NS_OK;
  218: }
  219: 
  220: /* attribute nsIWebBrowser webBrowser; */
  221: NS_IMETHODIMP 
  222: nsCocoaBrowserListener::GetWebBrowser(nsIWebBrowser * *aWebBrowser)
  223: {
  224:   NS_ENSURE_ARG_POINTER(aWebBrowser);
  225:   if (!mView) {
  226:     return NS_ERROR_FAILURE;
  227:   }
  228:   *aWebBrowser = [mView getWebBrowser];
  229: 
  230:   return NS_OK;
  231: }
  232: NS_IMETHODIMP 
  233: nsCocoaBrowserListener::SetWebBrowser(nsIWebBrowser * aWebBrowser)
  234: {
  235:   if (!mView) {
  236:     return NS_ERROR_FAILURE;
  237:   }
  238: 
  239:   [mView setWebBrowser:aWebBrowser];
  240: 
  241:   return NS_OK;
  242: }
  243: 
  244: /* attribute unsigned long chromeFlags; */
  245: NS_IMETHODIMP 
  246: nsCocoaBrowserListener::GetChromeFlags(PRUint32 *aChromeFlags)
  247: {
  248:   NS_ENSURE_ARG_POINTER(aChromeFlags);
  249:   *aChromeFlags = mChromeFlags;
  250:   return NS_OK;
  251: }
  252: NS_IMETHODIMP 
  253: nsCocoaBrowserListener::SetChromeFlags(PRUint32 aChromeFlags)
  254: {
  255:   // XXX Do nothing with them for now
  256:   mChromeFlags = aChromeFlags;
  257:   return NS_OK;
  258: }
  259: 
  260: /* void destroyBrowserWindow (); */
  261: NS_IMETHODIMP 
  262: nsCocoaBrowserListener::DestroyBrowserWindow()
  263: {
  264:   // XXX Could send this up to the container, but for now,
  265:   // we just destroy the enclosing window.
  266:   NSWindow* window = [mView window];
  267: 
  268:   if (window) {
  269:     [window close];
  270:   }
  271: 
  272:   return NS_OK;
  273: }
  274: 
  275: /* void sizeBrowserTo (in long aCX, in long aCY); */
  276: NS_IMETHODIMP 
  277: nsCocoaBrowserListener::SizeBrowserTo(PRInt32 aCX, PRInt32 aCY)
  278: {
  279:   if (mContainer) {
  280:     NSSize size;
  281:     
  282:     size.width = (float)aCX;
  283:     size.height = (float)aCY;
  284: 
  285:     [mContainer sizeBrowserTo:size];
  286:   }
  287:   
  288:   return NS_OK;
  289: }
  290: 
  291: /* void showAsModal (); */
  292: NS_IMETHODIMP 
  293: nsCocoaBrowserListener::ShowAsModal()
  294: {
  295:   if (!mView) {
  296:     return NS_ERROR_FAILURE;
  297:   }
  298: 
  299:   NSWindow* window = [mView window];
  300: 
  301:   if (!window) {
  302:     return NS_ERROR_FAILURE;
  303:   }
  304: 
  305:   mIsModal = PR_TRUE;
  306:   //int result = [NSApp runModalForWindow:window];
  307:   mIsModal = PR_FALSE;
  308: 
  309:   return NS_OK;
  310: }
  311: 
  312: /* boolean isWindowModal (); */
  313: NS_IMETHODIMP 
  314: nsCocoaBrowserListener::IsWindowModal(PRBool *_retval)
  315: {
  316:   NS_ENSURE_ARG_POINTER(_retval);
  317: 
  318:   *_retval = mIsModal;
  319: 
  320:   return NS_OK;
  321: }
  322: 
  323: /* void exitModalEventLoop (in nsresult aStatus); */
  324: NS_IMETHODIMP 
  325: nsCocoaBrowserListener::ExitModalEventLoop(nsresult aStatus)
  326: {
  327: //  [NSApp stopModalWithCode:(int)aStatus];
  328: 
  329:   return NS_OK;
  330: }
  331: 
  332: // Implementation of nsIEmbeddingSiteWindow
  333: /* void setDimensions (in unsigned long flags, in long x, in long y, in long cx, in long cy); */
  334: NS_IMETHODIMP 
  335: nsCocoaBrowserListener::SetDimensions(PRUint32 flags, 
  336: 				      PRInt32 x, 
  337: 				      PRInt32 y, 
  338: 				      PRInt32 cx, 
  339: 				      PRInt32 cy)
  340: {
  341:   if (!mView) {
  342:     return NS_ERROR_FAILURE;
  343:   }
  344: 
  345:   NSWindow* window = [mView window];
  346:   if (!window) {
  347:     return NS_ERROR_FAILURE;
  348:   }
  349: 
  350:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
  351:     NSPoint origin;
  352:     origin.x = (float)x;
  353:     origin.y = (float)y;
  354:     [window setFrameOrigin:origin];
  355:   }
  356: 
  357:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
  358:     NSRect frame = [window frame];
  359:     frame.size.width = (float)cx;
  360:     frame.size.height = (float)cy;
  361:     [window setFrame:frame display:YES];
  362:   }
  363:   else if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER) {
  364:     NSSize size;
  365:     size.width = (float)cx;
  366:     size.height = (float)cy;
  367:     [window setContentSize:size];
  368:   }
  369: 
  370:   return NS_OK;
  371: }
  372: 
  373: /* void getDimensions (in unsigned long flags, out long x, out long y, out long cx, out long cy); */
  374: NS_IMETHODIMP 
  375: nsCocoaBrowserListener::GetDimensions(PRUint32 flags, 
  376: 				      PRInt32 *x, 
  377: 				      PRInt32 *y, 
  378: 				      PRInt32 *cx, 
  379: 				      PRInt32 *cy)
  380: {
  381:   NS_ENSURE_ARG_POINTER(x);
  382:   NS_ENSURE_ARG_POINTER(y);
  383:   NS_ENSURE_ARG_POINTER(cx);
  384:   NS_ENSURE_ARG_POINTER(cy);
  385: 
  386:   if (!mView) {
  387:     return NS_ERROR_FAILURE;
  388:   }
  389: 
  390:   NSWindow* window = [mView window];
  391:   if (!window) {
  392:     return NS_ERROR_FAILURE;
  393:   }
  394: 
  395:   NSRect frame = [window frame];
  396:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
  397:     *x = (PRInt32)frame.origin.x;
  398:     *y = (PRInt32)frame.origin.y;
  399:   }
  400:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
  401:     *cx = (PRInt32)frame.size.width;
  402:     *cy = (PRInt32)frame.size.height;
  403:   }
  404:   else if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER) {
  405:     NSView* contentView = [window contentView];
  406:     NSRect contentFrame = [contentView frame];
  407:     *cx = (PRInt32)contentFrame.size.width;
  408:     *cy = (PRInt32)contentFrame.size.height;    
  409:   }
  410: 
  411:   return NS_OK;
  412: }
  413: 
  414: /* void setFocus (); */
  415: NS_IMETHODIMP 
  416: nsCocoaBrowserListener::SetFocus()
  417: {
  418:   if (!mView) {
  419:     return NS_ERROR_FAILURE;
  420:   }
  421: 
  422:   NSWindow* window = [mView window];
  423:   if (!window) {
  424:     return NS_ERROR_FAILURE;
  425:   }
  426: 
  427:   [window makeKeyAndOrderFront:window];
  428: 
  429:   return NS_OK;
  430: }
  431: 
  432: /* attribute boolean visibility; */
  433: NS_IMETHODIMP 
  434: nsCocoaBrowserListener::GetVisibility(PRBool *aVisibility)
  435: {
  436:   NS_ENSURE_ARG_POINTER(aVisibility);
  437: 
  438:   if (!mView) {
  439:     return NS_ERROR_FAILURE;
  440:   }
  441: 
  442:   NSWindow* window = [mView window];
  443:   if (!window) {
  444:     return NS_ERROR_FAILURE;
  445:   }
  446: 
  447:   *aVisibility = [window isMiniaturized];
  448: 
  449:   return NS_OK;
  450: }
  451: NS_IMETHODIMP 
  452: nsCocoaBrowserListener::SetVisibility(PRBool aVisibility)
  453: {
  454:   if (!mView) {
  455:     return NS_ERROR_FAILURE;
  456:   }
  457: 
  458:   NSWindow* window = [mView window];
  459:   if (!window) {
  460:     return NS_ERROR_FAILURE;
  461:   }
  462: 
  463:   if (aVisibility) {
  464:     [window deminiaturize:window];
  465:   }
  466:   else {
  467:     [window miniaturize:window];
  468:   }
  469: 
  470:   return NS_OK;
  471: }
  472: 
  473: /* attribute wstring title; */
  474: NS_IMETHODIMP 
  475: nsCocoaBrowserListener::GetTitle(PRUnichar * *aTitle)
  476: {
  477:   NS_ENSURE_ARG_POINTER(aTitle);
  478: 
  479:   if (!mContainer) {
  480:     return NS_ERROR_FAILURE;
  481:   }
  482: 
  483:   NSString* title = [mContainer title];
  484:   unsigned int length = [title length];
  485:   if (length) {
  486:     *aTitle = (PRUnichar*)nsMemory::Alloc((length+1)*sizeof(PRUnichar));
  487:     if (!*aTitle) {
  488:       return NS_ERROR_OUT_OF_MEMORY;
  489:     }
  490:     [title getCharacters:*aTitle];
  491:   }
  492:   else {
  493:     *aTitle = nsnull;
  494:   }
  495:   
  496:   return NS_OK;
  497: }
  498: NS_IMETHODIMP 
  499: nsCocoaBrowserListener::SetTitle(const PRUnichar * aTitle)
  500: {
  501:   NS_ENSURE_ARG(aTitle);
  502: 
  503:   if (!mContainer) {
  504:     return NS_ERROR_FAILURE;
  505:   }
  506: 
  507:   NSString* str = [NSString stringWithCharacters:aTitle length:nsCRT::strlen(aTitle)];
  508:   [mContainer setTitle:str];
  509: 
  510:   return NS_OK;
  511: }
  512: 
  513: /* [noscript] readonly attribute voidPtr siteWindow; */
  514: NS_IMETHODIMP 
  515: nsCocoaBrowserListener::GetSiteWindow(void * *aSiteWindow)
  516: {
  517:   NS_ENSURE_ARG_POINTER(aSiteWindow);
  518: 
  519:   if (!mView) {
  520:     return NS_ERROR_FAILURE;
  521:   }
  522: 
  523:   NSWindow* window = [mView window];
  524:   if (!window) {
  525:     return NS_ERROR_FAILURE;
  526:   }
  527: 
  528:   *aSiteWindow = (void*)window;
  529: 
  530:   return NS_OK;
  531: }
  532: 
  533: // Implementation of nsIWebProgressListener
  534: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
  535: NS_IMETHODIMP 
  536: nsCocoaBrowserListener::OnStateChange(nsIWebProgress *aWebProgress, 
  537: 				      nsIRequest *aRequest, 
  538: 				      PRInt32 aStateFlags, 
  539: 				      PRUint32 aStatus)
  540: {
  541:   NSEnumerator* enumerator = [mListeners objectEnumerator];
  542:   id obj;
  543:   
  544:   if (aStateFlags & nsIWebProgressListener::STATE_IS_NETWORK) {
  545:     if (aStateFlags & nsIWebProgressListener::STATE_START) {
  546:       while ((obj = [enumerator nextObject])) {
  547: 	[obj onLoadingStarted];
  548:       }
  549:     }
  550:     else if (aStateFlags & nsIWebProgressListener::STATE_STOP) {
  551:       while ((obj = [enumerator nextObject])) {
  552: 	[obj onLoadingCompleted:(NS_SUCCEEDED(aStatus))];
  553:       }
  554:     }
  555:   }
  556: 
  557: 
  558:   return NS_OK;
  559: }
  560: 
  561: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
  562: NS_IMETHODIMP 
  563: nsCocoaBrowserListener::OnProgressChange(nsIWebProgress *aWebProgress, 
  564: 					 nsIRequest *aRequest, 
  565: 					 PRInt32 aCurSelfProgress, 
  566: 					 PRInt32 aMaxSelfProgress, 
  567: 					 PRInt32 aCurTotalProgress, 
  568: 					 PRInt32 aMaxTotalProgress)
  569: {
  570:   NSEnumerator* enumerator = [mListeners objectEnumerator];
  571:   id obj;
  572:  
  573:   while ((obj = [enumerator nextObject])) {
  574:     [obj onProgressChange:aCurTotalProgress outOf:aMaxTotalProgress];
  575:   }
  576:   
  577:   return NS_OK;
  578: }
  579: 
  580: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
  581: NS_IMETHODIMP 
  582: nsCocoaBrowserListener::OnLocationChange(nsIWebProgress *aWebProgress, 
  583: 					 nsIRequest *aRequest, 
  584: 					 nsIURI *location)
  585: {
  586:   if (!location)
  587:     return NS_ERROR_FAILURE;
  588:     
  589:   nsCAutoString spec;
  590:   location->GetSpec(spec);
  591:   const char* cstr = spec.get();
  592:   NSString* str = [NSString stringWithCString:cstr];
  593:   NSURL* url = [NSURL URLWithString:str];
  594: 
  595:   NSEnumerator* enumerator = [mListeners objectEnumerator];
  596:   id obj;
  597:  
  598:   while ((obj = [enumerator nextObject])) {
  599:     [obj onLocationChange:url];
  600:   }
  601: 
  602:   return NS_OK;
  603: }
  604: 
  605: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
  606: NS_IMETHODIMP 
  607: nsCocoaBrowserListener::OnStatusChange(nsIWebProgress *aWebProgress, 
  608: 				       nsIRequest *aRequest, 
  609: 				       nsresult aStatus, 
  610: 				       const PRUnichar *aMessage)
  611: {
  612:   nsCAutoString msg; msg.AssignWithConversion(aMessage);
  613:   
  614:   NSString* str = [NSString stringWithCString:msg.get()];
  615:   
  616:   NSEnumerator* enumerator = [mListeners objectEnumerator];
  617:   id obj;
  618:  
  619:   while ((obj = [enumerator nextObject])) {
  620:     [obj onStatusChange: str];
  621:   }
  622: 
  623:   return NS_OK;
  624: }
  625: 
  626: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
  627: NS_IMETHODIMP 
  628: nsCocoaBrowserListener::OnSecurityChange(nsIWebProgress *aWebProgress, 
  629: 					 nsIRequest *aRequest, 
  630: 					 PRInt32 state)
  631: {
  632:   return NS_OK;
  633: }
  634: 
  635: void 
  636: nsCocoaBrowserListener::AddListener(id <NSBrowserListener> aListener)
  637: {
  638:   [mListeners addObject:aListener];
  639: }
  640: 
  641: void 
  642: nsCocoaBrowserListener::RemoveListener(id <NSBrowserListener> aListener)
  643: {
  644:   [mListeners removeObject:aListener];
  645: }
  646: 
  647: void 
  648: nsCocoaBrowserListener::SetContainer(id <NSBrowserContainer> aContainer)
  649: {
  650:   [mContainer autorelease];
  651: 
  652:   mContainer = aContainer;
  653: 
  654:   [mContainer retain];
  655: }
  656: 
  657: // Implementation of a header sniffer class that is used when saving Web pages and images.
  658: class nsHeaderSniffer :  public nsIWebProgressListener
  659: {
  660: public:
  661:     nsHeaderSniffer(nsIWebBrowserPersist* aPersist, nsIFile* aFile, nsIURI* aURL,
  662:                     nsIDOMDocument* aDocument, nsIInputStream* aPostData, PRBool aBypassCache,
  663:                     NSView* aFilterView, NSPopUpButton* aFilterList)
  664:     {
  665:         NS_INIT_REFCNT();
  666:         mPersist = aPersist;
  667:         mTmpFile = aFile;
  668:         mURL = aURL;
  669:         mDocument = aDocument;
  670:         mPostData = aPostData;
  671:         mBypassCache = aBypassCache;
  672:         mFilterView = aFilterView;
  673:         mFilterList = aFilterList;
  674:     }
  675:                   
  676:     virtual ~nsHeaderSniffer() 
  677:     {
  678:     };
  679: 
  680:     NS_DECL_ISUPPORTS
  681:     NS_DECL_NSIWEBPROGRESSLISTENER
  682:   
  683: protected:
  684:     void PerformSave();
  685:     
  686: private:
  687:     nsIWebBrowserPersist* mPersist; // Weak. It owns us as a listener.
  688:     nsCOMPtr<nsIFile> mTmpFile;
  689:     nsCOMPtr<nsIURI> mURL;
  690:     nsCOMPtr<nsIDOMDocument> mDocument;
  691:     nsCOMPtr<nsIInputStream> mPostData;
  692:     PRBool mBypassCache;
  693:     nsCString mContentType;
  694:     nsCString mContentDisposition;
  695:     NSView* mFilterView;
  696:     NSPopUpButton* mFilterList;
  697: };
  698: 
  699: NS_IMPL_ISUPPORTS1(nsHeaderSniffer, nsIWebProgressListener)
  700: 
  701: // Implementation of nsIWebProgressListener
  702: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
  703: NS_IMETHODIMP 
  704: nsHeaderSniffer::OnStateChange(nsIWebProgress *aWebProgress, 
  705: 				      nsIRequest *aRequest, 
  706: 				      PRInt32 aStateFlags, 
  707: 				      PRUint32 aStatus)
  708: {
  709:     if (aStateFlags & nsIWebProgressListener::STATE_START) {
  710:         nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
  711:         channel->GetContentType(mContentType);
  712:        
  713:         // Get the content-disposition if we're an HTTP channel.
  714:         nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
  715:         if (httpChannel)
  716:             httpChannel->GetResponseHeader(nsCAutoString("content-disposition"), mContentDisposition);
  717:         
  718:         mPersist->CancelSave();
  719:         PRBool exists;
  720:         mTmpFile->Exists(&exists);
  721:         if (exists)
  722:             mTmpFile->Remove(PR_FALSE);
  723:         PerformSave();
  724:     }
  725:     return NS_OK;
  726: }
  727: 
  728: void nsHeaderSniffer::PerformSave()
  729: {
  730:     // Are we an HTML document? If so, we will want to append an accessory view to
  731:     // the save dialog to provide the user with the option of doing a complete
  732:     // save vs. a single file save.
  733:     PRBool isHTML = (mDocument && mContentType.Equals("text/html") ||
  734:                      mContentType.Equals("text/xml") ||
  735:                      mContentType.Equals("application/xhtml+xml"));
  736:     
  737:     // Next find out the directory that we should start in.
  738:     nsCOMPtr<nsIPrefService> prefs(do_GetService("@mozilla.org/preferences-service;1"));
  739:     if (!prefs)
  740:         return;
  741:     nsCOMPtr<nsIPrefBranch> dirBranch;
  742:     prefs->GetBranch("browser.download.", getter_AddRefs(dirBranch));
  743:     PRInt32 filterIndex = 0;
  744:     if (dirBranch) {
  745:         nsresult rv = dirBranch->GetIntPref("save_converter_index", &filterIndex);
  746:         if (NS_FAILED(rv))
  747:             filterIndex = 0;
  748:     }
  749:     if (mFilterList)
  750:         [mFilterList selectItemAtIndex: filterIndex];
  751:         
  752:     // We need to figure out what file name to use.
  753:     nsCAutoString defaultFileName;
  754:     
  755:     if (!mContentDisposition.IsEmpty()) {
  756:         // (1) Use the HTTP header suggestion.
  757:         PRInt32 index = mContentDisposition.Find("filename=");
  758:         if (index >= 0) {
  759:             // Take the substring following the prefix.
  760:             index += 9;
  761:             nsCAutoString filename;
  762:             mContentDisposition.Right(filename, mContentDisposition.Length() - index);
  763:             defaultFileName = filename;
  764:         }
  765:     }
  766:     
  767:     if (defaultFileName.IsEmpty()) {
  768:         nsCOMPtr<nsIURL> url(do_QueryInterface(mURL));
  769:         if (url)
  770:             url->GetFileName(defaultFileName); // (2) For file URLs, use the file name.
  771:     }
  772:     
  773:     if (defaultFileName.IsEmpty() && mDocument && isHTML) {
  774:         nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(mDocument));
  775:         nsAutoString title;
  776:         if (htmlDoc)
  777:             htmlDoc->GetTitle(title); // (3) Use the title of the document.
  778:         defaultFileName.AssignWithConversion(title);
  779:     }
  780:     
  781:     if (defaultFileName.IsEmpty()) {
  782:         // (4) Use the caller provided name. XXXdwh
  783:     }
  784: 
  785:     if (defaultFileName.IsEmpty())
  786:         // (5) Use the host.
  787:         mURL->GetHost(defaultFileName);
  788:     
  789:     // One last case to handle about:blank and other fruity untitled pages.
  790:     if (defaultFileName.IsEmpty())
  791:         defaultFileName = "untitled";
  792:         
  793:     // Validate the file name to ensure legality.
  794:     for (PRUint32 i = 0; i < defaultFileName.Length(); i++)
  795:         if (defaultFileName[i] == ':' || defaultFileName[i] == '/')
  796:             defaultFileName.SetCharAt(i, ' ');
  797:             
  798:     // Make sure the appropriate extension is appended to the suggested file name.
  799:     nsCOMPtr<nsIURI> fileURI(do_CreateInstance("@mozilla.org/network/standard-url;1"));
  800:     nsCOMPtr<nsIURL> fileURL(do_QueryInterface(fileURI));
  801:     if (!fileURL)
  802:         return;
  803:     fileURL->SetFilePath(defaultFileName);
  804:     
  805:     nsCAutoString fileExtension;
  806:     fileURL->GetFileExtension(fileExtension);
  807:     
  808:     PRBool setExtension = PR_FALSE;
  809:     if (mContentType.Equals("text/html")) {
  810:         if (fileExtension.IsEmpty() || (!fileExtension.Equals("htm") && !fileExtension.Equals("html"))) {
  811:             defaultFileName += ".html";
  812:             setExtension = PR_TRUE;
  813:         }
  814:     }
  815:     
  816:     if (!setExtension && fileExtension.IsEmpty()) {
  817:         nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1"));
  818:         if (!mimeService)
  819:             return;
  820:         nsCOMPtr<nsIMIMEInfo> mimeInfo;
  821:         mimeService->GetFromMIMEType(mContentType.get(), getter_AddRefs(mimeInfo));
  822:         
  823:         PRUint32 extCount;
  824:         char** extList;
  825:         mimeInfo->GetFileExtensions(&extCount, &extList);
  826:         
  827:         if (extCount > 0) {
  828:             defaultFileName += ".";
  829:             defaultFileName += extList[0];
  830:         }
  831:     }
  832:     
  833:     // Now it's time to pose the save dialog.
  834:     NSSavePanel* savePanel = [NSSavePanel savePanel];
  835:     NSString* file = nil;
  836:     if (!defaultFileName.IsEmpty())
  837:         file = [[NSString alloc] initWithCString: defaultFileName.get()];
  838:         
  839:     if (isHTML)
  840:         [savePanel setAccessoryView: mFilterView];
  841:         
  842:     if ([savePanel runModalForDirectory: nil file: file] == NSFileHandlingPanelCancelButton)
  843:         return;
  844:        
  845:     // Release the file string.
  846:     [file release];
  847:     
  848:     // Update the filter index.
  849:     if (isHTML && mFilterList) {
  850:         filterIndex = [mFilterList indexOfSelectedItem];
  851:         dirBranch->SetIntPref("save_converter_index", filterIndex);
  852:     }
  853:     
  854:     // Convert the content type to text/plain if it was selected in the filter.
  855:     if (isHTML && filterIndex == 2)
  856:         mContentType = "text/plain";
  857:     
  858:     nsCOMPtr<nsISupports> sourceData;
  859:     if (isHTML && filterIndex != 1)
  860:         sourceData = do_QueryInterface(mDocument);
  861:     else
  862:         sourceData = do_QueryInterface(mURL);
  863:         
  864:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
  865:     ProgressDlgController* progressDialog = [[ProgressDlgController alloc] initWithWindowNibName: @"ProgressDialog"];
  866:     [progressDialog setWebPersist: webPersist 
  867:                     source: sourceData.get()
  868:                     destination: [savePanel filename]
  869:                     contentType: mContentType.get()
  870:                     postData: mPostData
  871:                     bypassCache: mBypassCache];
  872:                     
  873:     [progressDialog showWindow: progressDialog];
  874: }
  875: 
  876: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
  877: NS_IMETHODIMP 
  878: nsHeaderSniffer::OnProgressChange(nsIWebProgress *aWebProgress, 
  879: 					 nsIRequest *aRequest, 
  880: 					 PRInt32 aCurSelfProgress, 
  881: 					 PRInt32 aMaxSelfProgress, 
  882: 					 PRInt32 aCurTotalProgress, 
  883: 					 PRInt32 aMaxTotalProgress)
  884: {
  885:   return NS_OK;
  886: }
  887: 
  888: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
  889: NS_IMETHODIMP 
  890: nsHeaderSniffer::OnLocationChange(nsIWebProgress *aWebProgress, 
  891: 					 nsIRequest *aRequest, 
  892: 					 nsIURI *location)
  893: {
  894:   return NS_OK;
  895: }
  896: 
  897: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
  898: NS_IMETHODIMP 
  899: nsHeaderSniffer::OnStatusChange(nsIWebProgress *aWebProgress, 
  900: 				       nsIRequest *aRequest, 
  901: 				       nsresult aStatus, 
  902: 				       const PRUnichar *aMessage)
  903: {
  904:   return NS_OK;
  905: }
  906: 
  907: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
  908: NS_IMETHODIMP 
  909: nsHeaderSniffer::OnSecurityChange(nsIWebProgress *aWebProgress, 
  910: 					              nsIRequest *aRequest, 
  911:                                   PRInt32 state)
  912: {
  913:   return NS_OK;
  914: }
  915: 
  916: 
  917: @implementation NSBrowserView
  918: 
  919: - (id)initWithFrame:(NSRect)frame
  920: {
  921:   [super initWithFrame:frame];
  922: 
  923:   nsresult rv = nsCocoaBrowserService::InitEmbedding();
  924:   if (NS_FAILED(rv)) {
  925:     // XXX need to throw
  926:   }
  927:   
  928:   _listener = new nsCocoaBrowserListener(self);
  929:   NS_ADDREF(_listener);
  930: 
  931:   // Create the web browser instance
  932:   nsCOMPtr<nsIWebBrowser> browser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
  933:   if (NS_FAILED(rv)) {
  934:     // XXX need to throw
  935:   }
  936: 
  937:   _webBrowser = browser;
  938:   NS_ADDREF(_webBrowser);
  939: 
  940:   // Set the container nsIWebBrowserChrome
  941:   _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
  942: 						 _listener));
  943:   
  944:   // Register as a listener for web progress
  945:   nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, _listener));
  946:   _webBrowser->AddWebBrowserListener(weak, NS_GET_IID(nsIWebProgressListener));
  947: 
  948:   // Hook up the widget hierarchy with us as the parent
  949:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
  950:   baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
  951: 		      frame.size.width, frame.size.height);
  952:   baseWin->Create();
  953: 
  954:   return self;
  955: }
  956: 
  957: - (void)destroyWebBrowser
  958: {
  959:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
  960:   baseWin->Destroy();
  961: }
  962: 
  963: - (void)dealloc 
  964: {
  965:   [super dealloc];
  966:   
  967:   NS_RELEASE(_listener);
  968:   NS_IF_RELEASE(_webBrowser);
  969:   
  970:   nsCocoaBrowserService::BrowserClosed();
  971:   
  972:   printf("NSBrowserView died.\n");
  973: }
  974: 
  975: - (void)setFrame:(NSRect)frameRect 
  976: {
  977:   [super setFrame:frameRect];
  978:   if (_webBrowser) {
  979:     nsCOMPtr<nsIBaseWindow> window = do_QueryInterface(_webBrowser);
  980:     window->SetSize((PRInt32)frameRect.size.width, 
  981: 		    (PRInt32)frameRect.size.height,
  982: 		    PR_TRUE);
  983:   }
  984: }
  985: 
  986: - (void)addListener:(id <NSBrowserListener>)listener
  987: {
  988:   _listener->AddListener(listener);
  989: }
  990: 
  991: - (void)removeListener:(id <NSBrowserListener>)listener
  992: {
  993:   _listener->RemoveListener(listener);
  994: }
  995: 
  996: - (void)setContainer:(id <NSBrowserContainer>)container
  997: {
  998:   _listener->SetContainer(container);
  999: }
 1000: 
 1001: - (nsIDOMWindow*)getContentWindow
 1002: {
 1003:   nsIDOMWindow* window;
 1004: 
 1005:   _webBrowser->GetContentDOMWindow(&window);
 1006: 
 1007:   return window;
 1008: }
 1009: 
 1010: - (void)loadURI:(NSURL *)url flags:(unsigned int)flags
 1011: {
 1012:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1013:   
 1014:   NSString* spec = [url absoluteString];
 1015:   int length = [spec length];
 1016:   PRUnichar* specStr = nsMemory::Alloc((length+1) * sizeof(PRUnichar));
 1017:   [spec getCharacters:specStr];
 1018:   specStr[length] = PRUnichar(0);
 1019:   
 1020: 
 1021:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
 1022:   if (flags & NSLoadFlagsDontPutInHistory) {
 1023:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY;
 1024:   }
 1025:   if (flags & NSLoadFlagsReplaceHistoryEntry) {
 1026:     navFlags |= nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY;
 1027:   }
 1028:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
 1029:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
 1030:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
 1031:   }
 1032: 
 1033:   nsresult rv = nav->LoadURI(specStr, navFlags, nsnull, nsnull, nsnull);
 1034:   if (NS_FAILED(rv)) {
 1035:     // XXX need to throw
 1036:   }
 1037: 
 1038:   nsMemory::Free(specStr);
 1039: }
 1040: 
 1041: - (void)reload:(unsigned int)flags
 1042: {
 1043:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1044: 
 1045:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
 1046:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
 1047:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
 1048:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
 1049:   }
 1050: 
 1051:   nsresult rv = nav->Reload(navFlags);
 1052:   if (NS_FAILED(rv)) {
 1053:     // XXX need to throw
 1054:   }  
 1055: }
 1056: 
 1057: - (BOOL)canGoBack
 1058: {
 1059:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1060: 
 1061:   PRBool can;
 1062:   nav->GetCanGoBack(&can);
 1063: 
 1064:   return can ? YES : NO;
 1065: }
 1066: 
 1067: - (BOOL)canGoForward
 1068: {
 1069:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1070: 
 1071:   PRBool can;
 1072:   nav->GetCanGoForward(&can);
 1073: 
 1074:   return can ? YES : NO;
 1075: }
 1076: 
 1077: - (void)goBack
 1078: {
 1079:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1080: 
 1081:   nsresult rv = nav->GoBack();
 1082:   if (NS_FAILED(rv)) {
 1083:     // XXX need to throw
 1084:   }  
 1085: }
 1086: 
 1087: - (void)goForward
 1088: {
 1089:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1090: 
 1091:   nsresult rv = nav->GoForward();
 1092:   if (NS_FAILED(rv)) {
 1093:     // XXX need to throw
 1094:   }  
 1095: }
 1096: 
 1097: - (void)gotoIndex:(int)index
 1098: {
 1099:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1100: 
 1101:   nsresult rv = nav->GotoIndex(index);
 1102:   if (NS_FAILED(rv)) {
 1103:     // XXX need to throw
 1104:   }    
 1105: }
 1106: 
 1107: - (void)stop:(unsigned int)flags
 1108: {
 1109:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1110: 
 1111:   nsresult rv = nav->Stop(flags);
 1112:   if (NS_FAILED(rv)) {
 1113:     // XXX need to throw
 1114:   }    
 1115: }
 1116: 
 1117: - (NSURL*)getCurrentURI
 1118: {
 1119:   nsCOMPtr<nsIURI> uri;
 1120:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1121: 
 1122:   nav->GetCurrentURI(getter_AddRefs(uri));
 1123:   if (!uri) {
 1124:     return nsnull;
 1125:   }
 1126: 
 1127:   nsCAutoString spec;
 1128:   uri->GetSpec(spec);
 1129:   
 1130:   const char* cstr = spec.get();
 1131:   NSString* str = [NSString stringWithCString:cstr];
 1132:   NSURL* url = [NSURL URLWithString:str];
 1133:   
 1134:   return url;
 1135: }
 1136: 
 1137: - (nsCocoaBrowserListener*)getCocoaBrowserListener
 1138: {
 1139:   return _listener;
 1140: }
 1141: 
 1142: - (nsIWebBrowser*)getWebBrowser
 1143: {
 1144:   NS_IF_ADDREF(_webBrowser);
 1145:   return _webBrowser;
 1146: }
 1147: 
 1148: - (void)setWebBrowser:(nsIWebBrowser*)browser
 1149: {
 1150:   _webBrowser = browser;
 1151: 
 1152:   if (_webBrowser) {
 1153:     // Set the container nsIWebBrowserChrome
 1154:     _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
 1155: 						   _listener));
 1156: 
 1157:     NSRect frame = [self frame];
 1158:  
 1159:     // Hook up the widget hierarchy with us as the parent
 1160:     nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
 1161:     baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
 1162: 			frame.size.width, frame.size.height);
 1163:     baseWin->Create();
 1164:   }
 1165: 
 1166: }
 1167: 
 1168: -(void) saveInternal: (nsIURI*)aURI
 1169:         withDocument: (nsIDOMDocument*)aDocument
 1170:         bypassCache: (BOOL)aBypassCache
 1171:         filterView: (NSView*)aFilterView
 1172:         filterList: (NSPopUpButton*)aFilterList
 1173: {
 1174:     // Create our web browser persist object.  This is the object that knows
 1175:     // how to actually perform the saving of the page (and of the images
 1176:     // on the page).
 1177:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
 1178:     if (!webPersist)
 1179:         return;
 1180:     
 1181:     // Make a temporary file object that we can save to.
 1182:     nsCOMPtr<nsIProperties> dirService(do_GetService(dirServiceContractID));
 1183:     if (!dirService)
 1184:         return;
 1185:     nsCOMPtr<nsIFile> tmpFile;
 1186:     dirService->Get("TmpD", NS_GET_IID(nsIFile), getter_AddRefs(tmpFile));
 1187:     static short unsigned int tmpRandom = 0;
 1188:     nsCAutoString tmpNo; tmpNo.AppendInt(tmpRandom++);
 1189:     nsCAutoString saveFile("-sav");
 1190:     saveFile += tmpNo;
 1191:     saveFile += "tmp";
 1192:     tmpFile->Append(saveFile.get()); 
 1193:     
 1194:     // Get the post data if we're an HTML doc.
 1195:     nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(_webBrowser));
 1196:     nsCOMPtr<nsISHistory> sessionHistory;
 1197:     webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
 1198:     nsCOMPtr<nsIHistoryEntry> entry;
 1199:     PRInt32 sindex;
 1200:     sessionHistory->GetIndex(&sindex);
 1201:     sessionHistory->GetEntryAtIndex(sindex, PR_FALSE, getter_AddRefs(entry));
 1202:     nsCOMPtr<nsISHEntry> shEntry(do_QueryInterface(entry));
 1203:     nsCOMPtr<nsIInputStream> postData;
 1204:     if (shEntry)
 1205:         shEntry->GetPostData(getter_AddRefs(postData));
 1206:     
 1207:     nsHeaderSniffer* sniffer = new nsHeaderSniffer(webPersist, tmpFile, aURI, 
 1208:                                                    aDocument, postData, aBypassCache,
 1209:                                                    aFilterView, aFilterList);
 1210:     if (!sniffer)
 1211:         return;
 1212:     webPersist->SetProgressListener(sniffer);
 1213:     webPersist->SaveURI(aURI, nsnull, tmpFile);
 1214: }
 1215: 
 1216: -(void)printDocument
 1217: {
 1218:     nsCOMPtr<nsIDOMWindow> domWindow;
 1219:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
 1220:     nsCOMPtr<nsIInterfaceRequestor> ir(do_QueryInterface(domWindow));
 1221:     nsCOMPtr<nsIWebBrowserPrint> print;
 1222:     ir->GetInterface(NS_GET_IID(nsIWebBrowserPrint), getter_AddRefs(print));
 1223:     print->Print(nsnull, nsnull);
 1224: }
 1225: 
 1226: -(void)findInPage:(NSString*)inText
 1227: {
 1228:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
 1229:     nsCOMPtr<nsIDOMWindow> rootWindow;
 1230:     nsCOMPtr<nsIDOMWindow> focusedWindow;
 1231:     _webBrowser->GetContentDOMWindow(getter_AddRefs(rootWindow));
 1232:     wbf->GetFocusedWindow(getter_AddRefs(focusedWindow));
 1233:     if (!focusedWindow)
 1234:         focusedWindow = rootWindow;
 1235:     nsCOMPtr<nsIWebBrowserFind> webFind(do_GetInterface(_webBrowser));
 1236:     nsCOMPtr<nsIWebBrowserFindInFrames> framesFind(do_QueryInterface(webFind));
 1237:     framesFind->SetRootSearchFrame(rootWindow);
 1238:     framesFind->SetCurrentSearchFrame(focusedWindow);
 1239:     
 1240:     PRUnichar* text = (PRUnichar*)nsMemory::Alloc(([inText length]+1)*sizeof(PRUnichar));
 1241:     if ( text ) {
 1242:       [inText getCharacters:text];
 1243:       text[[inText length]] = 0;
 1244:       webFind->SetSearchString(text);
 1245:       PRBool found;
 1246:       webFind->FindNext(&found);
 1247:       nsMemory::Free(text);
 1248:     }
 1249: }
 1250: 
 1251: -(void)findAgain
 1252: {
 1253: }
 1254: 
 1255: -(void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
 1256: {
 1257:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
 1258:     nsCOMPtr<nsIDOMWindow> domWindow;
 1259:     wbf->GetFocusedWindow(getter_AddRefs(domWindow));
 1260:     if (!domWindow)
 1261:         _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
 1262:     if (!domWindow)
 1263:         return;
 1264:     
 1265:     nsCOMPtr<nsIDOMDocument> domDocument;
 1266:     domWindow->GetDocument(getter_AddRefs(domDocument));
 1267:     if (!domDocument)
 1268:         return;
 1269:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
 1270:     if (!nsDoc)
 1271:         return;
 1272:     nsCOMPtr<nsIDOMLocation> location;
 1273:     nsDoc->GetLocation(getter_AddRefs(location));
 1274:     if (!location)
 1275:         return;
 1276:     nsAutoString urlStr;
 1277:     location->GetHref(urlStr);
 1278:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
 1279:     nsCOMPtr<nsIURI> url;
 1280:     nsresult rv = NS_NewURI(getter_AddRefs(url), urlCStr.get());
 1281:     if (NS_FAILED(rv))
 1282:         return;
 1283:         
 1284: #if 0
 1285:     nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDocument));
 1286:     if (!doc)
 1287:         return;
 1288:     nsCOMPtr<nsIURI> url;
 1289:     doc->GetDocumentURL(getter_AddRefs(url));
 1290: #endif
 1291:      
 1292:     [self saveInternal: url.get()
 1293:           withDocument: domDocument
 1294:           bypassCache: NO
 1295:           filterView: aFilterView
 1296:           filterList: aFilterList];
 1297: }
 1298: 
 1299: 
 1300: -(IBAction)cut:(id)aSender
 1301: {
 1302:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1303:     clipboard->CutSelection();
 1304: }
 1305: 
 1306: -(IBAction)copy:(id)aSender
 1307: {
 1308:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1309:     clipboard->CopySelection();
 1310: }
 1311: 
 1312: -(IBAction)paste:(id)aSender
 1313: {
 1314:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1315:     clipboard->Paste();
 1316: }
 1317: 
 1318: -(IBAction)clear:(id)aSender
 1319: {
 1320:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1321:     clipboard->SelectNone();
 1322: }
 1323: 
 1324: -(IBAction)selectAll:(id)aSender
 1325: {
 1326:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1327:     clipboard->SelectAll();
 1328: }
 1329: 
 1330: -(IBAction)undo:(id)aSender
 1331: {
 1332:   NSLog(@"Undo not yet implemented");
 1333: }
 1334: 
 1335: -(NSString*)getCurrentURLSpec
 1336: {
 1337:     NSString* empty = @"";
 1338:     nsCOMPtr<nsIDOMWindow> domWindow;
 1339:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
 1340:     if (!domWindow)
 1341:         return empty;
 1342:     
 1343:     nsCOMPtr<nsIDOMDocument> domDocument;
 1344:     domWindow->GetDocument(getter_AddRefs(domDocument));
 1345:     if (!domDocument)
 1346:         return empty;
 1347:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
 1348:     if (!nsDoc)
 1349:         return empty;
 1350:     nsCOMPtr<nsIDOMLocation> location;
 1351:     nsDoc->GetLocation(getter_AddRefs(location));
 1352:     if (!location)
 1353:         return empty;
 1354:     nsAutoString urlStr;
 1355:     location->GetHref(urlStr);
 1356:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
 1357:     
 1358:     return [NSString stringWithCString: urlCStr.get()];
 1359: }
 1360: 
 1361: - (void)setActive: (BOOL)aIsActive
 1362: {
 1363:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
 1364:     if (aIsActive)
 1365:         wbf->Activate();
 1366:     else
 1367:         wbf->Deactivate();
 1368: }
 1369: 
 1370: -(NSMenu*)getContextMenu
 1371: {
 1372:   return [[self superview] getContextMenu];
 1373: }
 1374: @end
 1375: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>