File:  [mozdev] / chimera / NSBrowserView.mm
Revision 1.17: download - view: text, annotated - select for diffs - revision graph
Sat Feb 9 01:24:11 2002 UTC (17 years, 3 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Stubs for adding bookmarks.

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

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