File:  [mozdev] / chimera / ProgressDlgController.mm
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Mon Feb 4 01:33:00 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Saving of simple HTML source now 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)dealloc
  164: {
  165:     delete mDownloadListener;
  166:     mDownloadListener = nsnull;
  167:     [super dealloc];
  168: }
  169: 
  170: -(void)setWebPersist:(nsIWebBrowserPersist*)aPersist 
  171:               source:(nsISupports*)aSource
  172:          destination:(NSString*)aDestination
  173:          contentType:(const char*)aContentType
  174:             postData:(nsIInputStream*)aInputStream
  175:          bypassCache:(BOOL)aBypassCache
  176: {
  177:     mDownloadListener = new nsDownloadListener(self, aPersist, aSource,
  178:                                                aDestination, aContentType,
  179:                                                aInputStream, aBypassCache);
  180: }
  181: 
  182: - (void)windowDidLoad
  183: {
  184:     [self setupToolbar];
  185:     if (mDownloadListener)
  186:         mDownloadListener->BeginDownload();
  187: }
  188: 
  189: - (void)setupToolbar
  190: {
  191:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:SaveFileToolbarIdentifier] autorelease];
  192: 
  193:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
  194:     [toolbar setAllowsUserCustomization:YES];
  195:     [toolbar setAutosavesConfiguration:YES];
  196:     [toolbar setDelegate:self];
  197:     [[self window] setToolbar:toolbar];
  198: }
  199: 
  200: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
  201: {
  202:     return [NSArray arrayWithObjects: CancelToolbarItemIdentifier,
  203:         PauseResumeToolbarItemIdentifier,
  204:         ShowFileToolbarItemIdentifier,
  205:         OpenFileToolbarItemIdentifier,
  206:         LeaveOpenToolbarItemIdentifier,
  207:         NSToolbarCustomizeToolbarItemIdentifier,
  208:         NSToolbarFlexibleSpaceItemIdentifier,
  209:         NSToolbarSpaceItemIdentifier,
  210:         NSToolbarSeparatorItemIdentifier,
  211:         nil];
  212: }
  213: 
  214: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
  215: {
  216:     return [NSArray arrayWithObjects: CancelToolbarItemIdentifier,
  217:         PauseResumeToolbarItemIdentifier,
  218:         NSToolbarFlexibleSpaceItemIdentifier,
  219:         LeaveOpenToolbarItemIdentifier,
  220:         ShowFileToolbarItemIdentifier,
  221:         OpenFileToolbarItemIdentifier,
  222:         nil];
  223: }
  224: 
  225: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  226:       itemForItemIdentifier:(NSString *)itemIdent
  227:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  228: {
  229:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  230: 
  231:     if ( [itemIdent isEqual:CancelToolbarItemIdentifier] ) {
  232:         [toolbarItem setLabel:@"Cancel"];
  233:         [toolbarItem setPaletteLabel:@"Cancel Download"];
  234:         [toolbarItem setToolTip:@"Cancel this file download"];
  235:         [toolbarItem setImage:[NSImage imageNamed:@"saveCancel"]];
  236:         [toolbarItem setTarget:self];
  237:         [toolbarItem setAction:@selector(cancel)];
  238:     } else if ( [itemIdent isEqual:PauseResumeToolbarItemIdentifier] ) {
  239:         [toolbarItem setLabel:@"Pause"];
  240:         [toolbarItem setPaletteLabel:@"Pause Download"];
  241:         [toolbarItem setToolTip:@"Pause this FTP file download"];
  242:         [toolbarItem setImage:[NSImage imageNamed:@"savePause"]];
  243:         [toolbarItem setTarget:self];
  244:         [toolbarItem setAction:@selector(pauseAndResumeDownload)];
  245:         pauseResumeToggleToolbarItem = toolbarItem; //establish reference
  246:     } else if ( [itemIdent isEqual:ShowFileToolbarItemIdentifier] ) {
  247:         [toolbarItem setLabel:@"Show File"];
  248:         [toolbarItem setPaletteLabel:@"Show File"];
  249:         [toolbarItem setToolTip:@"Show the saved file in the Finder"];
  250:         [toolbarItem setImage:[NSImage imageNamed:@"saveShowFile"]];
  251:         [toolbarItem setTarget:self];
  252:         [toolbarItem setAction:@selector(showFile)];
  253:     } else if ( [itemIdent isEqual:OpenFileToolbarItemIdentifier] ) {
  254:         [toolbarItem setLabel:@"Open File"];
  255:         [toolbarItem setPaletteLabel:@"Open File"];
  256:         [toolbarItem setToolTip:@"Open the saved file in its default application."];
  257:         [toolbarItem setImage:[NSImage imageNamed:@"saveOpenFile"]];
  258:         [toolbarItem setTarget:self];
  259:         [toolbarItem setAction:@selector(openFile)];
  260:     } else if ( [itemIdent isEqual:LeaveOpenToolbarItemIdentifier] ) {
  261:         if ( mSaveFileDialogShouldStayOpen ) {
  262:             [toolbarItem setLabel:@"Leave Open"];
  263:             [toolbarItem setPaletteLabel:@"Toggle Close Behavior"];
  264:             [toolbarItem setToolTip:@"Window will stay open when download finishes."];
  265:             [toolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenYES"]];
  266:             [toolbarItem setTarget:self];
  267:             [toolbarItem setAction:@selector(toggleLeaveOpen)];
  268:         } else {
  269:             [toolbarItem setLabel:@"Close When Done"];
  270:             [toolbarItem setPaletteLabel:@"Toggle Close Behavior"];
  271:             [toolbarItem setToolTip:@"Window will close automatically when download finishes."];
  272:             [toolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenNO"]];
  273:             [toolbarItem setTarget:self];
  274:             [toolbarItem setAction:@selector(toggleLeaveOpen)];
  275:         }
  276:         leaveOpenToggleToolbarItem = toolbarItem; //establish reference
  277:     } else {
  278:         toolbarItem = nil;
  279:     }
  280: 
  281:     return toolbarItem;
  282: }
  283: 
  284: -(void)cancel
  285: {
  286:     NSLog(@"Request to cancel download.");
  287:     //Put logic here!
  288: }
  289: 
  290: -(void)pauseAndResumeDownload
  291: {
  292:     if ( ! mDownloadIsPaused ) {
  293:         //Do logic to pause download
  294:         mDownloadIsPaused = YES;
  295:         [pauseResumeToggleToolbarItem setLabel:@"Resume"];
  296:         [pauseResumeToggleToolbarItem setPaletteLabel:@"Resume Download"];
  297:         [pauseResumeToggleToolbarItem setToolTip:@"Resume the paused FTP download"];
  298:         [pauseResumeToggleToolbarItem setImage:[NSImage imageNamed:@"saveResume"]];
  299:     } else {
  300:         //Do logic to resume download
  301:         mDownloadIsPaused = NO;
  302:         [pauseResumeToggleToolbarItem setLabel:@"Pause"];
  303:         [pauseResumeToggleToolbarItem setPaletteLabel:@"Pause Download"];
  304:         [pauseResumeToggleToolbarItem setToolTip:@"Pause this FTP file download"];
  305:         [pauseResumeToggleToolbarItem setImage:[NSImage imageNamed:@"savePause"]];
  306:     }
  307: }
  308: 
  309: -(void)showFile
  310: {
  311:     NSLog(@"Request to show file.");
  312: }
  313: 
  314: -(void)openFile
  315: {
  316:     NSLog(@"Request to open file.");
  317: }
  318: 
  319: -(void)toggleLeaveOpen
  320: {
  321:     if ( ! mSaveFileDialogShouldStayOpen ) {
  322:         //Do logic to set the preference and make it take effect
  323:         mSaveFileDialogShouldStayOpen = YES;
  324:         [leaveOpenToggleToolbarItem setLabel:@"Leave Open"];
  325:         [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];
  326:         [leaveOpenToggleToolbarItem setToolTip:@"Window will stay open when download finishes."];
  327:         [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenYES"]];
  328:     } else {
  329:         //Do logic to set the preference and make it take effect
  330:         mSaveFileDialogShouldStayOpen = NO;
  331:         [leaveOpenToggleToolbarItem setLabel:@"Close When Done"];
  332:         [leaveOpenToggleToolbarItem setPaletteLabel:@"Toggle Close Behavior"];
  333:         [leaveOpenToggleToolbarItem setToolTip:@"Window will close automatically when download finishes."];
  334:         [leaveOpenToggleToolbarItem setImage:[NSImage imageNamed:@"saveLeaveOpenNO"]];
  335:     }
  336: }
  337: 
  338: @end

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