Annotation of chimera/ProgressDlgController.mm, revision 1.6

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

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