This repository was archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 208
Add download support for MacGap #100
Open
steedos
wants to merge
1
commit into
MacGapProject:master
Choose a base branch
from
steedos:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
#import <Cocoa/Cocoa.h> | ||
#import <WebKit/WebKit.h> | ||
#import "DownloadDelegate.h" | ||
|
||
@class WebViewDelegate; | ||
|
||
@interface ContentView : NSView { | ||
IBOutlet WebView* webView; | ||
WebViewDelegate* delegate; | ||
DownloadDelegate* downloadDelegate; | ||
} | ||
|
||
@property (retain) WebView* webView; | ||
@property (retain) WebViewDelegate* delegate; | ||
@property (retain) DownloadDelegate* downloadDelegate; | ||
@property (strong) IBOutlet NSMenu *mainMenu; | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// DownloadDelegate.h | ||
// MacGap | ||
// | ||
// Created by Jack Zhuang on 4/11/2014. | ||
// Copyright (c) 2014 SteedOS Inc. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface DownloadDelegate : NSObject { | ||
NSString *suggestedFilename; | ||
|
||
NSSavePanel *panel; | ||
} | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// DownloadDelegate.m | ||
// MacGap | ||
// | ||
// Created by Jack Zhuang on 4/11/2014. | ||
// Copyright (c) 2014 SteedOS Inc. All rights reserved. | ||
// | ||
|
||
#import "DownloadDelegate.h" | ||
#import "DownloadInfo.h" | ||
|
||
|
||
@implementation DownloadDelegate { | ||
NSMapTable *downloads; | ||
} | ||
|
||
- (id)init | ||
{ | ||
self = [super init]; | ||
if (self && NSClassFromString(@"NSProgress")) { | ||
downloads = [NSMapTable weakToStrongObjectsMapTable]; | ||
} | ||
return self; | ||
} | ||
|
||
|
||
- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename | ||
{ | ||
NSString *downloadDir = [NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | ||
NSString *destinationFilename = [downloadDir stringByAppendingPathComponent:filename]; | ||
[download setDestination:destinationFilename allowOverwrite:NO]; | ||
} | ||
|
||
-(void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path | ||
{ | ||
if (downloads) { | ||
NSURL *downloadUrl = [NSURL fileURLWithPath:path]; | ||
DownloadInfo *info = [downloads objectForKey:download]; | ||
[info setFilename:path]; | ||
|
||
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: | ||
NSProgressFileOperationKindDownloading, | ||
NSProgressFileOperationKindKey, | ||
nil]; | ||
NSProgress *progress = [NSProgress progressWithTotalUnitCount:0]; | ||
progress = [progress initWithParent:nil userInfo:userInfo]; | ||
[progress setKind:NSProgressKindFile]; | ||
[progress setUserInfoObject:downloadUrl forKey:NSProgressFileURLKey]; | ||
[progress publish]; | ||
[info setProgress:progress]; | ||
} | ||
} | ||
|
||
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response | ||
{ | ||
if (downloads) { | ||
DownloadInfo *info = [[DownloadInfo alloc] init]; | ||
[info setResponse:response]; | ||
[downloads setObject:info forKey:download]; | ||
} | ||
} | ||
|
||
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(unsigned)length { | ||
if (downloads) { | ||
DownloadInfo *info = [downloads objectForKey:download]; | ||
long long expectedLength = [[info response] expectedContentLength]; | ||
int64_t completed = [[info progress] completedUnitCount]; | ||
[[info progress] setCompletedUnitCount:(completed + length)]; | ||
if (expectedLength != NSURLResponseUnknownLength) [[info progress] setTotalUnitCount:expectedLength]; | ||
} | ||
} | ||
|
||
- (void)downloadDidFinish:(NSURLDownload *)download { | ||
if (downloads) { | ||
DownloadInfo *info = [downloads objectForKey:download]; | ||
[[info progress] unpublish]; | ||
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"com.apple.DownloadFileFinished" object:[info filename]]; | ||
[downloads removeObjectForKey:download]; | ||
} | ||
} | ||
|
||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// A wrapper class for storing all the information relevant to a particular download. | ||
// | ||
// Created by Jack Zhuang on 4/11/2014. | ||
// Copyright (c) 2014 SteedOS Inc. All rights reserved. | ||
// | ||
|
||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface DownloadInfo : NSObject<NSCopying> | ||
|
||
@property NSProgress *progress; | ||
@property NSURLResponse *response; | ||
@property NSString *filename; | ||
|
||
- (id)copyWithZone:(NSZone *)zone; | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Created by Jack Zhuang on 4/11/2014. | ||
// Copyright (c) 2014 SteedOS Inc. All rights reserved. | ||
// | ||
|
||
|
||
#import "DownloadInfo.h" | ||
|
||
@implementation DownloadInfo | ||
|
||
- (id)copyWithZone:(NSZone *)zone | ||
{ | ||
DownloadInfo *copy = [[[self class] alloc] init]; | ||
|
||
if (copy) { | ||
copy.progress = self.progress; | ||
copy.response = self.response; | ||
copy.filename = self.filename; | ||
} | ||
|
||
return copy; | ||
} | ||
|
||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,7 @@ - (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary | |
switch (tag) | ||
{ | ||
case WebMenuItemTagOpenLinkInNewWindow: | ||
case WebMenuItemTagDownloadLinkToDisk: | ||
//case WebMenuItemTagDownloadLinkToDisk: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to remove commented line. |
||
case WebMenuItemTagCopyLinkToClipboard: | ||
case WebMenuItemTagOpenImageInNewWindow: | ||
case WebMenuItemTagDownloadImageToDisk: | ||
|
@@ -196,4 +196,25 @@ + (BOOL) isKeyExcludedFromWebScript:(const char*)name | |
} | ||
|
||
|
||
/* | ||
* Download everything except HTML pages. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason this needs to be mandatory for all use-cases? |
||
*/ | ||
- (void)webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type | ||
request:(NSURLRequest *)request | ||
frame:(WebFrame *)frame | ||
decisionListener:(id < WebPolicyDecisionListener >)listener | ||
{ | ||
NSLog (@"%@", type); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to remove NSLog. |
||
if([type isEqualToString:@"text/html"]) | ||
{ | ||
|
||
[listener use]; | ||
} | ||
else{ | ||
[listener download]; | ||
}; | ||
} | ||
|
||
|
||
|
||
@end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to see some use of our JSEventHelper's triggerEvent — I think there are actually several places in your code where it could be really handy to fire off some JS events.