File:  [mozdev] / chimera / NSBrowserView.mm
Revision 1.30: download - view: text, annotated - select for diffs - revision graph
Wed Apr 10 07:46:37 2002 UTC (17 years, 7 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Adding context menus to the NIB.

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

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