File:  [mozdev] / chimera / ProgressDlgController.mm
Revision 1.8: download - view: text, annotated - select for diffs - revision graph
Mon Feb 4 02:55:17 2002 UTC (17 years, 10 months ago) by macserv
Branches: MAIN
CVS tags: HEAD
WindowController leak removal... we'll see how well this works...

    1: //
    2: //  ProgressDlgController.m
    3: //  Chimera
    4: //
    5: //  Created by David Hyatt on Sat Feb 02 2002.
    6: //  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
    7: //
    8: 
    9: #import "ProgressDlgController.h"
   10: 
   11: #include "nsCOMPtr.h"
   12: #include "nsString.h"
   13: #include "nsIWebBrowserPersist.h"
   14: #include "nsIInputStream.h"
   15: #include "nsIURL.h"
   16: #include "nsILocalFile.h"
   17: #include "nsIDOMDocument.h"
   18: #include "nsIWebProgressListener.h"
   19: #include "nsIComponentManager.h"
   20: 
   21: class nsDownloadListener : public nsIWebProgressListener
   22: {
   23: public:
   24:     nsDownloadListener(ProgressDlgController* aController,
   25:                        nsIWebBrowserPersist* aPersist,
   26:                        nsISupports* aSource,
   27:                        NSString* aDestination,
   28:                        const char* aContentType,
   29:                        nsIInputStream* aPostData,
   30:                        BOOL aBypassCache)
   31:     {
   32:         NS_INIT_REFCNT();
   33:         mController = aController;
   34:         mWebPersist = aPersist;
   35:         // The source is either a simple URL or a complete document.
   36:         mURL = do_QueryInterface(aSource);
   37:         if (!mURL)
   38:             mDocument = do_QueryInterface(aSource);
   39:         nsCAutoString dstStr = [aDestination cString];
   40:         NS_NewLocalFile(dstStr.get(), PR_FALSE, getter_AddRefs(mDestination));
   41:         mContentType = aContentType;
   42:         mPostData = aPostData;
   43:         mBypassCache = aBypassCache;
   44:     };
   45:     
   46:     virtual ~nsDownloadListener() {};
   47: 
   48:     NS_DECL_ISUPPORTS
   49:     NS_DECL_NSIWEBPROGRESSLISTENER
   50:   
   51: public:
   52:     void BeginDownload();
   53: 
   54: private: // Member variables
   55:     ProgressDlgController* mController; // Controller for our UI.
   56:     nsCOMPtr<nsIWebBrowserPersist> mWebPersist; // Our web persist object.
   57:     nsCOMPtr<nsIURL> mURL; // The URL of our source file. Null if we're saving a complete document.
   58:     nsCOMPtr<nsILocalFile> mDestination; // Our destination URL.
   59:     nsCString mContentType; // Our content type string.
   60:     nsCOMPtr<nsIDOMDocument> mDocument; // A DOM document.  Null if we're only saving a simple URL.
   61:     nsCOMPtr<nsIInputStream> mPostData;  // For complete documents, this is our post data from session history.
   62:     PRBool mBypassCache; // Whether we should bypass the cache or not.
   63: };
   64: 
   65: NS_IMPL_ISUPPORTS1(nsDownloadListener, nsIWebProgressListener)
   66: 
   67: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
   68: NS_IMETHODIMP 
   69: nsDownloadListener::OnProgressChange(nsIWebProgress *aWebProgress, 
   70: 					 nsIRequest *aRequest, 
   71: 					 PRInt32 aCurSelfProgress, 
   72: 					 PRInt32 aMaxSelfProgress, 
   73: 					 PRInt32 aCurTotalProgress, 
   74: 					 PRInt32 aMaxTotalProgress)
   75: {
   76:   return NS_OK;
   77: }
   78: 
   79: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
   80: NS_IMETHODIMP 
   81: nsDownloadListener::OnLocationChange(nsIWebProgress *aWebProgress, 
   82: 					 nsIRequest *aRequest, 
   83: 					 nsIURI *location)
   84: {
   85:   return NS_OK;
   86: }
   87: 
   88: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
   89: NS_IMETHODIMP 
   90: nsDownloadListener::OnStatusChange(nsIWebProgress *aWebProgress, 
   91: 				       nsIRequest *aRequest, 
   92: 				       nsresult aStatus, 
   93: 				       const PRUnichar *aMessage)
   94: {
   95:   return NS_OK;
   96: }
   97: 
   98: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
   99: NS_IMETHODIMP 
  100: nsDownloadListener::OnSecurityChange(nsIWebProgress *aWebProgress, 
  101: 					              nsIRequest *aRequest, 
  102:                                   PRInt32 state)
  103: {
  104:   return NS_OK;
  105: }
  106: 
  107: // Implementation of nsIWebProgressListener
  108: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
  109: NS_IMETHODIMP 
  110: nsDownloadListener::OnStateChange(nsIWebProgress *aWebProgress, 
  111: 				      nsIRequest *aRequest, 
  112: 				      PRInt32 aStateFlags, 
  113: 				      PRUint32 aStatus)
  114: {
  115:     return NS_OK;
  116: }
  117: 
  118: void
  119: nsDownloadListener::BeginDownload()
  120: {
  121:     if (mWebPersist) {
  122:         mWebPersist->SetProgressListener(this);
  123:         PRInt32 flags = nsIWebBrowserPersist::PERSIST_FLAGS_NO_CONVERSION | 
  124:                         nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES;
  125:         if (mBypassCache)
  126:             flags |= nsIWebBrowserPersist::PERSIST_FLAGS_BYPASS_CACHE;
  127:         else
  128:             flags |= nsIWebBrowserPersist::PERSIST_FLAGS_FROM_CACHE;
  129:             
  130:         if (mURL)
  131:             mWebPersist->SaveURI(mURL, mPostData, mDestination);
  132:         else {
  133:             
  134:         }
  135:     }
  136: }
  137: 
  138: static NSString *SaveFileToolbarIdentifier			= @"Save File Dialog Toolbar";
  139: static NSString *CancelToolbarItemIdentifier		= @"Cancel Toolbar Item";
  140: static NSString *PauseResumeToolbarItemIdentifier	= @"Pause and Resume Toggle Toolbar Item";
  141: static NSString *ShowFileToolbarItemIdentifier		= @"Show File Toolbar Item";
  142: static NSString *OpenFileToolbarItemIdentifier		= @"Open File Toolbar Item";
  143: static NSString *LeaveOpenToolbarItemIdentifier		= @"Leave Open Toggle Toolbar Item";
  144: 
  145: @interface ProgressDlgController(Private)
  146: -(void)setupToolbar;
  147: @end
  148: 
  149: @implementation ProgressDlgController
  150: 
  151: - (id)init
  152: {
  153:     if ((self = [super init])) {
  154:         mDownloadIsPaused = NO;  //Get this value from preferences instead of hardcoding NO.
  155:         mSaveFileDialogShouldStayOpen = NO;
  156:     }
  157:     
  158:     mDownloadListener = nsnull;
  159:     
  160:     return self;
  161: }
  162: 
  163: -(void)setWebPersist:(nsIWebBrowserPersist*)aPersist 
  164:               source:(nsISupports*)aSource
  165:          destination:(NSString*)aDestination
  166:          contentType:(const char*)aContentType
  167:             postData:(nsIInputStream*)aInputStream
  168:          bypassCache:(BOOL)aBypassCache
  169: {
  170:     mDownloadListener = new nsDownloadListener(self, aPersist, aSource,
  171:                                                aDestination, aContentType,
  172:                                                aInputStream, aBypassCache);
  173: }
  174: 
  175: - (void)windowDidLoad
  176: {
  177:     [self setupToolbar];
  178:     if (mDownloadListener)
  179:         mDownloadListener->BeginDownload();
  180: }
  181: 
  182: - (void)setupToolbar
  183: {
  184:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:SaveFileToolbarIdentifier] autorelease];
  185: 
  186:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
  187:     [toolbar setAllowsUserCustomization:YES];
  188:     [toolbar setAutosavesConfiguration:YES];
  189:     [toolbar setDelegate:self];
  190:     [[self window] setToolbar:toolbar];
  191: }
  192: 
  193: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
  194: {
  195:     return [NSArray arrayWithObjects: CancelToolbarItemIdentifier,
  196:         PauseResumeToolbarItemIdentifier,
  197:         ShowFileToolbarItemIdentifier,
  198:         OpenFileToolbarItemIdentifier,
  199:         LeaveOpenToolbarItemIdentifier,
  200:         NSToolbarCustomizeToolbarItemIdentifier,
  201:         NSToolbarFlexibleSpaceItemIdentifier,
  202:         NSToolbarSpaceItemIdentifier,
  203:         NSToolbarSeparatorItemIdentifier,
  204:         nil];
  205: }
  206: 
  207: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
  208: {
  209:     return [NSArray arrayWithObjects: CancelToolbarItemIdentifier,
  210:         PauseResumeToolbarItemIdentifier,
  211:         NSToolbarFlexibleSpaceItemIdentifier,
  212:         LeaveOpenToolbarItemIdentifier,
  213:         ShowFileToolbarItemIdentifier,
  214:         OpenFileToolbarItemIdentifier,
  215:         nil];
  216: }
  217: 
  218: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  219:       itemForItemIdentifier:(NSString *)itemIdent
  220:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  221: {
  222:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  223: 
  224:     if ( [itemIdent isEqual:CancelToolbarItemIdentifier] ) {
  225:         [toolbarItem setLabel:@"Cancel"];
  226:         [toolbarItem setPaletteLabel:@"Cancel Download"];
  227:         [toolbarItem setToolTip:@"Cancel this file download"];
  228:         [toolbarItem setImage:[NSImage imageNamed:@"saveCancel"]];
  229:         [toolbarItem setTarget:self];
  230:         [toolbarItem setAction:@selector(cancel)];
  231:     } else if ( [itemIdent isEqual:PauseResumeToolbarItemIdentifier] ) {
  232:         [toolbarItem setLabel:@"Pause"];
  233:         [toolbarItem setPaletteLabel:@"Pause Download"];
  234:         [toolbarItem setToolTip:@"Pause this FTP file download"];
  235:         [toolbarItem setImage:[NSImage imageNamed:@"savePause"]];
  236:         [toolbarItem setTarget:self];
  237:         [toolbarItem setAction:@selector(pauseAndResumeDownload)];
  238:         pauseResumeToggleToolbarItem = toolbarItem; //establish reference
  239:     } else if ( [itemIdent isEqual:ShowFileToolbarItemIdentifier] ) {
  240:         [toolbarItem setLabel:@"Show File"];
  241:         [toolbarItem setPaletteLabel:@"Show File"];
  242:         [toolbarItem setToolTip:@"Show the saved file in the Finder"];
  243:         [toolbarItem setImage:[NSImage imageNamed:@"saveShowFile"]];
  244:         [toolbarItem setTarget:self];
  245:         [toolbarItem setAction:@selector(showFile)];
  246:     } else if ( [itemIdent isEqual:OpenFileToolbarItemIdentifier] ) {
  247:         [toolbarItem setLabel:@"Open File"];
  248:         [toolbarItem setPaletteLabel:@"Open File"];
  249:         [toolbarItem setToolTip:@"Open the saved file in its default application."];
  250:         [toolbarItem setImage:[NSImage imageNamed:@"saveOpenFile"]];
  251:         [toolbarItem setTarget:self];
  252:         [toolbarItem setAction:@selector(openFile)];
  253:     } else if ( [itemIdent isEqual:LeaveOpenToolbarItemIdentifier] ) {
  254:         if ( mSaveFileDialogShouldStayOpen ) {
  255:             [toolbarItem setLabel:@"Leave Open"];
  256:             [toolbarItem setPaletteLabel:@"Toggle Close Behavior"];
  257:             [toolbarItem setToolTip:@"Window will stay open when download finishes."];
  258:             [toolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenYES"]];
  259:             [toolbarItem setTarget:self];
  260:             [toolbarItem setAction:@selector(toggleLeaveOpen)];
  261:         } else {
  262:             [toolbarItem setLabel:@"Close When Done"];
  263:             [toolbarItem setPaletteLabel:@"Toggle Close Behavior"];
  264:             [toolbarItem setToolTip:@"Window will close automatically when download finishes."];
  265:             [toolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenNO"]];
  266:             [toolbarItem setTarget:self];
  267:             [toolbarItem setAction:@selector(toggleLeaveOpen)];
  268:         }
  269:         leaveOpenToggleToolbarItem = toolbarItem; //establish reference
  270:     } else {
  271:         toolbarItem = nil;
  272:     }
  273: 
  274:     return toolbarItem;
  275: }
  276: 
  277: -(void)cancel
  278: {
  279:     NSLog(@"Request to cancel download.");
  280:     //Put logic here!
  281: }
  282: 
  283: -(void)pauseAndResumeDownload
  284: {
  285:     if ( ! mDownloadIsPaused ) {
  286:         //Do logic to pause download
  287:         mDownloadIsPaused = YES;
  288:         [pauseResumeToggleToolbarItem setLabel:@"Resume"];
  289:         [pauseResumeToggleToolbarItem setPaletteLabel:@"Resume Download"];
  290:         [pauseResumeToggleToolbarItem setToolTip:@"Resume the paused FTP download"];
  291:         [pauseResumeToggleToolbarItem setImage:[NSImage imageNamed:@"saveResume"]];
  292:     } else {
  293:         //Do logic to resume download
  294:         mDownloadIsPaused = NO;
  295:         [pauseResumeToggleToolbarItem setLabel:@"Pause"];
  296:         [pauseResumeToggleToolbarItem setPaletteLabel:@"Pause Download"];
  297:         [pauseResumeToggleToolbarItem setToolTip:@"Pause this FTP file download"];
  298:         [pauseResumeToggleToolbarItem setImage:[NSImage imageNamed:@"savePause"]];
  299:     }
  300: }
  301: 
  302: -(void)showFile
  303: {
  304:     NSLog(@"Request to show file.");
  305: }
  306: 
  307: -(void)openFile
  308: {
  309:     NSLog(@"Request to open file.");
  310: }
  311: 
  312: -(void)toggleLeaveOpen
  313: {
  314:     if ( ! mSaveFileDialogShouldStayOpen ) {
  315:         //Do logic to set the preference and make it take effect
  316:         mSaveFileDialogShouldStayOpen = YES;
  317:         [leaveOpenToggleToolbarItem setLabel:@"Leave Open"];
  318:         [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];
  319:         [leaveOpenToggleToolbarItem setToolTip:@"Window will stay open when download finishes."];
  320:         [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenYES"]];
  321:     } else {
  322:         //Do logic to set the preference and make it take effect
  323:         mSaveFileDialogShouldStayOpen = NO;
  324:         [leaveOpenToggleToolbarItem setLabel:@"Close When Done"];
  325:         [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];
  326:         [leaveOpenToggleToolbarItem setToolTip:@"Window will close automatically when download finishes."];
  327:         [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenNO"]];
  328:     }
  329: }
  330: 
  331: - (void)windowWillClose:(NSNotification *)notification
  332: {
  333:     [self release];
  334: }
  335: 
  336: - (void)dealloc
  337: {
  338:     delete mDownloadListener;
  339:     mDownloadListener = nsnull;
  340:     [pauseResumeToggleToolbarItem release];
  341:     [leaveOpenToggleToolbarItem release];
  342:     [super dealloc];
  343: }
  344: 
  345: @end

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