File:  [mozdev] / chimera / NSBrowserView.mm
Revision 1.33: download - view: text, annotated - select for diffs - revision graph
Sun Apr 14 08:43:03 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
So many changes I can't even go into it. :)

    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,
  663:                     const nsCString& aSuggestedFilename, PRBool aBypassCache,
  664:                     NSView* aFilterView, NSPopUpButton* aFilterList)
  665:     {
  666:         NS_INIT_REFCNT();
  667:         mPersist = aPersist;
  668:         mTmpFile = aFile;
  669:         mURL = aURL;
  670:         mDocument = aDocument;
  671:         mPostData = aPostData;
  672:         mDefaultFilename = aSuggestedFilename;
  673:         mBypassCache = aBypassCache;
  674:         mFilterView = aFilterView;
  675:         mFilterList = aFilterList;
  676:     }
  677:                   
  678:     virtual ~nsHeaderSniffer() 
  679:     {
  680:     };
  681: 
  682:     NS_DECL_ISUPPORTS
  683:     NS_DECL_NSIWEBPROGRESSLISTENER
  684:   
  685: protected:
  686:     void PerformSave();
  687:     
  688: private:
  689:     nsIWebBrowserPersist* mPersist; // Weak. It owns us as a listener.
  690:     nsCOMPtr<nsIFile> mTmpFile;
  691:     nsCOMPtr<nsIURI> mURL;
  692:     nsCOMPtr<nsIDOMDocument> mDocument;
  693:     nsCOMPtr<nsIInputStream> mPostData;
  694:     nsCString mDefaultFilename;
  695:     PRBool mBypassCache;
  696:     nsCString mContentType;
  697:     nsCString mContentDisposition;
  698:     NSView* mFilterView;
  699:     NSPopUpButton* mFilterList;
  700: };
  701: 
  702: NS_IMPL_ISUPPORTS1(nsHeaderSniffer, nsIWebProgressListener)
  703: 
  704: // Implementation of nsIWebProgressListener
  705: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
  706: NS_IMETHODIMP 
  707: nsHeaderSniffer::OnStateChange(nsIWebProgress *aWebProgress, 
  708: 				      nsIRequest *aRequest, 
  709: 				      PRInt32 aStateFlags, 
  710: 				      PRUint32 aStatus)
  711: {
  712:     if (aStateFlags & nsIWebProgressListener::STATE_START) {
  713:         nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
  714:         channel->GetContentType(mContentType);
  715:        
  716:         // Get the content-disposition if we're an HTTP channel.
  717:         nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
  718:         if (httpChannel)
  719:             httpChannel->GetResponseHeader(nsCAutoString("content-disposition"), mContentDisposition);
  720:         
  721:         mPersist->CancelSave();
  722:         PRBool exists;
  723:         mTmpFile->Exists(&exists);
  724:         if (exists)
  725:             mTmpFile->Remove(PR_FALSE);
  726:         PerformSave();
  727:     }
  728:     return NS_OK;
  729: }
  730: 
  731: void nsHeaderSniffer::PerformSave()
  732: {
  733:     // Are we an HTML document? If so, we will want to append an accessory view to
  734:     // the save dialog to provide the user with the option of doing a complete
  735:     // save vs. a single file save.
  736:     PRBool isHTML = (mDocument && mContentType.Equals("text/html") ||
  737:                      mContentType.Equals("text/xml") ||
  738:                      mContentType.Equals("application/xhtml+xml"));
  739:     
  740:     // Next find out the directory that we should start in.
  741:     nsCOMPtr<nsIPrefService> prefs(do_GetService("@mozilla.org/preferences-service;1"));
  742:     if (!prefs)
  743:         return;
  744:     nsCOMPtr<nsIPrefBranch> dirBranch;
  745:     prefs->GetBranch("browser.download.", getter_AddRefs(dirBranch));
  746:     PRInt32 filterIndex = 0;
  747:     if (dirBranch) {
  748:         nsresult rv = dirBranch->GetIntPref("save_converter_index", &filterIndex);
  749:         if (NS_FAILED(rv))
  750:             filterIndex = 0;
  751:     }
  752:     if (mFilterList)
  753:         [mFilterList selectItemAtIndex: filterIndex];
  754:         
  755:     // We need to figure out what file name to use.
  756:     nsCAutoString defaultFileName;
  757:     if (!mContentDisposition.IsEmpty()) {
  758:         // (1) Use the HTTP header suggestion.
  759:         PRInt32 index = mContentDisposition.Find("filename=");
  760:         if (index >= 0) {
  761:             // Take the substring following the prefix.
  762:             index += 9;
  763:             nsCAutoString filename;
  764:             mContentDisposition.Right(filename, mContentDisposition.Length() - index);
  765:             defaultFileName = filename;
  766:         }
  767:     }
  768:     
  769:     if (defaultFileName.IsEmpty()) {
  770:         nsCOMPtr<nsIURL> url(do_QueryInterface(mURL));
  771:         if (url)
  772:             url->GetFileName(defaultFileName); // (2) For file URLs, use the file name.
  773:     }
  774:     
  775:     if (defaultFileName.IsEmpty() && mDocument && isHTML) {
  776:         nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(mDocument));
  777:         nsAutoString title;
  778:         if (htmlDoc)
  779:             htmlDoc->GetTitle(title); // (3) Use the title of the document.
  780:         defaultFileName.AssignWithConversion(title);
  781:     }
  782:     
  783:     if (defaultFileName.IsEmpty()) {
  784:         // (4) Use the caller provided name.
  785:         defaultFileName = mDefaultFilename;
  786:     }
  787: 
  788:     if (defaultFileName.IsEmpty())
  789:         // (5) Use the host.
  790:         mURL->GetHost(defaultFileName);
  791:     
  792:     // One last case to handle about:blank and other fruity untitled pages.
  793:     if (defaultFileName.IsEmpty())
  794:         defaultFileName = "untitled";
  795:         
  796:     // Validate the file name to ensure legality.
  797:     for (PRUint32 i = 0; i < defaultFileName.Length(); i++)
  798:         if (defaultFileName[i] == ':' || defaultFileName[i] == '/')
  799:             defaultFileName.SetCharAt(i, ' ');
  800:             
  801:     // Make sure the appropriate extension is appended to the suggested file name.
  802:     nsCOMPtr<nsIURI> fileURI(do_CreateInstance("@mozilla.org/network/standard-url;1"));
  803:     nsCOMPtr<nsIURL> fileURL(do_QueryInterface(fileURI));
  804:     if (!fileURL)
  805:         return;
  806:     fileURL->SetFilePath(defaultFileName);
  807:     
  808:     nsCAutoString fileExtension;
  809:     fileURL->GetFileExtension(fileExtension);
  810:     
  811:     PRBool setExtension = PR_FALSE;
  812:     if (mContentType.Equals("text/html")) {
  813:         if (fileExtension.IsEmpty() || (!fileExtension.Equals("htm") && !fileExtension.Equals("html"))) {
  814:             defaultFileName += ".html";
  815:             setExtension = PR_TRUE;
  816:         }
  817:     }
  818:     
  819:     if (!setExtension && fileExtension.IsEmpty()) {
  820:         nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1"));
  821:         if (!mimeService)
  822:             return;
  823:         nsCOMPtr<nsIMIMEInfo> mimeInfo;
  824:         mimeService->GetFromMIMEType(mContentType.get(), getter_AddRefs(mimeInfo));
  825:         
  826:         PRUint32 extCount;
  827:         char** extList;
  828:         mimeInfo->GetFileExtensions(&extCount, &extList);
  829:         
  830:         if (extCount > 0) {
  831:             defaultFileName += ".";
  832:             defaultFileName += extList[0];
  833:         }
  834:     }
  835:     
  836:     // Now it's time to pose the save dialog.
  837:     NSSavePanel* savePanel = [NSSavePanel savePanel];
  838:     NSString* file = nil;
  839:     if (!defaultFileName.IsEmpty())
  840:         file = [[NSString alloc] initWithCString: defaultFileName.get()];
  841:         
  842:     if (isHTML)
  843:         [savePanel setAccessoryView: mFilterView];
  844:         
  845:     if ([savePanel runModalForDirectory: nil file: file] == NSFileHandlingPanelCancelButton)
  846:         return;
  847:        
  848:     // Release the file string.
  849:     [file release];
  850:     
  851:     // Update the filter index.
  852:     if (isHTML && mFilterList) {
  853:         filterIndex = [mFilterList indexOfSelectedItem];
  854:         dirBranch->SetIntPref("save_converter_index", filterIndex);
  855:     }
  856:     
  857:     // Convert the content type to text/plain if it was selected in the filter.
  858:     if (isHTML && filterIndex == 2)
  859:         mContentType = "text/plain";
  860:     
  861:     nsCOMPtr<nsISupports> sourceData;
  862:     if (isHTML && filterIndex != 1)
  863:         sourceData = do_QueryInterface(mDocument);
  864:     else
  865:         sourceData = do_QueryInterface(mURL);
  866:         
  867:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
  868:     ProgressDlgController* progressDialog = [[ProgressDlgController alloc] initWithWindowNibName: @"ProgressDialog"];
  869:     [progressDialog setWebPersist: webPersist 
  870:                     source: sourceData.get()
  871:                     destination: [savePanel filename]
  872:                     contentType: mContentType.get()
  873:                     postData: mPostData
  874:                     bypassCache: mBypassCache];
  875:                     
  876:     [progressDialog showWindow: progressDialog];
  877: }
  878: 
  879: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
  880: NS_IMETHODIMP 
  881: nsHeaderSniffer::OnProgressChange(nsIWebProgress *aWebProgress, 
  882: 					 nsIRequest *aRequest, 
  883: 					 PRInt32 aCurSelfProgress, 
  884: 					 PRInt32 aMaxSelfProgress, 
  885: 					 PRInt32 aCurTotalProgress, 
  886: 					 PRInt32 aMaxTotalProgress)
  887: {
  888:   return NS_OK;
  889: }
  890: 
  891: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
  892: NS_IMETHODIMP 
  893: nsHeaderSniffer::OnLocationChange(nsIWebProgress *aWebProgress, 
  894: 					 nsIRequest *aRequest, 
  895: 					 nsIURI *location)
  896: {
  897:   return NS_OK;
  898: }
  899: 
  900: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
  901: NS_IMETHODIMP 
  902: nsHeaderSniffer::OnStatusChange(nsIWebProgress *aWebProgress, 
  903: 				       nsIRequest *aRequest, 
  904: 				       nsresult aStatus, 
  905: 				       const PRUnichar *aMessage)
  906: {
  907:   return NS_OK;
  908: }
  909: 
  910: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
  911: NS_IMETHODIMP 
  912: nsHeaderSniffer::OnSecurityChange(nsIWebProgress *aWebProgress, 
  913: 					              nsIRequest *aRequest, 
  914:                                   PRInt32 state)
  915: {
  916:   return NS_OK;
  917: }
  918: 
  919: 
  920: @implementation NSBrowserView
  921: 
  922: - (id)initWithFrame:(NSRect)frame
  923: {
  924:   [super initWithFrame:frame];
  925: 
  926:   nsresult rv = nsCocoaBrowserService::InitEmbedding();
  927:   if (NS_FAILED(rv)) {
  928:     // XXX need to throw
  929:   }
  930:   
  931:   _listener = new nsCocoaBrowserListener(self);
  932:   NS_ADDREF(_listener);
  933: 
  934:   // Create the web browser instance
  935:   nsCOMPtr<nsIWebBrowser> browser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
  936:   if (NS_FAILED(rv)) {
  937:     // XXX need to throw
  938:   }
  939: 
  940:   _webBrowser = browser;
  941:   NS_ADDREF(_webBrowser);
  942: 
  943:   // Set the container nsIWebBrowserChrome
  944:   _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
  945: 						 _listener));
  946:   
  947:   // Register as a listener for web progress
  948:   nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, _listener));
  949:   _webBrowser->AddWebBrowserListener(weak, NS_GET_IID(nsIWebProgressListener));
  950: 
  951:   // Hook up the widget hierarchy with us as the parent
  952:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
  953:   baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
  954: 		      frame.size.width, frame.size.height);
  955:   baseWin->Create();
  956: 
  957:   return self;
  958: }
  959: 
  960: - (void)destroyWebBrowser
  961: {
  962:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
  963:   baseWin->Destroy();
  964: }
  965: 
  966: - (void)dealloc 
  967: {
  968:   [super dealloc];
  969:   
  970:   NS_RELEASE(_listener);
  971:   NS_IF_RELEASE(_webBrowser);
  972:   
  973:   nsCocoaBrowserService::BrowserClosed();
  974:   
  975:   printf("NSBrowserView died.\n");
  976: }
  977: 
  978: - (void)setFrame:(NSRect)frameRect 
  979: {
  980:   [super setFrame:frameRect];
  981:   if (_webBrowser) {
  982:     nsCOMPtr<nsIBaseWindow> window = do_QueryInterface(_webBrowser);
  983:     window->SetSize((PRInt32)frameRect.size.width, 
  984: 		    (PRInt32)frameRect.size.height,
  985: 		    PR_TRUE);
  986:   }
  987: }
  988: 
  989: - (void)addListener:(id <NSBrowserListener>)listener
  990: {
  991:   _listener->AddListener(listener);
  992: }
  993: 
  994: - (void)removeListener:(id <NSBrowserListener>)listener
  995: {
  996:   _listener->RemoveListener(listener);
  997: }
  998: 
  999: - (void)setContainer:(id <NSBrowserContainer>)container
 1000: {
 1001:   _listener->SetContainer(container);
 1002: }
 1003: 
 1004: - (nsIDOMWindow*)getContentWindow
 1005: {
 1006:   nsIDOMWindow* window;
 1007: 
 1008:   _webBrowser->GetContentDOMWindow(&window);
 1009: 
 1010:   return window;
 1011: }
 1012: 
 1013: - (void)loadURI:(NSURL *)url flags:(unsigned int)flags
 1014: {
 1015:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1016:   
 1017:   NSString* spec = [url absoluteString];
 1018:   int length = [spec length];
 1019:   PRUnichar* specStr = nsMemory::Alloc((length+1) * sizeof(PRUnichar));
 1020:   [spec getCharacters:specStr];
 1021:   specStr[length] = PRUnichar(0);
 1022:   
 1023: 
 1024:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
 1025:   if (flags & NSLoadFlagsDontPutInHistory) {
 1026:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY;
 1027:   }
 1028:   if (flags & NSLoadFlagsReplaceHistoryEntry) {
 1029:     navFlags |= nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY;
 1030:   }
 1031:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
 1032:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
 1033:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
 1034:   }
 1035: 
 1036:   nsresult rv = nav->LoadURI(specStr, navFlags, nsnull, nsnull, nsnull);
 1037:   if (NS_FAILED(rv)) {
 1038:     // XXX need to throw
 1039:   }
 1040: 
 1041:   nsMemory::Free(specStr);
 1042: }
 1043: 
 1044: - (void)reload:(unsigned int)flags
 1045: {
 1046:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1047: 
 1048:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
 1049:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
 1050:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
 1051:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
 1052:   }
 1053: 
 1054:   nsresult rv = nav->Reload(navFlags);
 1055:   if (NS_FAILED(rv)) {
 1056:     // XXX need to throw
 1057:   }  
 1058: }
 1059: 
 1060: - (BOOL)canGoBack
 1061: {
 1062:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1063: 
 1064:   PRBool can;
 1065:   nav->GetCanGoBack(&can);
 1066: 
 1067:   return can ? YES : NO;
 1068: }
 1069: 
 1070: - (BOOL)canGoForward
 1071: {
 1072:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1073: 
 1074:   PRBool can;
 1075:   nav->GetCanGoForward(&can);
 1076: 
 1077:   return can ? YES : NO;
 1078: }
 1079: 
 1080: - (void)goBack
 1081: {
 1082:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1083: 
 1084:   nsresult rv = nav->GoBack();
 1085:   if (NS_FAILED(rv)) {
 1086:     // XXX need to throw
 1087:   }  
 1088: }
 1089: 
 1090: - (void)goForward
 1091: {
 1092:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1093: 
 1094:   nsresult rv = nav->GoForward();
 1095:   if (NS_FAILED(rv)) {
 1096:     // XXX need to throw
 1097:   }  
 1098: }
 1099: 
 1100: - (void)gotoIndex:(int)index
 1101: {
 1102:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1103: 
 1104:   nsresult rv = nav->GotoIndex(index);
 1105:   if (NS_FAILED(rv)) {
 1106:     // XXX need to throw
 1107:   }    
 1108: }
 1109: 
 1110: - (void)stop:(unsigned int)flags
 1111: {
 1112:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1113: 
 1114:   nsresult rv = nav->Stop(flags);
 1115:   if (NS_FAILED(rv)) {
 1116:     // XXX need to throw
 1117:   }    
 1118: }
 1119: 
 1120: - (NSURL*)getCurrentURI
 1121: {
 1122:   nsCOMPtr<nsIURI> uri;
 1123:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
 1124: 
 1125:   nav->GetCurrentURI(getter_AddRefs(uri));
 1126:   if (!uri) {
 1127:     return nsnull;
 1128:   }
 1129: 
 1130:   nsCAutoString spec;
 1131:   uri->GetSpec(spec);
 1132:   
 1133:   const char* cstr = spec.get();
 1134:   NSString* str = [NSString stringWithCString:cstr];
 1135:   NSURL* url = [NSURL URLWithString:str];
 1136:   
 1137:   return url;
 1138: }
 1139: 
 1140: - (nsCocoaBrowserListener*)getCocoaBrowserListener
 1141: {
 1142:   return _listener;
 1143: }
 1144: 
 1145: - (nsIWebBrowser*)getWebBrowser
 1146: {
 1147:   NS_IF_ADDREF(_webBrowser);
 1148:   return _webBrowser;
 1149: }
 1150: 
 1151: - (void)setWebBrowser:(nsIWebBrowser*)browser
 1152: {
 1153:   _webBrowser = browser;
 1154: 
 1155:   if (_webBrowser) {
 1156:     // Set the container nsIWebBrowserChrome
 1157:     _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
 1158: 						   _listener));
 1159: 
 1160:     NSRect frame = [self frame];
 1161:  
 1162:     // Hook up the widget hierarchy with us as the parent
 1163:     nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
 1164:     baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
 1165: 			frame.size.width, frame.size.height);
 1166:     baseWin->Create();
 1167:   }
 1168: 
 1169: }
 1170: 
 1171: -(void) saveInternal: (nsIURI*)aURI
 1172:         withDocument: (nsIDOMDocument*)aDocument
 1173:         suggestedFilename: (const char*)aFilename
 1174:         bypassCache: (BOOL)aBypassCache
 1175:         filterView: (NSView*)aFilterView
 1176:         filterList: (NSPopUpButton*)aFilterList
 1177: {
 1178:     // Create our web browser persist object.  This is the object that knows
 1179:     // how to actually perform the saving of the page (and of the images
 1180:     // on the page).
 1181:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
 1182:     if (!webPersist)
 1183:         return;
 1184:     
 1185:     // Make a temporary file object that we can save to.
 1186:     nsCOMPtr<nsIProperties> dirService(do_GetService(dirServiceContractID));
 1187:     if (!dirService)
 1188:         return;
 1189:     nsCOMPtr<nsIFile> tmpFile;
 1190:     dirService->Get("TmpD", NS_GET_IID(nsIFile), getter_AddRefs(tmpFile));
 1191:     static short unsigned int tmpRandom = 0;
 1192:     nsCAutoString tmpNo; tmpNo.AppendInt(tmpRandom++);
 1193:     nsCAutoString saveFile("-sav");
 1194:     saveFile += tmpNo;
 1195:     saveFile += "tmp";
 1196:     tmpFile->Append(saveFile.get()); 
 1197:     
 1198:     // Get the post data if we're an HTML doc.
 1199:     nsCOMPtr<nsIInputStream> postData;
 1200:     if (aDocument) {
 1201:       nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(_webBrowser));
 1202:       nsCOMPtr<nsISHistory> sessionHistory;
 1203:       webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
 1204:       nsCOMPtr<nsIHistoryEntry> entry;
 1205:       PRInt32 sindex;
 1206:       sessionHistory->GetIndex(&sindex);
 1207:       sessionHistory->GetEntryAtIndex(sindex, PR_FALSE, getter_AddRefs(entry));
 1208:       nsCOMPtr<nsISHEntry> shEntry(do_QueryInterface(entry));
 1209:       if (shEntry)
 1210:           shEntry->GetPostData(getter_AddRefs(postData));
 1211:     }
 1212: 
 1213:     nsCAutoString fileName(aFilename);
 1214:     nsHeaderSniffer* sniffer = new nsHeaderSniffer(webPersist, tmpFile, aURI, 
 1215:                                                    aDocument, postData, fileName, aBypassCache,
 1216:                                                    aFilterView, aFilterList);
 1217:     if (!sniffer)
 1218:         return;
 1219:     webPersist->SetProgressListener(sniffer);
 1220:     webPersist->SaveURI(aURI, nsnull, tmpFile);
 1221: }
 1222: 
 1223: -(void)printDocument
 1224: {
 1225:     nsCOMPtr<nsIDOMWindow> domWindow;
 1226:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
 1227:     nsCOMPtr<nsIInterfaceRequestor> ir(do_QueryInterface(domWindow));
 1228:     nsCOMPtr<nsIWebBrowserPrint> print;
 1229:     ir->GetInterface(NS_GET_IID(nsIWebBrowserPrint), getter_AddRefs(print));
 1230:     print->Print(nsnull, nsnull);
 1231: }
 1232: 
 1233: -(BOOL)findInPage:(NSString*)inText
 1234: {
 1235:     PRBool found =  PR_FALSE;
 1236:     
 1237:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
 1238:     nsCOMPtr<nsIDOMWindow> rootWindow;
 1239:     nsCOMPtr<nsIDOMWindow> focusedWindow;
 1240:     _webBrowser->GetContentDOMWindow(getter_AddRefs(rootWindow));
 1241:     wbf->GetFocusedWindow(getter_AddRefs(focusedWindow));
 1242:     if (!focusedWindow)
 1243:         focusedWindow = rootWindow;
 1244:     nsCOMPtr<nsIWebBrowserFind> webFind(do_GetInterface(_webBrowser));
 1245:     if ( webFind ) {
 1246:       nsCOMPtr<nsIWebBrowserFindInFrames> framesFind(do_QueryInterface(webFind));
 1247:       framesFind->SetRootSearchFrame(rootWindow);
 1248:       framesFind->SetCurrentSearchFrame(focusedWindow);
 1249:       
 1250:       PRUnichar* text = (PRUnichar*)nsMemory::Alloc(([inText length]+1)*sizeof(PRUnichar));
 1251:       if ( text ) {
 1252:         [inText getCharacters:text];
 1253:         text[[inText length]] = 0;
 1254:         webFind->SetSearchString(text);
 1255:         webFind->FindNext(&found);
 1256:         nsMemory::Free(text);
 1257:       }
 1258:     }
 1259:     return found;
 1260: }
 1261: 
 1262: - (void)saveURL: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
 1263:             url: (NSURL*)aURL suggestedFilename: (NSString*)aFilename
 1264: {
 1265:   nsCOMPtr<nsIURI> url;
 1266:   nsresult rv = NS_NewURI(getter_AddRefs(url), [[aURL absoluteString] cString]);
 1267:   if (NS_FAILED(rv))
 1268:     return;
 1269:   
 1270:   [self saveInternal: url.get()
 1271:         withDocument: nsnull
 1272:    suggestedFilename: [aFilename cString]
 1273:          bypassCache: YES
 1274:           filterView: aFilterView
 1275:           filterList: aFilterList];
 1276: }
 1277: 
 1278: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
 1279: {
 1280:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
 1281:     nsCOMPtr<nsIDOMWindow> domWindow;
 1282:     wbf->GetFocusedWindow(getter_AddRefs(domWindow));
 1283:     if (!domWindow)
 1284:         _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
 1285:     if (!domWindow)
 1286:         return;
 1287:     
 1288:     nsCOMPtr<nsIDOMDocument> domDocument;
 1289:     domWindow->GetDocument(getter_AddRefs(domDocument));
 1290:     if (!domDocument)
 1291:         return;
 1292:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
 1293:     if (!nsDoc)
 1294:         return;
 1295:     nsCOMPtr<nsIDOMLocation> location;
 1296:     nsDoc->GetLocation(getter_AddRefs(location));
 1297:     if (!location)
 1298:         return;
 1299:     nsAutoString urlStr;
 1300:     location->GetHref(urlStr);
 1301:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
 1302:     nsCOMPtr<nsIURI> url;
 1303:     nsresult rv = NS_NewURI(getter_AddRefs(url), urlCStr.get());
 1304:     if (NS_FAILED(rv))
 1305:         return;
 1306:     
 1307:     [self saveInternal: url.get()
 1308:           withDocument: domDocument
 1309:           suggestedFilename: ""
 1310:           bypassCache: NO
 1311:           filterView: aFilterView
 1312:           filterList: aFilterList];
 1313: }
 1314: 
 1315: 
 1316: -(IBAction)cut:(id)aSender
 1317: {
 1318:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1319:     clipboard->CutSelection();
 1320: }
 1321: 
 1322: -(IBAction)copy:(id)aSender
 1323: {
 1324:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1325:     clipboard->CopySelection();
 1326: }
 1327: 
 1328: -(IBAction)paste:(id)aSender
 1329: {
 1330:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1331:     clipboard->Paste();
 1332: }
 1333: 
 1334: -(IBAction)clear:(id)aSender
 1335: {
 1336:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1337:     clipboard->SelectNone();
 1338: }
 1339: 
 1340: -(IBAction)selectAll:(id)aSender
 1341: {
 1342:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
 1343:     clipboard->SelectAll();
 1344: }
 1345: 
 1346: -(IBAction)undo:(id)aSender
 1347: {
 1348:   NSLog(@"Undo not yet implemented");
 1349: }
 1350: 
 1351: -(NSString*)getCurrentURLSpec
 1352: {
 1353:     NSString* empty = @"";
 1354:     nsCOMPtr<nsIDOMWindow> domWindow;
 1355:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
 1356:     if (!domWindow)
 1357:         return empty;
 1358:     
 1359:     nsCOMPtr<nsIDOMDocument> domDocument;
 1360:     domWindow->GetDocument(getter_AddRefs(domDocument));
 1361:     if (!domDocument)
 1362:         return empty;
 1363:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
 1364:     if (!nsDoc)
 1365:         return empty;
 1366:     nsCOMPtr<nsIDOMLocation> location;
 1367:     nsDoc->GetLocation(getter_AddRefs(location));
 1368:     if (!location)
 1369:         return empty;
 1370:     nsAutoString urlStr;
 1371:     location->GetHref(urlStr);
 1372:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
 1373:     
 1374:     return [NSString stringWithCString: urlCStr.get()];
 1375: }
 1376: 
 1377: - (void)setActive: (BOOL)aIsActive
 1378: {
 1379:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
 1380:     if (aIsActive)
 1381:         wbf->Activate();
 1382:     else
 1383:         wbf->Deactivate();
 1384: }
 1385: 
 1386: -(NSMenu*)getContextMenu
 1387: {
 1388:   return [[self superview] getContextMenu];
 1389: }
 1390: @end
 1391: 

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