Diff for /chimera/ProgressDlgController.mm between versions 1.4 and 1.13

version 1.4, 2002/02/03 19:40:10 version 1.13, 2002/03/07 18:42:43
Line 1 Line 1
///* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
//  ProgressDlgController.m/* ***** BEGIN LICENSE BLOCK *****
//  Chimera * Version: NPL 1.1/GPL 2.0/LGPL 2.1
// *
//  Created by David Hyatt on Sat Feb 02 2002. * The contents of this file are subject to the Netscape Public License
//  Copyright (c) 2001 __MyCompanyName__. All rights reserved. * Version 1.1 (the "License"); you may not use this file except in
// * compliance with the License. You may obtain a copy of the License at
  * http://www.mozilla.org/NPL/
  * Software distributed under the License is distributed on an "AS IS" basis,
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  * for the specific language governing rights and limitations under the
  * License.
  * The Original Code is mozilla.org code.
  * The Initial Developer of the Original Code is 
  * Netscape Communications Corporation.
  * Portions created by the Initial Developer are Copyright (C) 2002
  * the Initial Developer. All Rights Reserved.
  * Contributor(s):
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the NPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the NPL, the GPL or the LGPL.
  * ***** END LICENSE BLOCK ***** */
 #import "ProgressDlgController.h"  #import "ProgressDlgController.h"
   #include "nsCOMPtr.h"
   #include "nsString.h"
 #include "nsIWebBrowserPersist.h"  #include "nsIWebBrowserPersist.h"
 #include "nsIInputStream.h"  #include "nsIInputStream.h"
 #include "nsIURL.h"  #include "nsIURL.h"
#include "nsIDOMDocument.h"#include "nsILocalFile.h"
 #include "nsIDOMHTMLDocument.h"
 #include "nsIWebProgressListener.h"
 #include "nsIComponentManager.h"
 #include "nsIPrefBranch.h"
 const char* prefContractID = "@mozilla.org/preferences-service;1";
 class nsDownloadListener : public nsIWebProgressListener
     nsDownloadListener(ProgressDlgController* aController,
                        nsIWebBrowserPersist* aPersist,
                        nsISupports* aSource,
                        NSString* aDestination,
                        const char* aContentType,
                        nsIInputStream* aPostData,
                        BOOL aBypassCache)
         mController = aController;
         mWebPersist = aPersist;
         // The source is either a simple URL or a complete document.
         mURL = do_QueryInterface(aSource);
         if (!mURL)
             mDocument = do_QueryInterface(aSource);
         nsCAutoString dstStr = [aDestination cString];
         NS_NewLocalFile(dstStr.get(), PR_FALSE, getter_AddRefs(mDestination));
         mContentType = aContentType;
         mPostData = aPostData;
         mBypassCache = aBypassCache;
     virtual ~nsDownloadListener() {};
     void BeginDownload();
     void InitDialog();
 private: // Member variables
     ProgressDlgController* mController; // Controller for our UI.
     nsCOMPtr<nsIWebBrowserPersist> mWebPersist; // Our web persist object.
     nsCOMPtr<nsIURL> mURL; // The URL of our source file. Null if we're saving a complete document.
     nsCOMPtr<nsILocalFile> mDestination; // Our destination URL.
     nsCString mContentType; // Our content type string.
     nsCOMPtr<nsIDOMHTMLDocument> mDocument; // A DOM document.  Null if we're only saving a simple URL.
     nsCOMPtr<nsIInputStream> mPostData;  // For complete documents, this is our post data from session history.
     PRBool mBypassCache; // Whether we should bypass the cache or not.
 NS_IMPL_ISUPPORTS1(nsDownloadListener, nsIWebProgressListener)
 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
 nsDownloadListener::OnProgressChange(nsIWebProgress *aWebProgress, 
                                          nsIRequest *aRequest, 
                                          PRInt32 aCurSelfProgress, 
                                          PRInt32 aMaxSelfProgress, 
                                          PRInt32 aCurTotalProgress, 
                                          PRInt32 aMaxTotalProgress)
   return NS_OK;
 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
 nsDownloadListener::OnLocationChange(nsIWebProgress *aWebProgress, 
                                          nsIRequest *aRequest, 
                                          nsIURI *location)
   return NS_OK;
 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
 nsDownloadListener::OnStatusChange(nsIWebProgress *aWebProgress, 
                                        nsIRequest *aRequest, 
                                        nsresult aStatus, 
                                        const PRUnichar *aMessage)
   return NS_OK;
 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
 nsDownloadListener::OnSecurityChange(nsIWebProgress *aWebProgress, 
                                                       nsIRequest *aRequest, 
                                   PRInt32 state)
   return NS_OK;
 // Implementation of nsIWebProgressListener
 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
 nsDownloadListener::OnStateChange(nsIWebProgress *aWebProgress, 
                                       nsIRequest *aRequest, 
                                       PRInt32 aStateFlags, 
                                       PRUint32 aStatus)
     return NS_OK;
     if (mWebPersist) {
         PRInt32 flags = nsIWebBrowserPersist::PERSIST_FLAGS_NO_CONVERSION | 
         if (mBypassCache)
             flags |= nsIWebBrowserPersist::PERSIST_FLAGS_BYPASS_CACHE;
             flags |= nsIWebBrowserPersist::PERSIST_FLAGS_FROM_CACHE;
         if (mURL)
             mWebPersist->SaveURI(mURL, mPostData, mDestination);
         else {
             PRInt32 encodingFlags = 0;
             nsCOMPtr<nsILocalFile> filesFolder;
             if (!mContentType.Equals("text/plain")) {
                 // Create a local directory in the same dir as our file.  It
                 // will hold our associated files.
                 filesFolder = do_CreateInstance("@mozilla.org/file/local;1");
                 nsXPIDLString unicodePath;
                 nsXPIDLCString leafName;
                 nsCAutoString nameMinusExt(leafName.get());
                 PRInt32 index = nameMinusExt.RFind(".");
                 if (index >= 0)
                     nameMinusExt.Left(nameMinusExt, index);
                 nameMinusExt += " Files"; // XXXdwh needs to be localizable!
                 PRBool exists = PR_FALSE;
                 if (!exists)
                     filesFolder->Create(nsILocalFile::DIRECTORY_TYPE, 0755);
                 encodingFlags |= nsIWebBrowserPersist::ENCODE_FLAGS_FORMATTED |
                                  nsIWebBrowserPersist::ENCODE_FLAGS_ABSOLUTE_LINKS |
             mWebPersist->SaveDocument(mDocument, mDestination, filesFolder, mContentType.get(),
                                       encodingFlags, 80);
     if (!mURL && !mDocument)
     if (mWebPersist) {
         if (mURL) {
             nsCAutoString spec;
             [mController setSourceURL: spec.get()];
         else {
             nsAutoString spec;
             nsCAutoString spec2; spec2.AssignWithConversion(spec);
             [mController setSourceURL: spec2.get()];
     nsXPIDLString path;
     nsCAutoString pathStr; pathStr.AssignWithConversion(path.get());
     [mController setDestination: pathStr.get()];
 static NSString *SaveFileToolbarIdentifier      = @"Save File Dialog Toolbar";  static NSString *SaveFileToolbarIdentifier      = @"Save File Dialog Toolbar";
 static NSString *CancelToolbarItemIdentifier    = @"Cancel Toolbar Item";  static NSString *CancelToolbarItemIdentifier    = @"Cancel Toolbar Item";
Line 26  static NSString *LeaveOpenToolbarItemIde Line 241  static NSString *LeaveOpenToolbarItemIde
 @implementation ProgressDlgController  @implementation ProgressDlgController
 - (id)init  
     if ((self = [super init])) {  
         mDownloadIsPaused = NO;  //Get this value from preferences instead of hardcoding NO.  
         mSaveFileDialogShouldStayOpen = NO;  
     return self;  
 -(void)setWebPersist:(nsIWebBrowserPersist*)aPersist   -(void)setWebPersist:(nsIWebBrowserPersist*)aPersist 
               source:(nsISupports*)aSource                source:(nsISupports*)aSource
          destination:(NSString*)aDestination           destination:(NSString*)aDestination
Line 42  static NSString *LeaveOpenToolbarItemIde Line 248  static NSString *LeaveOpenToolbarItemIde
             postData:(nsIInputStream*)aInputStream              postData:(nsIInputStream*)aInputStream
          bypassCache:(BOOL)aBypassCache           bypassCache:(BOOL)aBypassCache
 {  {
       mDownloadListener = new nsDownloadListener(self, aPersist, aSource,
                                                  aDestination, aContentType,
                                                  aInputStream, aBypassCache);
   -(void) setSourceURL: (const char*)aSource
       [mFromField setStringValue: [NSString stringWithCString: aSource]];
   -(void) setDestination: (const char*)aDestination
       [mToField setStringValue: [NSString stringWithCString: aDestination]];
 }  }
 - (void)windowDidLoad  - (void)windowDidLoad
 {  {
       mDownloadIsPaused = NO; 
       nsCOMPtr<nsIPrefBranch> prefs(do_GetService(prefContractID));
       PRBool save = PR_FALSE;
       mSaveFileDialogShouldStayOpen = save;
     [self setupToolbar];      [self setupToolbar];
       if (mDownloadListener)
 }  }
 - (void)setupToolbar  - (void)setupToolbar
Line 81  static NSString *LeaveOpenToolbarItemIde Line 309  static NSString *LeaveOpenToolbarItemIde
         PauseResumeToolbarItemIdentifier,          PauseResumeToolbarItemIdentifier,
         NSToolbarFlexibleSpaceItemIdentifier,          NSToolbarFlexibleSpaceItemIdentifier,
         LeaveOpenToolbarItemIdentifier,          LeaveOpenToolbarItemIdentifier,
         ShowFileToolbarItemIdentifier,          ShowFileToolbarItemIdentifier,
         OpenFileToolbarItemIdentifier,          OpenFileToolbarItemIdentifier,
         nil];          nil];
Line 106  static NSString *LeaveOpenToolbarItemIde Line 335  static NSString *LeaveOpenToolbarItemIde
         [toolbarItem setImage:[NSImage imageNamed:@"savePause"]];          [toolbarItem setImage:[NSImage imageNamed:@"savePause"]];
         [toolbarItem setTarget:self];          [toolbarItem setTarget:self];
         [toolbarItem setAction:@selector(pauseAndResumeDownload)];          [toolbarItem setAction:@selector(pauseAndResumeDownload)];
        pauseResumeToggleToolbarItem = toolbarItem; //establish reference        if ( willBeInserted ) {
             pauseResumeToggleToolbarItem = toolbarItem; //establish reference
     } else if ( [itemIdent isEqual:ShowFileToolbarItemIdentifier] ) {      } else if ( [itemIdent isEqual:ShowFileToolbarItemIdentifier] ) {
         [toolbarItem setLabel:@"Show File"];          [toolbarItem setLabel:@"Show File"];
         [toolbarItem setPaletteLabel:@"Show File"];          [toolbarItem setPaletteLabel:@"Show File"];
Line 137  static NSString *LeaveOpenToolbarItemIde Line 368  static NSString *LeaveOpenToolbarItemIde
             [toolbarItem setTarget:self];              [toolbarItem setTarget:self];
             [toolbarItem setAction:@selector(toggleLeaveOpen)];              [toolbarItem setAction:@selector(toggleLeaveOpen)];
         }          }
        leaveOpenToggleToolbarItem = toolbarItem; //establish reference        if ( willBeInserted ) {
             leaveOpenToggleToolbarItem = toolbarItem; //establish reference
     } else {      } else {
         toolbarItem = nil;          toolbarItem = nil;
     }      }
Line 148  static NSString *LeaveOpenToolbarItemIde Line 381  static NSString *LeaveOpenToolbarItemIde
 -(void)cancel  -(void)cancel
 {  {
     NSLog(@"Request to cancel download.");      NSLog(@"Request to cancel download.");
     //Put logic here!  
 }  }
 -(void)pauseAndResumeDownload  -(void)pauseAndResumeDownload
 {  {
     if ( ! mDownloadIsPaused ) {      if ( ! mDownloadIsPaused ) {
         //Do logic to pause download          //Do logic to pause download
         mDownloadIsPaused = YES;          mDownloadIsPaused = YES;
Line 169  static NSString *LeaveOpenToolbarItemIde Line 400  static NSString *LeaveOpenToolbarItemIde
         [pauseResumeToggleToolbarItem setToolTip:@"Pause this FTP file download"];          [pauseResumeToggleToolbarItem setToolTip:@"Pause this FTP file download"];
         [pauseResumeToggleToolbarItem setImage:[NSImage imageNamed:@"savePause"]];          [pauseResumeToggleToolbarItem setImage:[NSImage imageNamed:@"savePause"]];
     }      }
 }  }
 -(void)showFile  -(void)showFile
Line 185  static NSString *LeaveOpenToolbarItemIde Line 415  static NSString *LeaveOpenToolbarItemIde
 -(void)toggleLeaveOpen  -(void)toggleLeaveOpen
 {  {
     if ( ! mSaveFileDialogShouldStayOpen ) {      if ( ! mSaveFileDialogShouldStayOpen ) {
         //Do logic to set the preference and make it take effect  
         mSaveFileDialogShouldStayOpen = YES;          mSaveFileDialogShouldStayOpen = YES;
         [leaveOpenToggleToolbarItem setLabel:@"Leave Open"];          [leaveOpenToggleToolbarItem setLabel:@"Leave Open"];
         [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];          [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];
         [leaveOpenToggleToolbarItem setToolTip:@"Window will stay open when download finishes."];          [leaveOpenToggleToolbarItem setToolTip:@"Window will stay open when download finishes."];
         [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenYES"]];          [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenYES"]];
     } else {      } else {
         //Do logic to set the preference and make it take effect  
         mSaveFileDialogShouldStayOpen = NO;          mSaveFileDialogShouldStayOpen = NO;
         [leaveOpenToggleToolbarItem setLabel:@"Close When Done"];          [leaveOpenToggleToolbarItem setLabel:@"Close When Done"];
         [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];          [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];
         [leaveOpenToggleToolbarItem setToolTip:@"Window will close automatically when download finishes."];          [leaveOpenToggleToolbarItem setToolTip:@"Window will close automatically when download finishes."];
         [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenNO"]];          [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenNO"]];
     }      }
       nsCOMPtr<nsIPrefBranch> prefs(do_GetService(prefContractID));
       prefs->SetBoolPref("browser.download.progressDnldDialog.keepAlive", mSaveFileDialogShouldStayOpen);
   - (void)windowWillClose:(NSNotification *)notification
       [self autorelease];
   - (void)dealloc
       [super dealloc];
 }  }
 @end  @end

Removed from v.1.4  
changed lines
  Added in v.1.13

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