File:  [mozdev] / chimera / ProgressDlgController.mm
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Mon Feb 4 00:55:49 2002 UTC (15 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Checking in stub for the DownloadListener that will update the download UI.

//
//  ProgressDlgController.m
//  Chimera
//
//  Created by David Hyatt on Sat Feb 02 2002.
//  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//

#import "ProgressDlgController.h"

#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsIWebBrowserPersist.h"
#include "nsIInputStream.h"
#include "nsIURL.h"
#include "nsIDOMDocument.h"
#include "nsIWebProgressListener.h"

class nsDownloadListener : public nsIWebProgressListener
{
public:
    nsDownloadListener(ProgressDlgController* aController,
                       nsIWebBrowserPersist* aPersist,
                       nsISupports* aSource,
                       NSString* aDestination,
                       const char* aContentType,
                       nsIInputStream* aPostData,
                       BOOL aBypassCache)
    {
        NS_INIT_REFCNT();
        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);
        mDestination = [aDestination cString];
        mContentType = aContentType;
        mPostData = aPostData;
        mBypassCache = aBypassCache;
    };
    
    virtual ~nsDownloadListener() {};

    NS_DECL_ISUPPORTS
    NS_DECL_NSIWEBPROGRESSLISTENER
  
public:
    void BeginDownload();

private: // Member variables
    ProgressDlgController* mController; // Controller for our UI.
    nsIWebBrowserPersist* mWebPersist; // Our web persist object.
    nsCOMPtr<nsIURL> mURL; // The URL of our source file. Null if we're saving a complete document.
    nsCString mDestination; // Our destination string.
    nsCString mContentType; // Our content type string.
    nsCOMPtr<nsIDOMDocument> 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); */
NS_IMETHODIMP 
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); */
NS_IMETHODIMP 
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); */
NS_IMETHODIMP 
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); */
NS_IMETHODIMP 
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); */
NS_IMETHODIMP 
nsDownloadListener::OnStateChange(nsIWebProgress *aWebProgress, 
				      nsIRequest *aRequest, 
				      PRInt32 aStateFlags, 
				      PRUint32 aStatus)
{
    return NS_OK;
}

void
nsDownloadListener::BeginDownload()
{

}

static NSString *SaveFileToolbarIdentifier			= @"Save File Dialog Toolbar";
static NSString *CancelToolbarItemIdentifier		= @"Cancel Toolbar Item";
static NSString *PauseResumeToolbarItemIdentifier	= @"Pause and Resume Toggle Toolbar Item";
static NSString *ShowFileToolbarItemIdentifier		= @"Show File Toolbar Item";
static NSString *OpenFileToolbarItemIdentifier		= @"Open File Toolbar Item";
static NSString *LeaveOpenToolbarItemIdentifier		= @"Leave Open Toggle Toolbar Item";

@interface ProgressDlgController(Private)
-(void)setupToolbar;
@end

@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 
              source:(nsISupports*)aSource
         destination:(NSString*)aDestination
         contentType:(const char*)aContentType
            postData:(nsIInputStream*)aInputStream
         bypassCache:(BOOL)aBypassCache
{
    nsDownloadListener* downloadListener = new nsDownloadListener(self, aPersist, aSource,
                                                                  aDestination, aContentType,
                                                                  aInputStream, aBypassCache);
    if (!downloadListener)
        return;
    
    downloadListener->BeginDownload();
}

- (void)windowDidLoad
{
    [self setupToolbar];
}

- (void)setupToolbar
{
    NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:SaveFileToolbarIdentifier] autorelease];

    [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
    [toolbar setAllowsUserCustomization:YES];
    [toolbar setAutosavesConfiguration:YES];
    [toolbar setDelegate:self];
    [[self window] setToolbar:toolbar];
}

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
    return [NSArray arrayWithObjects: CancelToolbarItemIdentifier,
        PauseResumeToolbarItemIdentifier,
        ShowFileToolbarItemIdentifier,
        OpenFileToolbarItemIdentifier,
        LeaveOpenToolbarItemIdentifier,
        NSToolbarCustomizeToolbarItemIdentifier,
        NSToolbarFlexibleSpaceItemIdentifier,
        NSToolbarSpaceItemIdentifier,
        NSToolbarSeparatorItemIdentifier,
        nil];
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
    return [NSArray arrayWithObjects: CancelToolbarItemIdentifier,
        PauseResumeToolbarItemIdentifier,
        NSToolbarFlexibleSpaceItemIdentifier,
        LeaveOpenToolbarItemIdentifier,
        ShowFileToolbarItemIdentifier,
        OpenFileToolbarItemIdentifier,
        nil];
}

- (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
      itemForItemIdentifier:(NSString *)itemIdent
  willBeInsertedIntoToolbar:(BOOL)willBeInserted
{
    NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];

    if ( [itemIdent isEqual:CancelToolbarItemIdentifier] ) {
        [toolbarItem setLabel:@"Cancel"];
        [toolbarItem setPaletteLabel:@"Cancel Download"];
        [toolbarItem setToolTip:@"Cancel this file download"];
        [toolbarItem setImage:[NSImage imageNamed:@"saveCancel"]];
        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(cancel)];
    } else if ( [itemIdent isEqual:PauseResumeToolbarItemIdentifier] ) {
        [toolbarItem setLabel:@"Pause"];
        [toolbarItem setPaletteLabel:@"Pause Download"];
        [toolbarItem setToolTip:@"Pause this FTP file download"];
        [toolbarItem setImage:[NSImage imageNamed:@"savePause"]];
        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(pauseAndResumeDownload)];
        pauseResumeToggleToolbarItem = toolbarItem; //establish reference
    } else if ( [itemIdent isEqual:ShowFileToolbarItemIdentifier] ) {
        [toolbarItem setLabel:@"Show File"];
        [toolbarItem setPaletteLabel:@"Show File"];
        [toolbarItem setToolTip:@"Show the saved file in the Finder"];
        [toolbarItem setImage:[NSImage imageNamed:@"saveShowFile"]];
        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(showFile)];
    } else if ( [itemIdent isEqual:OpenFileToolbarItemIdentifier] ) {
        [toolbarItem setLabel:@"Open File"];
        [toolbarItem setPaletteLabel:@"Open File"];
        [toolbarItem setToolTip:@"Open the saved file in its default application."];
        [toolbarItem setImage:[NSImage imageNamed:@"saveOpenFile"]];
        [toolbarItem setTarget:self];
        [toolbarItem setAction:@selector(openFile)];
    } else if ( [itemIdent isEqual:LeaveOpenToolbarItemIdentifier] ) {
        if ( mSaveFileDialogShouldStayOpen ) {
            [toolbarItem setLabel:@"Leave Open"];
            [toolbarItem setPaletteLabel:@"Toggle Close Behavior"];
            [toolbarItem setToolTip:@"Window will stay open when download finishes."];
            [toolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenYES"]];
            [toolbarItem setTarget:self];
            [toolbarItem setAction:@selector(toggleLeaveOpen)];
        } else {
            [toolbarItem setLabel:@"Close When Done"];
            [toolbarItem setPaletteLabel:@"Toggle Close Behavior"];
            [toolbarItem setToolTip:@"Window will close automatically when download finishes."];
            [toolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenNO"]];
            [toolbarItem setTarget:self];
            [toolbarItem setAction:@selector(toggleLeaveOpen)];
        }
        leaveOpenToggleToolbarItem = toolbarItem; //establish reference
    } else {
        toolbarItem = nil;
    }

    return toolbarItem;
}

-(void)cancel
{
    NSLog(@"Request to cancel download.");
    //Put logic here!
}

-(void)pauseAndResumeDownload
{
    if ( ! mDownloadIsPaused ) {
        //Do logic to pause download
        mDownloadIsPaused = YES;
        [pauseResumeToggleToolbarItem setLabel:@"Resume"];
        [pauseResumeToggleToolbarItem setPaletteLabel:@"Resume Download"];
        [pauseResumeToggleToolbarItem setToolTip:@"Resume the paused FTP download"];
        [pauseResumeToggleToolbarItem setImage:[NSImage imageNamed:@"saveResume"]];
    } else {
        //Do logic to resume download
        mDownloadIsPaused = NO;
        [pauseResumeToggleToolbarItem setLabel:@"Pause"];
        [pauseResumeToggleToolbarItem setPaletteLabel:@"Pause Download"];
        [pauseResumeToggleToolbarItem setToolTip:@"Pause this FTP file download"];
        [pauseResumeToggleToolbarItem setImage:[NSImage imageNamed:@"savePause"]];
    }
}

-(void)showFile
{
    NSLog(@"Request to show file.");
}

-(void)openFile
{
    NSLog(@"Request to open file.");
}

-(void)toggleLeaveOpen
{
    if ( ! mSaveFileDialogShouldStayOpen ) {
        //Do logic to set the preference and make it take effect
        mSaveFileDialogShouldStayOpen = YES;
        [leaveOpenToggleToolbarItem setLabel:@"Leave Open"];
        [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];
        [leaveOpenToggleToolbarItem setToolTip:@"Window will stay open when download finishes."];
        [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenYES"]];
    } else {
        //Do logic to set the preference and make it take effect
        mSaveFileDialogShouldStayOpen = NO;
        [leaveOpenToggleToolbarItem setLabel:@"Close When Done"];
        [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];
        [leaveOpenToggleToolbarItem setToolTip:@"Window will close automatically when download finishes."];
        [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenNO"]];
    }
}

@end

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